1 /*
2  * Copyright (c) 2022 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 "napi/native_api.h"
17 #include "print_log.h"
18 #include "print_attributes_helper.h"
19 
20 namespace OHOS::Print {
21 static constexpr const char *PARAM_JOB_COPYNUMBER = "copyNumber";
22 static constexpr const char *PARAM_JOB_PAGERANGE = "pageRange";
23 static constexpr const char *PARAM_JOB_ISSEQUENTIAL = "isSequential";
24 static constexpr const char *PARAM_JOB_PAGESIZE = "pageSize";
25 static constexpr const char *PARAM_JOB_ISLANDSCAPE = "isLandscape";
26 static constexpr const char *PARAM_JOB_DIRECTIONMODE = "directionMode";
27 static constexpr const char *PARAM_JOB_COLORMODE = "colorMode";
28 static constexpr const char *PARAM_JOB_DUPLEXMODE = "duplexMode";
29 static constexpr const char *PARAM_JOB_MARGIN = "margin";
30 static constexpr const char *PARAM_JOB_OPTION = "options";
31 
MakeJsObject(napi_env env,const PrintAttributes & attributes)32 napi_value PrintAttributesHelper::MakeJsObject(napi_env env, const PrintAttributes &attributes)
33 {
34     napi_value jsObj = nullptr;
35     PRINT_CALL(env, napi_create_object(env, &jsObj));
36     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_JOB_COPYNUMBER, attributes.GetCopyNumber());
37 
38     if (!CreatePageRange(env, jsObj, attributes)) {
39         PRINT_HILOGE("Failed to create page range property of print job");
40         return nullptr;
41     }
42 
43     NapiPrintUtils::SetBooleanProperty(env, jsObj, PARAM_JOB_ISSEQUENTIAL, attributes.GetIsSequential());
44 
45     if (!CreatePageSize(env, jsObj, attributes)) {
46         PRINT_HILOGE("Failed to create page size property of print job");
47         return nullptr;
48     }
49 
50     NapiPrintUtils::SetBooleanProperty(env, jsObj, PARAM_JOB_ISLANDSCAPE, attributes.GetIsLandscape());
51     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_JOB_DIRECTIONMODE, attributes.GetDirectionMode());
52     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_JOB_COLORMODE, attributes.GetColorMode());
53     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_JOB_DUPLEXMODE, attributes.GetDuplexMode());
54 
55     if (!CreateMargin(env, jsObj, attributes)) {
56         PRINT_HILOGE("Failed to create margin property of print job");
57         return nullptr;
58     }
59 
60     if (attributes.HasOption()) {
61         NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_JOB_OPTION, attributes.GetOption());
62     }
63     return jsObj;
64 }
65 
BuildFromJs(napi_env env,napi_value jsValue)66 std::shared_ptr<PrintAttributes> PrintAttributesHelper::BuildFromJs(napi_env env, napi_value jsValue)
67 {
68     auto nativeObj = std::make_shared<PrintAttributes>();
69 
70     if (!ValidateProperty(env, jsValue)) {
71         PRINT_HILOGE("Invalid property of print job");
72         return nullptr;
73     }
74 
75     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_COPYNUMBER)) {
76         uint32_t copyNumber = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_JOB_COPYNUMBER);
77         nativeObj->SetCopyNumber(copyNumber);
78     }
79 
80     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_ISSEQUENTIAL)) {
81         bool isSequential = NapiPrintUtils::GetBooleanProperty(env, jsValue, PARAM_JOB_ISSEQUENTIAL);
82         nativeObj->SetIsSequential(isSequential);
83     }
84 
85     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_ISLANDSCAPE)) {
86         bool isLandscape = NapiPrintUtils::GetBooleanProperty(env, jsValue, PARAM_JOB_ISLANDSCAPE);
87         nativeObj->SetIsLandscape(isLandscape);
88     }
89 
90     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_DIRECTIONMODE)) {
91         uint32_t directionMode = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_JOB_DIRECTIONMODE);
92         nativeObj->SetDirectionMode(directionMode);
93     }
94 
95     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_COLORMODE)) {
96         uint32_t colorMode = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_JOB_COLORMODE);
97         nativeObj->SetColorMode(colorMode);
98     }
99 
100     if (NapiPrintUtils::HasNamedProperty(env, jsValue, PARAM_JOB_DUPLEXMODE)) {
101         uint32_t duplexMode = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_JOB_DUPLEXMODE);
102         nativeObj->SetDuplexMode(duplexMode);
103     }
104 
105     BuildJsWorkerIsLegal(env, jsValue, nativeObj);
106     nativeObj->Dump();
107     return nativeObj;
108 }
109 
BuildJsWorkerIsLegal(napi_env env,napi_value jsValue,std::shared_ptr<PrintAttributes> & nativeObj)110 void PrintAttributesHelper::BuildJsWorkerIsLegal(napi_env env, napi_value jsValue,
111     std::shared_ptr<PrintAttributes> &nativeObj)
112 {
113     if (nativeObj == nullptr) {
114         PRINT_HILOGE("nativeObj is nullptr");
115         return;
116     }
117     napi_value jsPageRange = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_PAGERANGE);
118     if (jsPageRange != nullptr) {
119         auto pageRangePtr = PrintRangeHelper::BuildFromJs(env, jsPageRange);
120         if (pageRangePtr != nullptr) {
121             nativeObj->SetPageRange(*pageRangePtr);
122         }
123     }
124 
125     BuildFromJsPageSize(env, jsValue, nativeObj);
126 
127     napi_value jsMargin = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_MARGIN);
128     if (jsMargin != nullptr) {
129         auto marginPtr = PrintMarginHelper::BuildFromJs(env, jsMargin);
130         if (marginPtr != nullptr) {
131             nativeObj->SetMargin(*marginPtr);
132         }
133     }
134 
135     napi_value jsOption = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_OPTION);
136     if (jsOption != nullptr) {
137         nativeObj->SetOption(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_JOB_OPTION));
138     }
139 }
140 
BuildFromJsPageSize(napi_env env,napi_value jsValue,std::shared_ptr<PrintAttributes> & nativeObj)141 void PrintAttributesHelper::BuildFromJsPageSize(
142     napi_env env, napi_value jsValue, std::shared_ptr<PrintAttributes> &nativeObj)
143 {
144     napi_value jsPageSize = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_PAGESIZE);
145     if (jsPageSize == nullptr) {
146         return;
147     }
148 
149     if (NapiPrintUtils::GetValueType(env, jsPageSize) == napi_number) {
150         int32_t pageSizeValue = NapiPrintUtils::GetInt32FromValue(env, jsPageSize);
151         auto pageSizePtr = std::make_shared<PrintPageSize>();
152         pageSizePtr->SetId(std::to_string(pageSizeValue));
153         pageSizePtr->SetName(std::to_string(pageSizeValue));
154         nativeObj->SetPageSize(*pageSizePtr);
155     } else {
156         auto pageSizePtr = PrintPageSizeHelper::BuildFromJs(env, jsPageSize);
157         if (pageSizePtr != nullptr) {
158             nativeObj->SetPageSize(*pageSizePtr);
159         }
160     }
161 }
162 
CreatePageRange(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)163 bool PrintAttributesHelper::CreatePageRange(napi_env env, napi_value &jsPrintAttributes,
164     const PrintAttributes &attributes)
165 {
166     PrintRange range;
167     attributes.GetPageRange(range);
168     napi_value jsPageRange = PrintRangeHelper::MakeJsObject(env, range);
169     PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_PAGERANGE, jsPageRange), false);
170     return true;
171 }
172 
CreatePageSize(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)173 bool PrintAttributesHelper::CreatePageSize(napi_env env, napi_value &jsPrintAttributes,
174     const PrintAttributes &attributes)
175 {
176     PrintPageSize pageSize;
177     attributes.GetPageSize(pageSize);
178     napi_value jsPageSize = PrintPageSizeHelper::MakeJsObject(env, pageSize);
179     PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_PAGESIZE, jsPageSize), false);
180     return true;
181 }
182 
CreateMargin(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)183 bool PrintAttributesHelper::CreateMargin(napi_env env, napi_value &jsPrintAttributes,
184     const PrintAttributes &attributes)
185 {
186     PrintMargin margin;
187     attributes.GetMargin(margin);
188     napi_value jsMargin = PrintMarginHelper::MakeJsObject(env, margin);
189     PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_MARGIN, jsMargin), false);
190     return true;
191 }
192 
ValidateProperty(napi_env env,napi_value object)193 bool PrintAttributesHelper::ValidateProperty(napi_env env, napi_value object)
194 {
195     std::map<std::string, PrintParamStatus> propertyList = {
196         {PARAM_JOB_COPYNUMBER, PRINT_PARAM_OPT},
197         {PARAM_JOB_PAGERANGE, PRINT_PARAM_OPT},
198         {PARAM_JOB_ISSEQUENTIAL, PRINT_PARAM_OPT},
199         {PARAM_JOB_PAGESIZE, PRINT_PARAM_OPT},
200         {PARAM_JOB_ISLANDSCAPE, PRINT_PARAM_OPT},
201         {PARAM_JOB_DIRECTIONMODE, PRINT_PARAM_OPT},
202         {PARAM_JOB_COLORMODE, PRINT_PARAM_OPT},
203         {PARAM_JOB_DUPLEXMODE, PRINT_PARAM_OPT},
204         {PARAM_JOB_MARGIN, PRINT_PARAM_OPT},
205         {PARAM_JOB_OPTION, PRINT_PARAM_OPT},
206     };
207 
208     auto names = NapiPrintUtils::GetPropertyNames(env, object);
209     return NapiPrintUtils::VerifyProperty(names, propertyList);
210 }
211 }
212