Fix TC-2372 Dialer crashes when BT phone is offline
[profile/ivi/lemolo.git] / utils / util.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <Eina.h>
6 #include <time.h>
7 #include <Evas.h>
8 #include <Elementary.h>
9
10 #include "util.h"
11 #include "log.h"
12
13 static char def_theme[PATH_MAX] = "";
14 static char *_last_user_mode_config_path;
15 static Eet_Data_Descriptor *_last_user_mode_descriptor;
16
17 /* TODO: find a configurable way to format the number.
18  * Right now it's: 1-234-567-8901 as per
19  * http://en.wikipedia.org/wiki/Local_conventions_for_writing_telephone_numbers#North_America
20  *
21  * IDEA: use ordered set of regexp -> replacement.
22  */
23 char *phone_format(const char *number)
24 {
25         size_t i, slen;
26         Eina_Strbuf *d;
27         char *ret;
28
29         if (!number)
30                 return NULL;
31
32         slen = strlen(number);
33         if (slen < 1)
34                 return NULL;
35
36         if ((slen <= 4) || (slen > 12))
37                 goto show_verbatim;
38
39         if ((slen == 12) && (number[0] != '+'))
40                 goto show_verbatim;
41
42         for (i = 0; i < slen; i++) {
43                 if ((number[i] < '0') || (number[i] > '9')) {
44                         if ((number[i] == '+') && (i == 0))
45                                 continue;
46                         goto show_verbatim;
47                 }
48         }
49
50         d = eina_strbuf_new();
51         eina_strbuf_append_length(d, number, slen);
52         if ((slen > 4) && (number[slen - 5] != '+'))
53                 eina_strbuf_insert_char(d, '-', slen - 4);
54         if (slen > 7) {
55                 if ((slen > 7) && (number[slen - 8] != '+'))
56                         eina_strbuf_insert_char(d, '-', slen - 7);
57                 if ((slen > 10) && (number[slen - 11] != '+'))
58                         eina_strbuf_insert_char(d, '-', slen - 10);
59         }
60
61         ret = eina_strbuf_string_steal(d);
62         eina_strbuf_free(d);
63         return ret;
64
65 show_verbatim:
66         return strdup(number);
67 }
68
69 char *date_format(time_t date)
70 {
71         time_t now = time(NULL);
72         double dt = difftime(now, date);
73         char *buf;
74         int r;
75
76         if (dt < 30)
77                 r = asprintf(&buf, "Just now");
78         else if (dt < (MINUTE * 2))
79                 r = asprintf(&buf, "One minute ago");
80         else if (dt < (HOUR * 2))
81                 r = asprintf(&buf, "%dmin ago", (int)dt/60);
82         else if (dt < (HOUR * 4))
83                 r = asprintf(&buf, "%dh ago", (int)dt/3600);
84         else if (dt <= DAY) {
85                 struct tm *f_time = localtime(&date);
86                 r = asprintf(&buf,  "%02d:%02d", f_time->tm_hour,
87                                 f_time->tm_min);
88         } else if (dt < WEEK) {
89                 char tmp[256];
90                 struct tm *tm = localtime(&date);
91                 strftime(tmp, sizeof(tmp), "%A", tm);
92                 r = asprintf(&buf, "%s", tmp);
93         } else {
94                 char tmp[256];
95                 struct tm *tm = localtime(&date);
96                 strftime(tmp, sizeof(tmp), "%x", tm);
97                 r = asprintf(&buf, "%s", tmp);
98         }
99
100         if (r < 0)
101                 buf = strdup("");
102
103         return buf;
104 }
105
106 char *date_short_format(time_t date)
107 {
108         time_t now = time(NULL);
109         double dt = difftime(now, date);
110         char *buf;
111         int r;
112
113         if (dt <= DAY) {
114                 struct tm *f_time = localtime(&date);
115                 r = asprintf(&buf,  "%02d:%02d", f_time->tm_hour,
116                                 f_time->tm_min);
117         } else if (dt < WEEK) {
118                 char tmp[256];
119                 struct tm *tm = localtime(&date);
120                 strftime(tmp, sizeof(tmp), "%A", tm);
121                 r = asprintf(&buf, "%s", tmp);
122         } else {
123                 char tmp[256];
124                 struct tm *tm = localtime(&date);
125                 strftime(tmp, sizeof(tmp), "%x", tm);
126                 r = asprintf(&buf, "%s", tmp);
127         }
128
129         if (r < 0)
130                 buf = strdup("");
131
132         return buf;
133 }
134
135 Evas_Object *picture_icon_get(Evas_Object *parent, const char *picture)
136 {
137         Evas_Object *icon = elm_icon_add(parent);
138
139         if (!picture || *picture == '\0')
140                 elm_icon_standard_set(icon, "no-picture");
141         else {
142                 char path[PATH_MAX];
143                 const char *prefix;
144                 prefix = efreet_config_home_get();
145                 snprintf(path, sizeof(path), "%s/%s/%s", prefix, PACKAGE_NAME,
146                                 picture);
147                 elm_image_file_set(icon, path, NULL);
148         }
149         return icon;
150 }
151
152 Evas_Object *layout_add(Evas_Object *parent, const char *style)
153 {
154         Evas_Object *layout = elm_layout_add(parent);
155         if (!elm_layout_theme_set(layout, "layout", "ofono-efl", style)) {
156                 CRITICAL("No theme for 'elm/layout/ofono-efl/%s' at %s",
157                                 style, def_theme);
158                 evas_object_del(layout);
159                 return NULL;
160         }
161         return layout;
162 }
163
164 Eina_Bool util_set_night_mode(Eina_Bool night_mode)
165 {
166         snprintf(def_theme, sizeof(def_theme), "%s/themes/night.edj",
167                         elm_app_data_dir_get());
168
169         elm_theme_extension_add(NULL, def_theme);
170
171         if (night_mode)
172         {
173                 night_mode = EINA_TRUE;
174                 elm_theme_overlay_add(NULL, def_theme);
175                 DBG("Load theme at %s", def_theme);
176         }
177         else
178         {
179                 night_mode = EINA_FALSE;
180                 elm_theme_overlay_del(NULL, def_theme);
181                 DBG("Unload theme at %s", def_theme);
182         }
183
184         return EINA_TRUE;
185 }
186
187 Eina_Bool util_set_last_user_mode(Last_User_Mode *mode) {
188         Eina_Bool ret;
189         Eet_File *efile;
190
191         EINA_SAFETY_ON_NULL_RETURN_VAL(_last_user_mode_config_path, EINA_FALSE);
192         EINA_SAFETY_ON_NULL_RETURN_VAL(_last_user_mode_descriptor, EINA_FALSE);
193
194         efile = eet_open(_last_user_mode_config_path, EET_FILE_MODE_WRITE);
195         if (!efile) {
196                 ERR("Cannot open %s for write", _last_user_mode_config_path);
197                 return EINA_FALSE;
198         }
199
200         ret = eet_data_write(efile, _last_user_mode_descriptor, LAST_USER_MODE_ENTRY, mode, EINA_TRUE);
201         if (!ret) {
202                 ERR("Cannot write to %s", _last_user_mode_config_path);
203         }
204         eet_close(efile);
205         return ret;
206 }
207
208 Last_User_Mode *util_get_last_user_mode() {
209         Eet_File *efile;
210         Last_User_Mode *mode;
211
212         EINA_SAFETY_ON_NULL_RETURN_VAL(_last_user_mode_config_path, NULL);
213
214         efile = eet_open(_last_user_mode_config_path, EET_FILE_MODE_READ);
215         if (!efile) {
216                 ERR("Cannot open %s for read", _last_user_mode_config_path);
217                 return NULL;
218         }
219
220         mode = eet_data_read(efile, _last_user_mode_descriptor, LAST_USER_MODE_ENTRY);
221         eet_close(efile);
222
223         return mode;
224 }
225
226 static void _last_user_mode_descriptor_init(void) {
227         Eet_Data_Descriptor_Class eddc;
228
229         EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Last_User_Mode);
230         _last_user_mode_descriptor = eet_data_descriptor_stream_new(&eddc);
231
232         EET_DATA_DESCRIPTOR_ADD_BASIC(_last_user_mode_descriptor, Last_User_Mode,
233                                         "last_view", last_view, EET_T_UINT);
234         EET_DATA_DESCRIPTOR_ADD_BASIC(_last_user_mode_descriptor, Last_User_Mode,
235                                         "last_history_view", last_history_view, EET_T_UINT);
236         EET_DATA_DESCRIPTOR_ADD_BASIC(_last_user_mode_descriptor, Last_User_Mode,
237                                         "last_number", last_number, EET_T_STRING);
238 }
239
240
241 Eina_Bool util_init(const char *theme)
242 {
243         int ret;
244         const char *config_path;
245         char base_dir[PATH_MAX];
246
247         eet_init();
248         efreet_init();
249         config_path = efreet_config_home_get();
250         snprintf(base_dir, sizeof(base_dir), "%s/%s", config_path, PACKAGE_NAME);
251         ecore_file_mkpath(base_dir);
252         ret = asprintf(&_last_user_mode_config_path,  "%s/%s/last_user_mode.eet", config_path, PACKAGE_NAME);
253         if (ret < 0)
254                 return EINA_FALSE;
255
256         _last_user_mode_descriptor_init();
257         elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
258         elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
259         elm_app_info_set(util_init, "ofono-efl", "themes/default.edj");
260
261         snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
262                         elm_app_data_dir_get());
263
264         elm_theme_extension_add(NULL, def_theme);
265         if (!theme)
266                 elm_theme_overlay_add(NULL, def_theme);
267         else {
268                 char tmp[PATH_MAX];
269                 if (theme[0] != '/') {
270                         if (eina_str_has_suffix(theme, ".edj")) {
271                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s",
272                                                 elm_app_data_dir_get(), theme);
273                         } else {
274                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
275                                                 elm_app_data_dir_get(), theme);
276                         }
277                         theme = tmp;
278                 }
279                 elm_theme_overlay_add(NULL, theme);
280         }
281
282         return EINA_TRUE;
283 }
284
285 void util_shutdown(void)
286 {
287         if (_last_user_mode_config_path)
288                 free(_last_user_mode_config_path);
289         if (_last_user_mode_descriptor)
290                 eet_data_descriptor_free(_last_user_mode_descriptor);
291         efreet_shutdown();
292         eet_shutdown();
293 }