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_CONSTRUCTOR_H
17 #define OHOS_VCARD_CONSTRUCTOR_H
18 
19 #include <memory>
20 #include <sstream>
21 
22 #include "vcard_configuration.h"
23 #include "vcard_contact.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 class PhoneNumberEncodedCallback {
28 public:
29     virtual void onCallback(std::string number, int type, std::string label, bool primary) = 0;
30 };
31 
32 class VCardConstructor {
33 public:
34     explicit VCardConstructor(int32_t cardType = VCardConfiguration::VER_21, const std::string &charset = "UTF-8");
35     std::string ContactVCard(std::shared_ptr<VCardContact> contact);
36     void SetPhoneNumberEncodedCallback(std::shared_ptr<PhoneNumberEncodedCallback> phoneNumberEncodedCallback);
37     void ContactBegin();
38     void ContactEnd();
39     void AddNameData(const std::string &family, const std::string &given, const std::string &middle,
40         const std::string &prefix, const std::string &suffix);
41     int32_t ConstructPhoneticNameFields(std::shared_ptr<VCardNameData> nameData);
42     int32_t AddPhoneticName(const std::string &phoneticType, const std::string &phoneticName);
43     void DealNoEmptyFimilyOrGivenName(const std::string &familyName, const std::string &givenName,
44         const std::string &middleName, const std::string &prefix, const std::string &suffix,
45         const std::string &displayName);
46     void AddCharsetOrQuotedPrintable(bool needAddCharset, bool needAddQuotedPrintable);
47     void AddSinglePartNameField(std::string property, std::string part);
48     void AddCustomType(const std::string &type, std::vector<std::string> values);
49     void HandleCharacter(int32_t i, int32_t length, std::string value, std::string &temp);
50     std::string DealCharacters(std::string value);
51     std::string EncodeQuotedPrintable(const std::string &input);
52     bool IsNeedCharsetParam(std::vector<std::string> strs);
53     void AddLine(const std::string &type, const std::string &rawValue);
54     void AddLine(const std::string &type, std::vector<std::string> valueList);
55     void AddLine(const std::string &type, const std::string &rawValue, bool needCharset, bool needQuotedPrintable);
56     void AddLine(const std::string &type, const std::vector<std::string> &paramList, const std::string &rawValue);
57     void AddLine(const std::string &type, const std::vector<std::string> &paramList, const std::string &rawValue,
58         bool needCharset, bool useQuotedPrintable);
59     void AddLine(
60         const std::string &type, std::vector<std::string> valueList, bool needCharset, bool needQuotedPrintable);
61     void AddLine(const std::string &type, const std::vector<std::string> &paramList, std::vector<std::string> valueList,
62         bool needCharset, bool needQuotedPrintable);
63     void AddParamType(const std::string &paramType);
64     void AddParamTypes(std::vector<std::string> types);
65     void AddParamType(std::stringstream &result, const std::string &paramType);
66     std::string FormatFullName(
67         const std::string &givenName, const std::string &middleName, const std::string &familyName);
68     void AddLineWithCharsetAndQP(const std::string &type, std::vector<std::string> valueList);
69     void AddTelLine(const std::string &labelId, const std::string &labelName, const std::string &number);
70     void AddPhotoLine(const std::string &encodedValue, const std::string &photoType);
71     void AddEmailLine(
72         int32_t emailType, const std::string &labelName, const std::string &email, const std::string &displayName);
73     void ConstructPostalLine(std::shared_ptr<VCardPostalData> postalData, std::stringstream &postalLine,
74         bool &needCharset, bool &needAddQuotedPrintable);
75     void AddPostalLine(std::shared_ptr<VCardPostalData> postalData, int32_t postalType, const std::string &labelName);
76     std::string ToString();
77 
78 private:
79     int32_t ConstructName(std::shared_ptr<VCardContact> contact);
80     int32_t ConstructPhones(std::shared_ptr<VCardContact> contact);
81     int32_t ConstructRelation(std::shared_ptr<VCardContact> contact);
82     int32_t ConstructIms(std::shared_ptr<VCardContact> contact);
83     int32_t ConstructSipAddresses(std::shared_ptr<VCardContact> contact);
84     int32_t ConstructNickNames(std::shared_ptr<VCardContact> contact);
85     int32_t ConstructEmails(std::shared_ptr<VCardContact> contact);
86     int32_t ConstructPostals(std::shared_ptr<VCardContact> contact);
87     int32_t ConstructOrganizations(std::shared_ptr<VCardContact> contact);
88     int32_t ConstructWebsites(std::shared_ptr<VCardContact> contact);
89     int32_t ConstructPhotos(std::shared_ptr<VCardContact> contact);
90     int32_t ConstructNotes(std::shared_ptr<VCardContact> contact);
91     int32_t ConstructEvents(std::shared_ptr<VCardContact> contact);
92     int32_t ConstructNameV40(std::shared_ptr<VCardContact> contact);
93 
94 private:
95     size_t headLength_;
96     int32_t cardType_;
97     std::string charset_;
98     std::stringstream result_;
99     std::string charsetParam_;
100     std::shared_ptr<PhoneNumberEncodedCallback> phoneNumberEncodedCallback_;
101     bool isV30OrV40_;
102     bool needCharsetParam_;
103     bool needQP_;
104 };
105 } // namespace Telephony
106 } // namespace OHOS
107 #endif // OHOS_VCARD_CONSTRUCTOR_H
108