Add Setting feature on system-server lib
[platform/framework/native/appfw.git] / src / system-server / setting / providers / FSys_Icu.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                FSys_Icu.cpp
19  * @brief               This is the implementation file for _Icu class.
20  */
21
22 #include <stdlib.h>
23 #include <memory.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBase_NativeError.h>
27 #include "FSys_Icu.h"
28
29 namespace Tizen { namespace System
30 {
31
32 static const int _MAX_COUNTRY_LENTH = 2;
33 static const int _LANGUAGE_START_INDEX = 0;
34 static const int _MAX_LANGUAGE_LENTH = 2;
35
36 _Icu::_Icu()
37 {
38 }
39
40 _Icu::~_Icu()
41 {
42 }
43
44 char*
45 _Icu::GetIcuPatternN(const char* pLocale, UChar* pCustomSkeleton)
46 {
47         UErrorCode status = U_ZERO_ERROR;
48         UDateTimePatternGenerator* pGenerator = null;
49         UChar bestPattern[64] = {0,};
50
51         if(pLocale == null || pCustomSkeleton == null)
52         {
53                 return null;
54         }
55
56         char*   pBestPatternString = null;
57         int32_t bestPatternCapacity = 0;
58         int32_t bestPatternLength = 0;
59
60         pGenerator = udatpg_open(pLocale, &status);
61
62         if (U_FAILURE(status) == true)
63         {
64                 return null;
65         }
66
67         status = U_ZERO_ERROR;
68
69         bestPatternCapacity = (int32_t)(sizeof(bestPattern)/sizeof((bestPattern)[0]));
70         bestPatternLength = udatpg_getBestPattern(pGenerator, pCustomSkeleton, u_strlen(pCustomSkeleton), bestPattern, bestPatternCapacity, &status);
71         if (U_FAILURE(status) == true)
72         {
73                 return null;
74         }
75
76         status = U_ZERO_ERROR;
77         pBestPatternString = (char*)malloc(128);
78         if (pBestPatternString == null)
79         {
80                 return null;
81         }
82         memset(pBestPatternString, 0, 128);
83         u_austrncpy(pBestPatternString, bestPattern, 128);
84         udatpg_close(pGenerator);
85         SysLog(NID_SYS, "Format is [%s] \n", pBestPatternString);
86         return pBestPatternString;
87 }
88
89 char*
90 _Icu::GetDateTimeFormatN(const char* pIcuFormat)
91 {
92         char* pFormatString = null;
93         char* pDateTimeFormat = null;
94
95         if (pIcuFormat == null)
96         {
97                 return null;
98         }
99
100         UChar   customSkeleton[256] = {0, };
101         const char* pLocaleDefault = uloc_getDefault();
102
103         pDateTimeFormat = const_cast< char* > (pLocaleDefault);
104
105         u_uastrncpy(customSkeleton, pIcuFormat, sizeof(customSkeleton));
106         pFormatString = GetIcuPatternN(pDateTimeFormat, customSkeleton);
107
108         return pFormatString;
109 }
110
111 } } // Tizen::System