tizen 2.4 release
[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 lang_changed(void * priv)
40 {
41         if (!priv) {
42                 MYACCOUNT_SLOGE("lang_changed ### priv=%p\n", priv);
43                 return ;
44         }
45
46         myaccount_appdata *ugd;
47         ugd = (myaccount_appdata*)priv;
48
49         char* language = NULL;
50         language = vconf_get_str(VCONFKEY_LANGSET);
51
52         if(language) {
53                 char tmp_buf[6]={0,};
54                 MA_STRNCPY(tmp_buf, language, 6);
55                 ugd->current_language = strdup(tmp_buf);
56         }
57         MA_MEMFREE(language);
58
59         elm_object_item_text_set (ugd->nf_it, dgettext(MA_UG_NAME, "IDS_MA_HEADER_ACCOUNTS"));
60         myaccount_list_refresh_item_list(ugd);
61
62         if( ugd->add_genlist ) {
63                 // title text update
64                 elm_object_item_text_set (ugd->add_nf_it, dgettext(MA_UG_NAME, "IDS_MA_HEADER_ADD_ACCOUNT"));
65                 myaccount_addaccount_refresh_item_list(ugd);
66         } else {
67                 MYACCOUNT_SLOGE("lang_changed ### ugd->add_genlist is NULL\n");
68         }
69 }
70
71
72 static gboolean __myaccount_account_list_parse_param(myaccount_mode_e
73                                                                                                                 *mode,
74                                                                                                                 app_control_h data,
75                                                                                                                 myaccount_appdata *ad)
76 {
77         if (!data || !ad) {
78                 return FALSE;
79         } else {
80                 char *extracted = NULL;
81                 ad->capability_filter = NULL;
82                 app_control_get_extra_data(data, "mode", &extracted);
83                 app_control_get_extra_data(data, "capability_filter", &ad->capability_filter);
84                 app_control_get_extra_data(data, "myaccount_userdata", &ad->caller_userdata);
85                 app_control_get_extra_data(data, "from", &ad->called_from);
86                 if (extracted) {
87                         MYACCOUNT_SLOGD("view_account_list = %s\n", extracted);
88
89                         if (!strcmp(extracted, "account_list"))
90                                 *mode = eMYACCOUNT_ACCOUNT_LIST;
91                         else if (!strcmp(extracted, "add_account"))
92                                 *mode = eMYACCOUNT_ADD_ACCOUNT;
93                         else
94                                 *mode = eMYACCOUNT_ACCOUNT_LIST;
95                 } else {
96                         *mode = eMYACCOUNT_ACCOUNT_LIST;
97                 }
98
99                 MA_MEMFREE(extracted);
100         }
101         return TRUE;
102 }
103
104 static void __save_indicator_state(myaccount_appdata *ugd)
105 {
106         /* Save old view's indicator values */
107         ugd->indi_mode = elm_win_indicator_mode_get(ugd->win_main);
108         ugd->indi_o_mode = elm_win_indicator_opacity_get(ugd->win_main);
109         /* Save old view's overlap mode */
110         ugd->overlap_mode = (int)evas_object_data_get(ugd->conformant, "overlap");
111         MYACCOUNT_DBUG("indi_o_mode: %d, indi_mode: %d", ugd->indi_o_mode,      ugd->indi_mode);
112         MYACCOUNT_DBUG("overlap_mode: %d", ugd->overlap_mode);
113 }
114
115 void *myaccount_account_list_create(ui_gadget_h ug,
116                                                                         enum ug_mode mode,
117                                                                         app_control_h data, void *priv)
118 {
119          Evas_Object *parent;
120          myaccount_appdata *ugd;
121          int count = -1;
122          int error_code = -1;
123
124          if (!ug || !priv) {
125                  return NULL;
126          }
127
128
129          bindtextdomain("setting-myaccount-efl", "/usr/apps/setting-myaccount-efl/res/locale");
130          ugd = (myaccount_appdata*)priv;
131          ugd->ug = ug;
132          ugd->eMode = 0;
133          parent = (Evas_Object *)ug_get_window();
134          if (!parent) {
135                  return NULL;
136          }
137          g_appdata = ugd;
138
139         ugd->win_main = parent;
140         Evas_Object *conformant = (Evas_Object *)ug_get_conformant();
141         __save_indicator_state(ugd);
142
143         Evas_Object *indi_bg = elm_bg_add(conformant);
144         elm_object_style_set(indi_bg, "indicator/headerbg");
145         elm_object_part_content_set(conformant, "elm.swallow.indicator_bg", indi_bg);
146         evas_object_show(indi_bg);
147         ugd->conformant = conformant;
148         __reset_indicator(ugd);
149
150          /*sbscribe to vconf noti key which will recevie notification about account update, delete and insert*/
151 #ifdef ENABLE_NOTI
152          myaccount_subscribe_to_account_notification_vconfkey();
153 #endif
154         /*listen to pkgmgr for uninstall of pkg*/
155         myaccount_common_listen_pkgmgr_for_pkg_uninstall();
156
157          __myaccount_account_list_parse_param(&ugd->eMode, data, ugd);
158
159         if( ugd->capability_filter
160                 && strlen(ugd->capability_filter) > 0 ) {
161
162                 if(!strcmp(ugd->capability_filter, "http://tizen.org/account/capability/tizen_email")){
163                         int cnt1 = 0;
164                         int cnt2 = 0;
165                         int cnt3 = 0;
166                         error_code = myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID, &cnt1);
167                         error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt2);
168                         error_code = myaccount_common_get_account_cnt_by_appid(GOOGLE_ACCOUNT_APPID, &cnt3);
169                         count = cnt1+cnt2+cnt3;
170                 } else {
171                         error_code = myaccount_common_get_account_cnt_by_capability(ugd->capability_filter, &count);
172                 }
173
174                 if (error_code != ACCOUNT_ERROR_NONE) {
175                         MYACCOUNT_SLOGE("myaccount_account_list_create: myaccount_common_get_all_account_cnt fail(%d) \n",
176                                                                 error_code);
177                 }
178
179         } else {
180                 error_code = myaccount_common_get_all_account_cnt(&count, false);
181                 if (error_code != ACCOUNT_ERROR_NONE) {
182                         MYACCOUNT_SLOGE("myaccount_account_list_create: myaccount_common_get_all_account_cnt fail(%d) \n",
183                                                                 error_code);
184                 }
185         }
186
187         char* language = NULL;
188         language = vconf_get_str(VCONFKEY_LANGSET);
189
190         if(language) {
191                 char tmp_buf[6]={0,};
192
193                 MA_STRNCPY(tmp_buf, language, 6);
194
195                 ugd->current_language = strdup(tmp_buf);
196         }
197         MA_MEMFREE(language);
198
199         myaccount_list_view_create(ugd);
200
201         myaccount_list_navi_create(ugd);
202
203         if (ugd->eMode == eMYACCOUNT_ACCOUNT_LIST) {
204                 if (count > 0) {
205                         myaccount_ug_account_list_create((void*)priv);
206                 } else if (count == 0) {
207                         myaccount_addaccount_create_view(ugd);
208                 } else {
209                         MYACCOUNT_ERROR("add list count(%d)", count);
210                 }
211         } else if (ugd->eMode == eMYACCOUNT_ADD_ACCOUNT) {
212                 myaccount_addaccount_create_view(ugd);
213         }
214
215          return ugd->base;
216  }
217
218 bool myaccount_subscribe_callback(const char* event_type, int account_id, void* user_data)
219 {
220         myaccount_common_handle_notification(event_type);
221
222         return TRUE;
223 }
224
225 int myaccount_subscribe_to_account_notification_vconfkey()
226 {
227         myaccount_appdata* ad = (myaccount_appdata*)myaccount_get_appdata();
228         int ret = -1;
229
230         if(!ad) return 0;
231
232         ret = account_subscribe_create(&ad->account_subscribe);
233         if(ret != ACCOUNT_ERROR_NONE){
234                 MYACCOUNT_ERROR("account_subscribe_create fail");
235                 return 0;
236         }
237         ret = account_subscribe_notification_ex(ad->account_subscribe, myaccount_subscribe_callback, NULL);
238         if(ret != ACCOUNT_ERROR_NONE){
239                 MYACCOUNT_ERROR("account_subscribe_notification_ex fail");
240                 return 0;
241         }
242
243         return 1;
244 }
245
246  static void myaccount_account_list_start(ui_gadget_h ug, app_control_h data, void *priv)
247  {
248          //MYACCOUNT_DBUG("ug start\n");
249  }
250
251  static void myaccount_account_list_pause(ui_gadget_h ug, app_control_h data, void *priv)
252  {
253         __attribute__((__unused__)) myaccount_appdata *ugd;
254          ugd = (myaccount_appdata*)priv;
255
256          MYACCOUNT_DBUG("My account ug goes to pause state\n");
257  }
258
259  static void myaccount_account_list_resume(ui_gadget_h ug, app_control_h data, void *priv)
260  {
261          myaccount_appdata *ugd;
262          ugd = (myaccount_appdata*)priv;
263          MYACCOUNT_DBUG("Myaccount ug resume\n");
264         myaccount_common_set_item_selected_state(false);
265 #ifndef ENABLE_NOTI
266          myaccount_common_handle_notification(NULL);
267 #endif
268
269         char* language = NULL;
270         language = vconf_get_str(VCONFKEY_LANGSET);
271
272         if(language && (strcmp(language, ugd->current_language) == 0)) {
273                 lang_changed(priv);
274         } else {
275                 /* refresh item to update font size */
276                 myaccount_list_refresh_item_list(ugd);
277                 if( ugd->add_genlist ) {
278                         myaccount_addaccount_refresh_item_list(ugd);
279                 }
280         }
281         MA_MEMFREE(language);
282 }
283
284  static void __reset_indicator(myaccount_appdata *ugd)
285  {
286         if (!ugd) {
287                 MYACCOUNT_SLOGE("__reset_indicator ### ugd=%p\n",ugd);
288                 return;
289         }
290         MYACCOUNT_DBUG("indi_o_mode: %d, indi_mode: %d", ugd->indi_o_mode,      ugd->indi_mode);
291         MYACCOUNT_DBUG("overlap_mode: %d", ugd->overlap_mode);
292          /* Set old view's indicator */
293          elm_win_indicator_mode_set(ugd->win_main, ugd->indi_mode);
294          elm_win_indicator_opacity_set(ugd->win_main, ugd->indi_o_mode);
295          /* set old view's conformant overlap mode
296                  if layout is different with new view and needs starts from (0,60) */
297          if (!ugd->overlap_mode) {
298                  elm_object_signal_emit(ugd->conformant,
299                                                 "elm,state,indicator,nooverlap", "");
300                  evas_object_data_set(ugd->conformant, "overlap", NULL);
301          } else {
302                  elm_object_signal_emit(ugd->conformant, "elm,state,indicator,overlap", "");
303                  evas_object_data_set(ugd->conformant, "overlap", (void *)EINA_TRUE);
304          }
305          MYACCOUNT_DBUG("__reset_indicator\n");
306  }
307
308 void myaccount_destroy_data()
309 {
310         if (!g_appdata) {
311                 MYACCOUNT_SLOGE("myaccount_account_ug_destroy ###  g_appdata=%p\n", g_appdata);
312                 return;
313         }
314
315         myaccount_appdata *ugd = g_appdata;
316         account_unsubscribe_notification_ex(ugd->account_subscribe);
317         ugd->account_subscribe = NULL;
318
319         myaccount_destroy_appdata();
320
321         if (g_handle_noti > 0) {
322                 g_source_remove(g_handle_noti);
323                 g_handle_noti = 0;
324         }
325
326         MYACCOUNT_DBUG("Terminate Myaccount_ug_main[%s]", __func__);
327         g_appdata = NULL;
328 }
329
330 static void myaccount_account_list_destroy(ui_gadget_h ug, app_control_h data, void *priv)
331 {
332         myaccount_destroy_data();
333 }
334
335  static void myaccount_account_list_message(ui_gadget_h ug,
336                                                                                                         app_control_h msg,
337                                                                                                         app_control_h data,
338                                                                                                         void *priv)
339  {
340
341  }
342
343  static void myaccount_account_list_event(ui_gadget_h ug,
344                                                                                                         enum ug_event event,
345                                                                                                         app_control_h data,
346                                                                                                         void *priv)
347  {
348         switch (event) {
349         case UG_EVENT_LOW_MEMORY:
350 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LOW_MEMORY \n");
351                         break;
352         case UG_EVENT_LOW_BATTERY:
353 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LOW_BATTERY \n");
354                         break;
355         case UG_EVENT_LANG_CHANGE: /* old : SG_BINDTEXTDOMAIN_NOTIFY*/
356                         lang_changed(priv);
357                         MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_LANG_CHANGE \n");
358                         break;
359         case UG_EVENT_ROTATE_PORTRAIT:
360 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_PORTRAIT \n");
361                         break;
362         case UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN:
363 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN \n");
364                         break;
365         case UG_EVENT_ROTATE_LANDSCAPE:
366 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_LANDSCAPE \n");
367                         break;
368         case UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN:
369 //                      MYACCOUNT_DBUG("myaccount_account_list_event: UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN \n");
370                         break;
371         default:
372 //                      MYACCOUNT_DBUG("myaccount_account_list_event: default !!! \n");
373                         break;
374          }
375  }
376
377 static void myaccount_on_key_event(ui_gadget_h ug,
378                                                                                                         enum ug_key_event event ,
379                                                                                                         app_control_h data,
380                                                                                                         void * priv)
381 {
382         if(!ug) {
383                 MYACCOUNT_DBUG("myaccount_on_key_event ug is NULL !!!");
384                 return;
385         }
386         switch (event) {
387                 case UG_KEY_EVENT_END: {
388                                 ui_gadget_h ug = ((myaccount_appdata *)priv)->ug;
389                                 ug_destroy_me(ug);
390                         }
391                         break;
392                 default:
393                         break;
394         }
395 }
396
397 UG_MODULE_API int UG_MODULE_INIT(struct ug_module_ops *ops)
398 {
399         myaccount_appdata *ugd;
400         if (!ops) {
401                 MYACCOUNT_DBUG("UG_MODULE_INIT ops is NULL !!!");
402                 return -1;
403         }
404         ugd = (myaccount_appdata*)myaccount_init_appdata();
405         if (!ugd) {
406                 MYACCOUNT_DBUG("myaccount_init_appdata failed!\n");
407                 return -1;
408         }
409         ops->create = myaccount_account_list_create;
410         ops->start = myaccount_account_list_start;
411         ops->pause = myaccount_account_list_pause;
412         ops->resume = myaccount_account_list_resume;
413         ops->destroy = myaccount_account_list_destroy;
414         ops->message = myaccount_account_list_message;
415         ops->event = myaccount_account_list_event;
416         ops->key_event= myaccount_on_key_event;/*to handle BACK(END)key Event recently added in UG*/
417         ops->priv = ugd;
418         ops->opt = UG_OPT_INDICATOR_ENABLE;/*UG_OPT_INDICATOR_ENABLE;*/
419
420         return 0;
421 }
422
423 UG_MODULE_API void UG_MODULE_EXIT(struct ug_module_ops *ops)
424 {
425         MYACCOUNT_DBUG("myaccount UG_MODULE_EXIT!\n");
426 }
427
428 typedef enum {
429         MYACCOUNT_RESET_FAILED = -1,
430         MYACCOUNT_UNDEFINED_LIB_SYMBOL = -2,
431         MYACCOUNT_OPERATE_LIB_SUCCESS = 0
432 }MyAccountResetErrorCode_e;
433
434 static MyAccountResetErrorCode_e
435 __myaccount_account_exchange_reset(app_control_h data, void *priv)
436 {
437         int (*reset) (app_control_h pair, void *ptr);
438         MyAccountResetErrorCode_e ret = MYACCOUNT_OPERATE_LIB_SUCCESS;
439
440         void *handle = dlopen("/usr/ug/lib/libug-activesync-efl.so", RTLD_LAZY);
441         if (!handle) {
442                 return MYACCOUNT_RESET_FAILED;
443         }
444
445         reset = dlsym(handle, "setting_plugin_reset");
446 //      if (dlerror() != NULL){
447         if (reset == NULL) {
448                 MYACCOUNT_ERROR("dlsym return value is NULL : dlsym() error or setting_plugin_reset symbol is NULL");
449                 dlclose(handle);
450                 return MYACCOUNT_UNDEFINED_LIB_SYMBOL;
451         }
452         if (reset)
453                 ret = (*reset) (data, priv);
454
455         if (ret < 0) ret = MYACCOUNT_RESET_FAILED;
456
457         dlclose(handle);
458         return ret;
459 }
460
461 UG_MODULE_API int setting_plugin_reset(app_control_h data, void *priv)
462 {
463         /*reset exchange account information first*/
464 #ifdef TIZEN_ENGINEER_MODE
465         int error_code = -1;
466 #endif
467         MyAccountResetErrorCode_e ret = MYACCOUNT_OPERATE_LIB_SUCCESS;
468
469         ret = __myaccount_account_exchange_reset(data, priv);
470         if(ret != MYACCOUNT_OPERATE_LIB_SUCCESS) {
471 #ifdef TIZEN_ENGINEER_MODE
472                 MYACCOUNT_SLOGE("__myaccount_account_exchange_reset returns %d\n",
473                                                                 error_code);
474 #endif
475                 return (int)ret;
476         }
477         return (int)MYACCOUNT_OPERATE_LIB_SUCCESS;
478 }