[Engine] patch for normal support of pkgmgr signal and thread problem
[framework/web/wrt-commons.git] / modules / localization / src / localization_utils.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    localization_utils.cpp
18  * @author  Bartosz Janiak (b.janiak@samsung.com)
19  * @version 1.0
20  */
21
22 #include <dpl/localization/localization_utils.h>
23
24 #include <appcore-efl.h>
25 #include <vconf.h>
26 #include <dpl/framework_efl.h>
27
28 #include <dpl/foreach.h>
29 #include <dpl/mutex.h>
30
31 namespace {
32
33 static int LanguageChanged(void *)
34 {
35     char* lang = vconf_get_str(VCONFKEY_LANGSET);
36     if (!lang) {
37         LogError("Cannot get locale settings from vconf");
38         return 0;
39     }
40     LogDebug("Language set to: " << lang);
41
42     using namespace LocalizationUtils;
43
44     LanguageTagsList list;
45     list.push_back(DPL::FromUTF8String(lang));
46     SetSystemLanguageTags(list);
47
48     LogDebug("LanguageChanged to " << lang);
49
50     return 0;
51 }
52 }
53
54 namespace LocalizationUtils {
55 static LanguageTagsList m_systemLanguageTags;
56 static LanguageTagsList m_userLanguageTags;
57 static LanguageTagsList m_languageTags;
58 static DPL::ReadWriteMutex m_readWriteMutex;
59
60 template<typename StringType>
61 void FindAndReplace(StringType& source,
62         const StringType& find,
63         const StringType& replace)
64 {
65     size_t pos = 0;
66     while ((pos = source.find(find, pos)) != StringType::npos) {
67         source.replace(pos, find.length(), replace);
68         pos += replace.length();
69     }
70 }
71
72 DPL::String BCP47LanguageTagToLocale(const DPL::String& inLanguageTag)
73 {
74     DPL::String languageTag(inLanguageTag);
75     FindAndReplace(languageTag, DPL::String(L"-"), DPL::String(L"_"));
76     return languageTag;
77 }
78
79 DPL::String LocaleToBCP47LanguageTag(const DPL::String& inLocaleString)
80 {
81     DPL::String localeString = inLocaleString.substr(
82             0,
83             inLocaleString.
84                 find_first_of(L"."));
85     FindAndReplace(localeString, DPL::String(L"_"), DPL::String(L"-"));
86     return localeString;
87 }
88
89 void UpdateUserAgentLanguageTags()
90 {
91     // WARNING!!!!!  This function shall be called
92     // only when mutex is locked in readWriteMode!
93
94     LanguageTagsList list = m_userLanguageTags;
95     list.insert(list.begin(),
96                 m_systemLanguageTags.begin(),
97                 m_systemLanguageTags.end());
98     m_languageTags.clear();
99
100     FOREACH(i, list) {
101         DPL::String tag = LocaleToBCP47LanguageTag(*i);
102         while (true) { //W3C Packaging 9. Step 5. 2. D
103             if (tag.empty()) { continue; }
104
105             LogDebug("Adding user locale '" << tag << "'");
106             m_languageTags.push_back(tag);
107
108             size_t subtagPos = tag.find_last_of(L"-");
109             if (subtagPos == DPL::String::npos) {
110                 break;
111             }
112             tag = tag.substr(0, subtagPos);
113         }
114     }
115
116     m_languageTags.push_back(L"en");
117     m_languageTags.push_back(L"");
118 }
119
120 void SetUserLanguageTags(const LanguageTagsList& tags)
121 {
122     DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
123     if (m_userLanguageTags != tags) {
124         m_userLanguageTags = tags;
125         UpdateUserAgentLanguageTags();
126     }
127 }
128
129 void SetSystemLanguageTags(const LanguageTagsList& tags)
130 {
131     DPL::ReadWriteMutex::ScopedWriteLock lock(&m_readWriteMutex);
132     if (m_systemLanguageTags != tags) {
133         m_systemLanguageTags = tags;
134         UpdateUserAgentLanguageTags();
135     }
136 }
137
138 LanguageTagsList GetUserAgentLanguageTags()
139 {
140     DPL::ReadWriteMutex::ScopedReadLock lock(&m_readWriteMutex);
141     return m_languageTags;
142 }
143
144 void Initialize()
145 {
146     appcore_set_event_callback(
147         APPCORE_EVENT_LANG_CHANGE,
148         &LanguageChanged,
149         NULL);
150
151     LanguageChanged(NULL); // updating language for the first time
152 }
153
154 }