Initialize Tizen 2.3
[apps/home/b2-clocksetting.git] / src / util.c
1 /*
2  * Copyright (c) 2000 - 2013 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 /*
18  * util.c
19  *
20  *  Created on: Nov 21, 2013
21  *      Author: min-hoyun
22  */
23
24 #include <string.h>
25 #include <vconf.h>
26 #include <unicode/unum.h>
27 #include <fcntl.h>
28 #include "util.h"
29
30
31 char *setting_gettext(const char *s)
32 {
33         /* fisrt find in app pg */
34
35         if (s == NULL) {
36                 return "NULL";
37         }
38
39         char *p = dgettext(SETTING_PACKAGE, s);
40
41         if (!strcmp(s, p)) {    /* not found */
42                 /* find in system pkg */
43                 p = dgettext(SYSTEM_PACKAGE, s);
44         }
45         return p;
46 }
47
48 char * replace( char * str, char * orig, char * repl)
49 {
50         static char buffer[124];
51         char * ch;
52         if( !(ch = strstr(str, orig)))
53         {
54                 return str;
55         }
56         strncpy(buffer, str, ch - str);
57         buffer[ch - str] = 0;
58         sprintf(buffer + (ch - str), "%s%s", repl, ch + strlen(orig));
59
60         return buffer;
61 }
62
63 void setting_popup_back_cb(void *data, Evas_Object *obj, void *event_info)
64 {
65         appdata *ad = (appdata *)data;
66         if( ad == NULL ) {
67                 return;
68         }
69
70         if (ad->popup) {
71                 evas_object_del(ad->popup);
72                 ad->popup = NULL;
73         }
74 }
75
76 int is_connected_GM()
77 {
78         int enable = 0;
79         vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &enable);
80
81         return enable;
82 }
83
84 bool colorstr_to_decimal(char *color, int *R, int *G, int *B)
85 {
86         DBG("_colorstr_to_decimal");
87         if (color == NULL)
88                 return false;
89
90         char *ptr;
91         long value;
92         value = strtol(color, &ptr, 16);
93         *R = (value>>16)&0xFF;
94         *G = (value>>8)&0xFF;
95         *B = value&0xFF;
96         return true;
97 }
98
99 char *_get_strnum_from_icu(int number)
100 {
101         char *locale_tmp = vconf_get_str(VCONFKEY_REGIONFORMAT);
102         char locale[32] = {0,};
103         char *p = NULL;
104         strcpy(locale, locale_tmp);
105
106         if (locale[0] != '\0') {
107                 p = strstr(locale, ".UTF-8");
108                 if (p) {
109                         *p = 0; 
110                 }    
111         }    
112
113         char *ret_str = NULL;
114         UErrorCode status = U_ZERO_ERROR;
115         
116         UNumberFormat *num_fmt;
117         UChar result[20] = { 0, };
118         char res[20] = { 0, };
119         int32_t len = (int32_t) (sizeof(result) / sizeof((result)[0]));
120
121         num_fmt = unum_open(UNUM_DEFAULT, NULL, -1, locale, NULL, &status);
122         unum_format(num_fmt, number, result, len, NULL, &status);
123
124         u_austrcpy(res, result);
125
126         unum_close(num_fmt);
127
128         ret_str = strdup(res);
129         return ret_str;
130 }
131
132 bool is_file_exist(char * file_path)
133 {
134         int fd;
135
136         if( !file_path && !strlen(file_path) )
137         {
138                 DBG("Setting - file path is wrong!!");
139                 return false;
140         }
141
142         if( (fd = open(file_path, O_RDONLY)) == -1)
143         {
144                 DBG("Setting - file(%s) do not exist!!", file_path);
145                 return false;
146         }
147
148         DBG("Setting - file exist!!");
149
150         return true;
151 }