• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PRINT_SYSTEM_DATA_H
17 #define PRINT_SYSTEM_DATA_H
18 
19 #include <string>
20 #include <map>
21 #include <vector>
22 #include <json/json.h>
23 #include "printer_info.h"
24 #include "printer_capability.h"
25 #include "print_constant.h"
26 #include "print_utils.h"
27 #include "print_log.h"
28 #include "print_map_safe.h"
29 #include "printer_preferences.h"
30 
31 namespace OHOS {
32 namespace Print {
33 
34 struct CupsPrinterInfo {
35     std::string name;
36     std::string uri;
37     std::string maker;
38     PrinterCapability printerCapability;
39     PrinterStatus printerStatus = PRINTER_STATUS_UNAVAILABLE;
40     std::string alias;
41     PrinterPreferences printPreferences;
42 };
43 
44 class PrintSystemData {
45 public:
46     bool Init();
47     void InsertCupsPrinter(const std::string &printerId, const CupsPrinterInfo &printerInfo);
48     void SavePrinterFile(const std::string &printerId);
49     std::string QueryPrinterIdByStandardizeName(const std::string &printerName);
50     bool QueryCupsPrinterInfoByPrinterId(const std::string &printerId, CupsPrinterInfo &cupsPrinter);
51     void InsertPrinterInfo(const std::string &printerId, const PrinterInfo &printerInfo);
52     std::shared_ptr<PrinterInfo> QueryPrinterInfoByPrinterId(const std::string &printerId);
53     bool IsPrinterAdded(const std::string &printerId);
54     bool GetPrinterCapabilityFromSystemData(
55         CupsPrinterInfo &cupsPrinter, std::string printerId, PrinterCapability &printerCapability);
56     void DeleteCupsPrinter(const std::string &printerId, const std::string &printerName);
57     void GetAddedPrinterListFromSystemData(std::vector<std::string> &printerNameList);
58     void UpdatePrinterStatus(const std::string &printerId, PrinterStatus printerStatus);
59     bool UpdatePrinterAlias(const std::string& printerId, const std::string& printerAlias);
60     void UpdatePrinterUri(const std::shared_ptr<PrinterInfo> &printerInfo);
61     void UpdatePrinterPreferences(const std::string &printerId, const PrinterPreferences &preferences);
62     void QueryPrinterInfoById(const std::string &printerId, PrinterInfo &printerInfo);
63     bool CheckPrinterBusy(const std::string &printerId);
64     bool GetAllPrintUser(std::vector<int32_t> &allPrintUserList);
65 
66     std::vector<std::string> QueryAddedPrinterIdList();
67     std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &printerId);
68     std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoByName(const std::string &printerName);
69     void AddPrinterToDiscovery(std::shared_ptr<PrinterInfo> printerInfo);
70     void RemovePrinterFromDiscovery(const std::string &printerId);
71     std::map<std::string, std::shared_ptr<PrinterInfo>> GetDiscoveredPrinterInfo();
72     size_t GetDiscoveredPrinterCount();
73     void ClearDiscoveredPrinterList();
74 
75     void AddIpPrinterToList(std::shared_ptr<PrinterInfo> printerInfo);
76     void RemoveIpPrinterFromList(const std::string &printerId);
77     std::shared_ptr<PrinterInfo> QueryIpPrinterInfoById(const std::string &printerId);
78     int32_t BuildPrinterPreference(const PrinterCapability &cap, PrinterPreferences &printPreferences);
79     void BuildEprintPreference(const PrinterCapability &cap, PrinterPreferences &printPreferences);
80 
81 private:
82     bool ParsePrinterListJsonV1(Json::Value& jsonObject);
83     bool GetJsonObjectFromFile(Json::Value &jsonObject, const std::string &fileName);
84     void ConvertPrinterCapabilityToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
85     void ConvertPrintMarginToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
86     void ConvertPageSizeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
87     void ConvertPrintResolutionToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
88     void ConvertSupportedColorModeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
89     void ConvertSupportedDuplexModeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
90     void ConvertSupportedMediaTypeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
91     void ConvertSupportedQualityToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
92     bool ConvertJsonToPrinterCapability(Json::Value &capsJson, PrinterCapability &printerCapability);
93     bool ConvertJsonToPrintMargin(Json::Value &capsJson, PrinterCapability &printerCapability);
94     bool ConvertJsonToPageSize(Json::Value &capsJson, PrinterCapability &printerCapability);
95     bool ConvertJsonToPrintResolution(Json::Value &capsJson, PrinterCapability &printerCapability);
96     bool ConvertJsonToSupportedColorMode(Json::Value &capsJson, PrinterCapability &printerCapability);
97     bool ConvertJsonToSupportedDuplexMode(Json::Value &capsJson, PrinterCapability &printerCapability);
98     bool ConvertJsonToSupportedMediaType(Json::Value &capsJson, PrinterCapability &printerCapability);
99     bool ConvertJsonToSupportedQuality(Json::Value &capsJson, PrinterCapability &printerCapability);
100     bool ConvertJsonToSupportedOrientation(Json::Value &capsJson, PrinterCapability &printerCapability);
101     bool GetPrinterCapabilityFromFile(std::string printerId, PrinterCapability &printerCapability);
102     bool CheckPrinterInfoJson(Json::Value &object, std::string &printerId);
103     bool GetPrinterCapabilityFromJson(
104         std::string printerId, Json::Value &jsonObject, PrinterCapability &printerCapability);
105     bool ParseUserListJsonV1(
106         Json::Value &jsonObject, std::vector<int32_t> &allPrintUserList);
107     bool ConvertJsonToCupsPrinterInfo(Json::Value &object);
108     void ConvertInnerJsonToCupsPrinterInfo(Json::Value &object, CupsPrinterInfo &info);
109 
110     bool ParsePreviousPreferencesSetting(Json::Value &settingJson, PrinterPreferences &preferences);
111     bool ParsePrinterPreferencesJson(Json::Value &jsonObject);
112     bool ReadJsonFile(const std::filesystem::path &path);
113     std::string ParseDefaultPageSizeId(const PrinterCapability &cap, Json::Value &capOpt);
114     int32_t ParseDefaultOrientation(const PrinterCapability &cap, Json::Value &capOpt);
115     int32_t ParseDefaultDuplexMode(const PrinterCapability &cap, Json::Value &capOpt);
116     int32_t ParseDefaultPrintQuality(const PrinterCapability &cap, Json::Value &capOpt);
117     std::string ParseDefaultMediaType(const PrinterCapability &cap, Json::Value &capOpt);
118     void DeleteFile(const std::filesystem::path &path);
119 
120     template<typename T>
ProcessJsonToCapabilityList(Json::Value & capsJson,const std::string & key,PrinterCapability & printerCapability,void (PrinterCapability::* setter)(const std::vector<T> &),std::function<bool (const Json::Value &,T &)> converter)121     bool ProcessJsonToCapabilityList(Json::Value &capsJson,
122                                      const std::string &key,
123                                      PrinterCapability &printerCapability,
124                                      void (PrinterCapability::*setter)(const std::vector <T> &),
125                                      std::function<bool(const Json::Value &, T &)> converter)
126     {
127         if (!PrintJsonUtil::IsMember(capsJson, key) || !capsJson[key].isArray()) {
128             PRINT_HILOGW("Cannot find %{public}s or it's not an array", key.c_str());
129             return true;
130         }
131         PRINT_HILOGD("find Capability %{public}s success", key.c_str());
132         std::vector<T> resultList;
133         for (const auto &item: capsJson[key]) {
134             if (!PrintUtils::CheckJsonType<T>(item)) {
135                 PRINT_HILOGE("%{public}s item has incorrect type", key.c_str());
136                 return false;
137             }
138             T object;
139             bool ret = converter(item, object);
140             if (!ret) {
141                 PRINT_HILOGE("Invalid format,key is %{public}s", key.c_str());
142                 return false;
143             }
144             resultList.push_back(object);
145         }
146         if (!resultList.empty()) {
147             (printerCapability.*setter)(resultList);
148         }
149         PRINT_HILOGD("processCapabilityList success, %{public}s", key.c_str());
150         return true;
151     }
152 
153 private:
154     PrintMapSafe<CupsPrinterInfo> addedPrinterMap_;
155     std::map<std::string, std::shared_ptr<PrinterInfo>> addedPrinterInfoList_;
156     std::map<std::string, std::shared_ptr<PrinterInfo>> discoveredPrinterInfoList_;
157     std::map<std::string, std::shared_ptr<PrinterInfo>> connectingIpPrinterInfoList_;
158     std::mutex discoveredListMutex;
159     std::mutex connectingIpPrinterListMutex;
160     const uint32_t MAX_PRINTER_SIZE = 1000;
161 };
162 
163 }  // namespace Print
164 }  // namespace OHOS
165 #endif  // PRINT_SYSTEM_DATA_H
166