04fdc58441abc4d78dacf19b031ca80f26159382
[platform/core/api/libaccount-service.git] / src / accounts / client / account.c
1 /*
2  *
3  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <gio/gio.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <fcntl.h>
23 #include <glib/gprintf.h>
24 #ifdef G_OS_UNIX
25 #include <gio/gunixfdlist.h>
26 #include <unistd.h>
27 #endif
28 #include <vconf.h>
29 #include <security-server.h>
30
31 #include "account.h"
32 #include "account-error.h"
33 #include "account-private.h"
34 #include "account-types.h"
35 #include "account_ipc_marshal.h"
36
37 #include "dbg.h"
38 #include "account-mgr-stub.h"
39
40 #define ACCOUNT_DB_OPEN_READONLY 0
41 #define ACCOUNT_DB_OPEN_READWRITE 1
42
43 #define VCONF_OK 0
44
45 #define _GET_COOKIE     int ret_code = 0;\
46 char *e_cookie = NULL;\
47 char cookie[256] = {0,};\
48 int size = 0;\
49 \
50 size = security_server_get_cookie_size();\
51 ret_code = security_server_request_cookie(cookie, size);\
52 if (ret_code < 0) {\
53         _ERR("security_server_request_cookie() is failed.");\
54         return ACCOUNT_ERROR_PERMISSION_DENIED;\
55 }\
56 \
57 e_cookie = g_base64_encode((const guchar *)cookie, size);\
58 if (e_cookie == NULL) {\
59         _ERR("g_base64_encode() is failed.");\
60         return ACCOUNT_ERROR_PERMISSION_DENIED;\
61 }
62
63 static AccountManager *_acc_mgr = NULL;
64
65 static char *_account_get_text(const char *text_data);
66 static int _account_gslist_free(GSList* list);
67 static int _account_glist_free(GList* list);
68
69 static int _account_free_capability_items(account_capability_s *data)
70 {
71         _ACCOUNT_FREE(data->type);
72         _ACCOUNT_FREE(data->package_name);
73         _ACCOUNT_FREE(data->user_name);
74
75         return ACCOUNT_ERROR_NONE;
76 }
77
78 static int _account_custom_item_free(account_custom_s *data)
79 {
80         _ACCOUNT_FREE(data->app_id);
81         _ACCOUNT_FREE(data->key);
82         _ACCOUNT_FREE(data->value);
83
84         return ACCOUNT_ERROR_NONE;
85 }
86
87 static int _account_custom_gslist_free(GSList* list)
88 {
89         if(!list){
90                 return ACCOUNT_ERROR_INVALID_PARAMETER;
91         }
92
93         GSList* iter;
94
95         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
96                 account_custom_s *custom_data = (account_custom_s*)iter->data;
97                 _account_custom_item_free(custom_data);
98                 _ACCOUNT_FREE(custom_data);
99         }
100
101         g_slist_free(list);
102         list = NULL;
103
104         return ACCOUNT_ERROR_NONE;
105 }
106
107 static int _account_list_free(GList* list)
108 {
109         if(!list){
110                 return ACCOUNT_ERROR_INVALID_PARAMETER;
111         }
112
113         g_list_free_full(list, g_free);
114         list = NULL;
115
116         return ACCOUNT_ERROR_NONE;
117 }
118
119 static int _account_free_account_items(account_s *data)
120 {
121         _ACCOUNT_FREE(data->user_name);
122         _ACCOUNT_FREE(data->email_address);
123         _ACCOUNT_FREE(data->display_name);
124         _ACCOUNT_FREE(data->icon_path);
125         _ACCOUNT_FREE(data->source);
126         _ACCOUNT_FREE(data->package_name);
127         _ACCOUNT_FREE(data->domain_name);
128         _ACCOUNT_FREE(data->access_token);
129
130         int i;
131         for(i=0;i<USER_TXT_CNT;i++)
132                 _ACCOUNT_FREE(data->user_data_txt[i]);
133
134         _account_gslist_free(data->capablity_list);
135         _account_glist_free(data->account_list);
136         _account_custom_gslist_free(data->custom_list);
137         _account_list_free(data->domain_list);
138         _account_list_free(data->mechanism_list);
139
140         return ACCOUNT_ERROR_NONE;
141 }
142
143 static int _account_gslist_free(GSList* list)
144 {
145         if(!list){
146                 return ACCOUNT_ERROR_INVALID_PARAMETER;
147         }
148
149         GSList* iter;
150
151         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
152                 account_capability_s *cap_data = (account_capability_s*)iter->data;
153                 _account_free_capability_items(cap_data);
154                 _ACCOUNT_FREE(cap_data);
155         }
156
157         g_slist_free(list);
158         list = NULL;
159
160         return ACCOUNT_ERROR_NONE;
161 }
162
163 static int _account_glist_free(GList* list)
164 {
165         if(!list){
166                 return ACCOUNT_ERROR_INVALID_PARAMETER;
167         }
168
169         GList* iter;
170
171         for (iter = list; iter != NULL; iter = g_list_next(iter)) {
172                 account_s *account_record = (account_s*)iter->data;
173                 _account_free_account_items(account_record);
174                 _ACCOUNT_FREE(account_record);
175         }
176
177         g_list_free(list);
178         list = NULL;
179
180         return ACCOUNT_ERROR_NONE;
181 }
182
183 static char *_account_get_text(const char *text_data)
184 {
185         char *text_value = NULL;
186
187         if (text_data != NULL) {
188                 text_value = strdup(text_data);
189         }
190         return text_value;
191 }
192
193 //FIXME : add true singleton logic
194 AccountManager *
195 _account_manager_get_instance ()
196 {
197         _INFO("_account_manager_get_instance");
198         if (_acc_mgr != NULL)
199         {
200                 _INFO("instance already created.");
201                 return _acc_mgr;
202         }
203
204 #if !GLIB_CHECK_VERSION(2,35,0)
205         g_type_init();
206 #endif
207
208         GDBusConnection *connection = NULL;
209         GError *error = NULL;
210
211         connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
212
213         _INFO("after g_bus_get_sync");
214
215
216         /* Create the object */
217         _acc_mgr =
218                 account_manager_proxy_new_sync(connection,
219                                                                                  G_DBUS_PROXY_FLAGS_NONE,
220                                                                                  "org.tizen.account.manager",
221                                                                                 "/org/tizen/account/manager",
222                                                                                 NULL,
223                                                                                  &error);
224         _INFO("_account_manager_get_instance end");
225         return _acc_mgr;
226 }
227
228 GDBusErrorEntry _account_svc_errors[] =
229 {
230         {ACCOUNT_ERROR_NONE, _ACCOUNT_SVC_ERROR_PREFIX".NoError"},
231         {ACCOUNT_ERROR_OUT_OF_MEMORY, _ACCOUNT_SVC_ERROR_PREFIX".OutOfMemory"},
232         {ACCOUNT_ERROR_INVALID_PARAMETER, _ACCOUNT_SVC_ERROR_PREFIX".InvalidParameter"},
233         {ACCOUNT_ERROR_DUPLICATED, _ACCOUNT_SVC_ERROR_PREFIX".Duplicated"},
234         {ACCOUNT_ERROR_NO_DATA, _ACCOUNT_SVC_ERROR_PREFIX".NoData"},
235         {ACCOUNT_ERROR_RECORD_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".RecordNotFound"},
236         {ACCOUNT_ERROR_DB_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".DBFailed"},
237         {ACCOUNT_ERROR_DB_NOT_OPENED, _ACCOUNT_SVC_ERROR_PREFIX".DBNotOpened"},
238         {ACCOUNT_ERROR_QUERY_SYNTAX_ERROR, _ACCOUNT_SVC_ERROR_PREFIX".QuerySynTaxError"},
239         {ACCOUNT_ERROR_ITERATOR_END, _ACCOUNT_SVC_ERROR_PREFIX".IteratorEnd"},
240         {ACCOUNT_ERROR_NOTI_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".NotiFalied"},
241         {ACCOUNT_ERROR_PERMISSION_DENIED, _ACCOUNT_SVC_ERROR_PREFIX".PermissionDenied"},
242         {ACCOUNT_ERROR_XML_PARSE_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".XMLParseFailed"},
243         {ACCOUNT_ERROR_XML_FILE_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".FileNotFound"},
244         {ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL, _ACCOUNT_SVC_ERROR_PREFIX".SubscriptionFailed"},
245         {ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, _ACCOUNT_SVC_ERROR_PREFIX".NotRegisteredProvider"},
246         {ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, _ACCOUNT_SVC_ERROR_PREFIX".NotAllowMultiple"},
247         {ACCOUNT_ERROR_DATABASE_BUSY, _ACCOUNT_SVC_ERROR_PREFIX".database_busy"},
248 };
249
250 static int _account_get_error_code(bool is_success, GError *error)
251 {
252         if (!is_success)
253         {
254                 _INFO("Received error Domain[%d] Message[%s] Code[%d]", error->domain, error->message, error->code);
255
256                 if (g_dbus_error_is_remote_error(error))
257                 {
258                         gchar *remote_error = g_dbus_error_get_remote_error(error);
259                         if (remote_error)
260                         {
261                                 _INFO("Remote error[%s]", remote_error);
262
263                                 //FIXME: Temp fix, error->code sent from daemon is not the same as the one received.
264                                 //However error->message matches, so doing reverse lookup
265                                 int error_enum_count = G_N_ELEMENTS(_account_svc_errors);
266                                 int i = 0;
267                                 for (i = 0; i < error_enum_count; i++)
268                                 {
269                                         if (g_strcmp0(_account_svc_errors[i].dbus_error_name, remote_error) == 0)
270                                         {
271                                                 _INFO("Remote error code matched[%d]", _account_svc_errors[i].error_code);
272                                                 return _account_svc_errors[i].error_code;
273                                         }
274                                 }
275                         }
276                 }
277                 //All undocumented errors mapped to ACCOUNT_ERROR_PERMISSION_DENIED
278                 return ACCOUNT_ERROR_PERMISSION_DENIED;
279         }
280         return ACCOUNT_ERROR_NONE;
281 }
282
283 ACCOUNT_API int account_connect(void)
284 {
285         return ACCOUNT_ERROR_NONE;
286 }
287
288 ACCOUNT_API int account_connect_readonly(void)
289 {
290         return ACCOUNT_ERROR_NONE;
291 }
292
293 ACCOUNT_API int account_disconnect(void)
294 {
295         return ACCOUNT_ERROR_NONE;
296 }
297
298
299 ACCOUNT_API int account_insert_to_db(account_h account, int *account_db_id)
300 {
301         _INFO("1. account_insert_to_db start");
302         char* account_db_path = ACCOUNT_DB_NAME;
303
304         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
305         ACCOUNT_RETURN_VAL((account_db_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
306
307         _GET_COOKIE
308
309         account_s *account_data = (account_s*) account;
310         int error_code = ACCOUNT_ERROR_NONE;
311         GError *error = NULL;
312
313         _INFO("2. Before _account_manager_get_instance()");
314         AccountManager* acc_mgr = _account_manager_get_instance();
315         ACCOUNT_CATCH_ERROR((acc_mgr != NULL), {}, ACCOUNT_ERROR_PERMISSION_DENIED, "Failed to get dbus.");
316
317         int db_id = -1;
318         GVariant* account_serialized = marshal_account(account_data);
319
320         _INFO("3. Before account_manager_call_account_add_sync");
321         bool is_success = account_manager_call_account_add_sync(acc_mgr, account_db_path, account_serialized, e_cookie, &db_id, NULL, &error);
322         ACCOUNT_CATCH_ERROR((is_success != false), {}, _account_get_error_code(is_success, error), "Failed to get dbus.");
323
324         *account_db_id = db_id;
325         account_data->id = db_id;
326
327         _INFO("4. account_insert_to_db end, added db id [%d] [%d] [%d]", db_id, *account_db_id, account_data->id);
328
329         return ACCOUNT_ERROR_NONE;
330
331 CATCH:
332         //Failed to get dbus.
333         _ERR("account_manager_call_account_add_sync()=[%d]", error_code);
334
335         return error_code;
336 }
337
338 ACCOUNT_API int account_delete_from_db_by_id(int account_db_id)
339 {
340         _INFO("1. account_delete_from_db_by_id starting [%d]", account_db_id);
341         char* account_db_path = ACCOUNT_DB_NAME;
342
343         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
344
345         _GET_COOKIE
346
347         GError *error = NULL;
348
349         AccountManager* acc_mgr = _account_manager_get_instance();
350         if (acc_mgr == NULL)
351         {
352                 _ERR("g_bus_get_sync failed");
353                 return ACCOUNT_ERROR_PERMISSION_DENIED;
354         }
355
356         _INFO("2. Before account_manager_call_account_query_account_by_account_id_sync");
357         GVariant *account_serialized_old = NULL;
358         bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, &account_serialized_old, NULL, &error);
359         if (!is_success)
360         {
361                 _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error->code);
362                 return _account_get_error_code(is_success, error);
363         }
364
365         _INFO("3. Before account_manager_call_account_delete_from_db_by_id_sync");
366         is_success = account_manager_call_account_delete_from_db_by_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, NULL, &error);
367         if (!is_success)
368         {
369                 _ERR("account_manager_call_account_delete_from_db_by_id_sync failed [%d]", error->code);
370                 return _account_get_error_code(is_success, error);
371         }
372
373         _INFO("4. Before account_delete_from_db_by_id end");
374         return ACCOUNT_ERROR_NONE;
375 }
376
377 ACCOUNT_API int account_delete_from_db_by_user_name(char *user_name, char *package_name)
378 {
379         _INFO("account_delete_from_db_by_user_name start");
380         char* account_db_path = ACCOUNT_DB_NAME;
381
382         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
383         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
384
385         _GET_COOKIE
386
387         GError *error = NULL;
388
389         AccountManager* acc_mgr = _account_manager_get_instance();
390         if (acc_mgr == NULL)
391         {
392                 _ERR("g_bus_get_sync failed");
393                 return ACCOUNT_ERROR_PERMISSION_DENIED;
394         }
395
396         GVariant* account_list_variant = NULL;
397         bool is_success = account_manager_call_account_query_account_by_user_name_sync(acc_mgr, account_db_path, user_name, e_cookie, &account_list_variant, NULL, &error);
398
399         int error_code = _account_get_error_code(is_success, error);
400         if (error_code != ACCOUNT_ERROR_NONE)
401         {
402                 _ERR("account_query_account_by_user_name error=[%d]", error_code);
403                 return error_code;
404         }
405
406         _INFO("before unmarshal_account_list");
407         GSList* account_list = unmarshal_account_list(account_list_variant);
408         _INFO("after unmarshal_account_list");
409         if (account_list == NULL)
410         {
411                 return ACCOUNT_ERROR_NO_DATA;
412         }
413
414         //TODO free account_list, account_list_variant
415
416         is_success = account_manager_call_account_delete_from_db_by_user_name_sync(acc_mgr, account_db_path, user_name, package_name, e_cookie, NULL, &error);
417
418         if (!is_success)
419         {
420                 _ERR("account_manager_call_account_delete_from_db_by_user_name_sync failed [%d]", error->code);
421                 return _account_get_error_code(is_success, error);
422         }
423
424         return ACCOUNT_ERROR_NONE;
425 }
426
427 int _account_delete_from_db_by_package_name(const char *package_name, bool permission)
428 {
429         _INFO("_account_delete_from_db_by_package_name starting permission opions = %d", permission);
430         char* account_db_path = ACCOUNT_DB_NAME;
431
432         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
433
434         _GET_COOKIE
435
436         GError *error = NULL;
437
438         AccountManager* acc_mgr = _account_manager_get_instance();
439         if (acc_mgr == NULL)
440         {
441                 _ERR("g_bus_get_sync failed");
442                 return ACCOUNT_ERROR_PERMISSION_DENIED;
443         }
444
445         //First get account list of user_name, used for gSSO DB deletion
446         GVariant* account_list_variant = NULL;
447         bool is_success = account_manager_call_account_query_account_by_package_name_sync(acc_mgr, account_db_path, package_name, e_cookie, &account_list_variant, NULL, &error);
448
449         int error_code = _account_get_error_code(is_success, error);
450         if (error_code != ACCOUNT_ERROR_NONE)
451         {
452                 return error_code;
453         }
454
455         _INFO("before unmarshal_account_list");
456         GSList* account_list = unmarshal_account_list(account_list_variant);
457         _INFO("after unmarshal_account_list");
458         if (account_list == NULL)
459         {
460                 return ACCOUNT_ERROR_NO_DATA;
461         }
462
463         is_success = account_manager_call_account_delete_from_db_by_package_name_sync(acc_mgr, account_db_path, package_name, permission, e_cookie, NULL, &error);
464
465         if (!is_success)
466         {
467                 _ERR("account_manager_call_account_delete_from_db_by_package_name_sync failed [%d]", error->code);
468                 return _account_get_error_code(is_success, error);
469         }
470
471         return ACCOUNT_ERROR_NONE;
472 }
473
474 ACCOUNT_API int account_delete_from_db_by_package_name(const char *package_name)
475 {
476         _INFO("account_delete_from_db_by_package_name starting with permission");
477         return _account_delete_from_db_by_package_name(package_name, true);
478 }
479
480 ACCOUNT_API int account_delete_from_db_by_package_name_without_permission(const char *package_name)
481 {
482         _INFO("account_delete_from_db_by_package_name starting without permission");
483         return _account_delete_from_db_by_package_name(package_name, false);
484 }
485
486 ACCOUNT_API int account_update_to_db_by_id(account_h account, int account_id)
487 {
488         //First we will update account db
489         _INFO("1. account_update_to_db_by_id start");
490         char* account_db_path = ACCOUNT_DB_NAME;
491
492         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
493         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
494
495         _GET_COOKIE
496
497         GError *error = NULL;
498
499         AccountManager* acc_mgr = _account_manager_get_instance();
500         if (acc_mgr == NULL)
501         {
502                 _ERR("g_bus_get_sync failed");
503                 return ACCOUNT_ERROR_PERMISSION_DENIED;
504         }
505
506         _INFO("2. Before account_manager_call_account_query_account_by_account_id_sync");
507         GVariant *account_serialized_old = NULL;
508         bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &account_serialized_old, NULL, &error);
509         if (!is_success)
510         {
511                 _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error->code);
512                 return _account_get_error_code(is_success, error);
513         }
514
515         _INFO("3. Before account_manager_call_account_update_to_db_by_id_sync");
516         GVariant* account_serialized = marshal_account((account_s*) account);
517         is_success = account_manager_call_account_update_to_db_by_id_sync(acc_mgr, account_db_path, account_serialized, account_id, e_cookie, NULL, &error);
518
519         if (!is_success)
520         {
521                 _ERR("account_manager_call_account_update_to_db_by_id_sync failed [%d]", error->code);
522                 return _account_get_error_code(is_success, error);
523         }
524
525         _INFO("4. account_update_to_db_by_id end");
526         return ACCOUNT_ERROR_NONE;
527 }
528
529 ACCOUNT_API int account_update_to_db_by_id_ex(account_h account, int account_id)
530 {
531         int ret = -1;
532         ret = account_update_to_db_by_id(account, account_id);
533
534         return ret;
535 }
536
537 ACCOUNT_API int account_update_to_db_by_id_without_permission(account_h account, int account_id)
538 {
539         //First we will update account db
540         //Then we will update gSSO DB, if it fails then we will rollback account db updates
541
542         _INFO("account_update_to_db_by_id_without_permission start");
543         char* account_db_path = ACCOUNT_DB_NAME;
544
545         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
546         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
547
548         _GET_COOKIE
549
550         GError *error = NULL;
551
552         AccountManager* acc_mgr = _account_manager_get_instance();
553         if (acc_mgr == NULL)
554         {
555                 _ERR("g_bus_get_sync failed");
556                 return ACCOUNT_ERROR_PERMISSION_DENIED;
557         }
558
559         GVariant *account_serialized_old = NULL;
560         _INFO("before query() account_id[%d]", account_id);
561         bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &account_serialized_old, NULL, &error);
562         if (!is_success)
563         {
564                 _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error->code);
565                 return _account_get_error_code(is_success, error);
566         }
567
568         _INFO("before marshal() : account_id[%d], user_name=%s", account_id, ((account_s*)account)->user_name);
569         GVariant* account_serialized = marshal_account((account_s*) account);
570         _INFO("after marshal() : account_id[%d]", account_id);
571         if (account_serialized == NULL)
572         {
573                 _ERR("Invalid input");
574                 return ACCOUNT_ERROR_INVALID_PARAMETER;
575         }
576
577         _INFO("before call update() : account_id[%d]", account_id);
578         is_success = account_manager_call_account_update_to_db_by_id_ex_sync(acc_mgr, account_db_path, account_serialized, account_id, e_cookie, NULL, &error);
579         _INFO("after call update() : is_success=%d", is_success);
580         if (!is_success)
581         {
582                 _ERR("account_manager_call_account_update_to_db_by_id_ex_sync failed [%d]", error->code);
583                 return _account_get_error_code(is_success, error);
584         }
585
586         return ACCOUNT_ERROR_NONE;
587 }
588
589 ACCOUNT_API int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
590 {
591         //First we will update account db
592         _INFO("account_update_to_db_by_user_name starting");
593         char* account_db_path = ACCOUNT_DB_NAME;
594
595         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
596         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
597         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
598
599         _GET_COOKIE
600
601         GError *error = NULL;
602
603         AccountManager* acc_mgr = _account_manager_get_instance();
604         if (acc_mgr == NULL)
605         {
606                 _ERR("g_bus_get_sync failed");
607                 return ACCOUNT_ERROR_PERMISSION_DENIED;
608         }
609
610         GVariant *account_serialized_old = NULL;
611         account_s *account_data = (account_s*) account;
612         bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_data->id,     e_cookie, &account_serialized_old, NULL, &error);
613         if (!is_success)
614         {
615                 _ERR("account_manager_call_account_query_account_by_account_id_sync failed [%d]", error->code);
616                 return _account_get_error_code(is_success, error);
617         }
618
619         GVariant* account_serialized = marshal_account(account_data);
620         is_success = account_manager_call_account_update_to_db_by_user_name_sync(acc_mgr, account_db_path, account_serialized, user_name, package_name, e_cookie, NULL, &error);
621         if (!is_success)
622         {
623                 _ERR("account_manager_call_account_update_to_db_by_user_name_sync failed [%d]", error->code);
624                 return _account_get_error_code(is_success, error);
625         }
626
627         return ACCOUNT_ERROR_NONE;
628 }
629
630 ACCOUNT_API int account_create(account_h *account)
631 {
632         if (!account) {
633                 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
634                 return ACCOUNT_ERROR_INVALID_PARAMETER;
635         }
636
637         account_s *data = (account_s*)malloc(sizeof(account_s));
638
639         if (data == NULL) {
640                 ACCOUNT_FATAL("Memory Allocation Failed");
641                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
642         }
643         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
644
645         /*Setting account as visible by default*/
646         data->secret = ACCOUNT_SECRECY_VISIBLE;
647
648         /*Setting account as not supporting sync by default*/
649         data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
650
651         data->auth_type = ACCOUNT_AUTH_TYPE_INVALID;
652
653         data->account_list = NULL;
654         data->capablity_list = NULL;
655         data->custom_list = NULL;
656         data->domain_list = NULL;
657         data->mechanism_list = NULL;
658
659         *account = (account_h)data;
660
661         return ACCOUNT_ERROR_NONE;
662 }
663
664 ACCOUNT_API int account_destroy(account_h account)
665 {
666         account_s *data = (account_s*)account;
667
668         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
669
670         _account_free_account_items(data);
671         _ACCOUNT_FREE(data);
672
673         return ACCOUNT_ERROR_NONE;
674 }
675
676 ACCOUNT_API int account_set_user_name(account_h account, const char *user_name)
677 {
678         if (!account) {
679                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
680                 return ACCOUNT_ERROR_INVALID_PARAMETER;
681         }
682
683         if (!user_name) {
684                 ACCOUNT_SLOGE("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
685                 return ACCOUNT_ERROR_INVALID_PARAMETER;
686         }
687
688         account_s *data = (account_s*)account;
689
690         _ACCOUNT_FREE(data->user_name);
691         data->user_name = _account_get_text(user_name);
692
693         return ACCOUNT_ERROR_NONE;
694 }
695
696 ACCOUNT_API int account_set_display_name(account_h account, const char *display_name)
697 {
698         if (!account) {
699                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
700                 return ACCOUNT_ERROR_INVALID_PARAMETER;
701         }
702
703         if (!display_name) {
704                 ACCOUNT_SLOGE("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
705                 return ACCOUNT_ERROR_INVALID_PARAMETER;
706         }
707
708         account_s *data = (account_s*)account;
709
710         _ACCOUNT_FREE(data->display_name);
711         data->display_name = _account_get_text(display_name);
712
713         return ACCOUNT_ERROR_NONE;
714 }
715
716 ACCOUNT_API int account_set_email_address(account_h account, const char *email_address)
717 {
718         if (!account) {
719                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
720                 return ACCOUNT_ERROR_INVALID_PARAMETER;
721         }
722
723         if (!email_address) {
724                 ACCOUNT_SLOGE("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
725                 return ACCOUNT_ERROR_INVALID_PARAMETER;
726         }
727
728         account_s *data = (account_s*)account;
729
730         _ACCOUNT_FREE(data->email_address);
731         data->email_address = _account_get_text(email_address);
732
733         return ACCOUNT_ERROR_NONE;
734 }
735
736 ACCOUNT_API int account_set_icon_path(account_h account, const char *icon_path)
737 {
738         if (!account) {
739                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
740                 return ACCOUNT_ERROR_INVALID_PARAMETER;
741         }
742
743         if (!icon_path) {
744                 ACCOUNT_SLOGE("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
745                 return ACCOUNT_ERROR_INVALID_PARAMETER;
746         }
747
748         account_s *data = (account_s*)account;
749
750         _ACCOUNT_FREE(data->icon_path);
751         data->icon_path = _account_get_text(icon_path);
752
753         return ACCOUNT_ERROR_NONE;
754 }
755
756 ACCOUNT_API int account_set_source(account_h account, const char *source)
757 {
758         if (!account) {
759                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
760                 return ACCOUNT_ERROR_INVALID_PARAMETER;
761         }
762
763         if (!source) {
764                 ACCOUNT_SLOGE("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
765                 return ACCOUNT_ERROR_INVALID_PARAMETER;
766         }
767         account_s *data = (account_s*)account;
768
769         _ACCOUNT_FREE(data->source);
770         data->source = _account_get_text(source);
771
772         return ACCOUNT_ERROR_NONE;
773 }
774
775 ACCOUNT_API int account_set_package_name(account_h account, const char *package_name)
776 {
777         if (!account) {
778                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
779                 return ACCOUNT_ERROR_INVALID_PARAMETER;
780         }
781
782         if (!package_name) {
783                 ACCOUNT_SLOGE("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
784                 return ACCOUNT_ERROR_INVALID_PARAMETER;
785         }
786
787         account_s *data = (account_s*)account;
788
789         _ACCOUNT_FREE(data->package_name);
790         data->package_name = _account_get_text(package_name);
791
792         return ACCOUNT_ERROR_NONE;
793 }
794
795 ACCOUNT_API int account_set_domain_name(account_h account, const char *domain_name)
796 {
797         if (!account) {
798                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
799                 return ACCOUNT_ERROR_INVALID_PARAMETER;
800         }
801
802         if (!domain_name) {
803                 ACCOUNT_SLOGE("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
804                 return ACCOUNT_ERROR_INVALID_PARAMETER;
805         }
806         account_s *data = (account_s*)account;
807
808         _ACCOUNT_FREE(data->domain_name);
809         data->domain_name = _account_get_text(domain_name);
810
811         return ACCOUNT_ERROR_NONE;
812 }
813
814 ACCOUNT_API int account_set_access_token(account_h account, const char *access_token)
815 {
816         if (!account) {
817                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
818                 return ACCOUNT_ERROR_INVALID_PARAMETER;
819         }
820
821         if (!access_token) {
822                 ACCOUNT_SLOGE("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
823                 return ACCOUNT_ERROR_INVALID_PARAMETER;
824         }
825
826         account_s *data = (account_s*)account;
827
828         _ACCOUNT_FREE(data->access_token);
829         data->access_token = _account_get_text(access_token);
830
831         return ACCOUNT_ERROR_NONE;
832 }
833
834 ACCOUNT_API int account_set_user_text(account_h account, int idx, const char *user_txt)
835 {
836         if (!account) {
837                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
838                 return ACCOUNT_ERROR_INVALID_PARAMETER;
839         }
840
841         if (!user_txt) {
842                 ACCOUNT_SLOGE("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
843                 return ACCOUNT_ERROR_INVALID_PARAMETER;
844         }
845         if (idx >= USER_TXT_CNT || idx < 0) {
846                 ACCOUNT_SLOGE("(%s)-(%d) idx rage should be between 0-4.\n", __FUNCTION__, __LINE__);
847                 return ACCOUNT_ERROR_INVALID_PARAMETER;
848         }
849
850         account_s *data = (account_s*)account;
851
852         _ACCOUNT_FREE(data->user_data_txt[idx]);
853         data->user_data_txt[idx] = _account_get_text(user_txt);
854
855         return ACCOUNT_ERROR_NONE;
856 }
857
858 ACCOUNT_API int account_set_custom(account_h account, const char* key, const char* value)
859 {
860         if (!account) {
861                 ACCOUNT_SLOGE("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
862                 return ACCOUNT_ERROR_INVALID_PARAMETER;
863         }
864
865         if (!key) {
866                 ACCOUNT_SLOGE("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
867                 return ACCOUNT_ERROR_INVALID_PARAMETER;
868         }
869
870         if (!value) {
871                 ACCOUNT_SLOGE("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
872                 return ACCOUNT_ERROR_INVALID_PARAMETER;
873         }
874
875         account_s *data = (account_s*)account;
876
877         GSList *iter;
878         bool b_is_new = TRUE;
879
880         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
881
882                 account_custom_s* custom_data = NULL;
883                 custom_data = (account_custom_s*)iter->data;
884                 ACCOUNT_SLOGD( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
885
886                 if(!strcmp(custom_data->key, key)) {
887                         _ACCOUNT_FREE(custom_data->value);
888                         custom_data->value = _account_get_text(value);
889                         b_is_new = FALSE;
890                 }
891         }
892
893         if(b_is_new) {
894                 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
895
896                 if (custom_data == NULL) {
897                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
898                 }
899                 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
900                 custom_data->account_id = data->id;
901                 custom_data->app_id = _account_get_text(data->package_name);
902                 custom_data->key = _account_get_text(key);
903                 custom_data->value = _account_get_text(value);
904                 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
905         }
906
907         return ACCOUNT_ERROR_NONE;
908 }
909
910 ACCOUNT_API int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
911 {
912         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
913
914         if ( ((int)auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
915                 return ACCOUNT_ERROR_INVALID_PARAMETER;
916         }
917
918         account_s *data = (account_s*)account;
919
920         data->auth_type = (int)auth_type;
921
922         return ACCOUNT_ERROR_NONE;
923 }
924
925 ACCOUNT_API int account_set_secret(account_h account, const account_secrecy_state_e secret)
926 {
927         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
928
929         if ( ((int)secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
930                 return ACCOUNT_ERROR_INVALID_PARAMETER;
931         }
932
933         account_s *data = (account_s*)account;
934
935         data->secret = (int)secret;
936
937         return ACCOUNT_ERROR_NONE;
938 }
939
940 ACCOUNT_API int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
941 {
942         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
943
944         if ( ((int)sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
945                 return ACCOUNT_ERROR_INVALID_PARAMETER;
946         }
947
948         account_s *data = (account_s*)account;
949
950         data->sync_support= (int)sync_support;
951
952         return ACCOUNT_ERROR_NONE;
953 }
954
955 ACCOUNT_API int account_set_user_int(account_h account, int idx, const int user_int)
956 {
957         if (!account) {
958                 return ACCOUNT_ERROR_INVALID_PARAMETER;
959         }
960
961         if (idx >= USER_INT_CNT ||idx < 0) {
962                 return ACCOUNT_ERROR_INVALID_PARAMETER;
963         }
964
965         account_s *data = (account_s*)account;
966
967         data->user_data_int[idx] = user_int;
968
969         return ACCOUNT_ERROR_NONE;
970 }
971
972 ACCOUNT_API int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
973 {
974         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
975         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
976
977         if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
978                 return ACCOUNT_ERROR_INVALID_PARAMETER;
979         }
980
981         account_s *data = (account_s*)account;
982
983         GSList *iter = NULL;
984         bool b_is_new = TRUE;
985
986         for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
987                 account_capability_s *cap_data = NULL;
988                 cap_data = (account_capability_s*)iter->data;
989
990                 if(!strcmp(cap_data->type, capability_type)) {
991                         cap_data->value = capability_value;
992                         b_is_new = FALSE;
993                         break;
994                 }
995         }
996
997         if(b_is_new) {
998                 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
999
1000                 if (cap_data == NULL)
1001                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
1002                 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1003
1004                 cap_data->type = _account_get_text(capability_type);
1005                 cap_data->value = capability_value;
1006                 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1007         }
1008
1009         return ACCOUNT_ERROR_NONE;
1010 }
1011
1012 ACCOUNT_API int account_get_user_name(account_h account, char **user_name)
1013 {
1014         if (!account) {
1015                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1016         }
1017
1018         if (!user_name) {
1019                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1020         }
1021
1022         account_s *data = (account_s*)account;
1023
1024         (*user_name) = NULL;
1025         *user_name = _account_get_text(data->user_name);
1026
1027         return ACCOUNT_ERROR_NONE;
1028 }
1029
1030 ACCOUNT_API int account_get_display_name(account_h account, char **display_name)
1031 {
1032         if (!account) {
1033                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1034         }
1035
1036         if (!display_name) {
1037                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1038         }
1039
1040         account_s *data = (account_s*)account;
1041
1042         (*display_name) = NULL;
1043
1044         *display_name = _account_get_text(data->display_name);
1045
1046         return ACCOUNT_ERROR_NONE;
1047 }
1048
1049 ACCOUNT_API int account_get_email_address(account_h account,char **email_address)
1050 {
1051         if (!account) {
1052                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1053         }
1054
1055         if (!email_address) {
1056                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1057         }
1058
1059         account_s *data = (account_s*)account;
1060
1061         (*email_address) = NULL;
1062
1063         *email_address = _account_get_text(data->email_address);
1064
1065         return ACCOUNT_ERROR_NONE;
1066 }
1067
1068 ACCOUNT_API int  account_get_icon_path(account_h account, char **icon_path)
1069 {
1070         if (!account) {
1071                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1072         }
1073
1074         if (!icon_path) {
1075                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1076         }
1077
1078         account_s *data = (account_s*)account;
1079
1080         (*icon_path) = NULL;
1081
1082         *icon_path = _account_get_text(data->icon_path);
1083
1084         return ACCOUNT_ERROR_NONE;
1085 }
1086
1087 ACCOUNT_API int account_get_source(account_h account, char **source)
1088 {
1089         if (!account) {
1090                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1091         }
1092
1093         if (!source) {
1094                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1095         }
1096
1097         account_s *data = (account_s*)account;
1098
1099         (*source) = NULL;
1100
1101         *source = _account_get_text(data->source);
1102
1103         return ACCOUNT_ERROR_NONE;
1104 }
1105
1106 ACCOUNT_API int account_get_package_name(account_h account, char **package_name)
1107 {
1108         if (!account) {
1109                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1110         }
1111
1112         if (!package_name) {
1113                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1114         }
1115
1116         account_s *data = (account_s*)account;
1117
1118         (*package_name) = NULL;
1119
1120         *package_name = _account_get_text(data->package_name);
1121
1122         return ACCOUNT_ERROR_NONE;
1123 }
1124
1125 ACCOUNT_API int account_get_domain_name(account_h account, char **domain_name)
1126 {
1127         if (!account) {
1128                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1129         }
1130
1131         if (!domain_name) {
1132                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1133         }
1134
1135         account_s *data = (account_s*)account;
1136
1137         (*domain_name) = NULL;
1138
1139         *domain_name = _account_get_text(data->domain_name);
1140
1141         return ACCOUNT_ERROR_NONE;
1142 }
1143
1144 ACCOUNT_API int account_get_access_token(account_h account, char **access_token)
1145 {
1146         if (!account) {
1147                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1148         }
1149
1150         if (!access_token) {
1151                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1152         }
1153
1154         account_s *data = (account_s*)account;
1155
1156         (*access_token) = NULL;
1157
1158         *access_token = _account_get_text(data->access_token);
1159
1160         return ACCOUNT_ERROR_NONE;
1161 }
1162
1163 ACCOUNT_API int account_get_user_text(account_h account, int user_text_index, char **text)
1164 {
1165         if (!account) {
1166                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1167         }
1168
1169         if (!text) {
1170                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1171         }
1172         ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1173
1174         account_s *data = (account_s*)account;
1175
1176         (*text) = NULL;
1177
1178         *text = _account_get_text(data->user_data_txt[user_text_index]);
1179
1180         return ACCOUNT_ERROR_NONE;
1181 }
1182
1183 ACCOUNT_API int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
1184 {
1185         if (!account) {
1186                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1187         }
1188         if (!auth_type) {
1189                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1190         }
1191
1192         account_s* data = (account_s*)account;
1193
1194         *auth_type = data->auth_type;
1195
1196         return ACCOUNT_ERROR_NONE;
1197 }
1198
1199 ACCOUNT_API int account_get_secret(account_h account, account_secrecy_state_e *secret)
1200 {
1201         if (!account) {
1202                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1203         }
1204         if (!secret) {
1205                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1206         }
1207
1208         account_s* data = (account_s*)account;
1209
1210         *secret = data->secret;
1211
1212         return ACCOUNT_ERROR_NONE;
1213 }
1214
1215 ACCOUNT_API int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
1216 {
1217         if (!account) {
1218                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1219         }
1220         if (!sync_support) {
1221                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1222         }
1223
1224         account_s* data = (account_s*)account;
1225
1226         *sync_support = data->sync_support;
1227
1228         return ACCOUNT_ERROR_NONE;
1229 }
1230
1231 ACCOUNT_API int account_get_account_id(account_h account, int *account_id)
1232 {
1233         if (!account) {
1234                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1235         }
1236         if (!account_id) {
1237                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1238         }
1239
1240         account_s *data = (account_s*)account;
1241
1242         *account_id = data->id;
1243
1244         return ACCOUNT_ERROR_NONE;
1245 }
1246
1247 ACCOUNT_API int account_get_user_int(account_h account, int user_int_index, int *integer)
1248 {
1249         if (!account) {
1250                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1251         }
1252
1253         ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1254
1255         if (!integer) {
1256                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1257         }
1258
1259         account_s *data = (account_s*)account;
1260
1261         *integer = data->user_data_int[user_int_index];
1262
1263         return ACCOUNT_ERROR_NONE;
1264 }
1265
1266 ACCOUNT_API int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
1267 {
1268         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1269         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
1270         ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
1271
1272         GSList *iter;
1273         account_s *data = (account_s*)account;
1274
1275         _ERR("before for()");
1276         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1277                 account_capability_s *cap_data = NULL;
1278
1279                 cap_data = (account_capability_s*)iter->data;
1280
1281         _ERR("capability_type = %s, data->type = %s", capability_type, cap_data->type);
1282         _ERR("capability_value = %d, data->value= %d", capability_value, cap_data->value);
1283                 if(!strcmp(capability_type, cap_data->type)) {
1284                         *capability_value = cap_data->value;
1285                         return ACCOUNT_ERROR_NONE;
1286                 }
1287         }
1288         _ERR("after for()");
1289
1290         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1291 }
1292
1293 ACCOUNT_API int account_get_capability_all(account_h account, capability_cb callback, void *user_data)
1294 {
1295         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1296         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1297
1298         GSList *iter;
1299         account_s *data = (account_s*)account;
1300
1301         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1302                 account_capability_s *cap_data = NULL;
1303
1304                 cap_data = (account_capability_s*)iter->data;
1305
1306                 if(callback(cap_data->type, cap_data->value, user_data)!=TRUE){
1307                         return ACCOUNT_ERROR_NONE;
1308                 }
1309         }
1310
1311         return ACCOUNT_ERROR_NONE;
1312 }
1313
1314 ACCOUNT_API int account_get_custom(account_h account, const char* key, char** value)
1315 {
1316         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1317         ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
1318         ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
1319
1320         GSList *iter;
1321         account_s *data = (account_s*)account;
1322
1323         _ERR("before for()");
1324         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1325                 account_custom_s *custom_data = NULL;
1326
1327                 custom_data = (account_custom_s*)iter->data;
1328
1329                 _ERR("key = %s, custom_data->key = %s", key, custom_data->key);
1330                 _ERR("value = %s, custom_data->value = %s", value, custom_data->value);
1331
1332                 if(!strcmp(key, custom_data->key)) {
1333                         (*value) = NULL;
1334                         *value = _account_get_text(custom_data->value);
1335                         return ACCOUNT_ERROR_NONE;
1336                 }
1337         }
1338         _ERR("after for()");
1339
1340         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1341 }
1342
1343 ACCOUNT_API int account_get_custom_all(account_h account, account_custom_cb callback, void* user_data)
1344 {
1345         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1346         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1347
1348         GSList *iter;
1349         account_s *data = (account_s*)account;
1350
1351         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1352                 bool cb_ret = FALSE;
1353                 account_custom_s *custom_data = NULL;
1354
1355                 custom_data = (account_custom_s*)iter->data;
1356
1357                 cb_ret = callback(custom_data->key, custom_data->value, user_data);
1358                 if(!cb_ret) {
1359                         break;
1360                 }
1361         }
1362
1363         return ACCOUNT_ERROR_NONE;
1364 }
1365
1366 ACCOUNT_API int account_foreach_account_from_db(account_cb callback, void *user_data)
1367 {
1368         _INFO("account_type_foreach_account_type_from_db start");
1369         char* account_db_path = ACCOUNT_DB_NAME;
1370
1371         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL"));
1372
1373         _GET_COOKIE
1374
1375         GError *error = NULL;
1376
1377         AccountManager* acc_mgr = _account_manager_get_instance();
1378         if (acc_mgr == NULL)
1379         {
1380                 _ERR("g_bus_get_sync failed");
1381                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1382         }
1383
1384         GVariant* account_list_variant = NULL;
1385         bool is_success = account_manager_call_account_query_all_sync(acc_mgr, account_db_path, e_cookie, &account_list_variant, NULL, &error);
1386
1387         int error_code = _account_get_error_code(is_success, error);
1388         if (error_code != ACCOUNT_ERROR_NONE)
1389         {
1390                 return error_code;
1391         }
1392
1393         _INFO("before unmarshal_account_list");
1394         GSList* account_list = unmarshal_account_list(account_list_variant);
1395         _INFO("after unmarshal_account_list");
1396         GSList* iter;
1397
1398         for (iter = account_list; iter != NULL; iter = g_slist_next(iter))
1399         {
1400                 _INFO("iterating received account_list");
1401                 account_s *account = NULL;
1402                 account = (account_s*)iter->data;
1403                 _INFO("Before _account_query_identity_info_by_id");
1404
1405                 _INFO("account->id=%d", account->id);
1406                 if (callback((account_h)account, user_data) == false)
1407                 {
1408                         _INFO("application callback requested to discontinue.");
1409                         break;
1410                 }
1411                 _INFO("After one iteration callback");
1412         }
1413         _INFO("account_foreach_account_from_db end");
1414
1415         return ACCOUNT_ERROR_NONE;
1416 }
1417
1418 ACCOUNT_API int account_query_account_by_account_id(int account_db_id, account_h *account)
1419 {
1420         _INFO("account_query_account_by_account_id start [%d]", account_db_id);
1421         char* account_db_path = ACCOUNT_DB_NAME;
1422
1423         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1424         ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1425
1426         _GET_COOKIE
1427
1428         GError *error = NULL;
1429
1430         AccountManager* acc_mgr = _account_manager_get_instance();
1431         if (acc_mgr == NULL)
1432         {
1433                 _ERR("g_bus_get_sync failed");
1434                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1435         }
1436
1437         GVariant* account_variant = NULL;
1438         bool is_success = account_manager_call_account_query_account_by_account_id_sync(acc_mgr, account_db_path, account_db_id, e_cookie, &account_variant, NULL, &error);
1439
1440         int error_code = _account_get_error_code(is_success, error);
1441         if (error_code != ACCOUNT_ERROR_NONE)
1442         {
1443                 return error_code;
1444         }
1445
1446         _INFO("before unmarshal_account");
1447         account_s* account_data = umarshal_account(account_variant);
1448         _INFO("after unmarshal_account");
1449
1450         if (account_data == NULL)
1451         {
1452                 _ERR("Failed to unmarshal");
1453                 return ACCOUNT_ERROR_DB_FAILED;
1454         }
1455
1456         *account = (account_h) account_data;
1457
1458         _INFO("account_query_account_by_account_id end");
1459
1460         return ACCOUNT_ERROR_NONE;
1461 }
1462
1463 ACCOUNT_API int account_query_account_by_user_name(account_cb callback, const char* user_name, void* user_data)
1464 {
1465         _INFO("account_query_account_by_user_name starting");
1466         char* account_db_path = ACCOUNT_DB_NAME;
1467
1468         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1469         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
1470
1471         _GET_COOKIE
1472
1473         GError *error = NULL;
1474
1475         AccountManager* acc_mgr = _account_manager_get_instance();
1476         if (acc_mgr == NULL)
1477         {
1478                 _ERR("g_bus_get_sync failed");
1479                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1480         }
1481
1482         GVariant* account_list_variant = NULL;
1483         bool is_success = account_manager_call_account_query_account_by_user_name_sync(acc_mgr, account_db_path, user_name, e_cookie, &account_list_variant, NULL, &error);
1484
1485         int error_code = _account_get_error_code(is_success, error);
1486         if (error_code != ACCOUNT_ERROR_NONE)
1487         {
1488                 return error_code;
1489         }
1490
1491         _INFO("before unmarshal_account_list");
1492         GSList* account_list = unmarshal_account_list(account_list_variant);
1493         _INFO("after unmarshal_account_list");
1494         if (account_list == NULL)
1495         {
1496                 return ACCOUNT_ERROR_NO_DATA;
1497         }
1498
1499         GSList* iter;
1500
1501         for (iter = account_list; iter != NULL; iter = g_slist_next(iter))
1502         {
1503                 _INFO("iterating received account_list");
1504                 account_s *account = NULL;
1505                 account = (account_s*)iter->data;
1506                 if (callback((account_h)account, user_data) == false)
1507                 {
1508                         _INFO("application callback requested to discontinue.");
1509                         break;
1510                 }
1511         }
1512         _INFO("account_query_account_by_user_name end");
1513
1514         return ACCOUNT_ERROR_NONE;
1515 }
1516
1517 ACCOUNT_API int account_query_account_by_package_name(account_cb callback, const char *package_name, void *user_data)
1518 {
1519         _INFO("account_query_account_by_package_name starting");
1520         char* account_db_path = ACCOUNT_DB_NAME;
1521
1522         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1523         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
1524
1525         _GET_COOKIE
1526
1527         GError *error = NULL;
1528
1529         AccountManager* acc_mgr = _account_manager_get_instance();
1530         if (acc_mgr == NULL)
1531         {
1532                 _ERR("g_bus_get_sync failed");
1533                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1534         }
1535
1536         GVariant* account_list_variant = NULL;
1537         bool is_success = account_manager_call_account_query_account_by_package_name_sync(acc_mgr, account_db_path, package_name, e_cookie, &account_list_variant, NULL, &error);
1538         int error_code = _account_get_error_code(is_success, error);
1539         if (error_code != ACCOUNT_ERROR_NONE)
1540         {
1541                 return error_code;
1542         }
1543
1544         _INFO("before unmarshal_account_list");
1545         GSList* account_list = unmarshal_account_list(account_list_variant);
1546         _INFO("after unmarshal_account_list");
1547         if (account_list == NULL)
1548         {
1549                 return ACCOUNT_ERROR_NO_DATA;
1550         }
1551
1552         GSList* iter;
1553
1554         for (iter = account_list; iter != NULL; iter = g_slist_next(iter))
1555         {
1556                 _INFO("iterating received account_list");
1557                 account_s *account = NULL;
1558                 account = (account_s*)iter->data;
1559
1560                 if (callback((account_h)account, user_data) == false)
1561                 {
1562                         _INFO("application callback requested to discontinue.");
1563                         break;
1564                 }
1565         }
1566         _INFO("account_query_account_by_package_name end");
1567         return ACCOUNT_ERROR_NONE;
1568 }
1569
1570 ACCOUNT_API int account_query_account_by_capability(account_cb callback, const char* capability_type, account_capability_state_e capability_value, void *user_data)
1571 {
1572         _INFO("account_query_account_by_capability starting");
1573         char* account_db_path = ACCOUNT_DB_NAME;
1574
1575         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
1576
1577         if (((int)capability_value  < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
1578                 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1579                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1580         }
1581
1582         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
1583
1584         _GET_COOKIE
1585
1586         GError *error = NULL;
1587
1588         AccountManager* acc_mgr = _account_manager_get_instance();
1589         if (acc_mgr == NULL)
1590         {
1591                 _ERR("g_bus_get_sync failed");
1592                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1593         }
1594
1595         GVariant* account_list_variant = NULL;
1596         bool is_success = account_manager_call_account_query_account_by_capability_sync(acc_mgr, account_db_path, capability_type, capability_value, e_cookie, &account_list_variant, NULL, &error);
1597
1598         int error_code = _account_get_error_code(is_success, error);
1599         if (error_code != ACCOUNT_ERROR_NONE)
1600         {
1601                 return error_code;
1602         }
1603
1604         _INFO("before unmarshal_account_list");
1605         GSList* account_list = unmarshal_account_list(account_list_variant);
1606         _INFO("after unmarshal_account_list");
1607         if (account_list == NULL)
1608         {
1609                 return ACCOUNT_ERROR_NO_DATA;
1610         }
1611
1612         GSList* iter;
1613
1614         for (iter = account_list; iter != NULL; iter = g_slist_next(iter))
1615         {
1616                 _INFO("iterating received account_list");
1617                 account_s *account = NULL;
1618                 account = (account_s*)iter->data;
1619
1620                 if (callback((account_h)account, user_data) == false)
1621                 {
1622                         _INFO("application callback requested to discontinue.");
1623                         break;
1624                 }
1625         }
1626         _INFO("account_query_account_by_capability end");
1627         return ACCOUNT_ERROR_NONE;
1628 }
1629
1630 ACCOUNT_API int account_query_account_by_capability_type(account_cb callback, const char* capability_type, void* user_data)
1631 {
1632         _INFO("account_query_account_by_capability_type starting");
1633         char* account_db_path = ACCOUNT_DB_NAME;
1634
1635         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
1636         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
1637
1638         _GET_COOKIE
1639
1640         GError *error = NULL;
1641
1642         AccountManager* acc_mgr = _account_manager_get_instance();
1643         if (acc_mgr == NULL)
1644         {
1645                 _ERR("g_bus_get_sync failed");
1646                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1647         }
1648
1649         GVariant* account_list_variant = NULL;
1650         bool is_success = account_manager_call_account_query_account_by_capability_type_sync(acc_mgr, account_db_path, capability_type, e_cookie, &account_list_variant, NULL, &error);
1651
1652         int error_code = _account_get_error_code(is_success, error);
1653         if (error_code != ACCOUNT_ERROR_NONE)
1654         {
1655                 return error_code;
1656         }
1657
1658         _INFO("before unmarshal_account_list");
1659         GSList* account_list = unmarshal_account_list(account_list_variant);
1660         _INFO("after unmarshal_account_list");
1661         if (account_list == NULL)
1662         {
1663                 return ACCOUNT_ERROR_NO_DATA;
1664         }
1665
1666         GSList* iter;
1667
1668         for (iter = account_list; iter != NULL; iter = g_slist_next(iter))
1669         {
1670                 _INFO("iterating received account_list");
1671                 account_s *account = NULL;
1672                 account = (account_s*)iter->data;
1673
1674                 if (callback((account_h)account, user_data) == false)
1675                 {
1676                         _INFO("application callback requested to discontinue.");
1677                         break;
1678                 }
1679         }
1680         _INFO("account_query_account_by_capability end");
1681         return ACCOUNT_ERROR_NONE;
1682 }
1683
1684 ACCOUNT_API int account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data)
1685 {
1686         _INFO("account_query_capability_by_account_id starting");
1687         char* account_db_path = ACCOUNT_DB_NAME;
1688
1689         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1690         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1691
1692         _GET_COOKIE
1693
1694         GError *error = NULL;
1695         AccountManager* acc_mgr = _account_manager_get_instance();
1696         if (acc_mgr == NULL)
1697         {
1698                 _ERR("g_bus_get_sync failed");
1699                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1700         }
1701
1702         GVariant* capability_list_variant = NULL;
1703         bool is_success = account_manager_call_account_query_capability_by_account_id_sync(acc_mgr, account_db_path, account_id, e_cookie, &capability_list_variant, NULL, &error);
1704         int error_code = _account_get_error_code(is_success, error);
1705         if (error_code != ACCOUNT_ERROR_NONE)
1706         {
1707                 return error_code;
1708         }
1709
1710         _INFO("before unmarshal_capability_list");
1711         GSList* capability_list = unmarshal_capability_list(capability_list_variant);
1712         _INFO("after unmarshal_capability_list");
1713         if (capability_list == NULL)
1714         {
1715                 return ACCOUNT_ERROR_NO_DATA;
1716         }
1717
1718         GSList* iter;
1719
1720         for (iter = capability_list; iter != NULL; iter = g_slist_next(iter))
1721         {
1722                 _INFO("iterating received account_list");
1723                 account_capability_s *capability = NULL;
1724                 capability = (account_capability_s*)iter->data;
1725                 _INFO("");
1726                 if (callback(capability->type, capability->value, user_data) == false)
1727                 {
1728                         _INFO("application callback requested to discontinue.");
1729                         break;
1730                 }
1731                 _INFO("");
1732         }
1733         _INFO("account_query_capability_by_account_id end");
1734         return ACCOUNT_ERROR_NONE;
1735 }
1736
1737 static int _account_get_total_count(int *count, bool include_hidden)
1738 {
1739         _INFO("account_get_total_count_from_db starting");
1740         char* account_db_path = ACCOUNT_DB_NAME;
1741
1742         if (!count)
1743         {
1744                 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
1745                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1746         }
1747
1748         GError *error = NULL;
1749
1750         if (count == NULL)
1751         {
1752                 _INFO("Invalid input");
1753                 return -1;
1754         }
1755
1756         _GET_COOKIE
1757
1758         AccountManager* acc_mgr = _account_manager_get_instance();
1759         if (acc_mgr == NULL)
1760         {
1761                 _ERR("g_bus_get_sync failed");
1762                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1763         }
1764
1765         int temp_count = -1;
1766         bool is_success = account_manager_call_account_get_total_count_from_db_sync(acc_mgr, account_db_path, include_hidden, e_cookie, &temp_count, NULL, &error);
1767         int error_code = _account_get_error_code(is_success, error);
1768         if (error_code != ACCOUNT_ERROR_NONE)
1769         {
1770                 return error_code;
1771         }
1772
1773         *count = temp_count;
1774         _INFO("account_get_total_count_from_db end");
1775         return ACCOUNT_ERROR_NONE;
1776 }
1777
1778 ACCOUNT_API int account_get_total_count_from_db(int *count)
1779 {
1780         _INFO("account_get_total_count_from_db starting");
1781
1782         return _account_get_total_count(count, true);
1783 }
1784
1785 ACCOUNT_API int account_get_total_count_from_db_ex(int *count)
1786 {
1787         _INFO("account_get_total_count_from_db_ex starting");
1788
1789         return _account_get_total_count(count, false);
1790 }
1791
1792 ACCOUNT_API int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
1793 {
1794         _INFO("account_update_sync_status_by_id starting");
1795         char* account_db_path = ACCOUNT_DB_NAME;
1796
1797         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1798         if ( ((int)sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
1799                 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
1800                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1801         }
1802
1803         _GET_COOKIE
1804
1805         GError *error = NULL;
1806
1807         AccountManager* acc_mgr = _account_manager_get_instance();
1808         if (acc_mgr == NULL)
1809         {
1810                 _ERR("g_bus_get_sync failed");
1811                 return ACCOUNT_ERROR_PERMISSION_DENIED;
1812         }
1813
1814         bool is_success = account_manager_call_account_update_sync_status_by_id_sync(acc_mgr, account_db_path, account_db_id, sync_status, e_cookie, NULL, &error);
1815         return _account_get_error_code(is_success, error);
1816 }
1817
1818 static int _account_type_free_label_items(label_s *data)
1819 {
1820         _ACCOUNT_FREE(data->app_id);
1821         _ACCOUNT_FREE(data->label);
1822         _ACCOUNT_FREE(data->locale);
1823
1824         return ACCOUNT_ERROR_NONE;
1825 }
1826
1827 static int _account_type_gslist_free(GSList* list)
1828 {
1829         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
1830
1831         GSList* iter;
1832
1833         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
1834                 label_s *label_data = (label_s*)iter->data;
1835                 _account_type_free_label_items(label_data);
1836                 _ACCOUNT_FREE(label_data);
1837         }
1838
1839         g_slist_free(list);
1840         list = NULL;
1841
1842         return ACCOUNT_ERROR_NONE;
1843 }
1844
1845 static int _account_type_item_free(account_type_s *data)
1846 {
1847         _ACCOUNT_FREE(data->app_id);
1848         _ACCOUNT_FREE(data->service_provider_id);
1849         _ACCOUNT_FREE(data->icon_path);
1850         _ACCOUNT_FREE(data->small_icon_path);
1851
1852         return ACCOUNT_ERROR_NONE;
1853 }
1854
1855 static int _account_type_glist_free(GList* list)
1856 {
1857         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
1858
1859         GList* iter;
1860
1861         for (iter = list; iter != NULL; iter = g_list_next(iter)) {
1862                 account_type_s *account_type_record = (account_type_s*)iter->data;
1863                 _account_type_item_free(account_type_record);
1864                 _ACCOUNT_FREE(account_type_record);
1865         }
1866
1867         g_list_free(list);
1868         list = NULL;
1869
1870         return ACCOUNT_ERROR_NONE;
1871 }
1872
1873 static int _account_type_free_account_type_items(account_type_s *data)
1874 {
1875         _account_type_item_free(data);
1876
1877         _account_type_gslist_free(data->label_list);
1878         _account_type_glist_free(data->account_type_list);
1879
1880         return ACCOUNT_ERROR_NONE;
1881 }
1882
1883 ACCOUNT_API int account_type_create(account_type_h *account_type)
1884 {
1885         if (!account_type) {
1886                 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
1887                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1888         }
1889
1890         account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
1891
1892         if (data == NULL)
1893         {
1894                 ACCOUNT_ERROR("Memory Allocation Failed");
1895                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1896         }
1897
1898         ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
1899
1900         data->id = -1;
1901         data->app_id = NULL;
1902         data->service_provider_id = NULL;
1903         data->icon_path = NULL;
1904         data->small_icon_path = NULL;
1905         data->multiple_account_support = false;
1906         data->label_list = NULL;
1907         data->account_type_list = NULL;
1908         data->provider_feature_list = NULL;
1909
1910         *account_type = (account_type_h)data;
1911
1912         return ACCOUNT_ERROR_NONE;
1913 }
1914
1915 ACCOUNT_API int account_type_destroy(account_type_h account_type)
1916 {
1917         _INFO("account_type_destroy");
1918
1919         account_type_s *data = (account_type_s*)account_type;
1920
1921         if (data == NULL)
1922         {
1923                 _ERR("Account type handle is null!");
1924                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1925         }
1926
1927         _account_type_free_account_type_items(data);
1928         _ACCOUNT_FREE(data);
1929
1930         return ACCOUNT_ERROR_NONE;
1931 }
1932
1933 ACCOUNT_API int account_type_set_app_id(account_type_h account_type, const char *app_id)
1934 {
1935         return ACCOUNT_ERROR_NONE;
1936 }
1937
1938 ACCOUNT_API int account_type_set_app_id_internal(account_type_h account_type, const char *app_id)
1939 {
1940         if (!account_type) {
1941                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
1942                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1943         }
1944
1945         if (!app_id) {
1946                 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
1947                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1948         }
1949
1950         account_type_s *data = (account_type_s*)account_type;
1951
1952         _ACCOUNT_FREE(data->app_id);
1953         data->app_id = _account_get_text(app_id);
1954
1955         return ACCOUNT_ERROR_NONE;
1956 }
1957
1958 ACCOUNT_API int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
1959 {
1960         return ACCOUNT_ERROR_NONE;
1961 }
1962
1963 ACCOUNT_API int account_type_set_service_provider_id_internal(account_type_h account_type, const char *service_provider_id)
1964 {
1965         if (!account_type) {
1966                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1967         }
1968
1969         if (!service_provider_id) {
1970                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1971         }
1972
1973         account_type_s *data = (account_type_s*)account_type;
1974
1975         _ACCOUNT_FREE(data->service_provider_id);
1976         data->service_provider_id = _account_get_text(service_provider_id);
1977
1978         return ACCOUNT_ERROR_NONE;
1979 }
1980
1981 ACCOUNT_API int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
1982 {
1983         return ACCOUNT_ERROR_NONE;
1984 }
1985
1986 ACCOUNT_API int account_type_set_icon_path_internal(account_type_h account_type, const char *icon_path)
1987 {
1988         if (!account_type) {
1989                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1990         }
1991
1992         if (!icon_path) {
1993                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1994         }
1995
1996         account_type_s *data = (account_type_s*)account_type;
1997
1998         _ACCOUNT_FREE(data->icon_path);
1999         data->icon_path = _account_get_text(icon_path);
2000
2001         return ACCOUNT_ERROR_NONE;
2002 }
2003
2004 ACCOUNT_API int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
2005 {
2006         return ACCOUNT_ERROR_NONE;
2007 }
2008
2009 ACCOUNT_API int account_type_set_small_icon_path_internal(account_type_h account_type, const char *small_icon_path)
2010 {
2011         if (!account_type) {
2012                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2013         }
2014
2015         if (!small_icon_path) {
2016                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2017         }
2018
2019         account_type_s *data = (account_type_s*)account_type;
2020
2021         _ACCOUNT_FREE(data->small_icon_path);
2022         data->small_icon_path = _account_get_text(small_icon_path);
2023
2024         return ACCOUNT_ERROR_NONE;
2025 }
2026
2027 ACCOUNT_API int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
2028 {
2029         return ACCOUNT_ERROR_NONE;
2030 }
2031
2032 ACCOUNT_API int account_type_set_multiple_account_support_internal(account_type_h account_type, const bool multiple_account_support)
2033 {
2034         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
2035
2036         account_type_s *data = (account_type_s*)account_type;
2037
2038         data->multiple_account_support = multiple_account_support;
2039
2040         return ACCOUNT_ERROR_NONE;
2041 }
2042
2043 ACCOUNT_API int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
2044 {
2045         return ACCOUNT_ERROR_NONE;
2046 }
2047
2048 ACCOUNT_API int account_type_set_label_internal(account_type_h account_type, const char* label, const char* locale)
2049 {
2050         if (!account_type) {
2051                 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
2052                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2053         }
2054
2055         if(!label || !locale) {
2056                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2057         }
2058
2059         account_type_s *data = (account_type_s*)account_type;
2060         label_s *label_data = (label_s*)malloc(sizeof(label_s));
2061
2062         if (label_data == NULL) {
2063                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2064         }
2065         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
2066
2067         label_data->label = _account_get_text(label);
2068         label_data->locale = _account_get_text(locale);
2069
2070         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
2071
2072         return ACCOUNT_ERROR_NONE;
2073 }
2074
2075 ACCOUNT_API int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
2076 {
2077         return ACCOUNT_ERROR_NONE;
2078 }
2079
2080 ACCOUNT_API int account_type_set_provider_feature_internal(account_type_h account_type, const char* provider_feature)
2081 {
2082         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
2083         ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
2084
2085         account_type_s *data = (account_type_s*)account_type;
2086
2087         GSList *iter = NULL;
2088         bool b_is_new = TRUE;
2089
2090         for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
2091                 provider_feature_s *feature_data = NULL;
2092                 feature_data = (provider_feature_s*)iter->data;
2093
2094                 if(!strcmp(feature_data->key, provider_feature)) {
2095                         b_is_new = FALSE;
2096                         break;
2097                 }
2098         }
2099
2100         if(b_is_new) {
2101                 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
2102
2103                 if (feature_data == NULL)
2104                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
2105                 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
2106
2107                 feature_data->key = _account_get_text(provider_feature);
2108                 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
2109         }
2110
2111         return ACCOUNT_ERROR_NONE;
2112 }
2113
2114 ACCOUNT_API int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
2115 {
2116         _INFO("account_type_query_provider_feature_by_app_id start");
2117         char* account_db_path = ACCOUNT_DB_NAME;
2118
2119         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2120         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2121
2122         _GET_COOKIE
2123
2124         GError *error = NULL;
2125         gint error_code = ACCOUNT_ERROR_NONE;
2126
2127         AccountManager* acc_mgr = _account_manager_get_instance();
2128         if (acc_mgr == NULL)
2129         {
2130                 _ERR("g_bus_get_sync failed");
2131                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2132         }
2133
2134         GVariant* feature_list_variant = NULL;
2135         bool is_success = account_manager_call_account_type_query_provider_feature_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &feature_list_variant, NULL, &error);
2136         _INFO("account_manager_call_account_type_query_provider_feature_by_app_id_sync end=[%d]", is_success);
2137
2138         if (!is_success)
2139         {
2140                 error_code = error->code;
2141                 _ERR("Account IPC call returned error[%d]", error_code);
2142                 return error_code;
2143         }
2144
2145         GSList* provider_feature_list = variant_to_provider_feature_list(feature_list_variant);
2146         if (provider_feature_list == NULL)
2147         {
2148                 error_code = ACCOUNT_ERROR_NO_DATA;
2149                 _ERR("[%d]", error_code);
2150                 return error_code;
2151         }
2152
2153         GSList *iter;
2154         for (iter = provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
2155                 provider_feature_s *feature_data = NULL;
2156
2157                 feature_data = (provider_feature_s*)iter->data;
2158
2159                 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
2160                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
2161                         return ACCOUNT_ERROR_NONE;
2162                 }
2163         }
2164
2165         _INFO("account_type_query_provider_feature_by_app_id end");
2166         return error_code;
2167 }
2168
2169 ACCOUNT_API bool account_type_query_supported_feature(const char* app_id, const char* capability)
2170 {
2171         _INFO("account_type_query_supported_feature start");
2172         char* account_db_path = ACCOUNT_DB_NAME;
2173
2174         if (app_id == NULL || capability == NULL)
2175         {
2176                 set_last_result(ACCOUNT_ERROR_INVALID_PARAMETER);
2177                 return false;
2178         }
2179
2180         _GET_COOKIE
2181
2182         int is_supported = 0;
2183         GError *error = NULL;
2184         gint ret = ACCOUNT_ERROR_NONE;
2185
2186         AccountManager* acc_mgr = _account_manager_get_instance();
2187         if (acc_mgr == NULL)
2188         {
2189                 _ERR("g_bus_get_sync failed");
2190                 set_last_result(ACCOUNT_ERROR_PERMISSION_DENIED);
2191                 return false;
2192         }
2193
2194         bool is_success = account_manager_call_account_type_query_supported_feature_sync(acc_mgr, account_db_path, app_id, capability, e_cookie, &is_supported, NULL, &error);
2195         _INFO("account_manager_call_account_type_query_supported_feature_sync end=[%d]", is_success);
2196
2197         if (!is_success)
2198         {
2199                 ret = error->code;
2200                 _ERR("Account IPC call returned error[%d]", ret);
2201                 set_last_result(ret);
2202                 return false;
2203         }
2204
2205         set_last_result(ACCOUNT_ERROR_NONE);
2206         _INFO("account_type_query_supported_feature end");
2207         return is_supported;
2208 }
2209
2210 ACCOUNT_API int account_type_get_app_id(account_type_h account_type, char **app_id)
2211 {
2212         if (!account_type) {
2213                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2214         }
2215
2216         if (!app_id) {
2217                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2218         }
2219
2220         account_type_s *data = (account_type_s*)account_type;
2221
2222         (*app_id) = NULL;
2223         *app_id = _account_get_text(data->app_id);
2224
2225         return ACCOUNT_ERROR_NONE;
2226 }
2227
2228 ACCOUNT_API int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
2229 {
2230         if (!account_type) {
2231                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2232         }
2233
2234         if (!service_provider_id) {
2235                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2236         }
2237
2238         account_type_s *data = (account_type_s*)account_type;
2239
2240         (*service_provider_id) = NULL;
2241         *service_provider_id = _account_get_text(data->service_provider_id);
2242
2243         return ACCOUNT_ERROR_NONE;
2244 }
2245
2246 ACCOUNT_API int account_type_get_icon_path(account_type_h account_type, char **icon_path)
2247 {
2248         if (!account_type) {
2249                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2250         }
2251
2252         if (!icon_path) {
2253                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2254         }
2255
2256         account_type_s *data = (account_type_s*)account_type;
2257
2258         (*icon_path) = NULL;
2259         *icon_path = _account_get_text(data->icon_path);
2260
2261         return ACCOUNT_ERROR_NONE;
2262 }
2263
2264 ACCOUNT_API int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
2265 {
2266         if (!account_type) {
2267                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2268         }
2269
2270         if (!small_icon_path) {
2271                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2272         }
2273
2274         account_type_s *data = (account_type_s*)account_type;
2275
2276         (*small_icon_path) = NULL;
2277         *small_icon_path = _account_get_text(data->small_icon_path);
2278
2279         return ACCOUNT_ERROR_NONE;
2280 }
2281
2282 ACCOUNT_API int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
2283 {
2284         if (!account_type) {
2285                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2286         }
2287         if (!multiple_account_support) {
2288                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2289         }
2290
2291         account_type_s *data = (account_type_s*)account_type;
2292
2293         *multiple_account_support = data->multiple_account_support;
2294
2295         return ACCOUNT_ERROR_NONE;
2296 }
2297
2298 ACCOUNT_API int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
2299 {
2300         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2301         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2302
2303         GSList *iter;
2304         account_type_s *data = (account_type_s*)account_type;
2305
2306         for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
2307                 provider_feature_s *feature_data = NULL;
2308
2309                 feature_data = (provider_feature_s*)iter->data;
2310
2311                 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
2312                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
2313                         return ACCOUNT_ERROR_NONE;
2314                 }
2315         }
2316
2317         return ACCOUNT_ERROR_NONE;
2318 }
2319
2320 ACCOUNT_API int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
2321 {
2322         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2323         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
2324
2325         GSList *iter;
2326         account_type_s *data = (account_type_s*)account_type;
2327
2328         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
2329                 label_s *label_data = NULL;
2330
2331                 label_data = (label_s*)iter->data;
2332
2333                 *label = NULL;
2334
2335                 if(!strcmp(locale, label_data->locale)) {
2336                         *label = _account_get_text(label_data->label);
2337                         return ACCOUNT_ERROR_NONE;
2338                 }
2339                 gchar** tokens = g_strsplit(locale, "-", 2);
2340                 if(tokens != NULL) {
2341                         if((char*)(tokens[1]) != NULL) {
2342                                 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
2343                                 if(upper_token != NULL) {
2344                                         char* converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
2345                                         if(!strcmp(converted_locale, label_data->locale)) {
2346                                                 _ACCOUNT_FREE(converted_locale);
2347                                                 _ACCOUNT_FREE(upper_token);
2348                                                 g_strfreev(tokens);
2349                                                 *label = _account_get_text(label_data->label);
2350                                                 return ACCOUNT_ERROR_NONE;
2351                                         }
2352                                         _ACCOUNT_FREE(converted_locale);
2353                                 }
2354                                 _ACCOUNT_FREE(upper_token);
2355                         }
2356                 }
2357                 g_strfreev(tokens);
2358         }
2359
2360         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2361 }
2362
2363 ACCOUNT_API int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
2364 {
2365         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2366         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2367
2368         GSList *iter;
2369         account_type_s *data = (account_type_s*)account_type;
2370
2371         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
2372                 label_s *label_data = NULL;
2373
2374                 label_data = (label_s*)iter->data;
2375
2376                 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
2377                         ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
2378                         return ACCOUNT_ERROR_NONE;
2379                 }
2380         }
2381
2382         return ACCOUNT_ERROR_NONE;
2383 }
2384
2385 ACCOUNT_API int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
2386 {
2387         return ACCOUNT_ERROR_NONE;
2388 }
2389
2390 ACCOUNT_API int account_type_insert_to_db_internal(account_type_h account_type, int* account_type_id)
2391 {
2392         _INFO("account_type_insert_to_db starting");
2393         char* account_db_path = ACCOUNT_DB_NAME;
2394
2395         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
2396         ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
2397
2398         _GET_COOKIE
2399
2400         GError *error = NULL;
2401
2402         AccountManager* acc_mgr = _account_manager_get_instance();
2403         if (acc_mgr == NULL)
2404         {
2405                 _ERR("g_bus_get_sync failed");
2406                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2407         }
2408
2409         int db_id = -1;
2410         GVariant* account_type_serialized = marshal_account_type((account_type_s*) account_type);
2411         bool is_success = account_manager_call_account_type_add_sync(acc_mgr, account_db_path, account_type_serialized, e_cookie, &db_id, NULL, &error);
2412         int ret = _account_get_error_code(is_success, error);
2413         if (ret != ACCOUNT_ERROR_NONE)
2414         {
2415                 return ret;
2416         }
2417
2418         _INFO("account_type_insert_to_db end id=[%d]", db_id);
2419
2420         *account_type_id = db_id;
2421
2422         account_type_s* account_type_data = (account_type_s*)account_type;
2423         account_type_data->id = db_id;
2424
2425         return ACCOUNT_ERROR_NONE;
2426 }
2427
2428 ACCOUNT_API int account_type_update_to_db_by_app_id(const account_type_h account_type, const char* app_id)
2429 {
2430         return ACCOUNT_ERROR_NONE;
2431 }
2432
2433 ACCOUNT_API int account_type_update_to_db_by_app_id_internal(const account_type_h account_type, const char* app_id)
2434 {
2435         _INFO("account_type_update_to_db_by_app_id starting");
2436         char* account_db_path = ACCOUNT_DB_NAME;
2437
2438         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2439         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2440
2441         _GET_COOKIE
2442
2443         GError *error = NULL;
2444
2445         AccountManager* acc_mgr = _account_manager_get_instance();
2446         if (acc_mgr == NULL)
2447         {
2448                 _ERR("g_bus_get_sync failed");
2449                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2450         }
2451
2452         GVariant* account_type_variant = marshal_account_type((account_type_s*) account_type);
2453         if (account_type_variant == NULL)
2454         {
2455                 _ERR("Failed to serialize");
2456                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2457         }
2458
2459         bool is_success = account_manager_call_account_type_update_to_db_by_app_id_sync(acc_mgr, account_db_path, account_type_variant, app_id, e_cookie, NULL, &error);
2460         return _account_get_error_code(is_success, error);
2461 }
2462
2463 ACCOUNT_API int account_type_delete_by_app_id(const char* app_id)
2464 {
2465         return ACCOUNT_ERROR_NONE;
2466 }
2467
2468 ACCOUNT_API int account_type_delete_by_app_id_internal(const char* app_id)
2469 {
2470         _INFO("account_type_delete_by_app_id starting");
2471         char* account_db_path = ACCOUNT_DB_NAME;
2472
2473         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2474
2475         _GET_COOKIE
2476
2477         GError *error = NULL;
2478
2479         AccountManager* acc_mgr = _account_manager_get_instance();
2480         if (acc_mgr == NULL)
2481         {
2482                 _ERR("g_bus_get_sync failed");
2483                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2484         }
2485
2486         bool is_success = account_manager_call_account_type_delete_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, NULL, &error);
2487         return _account_get_error_code(is_success, error);
2488 }
2489
2490 ACCOUNT_API int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
2491 {
2492         _INFO("account_type_query_label_by_app_id starting");
2493         char* account_db_path = ACCOUNT_DB_NAME;
2494
2495         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL"));
2496         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2497
2498         _GET_COOKIE
2499
2500         GError *error = NULL;
2501
2502         AccountManager* acc_mgr = _account_manager_get_instance();
2503         if (acc_mgr == NULL)
2504         {
2505                 _ERR("g_bus_get_sync failed");
2506                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2507         }
2508
2509         GVariant* label_list_variant = NULL;
2510         bool is_success = account_manager_call_account_type_query_label_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &label_list_variant, NULL, &error);
2511         int ret = _account_get_error_code(is_success, error);
2512         if (ret != ACCOUNT_ERROR_NONE)
2513         {
2514                 return ret;
2515         }
2516
2517         _INFO("before variant_to_label_list");
2518         GSList* label_list = variant_to_label_list (label_list_variant);
2519         _INFO("after variant_to_label_list");
2520         if (label_list == NULL)
2521         {
2522                 return ACCOUNT_ERROR_NO_DATA;
2523         }
2524
2525         GSList* iter;
2526
2527         for (iter = label_list; iter != NULL; iter = g_slist_next(iter))
2528         {
2529                 _INFO("iterating received account_list");
2530                 label_s *label_record = NULL;
2531                 label_record = (label_s*)iter->data;
2532                 _INFO("");
2533                 if (callback(label_record->app_id, label_record->label, label_record->locale, user_data) == false)
2534                 {
2535                         _INFO("application callback requested to discontinue.");
2536                         break;
2537                 }
2538                 _INFO("");
2539         }
2540         _INFO("account_type_query_label_by_app_id end");
2541         return ACCOUNT_ERROR_NONE;
2542 }
2543
2544 ACCOUNT_API int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
2545 {
2546         _INFO("account_type_query_by_app_id starting");
2547         char* account_db_path = ACCOUNT_DB_NAME;
2548
2549         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2550         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE'S POINTER IS NULL"));
2551         ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
2552
2553         _GET_COOKIE
2554
2555         GError *error = NULL;
2556
2557         AccountManager* acc_mgr = _account_manager_get_instance();
2558         if (acc_mgr == NULL)
2559         {
2560                 _ERR("g_bus_get_sync failed");
2561                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2562         }
2563
2564         GVariant* account_type_variant = NULL;
2565         account_type_s *in_data = (account_type_s*) (*account_type);
2566
2567         bool is_success = account_manager_call_account_type_query_by_app_id_sync(acc_mgr, account_db_path, app_id, e_cookie, &account_type_variant, NULL, &error);
2568         int ret = _account_get_error_code(is_success, error);
2569         if (ret != ACCOUNT_ERROR_NONE)
2570         {
2571                 return ret;
2572         }
2573
2574         _INFO("before umarshal_account_type");
2575         account_type_s* received_account_type = umarshal_account_type (account_type_variant);
2576         _INFO("after umarshal_account_type");
2577         ACCOUNT_RETURN_VAL((received_account_type != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("INVALID DATA RECEIVED FROM SVC"));
2578
2579         in_data->id = received_account_type->id;
2580         in_data->app_id = received_account_type->app_id;
2581         in_data->service_provider_id = received_account_type->service_provider_id;
2582         in_data->icon_path = received_account_type->icon_path;
2583         in_data->small_icon_path = received_account_type->small_icon_path;
2584         in_data->multiple_account_support = received_account_type->multiple_account_support;
2585         in_data->label_list = received_account_type->label_list;
2586         in_data->account_type_list = received_account_type->account_type_list;
2587         in_data->provider_feature_list = received_account_type->provider_feature_list;
2588
2589         _INFO("account_type_query_by_app_id end");
2590         return ACCOUNT_ERROR_NONE;
2591 }
2592
2593 ACCOUNT_API int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
2594 {
2595         _INFO("account_type_foreach_account_type_from_db starting");
2596         char* account_db_path = ACCOUNT_DB_NAME;
2597
2598         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT Callback IS NULL"));
2599
2600         _GET_COOKIE
2601
2602         GError *error = NULL;
2603
2604         AccountManager* acc_mgr = _account_manager_get_instance();
2605         if (acc_mgr == NULL)
2606         {
2607                 _ERR("g_bus_get_sync failed");
2608                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2609         }
2610
2611         GVariant* account_type_list_variant = NULL;
2612         _INFO("before account_type_query_all_sync()");
2613         bool is_success = account_manager_call_account_type_query_all_sync(acc_mgr, account_db_path, e_cookie, &account_type_list_variant, NULL, &error);
2614         _INFO("after account_type_query_all_sync()");
2615         int ret = _account_get_error_code(is_success, error);
2616         if (ret != ACCOUNT_ERROR_NONE)
2617         {
2618                 return ret;
2619         }
2620
2621         _INFO("before unmarshal_account_type_list");
2622         GSList* account_type_list = unmarshal_account_type_list(account_type_list_variant);
2623         _INFO("after unmarshal_account_type_list");
2624         if (account_type_list == NULL)
2625         {
2626                 return ACCOUNT_ERROR_NO_DATA;
2627         }
2628
2629         GSList* iter;
2630
2631         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter))
2632         {
2633                 _INFO("iterating received account_list");
2634                 account_type_s *account_type = NULL;
2635                 account_type = (account_type_s*)iter->data;
2636
2637                 if (callback((account_type_h)account_type, user_data) == false)
2638                 {
2639                         _INFO("application callback requested to discontinue.");
2640                         break;
2641                 }
2642         }
2643         _INFO("account_foreach_account_from_db end");
2644         return ACCOUNT_ERROR_NONE;
2645 }
2646
2647 ACCOUNT_API int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
2648 {
2649         _INFO("account_type_query_label_by_locale starting");
2650         char* account_db_path = ACCOUNT_DB_NAME;
2651
2652         ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
2653         ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO LOCALE"));
2654         ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
2655
2656         _GET_COOKIE
2657
2658         GError *error = NULL;
2659
2660         AccountManager* acc_mgr = _account_manager_get_instance();
2661         if (acc_mgr == NULL)
2662         {
2663                 _ERR("g_bus_get_sync failed");
2664                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2665         }
2666
2667         char* label_temp = NULL;
2668         _INFO("before account_type_query_label_by_locale_sync()");
2669         bool is_success = account_manager_call_account_type_query_label_by_locale_sync(acc_mgr, account_db_path, app_id, locale, e_cookie, &label_temp, NULL, &error);
2670         _INFO("after account_type_query_label_by_locale_sync() : is_success=%d", is_success);
2671         int ret = _account_get_error_code(is_success, error);
2672         if (ret != ACCOUNT_ERROR_NONE)
2673         {
2674                 return ret;
2675         }
2676
2677         if (label_temp == NULL)
2678         {
2679                 return ACCOUNT_ERROR_NO_DATA;
2680         }
2681
2682         *label = _account_get_text(label_temp);
2683         _INFO("account_type_query_label_by_locale end");
2684         return ACCOUNT_ERROR_NONE;
2685
2686 }
2687
2688 ACCOUNT_API int account_type_query_by_provider_feature(account_type_cb callback, const char* key, void* user_data)
2689 {
2690         _INFO("account_type_query_by_provider_feature starting");
2691         char* account_db_path = ACCOUNT_DB_NAME;
2692
2693         ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
2694         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2695
2696         _GET_COOKIE
2697
2698         GError *error = NULL;
2699
2700         AccountManager* acc_mgr = _account_manager_get_instance();
2701         if (acc_mgr == NULL)
2702         {
2703                 _ERR("g_bus_get_sync failed");
2704                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2705         }
2706
2707         GVariant* account_type_list_variant = NULL;
2708         bool is_success = account_manager_call_account_type_query_by_provider_feature_sync(acc_mgr, account_db_path, key, e_cookie, &account_type_list_variant, NULL, &error);
2709         int ret = _account_get_error_code(is_success, error);
2710         if (ret != ACCOUNT_ERROR_NONE)
2711         {
2712                 return ret;
2713         }
2714
2715         _INFO("before unmarshal_account_type_list");
2716         GSList* account_type_list = unmarshal_account_type_list(account_type_list_variant);
2717         _INFO("after unmarshal_account_type_list");
2718         if (account_type_list == NULL)
2719         {
2720                 return ACCOUNT_ERROR_NO_DATA;
2721         }
2722
2723         GSList* iter;
2724
2725         for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter))
2726         {
2727                 _INFO("iterating received account_type_list");
2728                 account_type_s *account_type = NULL;
2729                 account_type = (account_type_s*)iter->data;
2730                 _INFO("");
2731                 if (callback((account_type_h)account_type, user_data) == false)
2732                 {
2733                         _INFO("Application callback requested not to continue");
2734                         break;
2735                 }
2736                 _INFO("");
2737         }
2738         _INFO("account_type_query_by_provider_feature end");
2739         return ACCOUNT_ERROR_NONE;
2740 }
2741
2742 ACCOUNT_API int account_type_query_app_id_exist(const char* app_id)
2743 {
2744         _INFO("account_type_query_app_id_exist starting");
2745         char* account_db_path = ACCOUNT_DB_NAME;
2746
2747         ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
2748
2749         _GET_COOKIE
2750
2751         GError *error = NULL;
2752
2753         AccountManager* acc_mgr = _account_manager_get_instance();
2754         if (acc_mgr == NULL)
2755         {
2756                 _ERR("g_bus_get_sync failed");
2757                 return ACCOUNT_ERROR_PERMISSION_DENIED;
2758         }
2759
2760         bool is_success = account_manager_call_account_type_query_app_id_exist_sync(acc_mgr, account_db_path, app_id, e_cookie, NULL, &error);
2761
2762         return _account_get_error_code(is_success, error);
2763 }
2764
2765
2766 static void _account_subscribe_vconf_callback(keynode_t *key, void *user_data)
2767 {
2768         account_subscribe_s* tmp = (account_subscribe_s*)user_data;
2769         char *msg = NULL, *vconf_key = NULL;
2770         char event_msg[256] ={0, };
2771         int account_id = -1;
2772
2773         if(!key) {
2774                 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
2775                 return;
2776         }
2777
2778         if(!tmp) {
2779                 ACCOUNT_ERROR("user data required\n");
2780                 return;
2781         }
2782
2783         if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
2784         {
2785                 vconf_key = vconf_keynode_get_str(key);
2786
2787                 if( vconf_key == NULL){
2788                         ACCOUNT_ERROR("vconf key is NULL.\n");
2789                         return;
2790                 }
2791                 msg = strdup(vconf_key);
2792
2793                 char* event_type = NULL;
2794                 char* id = NULL;
2795                 char* ptr = NULL;
2796
2797                 event_type = strtok_r(msg, ":", &ptr);
2798                 id = strtok_r(NULL, ":", &ptr);
2799
2800                 ACCOUNT_SLOGD("msg(%s), event_type(%s), id(%s)", msg, event_type, id);
2801
2802                 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
2803
2804                 account_id = atoi(id);
2805
2806                 if(tmp->account_subscription_callback)
2807                         tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
2808         }
2809
2810         _ACCOUNT_FREE(msg);
2811
2812 }
2813
2814 ACCOUNT_API int account_subscribe_create(account_subscribe_h* account_subscribe)
2815 {
2816         if (!account_subscribe) {
2817                 ACCOUNT_SLOGE("account is NULL.\n", __FUNCTION__, __LINE__);
2818                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2819         }
2820
2821         account_subscribe_s *data = (account_subscribe_s*)calloc(1,sizeof(account_subscribe_s));
2822
2823         if(!data) {
2824                 ACCOUNT_FATAL("OUT OF MEMORY\n");
2825                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2826         }
2827
2828         *account_subscribe = (account_subscribe_h)data;
2829
2830         return ACCOUNT_ERROR_NONE;
2831 }
2832
2833 ACCOUNT_API int account_subscribe_notification(account_subscribe_h account_subscribe, account_event_cb callback, void* user_data)
2834 {
2835         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
2836
2837         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
2838
2839         tmp->account_subscription_callback = callback;
2840         tmp->user_data = user_data;
2841
2842         int ret = -1;
2843         ret = vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
2844                                 (vconf_callback_fn)_account_subscribe_vconf_callback,
2845                                 (void*)tmp);
2846
2847         ACCOUNT_SLOGI("vconf_notify_key_changed ret = %d", ret);
2848
2849         if(ret != VCONF_OK) {
2850                 ACCOUNT_ERROR("Vconf Subscription Failed ret = %d", ret);
2851                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
2852         }
2853
2854         return ACCOUNT_ERROR_NONE;
2855 }
2856
2857 ACCOUNT_API int account_unsubscribe_notification(account_subscribe_h account_subscribe)
2858 {
2859         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
2860
2861         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
2862
2863         _ACCOUNT_FREE(tmp);
2864
2865         if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
2866            (vconf_callback_fn)_account_subscribe_vconf_callback) != 0) {
2867                 ACCOUNT_ERROR("Vconf Subscription Failed !!!!!\n");
2868                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
2869         }
2870
2871         return ACCOUNT_ERROR_NONE;
2872 }
2873
2874 static void _account_subscribe_vconf_callback_ex(keynode_t *key, void *user_data)
2875 {
2876         account_subscribe_s* tmp = (account_subscribe_s*)user_data;
2877         char *msg = NULL, *vconf_key = NULL;
2878         char event_msg[256] ={0, };
2879         int account_id = -1;
2880
2881         if(!key) {
2882                 ACCOUNT_ERROR("No subscribtion msg !!!!!\n");
2883                 return;
2884         }
2885
2886         if(!tmp) {
2887                 ACCOUNT_ERROR("user data required\n");
2888                 return;
2889         }
2890
2891         if(!memcmp(vconf_keynode_get_name(key), VCONFKEY_ACCOUNT_MSG_STR, strlen(VCONFKEY_ACCOUNT_MSG_STR)))
2892         {
2893                 vconf_key = vconf_keynode_get_str(key);
2894
2895                 if( vconf_key == NULL){
2896                         ACCOUNT_ERROR("vconf key is NULL.\n");
2897                         return;
2898                 }
2899                 msg = strdup(vconf_key);
2900
2901                 char* event_type = NULL;
2902                 char* id = NULL;
2903                 char* ptr = NULL;
2904
2905                 event_type = strtok_r(msg, ":", &ptr);
2906                 id = strtok_r(NULL, ":", &ptr);
2907
2908                 ACCOUNT_SLOGD("msg(%s), event_type(%s), id(%s)", msg, event_type, id);
2909
2910                 ACCOUNT_SNPRINTF(event_msg,sizeof(event_msg),"%s", event_type);
2911
2912                 account_id = atoi(id);
2913
2914                 if(tmp->account_subscription_callback)
2915                         tmp->account_subscription_callback(event_msg, account_id, tmp->user_data);
2916         }
2917
2918         _ACCOUNT_FREE(msg);
2919
2920 }
2921
2922 ACCOUNT_API int account_unsubscribe_notification_ex(account_subscribe_h account_subscribe)
2923 {
2924         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
2925
2926         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
2927
2928         _ACCOUNT_FREE(tmp);
2929
2930         if (vconf_ignore_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
2931            (vconf_callback_fn)_account_subscribe_vconf_callback_ex) != 0) {
2932                 ACCOUNT_ERROR("Vconf Subscription Failed !!!!!\n");
2933                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
2934         }
2935
2936         return ACCOUNT_ERROR_NONE;
2937 }
2938
2939 ACCOUNT_API int account_subscribe_notification_ex(account_subscribe_h account_subscribe, account_event_cb callback, void* user_data)
2940 {
2941         ACCOUNT_RETURN_VAL((account_subscribe != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account subscribe handle is NULL.\n",  __FUNCTION__, __LINE__));
2942
2943         account_subscribe_s* tmp =(account_subscribe_s*)account_subscribe;
2944
2945         tmp->account_subscription_callback = callback;
2946         tmp->user_data = user_data;
2947
2948         int ret = -1;
2949         ret = vconf_notify_key_changed(VCONFKEY_ACCOUNT_MSG_STR,
2950                                 (vconf_callback_fn)_account_subscribe_vconf_callback_ex,
2951                                 (void*)tmp);
2952
2953         ACCOUNT_SLOGI("vconf_notify_key_changed ret = %d", ret);
2954
2955         if(ret != VCONF_OK) {
2956                 ACCOUNT_ERROR("Vconf Subscription Failed ret = %d", ret);
2957                 return ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL;
2958         }
2959
2960         return ACCOUNT_ERROR_NONE;
2961 }