• 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 <nlohmann/json.hpp>
23 #include "printer_info.h"
24 #include "printer_capability.h"
25 #include "print_constant.h"
26 
27 namespace OHOS {
28 namespace Print {
29 
30 struct CupsPrinterInfo {
31     std::string name;
32     std::string uri;
33     std::string maker;
34     PrinterCapability printerCapability;
35     PrinterStatus printerStatus = PRINTER_STATUS_UNAVAILABLE;
36     std::string alias;
37 };
38 
39 class PrintSystemData {
40 public:
41     bool Init();
42     void InsertCupsPrinter(const std::string &printerId, const CupsPrinterInfo &printerInfo, bool needUpdateCaps);
43     bool SaveCupsPrinterMap();
44     std::string QueryPrinterIdByStandardizeName(const std::string &printerName);
45     bool QueryCupsPrinterInfoByPrinterId(const std::string &printerId, CupsPrinterInfo &cupsPrinter);
46     void InsertPrinterInfo(const std::string &printerId, const PrinterInfo &printerInfo);
47     std::shared_ptr<PrinterInfo> QueryPrinterInfoByPrinterId(const std::string &printerId);
48     bool IsPrinterAdded(const std::string &printerId);
49     bool GetPrinterCapabilityFromSystemData(
50         CupsPrinterInfo &cupsPrinter, std::string printerId, PrinterCapability &printerCapability);
51     void DeleteCupsPrinter(const std::string &printerId);
52     void GetAddedPrinterListFromSystemData(std::vector<std::string> &printerNameList);
53     void UpdatePrinterStatus(const std::string &printerId, PrinterStatus printerStatus);
54     bool UpdatePrinterAlias(const std::string& printerId, const std::string& printerAlias);
55     void QueryPrinterInfoById(const std::string &printerId, PrinterInfo &printerInfo);
56     bool CheckPrinterBusy(const std::string &printerId);
57     bool GetAllPrintUser(std::vector<int32_t> &allPrintUserList);
58 
59 private:
60     bool ParsePrinterListJsonV1(nlohmann::json& jsonObject);
61     bool GetJsonObjectFromFile(nlohmann::json &jsonObject, const std::string &fileName);
62     void ConvertPrinterCapabilityToJson(PrinterCapability &printerCapability, nlohmann::json &capsJson);
63     void ConvertPrintMarginToJson(PrinterCapability &printerCapability, nlohmann::json &capsJson);
64     void ConvertPageSizeToJson(PrinterCapability &printerCapability, nlohmann::json &capsJson);
65     void ConvertPrintResolutionToJson(PrinterCapability &printerCapability, nlohmann::json &capsJson);
66     bool ConvertJsonToPrinterCapability(nlohmann::json &capsJson, PrinterCapability &printerCapability);
67     void ConvertJsonToPrintMargin(nlohmann::json &capsJson, PrinterCapability &printerCapability);
68     bool ConvertJsonToPageSize(nlohmann::json &capsJson, PrinterCapability &printerCapability);
69     bool ConvertJsonToPrintResolution(nlohmann::json &capsJson, PrinterCapability &printerCapability);
70     bool GetPrinterCapabilityFromFile(std::string printerId, PrinterCapability &printerCapability);
71     bool CheckPrinterInfoJson(nlohmann::json &object, std::string &printerId);
72     bool GetPrinterCapabilityFromJson(
73         std::string printerId, nlohmann::json &jsonObject, PrinterCapability &printerCapability);
74     bool ParseUserListJsonV1(
75         nlohmann::json &jsonObject, std::vector<int32_t> &allPrintUserList);
76 
77 private:
78     std::map<std::string, std::shared_ptr<CupsPrinterInfo>> addedPrinterMap_;
79     std::map<uint32_t, std::string> addedPrinterOrderList_;
80     std::map<std::string, std::shared_ptr<PrinterInfo>> addedPrinterInfoList_;
81 };
82 
83 }  // namespace Print
84 }  // namespace OHOS
85 #endif  // PRINT_SYSTEM_DATA_H
86