tizen 2.3.1 release
[apps/home/b2-clocksetting.git] / src / util.c
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  *  Licensed under the Flora License, Version 1.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://floralicense.org/license/
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                 return str;
54         }
55         strncpy(buffer, str, ch - str);
56         buffer[ch - str] = 0;
57         sprintf(buffer + (ch - str), "%s%s", repl, ch + strlen(orig));
58
59         return buffer;
60 }
61
62 void setting_popup_back_cb(void *data, Evas_Object *obj, void *event_info)
63 {
64         appdata *ad = (appdata *)data;
65         if (ad == NULL) {
66                 return;
67         }
68
69         if (ad->popup) {
70                 evas_object_del(ad->popup);
71                 ad->popup = NULL;
72         }
73 }
74
75 int is_connected_GM()
76 {
77         int enable = 0;
78         vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &enable);
79
80         return enable;
81 }
82
83 bool colorstr_to_decimal(char *color, int *R, int *G, int *B)
84 {
85         DBG("_colorstr_to_decimal");
86         if (color == NULL)
87                 return false;
88
89         char *ptr;
90         long value;
91         value = strtol(color, &ptr, 16);
92         *R = (value >> 16) & 0xFF;
93         *G = (value >> 8) & 0xFF;
94         *B = value & 0xFF;
95         return true;
96 }
97
98 char *_get_strnum_from_icu(int number)
99 {
100         char *locale_tmp = vconf_get_str(VCONFKEY_REGIONFORMAT);
101         char locale[32] = {0,};
102         char *p = NULL;
103         if (strlen(locale_tmp) < 32)
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) {
137                 DBG("Setting - file path is wrong!!");
138                 return false;
139         }
140
141         if ((fd = open(file_path, O_RDONLY)) == -1) {
142                 DBG("Setting - file(%s) do not exist!!", file_path);
143                 return false;
144         }
145
146         DBG("Setting - file exist!!");
147
148         return true;
149 }