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