fix JIRA issue TREL-17, TDIS-4421 that setting ug is crashed
[apps/core/preloaded/email.git] / setting / src / email-setting.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 #ifndef UG_MODULE_API
18 #define UG_MODULE_API __attribute__ ((visibility("default")))
19 #endif
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <getopt.h>
24 #include <utilX.h>
25
26 #include "email-debug.h"
27 #include "email-setting.h"
28 #include "email-setting-utils.h"
29 #include "email-view-setting.h"
30 #include "email-view-sp-select.h"
31 #include "email-view-account-edit.h"
32 #include "email-view-account-setup.h"
33 #include "email-view-manual-setup.h"
34 #include "email-view-mailbox-mapping.h"
35 #include "email-view-account-options.h"
36 #include "email-view-initial.h"
37 #include "email-view-sync-schedule.h"
38 #include "email-view-sync-setup.h"
39
40 static void *on_create(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv);
41 static void on_event(ui_gadget_h ug, enum ug_event event, service_h service, void *priv);
42 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, service_h service, void *priv);
43 static void on_message(ui_gadget_h ug, service_h msg, service_h service, void *priv);
44 static void on_destroy(ui_gadget_h ug, service_h service, void *priv);
45 static void on_resume(ui_gadget_h ug, service_h service, void *priv);
46 static void on_pause(ui_gadget_h ug, service_h service, void *priv);
47 static void on_start(ui_gadget_h ug, service_h service, void *priv);
48
49 static void _init_data(struct ug_data *ugd);
50 static void _couple_data(struct ug_data *ugd, Viewtype t, struct viewdata *vd);
51 static void _dbus_receiver_setup(struct ug_data *ugd);
52 static void _remove_dbus_receiver(struct ug_data *ugd);
53 static int _account_init(struct ug_data *ugd);
54 static int _account_finalize(EmailSettingUGD *ugd);
55 static int _parse_option(service_h service, struct ug_data *ugd);
56 static void _dispatch_view(struct ug_data *ugd);
57 static void _create_navigationbar(struct ug_data *ugd);
58 static Evas_Object *_create_layout(Evas_Object *parent, struct ug_data *ugd);
59 static void _win_profile_changed_cb(void *data, Evas_Object *obj, void *event);
60
61 static void _layout_block_cb(ui_gadget_h ug, enum ug_mode mode, void *priv);
62 static void _result_block_cb(ui_gadget_h ug, service_h service, void *priv);
63 static void _destroy_block_cb(ui_gadget_h ug, void *priv);
64
65 static void _popup_ok_cb(void *data, Evas_Object *obj, void *event_info);
66
67 static void _create_max_account_popup_create(EmailSettingUGD *ugd);
68 static void _max_account_popup_ok_cb(void *data, Evas_Object *obj, void *event_info);
69 static void _language_changed_cb(EmailSettingUGD *ugd);
70
71
72 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
73 {
74         debug_log("");
75         struct ug_data *ugd;
76         if (!ops)
77                 return -1;
78
79         ugd = calloc(1, sizeof(struct ug_data));
80         if (!ugd)
81                 return -1;
82
83         ops->create = on_create;
84         ops->start = on_start;
85         ops->pause = on_pause;
86         ops->resume = on_resume;
87         ops->destroy = on_destroy;
88         ops->message = on_message;
89         ops->event = on_event;
90         ops->key_event = on_key_event;
91         ops->priv = ugd;
92         ops->opt = UG_OPT_INDICATOR_ENABLE;
93
94         return 0;
95 }
96
97 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
98 {
99         debug_log("");
100         struct ug_data *ugd;
101
102         if (!ops)
103                 return;
104
105         ugd = ops->priv;
106         if (ugd)
107                 free(ugd);
108 }
109
110 UG_MODULE_API int setting_plugin_reset(service_h service, void *priv)
111 {
112         return 0;
113 }
114
115 static void *on_create(ui_gadget_h ug, enum ug_mode mode, service_h service, void *priv)
116 {
117         debug_log("");
118         Evas_Object *parent;
119         struct ug_data *ugd;
120
121         if (!ug || !priv)
122                 return NULL;
123
124         ugd = priv;
125         ugd->ug = ug;
126
127         parent = ug_get_window();
128         if (!parent)
129                 return NULL;
130
131         ugd->win = parent;
132         ugd->evas = evas_object_evas_get(ugd->win);
133
134         bindtextdomain(PACKAGE, LOCALEDIR);
135
136         ugd->block_cbs.layout_cb = _layout_block_cb;
137         ugd->block_cbs.result_cb = _result_block_cb;
138         ugd->block_cbs.destroy_cb = _destroy_block_cb;
139         ugd->block_cbs.priv = (void *)ugd;
140
141         /* Init Setting View Data */
142         _init_data(ugd);
143
144         /* theme extension */
145         ugd->theme = elm_theme_new();
146         elm_theme_ref_set(ugd->theme, NULL);
147
148         ugd->layout_main = _create_layout(parent, ugd);
149         evas_object_smart_callback_add(ugd->win, "profile,changed", _win_profile_changed_cb, ugd);
150         _create_navigationbar(ugd);
151         ugd->base = ugd->layout_main;
152         elm_win_indicator_mode_set(ugd->win, ELM_WIN_INDICATOR_SHOW);
153
154         return ugd->layout_main;
155 }
156
157 static void on_event(ui_gadget_h ug, enum ug_event event, service_h service, void *priv)
158 {
159         debug_log("");
160
161         if (!ug || !priv)
162                 return;
163
164         EmailSettingUGD *ugd;
165         ugd = priv;
166
167         switch (event) {
168         case UG_EVENT_LOW_MEMORY:
169                 break;
170         case UG_EVENT_LOW_BATTERY:
171                 break;
172         case UG_EVENT_LANG_CHANGE:
173         _language_changed_cb(ugd);
174                 break;
175         case UG_EVENT_ROTATE_PORTRAIT:
176         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
177         case UG_EVENT_ROTATE_LANDSCAPE:
178         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
179                 if (ugd->custom_rotate_cb && ugd->custom_rotate_cb_data)
180                         ugd->custom_rotate_cb(ugd->custom_rotate_cb_data);
181                 break;
182         case UG_EVENT_REGION_CHANGE:
183                 break;
184         default:
185                 break;
186         }
187         return;
188 }
189
190 static void on_key_event(ui_gadget_h ug, enum ug_key_event event, service_h service, void *priv)
191 {
192         debug_log("");
193
194         if (!ug || !priv)
195                 return;
196
197         EmailSettingUGD *ugd;
198         ugd = priv;
199
200         switch (event) {
201         case UG_KEY_EVENT_END:
202                 ugd->end_key_event = 1;
203
204                 if (elm_naviframe_top_item_get(ugd->navi_bar) == elm_naviframe_bottom_item_get(ugd->navi_bar)) {
205                         if (ugd->popup_one != NULL || ugd->popup_validate != NULL) {
206                                 if (ugd->popup_one) {
207                                         evas_object_del(ugd->popup_one);
208                                         ugd->popup_one = NULL;
209                                 }
210
211                                 if (ugd->popup_validate) {
212                                         evas_object_del(ugd->popup_validate);
213                                         ugd->popup_validate = NULL;
214
215                                         ugd->cancel_event = 1;
216                                         email_engine_stop_working(ugd->account_id, ugd->handle);
217                                 }
218                         } else {
219                                 ug_destroy_me(ug);
220                         }
221                 } else {
222                         if (ugd->popup_one != NULL || ugd->popup_validate != NULL) {
223                                 if (ugd->popup_one) {
224                                         evas_object_del(ugd->popup_one);
225                                         ugd->popup_one = NULL;
226                                 }
227
228                                 if (ugd->popup_validate) {
229                                         evas_object_del(ugd->popup_validate);
230                                         ugd->popup_validate = NULL;
231
232                                         ugd->cancel_event = 1;
233                                         email_engine_stop_working(ugd->account_id, ugd->handle);
234                                 }
235                         } else {
236                                 if (ugd->change_view_ing == 0)
237                                         setting_change_view(VIEW_BACK, ugd);
238                         }
239                 }
240                 break;
241
242         default:
243                 break;
244         }
245 }
246
247 static void on_message(ui_gadget_h ug, service_h msg, service_h service, void *priv)
248 {
249         debug_log("");
250         EmailSettingUGD *ugd = NULL;
251         char *rotation_str = NULL;
252         int rotation_mode = UG_EVENT_ROTATE_PORTRAIT;
253
254         ugd = (EmailSettingUGD *)priv;
255         if (!ug || !msg)
256                 return;
257
258         service_get_extra_data(msg, EMAIL_BUNDLE_KEY_ROTATE, &rotation_str);
259         if (rotation_str) {
260                 debug_log("rotation mode: %s", rotation_str);
261                 int angle = 0;
262                 rotation_mode = atoi(rotation_str);
263                 switch (rotation_mode) {
264                         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
265                                 angle = 180;
266                                 break;
267                         case UG_EVENT_ROTATE_LANDSCAPE:
268                                 angle = -90;
269                                 break;
270                         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
271                                 angle = 90;
272                                 break;
273                         case UG_EVENT_ROTATE_PORTRAIT:
274                         default:
275                                 angle = 0;
276                                 break;
277                 }
278
279                 elm_win_rotation_with_resize_set(ugd->win, angle);
280         }
281         return;
282 }
283
284 static void on_destroy(ui_gadget_h ug, service_h service, void *priv)
285 {
286         debug_log("");
287         EmailSettingUGD *ugd;
288         struct viewdata *vd = NULL;
289         int i, r = 0;
290
291         if (!ug || !priv)
292                 return;
293
294         ugd = priv;
295         evas_object_del(ugd->base);
296         ugd->base = NULL;
297
298         if (ugd->popup_one) {
299                 evas_object_del(ugd->popup_one);
300                 ugd->popup_one = NULL;
301         }
302
303         if (ugd->popup_validate) {
304                 evas_object_del(ugd->popup_validate);
305                 ugd->popup_validate = NULL;
306         }
307
308         if (ugd->focus_timer) {
309                 ecore_timer_del(ugd->focus_timer);
310                 ugd->focus_timer = NULL;
311         }
312
313         if (ugd->preset_vc_timer) {
314                 ecore_timer_del(ugd->preset_vc_timer);
315                 ugd->preset_vc_timer = NULL;
316         }
317
318         if (ugd->other_vc_timer) {
319                 ecore_timer_del(ugd->other_vc_timer);
320                 ugd->other_vc_timer = NULL;
321         }
322
323         if (ugd->edit_vc_timer) {
324                 ecore_timer_del(ugd->edit_vc_timer);
325                 ugd->edit_vc_timer = NULL;
326         }
327
328         if (ugd->del_timer) {
329                 ecore_timer_del(ugd->del_timer);
330                 ugd->del_timer = NULL;
331         }
332
333         setting_free_sp_desc(ugd);
334
335         /* theme extension */
336         elm_theme_free(ugd->theme);
337
338         /* destroying view */
339         int count;
340         Viewtype cur_view;;
341         if (ugd->view_top >= 0) {
342         for (count = ugd->view_top; count >= 0; count--) {
343             cur_view = ugd->view_st[count];
344             vd = ugd->vd[cur_view];
345             debug_log("destorying view: %s", vd->grp_nm);
346             if (vd->destroy)
347                 vd->destroy(vd);
348         }
349     }
350
351         /* Free Viewdata's memory */
352         for (i = VIEW_SETTING; i < VIEW_END; i++) {
353                 vd = ugd->vd[i];
354                 if (vd != NULL) {
355                         debug_log("Memory Free");
356                         free(vd);
357                 }
358         }
359
360         email_engine_finalize();
361         r = _account_finalize(ugd);
362         _remove_dbus_receiver(ugd);
363 }
364
365 static void on_resume(ui_gadget_h ug, service_h service, void *priv)
366 {
367         debug_log("");
368
369         struct ug_data *ugd;
370
371         if (!ug || !priv)
372                 return;
373
374         ugd = priv;
375
376         ugd->b_on_pause = 0;
377
378         return;
379 }
380
381 static void on_pause(ui_gadget_h ug, service_h service, void *priv)
382 {
383         debug_log("");
384
385         struct ug_data *ugd;
386
387         if (!ug || !priv)
388                 return;
389
390         ugd = priv;
391
392         ugd->b_on_pause = 1;
393
394         return;
395 }
396
397 static void on_start(ui_gadget_h ug, service_h service, void *priv)
398 {
399         debug_log("");
400         struct ug_data *ugd;
401         int ret = 0;
402
403         if (!ug || !priv)
404                 return;
405
406         ugd = priv;
407         ugd->conformant = (Evas_Object *)ug_get_conformant();
408
409         /* Init Email Service */
410         if (!email_engine_initialize()) {
411                 Evas_Object *popup = elm_popup_add(ugd->layout_main);
412                 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
413                 elm_object_part_text_set(popup, "title,text", EMAIL_SETTING_STRING_WARNING);
414                 elm_object_text_set(popup, EMAIL_SETTING_STRING_FAILED_TO_START_EMAIL_APPLICATION);
415
416                 Evas_Object *btn1 = elm_button_add(popup);
417                 elm_object_style_set(btn1, "popup_button/default");
418                 elm_object_text_set(btn1, EMAIL_SETTING_STRING_OK);
419                 elm_object_part_content_set(popup, "button1", btn1);
420                 evas_object_smart_callback_add(btn1, "clicked", _popup_ok_cb, ugd);
421                 evas_object_show(popup);
422                 return;
423         }
424
425         /* Init Emf Account */
426         if (!_account_init(ugd)) {
427                 debug_log("Failed to get account list");
428         }
429
430         /* create xml tree */
431         setting_free_sp_desc(ugd);
432
433         xmlDocPtr xml_source = setting_create_xml_tree(SP_XML_PATH);
434         if(!xml_source) {
435                 Evas_Object *popup = elm_popup_add(ugd->layout_main);
436                 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
437                 elm_object_part_text_set(popup, "title,text", EMAIL_SETTING_STRING_WARNING);
438                 elm_object_text_set(popup, EMAIL_SETTING_STRING_FAIL_PARSE_XML);
439
440                 Evas_Object *btn1 = elm_button_add(popup);
441                 elm_object_style_set(btn1, "popup_button/default");
442                 elm_object_text_set(btn1, EMAIL_SETTING_STRING_OK);
443                 elm_object_part_content_set(popup, "button1", btn1);
444                 evas_object_smart_callback_add(btn1, "clicked", _popup_ok_cb, ugd);
445                 evas_object_show(popup);
446                 return;
447         }
448         ugd->xml_source = xml_source;
449
450         /* traverse xml tree */
451         xmlNode *root;
452         root= xmlDocGetRootElement(xml_source);
453         int sp_count = xmlChildElementCount(root);
454         ugd->sp_list_len = sp_count;
455         debug_log("Count of SP list:%d", sp_count);
456         if (sp_count > 0) {
457                 ugd->sp_list = (EmailSpDesc *)calloc(sp_count, sizeof(EmailSpDesc));
458                 setting_traverse_xml_node(root, 0, (void *)ugd, 1);
459                 setting_dump_sp_desc(ugd);
460         }
461         setting_cleanup_xml_parser(xml_source);
462
463         if (!_parse_option(service, ugd)) {
464                 return;
465         }
466
467         _dispatch_view(ugd);
468
469         /* DBUS */
470         _dbus_receiver_setup(ugd);
471
472         return;
473 }
474
475 static void _init_data(struct ug_data *ugd)
476 {
477         debug_log("");
478         struct viewdata *view_setting = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
479         struct viewdata *view_sp_select = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
480         struct viewdata *view_account_setup = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
481         struct viewdata *view_manual_setup = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
482         struct viewdata *view_account_edit = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
483         struct viewdata *view_mailbox_mapping = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
484         struct viewdata *view_account_options = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
485         struct viewdata *view_initial_access = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
486         struct viewdata *view_sync_schedule = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
487         struct viewdata *view_sync_setup = (struct viewdata *)calloc(sizeof(struct viewdata), 1);
488
489         setting_init_setting_view(view_setting);
490         setting_init_sp_select_view(view_sp_select);
491         setting_init_account_setup_view(view_account_setup);
492         setting_init_manual_setup_view(view_manual_setup);
493         setting_init_account_edit_view(view_account_edit);
494         setting_init_mailbox_mapping_view(view_mailbox_mapping);
495         setting_init_account_options_view(view_account_options);
496         setting_init_initial_access_view(view_initial_access);
497         setting_init_sync_schedule_view(view_sync_schedule);
498         setting_init_sync_setup_view(view_sync_setup);
499
500         ugd->view_top = -1;
501
502         _couple_data(ugd, VIEW_SETTING, view_setting);
503         _couple_data(ugd, VIEW_SP_SELECT, view_sp_select);
504         _couple_data(ugd, VIEW_ACCOUNT_SETUP, view_account_setup);
505         _couple_data(ugd, VIEW_MANUAL_SETUP, view_manual_setup);
506         _couple_data(ugd, VIEW_ACCOUNT_EDIT, view_account_edit);
507         _couple_data(ugd, VIEW_MAILBOX_MAPPING, view_mailbox_mapping);
508         _couple_data(ugd, VIEW_ACCOUNT_OPTIONS, view_account_options);
509         _couple_data(ugd, VIEW_INITIAL_ACCESS, view_initial_access);
510         _couple_data(ugd, VIEW_SYNC_SCHEDULE, view_sync_schedule);
511         _couple_data(ugd, VIEW_SYNC_SETUP, view_sync_setup);
512
513         /* add more views... */
514 }
515
516 static void _couple_data(struct ug_data *ugd, Viewtype t, struct viewdata *vd)
517 {
518         debug_log("");
519         debug_log("View type number%d", t);
520         ugd->vd[t] = vd;
521         vd->ugd = ugd;
522 }
523
524 static void _dbus_receiver_setup(struct ug_data *ugd)
525 {
526         debug_log("");
527
528         if (!ugd) {
529                 debug_log("ugd == NULL");
530                 return;
531         }
532
533         int ret = e_dbus_init();
534         debug_log("ret: %d", ret);
535
536         DBusError derror;
537
538         if (ugd->dbus_conn == NULL) {
539                 debug_log("");
540                 dbus_error_init(&derror);
541                 ugd->dbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
542
543                 if (ugd->dbus_conn == NULL) {
544                         debug_log("e_dbus_bus_get return NULL");
545                 }
546
547                 /* storage */
548                 if (e_dbus_request_name(ugd->dbus_conn, "User.Email.StorageChange", 0, NULL, NULL) == NULL) {
549                         debug_critical("Failed to e_dbus_request_name()");
550                         return;
551                 }
552
553                 if (ugd->signal_handler_storage != NULL) {
554                         debug_log("_g_setting_signal_handler_storage != NULL");
555                 }
556
557                 ugd->signal_handler_storage = e_dbus_signal_handler_add(ugd->dbus_conn, NULL,
558                                                                         "/User/Email/StorageChange",
559                                                                         "User.Email.StorageChange", "email",
560                                                                         setting_edbus_event_receive,
561                                                                         (struct ug_data *)ugd);
562
563                 if (ugd->signal_handler_storage == NULL) {
564                         debug_log("Failed to e_dbus_signal_handler_add()");
565                         return;
566                 }
567
568                 /* network */
569                 if (e_dbus_request_name(ugd->dbus_conn, "User.Email.NetworkStatus", 0, NULL, NULL) == NULL) {
570                         debug_critical("Failed to e_dbus_request_name()");
571                         return;
572                 }
573                 if (ugd->signal_handler_network != NULL) {
574                         debug_log("_g_setting_signal_handler_network != NULL");
575                 }
576                 ugd->signal_handler_network = e_dbus_signal_handler_add(ugd->dbus_conn, NULL,
577                                                                         "/User/Email/NetworkStatus",
578                                                                         "User.Email.NetworkStatus", "email",
579                                                                         setting_edbus_event_receive,
580                                                                         (struct ug_data *)ugd);
581
582                 if (ugd->signal_handler_network == NULL) {
583                         debug_critical("Failed to e_dbus_signal_handler_add()");
584                         return;
585                 }
586         }
587 }
588
589 static void _remove_dbus_receiver(struct ug_data *ugd)
590 {
591         debug_log("");
592
593         if (!ugd) {
594                 debug_log("ugd == NULL");
595                 return;
596         }
597
598         if (ugd->signal_handler_storage != NULL) {
599                 e_dbus_signal_handler_del(ugd->dbus_conn, ugd->signal_handler_storage);
600                 ugd->signal_handler_storage = NULL;
601         }
602
603         if (ugd->signal_handler_network != NULL) {
604                 e_dbus_signal_handler_del(ugd->dbus_conn, ugd->signal_handler_network);
605                 ugd->signal_handler_network = NULL;
606         }
607
608         if (e_dbus_release_name(ugd->dbus_conn, "User.Email.StorageChange", NULL, NULL) == NULL) {
609                 debug_critical("e_dbus_release_name to failed");
610         }
611
612         if (e_dbus_release_name(ugd->dbus_conn, "User.Email.NetworkStatus", NULL, NULL) == NULL) {
613                 debug_critical("e_dbus_release_name to failed");
614         }
615
616         if (ugd->dbus_conn != NULL) {
617                 /*e_dbus_connection_close(ugd->dbus_conn);*/
618                 ugd->dbus_conn = NULL;
619         }
620 }
621
622 static int _account_init(struct ug_data *ugd)
623 {
624         debug_log("");
625         int i = 0;
626
627         /* Get Email Account List Info */
628         ugd->account_list = NULL;
629         if (!email_engine_get_account_list(&ugd->account_count, &ugd->account_list)) {
630                 return FALSE;
631         }
632
633         for (i = 0; i < ugd->account_count; i++) {
634                 debug_log("*************Sending Option Details*************");
635                 debug_log("Acct Id                              : %d", ugd->account_list[i].account_id);
636                 debug_log("Email Addr                   : %s", ugd->account_list[i].user_email_address);
637                 debug_log("1.Priority                   : %d", ugd->account_list[i].options.priority);
638                 debug_log("2.Keep a copy                : %d", ugd->account_list[i].options.keep_local_copy);
639                 debug_log("3.Reply with body    : %d", ugd->account_list[i].options.reply_with_body);
640                 debug_log("4.Forward with files : %d", ugd->account_list[i].options.forward_with_files);
641                 debug_log("5.Get read report    : %d", ugd->account_list[i].options.req_read_receipt);
642                 debug_log("6.Get delivery report: %d", ugd->account_list[i].options.req_delivery_receipt);
643                 debug_log("7.Add my namecard    : %d", ugd->account_list[i].options.add_myname_card);
644                 debug_log("8.Add signature              : %d", ugd->account_list[i].options.add_signature);
645         }
646
647         return TRUE;
648 }
649
650 static int _account_finalize(EmailSettingUGD *ugd)
651 {
652         debug_log("");
653         int r;
654
655         /* Free Account List Info */
656         if (ugd->account_list != NULL) {
657                 r = email_engine_free_account_list(&(ugd->account_list), ugd->account_count);
658                 retv_if(r == FALSE, -1);
659         }
660         ugd->account_list = NULL;
661
662         return TRUE;
663 }
664
665 static int _parse_option(service_h service, struct ug_data *ugd)
666 {
667         debug_log("");
668
669         if (!service) {
670                 debug_log("service is NULL");
671                 return FALSE;
672         }
673
674         int ret;
675         char *operation = NULL;
676         ret = service_get_operation(service, &operation);
677         debug_log("service_get_operation: %d", ret);
678         debug_log("operation = %s", operation);
679
680         if (operation) {
681 #if 0
682                 if (g_strcmp0(operation, ACCOUNT_OPERATION_SIGNIN) == 0) {
683                         char *email_sp = NULL;
684                         debug_log("Operation ACCOUNT_OPERATION_SIGNIN");
685                         service_get_extra_data(service, /*ACCOUNT_DATA_SERVICE_PROVIDER*/ "http://tizen.org/account/data/service_provider", &email_sp);
686
687                         if (!email_sp) {
688                                 return FALSE;
689                         }
690                         debug_log("SP:%s", email_sp);
691
692                         if (ugd->start_view_type)
693                                 g_free(ugd->start_view_type);
694
695                         ugd->start_view_type = g_strdup(EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_ADD);
696
697                         EmailSpDesc *desc = NULL;
698                         if (g_strcmp0(email_sp, "gmail") == 0)
699                                 desc = setting_find_sp_desc("Gmail", ugd);
700                         else if (g_strcmp0(email_sp, "msn") == 0)
701                                 desc = setting_find_sp_desc("Hotmail", ugd);
702                         else
703                                 desc = NULL;
704
705                         if (desc)
706                                 ugd->account_info = desc;
707                         else
708                                 ugd->account_info = NULL;
709
710                         g_free(email_sp);
711                         return TRUE;
712
713                 } else if (g_strcmp0(operation, ACCOUNT_OPERATION_VIEW) == 0) {
714                         char *account_id = NULL;
715                         char *username = NULL;
716                         debug_log("Operation ACCOUNT_OPERATION_VIEW");
717
718                         service_get_extra_data(service, ACCOUNT_DATA_ID, &account_id);
719                         service_get_extra_data(service, ACCOUNT_DATA_USERNAME, &username);
720
721                         if (!account_id) {
722                                 return FALSE;
723                         }
724
725                         int my_account_id = 0;
726                         my_account_id = atoi(account_id);
727                         g_free(account_id);
728                         debug_log("MY_ACCOUNT_ID:%d", my_account_id);
729
730                         if (ugd->start_view_type)
731                                 g_free(ugd->start_view_type);
732
733                         ugd->start_view_type = g_strdup(EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_EDIT);
734
735                         int i = 0;
736
737                         if (ugd->account_list == NULL || ugd->account_count == 0) {
738                                 debug_critical("account info is @niL");
739                                 Evas_Object *popup = elm_popup_add(ugd->layout_main);
740                                 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
741                                 elm_object_part_text_set(popup, "title,text", EMAIL_SETTING_STRING_WARNING);
742                                 elm_object_text_set(popup, EMAIL_SETTING_STRING_ACCOUNT_LIST_NOT_FOUND);
743
744                                 Evas_Object *btn1 = elm_button_add(popup);
745                                 elm_object_style_set(btn1, "popup_button/default");
746                                 elm_object_text_set(btn1, EMAIL_SETTING_STRING_OK);
747                                 elm_object_part_content_set(popup, "button1", btn1);
748                                 evas_object_smart_callback_add(btn1, "clicked", _popup_ok_cb, ugd);
749                                 evas_object_show(popup);
750                                 return FALSE;
751                         }
752
753                         for (i = 0; i < ugd->account_count; i++) {
754                                 if (ugd->account_list[i].account_svc_id == my_account_id) {
755                                         ugd->account_id = ugd->account_list[i].account_id;
756                                         debug_log("ACCOUNT_ID:%d", ugd->account_id);
757                                 }
758                         }
759
760                         return TRUE;
761
762                 }
763 #endif
764         } else {
765                 service_get_extra_data(service, EMAIL_BUNDLE_KEY_VIEW_TYPE, &(ugd->start_view_type));
766                 debug_log("VIEW TYPE:%s", ugd->start_view_type);
767
768                 if (strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_EDIT) == 0) {
769                         /* account edit view */
770                         char *str = NULL;
771                         int my_account_id = 0;
772                         service_get_extra_data(service, EMAIL_BUNDLE_KEY_ACCOUNT_ID, &str);
773                         my_account_id = atoi(str);
774                         g_free(str);
775                         debug_log("MY_ACCOUNT_ID:%d", my_account_id);
776
777                         int i = 0;
778
779                         if (ugd->account_list == NULL || ugd->account_count == 0) {
780                                 debug_critical("account info is @niL");
781                                 Evas_Object *popup = elm_popup_add(ugd->layout_main);
782                                 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
783                                 elm_object_part_text_set(popup, "title,text", EMAIL_SETTING_STRING_WARNING);
784                                 elm_object_text_set(popup, EMAIL_SETTING_STRING_ACCOUNT_LIST_NOT_FOUND);
785
786                                 Evas_Object *btn1 = elm_button_add(popup);
787                                 elm_object_style_set(btn1, "popup_button/default");
788                                 elm_object_text_set(btn1, EMAIL_SETTING_STRING_OK);
789                                 elm_object_part_content_set(popup, "button1", btn1);
790                                 evas_object_smart_callback_add(btn1, "clicked", _popup_ok_cb, ugd);
791                                 evas_object_show(popup);
792                                 return FALSE;
793                         }
794
795                         for (i = 0; i < ugd->account_count; i++) {
796                                 if (ugd->account_list[i].account_svc_id == my_account_id) {
797                                         ugd->account_id = ugd->account_list[i].account_id;
798                                         debug_log("ACCOUNT_ID:%d", ugd->account_id);
799                                 }
800                         }
801                 } else if (strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_ADD) == 0) {
802                         /* account add view */
803                         char *account_name = NULL;
804                         service_get_extra_data(service, EMAIL_BUNDLE_KEY_ESP_NAME, &account_name);
805                         debug_log("Account name:%s", account_name);
806
807                         EmailSpDesc *desc = NULL;
808                         if (g_strcmp0(account_name, "gmail") == 0)
809                                 desc = setting_find_sp_desc("Gmail", ugd);
810                         else if (g_strcmp0(account_name, "msn") == 0)
811                                 desc = setting_find_sp_desc("Hotmail", ugd);
812                         else
813                                 desc = NULL;
814
815                         if (desc)
816                                 ugd->account_info = desc;
817                         else
818                                 ugd->account_info = NULL;
819
820                         g_free(account_name);
821                 }
822         }
823
824         return TRUE;
825 }
826
827 static void _dispatch_view(struct ug_data *ugd)
828 {
829         debug_log("");
830
831         if (ugd->start_view_type) {
832                 if (!strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_SETTING_MAIN)) {
833                         debug_log("Setting mail is up");
834                         setting_change_view(VIEW_SETTING, ugd);
835                 } else if (!strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_FIRST_SETUP)) {
836                         debug_log("Setup wizard is up");
837                         ugd->account_info = NULL;
838                         setting_change_view(VIEW_ACCOUNT_SETUP, ugd);
839                 } else if (!strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_EDIT)) {
840                         debug_log("Edit view is up");
841                         setting_change_view(VIEW_SYNC_SCHEDULE, ugd);
842                 } else if (!strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_ACCOUNT_ADD)) {
843                         if(ugd->account_count >= MAX_ACCOUNT_COUNT) {
844                                 _create_max_account_popup_create(ugd);
845                         } else {
846                                 debug_log("Add view is up");
847                                 setting_change_view(VIEW_ACCOUNT_SETUP, ugd);
848                         }
849                 } else if (!strcmp(ugd->start_view_type, EMAIL_BUNDLE_VAL_VIEW_SELECT_ACCOUNT)) {
850                         debug_log("select account view is up");
851                         ugd->account_info = NULL;
852                         setting_change_view(VIEW_ACCOUNT_SETUP, ugd);
853                 } else {
854                         setting_change_view(VIEW_SETTING, ugd);
855                 }
856         } else {
857                 debug_log("No service value!");
858                 setting_change_view(VIEW_SETTING, ugd);
859         }
860         return;
861 }
862
863 static void _create_navigationbar(struct ug_data *ugd)
864 {
865         debug_log("");
866         ugd->navi_bar = elm_naviframe_add(ugd->layout_main);
867
868         elm_object_part_content_set(ugd->layout_main, "elm.swallow.content", ugd->navi_bar);
869         evas_object_show(ugd->navi_bar);
870 }
871
872 static Evas_Object *_create_layout(Evas_Object *parent, struct ug_data *ugd)
873 {
874         debug_log("");
875         Evas_Object *base;
876
877         ugd->bg = elm_bg_add(ugd->win);
878         evas_object_size_hint_weight_set(ugd->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
879         elm_win_resize_object_add(ugd->win, ugd->bg);
880         /*elm_object_theme_set(ugd->bg, ugd->theme);*/
881         evas_object_show(ugd->bg);
882
883         base = elm_layout_add(parent);
884         if (base == NULL) {
885                 debug_log("base == NULL");
886                 return NULL;
887         }
888
889         elm_layout_theme_set(base, "layout", "application", "default");
890         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
891         evas_object_show(base);
892
893         elm_object_style_set(ugd->bg, "group_list");
894         elm_object_part_content_set(base, "elm.swallow.bg", ugd->bg);
895
896         return base;
897 }
898
899 static void _win_profile_changed_cb(void *data, Evas_Object *obj, void *event)
900 {
901         debug_log("");
902         if (!data) {
903                 debug_log("data is NULL");
904                 return;
905         }
906
907         EmailSettingUGD *ugd = (EmailSettingUGD *)data;
908
909         const char *profile = elm_config_profile_get();
910         if (!g_strcmp0(profile, "desktop"))
911                 elm_layout_theme_set(ugd->layout_main, "layout", "application", "noindicator");
912         else
913                 elm_layout_theme_set(ugd->layout_main, "layout", "application", "default");
914
915         evas_object_size_hint_weight_set(ugd->layout_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
916         evas_object_show(ugd->layout_main);
917 }
918
919 static void _layout_block_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
920 {
921         debug_log("");
922         EmailSettingUGD *ugd;
923         Evas_Object *base;
924
925         if (!ug || !priv)
926                 return;
927
928         ugd = (EmailSettingUGD *)priv;
929
930         base = ug_get_layout(ug);
931         if (!base)
932                 return;
933
934         switch (mode) {
935         case UG_MODE_FULLVIEW:
936                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
937                 evas_object_show(base);
938                 break;
939         default:
940                 break;
941         }
942 }
943
944 static void _result_block_cb(ui_gadget_h ug, service_h service, void *priv)
945 {
946         debug_log("");
947         EmailSettingUGD *ugd;
948
949         if (!ug || !priv)
950                 return;
951
952         ugd = (EmailSettingUGD *)priv;
953
954         return;
955 }
956
957 static void _destroy_block_cb(ui_gadget_h ug, void *priv)
958 {
959         debug_log("");
960         EmailSettingUGD *ugd;
961
962         if (!ug || !priv)
963                 return;
964
965         ugd = (EmailSettingUGD *)priv;
966
967         if (ug == ugd->ug_block) {
968                 ug_destroy(ugd->ug_block);
969                 ugd->ug_block = NULL;
970         }
971
972         return;
973 }
974
975 static void _popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
976 {
977         debug_log("");
978
979         if (!data) {
980                 debug_log("data is NULL");
981                 return;
982         }
983
984         if (obj)
985                 evas_object_del(obj);
986
987         EmailSettingUGD *ugd = (EmailSettingUGD *)data;
988         ug_destroy_me(ugd->ug);
989 }
990
991 static void _create_max_account_popup_create(EmailSettingUGD *ugd)
992 {
993         debug_log("");
994         Evas_Object *popup = elm_popup_add(ugd->win);
995         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
996         elm_object_text_set(popup, EMAIL_SETTING_STRING_ACCOUNT_NUM_MAX);
997         ugd->popup_one = popup; //Use ugd->popup_one member
998
999         Evas_Object *btn1 = elm_button_add(popup);
1000         elm_object_text_set(btn1, EMAIL_SETTING_STRING_OK);
1001         elm_object_part_content_set(popup, "button1", btn1);
1002         evas_object_smart_callback_add(btn1, "clicked", _max_account_popup_ok_cb, ugd);
1003         evas_object_show(popup);
1004
1005 }
1006
1007 static void _max_account_popup_ok_cb(void *data, Evas_Object *obj, void *event_info)
1008 {
1009         debug_log("");
1010
1011         if (!data) {
1012                 debug_log("data is NULL");
1013                 return;
1014         }
1015         EmailSettingUGD *ugd = (EmailSettingUGD *)data;
1016
1017         if (ugd->popup_one) {
1018                 evas_object_del(ugd->popup_one);
1019                 ugd->popup_one = NULL;
1020         }
1021
1022         ug_destroy_me(ugd->ug);
1023
1024 }
1025
1026 static void _language_changed_cb(EmailSettingUGD *ugd)
1027 {
1028         debug_log("");
1029         struct viewdata *vd = NULL;
1030         int count;
1031         Viewtype cur_view;;
1032
1033         if (ugd->view_top < 0) {
1034                 debug_log("View stack is empty..");
1035                 return;
1036         }
1037
1038         for (count = ugd->view_top; count >= 0; count--) {
1039                 cur_view = ugd->view_st[count];
1040                 vd = ugd->vd[cur_view];
1041                 debug_log("language change: %s", vd->grp_nm);
1042                 if (vd->lang_change)
1043                         vd->lang_change(vd);
1044         }
1045 }
1046 /* EOF */