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