Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / locales / FLcl_LocaleData.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file        FLclLocaleData.cpp
19  * @brief       This is the implementation file for _LocaleData class.
20  */
21
22 // Includes
23 #include <locale.h>
24 #include <unistd.h>
25 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringUtil.h>
28 #include "FBase_StringConverter.h"
29 #include "FLcl_LocaleData.h"
30
31
32 // Other defines
33 #define LOCALE_DATA_NUM_OF_MILLISEC_IN_HOUR 3600000LL   // number of mili second in a hour
34 #define LOCALE_DATA_NUM_OF_MILLISEC_IN_MINUTE 60000LL   // number of mili second in a minute
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Base::Utility;
39
40 namespace Tizen { namespace Locales
41 {
42
43 IcuFieldPosition
44 _LocaleData::GetIcuFieldPosition(_FieldPosition pos)
45 {
46         IcuFieldPosition icuPos(pos.GetField());            // Create IcuFieldPosition with field value in pos
47         icuPos.setBeginIndex(pos.GetBeginIndex());          // Set Beginning index
48         icuPos.setEndIndex(pos.GetEndIndex());              // Set End index
49         return icuPos;
50 }
51
52
53 U_ICU_NAMESPACE::UnicodeString
54 _LocaleData::GetIcuString(const String& ospStr)
55 {
56         IcuUnicodeString icuStr;
57
58         if (!ospStr.IsEmpty())                                                     // if ospStr is not empty
59         {
60                 ByteBuffer* pBuff = null;
61                 pBuff = StringUtil::StringToUtf8N(ospStr);                             // Convert unicode value to UTF8
62                 if (pBuff)
63                 {
64                         icuStr = IcuUnicodeString((const char*) pBuff->GetPointer());        // Create ICU string using UTF8 array
65                         icuStr.setCharAt(0, icuStr.charAt(0));                              // This is to handle ICU copy on write design
66                         delete pBuff;                                                       // delete temporary buffer
67                 }
68         }
69
70         return icuStr;
71 }
72
73
74 String
75 _LocaleData::GetOspString(const IcuUnicodeString& icuStr)
76 {
77         int len = icuStr.length();                                  // get length
78         if (len > 0)                                                    // if icuStr is not empty
79         {
80                 wchar_t wstr[len + 1];
81                 IcuUnicodeString tmpIcuStr(icuStr);
82                 for (int i = 0; i < len ; i++)
83                 {
84                         UChar icuChar;
85                         icuChar = tmpIcuStr.charAt(i);
86                         wstr[i] = (wchar_t)icuChar;
87                 }
88                 wstr[len] = 0;
89                 return String(wstr);
90         }
91         return String("");
92 }
93
94
95 ArrayList*
96 _LocaleData::ConvertIcuStringArrayToOspArrayN(const IcuUnicodeString* pIcuStrList, int count)
97 {
98         SysTryReturn(NID_LCL, pIcuStrList && count > 0, null, E_INVALID_ARG, "It is invalid argument.");
99         
100         result r = E_SUCCESS;
101         std::unique_ptr< ArrayList> pTempArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
102         SysTryReturn(NID_LCL, pTempArrayList, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
103         pTempArrayList->Construct(count);
104         
105         for (int i = 0; i < count; i++)
106         {
107                 std::unique_ptr< String > pString(new (std::nothrow) String(_LocaleData::GetOspString(pIcuStrList[i])));
108                 SysTryReturn(NID_LCL, pString, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
109
110                 if (!pString->IsEmpty())
111                 {
112                         r = pTempArrayList->Add(pString.get());                    // Add OSP string to arraylist if it is not empty
113                         SysTryReturn(NID_LCL, !IsFailed(r), null, E_SYSTEM, "It is failed to add string [%ls]", pString->GetPointer());
114                         pString.release();
115                 }
116         }
117
118         SetLastResult(r);                                                           // Setting last result value
119         return pTempArrayList.release();                                                      // Return array list
120 }
121
122 IcuUnicodeString*
123 _LocaleData::ConvertOspArrayToIcuStringArrayN(const Tizen::Base::Collection::IList* pIcuStrList, int& count)
124 {
125         count = 0;
126         SysTryReturn(NID_LCL, pIcuStrList, null, E_INVALID_ARG, "[%s] Invalid argument is used. pIcuStrList is null.", GetErrorMessage(E_INVALID_ARG));
127
128         count = pIcuStrList->GetCount();
129         SysTryReturn(NID_LCL, count > 0, null, E_INVALID_ARG, "The list is empty.");
130
131         std::unique_ptr< IcuUnicodeString[] > pIcuStrArray(new IcuUnicodeString[count]);
132         SysTryReturn(NID_LCL, pIcuStrArray, null, E_OUT_OF_MEMORY, "It is not enough memory.");
133
134         std::unique_ptr< IEnumerator > pEnum(pIcuStrList->GetEnumeratorN());
135         SysTryReturn(NID_LCL, pEnum, null, E_SYSTEM, "It is failed to get enumerator.");
136
137         int i = 0;
138         String* pObj = null;
139         while (pEnum->MoveNext() == E_SUCCESS)
140         {
141                 pObj = static_cast< String* >(pEnum->GetCurrent());
142                 if (pObj)
143                 {
144                         pIcuStrArray[i] = GetIcuString(*pObj);
145                         i++;
146                 }
147         }
148         return pIcuStrArray.release();
149 }
150
151
152 IcuLocale
153 _LocaleData::GetIcuLocale(const Locale& ospLocale)
154 {
155         ClearLastResult();
156         String language = ospLocale.GetLanguageCodeString();
157         String country = ospLocale.GetCountryCodeString();
158         String variant = ospLocale.GetVariantCodeString();
159
160         const char* pLangStr = _StringConverter::CopyToCharArrayN(language);
161         const char* pCountryStr = _StringConverter::CopyToCharArrayN(country);
162         const char* pVariantStr = null;
163
164         if (!variant.IsEmpty())
165         {
166                 pVariantStr = _StringConverter::CopyToCharArrayN(variant);
167         }
168
169         IcuLocale icuLocale = IcuLocale(pLangStr, pCountryStr, pVariantStr);
170
171         delete[] pLangStr;
172         delete[] pCountryStr;
173         delete[] pVariantStr;
174
175         if (icuLocale.isBogus())
176         {
177                 SetLastResult(E_SYSTEM);
178         }
179
180         return icuLocale;
181 }
182
183
184 IcuUDate
185 _LocaleData::GetIcuDate(DateTime date)
186 {
187         DateTime icuBaseTime;
188         icuBaseTime.SetValue(1970, 1, 1);
189         DateTime ospBaseTime = DateTime::GetMinValue();
190
191         result r = icuBaseTime.Subtract(ospBaseTime.GetTime());
192         if (r != E_SUCCESS)
193         {
194                 SysLogException(NID_LCL, r, "[%s] Propagated.", GetErrorMessage(r));
195         }
196
197         TimeSpan tsIcu = icuBaseTime.GetTime();
198         TimeSpan tsOsp = date.GetTime();
199         TimeSpan diff = tsOsp - tsIcu;
200
201         IcuUDate icuDate = diff.GetTicks();
202         return icuDate;
203 }
204
205 _LocaleData::_LocaleData(void)
206 {
207 }
208
209
210 _LocaleData::~_LocaleData(void)
211 {
212 }
213
214
215 };
216 };      // Tizen::Locales
217