Call screen now shows contact's photo
[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 #ifdef HAVE_TIZEN
117                 elm_icon_file_set(icon, picture, NULL);
118 #else
119                 char path[PATH_MAX];
120                 const char *prefix;
121                 prefix = efreet_config_home_get();
122                 snprintf(path, sizeof(path), "%s/%s/%s", prefix, PACKAGE_NAME,
123                                 picture);
124                 elm_image_file_set(icon, path, NULL);
125 #endif
126         }
127         return icon;
128 }
129
130 Evas_Object *layout_add(Evas_Object *parent, const char *style)
131 {
132         Evas_Object *layout = elm_layout_add(parent);
133         if (!elm_layout_theme_set(layout, "layout", "dialer", style)) {
134                 CRITICAL("No theme for 'elm/layout/dialer/%s' at %s",
135                                 style, def_theme);
136                 evas_object_del(layout);
137                 return NULL;
138         }
139         return layout;
140 }
141
142 Eina_Bool util_init(const char *theme)
143 {
144         elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
145         elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
146         elm_app_info_set(util_init, "ofono-efl", "themes/default.edj");
147
148         snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
149                         elm_app_data_dir_get());
150
151         elm_theme_extension_add(NULL, def_theme);
152         if (!theme)
153                 elm_theme_overlay_add(NULL, def_theme);
154         else {
155                 char tmp[PATH_MAX];
156                 if (theme[0] != '/') {
157                         if (eina_str_has_suffix(theme, ".edj")) {
158                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s",
159                                                 elm_app_data_dir_get(), theme);
160                         } else {
161                                 snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
162                                                 elm_app_data_dir_get(), theme);
163                         }
164                         theme = tmp;
165                 }
166                 elm_theme_overlay_add(NULL, theme);
167         }
168
169         return EINA_TRUE;
170 }
171
172 void util_shutdown(void)
173 {
174 }