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 #include "file.h"
16
17 namespace OHOS {
18 namespace UDMF {
File()19 File::File() : UnifiedRecord(FILE)
20 {
21 SetType(FILE);
22 utdId2_ = GENERAL_FILE_URI;
23 }
24
File(const std::string & uri)25 File::File(const std::string &uri) : UnifiedRecord(FILE)
26 {
27 SetType(FILE);
28 this->oriUri_ = uri;
29 utdId2_ = GENERAL_FILE_URI;
30 }
31
File(UDType type,ValueType value)32 File::File(UDType type, ValueType value) : UnifiedRecord(type, value)
33 {
34 SetType(FILE);
35 utdId2_ = GENERAL_FILE_URI;
36 if (std::holds_alternative<std::string>(value)) {
37 oriUri_ = std::get<std::string>(value);
38 } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
39 auto object = std::get<std::shared_ptr<Object>>(value);
40 object->GetValue(ORI_URI, oriUri_);
41 object->GetValue(REMOTE_URI, remoteUri_);
42 object->GetValue(FILE_TYPE, fileType_);
43 std::shared_ptr<Object> detailObj = nullptr;
44 if (object->GetValue(DETAILS, detailObj)) {
45 details_ = ObjectUtils::ConvertToUDDetails(detailObj);
46 }
47 }
48 }
49
GetSize()50 int64_t File::GetSize()
51 {
52 return static_cast<int64_t>(this->oriUri_.size() + this->remoteUri_.size() +
53 UnifiedDataUtils::GetDetailsSize(this->details_)) + GetInnerEntriesSize();
54 }
55
GetUri() const56 std::string File::GetUri() const
57 {
58 return this->oriUri_;
59 }
60
SetUri(const std::string & uri)61 void File::SetUri(const std::string &uri)
62 {
63 this->oriUri_ = uri;
64 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
65 auto object = std::get<std::shared_ptr<Object>>(value_);
66 object->value_[ORI_URI] = oriUri_;
67 }
68 }
69
GetRemoteUri() const70 std::string File::GetRemoteUri() const
71 {
72 return this->remoteUri_;
73 }
74
SetRemoteUri(const std::string & uri)75 void File::SetRemoteUri(const std::string &uri)
76 {
77 this->remoteUri_ = uri;
78 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
79 auto object = std::get<std::shared_ptr<Object>>(value_);
80 object->value_[REMOTE_URI] = remoteUri_;
81 }
82 }
83
SetDetails(UDDetails & variantMap)84 void File::SetDetails(UDDetails &variantMap)
85 {
86 this->details_ = variantMap;
87 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
88 auto object = std::get<std::shared_ptr<Object>>(value_);
89 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
90 }
91 }
92
GetDetails() const93 UDDetails File::GetDetails() const
94 {
95 return this->details_;
96 }
97
InitObject()98 void File::InitObject()
99 {
100 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
101 auto value = value_;
102 value_ = std::make_shared<Object>();
103 auto object = std::get<std::shared_ptr<Object>>(value_);
104 object->value_[UNIFORM_DATA_TYPE] = GENERAL_FILE_URI;
105 object->value_[ORI_URI] = oriUri_;
106 object->value_[REMOTE_URI] = remoteUri_;
107 if (!fileType_.empty()) {
108 object->value_[FILE_TYPE] = fileType_;
109 } else {
110 object->value_[FILE_TYPE] = "general.file";
111 }
112 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
113 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
114 }
115 }
116
117 } // namespace UDMF
118 } // namespace OHOS
119