• 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_USER_DATA_H
17 #define PRINT_USER_DATA_H
18 
19 #include <charconv>
20 #include <string>
21 #include <map>
22 #include <deque>
23 #include <mutex>
24 
25 #include "printer_info.h"
26 #include "print_util.h"
27 #include "iprint_callback.h"
28 
29 namespace OHOS {
30 namespace Print {
31 using namespace std;
32 struct JobIdCmp {
operatorJobIdCmp33     bool operator()(const std::string a, const std::string b) const
34     {
35         int32_t numA = 0;
36         int32_t numB = 0;
37         if (PrintUtil::ConvertToInt(a, numA) && PrintUtil::ConvertToInt(b, numB)) {
38             return numA > numB;
39         } else {
40             // If invalid, By default, the number A is greater than the number B.
41             return true;
42         }
43     }
44 };
45 
46 const uint32_t DELETE_DEFAULT_PRINTER = 101;
47 const uint32_t DELETE_LAST_USED_PRINTER = 102;
48 
49 class PrintUserData {
50 public:
51     void RegisterPrinterCallback(const std::string &type, const sptr<IPrintCallback> &listener);
52     void UnregisterPrinterCallback(const std::string &type);
53     void SendPrinterEvent(const std::string &type, int event, const PrinterInfo &info);
54     void AddToPrintJobList(const std::string jobId, const std::shared_ptr<PrintJob> &printjob);
55     void UpdateQueuedJobList(
56         const std::string &jobId, const std::shared_ptr<PrintJob> &printJob, std::string jobOrderId);
57     int32_t QueryPrintJobById(const std::string &printJobId, PrintJob &printJob);
58     int32_t QueryHistoryPrintJobById(const std::string &printJobId, PrintJob &printJob);
59     int32_t QueryAllActivePrintJob(std::vector<PrintJob> &printJobs);
60     int32_t QueryAllPrintJob(std::vector<std::string> printerIds, std::vector<PrintJob> &printJobs);
61     int32_t SetDefaultPrinter(const std::string &printerId, uint32_t type);
62     int32_t SetLastUsedPrinter(const std::string &printerId);
63     bool ParseUserData();
64     void SetUserId(int32_t userId);
65     std::string GetLastUsedPrinter();
66     std::string GetDefaultPrinter();
67     bool CheckIfUseLastUsedPrinterForDefault();
68     void DeletePrinter(const std::string &printerId);
69     bool FlushCacheFileToUserData(const std::string &jobId);
70     bool DeleteCacheFileFromUserData(const std::string &jobId);
71     bool OpenCacheFileFd(const std::string &jobId, std::vector<uint32_t> &fdList);
72     int32_t QueryQueuedPrintJobById(const std::string &printJobId, PrintJob &printJob);
73     bool AddPrintJobToHistoryList(const std::string &printerId, const std::string &jobId,
74         const std::shared_ptr<PrintJob> &printjob);
75     bool DeletePrintJobFromHistoryList(const std::string &jobId);
76     bool DeletePrintJobFromHistoryListByPrinterId(const std::string &printerId);
77     bool ContainsHistoryPrintJob(const std::vector<std::string> &printerIds, const std::string &jobId);
78 
79 private:
80     bool SetUserDataToFile();
81     bool GetFileData(std::string &fileData);
82     void ParseUserDataFromJson(Json::Value &jsonObject);
83     bool CheckFileData(std::string &fileData, Json::Value &jsonObject);
84     bool ConvertJsonToUsedPrinterList(Json::Value &userData);
85     void ConvertUsedPrinterListToJson(Json::Value &usedPrinterListJson);
86     void DeletePrinterFromUsedPrinterList(const std::string &printerId);
87     std::string ObtainUserCacheDirectory();
88     bool FlushCacheFile(int32_t fd, const std::string jobId, uint32_t index);
89     void FlushPrintHistoryJobFile(const std::string &printerId);
90     std::string ParsePrintHistoryJobListToJsonString(const std::string &printerId);
91     bool GetPrintHistoryJobFromFile(const std::string &printerId);
92     bool GetJsonObjectFromFile(Json::Value &jsonObject, const std::string &filePath, const std::string &printerId);
93     bool ParseJsonObjectToPrintHistory(Json::Value &jsonObject, const std::string &printerId);
94     PrintMargin  ParseJsonObjectToMargin(const Json::Value &jsonObject);
95     PrintRange ParseJsonObjectToPrintRange(const Json::Value &jsonObject);
96     PrintPageSize ParseJsonObjectToPrintPageSize(const Json::Value &jsonObject);
97     PrintPreviewAttribute ParseJsonObjectToPrintPreviewAttribute(const Json::Value &jsonObject);
98     void ParseOptionalJsonObjectToPrintJob(
99         const Json::Value &printJobInfoJson, std::shared_ptr<PrintJob> &printHistoryJob);
100     bool ParseJsonObjectToPrintJob(const Json::Value &printJobInfoJson, std::shared_ptr<PrintJob> &printHistoryJob);
101     void InitPrintHistoryJobList(const std::string &printerId);
102     bool CheckOptionalParam(const Json::Value &jsonObject, const std::string &param);
103 
104 public:
105     std::map<std::string, sptr<IPrintCallback>> registeredListeners_;
106     std::map<std::string, std::shared_ptr<PrintJob>> printJobList_;
107     std::map<std::string, std::shared_ptr<PrintJob>> queuedJobList_;
108     std::map<std::string, std::string, JobIdCmp> jobOrderList_;
109 
110 private:
111     std::string defaultPrinterId_ = "";
112     std::string lastUsedPrinterId_ = "";
113     int32_t userId_ = 0;
114     bool useLastUsedPrinterForDefault_ = true;
115     deque<std::string> usedPrinterList_;
116     std::recursive_mutex userDataMutex_;
117     const uint32_t MAX_PRINTER_SIZE = 1000;
118     const uint32_t MAX_HISTORY_JOB_NUM = 500;
119     std::map<std::string, std::unique_ptr<std::map<std::string, std::shared_ptr<PrintJob>>>>
120         printHistoryJobList_;
121 };
122 
123 }  // namespace Print
124 }  // namespace OHOS
125 #endif  // PRINT_USER_DATA_H
126