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 #ifndef OHOS_VCARD_MANAGER_H
17 #define OHOS_VCARD_MANAGER_H
18 
19 #include "mutex"
20 #include "vcard_configuration.h"
21 #include "vcard_contact.h"
22 #include "vcard_decoder.h"
23 
24 namespace OHOS {
25 namespace Telephony {
26 class VCardManager {
27 public:
28     int32_t Import(const std::string &path, int32_t accountId);
29     int32_t ImportLock(
30         const std::string &path, std::shared_ptr<DataShare::DataShareHelper> dataShareHelper, int32_t accountId);
31     int32_t Export(std::string &path, const DataShare::DataSharePredicates &predicates,
32         int32_t cardType = VCardConfiguration::VER_21, const std::string &charset = "UTF-8");
33     int32_t ExportLock(std::string &path, std::shared_ptr<DataShare::DataShareHelper> dataShareHelper,
34         const DataShare::DataSharePredicates &predicates, int32_t cardType = VCardConfiguration::VER_21,
35         const std::string &charset = "UTF-8");
36     int32_t ExportToStr(std::string &str, const DataShare::DataSharePredicates &predicates,
37         int32_t cardType = VCardConfiguration::VER_21, const std::string &charset = "UTF-8");
38     void SetDataHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper);
39     void OnStarted();
40     void OnEnded();
41     void OnOneContactStarted();
42     void OnOneContactEnded();
43     void OnRawDataCreated(std::shared_ptr<VCardRawData> rawData);
44     static VCardManager &GetInstance();
45     void Release();
46 
47 private:
48     class DecodeListener : public VCardDecodeListener {
49     public:
50         virtual void OnStarted();
51         virtual void OnEnded();
52         virtual void OnOneContactStarted();
53         virtual void OnOneContactEnded();
54         virtual void OnRawDataCreated(std::shared_ptr<VCardRawData> rawData);
55         std::vector<std::shared_ptr<VCardContact>> &GetContacts();
56 
57     private:
58         std::vector<std::shared_ptr<VCardContact>> contacts_;
59         std::shared_ptr<VCardContact> currentContact_;
60     };
61 
62 private:
63     VCardManager();
64     void Decode(const std::string &path, int32_t &errorCode);
65     void InsertContactDbAbility(int32_t accountId, int32_t &errorCode);
66     int32_t InsertRawContact(int32_t accountId);
67     bool IsAccountIdExit(int32_t accountId);
68     int32_t InsertContactData(int32_t rawId, std::shared_ptr<VCardContact> contact);
69     bool IsContactsIdExit(int32_t accountId);
70     int32_t GetAccountId();
71     bool ParameterTypeAndCharsetCheck(int32_t cardType, std::string charset, int32_t &errorCode);
72     std::vector<std::vector<std::shared_ptr<VCardContact>>> SplitContactsVector(
73         std::vector<std::shared_ptr<VCardContact>> list, size_t step);
74     void BatchInsertContactDbAbility(int32_t accountId, int32_t &errorCode);
75     void BatchInsertRawContact(int32_t accountId, uint32_t size, std::vector<int32_t> &rawIds, int32_t &errorCode);
76     void BatchInsertContactData(std::vector<int32_t> &rawIds,
77         const std::vector<std::shared_ptr<VCardContact>> &contactList, int32_t &errorCode);
78 
79 private:
80     std::shared_ptr<VCardManager::DecodeListener> listener_;
81     std::mutex mutex_;
82 };
83 } // namespace Telephony
84 } // namespace OHOS
85 #endif // OHOS_VCARD_MANAGER_H
86