• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + this->url_.size() +
58         this->description_.size()) + GetInnerEntriesSize();
59 }
60 
GetUrl() const61 std::string Link::GetUrl() const
62 {
63     return this->url_;
64 }
65 
SetUrl(const std::string & url)66 void Link::SetUrl(const std::string &url)
67 {
68     if (url.length() >= MAX_TEXT_LEN) {
69         return;
70     }
71     this->url_ = url;
72     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
73         std::get<std::shared_ptr<Object>>(value_)->value_[URL] = url_;
74     }
75 }
76 
GetDescription() const77 std::string Link::GetDescription() const
78 {
79     return this->description_;
80 }
81 
SetDescription(const std::string & description)82 void Link::SetDescription(const std::string &description)
83 {
84     if (description.length() >= MAX_TEXT_LEN) {
85         return;
86     }
87     this->description_ = description;
88     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
89         std::get<std::shared_ptr<Object>>(value_)->value_[DESCRIPTION] = description_;
90     }
91 }
92 
InitObject()93 void Link::InitObject()
94 {
95     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
96         auto value = value_;
97         value_ = std::make_shared<Object>();
98         auto object = std::get<std::shared_ptr<Object>>(value_);
99         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
100         object->value_[URL] = url_;
101         object->value_[DESCRIPTION] = description_;
102         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
103         object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
104     }
105 }
106 } // namespace UDMF
107 } // namespace OHOS