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 this->dataType_ = SYSTEM_DEFINED_FORM;
23 }
24
SystemDefinedForm(UDType type,ValueType value)25 SystemDefinedForm::SystemDefinedForm(UDType type, ValueType value) : SystemDefinedRecord(type, value)
26 {
27 this->dataType_ = 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(BUNDLE_NAME, bundleName_);
33 object->GetValue(ABILITY_NAME, 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 UnifiedDataUtils::GetDetailsSize(this->details_) + sizeof(formId_) + this->formName_.size()
46 + this->bundleName_.size() + this->abilityName_.size() + this->module_.size();
47 }
48
GetFormId() const49 int32_t SystemDefinedForm::GetFormId() const
50 {
51 return this->formId_;
52 }
53
SetFormId(const int32_t & formId)54 void SystemDefinedForm::SetFormId(const int32_t &formId)
55 {
56 this->formId_ = formId;
57 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
58 std::get<std::shared_ptr<Object>>(value_)->value_[FORMID] = formId_;
59 }
60 }
61
GetFormName() const62 std::string SystemDefinedForm::GetFormName() const
63 {
64 return this->formName_;
65 }
66
SetFormName(const std::string & formName)67 void SystemDefinedForm::SetFormName(const std::string &formName)
68 {
69 this->formName_ = formName;
70 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
71 std::get<std::shared_ptr<Object>>(value_)->value_[FORMNAME] = formName_;
72 }
73 }
74
GetBundleName() const75 std::string SystemDefinedForm::GetBundleName() const
76 {
77 return this->bundleName_;
78 }
79
SetBundleName(const std::string & bundleName)80 void SystemDefinedForm::SetBundleName(const std::string &bundleName)
81 {
82 this->bundleName_ = bundleName;
83 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
84 std::get<std::shared_ptr<Object>>(value_)->value_[BUNDLE_NAME] = bundleName_;
85 }
86 }
87
GetAbilityName() const88 std::string SystemDefinedForm::GetAbilityName() const
89 {
90 return this->abilityName_;
91 }
92
SetAbilityName(const std::string & abilityName)93 void SystemDefinedForm::SetAbilityName(const std::string &abilityName)
94 {
95 this->abilityName_ = abilityName;
96 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
97 std::get<std::shared_ptr<Object>>(value_)->value_[ABILITY_NAME] = abilityName_;
98 }
99 }
100
GetModule() const101 std::string SystemDefinedForm::GetModule() const
102 {
103 return this->module_;
104 }
105
SetModule(const std::string & module)106 void SystemDefinedForm::SetModule(const std::string &module)
107 {
108 this->module_ = module;
109 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
110 std::get<std::shared_ptr<Object>>(value_)->value_[MODULE] = module_;
111 }
112 }
113
SetItems(UDDetails & details)114 void SystemDefinedForm::SetItems(UDDetails& details)
115 {
116 for (auto &item : details) {
117 auto* value = std::get_if<std::string>(&item.second);
118 auto* intValue = std::get_if<int32_t>(&item.second);
119 if (value == nullptr && intValue == nullptr) {
120 continue;
121 }
122 if (intValue != nullptr && item.first == FORMID) {
123 SetFormId(*intValue);
124 continue;
125 }
126 if (value == nullptr) {
127 continue;
128 }
129 if (item.first == FORMNAME) {
130 SetFormName(*value);
131 }
132 if (item.first == MODULE) {
133 SetModule(*value);
134 }
135 if (item.first == BUNDLE_NAME) {
136 SetBundleName(*value);
137 }
138 if (item.first == ABILITY_NAME) {
139 SetAbilityName(*value);
140 }
141 }
142 }
143
GetItems()144 UDDetails SystemDefinedForm::GetItems()
145 {
146 UDDetails items;
147 items[FORMID] = GetFormId();
148 items[FORMNAME] = GetFormName();
149 items[MODULE] = GetModule();
150 items[BUNDLE_NAME] = GetBundleName();
151 items[ABILITY_NAME] = GetAbilityName();
152 return items;
153 }
154
InitObject()155 void SystemDefinedForm::InitObject()
156 {
157 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
158 auto value = value_;
159 value_ = std::make_shared<Object>();
160 auto object = std::get<std::shared_ptr<Object>>(value_);
161 object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
162 object->value_[FORMID] = formId_;
163 object->value_[FORMNAME] = formName_;
164 object->value_[BUNDLE_NAME] = bundleName_;
165 object->value_[ABILITY_NAME] = abilityName_;
166 object->value_[MODULE] = module_;
167 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
168 object->value_[VALUE_TYPE] = value;
169 }
170 }
171
172 } // namespace UDMF
173 } // namespace OHOS
174