1 /*
2  * Copyright (c) 2023 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_service_converter.h"
17 #include "print_log.h"
18 
19 namespace OHOS {
20 namespace Print {
GetQulityString(int code)21 std::string GetQulityString(int code)
22 {
23     return std::to_string(code);
24 }
25 
ConvertColorModeCode(const char * src,ColorModeCode & dst)26 bool ConvertColorModeCode(const char *src, ColorModeCode &dst)
27 {
28     if (src == nullptr) {
29         return false;
30     }
31     if (strcasestr(src, "color")) {
32         dst = COLOR_MODE_COLOR;
33     } else if (strcasestr(src, "monochrome")) {
34         dst = COLOR_MODE_MONOCHROME;
35     } else if (strcasestr(src, "auto")) {
36         dst = COLOR_MODE_AUTO;
37     } else {
38         return false;
39     }
40     return true;
41 }
42 
ConvertColorModeToJson(const ColorModeCode & code,nlohmann::json & jsonObject)43 bool ConvertColorModeToJson(const ColorModeCode &code, nlohmann::json &jsonObject)
44 {
45     jsonObject["color"] = std::to_string(static_cast<int>(code));
46     return true;
47 }
48 
ConvertDuplexModeToJson(const DuplexModeCode & code,nlohmann::json & jsonObject)49 bool ConvertDuplexModeToJson(const DuplexModeCode &code, nlohmann::json &jsonObject)
50 {
51     jsonObject["duplex"] = std::to_string(static_cast<int>(code));
52     return true;
53 }
54 
ConvertPageSizeId(const char * src,std::string & id)55 bool ConvertPageSizeId(const char *src, std::string &id)
56 {
57     if (src == nullptr) {
58         return false;
59     }
60     std::string pageString(src);
61     id = PrintPageSize::MatchPageSize(pageString);
62     return !id.empty();
63 }
64 
ConvertPrintPageSize(const char * src,PrintPageSize & dst)65 bool ConvertPrintPageSize(const char *src, PrintPageSize &dst)
66 {
67     std::string id;
68     if (ConvertPageSizeId(src, id)) {
69         return PrintPageSize::FindPageSizeById(id, dst);
70     }
71     return false;
72 }
73 
ConvertPageSizeToJson(const PrintPageSize & pageSize,nlohmann::json & jsonObject)74 bool ConvertPageSizeToJson(const PrintPageSize &pageSize, nlohmann::json &jsonObject)
75 {
76     jsonObject["id"] = pageSize.GetId();
77     jsonObject["name"] = pageSize.GetName();
78     jsonObject["width"] = pageSize.GetWidth();
79     jsonObject["height"] = pageSize.GetHeight();
80     return true;
81 }
82 }  // namespace Print
83 }  // namespace OHOS