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 "link.h"
17 
18 namespace OHOS {
19 namespace UDMF {
Link()20 Link::Link() : Link("", "")
21 {
22 }
23 
Link(const std::string & url)24 Link::Link(const std::string &url) : Link(url, "")
25 {
26 }
27 
Link(UDType type,ValueType value)28 Link::Link(UDType type, ValueType value) : Text(type, value)
29 {
30     SetType(HYPERLINK);
31     if (std::holds_alternative<std::string>(value)) {
32         url_ = std::get<std::string>(value);
33     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
34         auto object = std::get<std::shared_ptr<Object>>(value);
35         object->GetValue(URL, url_);
36         object->GetValue(DESCRIPTION, description_);
37         std::shared_ptr<Object> detailObj = nullptr;
38         if (object->GetValue(DETAILS, detailObj)) {
39             details_ = ObjectUtils::ConvertToUDDetails(detailObj);
40         }
41         hasObject_ = true;
42     }
43 }
44 
Link(const std::string & url,const std::string & description)45 Link::Link(const std::string &url, const std::string &description)
46 {
47     if (url.length() >= MAX_TEXT_LEN || description.length() >= MAX_TEXT_LEN) {
48         return;
49     }
50     SetType(HYPERLINK);
51     this->url_ = url;
52     this->description_ = description;
53 }
54 
GetSize()55 int64_t Link::GetSize()
56 {
57     return UnifiedDataUtils::GetDetailsSize(this->details_) + this->url_.size() + this->description_.size();
58 }
59 
GetUrl() const60 std::string Link::GetUrl() const
61 {
62     return this->url_;
63 }
64 
SetUrl(const std::string & url)65 void Link::SetUrl(const std::string &url)
66 {
67     if (url.length() >= MAX_TEXT_LEN) {
68         return;
69     }
70     this->url_ = url;
71     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
72         std::get<std::shared_ptr<Object>>(value_)->value_[URL] = url_;
73     }
74 }
75 
GetDescription() const76 std::string Link::GetDescription() const
77 {
78     return this->description_;
79 }
80 
SetDescription(const std::string & description)81 void Link::SetDescription(const std::string &description)
82 {
83     if (description.length() >= MAX_TEXT_LEN) {
84         return;
85     }
86     this->description_ = description;
87     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
88         std::get<std::shared_ptr<Object>>(value_)->value_[DESCRIPTION] = description_;
89     }
90 }
91 
InitObject()92 void Link::InitObject()
93 {
94     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
95         auto value = value_;
96         value_ = std::make_shared<Object>();
97         auto object = std::get<std::shared_ptr<Object>>(value_);
98         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
99         object->value_[URL] = url_;
100         object->value_[DESCRIPTION] = description_;
101         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
102         object->value_["VALUE_TYPE"] = value;
103     }
104 }
105 } // namespace UDMF
106 } // namespace OHOS