b056b611923749ea17fc0f66a5cb0ac2d93a8f79
[apps/home/my-account.git] / src / myaccount_ug_addaccount.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 #ifndef UG_MODULE_API
23 #define UG_MODULE_API __attribute__ ((visibility("default")))
24 #endif
25 #include <Ecore_X.h>
26 #include "myaccount_ug_common.h"
27 #include "myaccount_ug_addaccount.h"
28 #include "myaccount_ug_account_list.h"
29 #include <app.h>
30 #include <app_manager.h>
31 #include <system_settings.h>
32 #include <iniparser.h>
33
34 #define SORT_PRIOR_1 "1_"
35 #define SORT_PRIOR_2 "2_"
36 #define SORT_PRIOR_3 "3_"
37
38 static Elm_Genlist_Item_Class addacc_list_itc;
39
40 static bool _myaccount_addaccount_get_account_type_info_cb(account_type_h account_type, void *user_data);
41
42 static char *__myaccount_addaccount_gl_label_get(void *data,
43                                                                                         Evas_Object *obj, const char *part)
44 {
45         addaccount_list_priv *service = (addaccount_list_priv*)data;
46         char buf[2048]={0,};
47
48         if (!strcmp(part, "elm.text.main.left")) {
49                 int value = -1;
50                 int rc = -1;
51                 char *tmp_sp_name = NULL;
52
53                 rc = system_settings_get_value_int(SYSTEM_SETTINGS_KEY_FONT_SIZE, &value);
54                 if ( rc != SYSTEM_SETTINGS_ERROR_NONE ) {
55                         MYACCOUNT_DBUG("system_settings_get_value_int fail, rc=%d, value=%d", rc, value);
56                 }
57
58                 tmp_sp_name = elm_entry_utf8_to_markup(service->service_name);
59
60                 if(!strcmp(service->service_name, dgettext(MA_UG_NAME, "IDS_EMAIL_BODY_MICROSOFT_EXCHANGE_ACTIVESYNC"))
61                         && value == SYSTEM_SETTINGS_FONT_SIZE_NORMAL) {
62
63                         if(tmp_sp_name){
64                                 MA_SNPRINTF(buf, sizeof(buf), "<font_size=%d>%s</font_size>", 37, tmp_sp_name);
65                                 MA_MEMFREE(tmp_sp_name);
66                         }
67                         return strdup(buf);
68                 } else {
69                         return tmp_sp_name;
70                 }
71         }
72
73         return NULL;
74 }
75
76 bool myaccount_add_account_by_package_name_cb(account_h account,
77                                                                                                         void *user_data)
78 {
79         return false;
80 }
81
82 static void __myaccount_addaccount_gl_sel(void *data,
83                                                                 Evas_Object *obj, void *event_info)
84 {
85         myaccount_appdata *ad = myaccount_get_appdata();
86         addaccount_list_priv *service = (addaccount_list_priv*)data;
87         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
88         char email_sp[64] = {0,};
89
90         if(!ad) {
91                 MYACCOUNT_ERROR("__myaccount_account_list_gl_sel appdata is NULL\n");
92                 return;
93         }
94
95         MYACCOUNT_DBUG("ad->item_selected_state=%d\n",  myaccount_common_get_item_selected_state());
96
97         if( myaccount_common_get_item_selected_state() ) {
98                 elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
99                 return;
100         }
101
102         myaccount_common_set_item_selected_state(true);
103
104         if (item != NULL) {
105                 elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
106
107                 switch(service->sp_type) {
108                         case MYACCOUNT_TYPE_PICASA :
109                         case MYACCOUNT_TYPE_YOUTUBE :
110 //                      case MYACCOUNT_TYPE_FACEBOOK :
111                         case MYACCOUNT_TYPE_EXCHANGE :
112                                 myaccount_common_launch_application(
113                                                 MYACCOUNT_REQUEST_SIGNIN,
114                                                 strdup(service->package_name),
115                                                 NULL,
116                                                 email_sp,
117                                                 -1,
118                                                 service->sp_type,
119                                                 ad);
120                                 break;
121                         case MYACCOUNT_TYPE_GOOGLE :
122                                 MA_SNPRINTF(email_sp, sizeof(email_sp), "%s", "gmail");
123                                 myaccount_common_launch_application(
124                                                 MYACCOUNT_REQUEST_SIGNIN,
125                                                 strdup(service->package_name),
126                                                 NULL,
127                                                 email_sp,
128                                                 -1,
129                                                 service->sp_type,
130                                                 ad);
131                                 break;
132 //                      case MYACCOUNT_TYPE_MSN :
133 //                              MA_SNPRINTF(email_sp, sizeof(email_sp), "%s", "msn");
134 //                              myaccount_common_launch_application(
135 //                                              MYACCOUNT_REQUEST_SIGNIN,
136 //                                              strdup(service->package_name),
137 //                                              NULL,
138 //                                              email_sp,
139 //                                              service->sp_type,
140 //                                              -1,
141 //                                              ad);
142 //                              break;
143                         case MYACCOUNT_TYPE_OTHER:
144                                 MA_SNPRINTF(email_sp, sizeof(email_sp), "%s", "others");
145                                 myaccount_common_launch_application(
146                                                 MYACCOUNT_REQUEST_SIGNIN,
147                                                 strdup(service->package_name),
148                                                 NULL,
149                                                 email_sp,
150                                                 service->sp_type,
151                                                 -1,
152                                                 ad);
153                                 break;
154                         case MYACCOUNT_TYPE_CSC:
155                                 MA_SNPRINTF(email_sp, sizeof(email_sp), "%s", service->service_name);
156                                 myaccount_common_launch_application(
157                                                 MYACCOUNT_REQUEST_SIGNIN,
158                                                 strdup(service->package_name),
159                                                 NULL,
160                                                 email_sp,
161                                                 -1,
162                                                 service->sp_type,
163                                                 ad);
164                                 break;
165                         default:
166                                 if(myaccount_common_launch_application(
167                                                         MYACCOUNT_REQUEST_SIGNIN,
168                                                         strdup(service->package_name),
169                                                         NULL,
170                                                         "",
171                                                         -1,
172                                                         service->sp_type,
173                                                         ad) != APP_CONTROL_ERROR_NONE ) {
174                                         MYACCOUNT_ERROR("__myaccount_addaccount_gl_sel : Failed to launch application\n");
175                                         myaccount_common_set_item_selected_state(false);
176
177                                 }
178                                 break;
179                 }
180         }
181         return;
182 }
183
184
185 static Evas_Object *__myaccount_addaccount_icon_get( void *data,
186                                                                                         Evas_Object *obj, const char *part)
187 {
188         char tempbuf[PATH_MAX];
189         Evas_Object *icon = NULL;
190         addaccount_list_priv *service_item = (addaccount_list_priv*)data;
191         Evas_Object *ic = NULL;
192
193         if (!service_item) {
194                 MYACCOUNT_ERROR(" __myaccount_addaccount_icon_get: Data is NULL\n");
195                 return NULL;
196         }
197         memset(tempbuf, 0, sizeof(char)*PATH_MAX);
198
199         if (!strcmp(part, "elm.icon.1")) {
200                 if (strlen(service_item->icon_path) > 0) {
201                         MA_STRNCPY(tempbuf, service_item->icon_path, sizeof(tempbuf));
202                 } else {
203                         MA_STRNCPY(tempbuf, "A01_2_Icon_default.png", sizeof(tempbuf));
204                 }
205                 ic = elm_layout_add(obj);
206                 elm_layout_theme_set(ic, "layout", "list/B/type.2", "default");
207
208                 icon = elm_image_add(obj);
209                 if (strstr(tempbuf, "/"))
210                         elm_image_file_set(icon, tempbuf, NULL);
211                 else
212                         elm_image_file_set(icon, MA_IMAGE_EDJ_NAME, tempbuf);
213
214                 elm_layout_content_set(ic, "elm.swallow.content", icon);
215         }
216         return ic;
217 }
218
219 void myaccount_addaccount_free_priv_data(myaccount_appdata *appdata)
220 {
221         GList* it = NULL;
222         myaccount_appdata *ad = (myaccount_appdata *)appdata;
223
224         if(!ad) {
225                 MYACCOUNT_ERROR("No appdata!!\n");
226                 return;
227         }
228
229         for(it=ad->sorted_sp_list;it!=NULL;it=g_list_next(it)) {
230                 addaccount_list_priv* tmp = (addaccount_list_priv*)it->data;
231                 MA_MEMFREE(tmp);
232         }
233
234         ad->sorted_sp_list = NULL;
235 }
236
237 static addaccount_list_priv*
238 _myaccount_addaccount_create_priv_item()
239 {
240         addaccount_list_priv* sp_info = NULL;
241         sp_info = (addaccount_list_priv*)calloc(1,sizeof(addaccount_list_priv));
242         if(!sp_info) {
243                 MYACCOUNT_ERROR("memory allocation fail\n");
244                 return NULL;
245         }
246         memset(sp_info, 0x00, sizeof(addaccount_list_priv));
247         return sp_info;
248 }
249
250 static bool _myaccount_addaccount_get_account_type_info_cb(account_type_h account_type, void *user_data)
251 {
252         GList **service_info_list = (GList **)user_data;
253         char* type_buf = NULL;
254         int type_int = -1, ret = -1;
255         addaccount_list_priv* sp_info = NULL;
256
257         if(account_type == NULL) {
258                 MYACCOUNT_ERROR(" account type handle is NULL \n");
259                 return FALSE;
260         }
261
262         sp_info = _myaccount_addaccount_create_priv_item();
263
264         if(sp_info == NULL) {
265                 MYACCOUNT_ERROR(" sp_info is NULL \n");
266                 return FALSE;
267         }
268
269         ret = account_type_get_app_id(account_type, &type_buf);
270         if(ret == ACCOUNT_ERROR_NONE ) {
271                 if(type_buf) {
272                         MA_STRNCPY(sp_info->package_name, type_buf,
273                                                                 sizeof(sp_info->package_name));
274                 } else {
275                         MYACCOUNT_ERROR("No appid available\n");
276                 }
277         } else {
278                 MYACCOUNT_ERROR("account_type_get_app_id return(%x)\n", ret);
279         }
280         MA_MEMFREE(type_buf);
281
282         myaccount_appdata* ad = NULL;
283         ad = myaccount_get_appdata();
284
285         if(ad) {
286                 if(ad->called_from && !strcmp(ad->called_from, "setup_wizard")){
287                         MYACCOUNT_ERROR("FROM SETUP WIZARD\n");
288                         if(!strcmp(sp_info->package_name, SAMSUNG_ACCOUNT_APPID)) {
289                                 MA_MEMFREE(sp_info);
290                                 return TRUE;
291                         } else if(!strcmp(sp_info->package_name, TIZEN_ACCOUNT_APPID)) {
292                                 MA_MEMFREE(sp_info);
293                                 return TRUE;
294                         }
295                 }
296         } else {
297                 MYACCOUNT_ERROR("app data is null\n");
298         }
299
300         if(!strcmp(sp_info->package_name, "email-setting-efl")) {
301                 MA_MEMFREE(sp_info);
302                 /* SLP email */
303                 sp_info = _myaccount_addaccount_create_priv_item();
304                 if(sp_info == NULL) {
305                         MYACCOUNT_ERROR(" sp_info is NULL \n");
306                         return FALSE;
307                 }
308
309                 MA_STRNCPY(sp_info->service_name,
310                                         dgettext(MA_UG_NAME, "IDS_COM_BODY_EMAIL"),
311                                         sizeof(sp_info->service_name));
312                 MA_SNPRINTF(sp_info->service_sname, sizeof(sp_info->service_sname), "%s%s", SORT_PRIOR_3, sp_info->service_name);
313                 MA_STRNCPY(sp_info->package_name,
314                                         "email-setting-efl",
315                                         sizeof(sp_info->package_name));
316                 //myaccount_common_get_icon_by_name("email",
317                 //                      sp_info->icon_path);
318
319                 ret = account_type_get_icon_path(account_type, &type_buf);
320                 if(ret == ACCOUNT_ERROR_NONE ) {
321                         if(type_buf) {
322                                 MA_STRNCPY(sp_info->icon_path, type_buf,
323                                                                         sizeof(sp_info->icon_path));
324                         } else {
325                                 MYACCOUNT_ERROR("No icon path available\n");
326                         }
327                 } else {
328                         MYACCOUNT_ERROR("account_type_get_app_id return(%x)\n", ret);
329                 }
330                 MA_MEMFREE(type_buf);
331
332                 sp_info->sp_type = MYACCOUNT_TYPE_OTHER;
333                 sp_info->b_tizen_email = TRUE;
334                 *service_info_list = g_list_append(*service_info_list, (void*)sp_info);
335                 return TRUE;
336
337         } else if(!strcmp(sp_info->package_name, "activesync-ui")) {
338                 sp_info->b_tizen_email = TRUE;
339         } else if(!strcmp(sp_info->package_name, "com.samsung.rcs-im")
340                                 || !strcmp(sp_info->package_name, "ims-service")) {
341                 MA_MEMFREE(sp_info);
342                 return TRUE;
343         } else if(!strcmp(sp_info->package_name, GOOGLE_ACCOUNT_APPID)) {
344                 sp_info->b_tizen_email = TRUE;
345         }
346
347         ret = account_type_get_icon_path(account_type, &type_buf);
348         if(ret == ACCOUNT_ERROR_NONE ) {
349                 if(type_buf) {
350                         MA_STRNCPY(sp_info->icon_path, type_buf,
351                                                                 sizeof(sp_info->icon_path));
352                 } else {
353                         MYACCOUNT_ERROR("No icon path available\n");
354                 }
355         } else {
356                 MYACCOUNT_ERROR("account_type_get_app_id return(%x)\n", ret);
357         }
358         MA_MEMFREE(type_buf);
359
360         char* provider_name = NULL;
361
362         if(ad && ad->current_language){
363
364                 if(!strcmp(ad->current_language, "en_US")){
365                         ret = account_type_get_label_by_locale(account_type, "en_GB", &provider_name);
366                 }else{
367                         ret = account_type_get_label_by_locale(account_type, ad->current_language, &provider_name);
368                 }
369         }
370
371         if(ret != ACCOUNT_ERROR_NONE){
372                 /* fallback scenario */
373                 ret = account_type_get_label_by_locale(account_type, "default", &provider_name);
374                 if(ret != ACCOUNT_ERROR_NONE){
375                         MYACCOUNT_SLOGE("No service name(%s)\n", sp_info->package_name);
376                         MA_MEMFREE(sp_info);
377                         return TRUE;
378                 }
379         }
380
381         MA_STRNCPY(sp_info->service_name, provider_name,
382                                                 sizeof(sp_info->service_name));
383
384         MA_MEMFREE(provider_name);
385
386         if(!strcmp(sp_info->package_name, SAMSUNG_ACCOUNT_APPID)) {
387                 MA_SNPRINTF(sp_info->service_sname,
388                                                         sizeof(sp_info->service_sname), "%s%s", SORT_PRIOR_1, sp_info->service_name);
389         } else if(!strcmp(sp_info->package_name, TIZEN_ACCOUNT_APPID)){
390                 MA_SNPRINTF(sp_info->service_sname,
391                                                         sizeof(sp_info->service_sname), "%s%s", SORT_PRIOR_1, sp_info->service_name);
392         } else {
393                 MA_SNPRINTF(sp_info->service_sname,
394                                                         sizeof(sp_info->service_sname), "%s%s", SORT_PRIOR_3, sp_info->service_name);
395         }
396
397         account_type_get_multiple_account_support(account_type, &type_int);
398         sp_info->multiple_account_support = type_int;
399
400         if(type_int == FALSE
401                 && account_query_account_by_package_name(myaccount_add_account_by_package_name_cb, sp_info->package_name, NULL) == ACCOUNT_ERROR_NONE) {
402                 MA_MEMFREE(sp_info);
403                 return TRUE;
404         }
405
406         *service_info_list = g_list_append(*service_info_list, (void*)sp_info);
407
408         return TRUE;
409 }
410
411 static void _myaccount_addaccount_get_account_type_info(const char* capability_filter, GList** sp_info_list)
412 {
413         int ret = -1;
414         if(!sp_info_list) {
415                 MYACCOUNT_ERROR("sp_info_list is null\n");
416                 return;
417         }
418
419         ret = account_connect();
420         if(ret != ACCOUNT_ERROR_NONE){
421                 MYACCOUNT_ERROR("account database cannot be connected\n");
422                 return;
423         }
424
425         if(capability_filter) {
426                 if(!strcmp(capability_filter, "http://tizen.org/account/capability/tizen_email")){
427                         account_type_query_by_provider_feature(_myaccount_addaccount_get_account_type_info_cb, "http://tizen.org/account/capability/email", (void*)sp_info_list);
428                 }else {
429                         account_type_query_by_provider_feature(_myaccount_addaccount_get_account_type_info_cb, capability_filter, (void*)sp_info_list);
430                 }
431         }
432         else {
433                 account_type_foreach_account_type_from_db(_myaccount_addaccount_get_account_type_info_cb, (void*)sp_info_list);
434         }
435         ret = account_disconnect();
436         if(ret != ACCOUNT_ERROR_NONE){
437                 MYACCOUNT_ERROR("account database cannot be connected\n");
438                 return;
439         }
440 }
441
442 static int _myaccount_ug_addaccount_compare(gconstpointer a, gconstpointer b)
443 {
444         addaccount_list_priv* sp_info_a = (addaccount_list_priv*)a;
445         addaccount_list_priv* sp_info_b = (addaccount_list_priv*)b;
446
447         return g_ascii_strcasecmp(sp_info_a->service_sname, sp_info_b->service_sname);
448 }
449
450 static int __myaccount_addaccount_populate_service_info(myaccount_appdata *ad)
451 {
452         int pkg_count = 0;
453
454         //_myaccount_addaccount_get_csc_sp_list(ad->capability_filter, &ad->sp_info_list);
455         _myaccount_addaccount_get_account_type_info(ad->capability_filter, &ad->sorted_sp_list);
456
457         ad->sorted_sp_list = g_list_sort(ad->sorted_sp_list, (GCompareFunc)_myaccount_ug_addaccount_compare);
458         pkg_count = g_list_length(ad->sorted_sp_list);
459
460         return pkg_count;
461 }
462
463 static void _myaccount_ug_addaccount_gl_realized(void *data, Evas_Object *obj, void *ei)
464 {
465         Elm_Object_Item *it = NULL;
466         int total_count = 0;
467         int index = 0;
468
469         if(!ei) {
470                 MYACCOUNT_ERROR("Realized event info is NULL!!! \n");
471                 return;
472         }
473
474         it = (Elm_Object_Item *)ei;
475
476         total_count = elm_genlist_items_count(obj);
477         index = elm_genlist_item_index_get(it);
478
479         if(index != 0 && index != total_count) {
480                 /*Add account items. index 1 and last are the seperators*/
481                 /* but you don't need to do something. winset support accessibility for default item */
482         } else {
483                 /*Remove access for seperators*/
484                 elm_object_item_access_unregister(ei);
485         }
486
487         if(total_count <= 3) {
488                 //MYACCOUNT_VERBOSE("DO NOTHING only one add account item, total_count(%d) index(%d) !!! \n", total_count, index);
489         } else {
490                 if (index == 1) {
491                         elm_object_item_signal_emit(ei, "elm,state,top", "");
492                 } else if (index == (total_count-2)) {
493                         elm_object_item_signal_emit(ei, "elm,state,bottom", "");
494                 } else  {
495                         elm_object_item_signal_emit(ei, "elm,state,center", "");
496                 }
497         }
498 }
499
500 static void _myaccount_ug_addaccount_gl_deleted(void *data, Evas_Object *obj, void *ei)
501 {
502         MYACCOUNT_ERROR("# _myaccount_ug_addaccount_gl_deleted\n");
503         myaccount_appdata *ad = myaccount_get_appdata();
504         myaccount_addaccount_free_priv_data(ad);
505 }
506
507 static Evas_Object * __myaccount_addaccount_create_genlist_layout(
508                                                                                                 Evas_Object *navi_bar)
509 {
510         Evas_Object *genlist;
511
512         addacc_list_itc.item_style = "1line";
513         addacc_list_itc.func.text_get = __myaccount_addaccount_gl_label_get;
514         addacc_list_itc.func.content_get = __myaccount_addaccount_icon_get;
515         addacc_list_itc.func.state_get = NULL;
516         addacc_list_itc.func.del = NULL;
517
518         genlist = elm_genlist_add(navi_bar);
519         evas_object_smart_callback_add(genlist, "realized", _myaccount_ug_addaccount_gl_realized, NULL);
520         evas_object_smart_callback_add(genlist, "delete", _myaccount_ug_addaccount_gl_deleted, NULL);
521         return genlist;
522 }
523
524 static Evas_Object *__myaccount_addaccount_append_genlist_item(
525                                                                                         myaccount_appdata *ad, Evas_Object *genlist, int count)
526 {
527         if (ad == NULL) {
528                 MYACCOUNT_ERROR("__myaccount_addaccount_create_genlist returns NULL\n");
529                 return NULL;
530         }
531
532         GList* iter;
533
534         for(iter=ad->sorted_sp_list;iter!=NULL;iter=g_list_next(iter)) {
535                 addaccount_list_priv* tmp = (addaccount_list_priv*)iter->data;
536
537                 if(ad->capability_filter) {
538                         /* Samsung account exception */
539                         if(tmp->sp_type == MYACCOUNT_TYPE_SAMSUNG
540                                 && (!strcmp(ad->capability_filter, "http://tizen.org/account/capability/memo")
541                                         || !strcmp(ad->capability_filter, "http://tizen.org/account/capability/contact")
542                                         || !strcmp(ad->capability_filter, "http://tizen.org/account/capability/calendar"))) {
543                                 elm_genlist_item_append(genlist, &addacc_list_itc,
544                                                 (void *)tmp, NULL,
545                                                 ELM_GENLIST_ITEM_NONE,
546                                                 __myaccount_addaccount_gl_sel,
547                                                 (void *)tmp);
548                         } else if( !strcmp(ad->capability_filter, "http://tizen.org/account/capability/tizen_email")) { /* Tizen email exception */
549                                 if(tmp->b_tizen_email) {
550                                         elm_genlist_item_append(genlist, &addacc_list_itc,
551                                                         (void *)tmp, NULL,
552                                                         ELM_GENLIST_ITEM_NONE,
553                                                         __myaccount_addaccount_gl_sel,
554                                                         (void *)tmp);
555                                 }
556                         } else {
557                                 elm_genlist_item_append(genlist, &addacc_list_itc,
558                                                 (void *)tmp, NULL,
559                                                 ELM_GENLIST_ITEM_NONE,
560                                                 __myaccount_addaccount_gl_sel,
561                                                 (void *)tmp);
562                         }
563                 } else {
564                         elm_genlist_item_append(genlist, &addacc_list_itc,
565                                         (void *)tmp, NULL,
566                                         ELM_GENLIST_ITEM_NONE,
567                                         __myaccount_addaccount_gl_sel,
568                                         (void *)tmp);
569                 }
570
571         }
572
573         elm_genlist_block_count_set(genlist, count);
574
575         return genlist;
576 }
577
578 static Evas_Object *__myaccount_addaccount_create_genlist(
579                                                                                         myaccount_appdata *ad, int count)
580 {
581         Evas_Object *genlist;
582
583         if (ad == NULL) {
584                 MYACCOUNT_ERROR("__myaccount_addaccount_create_genlist returns NULL\n");
585                 return NULL;
586         }
587
588         genlist = __myaccount_addaccount_create_genlist_layout(ad->navi_bar);
589
590         return __myaccount_addaccount_append_genlist_item(ad, genlist, count);
591 }
592
593 static Evas_Object *__myaccount_addaccount_no_account(myaccount_appdata *ad, int count)
594 {
595         Evas_Object *no_account;
596
597         if (ad == NULL) {
598                 MYACCOUNT_ERROR("__myaccount_addaccount_no_account returns NULL\n");
599                 return NULL;
600         }
601         no_account = elm_layout_add(ad->navi_bar);
602         elm_layout_file_set(no_account, MA_NO_ACCOUNT_EDJ_NAME, "nocontents_layout");
603
604         elm_layout_theme_set(no_account, "layout", "nocontents", "default");
605         evas_object_size_hint_weight_set(no_account, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
606         evas_object_size_hint_align_set(no_account, EVAS_HINT_FILL, EVAS_HINT_FILL);
607         elm_object_part_text_set(no_account, "elm.text", dgettext(MA_UG_NAME, "IDS_MA_NPBODY_NO_ACCOUNT_PROVIDER_APPS_INSTALLED"));
608         /* left below line to future use */
609         //elm_object_part_text_set(no_account, "elm.help.text", "Please install account app to add account");
610         elm_layout_signal_emit(no_account, "text,disabled", "");
611         elm_layout_signal_emit(no_account, "align.center", "elm");
612
613         return no_account;
614 }
615
616 Eina_Bool __myaccount_addaccount_quit_cb( void *data,
617                                                         Elm_Object_Item *it )
618 {
619         int error_code = 0;
620         myaccount_appdata *priv = (myaccount_appdata*)data;
621         int count = -1;
622
623         MYACCOUNT_DBUG("Add account list quit\n");
624
625         if (!priv) {
626                 MYACCOUNT_ERROR("__myaccount_addaccount_quit_cb callback user data is null!!!\n");
627                 return EINA_TRUE;
628         }
629
630         if(priv->capability_filter && strlen(priv->capability_filter)){
631
632                 if(!strcmp(priv->capability_filter, "http://tizen.org/account/capability/tizen_email")){
633                         int cnt1 = 0;
634                         int cnt2 = 0;
635                         int cnt3 = 0;
636                         error_code = myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID, &cnt1);
637                         error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt2);
638                         error_code = myaccount_common_get_account_cnt_by_appid(GOOGLE_ACCOUNT_APPID, &cnt3);
639                         count = cnt1+cnt2+cnt3;
640                 } else {
641                         error_code = myaccount_common_get_account_cnt_by_capability(priv->capability_filter, &count);
642                 }
643
644                 if (error_code != ACCOUNT_ERROR_NONE) {
645                         MYACCOUNT_ERROR("__myaccount_addaccount_quit_cb: myaccount_common_get_all_account_cnt fail(%d) \n",
646                                                                                 error_code);
647                 }
648         } else {
649                 error_code = myaccount_common_get_all_account_cnt(&count);
650                 if (error_code != ACCOUNT_ERROR_NONE) {
651                         MYACCOUNT_ERROR("__myaccount_addaccount_quit_cb: myaccount_common_get_all_account_cnt fail(%d) \n",
652                                                                                 error_code);
653                 }
654         }
655
656         /*destroy add genlist before pop it*/
657
658         if ((count > 0) && (priv->eMode == eMYACCOUNT_ACCOUNT_LIST)) {
659                 MYACCOUNT_DBUG("# count > 0, elm_naviframe_item_pop return EINA_TRUE\n");
660                 if (priv->add_genlist) {
661                         evas_object_del(priv->add_genlist);
662                         priv->add_genlist = NULL;
663                 }
664                 priv->prev_app_cnt = 0;
665
666                 return EINA_TRUE;
667         }
668
669         if (priv->ug) {
670                 ui_gadget_h ug = priv->ug;
671                 ug_destroy_me(ug);
672                 return EINA_FALSE;
673         }
674
675         if (priv->add_genlist) {
676                 evas_object_del(priv->add_genlist);
677                 priv->add_genlist = NULL;
678         }
679         priv->prev_app_cnt = 0;
680         MYACCOUNT_DBUG("Add account list quit end\n");
681
682         return EINA_TRUE;
683 }
684
685 void myaccount_addaccount_genlist_create(void *data)
686 {
687         myaccount_appdata *ad = data;
688         Evas_Object *genlist;
689         Evas_Object *l_button;
690         int service_cnt=0;
691
692         if (ad == NULL) {
693                 MYACCOUNT_ERROR("myaccount_addaccount_genlist_create myaccount_appdata is null\n");
694                 return;
695         }
696
697         service_cnt = __myaccount_addaccount_populate_service_info(ad);
698         ad->prev_app_cnt = service_cnt;
699         if(service_cnt > 0){
700                 ad->add_genlist = genlist = __myaccount_addaccount_create_genlist(ad, service_cnt);
701         }else{
702                 /* If there's no account provider, display no account view */
703                 ad->add_genlist = genlist = __myaccount_addaccount_no_account(ad, service_cnt);
704         }
705
706         l_button = elm_button_add(ad->navi_bar);
707         elm_object_style_set(l_button , "naviframe/back_btn/default");
708
709         ad->add_nf_it = elm_naviframe_item_push(ad->navi_bar,
710                                         dgettext(MA_UG_NAME, "IDS_MA_HEADER_ADD_ACCOUNT"),
711                                         l_button, NULL, genlist, NULL);
712         MYACCOUNT_DBUG("#elm_naviframe_item_pushed..");
713         elm_naviframe_item_pop_cb_set(ad->add_nf_it, __myaccount_addaccount_quit_cb, (void*)ad);
714 }
715
716 void myaccount_addaccount_create_view(myaccount_appdata *ad)
717 {
718         MYACCOUNT_DBUG("myaccount_addaccount_create_view start\n");
719         if (ad->add_genlist == NULL) {
720                 MYACCOUNT_DBUG("Add account list start\n");
721                 myaccount_addaccount_free_priv_data(ad);
722                 myaccount_addaccount_genlist_create(ad);
723         }
724 }
725
726 void myaccount_addaccount_refresh_item_list(myaccount_appdata *ad)
727 {
728         int count = 0;
729
730         if(!ad) {
731                 MYACCOUNT_ERROR("no appdata!\n");
732                 return;
733         }
734
735         if(!ad->add_genlist) {
736                 MYACCOUNT_ERROR("no genlist!\n");
737                 return;
738         }
739
740         /*check if app count changed or not, if not changed, then do not refresh current view*/
741         int curr_cnt = 0;
742         GList *sp_list = NULL;
743         _myaccount_addaccount_get_account_type_info(ad->capability_filter, &sp_list);
744         curr_cnt = g_list_length(sp_list);
745         int valid_cnt = 0;
746         if (ad->prev_app_cnt == curr_cnt) {
747                 int i = 0;
748                 for (i = 0; i < curr_cnt; i++) {
749                         addaccount_list_priv *info = g_list_nth_data(sp_list, i);
750                         if (!info)
751                                 continue;
752                         int j = 0;
753                         for (j = 0; j < curr_cnt; j++) {
754                                 addaccount_list_priv *prev_info = g_list_nth_data(ad->sorted_sp_list, j);
755                                 if (!prev_info)
756                                         continue;
757                                 if (!g_strcmp0(prev_info->service_name, info->service_name)) {
758                                         valid_cnt++;
759                                         //MYACCOUNT_DBUG("info->service_name[%s]", info->service_name);
760                                 }
761                         }
762                 }
763         }
764         /*free temp data*/
765         GList* it = NULL;
766         for(it=sp_list;it!=NULL;it=g_list_next(it)) {
767                 addaccount_list_priv* tmp = (addaccount_list_priv*)it->data;
768                 MA_MEMFREE(tmp);
769         }
770         MYACCOUNT_DBUG("ad->prev_app_cnt[%d], valid_cnt[%d]", ad->prev_app_cnt, valid_cnt);
771         if (ad->prev_app_cnt == valid_cnt) {
772                 MYACCOUNT_DBUG("no change, no need to refresh genlist items!\n");
773                 return;
774         }
775
776         elm_genlist_clear(ad->add_genlist);
777         myaccount_addaccount_free_priv_data(ad);
778         count = __myaccount_addaccount_populate_service_info(ad);
779         ad->prev_app_cnt = count;
780         __myaccount_addaccount_append_genlist_item(ad, ad->add_genlist, count);
781
782         return;
783 }
784