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