fabda9bc25426fac49864236229b11f09ac0b45d
[apps/core/preloaded/email.git] / setting / src / email-view-sync-schedule.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "email-setting.h"
18 #include "email-setting-utils.h"
19
20 #include <unicode/uloc.h>
21 #include <unicode/ucal.h>
22 #include <unicode/udat.h>
23 #include <unicode/udatpg.h>
24 #include <unicode/ustring.h>
25
26 static email_account_t *account_data = NULL;
27 static struct viewdata *g_vd = NULL;
28
29 static UDateTimePatternGenerator *icu_pg = NULL;
30 static UDateFormat *icu_formatter = NULL;
31 static enum appcore_time_format icu_timeformat;
32
33 static int _create(struct viewdata *vd);
34 static int _update(struct viewdata *vd);
35 static int _destroy(struct viewdata *vd);
36 static int _show(struct viewdata *vd);
37 static int _hide(struct viewdata *vd);
38
39 static void _push_naviframe(struct viewdata *vd);
40 static void _create_list(struct viewdata *vd);
41 static void _update_account_info(struct viewdata *vd);
42 static void _get_icu_formatter(void);
43 static void _close_icu_formatter(void);
44 static void _get_date_text_with_formatter(time_t time, char *formatted_str);
45
46 static void _account_edit_cb(void *data, Evas_Object *obj, void *event_info);
47 static void _onoff_cb(void *data, Evas_Object *obj, void *event_info);
48 static void _refresh_cb(void *data, Evas_Object *obj, void *event_info);
49 static void _delete_cb(void *data, Evas_Object *obj, void *event_info);
50 static void _back_cb(void *data, Evas_Object *obj, void *event_info);
51 static Eina_Bool _after_delete_cb(void *data);
52
53 static void _popup_cancel_cb(void *data, Evas_Object *obj, void *event_info);
54 static void _popup_delete_ok_cb(void *data, Evas_Object *obj, void *event_info);
55
56 static char *_gl_account_text_get_cb(void *data, Evas_Object *obj, const char *part);
57 static char *_gl_onoff_text_get_cb(void *data, Evas_Object *obj, const char *part);
58 static char *_gl_sync_text_get_cb(void *data, Evas_Object *obj, const char *part);
59 static char *_gl_ex_sync_text_get_cb(void *data, Evas_Object *obj, const char *part);
60 static Evas_Object *_gl_account_content_get_cb(void *data, Evas_Object *obj, const char *part);
61 static Evas_Object *_gl_onoff_content_get_cb(void *data, Evas_Object *obj, const char *part);
62 static Evas_Object *_gl_ex_sync_content_get_cb(void *data, Evas_Object *obj, const char *part);
63 static void _gl_sel_cb(void *data, Evas_Object *obj, void *event_info);
64 static void _gl_ex_sel_cb(void *data, Evas_Object *obj, void *event_info);
65 static void _gl_ex_sync_sel_cb(void *data, Evas_Object *obj, void *event_info);
66 static void _gl_exp_cb(void *data, Evas_Object *obj, void *event_info);
67 static void _gl_con_cb(void *data, Evas_Object *obj, void *event_info);
68
69 static int sync_schedule[7] = { 0, 30, 60, 180, 360, 720, 1440 };
70
71 #define SYNC_STATUS_FINISHED                0        /* BIN 00000000 */
72 #define SYNC_STATUS_SYNCING                 1        /* BIN 00000001 */
73 #define SYNC_STATUS_HAVE_NEW_MAILS          2        /* BIN 00000010 */
74
75 struct priv_data {
76         struct viewdata *vd;
77
78         Evas_Object *cbar;
79         Evas_Object *onoff;
80
81         Evas_Object *l_button;
82         Evas_Object *r_button;
83         Evas_Object *layout;
84         Evas_Object *conform;
85
86         Evas_Object *sync_radio_grp;
87         Evas_Object *account_icon;
88
89         Evas_Object *genlist;
90         Elm_Genlist_Item_Class itc_account;
91         Elm_Genlist_Item_Class itc_onoff;
92         Elm_Genlist_Item_Class itc_sync;
93         Elm_Genlist_Item_Class itc_ex_sync;
94         Elm_Genlist_Item_Class itc_sep;
95         Elm_Genlist_Item_Class itc_sep2;
96
97         Elm_Object_Item * c_item[4];
98         Elm_Object_Item *gl_sync_item;
99         Elm_Object_Item *gl_onoff_item;
100         Elm_Object_Item *gl_account_item;
101
102         int account_deleted;
103         int syncing;
104 };
105
106 void setting_init_sync_schedule_view(struct viewdata *vd)
107 {
108         debug_log("");
109         vd->type = VIEW_SYNC_SCHEDULE;
110         vd->create = _create;
111         vd->update = _update;
112         vd->destroy = _destroy;
113         vd->show = _show;
114         vd->hide = _hide;
115         vd->grp_nm = "setting_sync_schedule";
116 }
117
118 void setting_schedule_sync_started(struct viewdata *vd, int account_id)
119 {
120         debug_log("");
121         struct priv_data *p = NULL;
122         EmailSettingUGD *ugd = vd->ugd;
123         p = vd->priv;
124
125         if (ugd->account_id != account_id) {
126                 debug_log("not interested");
127                 return;
128         }
129
130         if (!setting_get_acct_full_data(ugd->account_id, &account_data)) {
131                 debug_log("failed to get account data");
132         }
133
134         p->syncing = 1;
135         elm_object_disabled_set(p->r_button, TRUE);
136         elm_genlist_item_update(p->gl_account_item);
137 }
138
139 void setting_schedule_sync_finished(struct viewdata *vd, int account_id)
140 {
141         debug_log("");
142         struct priv_data *p = NULL;
143         EmailSettingUGD *ugd = vd->ugd;
144         p = vd->priv;
145
146         if (ugd->account_id != account_id) {
147                 debug_log("not interested");
148                 return;
149         }
150
151         if (!setting_get_acct_full_data(ugd->account_id, &account_data)) {
152                 debug_log("failed to get account data");
153         }
154
155         p->syncing = 0;
156         elm_object_disabled_set(p->r_button, FALSE);
157         elm_genlist_item_update(p->gl_account_item);
158 }
159
160 static int _create(struct viewdata *vd)
161 {
162         debug_log("");
163
164         if (!vd) {
165                 debug_log("vd is NULL");
166                 return FALSE;
167         }
168
169         struct priv_data *p = NULL;
170         EmailSettingUGD *ugd = vd->ugd;
171         p = vd->priv;
172
173         if (p == NULL) {
174                 p = vd->priv = calloc(1, sizeof(struct priv_data));
175                 p->vd = vd;
176
177                 if (!setting_get_acct_full_data(ugd->account_id, &account_data)) {
178                         debug_log("failed to get account data");
179                 }
180
181                 p->layout = setting_add_inner_layout(vd);
182                 _push_naviframe(vd);
183
184                 elm_win_conformant_set(ugd->win, 1);
185                 p->conform = elm_conformant_add(ugd->win);
186                 elm_object_style_set(p->conform, "internal_layout");
187                 evas_object_show(p->conform);
188
189                 elm_object_part_content_set(p->layout, "elm.swallow.content", p->conform);
190         }
191
192         _get_icu_formatter();
193
194         g_vd = vd;
195
196         _create_list(vd);
197
198         return TRUE;
199 }
200
201 static int _update(struct viewdata *vd)
202 {
203         debug_log("");
204
205         if (!vd) {
206                 debug_log("vd is NULL");
207                 return FALSE;
208         }
209
210         struct priv_data *p = NULL;
211         EmailSettingUGD *ugd = vd->ugd;
212         p = vd->priv;
213
214         if (vd->refresh) {
215                 if (!setting_get_acct_full_data(ugd->account_id, &account_data)) {
216                         debug_log("failed to get account data");
217                 }
218
219                 if (account_data->sync_status & SYNC_STATUS_SYNCING) {
220                         p->syncing = 1;
221                         elm_object_disabled_set(p->r_button, TRUE);
222                 } else {
223                         p->syncing = 0;
224                         elm_object_disabled_set(p->r_button, FALSE);
225                 }
226                 elm_genlist_item_update(p->gl_account_item);
227         }
228
229         return TRUE;
230 }
231
232 static int _destroy(struct viewdata *vd)
233 {
234         debug_log("");
235
236         if (!vd) {
237                 debug_log("vd is NULL");
238                 return FALSE;
239         }
240
241         struct priv_data *p = vd->priv;
242
243         if (p == NULL) {
244                 debug_log("priv is NULL");
245                 return FALSE;
246         }
247
248         evas_object_del(p->l_button);
249         evas_object_del(p->onoff);
250         evas_object_del(p->genlist);
251         evas_object_del(p->sync_radio_grp);
252
253         if (account_data) {
254                 email_engine_free_account_list(&account_data, 1);
255                 account_data = NULL;
256         }
257
258         _close_icu_formatter();
259
260         return TRUE;
261 }
262
263 static int _show(struct viewdata *vd)
264 {
265         debug_log("");
266
267         if (!vd) {
268                 debug_log("vd is NULL");
269                 return FALSE;
270         }
271
272         EmailSettingUGD *ugd = vd->ugd;
273
274         if (account_data) {
275                 email_engine_free_account_list(&account_data, 1);
276                 account_data = NULL;
277         }
278
279         if (!setting_get_acct_full_data(ugd->account_id, &account_data)) {
280                 debug_log("setting_get_acct_full_data failed");
281         }
282
283         return TRUE;
284 }
285
286 static int _hide(struct viewdata *vd)
287 {
288         debug_log("");
289
290         if (!vd) {
291                 debug_log("vd is NULL");
292                 return FALSE;
293         }
294
295         evas_object_hide(vd->ly);
296
297         return TRUE;
298 }
299
300 static void _push_naviframe(struct viewdata *vd)
301 {
302         debug_log("");
303         struct priv_data *p = vd->priv;
304
305         p->cbar = elm_toolbar_add(vd->ugd->navi_bar);
306         elm_toolbar_shrink_mode_set(p->cbar, ELM_TOOLBAR_SHRINK_EXPAND);
307         elm_object_style_set(p->cbar, "naviframe");
308         evas_object_show(p->cbar);
309
310         p->c_item[0] = elm_toolbar_item_append(p->cbar, NULL,
311                         dgettext("sys_string", "IDS_COM_SK_DELETE"), _delete_cb, vd);
312         elm_object_item_disabled_set(elm_toolbar_item_append(p->cbar, NULL, "", NULL, NULL), EINA_TRUE);
313         elm_object_item_disabled_set(elm_toolbar_item_append(p->cbar, NULL, "", NULL, NULL), EINA_TRUE);
314
315         p->r_button = elm_button_add(vd->ugd->navi_bar);
316         elm_object_style_set(p->r_button, "naviframe/title/default");
317         /*elm_object_text_set(p->r_button, _("IDS_EMAIL_OPT_REFRESH"));*/
318         Evas_Object *ic = elm_icon_add(p->r_button);
319         elm_icon_file_set(ic, TITLE_ICON_REFRESH, NULL);
320         elm_icon_resizable_set(ic, EINA_TRUE, EINA_TRUE);
321         evas_object_image_smooth_scale_set(ic, 0);
322         elm_object_content_set(p->r_button, ic);
323         evas_object_smart_callback_add(p->r_button, "clicked", _refresh_cb, vd);
324         evas_object_show(p->r_button);
325
326         if (account_data->sync_status & SYNC_STATUS_SYNCING) {
327                 p->syncing = 1;
328                 elm_object_disabled_set(p->r_button, TRUE);
329         }
330
331         p->l_button = elm_button_add(vd->ugd->navi_bar);
332         evas_object_smart_callback_add(p->l_button, "clicked", _back_cb, vd);
333         elm_object_style_set(p->l_button, "naviframe/back_btn/default");
334
335         Elm_Object_Item *navi_it = NULL;
336         navi_it = elm_naviframe_item_push(vd->ugd->navi_bar, account_data->account_name, p->l_button, NULL, p->layout, NULL);
337         elm_object_item_part_content_set(navi_it, "title_right_btn", p->r_button);
338         elm_object_item_part_content_set(navi_it, "controlbar", p->cbar);
339         evas_object_show(vd->ugd->navi_bar);
340 }
341
342 static void _create_list(struct viewdata *vd)
343 {
344         debug_log("");
345
346         if (!vd) {
347                 debug_log("vd is NULL");
348                 return;
349         }
350
351         struct priv_data *p = vd->priv;
352         EmailSettingUGD *ugd = vd->ugd;
353         Elm_Object_Item *item = NULL;
354         Elm_Object_Item *git = NULL;
355         int i = 0;
356
357         p->sync_radio_grp = elm_radio_add(ugd->navi_bar);
358         elm_radio_value_set(p->sync_radio_grp, 0);
359         evas_object_hide(p->sync_radio_grp);
360
361         p->genlist = elm_genlist_add(ugd->navi_bar);
362         elm_object_style_set(p->genlist, "dialogue");
363
364         p->itc_account.item_style = "dialogue/2text.3icon";
365         p->itc_account.func.text_get = _gl_account_text_get_cb;
366         p->itc_account.func.content_get = _gl_account_content_get_cb;
367         p->itc_account.func.state_get = NULL;
368         p->itc_account.func.del = NULL;
369
370         p->itc_onoff.item_style = "dialogue/2text.1icon.6";
371         p->itc_onoff.func.text_get = _gl_onoff_text_get_cb;
372         p->itc_onoff.func.content_get = _gl_onoff_content_get_cb;
373         p->itc_onoff.func.state_get = NULL;
374         p->itc_onoff.func.del = NULL;
375
376         p->itc_sync.item_style = "dialogue/2text.3/expandable";
377         p->itc_sync.func.text_get = _gl_sync_text_get_cb;
378         p->itc_sync.func.content_get = NULL;
379         p->itc_sync.func.state_get = NULL;
380         p->itc_sync.func.del = NULL;
381
382         p->itc_ex_sync.item_style = "dialogue/1text.1icon/expandable2";
383         p->itc_ex_sync.func.text_get = _gl_ex_sync_text_get_cb;
384         p->itc_ex_sync.func.content_get = _gl_ex_sync_content_get_cb;
385         p->itc_ex_sync.func.state_get = NULL;
386         p->itc_ex_sync.func.del = NULL;
387
388         p->itc_sep.item_style = "dialogue/separator";
389         p->itc_sep.func.text_get = NULL;
390         p->itc_sep.func.content_get = NULL;
391         p->itc_sep.func.state_get = NULL;
392         p->itc_sep.func.del = NULL;
393
394         p->itc_sep2.item_style = "dialogue/separator/end";
395         p->itc_sep2.func.text_get = NULL;
396         p->itc_sep2.func.content_get = NULL;
397         p->itc_sep2.func.state_get = NULL;
398         p->itc_sep2.func.del = NULL;
399
400         /*seperator*/
401         git = elm_genlist_item_append(p->genlist, &(p->itc_sep), NULL, NULL,
402                         ELM_GENLIST_ITEM_GROUP, NULL, NULL);
403         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
404
405         /*account*/
406         item = elm_genlist_item_append(p->genlist, &(p->itc_account), NULL, git,
407                         ELM_GENLIST_ITEM_NONE, _account_edit_cb, (void *)vd);
408         p->gl_account_item = item;
409
410         /*sync schedule*/
411         item = elm_genlist_item_append(p->genlist, &(p->itc_sync), NULL,
412                         git, ELM_GENLIST_ITEM_TREE, _gl_ex_sel_cb, (void *)vd);
413         p->gl_sync_item = item;
414
415         /*seperator*/
416         git = elm_genlist_item_append(p->genlist, &(p->itc_sep), NULL, NULL,
417                         ELM_GENLIST_ITEM_GROUP, NULL, NULL);
418         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
419
420         /*sync on/off*/
421         item = elm_genlist_item_append(p->genlist, &(p->itc_onoff), (void *)i,
422                         git, ELM_GENLIST_ITEM_NONE, _gl_sel_cb, (void *)i);
423         p->gl_onoff_item = item;
424
425         /*seperator*/
426         git = elm_genlist_item_append(p->genlist, &(p->itc_sep2), NULL, NULL,
427                         ELM_GENLIST_ITEM_GROUP, NULL, NULL);
428         elm_genlist_item_select_mode_set(git, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
429
430         evas_object_smart_callback_add(p->genlist, "expanded", _gl_exp_cb, (void *)vd);
431         evas_object_smart_callback_add(p->genlist, "contracted", _gl_con_cb, (void *)vd);
432
433         elm_object_content_set(p->conform, p->genlist);
434 }
435
436 static void _update_account_info(struct viewdata *vd)
437 {
438         debug_log("");
439         struct priv_data *p = NULL;
440
441         if (!vd) {
442                 debug_log("vd is NULL");
443                 return;
444         }
445
446         if (!vd->priv) {
447                 debug_log("priv is NULL");
448                 return;
449         }
450
451         p = vd->priv;
452
453         if (account_data == NULL) {
454                 debug_log("account_data is NULL");
455                 return;
456         }
457
458         if (email_engine_update_account(account_data->account_id, account_data) == TRUE)
459                 debug_log("Account updated successfully");
460 }
461
462 static void _get_icu_formatter(void)
463 {
464         debug_log("");
465         UErrorCode status = U_ZERO_ERROR;
466         char *icu_locale = NULL;
467
468         uloc_setDefault(getenv("LC_TIME"), &status);
469         if (U_FAILURE(status)) {
470                 debug_critical("uloc_setDefault() failed: %s\n", u_errorName(status));
471                 return;
472         }
473
474         icu_locale = (char *)uloc_getDefault();
475         debug_log("uloc_getDefault: %s", icu_locale);
476         appcore_get_timeformat(&icu_timeformat);
477
478         status = U_ZERO_ERROR;
479         icu_pg = udatpg_open(icu_locale, &status);
480         if (!icu_pg) {
481                 debug_log("udatpg_open() failed: %s", u_errorName(status));
482                 return;
483         }
484
485         char *skeleton = NULL;
486         if (icu_timeformat == APPCORE_TIME_FORMAT_12) {
487                 skeleton = "EEEMMMddhmma";
488         } else {
489                 skeleton = "EEEMMMddHmm";
490         }
491
492         UChar bestPattern[64] = { 0, };
493         UChar customSkeleton[64] = { 0, };
494         int32_t bestPatternCapacity;
495         int32_t bestPatternLength;
496         int skeletonLength = strlen(skeleton);
497
498         u_uastrncpy(customSkeleton, skeleton, skeletonLength);
499
500         status = U_ZERO_ERROR;
501         bestPatternCapacity = (int32_t) (sizeof(bestPattern) / sizeof(bestPattern[0]));
502         bestPatternLength = udatpg_getBestPattern(icu_pg, customSkeleton, u_strlen(customSkeleton), bestPattern, bestPatternCapacity, &status);
503
504         status = U_ZERO_ERROR;
505         icu_formatter = udat_open(UDAT_IGNORE, UDAT_IGNORE, icu_locale, NULL, -1, bestPattern, -1, &status);
506 }
507
508 static void _close_icu_formatter(void)
509 {
510         debug_log("");
511
512         if (icu_pg) {
513                 udatpg_close(icu_pg);
514                 icu_pg = NULL;
515         }
516
517         if (icu_formatter) {
518                 udat_close(icu_formatter);
519                 icu_formatter = NULL;
520         }
521 }
522
523 static void _get_date_text_with_formatter(time_t time, char *formatted_str)
524 {
525         debug_log("");
526         UErrorCode status = U_ZERO_ERROR;
527         UDate date = 0;
528         UChar formatted[64] = { 0, };
529         int32_t formattedCapacity;
530         int32_t formattedLength;
531
532         formattedCapacity = (int32_t) (sizeof(formatted) / sizeof(formatted[0]));
533         time_t msg_time = time;
534         date = (UDate)msg_time * 1000;
535
536         formattedLength = udat_format(icu_formatter, date, formatted, formattedCapacity, NULL, &status);
537         u_austrncpy(formatted_str, formatted, 127);
538         debug_log("time_t: %d", time);
539         debug_log("formatted time: %s", formatted_str);
540 }
541
542 static void _account_edit_cb(void *data, Evas_Object *obj, void *event_info)
543 {
544         debug_log("");
545         struct viewdata *vd = (struct viewdata *)data;
546         EmailSettingUGD *ugd = vd->ugd;
547
548         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
549         elm_genlist_item_selected_set(item, EINA_FALSE);
550
551         Viewtype top = ugd->view_st[ugd->view_top];
552         if (top != VIEW_SYNC_SCHEDULE) {
553                 debug_log("**double clicked**");
554                 return;
555         }
556
557         debug_log("Selected account id:%d", account_data->account_id);
558         vd->ugd->account_id = account_data->account_id;
559         vd->ugd->myaccount_id = account_data->account_svc_id;
560
561         setting_change_view(VIEW_ACCOUNT_EDIT, ugd);
562 }
563
564 static void _onoff_cb(void *data, Evas_Object *obj, void *event_info)
565 {
566         debug_log("");
567
568         struct viewdata *vd = (struct viewdata *)data;
569         struct priv_data *p = vd->priv;
570
571         Eina_Bool state = elm_check_state_get(obj);
572
573         if (state) {
574                 account_data->check_interval = abs(account_data->check_interval);
575                 account_data->sync_disabled = 0;
576
577                 if (p->gl_sync_item) {
578                         elm_genlist_item_update(p->gl_sync_item);
579                         elm_object_item_disabled_set(p->gl_sync_item, EINA_FALSE);
580                 }
581         } else {
582                 account_data->check_interval = -(abs(account_data->check_interval));
583                 account_data->sync_disabled = 1;
584
585                 if (p->gl_sync_item) {
586                         if (elm_genlist_item_expanded_get(p->gl_sync_item)) {
587                                 elm_genlist_item_expanded_set(p->gl_sync_item, 0);
588                         }
589                         elm_genlist_item_update(p->gl_sync_item);
590                         elm_object_item_disabled_set(p->gl_sync_item, EINA_TRUE);
591                 }
592         }
593
594         _update_account_info(vd);
595 }
596
597 static void _refresh_cb(void *data, Evas_Object *obj, void *event_info)
598 {
599         debug_log("");
600         struct viewdata *vd = (struct viewdata *)data;
601         if (vd == NULL) {
602                 debug_log("vd is NULL");
603                 return;
604         }
605
606         struct priv_data *p = vd->priv;
607         unsigned handle = 0;
608         int account_id = account_data->account_id;
609
610         /* Inbox sync */
611         int ret = 0;
612         gboolean res = FALSE;
613         email_mailbox_t *mailbox = NULL;
614         ret = email_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox);
615         if (ret != EMAIL_ERROR_NONE || mailbox == NULL || mailbox->mailbox_name == NULL) {
616                 debug_log("email_get_mailbox_by_mailbox_type failed");
617                 return;
618         }
619
620         res = email_engine_sync_folder(account_id, mailbox->mailbox_id, &handle);
621         debug_log("handle: %d", handle);
622         email_free_mailbox(&mailbox, 1);
623
624         p->syncing = 1;
625         elm_object_disabled_set(p->r_button, TRUE);
626         elm_genlist_item_update(p->gl_sync_item);
627         elm_genlist_item_update(p->gl_onoff_item);
628 }
629
630 static void _delete_cb(void *data, Evas_Object *obj, void *event_info)
631 {
632         debug_log("");
633         struct viewdata *vd = (struct viewdata *)data;
634
635         if (vd == NULL) {
636                 debug_log("vd is NULL");
637                 return;
638         }
639
640         struct priv_data *p = vd->priv;
641         EmailSettingUGD *ugd = vd->ugd;
642
643         if (ugd->popup_one) {
644                 evas_object_del(ugd->popup_one);
645                 ugd->popup_one = NULL;
646         }
647
648         if (ugd->popup_validate) {
649                 evas_object_del(ugd->popup_validate);
650                 ugd->popup_validate = NULL;
651         }
652
653         vd->ugd->account_id = account_data->account_id;
654
655         elm_object_disabled_set(p->l_button, EINA_TRUE);
656         elm_object_item_disabled_set(p->c_item[0], EINA_TRUE);
657
658         ugd->popup_one = setting_get_notify(vd, NULL, N_("All data related to this account will be deleted. Continue?"), 2,
659                                                                                         dgettext("sys_string", "IDS_COM_SK_YES"),
660                                                                                         _popup_delete_ok_cb,
661                                                                                         dgettext("sys_string", "IDS_COM_SK_NO"),
662                                                                                         _popup_cancel_cb);
663 }
664
665 static void _back_cb(void *data, Evas_Object *obj, void *event_info)
666 {
667         debug_log("");
668
669         struct viewdata *vd = data;
670         /*struct priv_data *p = vd->priv;*/
671
672         setting_back_to_prev_view(vd);
673 }
674
675 static Eina_Bool _after_delete_cb(void *data)
676 {
677         debug_log("");
678
679         struct viewdata *vd = (struct viewdata *)data;
680
681         if (vd == NULL) {
682                 debug_log("vd is NULL");
683                 return FALSE;
684         }
685
686         struct priv_data *p = vd->priv;
687         EmailSettingUGD *ugd = vd->ugd;
688
689         if (ugd->del_timer) {
690                 ecore_timer_del(ugd->del_timer);
691                 ugd->del_timer = NULL;
692         }
693
694         status_message_post(dgettext("sys_string", "IDS_COM_POP_DELETED"));
695
696         if (vd->ugd->start_view_type != NULL && g_strcmp0(vd->ugd->start_view_type, "account-edit") == 0) {
697                 ug_destroy_me(vd->ugd->ug);
698         } else {
699                 if (p->account_deleted) {
700                         setting_update_acct_list(vd);
701                         setting_back_to_prev_nth_view(vd, 2, vd->ugd->navi_main_it);
702                 } else {
703                         setting_back_to_prev_view(vd);
704                 }
705
706                 setting_get_small_notify(vd, dgettext("sys_string", "IDS_COM_POP_DELETED"));
707         }
708
709         return TRUE;
710 }
711
712 static void _popup_cancel_cb(void *data, Evas_Object *obj, void *event_info)
713 {
714         debug_log("");
715
716         if (!data) {
717                 debug_log("data is NULL");
718                 return;
719         }
720
721         struct viewdata *vd = (struct viewdata *)data;
722         struct priv_data *p = vd->priv;
723         EmailSettingUGD *ugd = vd->ugd;
724
725         if (ugd->popup_one) {
726                 evas_object_del(ugd->popup_one);
727                 ugd->popup_one = NULL;
728         }
729         if (ugd->popup_validate) {
730                 evas_object_del(ugd->popup_validate);
731                 ugd->popup_validate = NULL;
732         }
733
734         elm_object_disabled_set(p->l_button, EINA_FALSE);
735         elm_object_item_disabled_set(p->c_item[0], EINA_FALSE);
736 }
737
738 static void _popup_delete_ok_cb(void *data, Evas_Object *obj, void *event_info)
739 {
740         debug_log("");
741
742         if (!data) {
743                 debug_log("data is NULL");
744                 return;
745         }
746
747         struct viewdata *vd = (struct viewdata *)data;
748         struct priv_data *p = vd->priv;
749         EmailSettingUGD *ugd = vd->ugd;
750
751         if (ugd->popup_one) {
752                 evas_object_del(ugd->popup_one);
753                 ugd->popup_one = NULL;
754         }
755         if (ugd->popup_validate) {
756                 evas_object_del(ugd->popup_validate);
757                 ugd->popup_validate = NULL;
758         }
759
760         int account_id = vd->ugd->account_id;
761         int ret = email_engine_delete_account(account_id);
762
763         if (ret) {
764                 p->account_deleted = TRUE;
765                 ugd->del_timer = ecore_timer_add(0.5, _after_delete_cb, vd);
766                 debug_log("delete success");
767         } else {
768                 ugd->popup_one = setting_get_notify(vd, dgettext("sys_string", "IDS_COM_POP_WARNING"),
769                                                                                                 dgettext("sys_string", "IDS_COM_POP_UNABLE_TO_DELETE"), 1,
770                                                                                                 dgettext("sys_string", "IDS_COM_SK_OK"),
771                                                                                                 _popup_cancel_cb, NULL, NULL);
772                 debug_log("delete failed");
773         }
774 }
775
776 static char *_gl_account_text_get_cb(void *data, Evas_Object *obj, const char *part)
777 {
778         debug_log("");
779
780         if (!account_data) {
781                 debug_log("account_data is NULL");
782                 return NULL;
783         }
784
785         if (!g_strcmp0(part, "elm.text.1")) {
786                 return g_strdup(account_data->account_name);
787         }
788
789         if (!g_strcmp0(part, "elm.text.2")) {
790                 return g_strdup(account_data->user_email_address);
791         }
792
793         return NULL;
794 }
795
796 static char *_gl_onoff_text_get_cb(void *data, Evas_Object *obj, const char *part)
797 {
798         debug_log("");
799
800         if (!g_strcmp0(part, "elm.text.1")) {
801                 char buf[MAX_STR_LEN] = { 0, };
802
803                 snprintf(buf, sizeof(buf), "%s", dgettext("sys_string", "IDS_COM_BODY_EMAIL_SYNC"));
804                 return g_strdup(buf);
805         } else if (!g_strcmp0(part, "elm.text.2")) {
806                 char buf[MAX_STR_LEN] = { 0, };
807                 email_mailbox_t *mailbox = NULL;
808                 email_get_mailbox_by_mailbox_type(account_data->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox);
809                 if (mailbox) {
810                         time_t req_time = time(NULL);//mailbox->last_sync_time;
811                         _get_date_text_with_formatter(req_time, buf);
812                         email_free_mailbox(&mailbox, 1);
813                 }
814
815                 return g_strdup(buf);
816         }
817
818         return NULL;
819 }
820
821 static char *_gl_sync_text_get_cb(void *data, Evas_Object *obj, const char *part)
822 {
823         debug_log("");
824
825         if (!account_data) {
826                 debug_log("account_data is NULL");
827                 return NULL;
828         }
829
830         if (!g_strcmp0(part, "elm.text.1")) {
831                 char buf[MAX_STR_LEN] = { 0, };
832                 snprintf(buf, sizeof(buf), "%s", _("IDS_EMAIL_BODY_SYNC_SCHEDULE"));
833                 return g_strdup(buf);
834         }
835
836         if (!g_strcmp0(part, "elm.text.2")) {
837                 char buf[MAX_STR_LEN] = { 0, };
838
839                 /*int period = abs(account_data->check_interval);*/
840                 int period = account_data->check_interval;
841
842                 if (period <= sync_schedule[0]) {
843                         snprintf(buf, sizeof(buf), "%s", dgettext("sys_string", "IDS_COM_BODY_MANUAL"));
844                 } else if (period == sync_schedule[1]) {
845                         snprintf(buf, sizeof(buf), "%s", _("IDS_EMAIL_BODY_EVERY_30_MINUTES"));
846                 } else if (period == sync_schedule[2]) {
847                         snprintf(buf, sizeof(buf), "%s", N_("Every 1 hour"));
848                 } else if (period == sync_schedule[3]) {
849                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 3);
850                 } else if (period == sync_schedule[4]) {
851                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 6);
852                 } else if (period == sync_schedule[5]) {
853                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 12);
854                 } else if (period == sync_schedule[6]) {
855                         snprintf(buf, sizeof(buf), "%s", _("IDS_EMAIL_BODY_ONCE_A_DAY"));
856                 }
857                 return g_strdup(buf);
858         }
859
860         return NULL;
861 }
862
863 static char *_gl_ex_sync_text_get_cb(void *data, Evas_Object *obj, const char *part)
864 {
865         debug_log("");
866         int index = (int)data;
867
868         if (!g_strcmp0(part, "elm.text")) {
869                 char buf[MAX_STR_LEN] = { 0, };
870
871                 if (index == 0) {
872                         snprintf(buf, sizeof(buf), "%s", dgettext("sys_string", "IDS_COM_BODY_MANUAL"));
873                 } else if (index == 1) {
874                         snprintf(buf, sizeof(buf), "%s", _("IDS_EMAIL_BODY_EVERY_30_MINUTES"));
875                 } else if (index == 2) {
876                         snprintf(buf, sizeof(buf), "%s", N_("Every 1 hour"));
877                 } else if (index == 3) {
878                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 3);
879                 } else if (index == 4) {
880                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 6);
881                 } else if (index == 5) {
882                         snprintf(buf, sizeof(buf), _("IDS_EMAIL_BODY_EVERY_PD_HOURS"), 12);
883                 } else if (index == 6) {
884                         snprintf(buf, sizeof(buf), "%s", _("IDS_EMAIL_BODY_ONCE_A_DAY"));
885                 }
886
887                 return g_strdup(buf);
888         }
889
890         return NULL;
891 }
892
893 static Evas_Object *_gl_account_content_get_cb(void *data, Evas_Object *obj, const char *part)
894 {
895         debug_log("");
896
897         if (!account_data) {
898                 debug_log("account_data is NULL");
899                 return NULL;
900         }
901
902         struct viewdata *vd = g_vd;
903         EmailSettingUGD *ugd = vd->ugd;
904         struct priv_data *p = vd->priv;
905         Evas_Object *color_bar = NULL;
906
907         if (!g_strcmp0(part, "elm.icon.1")) {
908                 Evas_Object *icon = elm_icon_add(ugd->navi_bar);
909                 account_user_data_t *user_data = (account_user_data_t *)account_data->user_data;
910
911                 debug_log("is_preset_account:%d", user_data->is_preset_account);
912
913                 switch (user_data->is_preset_account) {
914                 case GMAIL:
915                         elm_icon_file_set(icon, ACCOUNT_ICON_GMAIL, NULL);
916                         break;
917                 case HOTMAIL:
918                         elm_icon_file_set(icon, ACCOUNT_ICON_HOTMAIL, NULL);
919                         break;
920                 case YAHOOMAIL:
921                         elm_icon_file_set(icon, ACCOUNT_ICON_YAHOO, NULL);
922                         break;
923                 case AOL:
924                         elm_icon_file_set(icon, ACCOUNT_ICON_AOL, NULL);
925                         break;
926                 case OTHERS:
927                 default:
928                         elm_icon_file_set(icon, ACCOUNT_ICON_OTHERS, NULL);
929                         break;
930                 }
931
932                 evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
933                 p->account_icon = icon;
934                 return icon;
935         }
936
937         if (!g_strcmp0(part, "elm.icon.2")) {
938                 if (p->syncing) {
939                         Evas_Object *pb = elm_progressbar_add(ugd->navi_bar);
940                         elm_object_style_set(pb, "list_process_small");
941                         elm_progressbar_horizontal_set(pb, EINA_TRUE);
942                         elm_progressbar_pulse(pb, EINA_TRUE);
943                         return pb;
944                 }
945         }
946
947         if (!g_strcmp0(part, "elm.swallow.colorbar")) {
948                 account_user_data_t *user_data = (account_user_data_t *)account_data->user_data;
949                 unsigned int val = user_data->index_color;
950                 int r = (val & 0xff000000) >> 24;
951                 int g = (val & 0x00ff0000) >> 16;
952                 int b = (val & 0x0000ff00) >> 8;
953                 int a = val & 0x000000ff;
954
955                 debug_log("R[%d] G[%d] B[%d] A[%d]", r, g, b, a);
956
957                 color_bar = evas_object_rectangle_add(evas_object_evas_get(obj));
958                 evas_object_size_hint_fill_set(color_bar, EVAS_HINT_FILL, EVAS_HINT_FILL);
959                 evas_object_color_set(color_bar, r, g, b, a);
960
961                 return color_bar;
962         }
963
964         return NULL;
965 }
966
967 static Evas_Object *_gl_onoff_content_get_cb(void *data, Evas_Object *obj, const char *part)
968 {
969         debug_log("");
970
971         if (!account_data) {
972                 debug_log("account_data is NULL");
973                 return NULL;
974         }
975
976         struct viewdata *vd = g_vd;
977         EmailSettingUGD *ugd = vd->ugd;
978         struct priv_data *p = vd->priv;
979
980         if (!g_strcmp0(part, "elm.icon")) {
981                 Evas_Object *check = elm_check_add(ugd->navi_bar);
982                 elm_object_style_set(check, "on&off");
983                 evas_object_smart_callback_add(check, "changed", _onoff_cb, vd);
984                 evas_object_propagate_events_set(check, EINA_FALSE);
985
986                 if (account_data->sync_disabled) {
987                         elm_check_state_set(check, EINA_FALSE);
988                         elm_object_item_disabled_set(p->gl_sync_item, EINA_TRUE);
989                 } else {
990                         elm_check_state_set(check, EINA_TRUE);
991                         elm_object_item_disabled_set(p->gl_sync_item, EINA_FALSE);
992                 }
993
994                 p->onoff = check;
995
996                 return check;
997         }
998
999         return NULL;
1000 }
1001
1002 static Evas_Object *_gl_ex_sync_content_get_cb(void *data, Evas_Object *obj, const char *part)
1003 {
1004         debug_log("");
1005
1006         if (!account_data) {
1007                 debug_log("account_data is NULL");
1008                 return NULL;
1009         }
1010
1011         int index = (int)data;
1012         struct viewdata *vd = g_vd;
1013         EmailSettingUGD *ugd = vd->ugd;
1014         struct priv_data *p = vd->priv;
1015
1016         if (!g_strcmp0(part, "elm.icon")) {
1017                 Evas_Object *radio = elm_radio_add(ugd->navi_bar);
1018                 elm_radio_group_add(radio, p->sync_radio_grp);
1019                 elm_radio_state_value_set(radio, index);
1020
1021                 if (sync_schedule[index] == abs(account_data->check_interval)) {
1022                         elm_radio_value_set(p->sync_radio_grp, index);
1023                 }
1024                 return radio;
1025         }
1026
1027         return NULL;
1028 }
1029
1030 static void _gl_sel_cb(void *data, Evas_Object *obj, void *event_info)
1031 {
1032         debug_log("");
1033
1034         if (!account_data) {
1035                 debug_log("account_data is NULL");
1036                 return;
1037         }
1038
1039         struct viewdata *vd = g_vd;
1040         struct priv_data *p = vd->priv;
1041
1042         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1043         elm_genlist_item_selected_set(item, EINA_FALSE);
1044
1045         Eina_Bool state = elm_check_state_get(p->onoff);
1046
1047         if (state) {
1048                 account_data->check_interval = -(abs(account_data->check_interval));
1049                 account_data->sync_disabled = 1;
1050                 elm_check_state_set(p->onoff, EINA_FALSE);
1051
1052                 if (p->gl_sync_item) {
1053                         if (elm_genlist_item_expanded_get(p->gl_sync_item)) {
1054                                 elm_genlist_item_expanded_set(p->gl_sync_item, 0);
1055                         }
1056                         elm_genlist_item_update(p->gl_sync_item);
1057                         elm_object_item_disabled_set(p->gl_sync_item, EINA_TRUE);
1058                 }
1059         } else {
1060                 account_data->check_interval = abs(account_data->check_interval);
1061                 account_data->sync_disabled = 0;
1062                 elm_check_state_set(p->onoff, EINA_TRUE);
1063
1064                 if (p->gl_sync_item) {
1065                         elm_genlist_item_update(p->gl_sync_item);
1066                         elm_object_item_disabled_set(p->gl_sync_item, EINA_FALSE);
1067                 }
1068         }
1069
1070         _update_account_info(vd);
1071 }
1072
1073 static void _gl_ex_sel_cb(void *data, Evas_Object *obj, void *event_info)
1074 {
1075         debug_log("");
1076
1077         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1078         elm_genlist_item_selected_set(item, EINA_FALSE);
1079
1080         if (!elm_genlist_item_expanded_get(item)) {
1081                 elm_genlist_item_expanded_set(item, 1);
1082         } else {
1083                 elm_genlist_item_expanded_set(item, 0);
1084         }
1085 }
1086
1087 static void _gl_ex_sync_sel_cb(void *data, Evas_Object *obj, void *event_info)
1088 {
1089         debug_log("");
1090
1091         if (!account_data) {
1092                 debug_log("account_data is NULL");
1093                 return;
1094         }
1095
1096         int index = (int)data;
1097         struct viewdata *vd = g_vd;
1098         struct priv_data *p = vd->priv;
1099         int sel_radio = 0;
1100
1101         Elm_Object_Item *item = (Elm_Object_Item *)event_info;
1102         elm_genlist_item_selected_set(item, EINA_FALSE);
1103
1104         sel_radio = elm_radio_value_get(p->sync_radio_grp);
1105
1106         if (sel_radio == index)
1107                 return;
1108
1109         if (index >= 0 && index < 7)
1110                 account_data->check_interval = sync_schedule[index];
1111
1112         elm_radio_value_set(p->sync_radio_grp, index);
1113
1114         _update_account_info(vd);
1115
1116         elm_genlist_item_update(p->gl_sync_item);
1117 }
1118
1119 static void _gl_exp_cb(void *data, Evas_Object *obj, void *event_info)
1120 {
1121         debug_log("");
1122         Elm_Object_Item *it = event_info;
1123
1124         struct viewdata *vd = (struct viewdata *)data;
1125         struct priv_data *p = vd->priv;
1126
1127         int i = 0;
1128
1129         for (i = 0; i < 7; i++) {
1130                 elm_genlist_item_append(p->genlist, &(p->itc_ex_sync), (void *)i,
1131                                 it, ELM_GENLIST_ITEM_NONE, _gl_ex_sync_sel_cb, (void *)i);
1132         }
1133 }
1134
1135 static void _gl_con_cb(void *data, Evas_Object *obj, void *event_info)
1136 {
1137         debug_log("");
1138         Elm_Object_Item *item = event_info;
1139         elm_genlist_item_subitems_clear(item);
1140 }
1141
1142 /* EOF */