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