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