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(std::string &printJobId, PrintJob &printJob); 58 int32_t QueryAllPrintJob(std::vector<PrintJob> &printJobs); 59 int32_t SetDefaultPrinter(const std::string &printerId, uint32_t type); 60 int32_t SetLastUsedPrinter(const std::string &printerId); 61 void ParseUserData(); 62 void SetUserId(int32_t userId); 63 std::string GetLastUsedPrinter(); 64 std::string GetDefaultPrinter(); 65 bool CheckIfUseLastUsedPrinterForDefault(); 66 void DeletePrinter(const std::string &printerId); 67 68 private: 69 bool SetUserDataToFile(); 70 bool GetFileData(std::string &fileData); 71 void ParseUserDataFromJson(nlohmann::json &jsonObject); 72 bool CheckFileData(std::string &fileData, nlohmann::json &jsonObject); 73 bool ConvertJsonToUsedPrinterList(nlohmann::json &userData); 74 void ConvertUsedPrinterListToJson(nlohmann::json &usedPrinterListJson); 75 void DeletePrinterFromUsedPrinterList(const std::string &printerId); 76 77 public: 78 std::map<std::string, sptr<IPrintCallback>> registeredListeners_; 79 std::map<std::string, std::shared_ptr<PrintJob>> printJobList_; 80 std::map<std::string, std::shared_ptr<PrintJob>> queuedJobList_; 81 std::map<std::string, std::string, JobIdCmp> jobOrderList_; 82 83 private: 84 std::string defaultPrinterId_ = ""; 85 std::string lastUsedPrinterId_ = ""; 86 int32_t userId_ = 0; 87 bool useLastUsedPrinterForDefault_ = true; 88 deque<std::string> usedPrinterList_; 89 std::recursive_mutex userDataMutex_; 90 }; 91 92 } // namespace Print 93 } // namespace OHOS 94 #endif // PRINT_USER_DATA_H 95