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 <string> 20 #include <map> 21 #include <deque> 22 #include <mutex> 23 24 #include "printer_info.h" 25 #include "iprint_callback.h" 26 27 namespace OHOS { 28 namespace Print { 29 30 using namespace std; 31 32 struct JobIdCmp { operatorJobIdCmp33 bool operator()(const std::string a, const std::string b) const 34 { 35 return atoi(a.c_str()) > atoi(b.c_str()); 36 } 37 }; 38 39 const uint32_t DELETE_DEFAULT_PRINTER = 101; 40 const uint32_t DELETE_LAST_USED_PRINTER = 102; 41 42 class PrintUserData { 43 public: 44 void RegisterPrinterCallback(const std::string &type, const sptr<IPrintCallback> &listener); 45 void UnregisterPrinterCallback(const std::string &type); 46 void SendPrinterEvent(const std::string &type, int event, const PrinterInfo &info); 47 void AddToPrintJobList(const std::string jobId, const std::shared_ptr<PrintJob> &printjob); 48 void UpdateQueuedJobList( 49 const std::string &jobId, const std::shared_ptr<PrintJob> &printJob, std::string jobOrderId); 50 int32_t QueryPrintJobById(std::string &printJobId, PrintJob &printJob); 51 int32_t QueryAllPrintJob(std::vector<PrintJob> &printJobs); 52 int32_t SetDefaultPrinter(const std::string &printerId, uint32_t type); 53 int32_t SetLastUsedPrinter(const std::string &printerId); 54 void ParseUserData(); 55 void SetUserId(int32_t userId); 56 std::string GetLastUsedPrinter(); 57 std::string GetDefaultPrinter(); 58 bool CheckIfUseLastUsedPrinterForDefault(); 59 void DeletePrinter(const std::string &printerId); 60 61 private: 62 bool SetUserDataToFile(); 63 bool GetFileData(std::string &fileData); 64 void ParseUserDataFromJson(nlohmann::json &jsonObject); 65 bool CheckFileData(std::string &fileData, nlohmann::json &jsonObject); 66 bool ConvertJsonToUsedPrinterList(nlohmann::json &userData); 67 void ConvertUsedPrinterListToJson(nlohmann::json &usedPrinterListJson); 68 void DeletePrinterFromUsedPrinterList(const std::string &printerId); 69 70 public: 71 std::map<std::string, sptr<IPrintCallback>> registeredListeners_; 72 std::map<std::string, std::shared_ptr<PrintJob>> printJobList_; 73 std::map<std::string, std::shared_ptr<PrintJob>> queuedJobList_; 74 std::map<std::string, std::string, JobIdCmp> jobOrderList_; 75 76 private: 77 std::string defaultPrinterId_ = ""; 78 std::string lastUsedPrinterId_ = ""; 79 int32_t userId_ = 0; 80 bool useLastUsedPrinterForDefault_ = true; 81 deque<std::string> usedPrinterList_; 82 std::recursive_mutex userDataMutex_; 83 }; 84 85 } // namespace Print 86 } // namespace OHOS 87 #endif // PRINT_USER_DATA_H 88