• 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 "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 static_cast<int64_t>(this->oriUri_.size() + this->remoteUri_.size() +
49         UnifiedDataUtils::GetDetailsSize(this->details_)) + GetInnerEntriesSize();
50 }
51 
GetUri() const52 std::string File::GetUri() const
53 {
54     return this->oriUri_;
55 }
56 
SetUri(const std::string & uri)57 void File::SetUri(const std::string &uri)
58 {
59     this->oriUri_ = uri;
60     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
61         auto object = std::get<std::shared_ptr<Object>>(value_);
62         object->value_[ORI_URI] = oriUri_;
63     }
64 }
65 
GetRemoteUri() const66 std::string File::GetRemoteUri() const
67 {
68     return this->remoteUri_;
69 }
70 
SetRemoteUri(const std::string & uri)71 void File::SetRemoteUri(const std::string &uri)
72 {
73     this->remoteUri_ = uri;
74     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
75         auto object = std::get<std::shared_ptr<Object>>(value_);
76         object->value_[REMOTE_URI] = remoteUri_;
77     }
78 }
79 
SetDetails(UDDetails & variantMap)80 void File::SetDetails(UDDetails &variantMap)
81 {
82     this->details_ = variantMap;
83     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
84         auto object = std::get<std::shared_ptr<Object>>(value_);
85         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
86     }
87 }
88 
GetDetails() const89 UDDetails File::GetDetails() const
90 {
91     return this->details_;
92 }
93 
InitObject()94 void File::InitObject()
95 {
96     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
97         auto value = value_;
98         value_ = std::make_shared<Object>();
99         auto object = std::get<std::shared_ptr<Object>>(value_);
100         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
101         object->value_[ORI_URI] = oriUri_;
102         object->value_[REMOTE_URI] = remoteUri_;
103         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
104         object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
105     }
106 }
107 
108 } // namespace UDMF
109 } // namespace OHOS
110