• 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 class PrintSystemData {
35 public:
36     bool Init();
37     void InsertAddedPrinter(const std::string &printerId, const PrinterInfo &printerInfo);
38     void SavePrinterFile(const std::string &printerId);
39     std::string QueryPrinterIdByStandardizeName(const std::string &printerName);
40     bool QueryAddedPrinterInfoByPrinterId(const std::string &printerId, PrinterInfo &printer);
41     bool IsPrinterAdded(const std::string &printerId);
42     bool GetPrinterCapabilityFromSystemData(
43         PrinterInfo &printer, std::string printerId, PrinterCapability &printerCapability);
44     void DeleteAddedPrinter(const std::string &printerId, const std::string &printerName);
45     void GetAddedPrinterListFromSystemData(std::vector<std::string> &printerNameList);
46     void UpdatePrinterStatus(const std::string &printerId, PrinterStatus printerStatus);
47     bool UpdatePrinterAlias(const std::string &printerId, const std::string &printerAlias);
48     void UpdatePrinterUri(const std::shared_ptr<PrinterInfo> &printerInfo);
49     void UpdatePrinterPreferences(const std::string &printerId, const PrinterPreferences &preferences);
50     void QueryPrinterInfoById(const std::string &printerId, PrinterInfo &printerInfo);
51     bool QueryPpdHashCodeByPrinterName(const std::string &standardPrinterName, std::string &ppdHashCode);
52     bool CheckPrinterBusy(const std::string &printerId);
53     bool GetAllPrintUser(std::vector<int32_t> &allPrintUserList);
54 
55     std::vector<std::string> QueryAddedPrinterIdList();
56     std::vector<std::string> QueryAddedPrintersByIp(const std::string &printerIp);
57     std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &printerId);
58     std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoByName(const std::string &printerName);
59     void AddPrinterToDiscovery(std::shared_ptr<PrinterInfo> printerInfo);
60     void RemovePrinterFromDiscovery(const std::string &printerId);
61     std::map<std::string, std::shared_ptr<PrinterInfo>> GetDiscoveredPrinterInfo();
62     size_t GetDiscoveredPrinterCount();
63     void ClearDiscoveredPrinterList();
64     void AddIpPrinterToList(std::shared_ptr<PrinterInfo> printerInfo);
65     void RemoveIpPrinterFromList(const std::string &printerId);
66     std::shared_ptr<PrinterInfo> QueryIpPrinterInfoById(const std::string &printerId);
67     int32_t BuildPrinterPreference(const PrinterCapability &cap, PrinterPreferences &printPreferences);
68     bool CheckPrinterVersionFile();
69 
70 private:
71     bool ParsePrinterListJsonV1(Json::Value& jsonObject);
72     bool GetJsonObjectFromFile(Json::Value &jsonObject, const std::string &fileName);
73     void ConvertPrinterCapabilityToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
74     void ConvertPrintMarginToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
75     void ConvertPageSizeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
76     void ConvertPrintResolutionToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
77     void ConvertSupportedColorModeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
78     void ConvertSupportedDuplexModeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
79     void ConvertSupportedMediaTypeToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
80     void ConvertSupportedQualityToJson(PrinterCapability &printerCapability, Json::Value &capsJson);
81     bool ConvertJsonToPrinterCapability(Json::Value &capsJson, PrinterCapability &printerCapability);
82     bool ConvertJsonToPrintMargin(Json::Value &capsJson, PrinterCapability &printerCapability);
83     bool ConvertJsonToPageSize(Json::Value &capsJson, PrinterCapability &printerCapability);
84     bool ConvertJsonToPrintResolution(Json::Value &capsJson, PrinterCapability &printerCapability);
85     bool ConvertJsonToSupportedColorMode(Json::Value &capsJson, PrinterCapability &printerCapability);
86     bool ConvertJsonToSupportedDuplexMode(Json::Value &capsJson, PrinterCapability &printerCapability);
87     bool ConvertJsonToSupportedMediaType(Json::Value &capsJson, PrinterCapability &printerCapability);
88     bool ConvertJsonToSupportedQuality(Json::Value &capsJson, PrinterCapability &printerCapability);
89     bool ConvertJsonToSupportedOrientation(Json::Value &capsJson, PrinterCapability &printerCapability);
90     bool GetPrinterCapabilityFromFile(std::string printerId, PrinterCapability &printerCapability);
91     bool CheckPrinterInfoJson(Json::Value &object, std::string &printerId);
92     bool GetPrinterCapabilityFromJson(
93         std::string printerId, Json::Value &jsonObject, PrinterCapability &printerCapability);
94     bool ParseUserListJsonV1(
95         Json::Value &jsonObject, std::vector<int32_t> &allPrintUserList);
96     bool ConvertJsonToPrinterInfo(Json::Value &object);
97     void ConvertInnerJsonToPrinterInfo(Json::Value &object, PrinterInfo &info);
98 
99     bool ParsePreviousPreferencesSetting(Json::Value &settingJson, PrinterPreferences &preferences);
100     bool ParsePrinterPreferencesJson(Json::Value &jsonObject);
101     bool ReadJsonFile(const std::filesystem::path &path);
102     std::string ParseDefaultPageSizeId(const PrinterCapability &cap);
103     void DeleteFile(const std::filesystem::path &path);
104     void BuildPrinterPreferenceByDefault(Json::Value &capOpt, PrinterPreferences &printPreferences);
105     void BuildPrinterPreferenceBySupport(const PrinterCapability &cap, PrinterPreferences &printPreferences);
106     Json::Value GetCupsOptionsJson(const PrinterCapability &cap);
107     void SaveJsonFile(const std::string &fileName, const std::string &jsonString);
108     PrintMapSafe<PrinterInfo>& GetAddedPrinterMap();
109     const std::string& GetPrintersPath();
110 
111     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)112     bool ProcessJsonToCapabilityList(Json::Value &capsJson,
113                                      const std::string &key,
114                                      PrinterCapability &printerCapability,
115                                      void (PrinterCapability::*setter)(const std::vector <T> &),
116                                      std::function<bool(const Json::Value &, T &)> converter)
117     {
118         if (!PrintJsonUtil::IsMember(capsJson, key) || !capsJson[key].isArray()) {
119             PRINT_HILOGW("Cannot find %{public}s or it's not an array", key.c_str());
120             return true;
121         }
122         PRINT_HILOGD("find Capability %{public}s success", key.c_str());
123         std::vector<T> resultList;
124         for (const auto &item: capsJson[key]) {
125             if (!PrintUtils::CheckJsonType<T>(item)) {
126                 PRINT_HILOGE("%{public}s item has incorrect type", key.c_str());
127                 return false;
128             }
129             T object;
130             bool ret = converter(item, object);
131             if (!ret) {
132                 PRINT_HILOGE("Invalid format,key is %{public}s", key.c_str());
133                 return false;
134             }
135             resultList.push_back(object);
136         }
137         if (!resultList.empty()) {
138             (printerCapability.*setter)(resultList);
139         }
140         PRINT_HILOGD("processCapabilityList success, %{public}s", key.c_str());
141         return true;
142     }
143 
144     template <typename T>
GetPreferencesFromSupport(const std::vector<T> & vec,const T & defaultValue)145     std::optional<T> GetPreferencesFromSupport(const std::vector<T>& vec, const T& defaultValue)
146     {
147         if (vec.size() == 0) {
148             return std::nullopt;
149         }
150 
151         for (const auto& elem : vec) {
152             if (elem == defaultValue) {
153                 return elem;
154             }
155         }
156         return vec[0];
157     }
158 
159 private:
160     PrintMapSafe<PrinterInfo> addedPrinterMap_;
161 #ifdef ENTERPRISE_ENABLE
162     PrintMapSafe<PrinterInfo> addedPrinterEnterpriseMap_;
163 #endif // ENTERPRISE_ENABLE
164     std::map<std::string, std::shared_ptr<PrinterInfo>> discoveredPrinterInfoList_;
165     std::map<std::string, std::shared_ptr<PrinterInfo>> connectingIpPrinterInfoList_;
166     std::mutex discoveredListMutex;
167     std::mutex connectingIpPrinterListMutex;
168     const uint32_t MAX_PRINTER_SIZE = 1000;
169 };
170 
171 }  // namespace Print
172 }  // namespace OHOS
173 #endif  // PRINT_SYSTEM_DATA_H
174