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 napi_value jsPageRange = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_PAGERANGE);
114 if (jsPageRange != nullptr) {
115 auto pageRangePtr = PrintRangeHelper::BuildFromJs(env, jsPageRange);
116 if (pageRangePtr != nullptr) {
117 nativeObj->SetPageRange(*pageRangePtr);
118 }
119 }
120
121 BuildFromJsPageSize(env, jsValue, nativeObj);
122
123 napi_value jsMargin = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_MARGIN);
124 if (jsMargin != nullptr) {
125 auto marginPtr = PrintMarginHelper::BuildFromJs(env, jsMargin);
126 if (marginPtr != nullptr) {
127 nativeObj->SetMargin(*marginPtr);
128 }
129 }
130
131 napi_value jsOption = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_OPTION);
132 if (jsOption != nullptr) {
133 nativeObj->SetOption(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_JOB_OPTION));
134 }
135 }
136
BuildFromJsPageSize(napi_env env,napi_value jsValue,std::shared_ptr<PrintAttributes> & nativeObj)137 void PrintAttributesHelper::BuildFromJsPageSize(
138 napi_env env, napi_value jsValue, std::shared_ptr<PrintAttributes> &nativeObj)
139 {
140 napi_value jsPageSize = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_JOB_PAGESIZE);
141 if (jsPageSize == nullptr) {
142 return;
143 }
144
145 if (NapiPrintUtils::GetValueType(env, jsPageSize) == napi_number) {
146 int32_t pageSizeValue = NapiPrintUtils::GetInt32FromValue(env, jsPageSize);
147 auto pageSizePtr = std::make_shared<PrintPageSize>();
148 pageSizePtr->SetId(std::to_string(pageSizeValue));
149 pageSizePtr->SetName(std::to_string(pageSizeValue));
150 nativeObj->SetPageSize(*pageSizePtr);
151 } else {
152 auto pageSizePtr = PrintPageSizeHelper::BuildFromJs(env, jsPageSize);
153 if (pageSizePtr != nullptr) {
154 nativeObj->SetPageSize(*pageSizePtr);
155 }
156 }
157 }
158
CreatePageRange(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)159 bool PrintAttributesHelper::CreatePageRange(napi_env env, napi_value &jsPrintAttributes,
160 const PrintAttributes &attributes)
161 {
162 PrintRange range;
163 attributes.GetPageRange(range);
164 napi_value jsPageRange = PrintRangeHelper::MakeJsObject(env, range);
165 PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_PAGERANGE, jsPageRange), false);
166 return true;
167 }
168
CreatePageSize(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)169 bool PrintAttributesHelper::CreatePageSize(napi_env env, napi_value &jsPrintAttributes,
170 const PrintAttributes &attributes)
171 {
172 PrintPageSize pageSize;
173 attributes.GetPageSize(pageSize);
174 napi_value jsPageSize = PrintPageSizeHelper::MakeJsObject(env, pageSize);
175 PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_PAGESIZE, jsPageSize), false);
176 return true;
177 }
178
CreateMargin(napi_env env,napi_value & jsPrintAttributes,const PrintAttributes & attributes)179 bool PrintAttributesHelper::CreateMargin(napi_env env, napi_value &jsPrintAttributes,
180 const PrintAttributes &attributes)
181 {
182 PrintMargin margin;
183 attributes.GetMargin(margin);
184 napi_value jsMargin = PrintMarginHelper::MakeJsObject(env, margin);
185 PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrintAttributes, PARAM_JOB_MARGIN, jsMargin), false);
186 return true;
187 }
188
ValidateProperty(napi_env env,napi_value object)189 bool PrintAttributesHelper::ValidateProperty(napi_env env, napi_value object)
190 {
191 std::map<std::string, PrintParamStatus> propertyList = {
192 {PARAM_JOB_COPYNUMBER, PRINT_PARAM_OPT},
193 {PARAM_JOB_PAGERANGE, PRINT_PARAM_OPT},
194 {PARAM_JOB_ISSEQUENTIAL, PRINT_PARAM_OPT},
195 {PARAM_JOB_PAGESIZE, PRINT_PARAM_OPT},
196 {PARAM_JOB_ISLANDSCAPE, PRINT_PARAM_OPT},
197 {PARAM_JOB_DIRECTIONMODE, PRINT_PARAM_OPT},
198 {PARAM_JOB_COLORMODE, PRINT_PARAM_OPT},
199 {PARAM_JOB_DUPLEXMODE, PRINT_PARAM_OPT},
200 {PARAM_JOB_MARGIN, PRINT_PARAM_OPT},
201 {PARAM_JOB_OPTION, PRINT_PARAM_OPT},
202 };
203
204 auto names = NapiPrintUtils::GetPropertyNames(env, object);
205 return NapiPrintUtils::VerifyProperty(names, propertyList);
206 }
207 }
208