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 "system_defined_form.h"
17
18 namespace OHOS {
19 namespace UDMF {
SystemDefinedForm()20 SystemDefinedForm::SystemDefinedForm()
21 {
22 SetType(SYSTEM_DEFINED_FORM);
23 }
24
SystemDefinedForm(UDType type,ValueType value)25 SystemDefinedForm::SystemDefinedForm(UDType type, ValueType value) : SystemDefinedRecord(type, value)
26 {
27 SetType(SYSTEM_DEFINED_FORM);
28 if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
29 auto object = std::get<std::shared_ptr<Object>>(value);
30 object->GetValue(FORMID, formId_);
31 object->GetValue(FORMNAME, formName_);
32 object->GetValue(BUNDLENAME, bundleName_);
33 object->GetValue(ABILITYNAME, abilityName_);
34 object->GetValue(MODULE, module_);
35 std::shared_ptr<Object> detailObj = nullptr;
36 if (object->GetValue(DETAILS, detailObj)) {
37 details_ = ObjectUtils::ConvertToUDDetails(detailObj);
38 }
39 hasObject_ = true;
40 }
41 }
42
GetSize()43 int64_t SystemDefinedForm::GetSize()
44 {
45 return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + sizeof(formId_) +
46 this->formName_.size() + this->bundleName_.size() + this->abilityName_.size() + this->module_.size()) +
47 GetInnerEntriesSize();
48 }
49
GetFormId() const50 int32_t SystemDefinedForm::GetFormId() const
51 {
52 return this->formId_;
53 }
54
SetFormId(const int32_t & formId)55 void SystemDefinedForm::SetFormId(const int32_t &formId)
56 {
57 this->formId_ = formId;
58 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
59 std::get<std::shared_ptr<Object>>(value_)->value_[FORMID] = formId_;
60 }
61 }
62
GetFormName() const63 std::string SystemDefinedForm::GetFormName() const
64 {
65 return this->formName_;
66 }
67
SetFormName(const std::string & formName)68 void SystemDefinedForm::SetFormName(const std::string &formName)
69 {
70 this->formName_ = formName;
71 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
72 std::get<std::shared_ptr<Object>>(value_)->value_[FORMNAME] = formName_;
73 }
74 }
75
GetBundleName() const76 std::string SystemDefinedForm::GetBundleName() const
77 {
78 return this->bundleName_;
79 }
80
SetBundleName(const std::string & bundleName)81 void SystemDefinedForm::SetBundleName(const std::string &bundleName)
82 {
83 this->bundleName_ = bundleName;
84 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
85 std::get<std::shared_ptr<Object>>(value_)->value_[BUNDLENAME] = bundleName_;
86 }
87 }
88
GetAbilityName() const89 std::string SystemDefinedForm::GetAbilityName() const
90 {
91 return this->abilityName_;
92 }
93
SetAbilityName(const std::string & abilityName)94 void SystemDefinedForm::SetAbilityName(const std::string &abilityName)
95 {
96 this->abilityName_ = abilityName;
97 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
98 std::get<std::shared_ptr<Object>>(value_)->value_[ABILITYNAME] = abilityName_;
99 }
100 }
101
GetModule() const102 std::string SystemDefinedForm::GetModule() const
103 {
104 return this->module_;
105 }
106
SetModule(const std::string & module)107 void SystemDefinedForm::SetModule(const std::string &module)
108 {
109 this->module_ = module;
110 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
111 std::get<std::shared_ptr<Object>>(value_)->value_[MODULE] = module_;
112 }
113 }
114
SetItems(UDDetails & details)115 void SystemDefinedForm::SetItems(UDDetails& details)
116 {
117 for (auto &item : details) {
118 auto* value = std::get_if<std::string>(&item.second);
119 auto* intValue = std::get_if<int32_t>(&item.second);
120 if (value == nullptr && intValue == nullptr) {
121 continue;
122 }
123 if (intValue != nullptr && item.first == FORMID) {
124 SetFormId(*intValue);
125 continue;
126 }
127 if (value == nullptr) {
128 continue;
129 }
130 if (item.first == FORMNAME) {
131 SetFormName(*value);
132 }
133 if (item.first == MODULE) {
134 SetModule(*value);
135 }
136 if (item.first == BUNDLENAME) {
137 SetBundleName(*value);
138 }
139 if (item.first == ABILITYNAME) {
140 SetAbilityName(*value);
141 }
142 }
143 }
144
GetItems()145 UDDetails SystemDefinedForm::GetItems()
146 {
147 UDDetails items;
148 items[FORMID] = GetFormId();
149 items[FORMNAME] = GetFormName();
150 items[MODULE] = GetModule();
151 items[BUNDLENAME] = GetBundleName();
152 items[ABILITYNAME] = GetAbilityName();
153 return items;
154 }
155
InitObject()156 void SystemDefinedForm::InitObject()
157 {
158 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
159 auto value = value_;
160 value_ = std::make_shared<Object>();
161 auto object = std::get<std::shared_ptr<Object>>(value_);
162 object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
163 object->value_[FORMID] = formId_;
164 object->value_[FORMNAME] = formName_;
165 object->value_[BUNDLENAME] = bundleName_;
166 object->value_[ABILITYNAME] = abilityName_;
167 object->value_[MODULE] = module_;
168 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
169 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
170 }
171 }
172
173 } // namespace UDMF
174 } // namespace OHOS
175