Introduce libofono-efl-utils to be used by dialer and other applications.
[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 + 30))
77                 r = asprintf(&buf, "One minute ago");
78         else if (dt < (HOUR + 100))
79                 r = asprintf(&buf, "%d minutes ago", (int)dt/60);
80         else if (dt < (HOUR * 4))
81                 r = asprintf(&buf, "%d hours ago", (int)dt/3600);
82         else if (dt <= DAY) {
83                 struct tm *f_time = gmtime(&date);
84                 EINA_SAFETY_ON_NULL_GOTO(f_time, err_gmtime);
85                 r = asprintf(&buf,  "%02d:%02d", f_time->tm_hour,
86                                 f_time->tm_min);
87         } else if (dt < WEEK) {
88                 char tmp[256];
89                 struct tm *tm = localtime(&date);
90                 strftime(tmp, sizeof(tmp), "%A", tm);
91                 int days = dt / DAY;
92                 r = asprintf(&buf, "%s, %d days ago", tmp, days);
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 err_gmtime:
106         return strdup("");
107 }
108
109 Evas_Object *picture_icon_get(Evas_Object *parent, const char *picture)
110 {
111         Evas_Object *icon = elm_icon_add(parent);
112
113         if (!picture || *picture == '\0')
114                 elm_icon_standard_set(icon, "no-picture");
115         else {
116                 char path[PATH_MAX];
117                 const char *prefix;
118                 prefix = efreet_config_home_get();
119                 snprintf(path, sizeof(path), "%s/%s/%s", prefix, PACKAGE_NAME,
120                                 picture);
121                 elm_image_file_set(icon, path, NULL);
122         }
123         return icon;
124 }
125
126 Evas_Object *layout_add(Evas_Object *parent, const char *style)
127 {
128         Evas_Object *layout = elm_layout_add(parent);
129         if (!elm_layout_theme_set(layout, "layout", "dialer", style)) {
130                 CRITICAL("No theme for 'elm/layout/dialer/%s' at %s",
131                                 style, def_theme);
132                 evas_object_del(layout);
133                 return NULL;
134         }
135         return layout;
136 }
137
138 Eina_Bool util_init(const char *theme)
139 {
140         elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
141         elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
142         elm_app_info_set(util_init, "ofono-efl", "themes/default.edj");
143
144         snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
145                         elm_app_data_dir_get());
146
147         elm_theme_extension_add(NULL, def_theme);
148         if (!theme)
149                 elm_theme_overlay_add(NULL, def_theme);
150         else {
151                 char tmp[PATH_MAX];
152                 if (theme[0] != '/') {
153                         if (eina_str_has_suffix(theme, ".edj")) {
154                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s",
155                                                 elm_app_data_dir_get(), theme);
156                         } else {
157                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
158                                                 elm_app_data_dir_get(), theme);
159                         }
160                         theme = tmp;
161                 }
162                 elm_theme_overlay_add(NULL, theme);
163         }
164
165         return EINA_TRUE;
166 }
167
168 void util_shutdown(void)
169 {
170 }