tizen 2.4 release
[apps/home/my-account.git] / src / myaccount_ug_common.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.h"
23 #include "myaccount_ug_common.h"
24 #include "myaccount_ug_addaccount.h"
25 #include "myaccount_ug_account_list.h"
26 #include <iniparser.h>
27 #include <app_control_internal.h>
28 #include <account_internal.h>
29
30 static myaccount_appdata *g_myaccount_appdata = NULL;
31
32 #define MYACCOUNT_MAX_EMAIL_COUNT 10
33 #define MYACCOUNT_LAUNCH_TIME_OUT 0.7
34 #define MYACCOUNT_RELAUNCH_INTERVAL 0.3
35 #define MYACCOUNT_MAX_TOUCH_BLOCK_INTERVAL 2.5
36
37
38 void *myaccount_init_appdata()
39 {
40         if (g_myaccount_appdata == NULL) {
41                 g_myaccount_appdata = calloc(1, sizeof(myaccount_appdata));
42                 g_myaccount_appdata->item_selected_state = false;
43         }
44         return g_myaccount_appdata;
45 }
46
47 void myaccount_destroy_appdata()
48 {
49         if (g_myaccount_appdata == NULL) {
50                 MYACCOUNT_ERROR("myaccount_destroy_appdata: appdata NULL \n");
51                 return;
52         }
53
54         if (g_myaccount_appdata->base) {
55                 evas_object_del(g_myaccount_appdata->base);
56                 g_myaccount_appdata->base = NULL;
57         }
58
59         MA_MEMFREE(g_myaccount_appdata->capability_filter)
60
61         /* For accounts list */
62         if (g_myaccount_appdata->ug_called) {
63                 ug_destroy(g_myaccount_appdata->ug_called);
64                 g_myaccount_appdata->ug_called = NULL;
65         }
66         if (g_myaccount_appdata->layout_main) {
67                 evas_object_del(g_myaccount_appdata->layout_main);
68                 g_myaccount_appdata->layout_main = NULL;
69         }
70         if (g_myaccount_appdata->layout_addaccount) {
71                 evas_object_del(g_myaccount_appdata->layout_addaccount);
72                 g_myaccount_appdata->layout_addaccount = NULL;
73         }
74         if (g_myaccount_appdata->navi_bar) {
75                 evas_object_del(g_myaccount_appdata->navi_bar);
76                 g_myaccount_appdata->navi_bar = NULL;
77         }
78         if (g_myaccount_appdata->help_label) {
79                 evas_object_del(g_myaccount_appdata->help_label);
80                 g_myaccount_appdata->help_label = NULL;
81         }
82         if (g_myaccount_appdata->refresh_btn) {
83                 evas_object_del(g_myaccount_appdata->refresh_btn);
84                 g_myaccount_appdata->refresh_btn = NULL;
85         }
86         if (g_myaccount_appdata->refresh_icon) {
87                 evas_object_del(g_myaccount_appdata->refresh_icon);
88                 g_myaccount_appdata->refresh_icon = NULL;
89         }
90         if (g_myaccount_appdata->cancel_sync_icon) {
91                 evas_object_del(g_myaccount_appdata->cancel_sync_icon);
92                 g_myaccount_appdata->cancel_sync_icon = NULL;
93         }
94         if (g_myaccount_appdata->auto_sync_check) {
95                 evas_object_del(g_myaccount_appdata->auto_sync_check);
96                 g_myaccount_appdata->auto_sync_check = NULL;
97         }
98         if (g_myaccount_appdata->bg) {
99                 evas_object_del(g_myaccount_appdata->bg);
100                 g_myaccount_appdata->bg = NULL;
101         }
102         if (g_myaccount_appdata->ly) {
103                 elm_object_part_content_unset(g_myaccount_appdata->ly, "elm.swallow.content");
104                 evas_object_del(g_myaccount_appdata->ly);
105                 g_myaccount_appdata->ly = NULL;
106         }
107         if (g_myaccount_appdata->modal_popup) {
108                 evas_object_del(g_myaccount_appdata->modal_popup);
109                 g_myaccount_appdata->modal_popup = NULL;
110         }
111         if (g_myaccount_appdata->r_title_sk) {
112                 evas_object_del(g_myaccount_appdata->r_title_sk);
113                 g_myaccount_appdata->r_title_sk = NULL;
114         }
115
116         if (g_myaccount_appdata->progress_timer) {
117                 ecore_timer_del(g_myaccount_appdata->progress_timer);
118                 g_myaccount_appdata->progress_timer = NULL;
119         }
120         if (g_myaccount_appdata->relaunch_timer) {
121                 ecore_timer_del(g_myaccount_appdata->relaunch_timer);
122                 g_myaccount_appdata->relaunch_timer = NULL;
123         }
124         if (g_myaccount_appdata->pc) {
125                 pkgmgr_client_free(g_myaccount_appdata->pc);
126                 g_myaccount_appdata->pc = NULL;
127         }
128
129         if(g_myaccount_appdata->theme) {
130                 elm_theme_free(g_myaccount_appdata->theme);
131                 g_myaccount_appdata->theme = NULL;
132         }
133
134         if(g_myaccount_appdata->clickblock_timer){
135                 ecore_timer_del(g_myaccount_appdata->clickblock_timer);
136                 g_myaccount_appdata->clickblock_timer = NULL;
137         }
138
139         g_myaccount_appdata->sorted_sp_list= NULL;
140         g_myaccount_appdata->sorted_account_list= NULL;
141
142         MA_MEMFREE(g_myaccount_appdata->caller_userdata);
143         MA_MEMFREE(g_myaccount_appdata->called_from);
144         MA_MEMFREE(g_myaccount_appdata);
145
146 }
147
148 void * myaccount_get_appdata()
149 {
150         return g_myaccount_appdata;
151 }
152
153 static Eina_Bool _myaccount_common_click_block_timer_cb(void *data)
154 {
155         myaccount_appdata *ad = (myaccount_appdata *)data;
156
157         if(!ad){
158                 return ECORE_CALLBACK_CANCEL;
159         }
160
161         if(ad->clickblock_timer){
162                 ecore_timer_del(ad->clickblock_timer);
163                 ad->clickblock_timer = NULL;
164         }
165
166         MYACCOUNT_DBUG("_myaccount_common_click_block_timer_cb: ad->item_selected_state=%d\n",  myaccount_common_get_item_selected_state());
167         myaccount_common_set_item_selected_state(FALSE);
168
169         return ECORE_CALLBACK_CANCEL;
170 }
171
172
173 void myaccount_common_set_item_selected_state(bool val)
174 {
175         myaccount_appdata *appdata = (myaccount_appdata*)myaccount_get_appdata();
176
177         MYACCOUNT_DBUG("myaccount_common_set_item_selected_state[%s]", val ? "true":"false");
178         if (!appdata) {
179                 MYACCOUNT_ERROR("appdata is NULL");
180                 return;
181         }
182
183         appdata->item_selected_state = val;
184
185         if(appdata->clickblock_timer){
186                 ecore_timer_del(appdata->clickblock_timer);
187                 appdata->clickblock_timer = NULL;
188         }
189
190         if( val == true ) {
191                 appdata->clickblock_timer = ecore_timer_add(MYACCOUNT_MAX_TOUCH_BLOCK_INTERVAL, _myaccount_common_click_block_timer_cb, appdata);
192         }
193 }
194
195 bool myaccount_common_get_item_selected_state()
196 {
197         myaccount_appdata *appdata = (myaccount_appdata*)myaccount_get_appdata();
198
199         return (appdata->item_selected_state);
200 }
201
202 void myaccount_common_get_icon_by_name(char *domain_name,
203                                                                                         char *icon_path)
204 {
205         if (!domain_name) {
206                 MYACCOUNT_ERROR("\n !!! domain_name is NULL \n");
207                 return;
208         }
209         char tmp_domain_name[64] = {0,};
210         myaccount_common_lowercase(domain_name, tmp_domain_name);
211
212         if (!icon_path) {
213                 MYACCOUNT_ERROR("\n !!! icon_path is NULL \n");
214                 return;
215         }
216
217         MA_SNPRINTF(icon_path, BUFSIZE, "%s", "A01_2_Icon_default.png");
218 }
219
220 MyAccountProviderType myaccount_common_get_provider_type(
221                                                                                                         char    *package_name)
222 {
223         MyAccountProviderType sp_type = MYACCOUNT_TYPE_UNKNOWN;
224
225         if (!package_name) {
226                 MYACCOUNT_ERROR("myaccount_common_get_provider_type : package name is NULL\n");
227                 return sp_type;
228         }
229
230         if (!strcmp(package_name, "com.samsung.picasa"))
231                 sp_type = MYACCOUNT_TYPE_PICASA;
232         else if (!strcmp(package_name, "com.samsung.youtube"))
233                 sp_type = MYACCOUNT_TYPE_YOUTUBE;
234         else if (!strcmp(package_name, "email-setting-efl"))
235                 sp_type = MYACCOUNT_TYPE_GOOGLE;
236         else if (!strcmp(package_name, "activesync-ui") || !strcmp(package_name, "activesync-efl"))
237                 sp_type = MYACCOUNT_TYPE_EXCHANGE;
238 //      else if (!strcmp(package_name, "com.samsung.facebook"))
239 //              sp_type = MYACCOUNT_TYPE_FACEBOOK;
240         else if (!strcmp(package_name, SAMSUNG_ACCOUNT_APPID))
241                 sp_type = MYACCOUNT_TYPE_SAMSUNG;
242         else
243                 sp_type = MYACCOUNT_TYPE_UNKNOWN;
244
245         return sp_type;
246 }
247
248 void myaccount_common_lowercase(char *src, char *dest)
249 {
250         int i;
251         int src_len=0;
252
253         if (!src || !dest) {
254                 MYACCOUNT_ERROR("myaccount_common_lowercase src=%p, dest=%p\n",
255                                                 src, dest);
256                 return;
257         }
258         src_len = strlen(src);
259         for(i=0;i<src_len;i++) {
260                 dest[i] = tolower(src[i]);
261         }
262         dest[i] = '\0';
263 }
264
265 void myaccount_common_handle_notification(const char* event_type)
266 {
267         myaccount_appdata *appdata = (myaccount_appdata*)myaccount_get_appdata();
268
269         if (!appdata) {
270                 MYACCOUNT_ERROR("App data NULL!!! \n");
271                 return;
272         }
273
274         int error_code = -1;
275         int count = -1;
276
277         myaccount_common_set_item_selected_state(false);
278
279         MYACCOUNT_DBUG("called_from[%s],eMode(%d),event_type(%s), capability_filter(%s)\n", appdata->called_from, appdata->eMode,event_type, appdata->capability_filter);
280         if(appdata->capability_filter && strlen(appdata->capability_filter)) {
281                 if(!strcmp(appdata->capability_filter, "http://tizen.org/account/capability/tizen_email")){
282                         int cnt1 = 0;
283                         int cnt2 = 0;
284                         int cnt3 = 0;
285                         error_code = myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID, &cnt1);
286                         if (error_code != ACCOUNT_ERROR_NONE) {
287                                 MYACCOUNT_DBUG(": myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID) code(%d)cnt1(%d) \n",    error_code, cnt1);
288                         }
289
290                         error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt2);
291                         if (error_code != ACCOUNT_ERROR_NONE) {
292                                 MYACCOUNT_DBUG(": myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID) code(%d)cnt2(%d) \n",    error_code, cnt2);
293                         }
294
295                         error_code = myaccount_common_get_account_cnt_by_appid(GOOGLE_ACCOUNT_APPID, &cnt3);
296                         if (error_code != ACCOUNT_ERROR_NONE) {
297                                 MYACCOUNT_DBUG(": myaccount_common_get_account_cnt_by_appid(GOOGLE_ACCOUNT_APPID) code(%d)cnt3(%d) \n", error_code, cnt3);
298                         }
299                         count = cnt1+cnt2+cnt3;
300
301                 } else {
302                         error_code = myaccount_common_get_account_cnt_by_capability(appdata->capability_filter, &count);
303                         if (error_code != ACCOUNT_ERROR_NONE) {
304                                 MYACCOUNT_DBUG(": myaccount_common_get_account_cnt_by_capability code(%d)count(%d) \n", error_code, count);
305                         }
306                 }
307
308
309         } else {
310                 error_code = myaccount_common_get_all_account_cnt(&count, false);
311                 if (error_code != ACCOUNT_ERROR_NONE) {
312                         MYACCOUNT_ERROR("myaccount_common_handle_notification: myaccount_common_get_all_account_cnt code(%d) \n",
313                                                                 error_code);
314                 }
315         }
316         MYACCOUNT_DBUG("count[%d],appdata->add_genlist[%p],appdata->account_genlist[%p]", count,appdata->add_genlist,appdata->account_genlist);
317
318         if (appdata->eMode == eMYACCOUNT_ACCOUNT_LIST) {
319                 if (count > 0) {
320                         if (appdata->add_genlist) {
321                                 if( strcmp(event_type, ACCOUNT_NOTI_NAME_SYNC_UPDATE)){ // not sync update -> insert, update, delete
322                                         MYACCOUNT_DBUG("#  recved noti-event(%s) -> elm_naviframe_item_pop / quit_cb should be called.", (event_type != NULL) ? event_type : "null");
323                                         elm_naviframe_item_pop(appdata->navi_bar);
324                                         appdata->add_genlist = NULL;
325                                         MYACCOUNT_DBUG("elm_naviframe_item_pop func finished.\n");
326                                 } else {
327                                         // if sync update received, do nothing.
328                                         MYACCOUNT_DBUG("recved noti-event(%s) -> do nothing.", (event_type != NULL) ? event_type : "null");
329                                 }
330                         }
331
332                         if (appdata->account_genlist) {
333                                 myaccount_list_refresh_item_list(appdata);
334                         } else {
335                                 myaccount_ug_account_list_create((void*)appdata);
336                         }
337                 } else {
338                         if (appdata->add_genlist) {
339                                 evas_object_show(appdata->add_genlist);
340                         } else {
341                                 MYACCOUNT_DBUG("eMYACCOUNT_ACCOUNT_LIST mode, count=0, add_genlist=NULL, event_type must be delete.");
342                                 myaccount_addaccount_create_view(appdata);      // event_type=delete, fix duplicate elm_naviframe_item_push happening.
343                         }
344                 }
345         } else if (appdata->eMode == eMYACCOUNT_ADD_ACCOUNT) {
346                 MYACCOUNT_SLOGD("myaccount_common_handle_notification: ADD_ACCOUNT, event_type = %s\n", event_type);
347                 if(event_type){
348                         if (appdata->ug && count > 0) {
349                                 if (!g_strcmp0(event_type, ACCOUNT_NOTI_NAME_INSERT) || !g_strcmp0(event_type, ACCOUNT_NOTI_NAME_SYNC_UPDATE)) {
350                                         app_control_h app_control = NULL;
351                                         app_control_create(&app_control);
352                                         app_control_add_extra_data(app_control, "account_noti", event_type);
353                                         error_code = ug_send_result(appdata->ug, app_control);
354                                         ui_gadget_h ug = appdata->ug;
355                                         error_code = ug_destroy_me(ug);
356                                         app_control_destroy(app_control);
357                                 }
358                         }
359                 }
360         }
361 }
362
363 bool myaccount_samsung_callback(char* result_code, char* user_id, void *user_data)
364 {
365         MYACCOUNT_SLOGD("Result Code = %s\n", result_code);
366         MYACCOUNT_SLOGD("User Id = %s\n", user_id);
367
368         if(user_data) {
369                 int* test = (int*)user_data;
370
371                 MA_MEMFREE(test);
372         }
373
374         return TRUE;
375 }
376
377 static void _myaccount_common_popup_cb(void *data, Evas_Object *obj, void *event_info)
378 {
379         myaccount_appdata *ad = (myaccount_appdata *)data;
380
381         if(ad->modal_popup)
382                 evas_object_del(ad->modal_popup);
383         ad->modal_popup = NULL;
384 }
385
386 Evas_Object* _myaccount_common_add_popup(Evas_Object* parent, const char* text, const char* title)
387 {
388         myaccount_appdata *ad = (myaccount_appdata*)myaccount_get_appdata();
389
390         MYACCOUNT_DBUG("multi window not enabled.\n");
391         ad->modal_popup = elm_popup_add(parent);
392
393         evas_object_size_hint_weight_set(ad->modal_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
394
395         elm_object_text_set(ad->modal_popup, text);
396         if(!title)
397                 elm_object_part_text_set(ad->modal_popup, "title,text", title);
398         evas_object_smart_callback_add(ad->modal_popup, "block,clicked", _myaccount_common_popup_cb, (void*)ad);
399 //      elm_popup_timeout_set(ad->modal_popup, 3);
400 //      evas_object_smart_callback_add(ad->modal_popup, "timeout", _myaccount_common_popup_cb, NULL);
401
402     Evas_Object *ok_btn = elm_button_add(ad->modal_popup);
403     elm_object_text_set(ok_btn, dgettext(MA_UG_NAME, "IDS_MA_BUTTON_OK"));
404     elm_object_part_content_set(ad->modal_popup, "button1", ok_btn);
405     evas_object_smart_callback_add(ok_btn, "clicked", _myaccount_common_popup_cb, (void*)ad);
406     elm_object_style_set(ok_btn, "popup_button/default");
407
408         evas_object_show(ad->modal_popup);
409         return ad->modal_popup;
410 }
411
412 static bool _myaccount_common_synchronise_query_cb(account_h account, void *user_data)
413 {
414         int ret = ACCOUNT_ERROR_NONE;
415         char** tmp_count = (char**)user_data;
416
417         if(!account) {
418                 MYACCOUNT_ERROR("account handle is NULL\n");
419                 return FALSE;
420         }
421
422         ret = account_get_custom(account, "profile_count", tmp_count);
423         if(ret != ACCOUNT_ERROR_NONE) {
424                 MYACCOUNT_ERROR("account_get_custom fail(%x)\n", ret);
425                 return FALSE;
426         }
427
428         return TRUE;
429 }
430
431 static Eina_Bool _myaccount_common_relaunch_application_timer_cb(void *data)
432 {
433         myaccount_appdata *ad = (myaccount_appdata *)data;
434
435         if(!ad){
436                 return ECORE_CALLBACK_CANCEL;
437         }
438
439         if(ad->relaunch_timer){
440                 ecore_timer_del(ad->relaunch_timer);
441                 ad->relaunch_timer = NULL;
442         }
443
444         app_control_send_launch_request(ad->appsvc_handle, NULL, NULL);
445
446         MYACCOUNT_DBUG("ad->item_selected_state=%d\n",  myaccount_common_get_item_selected_state());
447         myaccount_common_set_item_selected_state(FALSE);
448
449         app_control_destroy(ad->appsvc_handle);
450         ad->appsvc_handle = NULL;
451
452         return ECORE_CALLBACK_CANCEL;
453 }
454
455 static void _myaccount_common_try_relaunch(myaccount_appdata* ad, app_control_h app_control)
456 {
457         if(ad->relaunch_timer){
458                 ecore_timer_del(ad->relaunch_timer);
459                 ad->relaunch_timer = NULL;
460         }
461
462         ad->appsvc_handle = app_control;
463
464         ad->relaunch_timer = ecore_timer_add(MYACCOUNT_RELAUNCH_INTERVAL, _myaccount_common_relaunch_application_timer_cb, ad);
465 }
466
467
468 static int _myaccount_common_launch_samsung_account(myaccount_appdata *ad)
469 {
470         int ret = -1;
471         app_control_h app_control = NULL;
472
473         ret = app_control_create(&app_control);
474         if ( ret != APP_CONTROL_ERROR_NONE ) {
475                 MYACCOUNT_SLOGE("app_control_cteate() fail, ret = %d\n", ret);
476         }
477
478         ret = app_control_set_app_id(app_control,SAMSUNG_ACCOUNT_APPID);
479         if ( ret != APP_CONTROL_ERROR_NONE ) {
480                 MYACCOUNT_SLOGE("app_control_set_app_id() fail, ret = %d\n", ret);
481         }
482
483         ret = app_control_set_operation(app_control,ACCOUNT_OPERATION_SIGNIN);
484         if ( ret != APP_CONTROL_ERROR_NONE ) {
485                 MYACCOUNT_SLOGE("app_control_set_operation() fail, ret = %d\n", ret);
486         }
487
488         Ecore_X_Window win_id = elm_win_xwindow_get(ad->win_main);
489         ret = app_control_set_window(app_control, win_id);
490         if ( ret != APP_CONTROL_ERROR_NONE ) {
491                 MYACCOUNT_SLOGE("app_control_set_window() fail, ret = %d\n", ret);
492         }
493
494         ret = app_control_add_extra_data (app_control, "service_category", "from_myaccounts");
495         if ( ret != APP_CONTROL_ERROR_NONE ) {
496                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
497         }
498
499         ret = app_control_add_extra_data (app_control, "client_id", "gr47by21a5");
500         if ( ret != APP_CONTROL_ERROR_NONE ) {
501                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
502         }
503
504         ret = app_control_add_extra_data (app_control, "client_secret", "BD1F21218D46C1C082651EA0B8026721");
505         if ( ret != APP_CONTROL_ERROR_NONE ) {
506                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
507         }
508
509         ret = app_control_send_launch_request(app_control, NULL, NULL);
510
511         if(ret != APP_CONTROL_ERROR_NONE) {
512                 MYACCOUNT_SLOGE("(%s) launch fail. ret(%x)\n", SAMSUNG_ACCOUNT_APPID, ret);
513                 _myaccount_common_try_relaunch(ad, app_control);
514                 return 0;
515         }
516
517         ret = app_control_destroy(app_control);
518         if ( ret != APP_CONTROL_ERROR_NONE ) {
519                 MYACCOUNT_SLOGE("app_control_destroy() fail, ret = %d\n", ret);
520         }
521
522         return ret;
523 }
524
525 static int _myaccount_common_launch_tizen_account(myaccount_appdata *ad)
526 {
527         int ret = -1;
528         app_control_h app_control = NULL;
529
530         ret = app_control_create(&app_control);
531         if ( ret != APP_CONTROL_ERROR_NONE ) {
532                 MYACCOUNT_SLOGE("app_control_create() fail, ret = %d\n", ret);
533         }
534
535         ret = app_control_set_app_id(app_control,TIZEN_ACCOUNT_APPID);
536         if ( ret != APP_CONTROL_ERROR_NONE ) {
537                 MYACCOUNT_SLOGE("app_control_set_app_id() fail, ret = %d\n", ret);
538         }
539
540         ret = app_control_set_operation(app_control,ACCOUNT_OPERATION_SIGNIN);
541         if ( ret != APP_CONTROL_ERROR_NONE ) {
542                 MYACCOUNT_SLOGE("app_control_set_operation() fail, ret = %d\n", ret);
543         }
544
545
546         Ecore_X_Window win_id = elm_win_xwindow_get(ad->win_main);
547         ret = app_control_set_window(app_control, win_id);
548         if ( ret != APP_CONTROL_ERROR_NONE ) {
549                 MYACCOUNT_SLOGE("app_control_set_window() fail, ret = %d\n", ret);
550         }
551
552         ret = app_control_add_extra_data (app_control, "client_id", "j5p7llxxxx");
553         if ( ret != APP_CONTROL_ERROR_NONE ) {
554                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
555         }
556
557         ret = app_control_add_extra_data (app_control, "client_secret", "5113D0052DC14XXXXX751F753384E9XX");
558         if ( ret != APP_CONTROL_ERROR_NONE ) {
559                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
560         }
561
562         ret = app_control_add_extra_data (app_control, "service_category", "from_myaccounts");
563         if ( ret != APP_CONTROL_ERROR_NONE ) {
564                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
565         }
566
567         ret = app_control_send_launch_request(app_control, NULL, NULL);
568
569         if(ret != APP_CONTROL_ERROR_NONE) {
570                 MYACCOUNT_SLOGE("(%s) launch fail. ret(%x)\n", TIZEN_ACCOUNT_APPID, ret);
571                 _myaccount_common_try_relaunch(ad, app_control);
572                 return 0;
573         }
574
575         ret = app_control_destroy(app_control);
576         if ( ret != APP_CONTROL_ERROR_NONE ) {
577                 MYACCOUNT_SLOGE("app_control_destroy() fail, ret = %d\n", ret);
578         }
579
580         return ret;
581 }
582
583 static Eina_Bool _myaccount_common_launch_application_timer_cb(void *data)
584 {
585         myaccount_appdata *ad = (myaccount_appdata *)data;
586
587         if(!ad){
588                 return ECORE_CALLBACK_CANCEL;
589         }
590
591         if(ad->progress_timer){
592                 ecore_timer_del(ad->progress_timer);
593                 ad->progress_timer = NULL;
594         }
595
596         MYACCOUNT_DBUG("ad->item_selected_state=%d\n",  myaccount_common_get_item_selected_state());
597
598         return ECORE_CALLBACK_CANCEL;
599 }
600
601 static void _myaccount_common_launch_timer_start(myaccount_appdata *ad)
602 {
603         if(ad->progress_timer){
604                 ecore_timer_del(ad->progress_timer);
605                 ad->progress_timer = NULL;
606         }
607
608         ad->progress_timer = ecore_timer_add(MYACCOUNT_LAUNCH_TIME_OUT, _myaccount_common_launch_application_timer_cb, ad);
609 }
610
611 static bool _myaccount_common_is_email_count_max()
612 {
613         int cnt1 = 0, cnt2 = 0, cnt3 = 0, count = 0;
614
615         myaccount_common_get_account_cnt_by_appid("email-setting-efl", &cnt1);
616         myaccount_common_get_account_cnt_by_appid("com.samsung.google-service-account", &cnt2);
617         myaccount_common_get_account_cnt_by_appid("activesync-ui", &cnt3);
618         count = cnt1 + cnt2 + cnt3;
619
620         MYACCOUNT_DBUG("count(%d)cnt1(%d)cnt2(%d)cnt3(%d)", count, cnt1, cnt2, cnt3);
621         if(count >= MYACCOUNT_MAX_EMAIL_COUNT){
622                 return TRUE;
623         }
624         return FALSE;
625 }
626
627 int  myaccount_common_launch_application(MyAccountRequestViewType
628                                                                                                                 request_type,
629                                                                                                                 char *package_name,
630                                                                                                                 char *username,
631                                                                                                                 char *email_sp,
632                                                                                                                 int account_id,
633                                                                                                                 MyAccountProviderType sp_type,
634                                                                                                                 void *data )
635 {
636         int ret = APP_CONTROL_ERROR_NONE;
637         app_control_h app_control = NULL;
638         myaccount_appdata *ad = (myaccount_appdata *) data;
639         char win_id_buf[64]={0,};
640         char id_buf[64] = {0,};
641
642         if (!ad || !package_name) {
643                 MYACCOUNT_SLOGE("myaccount_common_launch_application: App data=%p, package_name=%p\n",
644                                                         ad, package_name);
645                 return 0;
646         }
647
648         MYACCOUNT_SLOGD("trying to launch (%s)\n", package_name);
649
650         ad->win_id = elm_win_xwindow_get(ad->win_main);
651         MA_SNPRINTF(win_id_buf, sizeof(win_id_buf)-1, "%d", ad->win_id);
652
653         if(!strcmp(package_name,SAMSUNG_ACCOUNT_APPID)){
654
655                 _myaccount_common_launch_samsung_account(ad);
656                 _myaccount_common_launch_timer_start(ad);
657
658                 MA_MEMFREE(package_name);
659                 return ret;
660         } else {
661                 ret = app_control_create(&app_control);
662                 if ( ret != APP_CONTROL_ERROR_NONE ) {
663                         MYACCOUNT_SLOGE("app_control_create() fail, ret = %d\n", ret);
664                 }
665
666                 ret = app_control_set_window(app_control, ad->win_id);
667                 if ( ret != APP_CONTROL_ERROR_NONE ) {
668                         MYACCOUNT_SLOGE("app_control_set_window() fail, ret = %d\n", ret);
669                 }
670
671                 ret = app_control_set_app_id(app_control, package_name);
672                 if ( ret != APP_CONTROL_ERROR_NONE ) {
673                         MYACCOUNT_SLOGE("app_control_set_app_id() fail, ret = %d\n", ret);
674                 }
675
676                 if(ad->caller_userdata) {
677                         ret = app_control_add_extra_data(app_control, "myaccount_userdata", ad->caller_userdata);
678                         if ( ret != APP_CONTROL_ERROR_NONE ) {
679                                 MYACCOUNT_SLOGE("app_control_add_extra_data() fail, ret = %d\n", ret);
680                         }
681
682                 }
683         }
684
685         if (ret != APP_CONTROL_ERROR_NONE)
686                 MYACCOUNT_ERROR("myaccount_common_launch_application: service_set_package failed\n");
687
688         switch (request_type) {
689                 case MYACCOUNT_REQUEST_SIGNIN :
690                                 //Check MAX synchronise count
691                                 if(strcmp(package_name,"setting-synchronise-efl") == 0){
692                                         int ret = -1, sync_cnt = 0;
693                                         char* synchronise_count = NULL;
694                                         ret = account_query_account_by_package_name(_myaccount_common_synchronise_query_cb, "setting-synchronise-efl", (void*)&synchronise_count);
695                                         if(ret != ACCOUNT_ERROR_NONE) {
696                                                 MYACCOUNT_ERROR("account_query_account_by_package_name fail (%x)\n", ret);
697                                         }
698
699                                         if(synchronise_count)
700                                                 sync_cnt = atoi(synchronise_count);
701
702                                         if(sync_cnt >= 5) {
703                                                 // draw popup and return
704                                                 char buf[256]={0,};
705
706                                                 MA_SNPRINTF(buf, sizeof(buf), "Synchronise profile is full");
707                                                 _myaccount_common_add_popup(ad->layout_main, buf, "Error");
708                                                 myaccount_common_set_item_selected_state(FALSE);
709                                                 MA_MEMFREE(synchronise_count);
710                                                 if(app_control)
711                                                         app_control_destroy(app_control);
712                                                 return -1;
713                                         }
714                                 }else if(!strcmp(package_name,TIZEN_ACCOUNT_APPID)){
715
716                                         _myaccount_common_launch_tizen_account(ad);
717                                         if(app_control)
718                                                 app_control_destroy(app_control);
719                                         _myaccount_common_launch_timer_start(ad);
720
721                                         MA_MEMFREE(package_name);
722                                         return ret;
723                                 } else if(strcmp(package_name,"email-setting-efl") == 0 ||
724                                                 strcmp(package_name,"com.samsung.google-service-account") == 0 ||
725                                                 strcmp(package_name,"activesync-ui") == 0){
726                                         char buf[256]={0,};
727
728                                         MYACCOUNT_DBUG("package_name[%s] check max count\n");
729                                         if( _myaccount_common_is_email_count_max() == TRUE ){
730                                                 MA_SNPRINTF(buf, sizeof(buf), dgettext(MA_UG_NAME,      "IDS_EMAIL_BODY_MAXIMUM_NUMBER_OF_EMAIL_ACCOUNTS_REACHED"));
731                                                 _myaccount_common_add_popup(ad->layout_main, buf, NULL);
732                                                 myaccount_common_set_item_selected_state(FALSE);
733                                                 if(app_control)
734                                                         app_control_destroy(app_control);
735                                                 return -1;
736                                         }
737
738                                 }
739
740                                 app_control_set_operation(app_control, ACCOUNT_OPERATION_SIGNIN);
741                                 if(email_sp && strlen(email_sp) > 0) {
742                                         app_control_add_extra_data(app_control, "http://tizen.org/account/data/service_provider", email_sp);
743                                 }
744
745                                 if(sp_type == MYACCOUNT_TYPE_CSC) {
746                                         app_control_add_extra_data(app_control, "http://tizen.org/account/data/csc", email_sp);
747                                 }
748
749                         break;
750                 case MYACCOUNT_REQUEST_VIEW :
751                         if(!username) {
752                                 MYACCOUNT_ERROR("myaccount_common_launch_application: username=%p !!!\n",
753                                                                 username);
754                                 MA_MEMFREE(package_name);
755                                 if(app_control)
756                                         app_control_destroy(app_control);
757                                 return 0;
758                         }
759
760                         MA_SNPRINTF(id_buf, sizeof(id_buf), "%d", account_id);
761                         ret = app_control_add_extra_data(app_control, ACCOUNT_DATA_ID, id_buf);
762                         if ( ret != APP_CONTROL_ERROR_NONE ) {
763                                 MYACCOUNT_ERROR("app_control_add_extra_data fail, ret = %d\n", ret);
764                         }
765
766                         ret = app_control_set_operation(app_control, ACCOUNT_OPERATION_VIEW);
767                         if ( ret != APP_CONTROL_ERROR_NONE ) {
768                                 MYACCOUNT_ERROR("app_control_set_operation fail, ret = %d\n", ret);
769                         }
770
771                         ret = app_control_add_extra_data(app_control, ACCOUNT_DATA_USERNAME,username);
772                         if ( ret != APP_CONTROL_ERROR_NONE ) {
773                                 MYACCOUNT_ERROR("app_control_add_extra_data fail, ret = %d\n", ret);
774                         }
775                         break;
776                 default :
777                         MA_MEMFREE(username);
778                         MA_MEMFREE(package_name);
779                         app_control_destroy(app_control);
780                         return 0;
781         }
782         MYACCOUNT_SLOGD("myaccount_common_launch_application request type=%d, package name = %s\n",
783                                                         request_type, package_name);
784
785         ret = app_control_add_extra_data(app_control, "XWINDOW_ID", win_id_buf);
786         if ( ret != APP_CONTROL_ERROR_NONE ) {
787                 MYACCOUNT_ERROR("app_control_add_extra_data fail, ret = %d\n", ret);
788         }
789
790         ret = app_control_send_launch_request(app_control, NULL, NULL);
791         if ( ret != APP_CONTROL_ERROR_NONE ) {
792                 MYACCOUNT_ERROR("app_control_send_launch_request fail, ret = %d\n", ret);
793         }
794
795
796         MA_MEMFREE(username);
797         MA_MEMFREE(package_name);
798
799         if(ret != APP_CONTROL_ERROR_NONE) {
800                 MYACCOUNT_SLOGE("(%s) launch fail. ret(%x)\n", package_name, ret);
801 //              char buf[64]={0,};
802 //              MA_SNPRINTF(buf, sizeof(buf), "Launch fail (%x)", ret);
803 //              _myaccount_common_add_popup(ad->win_main, buf, "Error");
804 //              ad->item_selected_state = FALSE;
805                 _myaccount_common_try_relaunch(ad, app_control);
806                 return 0;
807         }
808
809         app_control_destroy(app_control);
810         _myaccount_common_launch_timer_start(ad);
811
812         return ret;
813 }
814
815
816 Evas_Object *myaccount_common_progress_add(Evas_Object *parent)
817 {
818         if (!parent) {
819                 MYACCOUNT_ERROR("myaccount_common_progress_add: parent is NULL \n");
820                 return NULL;
821         }
822         Evas_Object *progressbar = NULL;
823         progressbar = elm_progressbar_add(parent);
824         elm_object_style_set(progressbar, "list_process");
825         evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, 0.5);
826         evas_object_size_hint_weight_set(progressbar,
827                                                 EVAS_HINT_EXPAND,
828                                                 EVAS_HINT_EXPAND);
829         evas_object_show(progressbar);
830         elm_progressbar_pulse(progressbar, EINA_TRUE);
831         return progressbar;
832 }
833
834 void myaccount_common_delete_modal_popup(Evas_Object *popup)
835 {
836         if (!popup){
837                 MYACCOUNT_ERROR("myaccount_common_delete_modal_popup: popup is NULL \n");
838                 return;
839         }
840         evas_object_del(popup);
841 }
842
843 Eina_Bool myaccount_common_modal_popup_timer_cb(gpointer data)
844 {
845         if (!data) {
846                 MYACCOUNT_ERROR("myaccount_common_modal_popup_timer_cb: data is NULL \n");
847                 return false;
848         }
849         myaccount_appdata *global_data = NULL;
850         global_data = (myaccount_appdata*)data;
851         if (global_data->modal_popup) {
852                 myaccount_common_delete_modal_popup(global_data->modal_popup);
853                 global_data->modal_popup = NULL;
854         }
855         if (global_data->progress_timer) {
856                 ecore_timer_del(global_data->progress_timer);
857                 global_data->progress_timer = NULL;
858         }
859         return false;
860 }
861
862 int myaccount_common_get_all_account_cnt(int *count, bool include_secret)
863 {
864         int error_code = -1;
865         int account_cnt = 0;
866         if (!count) {
867                 MYACCOUNT_ERROR("myaccount_common_get_all_account_cnt: count ptr is NULL \n");
868                 return error_code;
869         }
870         if( include_secret ) {
871                 error_code = account_get_total_count_from_db(&account_cnt);
872         }else {
873                 error_code = account_get_total_count_from_db_ex(&account_cnt);
874         }
875         if (error_code != ACCOUNT_ERROR_NONE) {
876                 return error_code;
877         }
878         *count = account_cnt;
879
880         return ACCOUNT_ERROR_NONE;
881 }
882
883 static bool _myaccount_common_query_cb(account_h account, void *user_data)
884 {
885         int* tmp_count = (int*)user_data;
886
887         *tmp_count = *tmp_count + 1;
888
889         return TRUE;
890 }
891
892 int myaccount_common_get_account_cnt_by_capability(const char* capability_type, int *count)
893 {
894         int error_code = -1;
895         int account_cnt = 0;
896         if (!count) {
897                 MYACCOUNT_ERROR("myaccount_common_get_account_cnt_by_capability: count ptr is NULL \n");
898                 return error_code;
899         }
900
901         error_code = account_query_account_by_capability_type(_myaccount_common_query_cb, capability_type, &account_cnt);
902         if (error_code != ACCOUNT_ERROR_NONE) {
903
904                 *count = 0;
905
906                 return error_code;
907         }
908         *count = account_cnt;
909
910         return ACCOUNT_ERROR_NONE;
911 }
912
913 int myaccount_common_get_account_cnt_by_appid(const char* appid, int *count)
914 {
915         int error_code = -1;
916         int account_cnt = 0;
917         if (!count) {
918                 MYACCOUNT_ERROR("myaccount_common_get_account_cnt_by_appid: count ptr is NULL \n");
919                 return error_code;
920         }
921
922         error_code = account_query_account_by_package_name(_myaccount_common_query_cb, appid, &account_cnt);
923
924         if (error_code != ACCOUNT_ERROR_NONE) {
925
926                 *count = 0;
927
928                 return error_code;
929         }
930         *count = account_cnt;
931
932         return ACCOUNT_ERROR_NONE;
933 }
934
935 static int _myaccount_common_pkmgr_return_cb(int req_id, const char *pkg_type,
936                                         const char *pkg_name,
937                                         const char *key,
938                                         const char *val,
939                                         const void *pmsg,
940                                         void *data)
941 {
942         MYACCOUNT_SLOGD(" _myaccount_common_pkmgr_return_cb: pkg_type = %s, pkg_name = %s, key = %s, val = %s\n", pkg_type, pkg_name,key,val);
943
944         int error_code = -1;
945         int count = -1;
946         if( strcmp(key, "end") == 0 && strcmp(val, "ok") == 0) {
947
948                 myaccount_appdata *appdata = (myaccount_appdata*)myaccount_get_appdata();
949
950                 if (!appdata) {
951                         MYACCOUNT_ERROR("App data NULL!!! \n");
952                         return -1;
953                 }
954                 if(appdata->capability_filter && strlen(appdata->capability_filter)){
955                         if(!strcmp(appdata->capability_filter, "http://tizen.org/account/capability/tizen_email")){
956                                 int cnt1 = 0;
957                                 int cnt2 = 0;
958                                 int cnt3 = 0;
959                                 error_code = myaccount_common_get_account_cnt_by_appid(TIZEN_EMAIL_APPID, &cnt1);
960                                 error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt2);
961                                 error_code = myaccount_common_get_account_cnt_by_appid(EXCHANGE_UI_APPID, &cnt3);
962                                 count = cnt1+cnt2+cnt3;
963                         } else {
964                                 error_code = myaccount_common_get_account_cnt_by_capability(appdata->capability_filter, &count);
965                         }
966
967                         if (error_code != ACCOUNT_ERROR_NONE) {
968                                 MYACCOUNT_ERROR("__myaccount_addaccount_quit_cb: myaccount_common_get_all_account_cnt fail(%d) \n",
969                                                                                         error_code);
970                         }
971                 } else {
972                         error_code = myaccount_common_get_all_account_cnt(&count, false);
973                         if (error_code != ACCOUNT_ERROR_NONE) {
974                                 MYACCOUNT_ERROR("__myaccount_addaccount_quit_cb: myaccount_common_get_all_account_cnt fail(%d) \n",
975                                                                                         error_code);
976                         }
977                 }
978
979                 if (appdata->eMode == eMYACCOUNT_ACCOUNT_LIST) {
980                         if (count > 0) {
981                                 if (appdata->add_genlist) {
982                                         evas_object_del(appdata->add_genlist);
983                                         appdata->add_genlist = NULL;
984                                         elm_naviframe_item_pop(appdata->navi_bar);
985                                 }
986                                 if (appdata->account_genlist) {
987                                         myaccount_list_refresh_item_list(appdata);
988                                 } else {
989                                         myaccount_ug_account_list_create((void*)appdata);
990                                 }
991                         } else {
992                                 if (appdata->add_genlist) {
993                                         evas_object_show(appdata->add_genlist);
994                                 } else {
995                                         myaccount_addaccount_create_view(appdata);
996                                 }
997                         }
998                 } else if (appdata->eMode == eMYACCOUNT_ADD_ACCOUNT) {
999                         if (appdata->ug) {
1000                                 ui_gadget_h ug = appdata->ug;
1001                                 error_code = ug_destroy_me(ug);
1002                         }
1003                 } else {
1004                         //MYACCOUNT_WARNING("Unknown mode !!!");
1005                 }
1006                 return 0;
1007         }
1008         return 0;
1009 }
1010
1011 void myaccount_common_listen_pkgmgr_for_pkg_uninstall()
1012 {
1013         myaccount_appdata *appdata = (myaccount_appdata*)myaccount_get_appdata();
1014
1015         if (!appdata) {
1016                 MYACCOUNT_ERROR("App data NULL!!! \n");
1017                 return;
1018         }
1019
1020         int result = 0;
1021         pkgmgr_client *pc = NULL;
1022         pc = pkgmgr_client_new(PC_LISTENING);
1023         if(pc == NULL) {
1024                 MYACCOUNT_ERROR("pc is NULL\n");
1025                 return;
1026         }
1027         appdata->pc = pc;
1028         result = pkgmgr_client_listen_status(pc, _myaccount_common_pkmgr_return_cb, pc);
1029         if(result < 0)
1030         {
1031                 MYACCOUNT_ERROR("status listen failed!\n");
1032                 return;
1033         }
1034 }
1035
1036
1037 int myaccount_common_get_csc_sp_count(const char* capability_filter)
1038 {
1039         dictionary* dic = NULL;
1040         int sp_count = -1;
1041
1042         if(capability_filter) {
1043                 // tizen email, email, none case, read csc email provider
1044                 // otherwise ignore csc list
1045                 if(strcmp(capability_filter, "http://tizen.org/account/capability/tizen_email")
1046                         && strcmp(capability_filter, "http://tizen.org/account/capability/email"))
1047                         return 0;
1048         }
1049
1050         // check ini file /opt/system/csc-default/data/csc-default-data-email-account.ini
1051         dic = iniparser_load(TIZEN_EMAIL_CSC_FILE_PATH);
1052         if(!dic){
1053                 MYACCOUNT_ERROR("email csc file is not exist\n");
1054                 return FALSE;
1055         }
1056
1057         sp_count = iniparser_getint(dic, TIZEN_EMAIL_CSC_SP_COUNT_KEY, 0);
1058
1059         if(sp_count <= 0) {
1060                 iniparser_freedict(dic);
1061                 MYACCOUNT_ERROR("email sp count 0\n");
1062                 return 0;
1063         }
1064
1065         iniparser_freedict(dic);
1066
1067         return sp_count;
1068 }
1069
1070 bool myaccount_common_account_by_package_name_cb(account_h account,
1071                                                                                                         void *user_data)
1072 {
1073         return false;
1074 }
1075
1076
1077 static bool _myaccount_common_get_account_type_count_cb(account_type_h account_type, void *user_data)
1078 {
1079         int *count = (int*)user_data;
1080         char* type_buf = NULL;
1081         int type_int = -1;
1082
1083         if(account_type == NULL) {
1084                 MYACCOUNT_ERROR(" account type handle is NULL \n");
1085                 return FALSE;
1086         }
1087
1088         account_type_get_app_id(account_type, &type_buf);
1089
1090         if(!type_buf) return FALSE;
1091
1092         if(!strcmp(type_buf, "email-setting-efl")) {
1093                 *count = *count+3; // predefined email count
1094                 MA_MEMFREE(type_buf);
1095                 return TRUE;
1096         }else if(!strcmp(type_buf, "activesync-ui")) {
1097                 *count = *count+1;
1098                 MA_MEMFREE(type_buf);
1099                 return TRUE;
1100         } else if(!strcmp(type_buf, "com.samsung.rcs-im")
1101                                 || !strcmp(type_buf, "ims-service")) {
1102                 MA_MEMFREE(type_buf);
1103                 return TRUE;
1104         }
1105
1106         account_type_get_multiple_account_support(account_type, &type_int);
1107
1108         if(type_int == FALSE
1109                 && account_query_account_by_package_name(myaccount_common_account_by_package_name_cb, type_buf, NULL) == ACCOUNT_ERROR_NONE) {
1110                 MYACCOUNT_SLOGD("Multiple account not support (%s)\n", type_buf);
1111                 MA_MEMFREE(type_buf);
1112                 return TRUE;
1113         }
1114
1115         *count = *count+1;
1116
1117         MA_MEMFREE(type_buf);
1118
1119         return TRUE;
1120 }
1121
1122
1123 int myaccount_common_get_account_type_count(const char* capability_filter)
1124 {
1125         int count = 0;
1126
1127         if(capability_filter) {
1128                 if(!strcmp(capability_filter, "http://tizen.org/account/capability/tizen_email")){
1129                         account_type_query_by_provider_feature(_myaccount_common_get_account_type_count_cb, "http://tizen.org/account/capability/email", (void*)&count);
1130                 }else {
1131                         account_type_query_by_provider_feature(_myaccount_common_get_account_type_count_cb, capability_filter, (void*)&count);
1132                 }
1133         }
1134         else {
1135                 account_type_foreach_account_type_from_db(_myaccount_common_get_account_type_count_cb, (void*)&count);
1136         }
1137
1138         MYACCOUNT_DBUG("count %d\n", count);
1139
1140         return count;
1141 }
1142