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_name_data.h"
16 
17 #include "telephony_errors.h"
18 
19 namespace OHOS {
20 namespace Telephony {
21 
BuildValuesBucket(OHOS::DataShare::DataShareValuesBucket & valuesBucket)22 int32_t VCardNameData::BuildValuesBucket(OHOS::DataShare::DataShareValuesBucket &valuesBucket)
23 {
24     valuesBucket.Put(ContactData::TYPE_ID, TypeId::NAME);
25     if (!given_.empty()) {
26         valuesBucket.Put(ContactData::GIVEN_NAME, given_);
27     }
28     if (!family_.empty()) {
29         valuesBucket.Put(ContactData::FAMILY_NAME, family_);
30     }
31     if (!middle_.empty()) {
32         valuesBucket.Put(ContactData::MIDDLE_NAME, middle_);
33     }
34     if (!prefix_.empty()) {
35         valuesBucket.Put(ContactData::NAME_PREFIX, prefix_);
36     }
37     if (!suffix_.empty()) {
38         valuesBucket.Put(ContactData::NAME_SUFFIX, suffix_);
39     }
40     bool isPhoneticNameSpecified = false;
41     if (!phoneticGiven_.empty()) {
42         valuesBucket.Put(ContactData::GIVEN_NAME_PHONETIC, phoneticGiven_);
43         isPhoneticNameSpecified = true;
44     }
45     if (!phoneticFamily_.empty()) {
46         valuesBucket.Put(ContactData::FAMILY_NAME_PHONETIC, phoneticFamily_);
47         isPhoneticNameSpecified = true;
48     }
49     if (!phoneticMiddle_.empty()) {
50         valuesBucket.Put(ContactData::MIDDLE_NAME_PHONETIC, phoneticMiddle_);
51         isPhoneticNameSpecified = true;
52     }
53     if (!isPhoneticNameSpecified) {
54         valuesBucket.Put(ContactData::GIVEN_NAME_PHONETIC, sort_);
55     }
56     valuesBucket.Put(ContactData::FULL_NAME, displayName_);
57     return TELEPHONY_SUCCESS;
58 }
59 
SetFamily(const std::string & family)60 void VCardNameData::SetFamily(const std::string &family)
61 {
62     family_ = family;
63 }
64 
SetGiven(const std::string & given)65 void VCardNameData::SetGiven(const std::string &given)
66 {
67     given_ = given;
68 }
69 
SetMiddle(const std::string & middle)70 void VCardNameData::SetMiddle(const std::string &middle)
71 {
72     middle_ = middle;
73 }
74 
SetPrefix(const std::string & prefix)75 void VCardNameData::SetPrefix(const std::string &prefix)
76 {
77     prefix_ = prefix;
78 }
79 
SetSuffix(const std::string & suffix)80 void VCardNameData::SetSuffix(const std::string &suffix)
81 {
82     suffix_ = suffix;
83 }
84 
SetFormatted(const std::string & formatted)85 void VCardNameData::SetFormatted(const std::string &formatted)
86 {
87     formatted_ = formatted;
88 }
89 
SetPhoneticFamily(const std::string & phoneticFamily)90 void VCardNameData::SetPhoneticFamily(const std::string &phoneticFamily)
91 {
92     phoneticFamily_ = phoneticFamily;
93 }
94 
SetPhoneticGiven(const std::string & phoneticGiven)95 void VCardNameData::SetPhoneticGiven(const std::string &phoneticGiven)
96 {
97     phoneticGiven_ = phoneticGiven;
98 }
99 
SetPhoneticMiddle(const std::string & phoneticMiddle)100 void VCardNameData::SetPhoneticMiddle(const std::string &phoneticMiddle)
101 {
102     phoneticMiddle_ = phoneticMiddle;
103 }
104 
setSort(const std::string & sort)105 void VCardNameData::setSort(const std::string &sort)
106 {
107     sort_ = sort;
108 }
109 
setDispalyName(const std::string & displayName)110 void VCardNameData::setDispalyName(const std::string &displayName)
111 {
112     displayName_ = displayName;
113 }
114 
GetPrefix()115 std::string VCardNameData::GetPrefix()
116 {
117     return prefix_;
118 }
119 
GetFamily()120 std::string VCardNameData::GetFamily()
121 {
122     return family_;
123 }
124 
GetGiven()125 std::string VCardNameData::GetGiven()
126 {
127     return given_;
128 }
129 
GetMiddle()130 std::string VCardNameData::GetMiddle()
131 {
132     return middle_;
133 }
134 
GetSuffix()135 std::string VCardNameData::GetSuffix()
136 {
137     return suffix_;
138 }
139 
GetFormatted()140 std::string VCardNameData::GetFormatted()
141 {
142     return formatted_;
143 }
144 
GetPhoneticFamily()145 std::string VCardNameData::GetPhoneticFamily()
146 {
147     return phoneticFamily_;
148 }
149 
GetPhoneticGiven()150 std::string VCardNameData::GetPhoneticGiven()
151 {
152     return phoneticGiven_;
153 }
154 
GetPhoneticMiddle()155 std::string VCardNameData::GetPhoneticMiddle()
156 {
157     return phoneticMiddle_;
158 }
159 
GetSort()160 std::string VCardNameData::GetSort()
161 {
162     return sort_;
163 }
164 
GetDisplayName()165 std::string VCardNameData::GetDisplayName()
166 {
167     return displayName_;
168 }
169 
BuildData(std::shared_ptr<DataShare::DataShareResultSet> resultSet)170 int32_t VCardNameData::BuildData(std::shared_ptr<DataShare::DataShareResultSet> resultSet)
171 {
172     if (resultSet == nullptr) {
173         return TELEPHONY_ERROR;
174     }
175     int32_t index;
176     resultSet->GetColumnIndex(ContactData::GIVEN_NAME, index);
177     resultSet->GetString(index, given_);
178     resultSet->GetColumnIndex(ContactData::FAMILY_NAME, index);
179     resultSet->GetString(index, family_);
180     resultSet->GetColumnIndex(ContactData::MIDDLE_NAME, index);
181     resultSet->GetString(index, middle_);
182     resultSet->GetColumnIndex(ContactData::NAME_PREFIX, index);
183     resultSet->GetString(index, prefix_);
184     resultSet->GetColumnIndex(ContactData::NAME_SUFFIX, index);
185     resultSet->GetString(index, suffix_);
186     resultSet->GetColumnIndex(ContactData::GIVEN_NAME_PHONETIC, index);
187     resultSet->GetString(index, phoneticGiven_);
188     resultSet->GetColumnIndex(ContactData::FAMILY_NAME_PHONETIC, index);
189     resultSet->GetString(index, phoneticFamily_);
190     resultSet->GetColumnIndex(ContactData::MIDDLE_NAME_PHONETIC, index);
191     resultSet->GetString(index, phoneticMiddle_);
192     resultSet->GetColumnIndex(ContactData::FULL_NAME, index);
193     resultSet->GetString(index, displayName_);
194     return TELEPHONY_SUCCESS;
195 }
196 
197 } // namespace Telephony
198 } // namespace OHOS
199