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 "vcard_postal_data.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace Telephony {
22 namespace {
23 constexpr int32_t ADDR_MAX_DATA_SIZE = 7;
24 } // namespace
25 
BuildValuesBucket(OHOS::DataShare::DataShareValuesBucket & valuesBucket)26 int32_t VCardPostalData::BuildValuesBucket(OHOS::DataShare::DataShareValuesBucket &valuesBucket)
27 {
28     valuesBucket.Put(ContactData::TYPE_ID, TypeId::POSTAL_ADDRESS);
29     valuesBucket.Put(ContactData::POBOX, pobox_);
30     valuesBucket.Put(ContactData::POSTCODE, postCode_);
31     valuesBucket.Put(ContactData::REGION, region_);
32     valuesBucket.Put(ContactData::STREET, street_);
33     valuesBucket.Put(ContactData::COUNTRY, country_);
34     valuesBucket.Put(ContactData::CITY, city_);
35     valuesBucket.Put(ContactData::LABEL_ID, labelId_);
36     valuesBucket.Put(ContactData::LABEL_NAME, labelName_);
37     valuesBucket.Put(ContactData::DETAIL_INFO, postalAddress_);
38     return TELEPHONY_SUCCESS;
39 }
40 
BuildData(std::shared_ptr<DataShare::DataShareResultSet> resultSet)41 int32_t VCardPostalData::BuildData(std::shared_ptr<DataShare::DataShareResultSet> resultSet)
42 {
43     if (resultSet == nullptr) {
44         return TELEPHONY_ERROR;
45     }
46     int32_t index;
47     resultSet->GetColumnIndex(ContactData::POBOX, index);
48     resultSet->GetString(index, pobox_);
49     resultSet->GetColumnIndex(ContactData::POSTCODE, index);
50     resultSet->GetString(index, postCode_);
51     resultSet->GetColumnIndex(ContactData::REGION, index);
52     resultSet->GetString(index, region_);
53     resultSet->GetColumnIndex(ContactData::STREET, index);
54     resultSet->GetString(index, street_);
55     resultSet->GetColumnIndex(ContactData::COUNTRY, index);
56     resultSet->GetString(index, country_);
57     resultSet->GetColumnIndex(ContactData::CITY, index);
58     resultSet->GetString(index, city_);
59     resultSet->GetColumnIndex(ContactData::LABEL_ID, index);
60     resultSet->GetString(index, labelId_);
61     resultSet->GetColumnIndex(ContactData::LABEL_NAME, index);
62     resultSet->GetString(index, labelName_);
63     resultSet->GetColumnIndex(ContactData::DETAIL_INFO, index);
64     resultSet->GetString(index, postalAddress_);
65     return TELEPHONY_SUCCESS;
66 }
67 
InitPostalData(std::vector<std::string> propValueList,int32_t type,std::string label)68 void VCardPostalData::InitPostalData(std::vector<std::string> propValueList, int32_t type, std::string label)
69 {
70     std::vector<std::string> dataArray(ADDR_MAX_DATA_SIZE, "");
71     int32_t size = static_cast<int32_t>(propValueList.size());
72     if (size > ADDR_MAX_DATA_SIZE) {
73         size = ADDR_MAX_DATA_SIZE;
74     }
75     int32_t i = 0;
76     for (std::string addressElement : propValueList) {
77         dataArray[i] = addressElement;
78         if (++i >= size) {
79             break;
80         }
81     }
82     while (i < ADDR_MAX_DATA_SIZE) {
83         dataArray[i++] = "";
84     }
85     pobox_ = dataArray[POBOX_VALUE_INDEX];
86     postalAddress_ = dataArray[POSTAL_ADDRESS_VALUE_INDEX];
87     street_ = dataArray[STREET_VALUE_INDEX];
88     if (!street_.empty() && postalAddress_.empty()) {
89         postalAddress_ = street_;
90     }
91     city_ = dataArray[CITY_VALUE_INDEX];
92     region_ = dataArray[REGION_VALUE_INDEX];
93     postCode_ = dataArray[POSTCODE_VALUE_INDEX];
94     country_ = dataArray[COUNTRY_VALUE_INDEX];
95     labelId_ = std::to_string(type);
96     labelName_ = label;
97 }
98 
GetPOBox()99 std::string VCardPostalData::GetPOBox()
100 {
101     return pobox_;
102 }
103 
SetPOBox(const std::string & pobox)104 void VCardPostalData::SetPOBox(const std::string &pobox)
105 {
106     pobox_ = pobox;
107 }
108 
GetPostCode()109 std::string VCardPostalData::GetPostCode()
110 {
111     return postCode_;
112 }
113 
SetPostCode(const std::string & postCode)114 void VCardPostalData::SetPostCode(const std::string &postCode)
115 {
116     postCode_ = postCode;
117 }
118 
GetRegion()119 std::string VCardPostalData::GetRegion()
120 {
121     return region_;
122 }
123 
SetRegion(const std::string & region)124 void VCardPostalData::SetRegion(const std::string &region)
125 {
126     region_ = region;
127 }
128 
GetCountry()129 std::string VCardPostalData::GetCountry()
130 {
131     return country_;
132 }
133 
SetCountry(const std::string & country)134 void VCardPostalData::SetCountry(const std::string &country)
135 {
136     country_ = country;
137 }
138 
GetCity()139 std::string VCardPostalData::GetCity()
140 {
141     return city_;
142 }
143 
SetCity(const std::string & city)144 void VCardPostalData::SetCity(const std::string &city)
145 {
146     city_ = city;
147 }
148 
GetStreet()149 std::string VCardPostalData::GetStreet()
150 {
151     return street_;
152 }
153 
SetStreet(const std::string & street)154 void VCardPostalData::SetStreet(const std::string &street)
155 {
156     street_ = street;
157 }
158 
GetNeighborhood()159 std::string VCardPostalData::GetNeighborhood()
160 {
161     return neighborhood_;
162 }
163 
SetNeighborhood(const std::string & neighborhood)164 void VCardPostalData::SetNeighborhood(const std::string &neighborhood)
165 {
166     neighborhood_ = neighborhood;
167 }
168 
GetPostalAddress()169 std::string VCardPostalData::GetPostalAddress()
170 {
171     return postalAddress_;
172 }
173 
SetPostalAddress(const std::string & postalAddress)174 void VCardPostalData::SetPostalAddress(const std::string &postalAddress)
175 {
176     postalAddress_ = postalAddress;
177 }
178 
GetLabelId()179 std::string VCardPostalData::GetLabelId()
180 {
181     return labelId_;
182 }
183 
SetLabelId(const std::string & labelId)184 void VCardPostalData::SetLabelId(const std::string &labelId)
185 {
186     labelId_ = labelId;
187 }
188 
GetLabelName()189 std::string VCardPostalData::GetLabelName()
190 {
191     return labelName_;
192 }
193 
SetLabelName(const std::string & labelName)194 void VCardPostalData::SetLabelName(const std::string &labelName)
195 {
196     labelName_ = labelName;
197 }
198 
199 } // namespace Telephony
200 } // namespace OHOS
201