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 "print_converter.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19 #include "print_util.h"
20
21 namespace {
22 const uint32_t ORIENTATION_OFFSET = 3;
23 } // namespace
24 namespace OHOS::Print {
ConvertToNativeErrorCode(int32_t errorCode)25 Print_ErrorCode ConvertToNativeErrorCode(int32_t errorCode)
26 {
27 if (errorCode >= E_PRINT_GENERIC_FAILURE && errorCode <= E_PRINT_UNKNOWN) {
28 return static_cast<Print_ErrorCode>(errorCode + PRINT_ERROR_GENERIC_FAILURE - E_PRINT_GENERIC_FAILURE);
29 }
30 return static_cast<Print_ErrorCode>(errorCode);
31 }
32
ConvertPrinterState(uint32_t state)33 Print_PrinterState ConvertPrinterState(uint32_t state)
34 {
35 switch (state) {
36 case PRINTER_ADDED:
37 return PRINTER_UNAVAILABLE;
38 case PRINTER_REMOVED:
39 return PRINTER_UNAVAILABLE;
40 case PRINTER_UPDATE_CAP:
41 return PRINTER_UNAVAILABLE;
42 case PRINTER_CONNECTED:
43 return PRINTER_IDLE;
44 case PRINTER_DISCONNECTED:
45 return PRINTER_UNAVAILABLE;
46 case PRINTER_RUNNING:
47 return PRINTER_BUSY;
48 default:
49 return PRINTER_UNAVAILABLE;
50 }
51 }
52
ConvertStringToInt(const char *src, int &dst)53 bool ConvertStringToInt(const char *src, int &dst)
54 {
55 if (src == nullptr) {
56 return false;
57 }
58 dst = atoi(src);
59 if (errno == ERANGE) {
60 PRINT_HILOGW("ConvertStringToInt fail: %{public}s", src);
61 return false;
62 }
63 return true;
64 }
65
ConvertOrientationMode(const uint32_t &src, Print_OrientationMode &dst)66 bool ConvertOrientationMode(const uint32_t &src, Print_OrientationMode &dst)
67 {
68 if (src >= ORIENTATION_OFFSET && src - ORIENTATION_OFFSET <= static_cast<uint32_t>(ORIENTATION_MODE_NONE)) {
69 dst = static_cast<Print_OrientationMode>(src - ORIENTATION_OFFSET);
70 return true;
71 } else {
72 return false;
73 }
74 }
75
ConvertColorMode(const uint32_t &src, Print_ColorMode &dst)76 bool ConvertColorMode(const uint32_t &src, Print_ColorMode &dst)
77 {
78 if (src >= 0 && src <= static_cast<uint32_t>(COLOR_MODE_AUTO)) {
79 dst = static_cast<Print_ColorMode>(src);
80 return true;
81 } else {
82 return false;
83 }
84 }
85
ConvertDuplexMode(const uint32_t &src, Print_DuplexMode &dst)86 bool ConvertDuplexMode(const uint32_t &src, Print_DuplexMode &dst)
87 {
88 if (src >= 0 && src <= static_cast<uint32_t>(DUPLEX_MODE_SHORT_EDGE)) {
89 dst = static_cast<Print_DuplexMode>(src);
90 return true;
91 } else {
92 return false;
93 }
94 }
95
ConvertQuality(const uint32_t &src, Print_Quality &dst)96 bool ConvertQuality(const uint32_t &src, Print_Quality &dst)
97 {
98 if (src >= static_cast<uint32_t>(PRINT_QUALITY_DRAFT) && src <= static_cast<uint32_t>(PRINT_QUALITY_HIGH)) {
99 dst = static_cast<Print_Quality>(src);
100 return true;
101 } else {
102 return false;
103 }
104 }
105
GetDocumentFormatString(Print_DocumentFormat format)106 std::string GetDocumentFormatString(Print_DocumentFormat format)
107 {
108 switch (format) {
109 case DOCUMENT_FORMAT_JPEG:
110 return "image/jpeg";
111 case DOCUMENT_FORMAT_PDF:
112 return "application/pdf";
113 case DOCUMENT_FORMAT_POSTSCRIPT:
114 return "application/postscript";
115 case DOCUMENT_FORMAT_TEXT:
116 return "text/plain";
117 case DOCUMENT_FORMAT_AUTO:
118 return "application/octet-stream";
119 default:
120 return "application/octet-stream";
121 }
122 }
123
GetResolutionString(Print_Resolution resolution)124 std::string GetResolutionString(Print_Resolution resolution)
125 {
126 return std::to_string(resolution.horizontalDpi) + "x" + std::to_string(resolution.verticalDpi) + "dpi";
127 }
128 } // namespace OHOS::Print
129