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 #include "vendor_ppd_driver.h"
17 #include "print_log.h"
18
19 using namespace OHOS::Print;
20
VendorPpdDriver()21 VendorPpdDriver::VendorPpdDriver() {}
22
~VendorPpdDriver()23 VendorPpdDriver::~VendorPpdDriver() {}
24
OnQueryCapability(const std::string & printerId,int timeout)25 bool VendorPpdDriver::OnQueryCapability(const std::string &printerId, int timeout)
26 {
27 PRINT_HILOGI("OnQueryCapability enter.");
28 PRINT_HILOGD("OnQueryCapability printerId=%{public}s", printerId.c_str());
29 std::vector<std::string> ppds;
30 if (!QueryPpdByPrinterId(printerId, ppds)) {
31 PRINT_HILOGW("OnQueryCapability query ppd fail. printerId = %{public}s", printerId.c_str());
32 return false;
33 }
34 if (!AddPrinterToCups(printerId, ppds[0])) {
35 PRINT_HILOGW("OnQueryCapability add printer fail.");
36 return false;
37 }
38 std::shared_ptr<PrinterInfo> printerInfo = QueryPrinterCapabilityFromPpd(printerId, ppds[0]);
39 if (printerInfo == nullptr) {
40 PRINT_HILOGW("get printerInfo failed.");
41 return false;
42 }
43 printerInfo->SetPrinterId(printerId);
44 if (!UpdateCapability(printerInfo)) {
45 PRINT_HILOGW("OnQueryCapability update capability fail.");
46 return false;
47 }
48 PRINT_HILOGI("OnQueryCapability success.");
49 return true;
50 }
51
OnQueryProperties(const std::string & printerId,const std::vector<std::string> & propertyKeys)52 bool VendorPpdDriver::OnQueryProperties(const std::string &printerId, const std::vector<std::string> &propertyKeys)
53 {
54 std::vector<std::string> ppds;
55 if (propertyKeys.empty()) {
56 return false;
57 }
58 if (vendorManager == nullptr) {
59 PRINT_HILOGW("OnQueryProperties vendorManager is null.");
60 return false;
61 }
62 if (!vendorManager->QueryPPDInformation(propertyKeys[0].c_str(), ppds)) {
63 PRINT_HILOGD("OnQueryProperties query ppd fail. printerId = %{public}s, printerMake = %{public}s",
64 printerId.c_str(), propertyKeys[0].c_str());
65 return false;
66 }
67 PRINT_HILOGD("OnQueryProperties ppd=%{public}s", ppds[0].c_str());
68 auto iter = privatePrinterPpdMap.find(printerId);
69 if (iter != privatePrinterPpdMap.end()) {
70 privatePrinterPpdMap.erase(iter);
71 }
72 privatePrinterPpdMap.insert(std::make_pair(printerId, std::vector(ppds)));
73 return true;
74 }
75
GetVendorName()76 std::string VendorPpdDriver::GetVendorName()
77 {
78 return VENDOR_PPD_DRIVER;
79 }
80
QueryPpdByPrinterId(const std::string & printerId,std::vector<std::string> & ppds)81 bool VendorPpdDriver::QueryPpdByPrinterId(const std::string &printerId, std::vector<std::string> &ppds)
82 {
83 PRINT_HILOGD("QueryPpdByPrinterId enter.");
84 auto iter = privatePrinterPpdMap.find(printerId);
85 if (iter != privatePrinterPpdMap.end()) {
86 ppds = std::vector(iter->second);
87 PRINT_HILOGD("QueryPpdByPrinterId success.");
88 return true;
89 }
90 PRINT_HILOGW("QueryPpdByPrinterId fail.");
91 return false;
92 }
93
AddPrinterToCups(const std::string & printerId,const std::string & ppdData)94 bool VendorPpdDriver::AddPrinterToCups(const std::string &printerId, const std::string &ppdData)
95 {
96 PRINT_HILOGI("AddPrinterToCups enter.");
97 if (vendorManager == nullptr) {
98 PRINT_HILOGW("AddPrinterToCups vendorManager is null.");
99 return false;
100 }
101 std::shared_ptr<PrinterInfo> discoveredPrinterInfo = vendorManager->
102 QueryDiscoveredPrinterInfoById(GetVendorName(), printerId);
103 if (discoveredPrinterInfo == nullptr) {
104 PRINT_HILOGW("AddPrinterToCups query fail.");
105 return false;
106 }
107 auto printerInfo = *discoveredPrinterInfo;
108 PRINT_HILOGD("AddPrinterToCups uri=%{public}s, name=%{public}s", printerInfo.GetUri().c_str(),
109 printerInfo.GetPrinterName().c_str());
110 if (vendorManager->AddPrinterToCupsWithPpd(GetVendorName(), printerInfo.GetPrinterId(), ppdData) !=
111 EXTENSION_ERROR_NONE) {
112 PRINT_HILOGW("AddPrinterToCups add printer fail.");
113 return false;
114 }
115 PRINT_HILOGI("AddPrinterToCups success.");
116 return true;
117 }
118
QueryPrinterCapabilityFromPpd(const std::string & printerId,const std::string & ppdData)119 std::shared_ptr<PrinterInfo> VendorPpdDriver::QueryPrinterCapabilityFromPpd(const std::string &printerId,
120 const std::string &ppdData)
121 {
122 PRINT_HILOGD("QueryPrinterCapabilityFromPpd enter.");
123 if (vendorManager == nullptr) {
124 PRINT_HILOGW("QueryPrinterCapabilityFromPpd vendorManager is null.");
125 return nullptr;
126 }
127 std::shared_ptr<PrinterInfo> printerInfo = std::make_shared<PrinterInfo>();
128 PrinterInfo info;
129 if (vendorManager->QueryPrinterInfoByPrinterId(GetVendorName(), printerId, info) != E_PRINT_NONE) {
130 return nullptr;
131 }
132 *printerInfo = info;
133 return printerInfo;
134 }
135
UpdateCapability(std::shared_ptr<PrinterInfo> printerInfo)136 bool VendorPpdDriver::UpdateCapability(std::shared_ptr<PrinterInfo> printerInfo)
137 {
138 PRINT_HILOGI("UpdateCapability enter.");
139 if (vendorManager == nullptr) {
140 PRINT_HILOGW("UpdateCapability vendorManager is null.");
141 return false;
142 }
143 if (printerInfo == nullptr) {
144 PRINT_HILOGW("printerInfo fail");
145 return false;
146 }
147 PRINT_HILOGI("UpdateCapability valid printerInfo.");
148 if (vendorManager->UpdatePrinterToDiscovery(GetVendorName(), *printerInfo) != EXTENSION_ERROR_NONE) {
149 PRINT_HILOGW("UpdateCapability UpdatePrinterToDiscovery fail.");
150 return false;
151 }
152 PRINT_HILOGI("UpdateCapability success.");
153 return true;
154 }