Version bump
[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
15 /* TODO: find a configurable way to format the number.
16  * Right now it's: 1-234-567-8901 as per
17  * http://en.wikipedia.org/wiki/Local_conventions_for_writing_telephone_numbers#North_America
18  *
19  * IDEA: use ordered set of regexp -> replacement.
20  */
21 char *phone_format(const char *number)
22 {
23         size_t i, slen;
24         Eina_Strbuf *d;
25         char *ret;
26
27         if (!number)
28                 return NULL;
29
30         slen = strlen(number);
31         if (slen < 1)
32                 return NULL;
33
34         if ((slen <= 4) || (slen > 12))
35                 goto show_verbatim;
36
37         if ((slen == 12) && (number[0] != '+'))
38                 goto show_verbatim;
39
40         for (i = 0; i < slen; i++) {
41                 if ((number[i] < '0') || (number[i] > '9')) {
42                         if ((number[i] == '+') && (i == 0))
43                                 continue;
44                         goto show_verbatim;
45                 }
46         }
47
48         d = eina_strbuf_new();
49         eina_strbuf_append_length(d, number, slen);
50         if ((slen > 4) && (number[slen - 5] != '+'))
51                 eina_strbuf_insert_char(d, '-', slen - 4);
52         if (slen > 7) {
53                 if ((slen > 7) && (number[slen - 8] != '+'))
54                         eina_strbuf_insert_char(d, '-', slen - 7);
55                 if ((slen > 10) && (number[slen - 11] != '+'))
56                         eina_strbuf_insert_char(d, '-', slen - 10);
57         }
58
59         ret = eina_strbuf_string_steal(d);
60         eina_strbuf_free(d);
61         return ret;
62
63 show_verbatim:
64         return strdup(number);
65 }
66
67 char *date_format(time_t date)
68 {
69         time_t now = time(NULL);
70         double dt = difftime(now, date);
71         char *buf;
72         int r;
73
74         if (dt < 30)
75                 r = asprintf(&buf, "Just now");
76         else if (dt < (MINUTE * 2))
77                 r = asprintf(&buf, "One minute ago");
78         else if (dt < (HOUR * 2))
79                 r = asprintf(&buf, "%dmin ago", (int)dt/60);
80         else if (dt < (HOUR * 4))
81                 r = asprintf(&buf, "%dh ago", (int)dt/3600);
82         else if (dt <= DAY) {
83                 struct tm *f_time = localtime(&date);
84                 r = asprintf(&buf,  "%02d:%02d", f_time->tm_hour,
85                                 f_time->tm_min);
86         } else if (dt < WEEK) {
87                 char tmp[256];
88                 struct tm *tm = localtime(&date);
89                 strftime(tmp, sizeof(tmp), "%A", tm);
90                 r = asprintf(&buf, "%s", tmp);
91         } else {
92                 char tmp[256];
93                 struct tm *tm = localtime(&date);
94                 strftime(tmp, sizeof(tmp), "%x", tm);
95                 r = asprintf(&buf, "%s", tmp);
96         }
97
98         if (r < 0)
99                 buf = strdup("");
100
101         return buf;
102 }
103
104 char *date_short_format(time_t date)
105 {
106         time_t now = time(NULL);
107         double dt = difftime(now, date);
108         char *buf;
109         int r;
110
111         if (dt <= DAY) {
112                 struct tm *f_time = localtime(&date);
113                 r = asprintf(&buf,  "%02d:%02d", f_time->tm_hour,
114                                 f_time->tm_min);
115         } else if (dt < WEEK) {
116                 char tmp[256];
117                 struct tm *tm = localtime(&date);
118                 strftime(tmp, sizeof(tmp), "%A", tm);
119                 r = asprintf(&buf, "%s", tmp);
120         } else {
121                 char tmp[256];
122                 struct tm *tm = localtime(&date);
123                 strftime(tmp, sizeof(tmp), "%x", tm);
124                 r = asprintf(&buf, "%s", tmp);
125         }
126
127         if (r < 0)
128                 buf = strdup("");
129
130         return buf;
131 }
132
133 Evas_Object *picture_icon_get(Evas_Object *parent, const char *picture)
134 {
135         Evas_Object *icon = elm_icon_add(parent);
136
137         if (!picture || *picture == '\0')
138                 elm_icon_standard_set(icon, "no-picture");
139         else {
140                 char path[PATH_MAX];
141                 const char *prefix;
142                 prefix = efreet_config_home_get();
143                 snprintf(path, sizeof(path), "%s/%s/%s", prefix, PACKAGE_NAME,
144                                 picture);
145                 elm_image_file_set(icon, path, NULL);
146         }
147         return icon;
148 }
149
150 Evas_Object *layout_add(Evas_Object *parent, const char *style)
151 {
152         Evas_Object *layout = elm_layout_add(parent);
153         if (!elm_layout_theme_set(layout, "layout", "ofono-efl", style)) {
154                 CRITICAL("No theme for 'elm/layout/ofono-efl/%s' at %s",
155                                 style, def_theme);
156                 evas_object_del(layout);
157                 return NULL;
158         }
159         return layout;
160 }
161
162 Eina_Bool util_init(const char *theme)
163 {
164         elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
165         elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
166         elm_app_info_set(util_init, "ofono-efl", "themes/default.edj");
167
168         snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
169                         elm_app_data_dir_get());
170
171         elm_theme_extension_add(NULL, def_theme);
172         if (!theme)
173                 elm_theme_overlay_add(NULL, def_theme);
174         else {
175                 char tmp[PATH_MAX];
176                 if (theme[0] != '/') {
177                         if (eina_str_has_suffix(theme, ".edj")) {
178                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s",
179                                                 elm_app_data_dir_get(), theme);
180                         } else {
181                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
182                                                 elm_app_data_dir_get(), theme);
183                         }
184                         theme = tmp;
185                 }
186                 elm_theme_overlay_add(NULL, theme);
187         }
188
189         return EINA_TRUE;
190 }
191
192 void util_shutdown(void)
193 {
194 }