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 "notification_button_option.h"
17
18 #include <string> // for basic_string, operator+, basic_string<>...
19 #include <memory> // for shared_ptr, shared_ptr<>::element_type
20
21
22 #include "ans_image_util.h"
23 #include "ans_log_wrapper.h"
24 #include "nlohmann/json.hpp" // for json, basic_json<>::object_t, basic_json
25 #include "parcel.h" // for Parcel
26
27 namespace OHOS {
28 namespace Notification {
29
SetButtonName(const std::string & buttonName)30 void NotificationButtonOption::SetButtonName(const std::string &buttonName)
31 {
32 buttonName_ = buttonName;
33 }
34
GetButtonName() const35 std::string NotificationButtonOption::GetButtonName() const
36 {
37 return buttonName_;
38 }
39
Dump()40 std::string NotificationButtonOption::Dump()
41 {
42 return "NotificationButtonOption{ "
43 "buttonName = " + buttonName_ +
44 " }";
45 }
46
ToJson(nlohmann::json & jsonObject) const47 bool NotificationButtonOption::ToJson(nlohmann::json &jsonObject) const
48 {
49 jsonObject["buttonName"] = buttonName_;
50 return true;
51 }
52
FromJson(const nlohmann::json & jsonObject)53 NotificationButtonOption *NotificationButtonOption::FromJson(const nlohmann::json &jsonObject)
54 {
55 if (jsonObject.is_null() or !jsonObject.is_object()) {
56 ANS_LOGE("Invalid JSON object");
57 return nullptr;
58 }
59
60 NotificationButtonOption *button = new (std::nothrow) NotificationButtonOption();
61 if (button == nullptr) {
62 ANS_LOGE("Failed to create button option instance");
63 return nullptr;
64 }
65
66 const auto &jsonEnd = jsonObject.cend();
67 if (jsonObject.find("buttonName") != jsonEnd && jsonObject.at("buttonName").is_string()) {
68 button->buttonName_ = jsonObject.at("buttonName").get<std::string>();
69 }
70
71 return button;
72 }
73
Marshalling(Parcel & parcel) const74 bool NotificationButtonOption::Marshalling(Parcel &parcel) const
75 {
76 if (!parcel.WriteString(buttonName_)) {
77 ANS_LOGE("Failed to write title");
78 return false;
79 }
80
81 return true;
82 }
83
ReadFromParcel(Parcel & parcel)84 bool NotificationButtonOption::ReadFromParcel(Parcel &parcel)
85 {
86 buttonName_ = parcel.ReadString();
87
88 return true;
89 }
90
Unmarshalling(Parcel & parcel)91 NotificationButtonOption *NotificationButtonOption::Unmarshalling(Parcel &parcel)
92 {
93 NotificationButtonOption *button = new (std::nothrow) NotificationButtonOption();
94
95 if (button && !button->ReadFromParcel(parcel)) {
96 delete button;
97 button = nullptr;
98 }
99
100 return button;
101 }
102 } // namespace Notification
103 } // namespace OHOS