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 || src[0] == '\0') {
56         return false;
57     }
58     if (!PrintUtil::ConvertToInt(std::string(src), dst)) {
59         PRINT_HILOGW("ConvertStringToInt fail: %{public}s", src);
60         return false;
61     }
62     return true;
63 }
64 
ConvertOrientationMode(const uint32_t & src,Print_OrientationMode & dst)65 bool ConvertOrientationMode(const uint32_t &src, Print_OrientationMode &dst)
66 {
67     if (src >= ORIENTATION_OFFSET && src - ORIENTATION_OFFSET <= static_cast<uint32_t>(ORIENTATION_MODE_NONE)) {
68         dst = static_cast<Print_OrientationMode>(src - ORIENTATION_OFFSET);
69         return true;
70     } else {
71         return false;
72     }
73 }
74 
ConvertColorMode(const uint32_t & src,Print_ColorMode & dst)75 bool ConvertColorMode(const uint32_t &src, Print_ColorMode &dst)
76 {
77     if (src >= 0 && src <= static_cast<uint32_t>(COLOR_MODE_AUTO)) {
78         dst = static_cast<Print_ColorMode>(src);
79         return true;
80     } else {
81         return false;
82     }
83 }
84 
ConvertDuplexMode(const uint32_t & src,Print_DuplexMode & dst)85 bool ConvertDuplexMode(const uint32_t &src, Print_DuplexMode &dst)
86 {
87     if (src >= 0 && src <= static_cast<uint32_t>(DUPLEX_MODE_SHORT_EDGE)) {
88         dst = static_cast<Print_DuplexMode>(src);
89         return true;
90     } else {
91         return false;
92     }
93 }
94 
ConvertQuality(const uint32_t & src,Print_Quality & dst)95 bool ConvertQuality(const uint32_t &src, Print_Quality &dst)
96 {
97     if (src >= static_cast<uint32_t>(PRINT_QUALITY_DRAFT) && src <= static_cast<uint32_t>(PRINT_QUALITY_HIGH)) {
98         dst = static_cast<Print_Quality>(src);
99         return true;
100     } else {
101         return false;
102     }
103 }
104 
GetDocumentFormatString(Print_DocumentFormat format)105 std::string GetDocumentFormatString(Print_DocumentFormat format)
106 {
107     switch (format) {
108         case DOCUMENT_FORMAT_JPEG:
109             return "image/jpeg";
110         case DOCUMENT_FORMAT_PDF:
111             return "application/pdf";
112         case DOCUMENT_FORMAT_POSTSCRIPT:
113             return "application/postscript";
114         case DOCUMENT_FORMAT_TEXT:
115             return "text/plain";
116         case DOCUMENT_FORMAT_AUTO:
117             return "application/octet-stream";
118         default:
119             return "application/octet-stream";
120     }
121 }
122 
GetResolutionString(Print_Resolution resolution)123 std::string GetResolutionString(Print_Resolution resolution)
124 {
125     return std::to_string(resolution.horizontalDpi) + "x" + std::to_string(resolution.verticalDpi) + "dpi";
126 }
127 }  // namespace OHOS::Print
128