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 "file.h"
17 
18 namespace OHOS {
19 namespace UDMF {
File()20 File::File() : UnifiedRecord(FILE)
21 {
22 }
23 
File(const std::string & uri)24 File::File(const std::string &uri) : UnifiedRecord(FILE)
25 {
26     this->oriUri_ = uri;
27 }
28 
File(UDType type,ValueType value)29 File::File(UDType type, ValueType value) : UnifiedRecord(type, value)
30 {
31     this->dataType_ = FILE;
32     if (std::holds_alternative<std::string>(value)) {
33         oriUri_ = std::get<std::string>(value);
34     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
35         auto object = std::get<std::shared_ptr<Object>>(value);
36         object->GetValue(ORI_URI, oriUri_);
37         object->GetValue(REMOTE_URI, remoteUri_);
38         std::shared_ptr<Object> detailObj = nullptr;
39         if (object->GetValue(DETAILS, detailObj)) {
40             details_ = ObjectUtils::ConvertToUDDetails(detailObj);
41         }
42         hasObject_ = true;
43     }
44 }
45 
GetSize()46 int64_t File::GetSize()
47 {
48     return this->oriUri_.size() + this->remoteUri_.size();
49 }
50 
GetUri() const51 std::string File::GetUri() const
52 {
53     return this->oriUri_;
54 }
55 
SetUri(const std::string & uri)56 void File::SetUri(const std::string &uri)
57 {
58     this->oriUri_ = uri;
59     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
60         auto object = std::get<std::shared_ptr<Object>>(value_);
61         object->value_[ORI_URI] = oriUri_;
62     }
63 }
64 
GetRemoteUri() const65 std::string File::GetRemoteUri() const
66 {
67     return this->remoteUri_;
68 }
69 
SetRemoteUri(const std::string & uri)70 void File::SetRemoteUri(const std::string &uri)
71 {
72     this->remoteUri_ = uri;
73     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
74         auto object = std::get<std::shared_ptr<Object>>(value_);
75         object->value_[REMOTE_URI] = remoteUri_;
76     }
77 }
78 
SetDetails(UDDetails & variantMap)79 void File::SetDetails(UDDetails &variantMap)
80 {
81     this->details_ = variantMap;
82     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
83         auto object = std::get<std::shared_ptr<Object>>(value_);
84         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
85     }
86 }
87 
GetDetails() const88 UDDetails File::GetDetails() const
89 {
90     return this->details_;
91 }
92 
InitObject()93 void File::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_[ORI_URI] = oriUri_;
101         object->value_[REMOTE_URI] = remoteUri_;
102         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
103         object->value_["VALUE_TYPE"] = value;
104     }
105 }
106 
107 } // namespace UDMF
108 } // namespace OHOS
109