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