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