3642ae08912aee7da6c4707c544bfcac3fa8d684
[apps/home/my-account.git] / src / myaccount_ug_main.c
1 /*
2  *  my-account
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Tarun Kumar <tarun.kr@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "myaccount_ug_account_list.h"
23 #include "myaccount_ug_addaccount.h"
24 #include "myaccount_ug_common.h"
25 #include <vconf.h>
26 #include <dlfcn.h>
27 #include <account_internal.h>
28
29 #ifndef UG_MODULE_API
30 #define UG_MODULE_API __attribute__ ((visibility("default")))
31 #endif
32
33 static myaccount_appdata *g_appdata = NULL;
34 static int g_handle_noti = 0;
35
36 int myaccount_subscribe_to_account_notification_vconfkey();
37 static void __reset_indicator(myaccount_appdata *ugd);
38
39 static void __change_ui_theme_changed_cb(keynode_t *pNode, void *pUserData)
40 {
41         MYACCOUNT_DBUG("__change_ui_theme_changed_cb");
42         if(!pUserData) {
43                 MYACCOUNT_ERROR("pUserDate is NULL:__vp_play_view_change_ui_theme_changed_cb.");
44                 return;
45         }
46         int value = vconf_keynode_get_int(pNode);
47         MYACCOUNT_DBUG("value[%d]", value);
48
49         myaccount_appdata *ad = (myaccount_appdata*)pUserData;
50
51 //      ea_theme_object_style_set(ad->layout_main, EA_THEME_STYLE_DARK);
52 //      // Color Table
53 //      if (ad->color_table) {
54 //              ea_theme_object_colors_set(ad->layout_main, ad->color_table, EA_THEME_STYLE_DARK);
55 //      }
56         ea_theme_object_style_set(ad->layout_main, EA_THEME_STYLE_LIGHT);
57         // Color Table
58         if (ad->color_table) {
59                 ea_theme_object_colors_set(ad->layout_main, ad->color_table, EA_THEME_STYLE_LIGHT);
60         }
61         // Font Table
62         if (ad->font_table) {
63                 ea_theme_object_fonts_set(ad->layout_main, ad->font_table);
64         }
65
66         return;
67 }
68
69 static void __init_changeable_resource(myaccount_appdata *ugd)
70 {
71         if (!ugd) {
72                 MYACCOUNT_ERROR("app data is NULL");
73                 return;
74         }
75         // Color Table
76         ugd->color_table = ea_theme_color_table_new("/usr/apps/setting-myaccount-efl/res/ug-setting-cloud/shared/res/tables/com.samsung.my-account_ChangeableColorInfo.xml");
77         if (ugd->color_table) {
78 //              ea_theme_object_colors_set(ugd->layout_main, ugd->color_table, EA_THEME_STYLE_DARK);
79                 ea_theme_object_colors_set(ugd->layout_main, ugd->color_table, EA_THEME_STYLE_LIGHT);
80                 MYACCOUNT_DBUG("color table create SUCCESS");
81         } else {
82                 MYACCOUNT_ERROR("color table create fail");
83         }
84
85         // Font Table
86         ugd->font_table = ea_theme_font_table_new("/usr/apps/setting-myaccount-efl/res/ug-setting-cloud/shared/res/tables/com.samsung.my-account_ChangeableFontInfo.xml");
87         if (ugd->font_table) {
88                 ea_theme_object_fonts_set(ugd->layout_main, ugd->font_table);
89                 MYACCOUNT_DBUG("font table create SUCCESS");
90         } else {
91                 MYACCOUNT_ERROR("font table create fail");
92         }
93
94         // regist UI theme
95         int err = 0;
96         err = vconf_notify_key_changed(VCONFKEY_SETAPPL_CHANGE_UI_THEME_INT, __change_ui_theme_changed_cb, ugd);
97         if (err != 0) {
98                 MYACCOUNT_ERROR("vconf_notify_key_changed is fail [0x%x]", err);
99         }
100         MYACCOUNT_DBUG("VCONFKEY_SETAPPL_CHANGE_UI_THEME_INT[%s]", VCONFKEY_SETAPPL_CHANGE_UI_THEME_INT);
101
102 }
103
104 static void __free_changeable_resource(myaccount_appdata *ad)
105 {
106         if (!ad) {
107                 MYACCOUNT_ERROR("app data is NULL");
108                 return;
109         }
110         vconf_ignore_key_changed( VCONFKEY_SETAPPL_CHANGE_UI_THEME_INT, __change_ui_theme_changed_cb );
111         if (ad->color_table != NULL) {
112 //              ea_theme_colors_unset(ad->color_table, EA_THEME_STYLE_DARK);
113                 ea_theme_colors_unset(ad->color_table, EA_THEME_STYLE_LIGHT);
114                 ea_theme_color_table_free(ad->color_table);
115                 ad->color_table = NULL;
116         }
117         if (ad->font_table != NULL) {
118                 ea_theme_font_table_free(ad->font_table);
119                 ad->font_table = NULL;
120         }
121 }
122
123 static void lang_changed(void * priv)
124 {
125         if (!priv) {
126                 MYACCOUNT_SLOGE("lang_changed ### priv=%p\n", priv);
127                 return ;
128         }
129
130         myaccount_appdata *ugd;
131         ugd = (myaccount_appdata*)priv;
132
133         char* language = NULL;
134         language = vconf_get_str(VCONFKEY_LANGSET);
135
136         if(language) {
137                 char tmp_buf[6]={0,};
138                 MA_STRNCPY(tmp_buf, language, 6);
139                 ugd->current_language = strdup(tmp_buf);
140         }
141
142         elm_object_item_text_set (ugd->nf_it, dgettext(MA_UG_NAME, "IDS_MA_HEADER_ACCOUNTS"));
143         myaccount_list_refresh_item_list(ugd);
144
145         if( ugd->add_genlist ) {
146                 // title text update
147                 elm_object_item_text_set (ugd->add_nf_it, dgettext(MA_UG_NAME, "IDS_MA_HEADER_ADD_ACCOUNT"));
148                 myaccount_addaccount_refresh_item_list(ugd);
149         } else {
150                 MYACCOUNT_SLOGE("lang_changed ### ugd->add_genlist is NULL\n");
151         }
152 }
153
154
155 static gboolean __myaccount_account_list_parse_param(myaccount_mode_e
156                                                                                                                 *mode,
157                                                                                                                 app_control_h data,
158                                                                                                                 myaccount_appdata *ad)
159 {
160         if (!data || !ad) {
161                 return FALSE;
162         } else {
163                 char *extracted = NULL;
164                 ad->capability_filter = NULL;
165                 app_control_get_extra_data(data, "mode", &extracted);
166                 app_control_get_extra_data(data, "capability_filter", &ad->capability_filter);
167                 app_control_get_extra_data(data, "myaccount_userdata", &ad->caller_userdata);
168                 app_control_get_extra_data(data, "from", &ad->called_from);
169                 if (extracted) {
170                         MYACCOUNT_SLOGD("view_account_list = %s\n", extracted);
171
172                         if (!strcmp(extracted, "account_list"))
173                                 *mode = eMYACCOUNT_ACCOUNT_LIST;
174                         else if (!strcmp(extracted, "add_account"))
175                                 *mode = eMYACCOUNT_ADD_ACCOUNT;
176                         else
177                                 *mode = eMYACCOUNT_ACCOUNT_LIST;
178                 } else {
179                         *mode = eMYACCOUNT_ACCOUNT_LIST;
180                 }
181
182                 MA_MEMFREE(extracted);
183         }
184         return TRUE;
185 }
186
187 static void __save_indicator_state(myaccount_appdata *ugd)
188 {
189         /* Save old view's indicator values */
190         ugd->indi_mode = elm_win_indicator_mode_get(ugd->win_main);
191         ugd->indi_o_mode = elm_win_indicator_opacity_get(ugd->win_main);
192         /* Save old view's overlap mode */
193         ugd->overlap_mode = (int)evas_object_data_get(ugd->conformant, "overlap");
194         MYACCOUNT_DBUG("indi_o_mode: %d, indi_mode: %d", ugd->indi_o_mode,      ugd->indi_mode);
195         MYACCOUNT_DBUG("overlap_mode: %d", ugd->overlap_mode);
196 }
197
198 void *myaccount_account_list_create(ui_gadget_h ug,
199                                                                         enum ug_mode mode,
200                                                                         app_control_h data, void *priv)
201 {
202          Evas_Object *parent;
203          myaccount_appdata *ugd;
204          int count = -1;
205          int error_code = -1;
206
207          if (!ug || !priv) {
208                  return NULL;
209          }
210
211
212          bindtextdomain("setting-myaccount-efl", "/usr/apps/setting-myaccount-efl/res/locale");
213          ugd = (myaccount_appdata*)priv;
214          ugd->ug = ug;
215          ugd->eMode = 0;
216          parent = (Evas_Object *)ug_get_window();
217          if (!parent) {
218                  return NULL;
219          }
220          g_appdata = ugd;
221
222         ugd->win_main = parent;
223         Evas_Object *conformant = (Evas_Object *)ug_get_conformant();
224         __save_indicator_state(ugd);
225
226         Evas_Object *indi_bg = elm_bg_add(conformant);
227         elm_object_style_set(indi_bg, "indicator/headerbg");
228         elm_object_part_content_set(conformant, "elm.swallow.indicator_bg", indi_bg);
229         evas_object_show(indi_bg);
230         ugd->conformant = conformant;
231         __reset_indicator(ugd);
232
233          /*sbscribe to vconf noti key which will recevie notification about account update, delete and insert*/
234 #ifdef ENABLE_NOTI
235          myaccount_subscribe_to_account_notification_vconfkey();
236 #endif
237         /*listen to pkgmgr for uninstall of pkg*/
238         myaccount_common_listen_pkgmgr_for_pkg_uninstall();
239
240          __myaccount_account_list_parse_param(&ugd->eMode, data, ugd);
241
242         if( ugd->capability_filter
243                 && strlen(ugd->capability_filter) > 0 ) {
244
245                 if(!strcmp(ugd->capability_filter, "http://tizen.org/account/capability/tizen_email")){
246                         int cnt1 = 0;
247                         int cnt2 = 0;
248                         int cnt3 = 0;
249                         error_code = myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID, &cnt1);
250                         error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt2);
251                         error_code = myaccount_common_get_account_cnt_by_appid(GOOGLE_ACCOUNT_APPID, &cnt3);
252                         count = cnt1+cnt2+cnt3;
253                 } else {
254                         error_code = myaccount_common_get_account_cnt_by_capability(ugd->capability_filter, &count);
255                 }
256
257                 if (error_code != ACCOUNT_ERROR_NONE) {
258                         MYACCOUNT_SLOGE("myaccount_account_list_create: myaccount_common_get_all_account_cnt fail(%d) \n",
259                                                                 error_code);
260                 }
261
262         } else {
263                 error_code = myaccount_common_get_all_account_cnt(&count);
264                 if (error_code != ACCOUNT_ERROR_NONE) {
265                         MYACCOUNT_SLOGE("myaccount_account_list_create: myaccount_common_get_all_account_cnt fail(%d) \n",
266                                                                 error_code);
267                 }
268         }
269
270         char* language = NULL;
271         language = vconf_get_str(VCONFKEY_LANGSET);
272
273         if(language) {
274                 char tmp_buf[6]={0,};
275
276                 MA_STRNCPY(tmp_buf, language, 6);
277
278                 ugd->current_language = strdup(tmp_buf);
279         }
280
281         myaccount_list_view_create(ugd);
282
283         __init_changeable_resource(ugd);
284
285         myaccount_list_navi_create(ugd);
286
287         if (ugd->eMode == eMYACCOUNT_ACCOUNT_LIST) {
288                 if (count > 0) {
289                         myaccount_ug_account_list_create((void*)priv);
290                 } else if (count == 0) {
291                         myaccount_addaccount_create_view(ugd);
292                 } else {
293                         MYACCOUNT_ERROR("add list count(%d)", count);
294                 }
295         } else if (ugd->eMode == eMYACCOUNT_ADD_ACCOUNT) {
296                 myaccount_addaccount_create_view(ugd);
297         }
298
299          return ugd->base;
300  }
301
302 bool myaccount_subscribe_callback(const char* event_type, int account_id, void* user_data)
303 {
304         myaccount_common_handle_notification(event_type);
305
306         return TRUE;
307 }
308
309 int myaccount_subscribe_to_account_notification_vconfkey()
310 {
311         myaccount_appdata* ad = (myaccount_appdata*)myaccount_get_appdata();
312         int ret = -1;
313
314         if(!ad) return 0;
315
316         ret = account_subscribe_create(&ad->account_subscribe);
317         if(ret != ACCOUNT_ERROR_NONE){
318                 MYACCOUNT_ERROR("account_subscribe_create fail");
319                 return 0;
320         }
321         ret = account_subscribe_notification_ex(ad->account_subscribe, myaccount_subscribe_callback, NULL);
322         if(ret != ACCOUNT_ERROR_NONE){
323                 MYACCOUNT_ERROR("account_subscribe_notification_ex fail");
324                 return 0;
325         }
326
327         return 1;
328 }
329
330  static void myaccount_account_list_start(ui_gadget_h ug, app_control_h data, void *priv)
331  {
332          //MYACCOUNT_DBUG("ug start\n");
333  }
334
335  static void myaccount_account_list_pause(ui_gadget_h ug, app_control_h data, void *priv)
336  {
337         __attribute__((__unused__)) myaccount_appdata *ugd;
338          ugd = (myaccount_appdata*)priv;
339
340          MYACCOUNT_DBUG("My account ug goes to pause state\n");
341  }
342
343  static void myaccount_account_list_resume(ui_gadget_h ug, app_control_h data, void *priv)
344  {
345          myaccount_appdata *ugd;
346          ugd = (myaccount_appdata*)priv;
347          MYACCOUNT_DBUG("Myaccount ug resume\n");
348         myaccount_common_set_item_selected_state(false);
349 #ifndef ENABLE_NOTI
350          myaccount_common_handle_notification(NULL);
351 #endif
352
353         char* language = NULL;
354         language = vconf_get_str(VCONFKEY_LANGSET);
355
356         if(language && (strcmp(language, ugd->current_language) == 0)) {
357                 lang_changed(priv);
358         } else {
359                 /* refresh item to update font size */
360                 myaccount_list_refresh_item_list(ugd);
361                 if( ugd->add_genlist ) {
362                         myaccount_addaccount_refresh_item_list(ugd);
363                 }
364         }
365 }
366
367  static void __reset_indicator(myaccount_appdata *ugd)
368  {
369         if (!ugd) {
370                 MYACCOUNT_SLOGE("__reset_indicator ### ugd=%p\n",ugd);
371                 return;
372         }
373         MYACCOUNT_DBUG("indi_o_mode: %d, indi_mode: %d", ugd->indi_o_mode,      ugd->indi_mode);
374         MYACCOUNT_DBUG("overlap_mode: %d", ugd->overlap_mode);
375          /* Set old view's indicator */
376          elm_win_indicator_mode_set(ugd->win_main, ugd->indi_mode);
377          elm_win_indicator_opacity_set(ugd->win_main, ugd->indi_o_mode);
378          /* set old view's conformant overlap mode
379                  if layout is different with new view and needs starts from (0,60) */
380          if (!ugd->overlap_mode) {
381                  elm_object_signal_emit(ugd->conformant,
382                                                 "elm,state,indicator,nooverlap", "");
383                  evas_object_data_set(ugd->conformant, "overlap", NULL);
384          } else {
385                  elm_object_signal_emit(ugd->conformant, "elm,state,indicator,overlap", "");
386                  evas_object_data_set(ugd->conformant, "overlap", (void *)EINA_TRUE);
387          }
388          MYACCOUNT_DBUG("__reset_indicator\n");
389  }
390
391 void myaccount_destroy_data()
392 {
393         if (!g_appdata) {
394                 MYACCOUNT_SLOGE("myaccount_account_ug_destroy ###  g_appdata=%p\n", g_appdata);
395                 return;
396         }
397
398         myaccount_appdata *ugd = g_appdata;
399         account_unsubscribe_notification_ex(ugd->account_subscribe);
400         ugd->account_subscribe = NULL;
401
402         __free_changeable_resource(ugd);
403         myaccount_destroy_appdata();
404
405         if (g_handle_noti > 0) {
406                 g_source_remove(g_handle_noti);
407                 g_handle_noti = 0;
408         }
409
410         MYACCOUNT_DBUG("Terminate Myaccount_ug_main[%s]", __func__);
411         g_appdata = NULL;
412 }
413
414 static void myaccount_account_list_destroy(ui_gadget_h ug, app_control_h data, void *priv)
415 {
416         myaccount_destroy_data();
417 }
418
419  static void myaccount_account_list_message(ui_gadget_h ug,
420                                                                                                         app_control_h msg,
421                                                                                                         app_control_h data,
422                                                                                                         void *priv)
423  {
424
425  }
426
427  static void myaccount_account_list_event(ui_gadget_h ug,
428                                                                                                         enum ug_event event,
429                                                                                                         app_control_h data,
430                                                                                                         void *priv)
431  {
432         switch (event) {
433         case UG_EVENT_LOW_MEMORY:
434 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LOW_MEMORY \n");
435                         break;
436         case UG_EVENT_LOW_BATTERY:
437 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LOW_BATTERY \n");
438                         break;
439         case UG_EVENT_LANG_CHANGE: /* old : SG_BINDTEXTDOMAIN_NOTIFY*/
440                         lang_changed(priv);
441                         MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LANG_CHANGE \n");
442                         break;
443         case UG_EVENT_ROTATE_PORTRAIT:
444 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_PORTRAIT \n");
445                         break;
446         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
447 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN \n");
448                         break;
449         case UG_EVENT_ROTATE_LANDSCAPE:
450 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_LANDSCAPE \n");
451                         break;
452         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
453 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN \n");
454                         break;
455         default:
456 //                      MYACCOUNT_DBUG("myaccount_account_list_event: default !!! \n");
457                         break;
458          }
459  }
460
461 static void myaccount_on_key_event(ui_gadget_h ug,
462                                                                                                         enum ug_key_event event ,
463                                                                                                         app_control_h data,
464                                                                                                         void * priv)
465 {
466         if(!ug) {
467                 MYACCOUNT_DBUG("myaccount_on_key_event ug is NULL !!!");
468                 return;
469         }
470         switch (event) {
471                 case UG_KEY_EVENT_END: {
472                                 ui_gadget_h ug = ((myaccount_appdata *)priv)->ug;
473                                 ug_destroy_me(ug);
474                         }
475                         break;
476                 default:
477                         break;
478         }
479 }
480
481 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
482 {
483         myaccount_appdata *ugd;
484         if (!ops) {
485                 MYACCOUNT_DBUG("UG_MODULE_INIT ops is NULL !!!");
486                 return -1;
487         }
488         ugd = (myaccount_appdata*)myaccount_init_appdata();
489         if (!ugd) {
490                 MYACCOUNT_DBUG("myaccount_init_appdata failed!\n");
491                 return -1;
492         }
493         ops->create = myaccount_account_list_create;
494         ops->start = myaccount_account_list_start;
495         ops->pause = myaccount_account_list_pause;
496         ops->resume = myaccount_account_list_resume;
497         ops->destroy = myaccount_account_list_destroy;
498         ops->message = myaccount_account_list_message;
499         ops->event = myaccount_account_list_event;
500         ops->key_event= myaccount_on_key_event;/*to handle BACK(END)key Event recently added in UG*/
501         ops->priv = ugd;
502         ops->opt = UG_OPT_INDICATOR_ENABLE;/*UG_OPT_INDICATOR_ENABLE;*/
503
504         return 0;
505 }
506
507 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
508 {
509         MYACCOUNT_DBUG("myaccount UG_MODULE_EXIT!\n");
510 }
511
512 typedef enum {
513         MYACCOUNT_RESET_FAILED = -1,
514         MYACCOUNT_UNDEFINED_LIB_SYMBOL = -2,
515         MYACCOUNT_OPERATE_LIB_SUCCESS = 0
516 }MyAccountResetErrorCode_e;
517
518 static MyAccountResetErrorCode_e
519 __myaccount_account_exchange_reset(app_control_h data, void *priv)
520 {
521         int (*reset) (app_control_h pair, void *ptr);
522         MyAccountResetErrorCode_e ret = MYACCOUNT_OPERATE_LIB_SUCCESS;
523
524         void *handle = dlopen("/usr/ug/lib/libug-activesync-efl.so", RTLD_LAZY);
525         if (!handle) {
526                 return MYACCOUNT_RESET_FAILED;
527         }
528
529         reset = dlsym(handle, "setting_plugin_reset");
530 //      if (dlerror() != NULL){
531         if (reset == NULL) {
532                 MYACCOUNT_ERROR("dlsym return value is NULL : dlsym() error or setting_plugin_reset symbol is NULL");
533                 dlclose(handle);
534
535                 return MYACCOUNT_UNDEFINED_LIB_SYMBOL;
536         }
537         if (reset)
538                 ret = (*reset) (data, priv);
539
540         if (ret < 0) ret = MYACCOUNT_RESET_FAILED;
541
542         dlclose(handle);
543         return ret;
544 }
545
546 UG_MODULE_API int setting_plugin_reset(app_control_h data, void *priv)
547 {
548         /*reset exchange account information first*/
549 #ifdef TIZEN_ENGINEER_MODE
550         int error_code = -1;
551 #endif
552         MyAccountResetErrorCode_e ret = MYACCOUNT_OPERATE_LIB_SUCCESS;
553
554         ret = __myaccount_account_exchange_reset(data, priv);
555         if(ret != MYACCOUNT_OPERATE_LIB_SUCCESS) {
556 #ifdef TIZEN_ENGINEER_MODE
557                 MYACCOUNT_SLOGE("__myaccount_account_exchange_reset returns %d\n",
558                                                                 error_code);
559 #endif
560                 return (int)ret;
561         }
562         return (int)MYACCOUNT_OPERATE_LIB_SUCCESS;
563 }