• 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 #include "vcard_encoder.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 #include "vcard_constant.h"
20 #include "vcard_contact.h"
21 
22 namespace OHOS {
23 namespace Telephony {
VCardEncoder(int32_t cardType,const std::string & charset)24 VCardEncoder::VCardEncoder(int32_t cardType, const std::string &charset)
25 {
26     contructor_ = std::make_shared<VCardConstructor>(cardType, charset);
27 }
28 
ContructVCard(std::vector<std::vector<int>> contactIdLists,int32_t & errorCode)29 std::string VCardEncoder::ContructVCard(std::vector<std::vector<int>> contactIdLists, int32_t &errorCode)
30 {
31     std::string result = "";
32     for (int i = 0; i < (int32_t)contactIdLists.size(); i++) {
33         std::vector<int> contactIdList = contactIdLists[i];
34         TELEPHONY_LOGW("export progress %{public}d / %{public}d", i, (int32_t)contactIdLists.size());
35         auto rawResultSet = GetRawContactResultSet(contactIdList);
36         if (rawResultSet == nullptr) {
37             TELEPHONY_LOGE("QueryRawContactId failed");
38             errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
39             return "";
40         }
41         int rowCount = 0;
42         rawResultSet->GetRowCount(rowCount);
43         TELEPHONY_LOGI("rawResultSet rowCount = %{public}d", rowCount);
44         if (rowCount == 0) {
45             TELEPHONY_LOGW("rawResultSet is empty");
46             continue;
47         }
48         std::vector<int32_t> rawContactIdList;
49         int resultSetNum = rawResultSet->GoToFirstRow();
50         while (resultSetNum == 0) {
51             int32_t index = 0;
52             int32_t rawContactId = 0;
53             rawResultSet->GetColumnIndex(RawContact::ID, index);
54             rawResultSet->GetInt(index, rawContactId);
55             rawContactIdList.push_back(rawContactId);
56             resultSetNum = rawResultSet->GoToNextRow();
57         }
58         rawResultSet->Close();
59         TELEPHONY_LOGW("rawContactIdListSize = %{public}d", (int32_t)rawContactIdList.size());
60         for (auto rawContactId : rawContactIdList) {
61             std::shared_ptr<VCardContact> contact = std::make_shared<VCardContact>();
62             ContructContact(contact, rawContactId, errorCode);
63             result += contructor_->ContactVCard(contact);
64         }
65     }
66     TELEPHONY_LOGW("ContructVCard Success");
67     if (phoneNumberEncodedCallback_ != nullptr) {
68         contructor_->SetPhoneNumberEncodedCallback(phoneNumberEncodedCallback_);
69     }
70     return result;
71 }
72 
SetPhoneNumberEncodedCallback(std::shared_ptr<PhoneNumberEncodedCallback> PhoneNumberEncodedCallback)73 void VCardEncoder::SetPhoneNumberEncodedCallback(std::shared_ptr<PhoneNumberEncodedCallback> PhoneNumberEncodedCallback)
74 {
75     phoneNumberEncodedCallback_ = PhoneNumberEncodedCallback;
76 }
77 
ContructContact(std::shared_ptr<VCardContact> contact,int32_t rawContactId,int32_t & errorCode)78 void VCardEncoder::ContructContact(std::shared_ptr<VCardContact> contact,
79     int32_t rawContactId, int32_t &errorCode)
80 {
81     std::vector<std::string> columns;
82     DataShare::DataSharePredicates predicates;
83     predicates.EqualTo(ContactData::RAW_CONTACT_ID, rawContactId);
84     auto contactDataResultSet = VCardRdbHelper::GetInstance().QueryContactData(columns, predicates);
85     if (contactDataResultSet == nullptr) {
86         TELEPHONY_LOGE("QueryContactData failed");
87         errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
88         return;
89     }
90     int32_t contactDataResultSetNum = contactDataResultSet->GoToFirstRow();
91     if (contactDataResultSetNum == 0) {
92         contact->BuildContact(contactDataResultSet);
93     }
94     contactDataResultSet->Close();
95 }
96 
GetRawContactResultSet(std::vector<int> contactIdList)97 std::shared_ptr<DataShare::DataShareResultSet> VCardEncoder::GetRawContactResultSet(std::vector<int> contactIdList)
98 {
99     std::vector<std::string> columns;
100     columns.push_back(RawContact::ID);
101     DataShare::DataSharePredicates predicates;
102     predicates.BeginWrap();
103     for (size_t i = 0; i < contactIdList.size(); i++) {
104         predicates.EqualTo(RawContact::CONTACT_ID, contactIdList[i]);
105         if (i != contactIdList.size() - 1) {
106             predicates.Or();
107         }
108     }
109     predicates.EndWrap();
110     predicates.And();
111     predicates.EqualTo(RawContact::IS_DELETED, CONTACTS_NOT_DELETED);
112     predicates.And();
113     predicates.NotEqualTo(RawContact::PRIMARY_CONTACT, TELEPHONY_ERROR);
114     return VCardRdbHelper::GetInstance().QueryRawContact(columns, predicates);
115 }
116 
117 } // namespace Telephony
118 } // namespace OHOS