• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "construction_name.h"
17 
18 #include "character_transliterate.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace Contacts {
23 std::string ConstructionName::local = "zh-CN";
ConstructionName()24 ConstructionName::ConstructionName()
25 {
26 }
27 
~ConstructionName()28 ConstructionName::~ConstructionName()
29 {
30 }
31 
GetConstructionName(std::string & chineseCharacter,ConstructionName & constructionName)32 ConstructionName ConstructionName::GetConstructionName(
33     std::string &chineseCharacter, ConstructionName &constructionName)
34 {
35     CharacterTransliterate characterTransliterate;
36     if (strcmp(local.c_str(), "zh-CN") == 0) {
37         Container container =
38             characterTransliterate.GetContainer(characterTransliterate.StringToWstring(chineseCharacter));
39         std::wstring split(L"||");
40         std::wstring initials = characterTransliterate.Join(container.initialsContainer_, split);
41         std::wstring nameFullFight = characterTransliterate.Join(container.nameFullFightContainer_, split);
42         constructionName.initials_ = characterTransliterate.WstringToString(initials);
43         constructionName.nameFullFight_ = characterTransliterate.WstringToString(nameFullFight);
44         const wchar_t *nameFullFightInitials = nameFullFight.c_str();
45         if ((nameFullFightInitials[0] >= L'a' && nameFullFightInitials[0] <= L'z') ||
46             (nameFullFightInitials[0] >= L'A' && nameFullFightInitials[0] <= L'Z')) {
47             std::string sortFirstLetterTemp = characterTransliterate.WstringToString(nameFullFight.substr(0, 1));
48             std::transform(
49                 sortFirstLetterTemp.begin(), sortFirstLetterTemp.end(), sortFirstLetterTemp.begin(), std::toupper);
50             constructionName.sortFirstLetter_ = sortFirstLetterTemp;
51             int code = constructionName.sortFirstLetter_.c_str()[0];
52             constructionName.sortFirstLetterCode_ = code;
53             HILOG_INFO(" GetConstructionName sortFirstLetterCode :%{public}d", code);
54         } else {
55             std::wstring sortFirstLetter(L"#");
56             constructionName.sortFirstLetter_ = characterTransliterate.WstringToString(sortFirstLetter);
57             constructionName.sortFirstLetterCode_ = -1;
58         }
59     } else {
60         constructionName.initials_ = chineseCharacter;
61         constructionName.nameFullFight_ = chineseCharacter;
62         if ((chineseCharacter[0] >= 'a' && chineseCharacter[0] <= 'z') ||
63             (chineseCharacter[0] >= 'A' && chineseCharacter[0] <= 'Z')) {
64             std::string sortFirstLetterTemp = chineseCharacter.substr(0, 1);
65             std::transform(
66                 sortFirstLetterTemp.begin(), sortFirstLetterTemp.end(), sortFirstLetterTemp.begin(), std::toupper);
67             constructionName.sortFirstLetter_ = sortFirstLetterTemp;
68             int code = constructionName.sortFirstLetter_.c_str()[0];
69             constructionName.sortFirstLetterCode_ = code;
70             HILOG_INFO(" GetConstructionName sortFirstLetterCode1 :%{public}d", code);
71         } else {
72             std::string sortFirstLetter("#");
73             constructionName.sortFirstLetter_ = sortFirstLetter;
74             constructionName.sortFirstLetterCode_ = -1;
75         }
76     }
77     constructionName.disPlayName_ = chineseCharacter;
78     return GetPhotoFirstName(constructionName);
79 }
80 
GetPhotoFirstName(ConstructionName & constructionName)81 ConstructionName ConstructionName::GetPhotoFirstName(ConstructionName &constructionName)
82 {
83     constructionName.photoFirstName_.clear();
84     if (!constructionName.disPlayName_.empty()) {
85         CharacterTransliterate characterTransliterate;
86         std::wstring nameWstr = characterTransliterate.StringToWstring(constructionName.disPlayName_);
87         unsigned int len = nameWstr.size();
88         for (unsigned int index = 0; index < len; index++) {
89             if (characterTransliterate.IsChineseCharacter(nameWstr[index])) {
90                 std::wstring childwChineseCharacter = nameWstr.substr(index, 1);
91                 constructionName.photoFirstName_ = characterTransliterate.WstringToString(childwChineseCharacter);
92                 return constructionName;
93             }
94         }
95         if ((constructionName.disPlayName_[0] >= 'a' && constructionName.disPlayName_[0] <= 'z') ||
96             (constructionName.disPlayName_[0] >= 'A' && constructionName.disPlayName_[0] <= 'Z')) {
97             constructionName.photoFirstName_ = constructionName.disPlayName_[0];
98         }
99     }
100     return constructionName;
101 }
102 } // namespace Contacts
103 } // namespace OHOS