8 #include <Elementary.h>
13 static char def_theme[PATH_MAX] = "";
14 static char *_last_user_mode_config_path;
15 static Eet_Data_Descriptor *_last_user_mode_descriptor;
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
21 * IDEA: use ordered set of regexp -> replacement.
23 char *phone_format(const char *number)
32 slen = strlen(number);
36 if ((slen <= 4) || (slen > 12))
39 if ((slen == 12) && (number[0] != '+'))
42 for (i = 0; i < slen; i++) {
43 if ((number[i] < '0') || (number[i] > '9')) {
44 if ((number[i] == '+') && (i == 0))
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);
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);
61 ret = eina_strbuf_string_steal(d);
66 return strdup(number);
69 char *date_format(time_t date)
71 time_t now = time(NULL);
72 double dt = difftime(now, date);
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);
85 struct tm *f_time = localtime(&date);
86 r = asprintf(&buf, "%02d:%02d", f_time->tm_hour,
88 } else if (dt < WEEK) {
90 struct tm *tm = localtime(&date);
91 strftime(tmp, sizeof(tmp), "%A", tm);
92 r = asprintf(&buf, "%s", tmp);
95 struct tm *tm = localtime(&date);
96 strftime(tmp, sizeof(tmp), "%x", tm);
97 r = asprintf(&buf, "%s", tmp);
106 char *date_short_format(time_t date)
108 time_t now = time(NULL);
109 double dt = difftime(now, date);
114 struct tm *f_time = localtime(&date);
115 r = asprintf(&buf, "%02d:%02d", f_time->tm_hour,
117 } else if (dt < WEEK) {
119 struct tm *tm = localtime(&date);
120 strftime(tmp, sizeof(tmp), "%A", tm);
121 r = asprintf(&buf, "%s", tmp);
124 struct tm *tm = localtime(&date);
125 strftime(tmp, sizeof(tmp), "%x", tm);
126 r = asprintf(&buf, "%s", tmp);
135 Evas_Object *picture_icon_get(Evas_Object *parent, const char *picture)
137 Evas_Object *icon = elm_icon_add(parent);
139 if (!picture || *picture == '\0')
140 elm_icon_standard_set(icon, "no-picture");
144 prefix = efreet_config_home_get();
145 snprintf(path, sizeof(path), "%s/%s/%s", prefix, PACKAGE_NAME,
147 elm_image_file_set(icon, path, NULL);
152 Evas_Object *layout_add(Evas_Object *parent, const char *style)
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",
158 evas_object_del(layout);
164 Eina_Bool util_set_night_mode(Eina_Bool night_mode)
166 snprintf(def_theme, sizeof(def_theme), "%s/themes/night.edj",
167 elm_app_data_dir_get());
169 elm_theme_extension_add(NULL, def_theme);
173 night_mode = EINA_TRUE;
174 elm_theme_overlay_add(NULL, def_theme);
175 DBG("Load theme at %s", def_theme);
179 night_mode = EINA_FALSE;
180 elm_theme_overlay_del(NULL, def_theme);
181 DBG("Unload theme at %s", def_theme);
187 Eina_Bool util_set_last_user_mode(Last_User_Mode *mode) {
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);
194 efile = eet_open(_last_user_mode_config_path, EET_FILE_MODE_WRITE);
196 ERR("Cannot open %s for write", _last_user_mode_config_path);
200 ret = eet_data_write(efile, _last_user_mode_descriptor, LAST_USER_MODE_ENTRY, mode, EINA_TRUE);
202 ERR("Cannot write to %s", _last_user_mode_config_path);
208 Last_User_Mode *util_get_last_user_mode() {
210 Last_User_Mode *mode;
212 EINA_SAFETY_ON_NULL_RETURN_VAL(_last_user_mode_config_path, NULL);
214 efile = eet_open(_last_user_mode_config_path, EET_FILE_MODE_READ);
216 ERR("Cannot open %s for read", _last_user_mode_config_path);
220 mode = eet_data_read(efile, _last_user_mode_descriptor, LAST_USER_MODE_ENTRY);
226 static void _last_user_mode_descriptor_init(void) {
227 Eet_Data_Descriptor_Class eddc;
229 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Last_User_Mode);
230 _last_user_mode_descriptor = eet_data_descriptor_stream_new(&eddc);
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);
241 Eina_Bool util_init(const char *theme)
244 const char *config_path;
245 char base_dir[PATH_MAX];
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);
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");
261 snprintf(def_theme, sizeof(def_theme), "%s/themes/default.edj",
262 elm_app_data_dir_get());
264 elm_theme_extension_add(NULL, def_theme);
266 elm_theme_overlay_add(NULL, def_theme);
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);
274 snprintf(tmp, sizeof(tmp), "%s/themes/%s.edj",
275 elm_app_data_dir_get(), theme);
279 elm_theme_overlay_add(NULL, theme);
285 void util_shutdown(void)
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);