Applied cynara in account's Daemon(remove security-server).
[platform/core/account/account-manager.git] / src / accounts / server / account-server.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 #define _DEFAULT_SOURCE
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <signal.h>
23 #include <glib.h>
24 #if !GLIB_CHECK_VERSION (2, 31, 0)
25 #include <glib/gmacros.h>
26 #endif
27 #include <cynara-client.h>
28 #include <cynara-session.h>
29 #include <cynara-creds-gdbus.h>
30
31 #include <gio/gio.h>
32
33 #include "dbg.h"
34 #include "account-server-db.h"
35 #include "account_ipc_marshal.h"
36 #include "account-mgr-stub.h"
37 #include "account-private.h"
38 #include "account-error.h"
39
40 #define _PRIVILEGE_ACCOUNT_READ "http://tizen.org/privilege/account.read"
41 #define _PRIVILEGE_ACCOUNT_WRITE "http://tizen.org/privilege/account.write"
42
43 #define ACCOUNT_MGR_DBUS_PATH       "/org/tizen/account/manager"
44 static guint owner_id = 0;
45 GDBusObjectManagerServer *account_mgr_server_mgr = NULL;
46 static AccountManager* account_mgr_server_obj = NULL;
47
48 static cynara *p_cynara;
49
50 //static gboolean has_owner = FALSE;
51
52 // pid-mode, TODO: make it sessionId-mode, were session id is mix of pid and some rand no, so that
53 // one client can have multiple connections having different modes
54 //static GHashTable* mode_table = NULL;
55
56 GDBusErrorEntry _account_svc_errors[] =
57 {
58         {ACCOUNT_ERROR_NONE, _ACCOUNT_SVC_ERROR_PREFIX".NoError"},
59         {ACCOUNT_ERROR_OUT_OF_MEMORY, _ACCOUNT_SVC_ERROR_PREFIX".OutOfMemory"},
60         {ACCOUNT_ERROR_INVALID_PARAMETER, _ACCOUNT_SVC_ERROR_PREFIX".InvalidParameter"},
61         {ACCOUNT_ERROR_DUPLICATED, _ACCOUNT_SVC_ERROR_PREFIX".Duplicated"},
62         {ACCOUNT_ERROR_NO_DATA, _ACCOUNT_SVC_ERROR_PREFIX".NoData"},
63         {ACCOUNT_ERROR_RECORD_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".RecordNotFound"},
64         {ACCOUNT_ERROR_DB_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".DBFailed"},
65         {ACCOUNT_ERROR_DB_NOT_OPENED, _ACCOUNT_SVC_ERROR_PREFIX".DBNotOpened"},
66         {ACCOUNT_ERROR_QUERY_SYNTAX_ERROR, _ACCOUNT_SVC_ERROR_PREFIX".QuerySynTaxError"},
67         {ACCOUNT_ERROR_ITERATOR_END, _ACCOUNT_SVC_ERROR_PREFIX".IteratorEnd"},
68         {ACCOUNT_ERROR_NOTI_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".NotiFalied"},
69         {ACCOUNT_ERROR_PERMISSION_DENIED, _ACCOUNT_SVC_ERROR_PREFIX".PermissionDenied"},
70         {ACCOUNT_ERROR_XML_PARSE_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".XMLParseFailed"},
71         {ACCOUNT_ERROR_XML_FILE_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".FileNotFound"},
72         {ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL, _ACCOUNT_SVC_ERROR_PREFIX".SubscriptionFailed"},
73         {ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, _ACCOUNT_SVC_ERROR_PREFIX".NotRegisteredProvider"},
74         {ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, _ACCOUNT_SVC_ERROR_PREFIX".NotAllowMultiple"},
75         {ACCOUNT_ERROR_DATABASE_BUSY, _ACCOUNT_SVC_ERROR_PREFIX".database_busy"},
76 };
77
78 static guint
79 _get_client_pid(GDBusMethodInvocation* invoc)
80 {
81         const char *name = NULL;
82         name = g_dbus_method_invocation_get_sender(invoc);
83         if (name == NULL)
84         {
85                 _ERR("g_dbus_method_invocation_get_sender failed");
86                 return -1;
87         }
88         _INFO("sender=[%s]", name);
89
90
91         guint pid = -1;
92         GError *error = NULL;
93         GVariant *_ret;
94
95         _INFO("calling GetConnectionUnixProcessID");
96
97         GDBusConnection* conn = g_dbus_method_invocation_get_connection(invoc);
98         _ret = g_dbus_connection_call_sync(conn,
99                         "org.freedesktop.DBus",
100                         "/org/freedesktop/DBus",
101                         "org.freedesktop.DBus",
102                         "GetConnectionUnixProcessID",
103                         g_variant_new("(s)", name),
104                         NULL,
105                         G_DBUS_CALL_FLAGS_NONE,
106                         -1,
107                         NULL,
108                         &error);
109
110         if (_ret != NULL)
111         {
112                 g_variant_get(_ret, "(u)", &pid);
113                 g_variant_unref(_ret);
114         }
115
116         _INFO("process Id = [%u]", pid);
117         return pid;
118 }
119
120 static GQuark
121 _account_error_quark (void)
122 {
123         static volatile gsize quark_volatile = 0;
124
125         g_dbus_error_register_error_domain (_ACCOUNT_SVC_ERROR_DOMAIN,
126                                                                                 &quark_volatile,
127                                                                                 _account_svc_errors,
128                                                                                 G_N_ELEMENTS (_account_svc_errors));
129
130         return (GQuark) quark_volatile;
131 }
132
133 static int __check_privilege_by_cynara(const char *client, const char *session, const char *user, const char *privilege)
134 {
135         int ret;
136         char err_buf[128] = {0,};
137
138         ret = cynara_check(p_cynara, client, session, user, privilege);
139         switch (ret) {
140                 case CYNARA_API_ACCESS_ALLOWED:
141                         _DBG("cynara_check success");
142                         return ACCOUNT_ERROR_NONE;
143                 case CYNARA_API_ACCESS_DENIED:
144                         _ERR("cynara_check permission deined, privilege=%s, error = CYNARA_API_ACCESS_DENIED", privilege);
145                         return ACCOUNT_ERROR_PERMISSION_DENIED;
146                 default:
147                         cynara_strerror(ret, err_buf, sizeof(err_buf));
148                         _ERR("cynara_check error : %s, privilege=%s, ret = %d", err_buf, privilege, ret);
149                         return ACCOUNT_ERROR_PERMISSION_DENIED;
150         }
151 }
152
153 int __get_information_for_cynara_check(GDBusMethodInvocation *invocation, char **client, char **user, char **session)
154 {
155         GDBusConnection *gdbus_conn = NULL;
156         char* sender = NULL;
157         int ret = -1;
158
159         //get GDBusConnection
160         gdbus_conn = g_dbus_method_invocation_get_connection(invocation);
161         if(gdbus_conn == NULL)
162         {
163                 _ERR("g_dbus_method_invocation_get_connection failed");
164                 return -1;
165         }
166
167         //get sender(unique_name)
168         sender = (char*) g_dbus_method_invocation_get_sender(invocation);
169         if (sender == NULL)
170         {
171                 _ERR("g_dbus_method_invocation_get_sender failed");
172                 return -1;
173         }
174
175         ret = cynara_creds_gdbus_get_user(gdbus_conn, sender, CLIENT_METHOD_SMACK, user);
176         if (ret != CYNARA_API_SUCCESS)
177         {
178                 _ERR("cynara_creds_gdbus_get_user failed, ret = %d", ret);
179                 return -1;
180         }
181
182         ret = cynara_creds_gdbus_get_client(gdbus_conn, sender, CLIENT_METHOD_SMACK, client);
183         if (ret != CYNARA_API_SUCCESS)
184         {
185                 _ERR("cynara_creds_gdbus_get_client failed, ret = %d", ret);
186                 return -1;
187         }
188
189         guint pid = _get_client_pid(invocation);
190         _INFO("client Id = [%u]", pid);
191
192         *session = cynara_session_from_pid(pid);
193         if (*session == NULL)
194         {
195                 _ERR("cynara_session_from_pid failed");
196                 return -1;
197         }
198         return ACCOUNT_ERROR_NONE;
199 }
200
201 int _check_privilege(GDBusMethodInvocation *invocation, const char* privilege)
202 {
203         int ret = -1;
204         char *client = NULL;
205         char *session = NULL;
206         char *user = NULL;
207
208         ret = __get_information_for_cynara_check(invocation, &client, &user, &session);
209         if ( ret != ACCOUNT_ERROR_NONE )
210         {
211                 _ERR("__get_information_for_cynara_check failed");
212                 g_free(client);
213                 g_free(user);
214                 _ACCOUNT_FREE(session);
215                 return ACCOUNT_ERROR_PERMISSION_DENIED;
216         }
217
218         ret = __check_privilege_by_cynara(client, session, user, privilege);
219         if ( ret != ACCOUNT_ERROR_NONE )
220         {
221                 _ERR("__check_privilege_by_cynara failed, ret = %d", ret);
222                 g_free(client);
223                 g_free(user);
224                 _ACCOUNT_FREE(session);
225                 return ACCOUNT_ERROR_PERMISSION_DENIED;
226         }
227         g_free(client);
228         g_free(user);
229         _ACCOUNT_FREE(session);
230         return ACCOUNT_ERROR_NONE;
231 }
232
233 int _check_priviliege_account_read(GDBusMethodInvocation *invocation)
234 {
235         return _check_privilege(invocation, _PRIVILEGE_ACCOUNT_READ);
236 }
237
238 int _check_priviliege_account_write(GDBusMethodInvocation *invocation)
239 {
240         return _check_privilege(invocation, _PRIVILEGE_ACCOUNT_WRITE);
241 }
242
243 gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gpointer user_data)
244 {
245         _INFO("account_manager_account_add start");
246         int db_id = -1;
247
248         guint pid = _get_client_pid(invocation);
249         _INFO("client Id = [%u]", pid);
250
251         int return_code = _check_priviliege_account_read(invocation);
252         if (return_code != ACCOUNT_ERROR_NONE)
253         {
254                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
255                 goto RETURN;
256         }
257
258         return_code = _check_priviliege_account_write(invocation);
259         if (return_code != ACCOUNT_ERROR_NONE)
260         {
261                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
262                 goto RETURN;
263         }
264
265         return_code = _account_db_open(1, (const char*)account_db_path);
266         if (return_code != ACCOUNT_ERROR_NONE)
267         {
268                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
269
270                 goto RETURN;
271         }
272
273         account_s* account = umarshal_account(account_data);
274         if (account == NULL)
275         {
276                 _ERR("account unmarshalling failed");
277                 return_code = ACCOUNT_ERROR_DB_FAILED;
278
279                 goto RETURN;
280         }
281
282         return_code = _account_insert_to_db(account, pid, &db_id);
283
284         if (return_code != ACCOUNT_ERROR_NONE)
285         {
286                 _ERR("_account_insert_to_db() error");
287
288                 goto RETURN;
289         }
290
291 RETURN:
292
293         if (return_code != ACCOUNT_ERROR_NONE)
294         {
295                 _ERR("Account SVC is returning error [%d]", return_code);
296                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
297                 g_dbus_method_invocation_return_gerror (invocation, error);
298         }
299         else
300         {
301                 account_manager_complete_account_add(obj, invocation, db_id);
302         }
303         _INFO("account_manager_account_add end");
304
305         return_code = _account_db_close();
306         if (return_code != ACCOUNT_ERROR_NONE)
307         {
308                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
309                 return_code = ACCOUNT_ERROR_NONE;
310         }
311
312         return true;
313 }
314
315 gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path)
316 {
317         _INFO("account_manager_account_query_all start");
318
319         GVariant* account_list_variant = NULL;
320
321         guint pid = _get_client_pid(invocation);
322         _INFO("client Id = [%u]", pid);
323
324         int return_code = _check_priviliege_account_read(invocation);
325         if (return_code != ACCOUNT_ERROR_NONE)
326         {
327                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
328                 goto RETURN;
329         }
330
331         return_code = _account_db_open(0, (const char*)account_db_path);
332         if (return_code != ACCOUNT_ERROR_NONE)
333         {
334                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
335
336                 goto RETURN;
337         }
338
339         //Mode checking not required, since default mode is read.
340
341         GSList* account_list = NULL;
342         account_list = _account_db_query_all(pid);
343
344         if (account_list == NULL)
345         {
346                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
347                 _ERR("No account found.");
348                 goto RETURN;
349         }
350
351         _INFO("account_list length= [%d]", g_slist_length(account_list));
352
353         return_code = 0;
354         _INFO("before calling marshal_account_list");
355         account_list_variant = marshal_account_list(account_list);
356         _INFO("after calling marshal_account_list");
357
358 RETURN:
359
360         if (account_list_variant == NULL)
361         {
362                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
363                 g_dbus_method_invocation_return_gerror (invocation, error);
364         }
365         else
366         {
367                 account_manager_complete_account_query_all(obj, invocation, account_list_variant);
368         }
369         _INFO("account_manager_account_query_all end");
370
371         return_code = _account_db_close();
372         if (return_code != ACCOUNT_ERROR_NONE)
373         {
374                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
375                 return_code = ACCOUNT_ERROR_NONE;
376         }
377
378         return true;
379 }
380
381 gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path)
382 {
383         _INFO("account_manager_account_query_all start");
384
385         GVariant* account_type_list_variant = NULL;
386         guint pid = _get_client_pid(invocation);
387         _INFO("client Id = [%u]", pid);
388
389         int return_code = _check_priviliege_account_read(invocation);
390         if (return_code != ACCOUNT_ERROR_NONE)
391         {
392                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
393                 goto RETURN;
394         }
395
396         return_code = _account_db_open(0, (const char*)account_db_path);
397         if (return_code != ACCOUNT_ERROR_NONE)
398         {
399                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
400
401                 goto RETURN;
402         }
403
404         //Mode checking not required, since default mode is read.
405
406         GSList* account_type_list = NULL;
407         account_type_list = _account_type_query_all();
408
409         if (account_type_list == NULL)
410         {
411                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
412                 _ERR("No account type found.");
413                 goto RETURN;
414         }
415
416         _INFO("account_type_list length= [%d]", g_slist_length(account_type_list));
417
418         return_code = 0;
419         _INFO("before calling marshal_account_type_list");
420         account_type_list_variant = marshal_account_type_list(account_type_list);
421         _INFO("after calling marshal_account_type_list");
422
423 RETURN:
424
425         if (account_type_list_variant == NULL)
426         {
427                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
428                 g_dbus_method_invocation_return_gerror (invocation, error);
429         }
430         else
431         {
432                 account_manager_complete_account_type_query_all(obj, invocation, account_type_list_variant);
433         }
434         _INFO("account_manager_account_query_all end");
435
436         return_code = _account_db_close();
437         if (return_code != ACCOUNT_ERROR_NONE)
438         {
439                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
440                 return_code = ACCOUNT_ERROR_NONE;
441         }
442
443         return true;
444 }
445
446 gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gpointer user_data)
447 {
448         int db_id = -1;
449
450         _INFO("account_manager_account_type_add start");
451
452         guint pid = _get_client_pid(invocation);
453         _INFO("client Id = [%u]", pid);
454
455         int return_code = _check_priviliege_account_read(invocation);
456         if (return_code != ACCOUNT_ERROR_NONE)
457         {
458                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
459                 goto RETURN;
460         }
461         return_code = _check_priviliege_account_write(invocation);
462         if (return_code != ACCOUNT_ERROR_NONE)
463         {
464                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
465                 goto RETURN;
466         }
467
468         return_code = _account_db_open(1, (const char*)account_db_path);
469         if (return_code != ACCOUNT_ERROR_NONE)
470         {
471                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
472
473                 goto RETURN;
474         }
475
476         account_type_s* account_type = umarshal_account_type(account_type_data);
477         if (account_type == NULL)
478         {
479                 _ERR("account_type unmarshalling failed");
480                 return_code = ACCOUNT_ERROR_DB_FAILED;
481                 goto RETURN;
482         }
483
484         _INFO("before _account_type_insert_to_db");
485         return_code = _account_type_insert_to_db(account_type, &db_id);
486         _INFO("after _account_type_insert_to_db");
487         if (return_code != ACCOUNT_ERROR_NONE)
488         {
489                 _ERR("_account_type_insert_to_db error");
490                 goto RETURN;
491         }
492
493 RETURN:
494
495         if (return_code != ACCOUNT_ERROR_NONE)
496         {
497                 _ERR("Account SVC is returning error [%d]", return_code);
498                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
499                 g_dbus_method_invocation_return_gerror (invocation, error);
500         }
501         else
502         {
503                 account_manager_complete_account_type_add(obj, invocation, db_id);
504         }
505         _INFO("account_manager_account_type_add end");
506
507         return_code = _account_db_close();
508         if (return_code != ACCOUNT_ERROR_NONE)
509         {
510                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
511                 return_code = ACCOUNT_ERROR_NONE;
512         }
513
514         return true;
515 }
516
517 gboolean account_manager_account_delete_from_db_by_id(AccountManager *object,
518                                                                                          GDBusMethodInvocation *invocation,
519                                                                                          gchar *account_db_path,
520                                                                                          gint account_db_id)
521 {
522         _INFO("account_manager_account_delete_from_db_by_id start");
523
524         guint pid = _get_client_pid(invocation);
525         _INFO("client Id = [%u]", pid);
526
527         int return_code = _check_priviliege_account_read(invocation);
528         if (return_code != ACCOUNT_ERROR_NONE)
529         {
530                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
531                 goto RETURN;
532         }
533         return_code = _check_priviliege_account_write(invocation);
534         if (return_code != ACCOUNT_ERROR_NONE)
535         {
536                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
537                 goto RETURN;
538         }
539
540         return_code = _account_db_open(1, (const char*)account_db_path);
541         if (return_code != ACCOUNT_ERROR_NONE)
542         {
543                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
544
545                 goto RETURN;
546         }
547
548         _INFO("before _account_delete");
549         return_code = _account_delete(pid, account_db_id);
550         _INFO("after _account_delete=[%d]", return_code);
551
552         if (return_code != ACCOUNT_ERROR_NONE)
553         {
554                 _ERR("_account_delete error");
555                 goto RETURN;
556         }
557
558 RETURN:
559
560         if (return_code != ACCOUNT_ERROR_NONE)
561         {
562                 _ERR("Account SVC is returning error [%d]", return_code);
563                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
564                 g_dbus_method_invocation_return_gerror (invocation, error);
565         }
566         else
567         {
568                 account_manager_complete_account_delete_from_db_by_id(object, invocation);
569         }
570         _INFO("account_manager_account_delete_from_db_by_id end");
571
572         return_code = _account_db_close();
573         if (return_code != ACCOUNT_ERROR_NONE)
574         {
575                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
576                 return_code = ACCOUNT_ERROR_NONE;
577         }
578
579         return true;
580 }
581
582 gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *object,
583                                                                                                                          GDBusMethodInvocation *invocation,
584                                                                                                                          gchar  *account_db_path,
585                                                                                                                          const gchar *user_name,
586                                                                                                                          const gchar *package_name)
587 {
588         _INFO("account_manager_account_delete_from_db_by_user_name start");
589
590         guint pid = _get_client_pid(invocation);
591         _INFO("client Id = [%u]", pid);
592
593         int return_code = _check_priviliege_account_read(invocation);
594         if (return_code != ACCOUNT_ERROR_NONE)
595         {
596                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
597                 goto RETURN;
598         }
599         return_code = _check_priviliege_account_write(invocation);
600         if (return_code != ACCOUNT_ERROR_NONE)
601         {
602                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
603                 goto RETURN;
604         }
605
606         return_code = _account_db_open(1, (const char*)account_db_path);
607         if (return_code != ACCOUNT_ERROR_NONE)
608         {
609                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
610
611                 goto RETURN;
612         }
613
614         _INFO("before _account_delete_from_db_by_user_name");
615         return_code = _account_delete_from_db_by_user_name(pid, user_name, package_name);
616         _INFO("after _account_delete_from_db_by_user_name=[%d]", return_code);
617
618         if (return_code != ACCOUNT_ERROR_NONE)
619         {
620                 _ERR("_account_delete_from_db_by_user_name error");
621                 goto RETURN;
622         }
623
624 RETURN:
625
626         if (return_code != ACCOUNT_ERROR_NONE)
627         {
628                 _ERR("Account SVC is returning error [%d]", return_code);
629                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
630                 g_dbus_method_invocation_return_gerror (invocation, error);
631         }
632         else
633         {
634                 account_manager_complete_account_delete_from_db_by_id(object, invocation);
635         }
636         _INFO("account_manager_account_delete_from_db_by_user_name end");
637
638         return_code = _account_db_close();
639         if (return_code != ACCOUNT_ERROR_NONE)
640         {
641                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
642                 return_code = ACCOUNT_ERROR_NONE;
643         }
644
645         return true;
646 }
647
648 gboolean account_manager_account_delete_from_db_by_package_name(AccountManager *object,
649                                                                                                                          GDBusMethodInvocation *invocation,
650                                                                                                                          gchar  *account_db_path,
651                                                                                                                          const gchar *package_name, gboolean permission)
652 {
653         _INFO("account_manager_account_delete_from_db_by_package_name start");
654
655         guint pid = _get_client_pid(invocation);
656         _INFO("client Id = [%u]", pid);
657
658         int return_code = _check_priviliege_account_read(invocation);
659         if (return_code != ACCOUNT_ERROR_NONE)
660         {
661                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
662                 goto RETURN;
663         }
664         return_code = _check_priviliege_account_write(invocation);
665         if (return_code != ACCOUNT_ERROR_NONE)
666         {
667                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
668                 goto RETURN;
669         }
670
671         return_code = _account_db_open(1, (const char*)account_db_path);
672         if (return_code != ACCOUNT_ERROR_NONE)
673         {
674                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
675
676                 goto RETURN;
677         }
678
679         _INFO("before account_delete_from_db_by_package_name");
680         return_code = _account_delete_from_db_by_package_name(pid, package_name, permission);
681         _INFO("after account_delete_from_db_by_package_name=[%d]", return_code);
682
683         if (return_code != ACCOUNT_ERROR_NONE)
684         {
685                 _ERR("_account_delete_from_db_by_package_name error");
686                 goto RETURN;
687         }
688
689 RETURN:
690
691         if (return_code != ACCOUNT_ERROR_NONE)
692         {
693                 _ERR("Account SVC is returning error [%d]", return_code);
694                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
695                 g_dbus_method_invocation_return_gerror (invocation, error);
696         }
697         else
698         {
699                 account_manager_complete_account_delete_from_db_by_package_name(object, invocation);
700         }
701         _INFO("account_manager_account_delete_from_db_by_package_name end");
702
703         return_code = _account_db_close();
704         if (return_code != ACCOUNT_ERROR_NONE)
705         {
706                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
707                 return_code = ACCOUNT_ERROR_NONE;
708         }
709
710         return true;
711 }
712
713 gboolean account_manager_account_update_to_db_by_id(AccountManager *object,
714                                                                                                                         GDBusMethodInvocation *invocation,
715                                                                                                                         gchar *account_db_path,
716                                                                                                                         GVariant *account_data,
717                                                                                                                         gint account_id)
718 {
719         _INFO("account_manager_account_update_to_db_by_id start");
720
721         guint pid = _get_client_pid(invocation);
722         _INFO("client Id = [%u]", pid);
723
724         int return_code = _check_priviliege_account_read(invocation);
725         if (return_code != ACCOUNT_ERROR_NONE)
726         {
727                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
728                 goto RETURN;
729         }
730         return_code = _check_priviliege_account_write(invocation);
731         if (return_code != ACCOUNT_ERROR_NONE)
732         {
733                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
734                 goto RETURN;
735         }
736
737         return_code = _account_db_open(1, (const char*)account_db_path);
738         if (return_code != ACCOUNT_ERROR_NONE)
739         {
740                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
741
742                 goto RETURN;
743         }
744
745         account_s* account = umarshal_account(account_data);
746         if (account == NULL)
747         {
748                 _ERR("Unmarshal failed");
749                 return_code = ACCOUNT_ERROR_DB_FAILED;
750                 goto RETURN;
751         }
752
753         _INFO("before account_update_to_db_by_id");
754         return_code = _account_update_to_db_by_id(pid, account, account_id);
755         _INFO("after account_update_to_db_by_id=[%d]", return_code);
756
757         if (return_code != ACCOUNT_ERROR_NONE)
758         {
759                 _ERR("_account_type_update_to_db_by_id error");
760                 goto RETURN;
761         }
762
763 RETURN:
764
765         if (return_code != ACCOUNT_ERROR_NONE)
766         {
767                 _ERR("Account SVC is returning error [%d]", return_code);
768                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
769                 g_dbus_method_invocation_return_gerror (invocation, error);
770         }
771         else
772         {
773                 account_manager_complete_account_update_to_db_by_id(object, invocation);
774         }
775         _INFO("account_manager_account_update_to_db_by_id end");
776
777         return_code = _account_db_close();
778         if (return_code != ACCOUNT_ERROR_NONE)
779         {
780                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
781                 return_code = ACCOUNT_ERROR_NONE;
782         }
783
784         return true;
785 }
786
787 gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager *object,
788                                                                                                                         GDBusMethodInvocation *invocation,
789                                                                                                                         gchar *account_db_path,
790                                                                                                                         GVariant *account_data,
791                                                                                                                         const gchar *user_name,
792                                                                                                                         const gchar *package_name)
793 {
794         _INFO("account_manager_handle_account_update_to_db_by_user_name start");
795
796         guint pid = _get_client_pid(invocation);
797         _INFO("client Id = [%u]", pid);
798
799         int return_code = _check_priviliege_account_read(invocation);
800         if (return_code != ACCOUNT_ERROR_NONE)
801         {
802                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
803                 goto RETURN;
804         }
805         return_code = _check_priviliege_account_write(invocation);
806         if (return_code != ACCOUNT_ERROR_NONE)
807         {
808                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
809                 goto RETURN;
810         }
811
812         return_code = _account_db_open(1, (const char*)account_db_path);
813         if (return_code != ACCOUNT_ERROR_NONE)
814         {
815                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
816
817                 goto RETURN;
818         }
819
820         account_s* account = umarshal_account(account_data);
821         if (account == NULL)
822         {
823                 _ERR("Unmarshal failed");
824                 return_code = ACCOUNT_ERROR_DB_FAILED;
825                 goto RETURN;
826         }
827
828         _INFO("before account_update_to_db_by_id");
829         return_code = _account_update_to_db_by_user_name(pid, account, user_name, package_name);
830         _INFO("after account_update_to_db_by_id=[%d]", return_code);
831
832         if (return_code != ACCOUNT_ERROR_NONE)
833         {
834                 _ERR("_account_update_to_db_by_id error");
835                 goto RETURN;
836         }
837
838 RETURN:
839
840         if (return_code != ACCOUNT_ERROR_NONE)
841         {
842                 _ERR("Account SVC is returning error [%d]", return_code);
843                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
844                 g_dbus_method_invocation_return_gerror (invocation, error);
845         }
846         else
847         {
848                 account_manager_complete_account_update_to_db_by_id(object, invocation);
849         }
850         _INFO("account_manager_handle_account_update_to_db_by_user_name end");
851
852         return_code = _account_db_close();
853         if (return_code != ACCOUNT_ERROR_NONE)
854         {
855                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
856                 return_code = ACCOUNT_ERROR_NONE;
857         }
858
859         return true;
860 }
861
862 gboolean
863 account_manager_handle_account_type_query_label_by_locale(AccountManager *object,
864                                                                                                                         GDBusMethodInvocation *invocation,
865                                                                                                                         gchar *account_db_path,
866                                                                                                                         const gchar *app_id,
867                                                                                                                         const gchar *locale)
868 {
869         _INFO("account_manager_handle_account_type_query_label_by_locale start");
870         guint pid = _get_client_pid(invocation);
871
872         _INFO("client Id = [%u]", pid);
873
874         int return_code = _check_priviliege_account_read(invocation);
875         if (return_code != ACCOUNT_ERROR_NONE)
876         {
877                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
878                 goto RETURN;
879         }
880
881         return_code = _account_db_open(0, (const char*)account_db_path);
882         if (return_code != ACCOUNT_ERROR_NONE)
883         {
884                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
885
886                 goto RETURN;
887         }
888
889         _INFO("before _account_type_query_label_by_locale");
890         char *label_name = NULL;
891         return_code = _account_type_query_label_by_locale(app_id, locale, &label_name);
892         _INFO("after _account_type_query_label_by_locale=[%d]", return_code);
893
894         if (return_code != ACCOUNT_ERROR_NONE)
895         {
896                 _ERR("_account_type_query_label_by_locale error");
897                 goto RETURN;
898         }
899
900 RETURN:
901         if (return_code != ACCOUNT_ERROR_NONE)
902         {
903                 _ERR("Account SVC is returning error [%d]", return_code);
904                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
905                 g_dbus_method_invocation_return_gerror (invocation, error);
906         }
907         else
908         {
909                 account_manager_complete_account_type_query_label_by_locale(object, invocation, label_name);
910         }
911         _INFO("account_manager_handle_account_type_query_label_by_locale end");
912
913         return_code = _account_db_close();
914         if (return_code != ACCOUNT_ERROR_NONE)
915         {
916                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
917                 return_code = ACCOUNT_ERROR_NONE;
918         }
919
920         return true;
921 }
922
923 gboolean
924 account_manager_handle_account_type_query_by_provider_feature(AccountManager *obj,
925                                                                                                                         GDBusMethodInvocation *invocation,
926                                                                                                                         gchar *account_db_path, const gchar *key)
927 {
928         _INFO("account_manager_handle_account_type_query_by_provider_feature start");
929         GVariant* account_type_list_variant = NULL;
930
931         guint pid = _get_client_pid(invocation);
932
933         _INFO("client Id = [%u]", pid);
934
935         int return_code = _check_priviliege_account_read(invocation);
936         if (return_code != ACCOUNT_ERROR_NONE)
937         {
938                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
939                 goto RETURN;
940         }
941
942         return_code = _account_db_open(0, (const char*)account_db_path);
943         if (return_code != ACCOUNT_ERROR_NONE)
944         {
945                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
946
947                 goto RETURN;
948         }
949
950         //Mode checking not required, since default mode is read.
951
952         GSList* account_type_list = NULL;
953         account_type_list = _account_type_query_by_provider_feature(key, &return_code);
954         if (return_code != 0)
955         {
956                 _ERR("_account_type_query_by_provider_feature=[%d]", return_code);
957                 goto RETURN;
958         }
959
960         if (account_type_list == NULL)
961         {
962                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
963                 _ERR("No account type found.");
964                 goto RETURN;
965         }
966
967         if (return_code != ACCOUNT_ERROR_NONE)
968         {
969                 _ERR("_account_type_query_by_provider_feature error");
970                 goto RETURN;
971         }
972
973         _INFO("account_type_list length= [%d]", g_slist_length(account_type_list));
974
975         _INFO("before calling marshal_account_type_list");
976         account_type_list_variant = marshal_account_type_list(account_type_list);
977         _INFO("after calling marshal_account_type_list");
978
979 RETURN:
980
981         if (account_type_list_variant == NULL)
982         {
983                 GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound");
984                 g_dbus_method_invocation_return_gerror (invocation, error);
985         }
986         else
987         {
988                 account_manager_complete_account_type_query_by_provider_feature(obj, invocation, account_type_list_variant);
989         }
990         _INFO("account_manager_handle_account_type_query_by_provider_feature end");
991
992         return_code = _account_db_close();
993         if (return_code != ACCOUNT_ERROR_NONE)
994         {
995                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
996                 return_code = ACCOUNT_ERROR_NONE;
997         }
998
999         return true;
1000 }
1001
1002 gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden)
1003 {
1004         _INFO("account_manager_account_get_total_count_from_db start");
1005         guint pid = _get_client_pid(invocation);
1006
1007         _INFO("client Id = [%u]", pid);
1008
1009         int return_code = _check_priviliege_account_read(invocation);
1010         if (return_code != ACCOUNT_ERROR_NONE)
1011         {
1012                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1013                 goto RETURN;
1014         }
1015
1016         return_code = _account_db_open(0, (const char*)account_db_path);
1017         if (return_code != ACCOUNT_ERROR_NONE)
1018         {
1019                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1020
1021                 goto RETURN;
1022         }
1023
1024
1025         _INFO("before account_get_total_count_from_db");
1026         int count = -1;
1027         return_code = _account_get_total_count_from_db(include_hidden, &count);
1028         _INFO("before account_get_total_count_from_db=[%d], return_code");
1029
1030         if (return_code != ACCOUNT_ERROR_NONE)
1031         {
1032                 _ERR("_account_get_total_count_from_db error");
1033                 goto RETURN;
1034         }
1035
1036 RETURN:
1037         if (return_code != ACCOUNT_ERROR_NONE)
1038         {
1039                 _ERR("Account SVC is returning error [%d]", return_code);
1040                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1041                 g_dbus_method_invocation_return_gerror (invocation, error);
1042         }
1043         else
1044         {
1045                 account_manager_complete_account_get_total_count_from_db(object, invocation, count);
1046         }
1047         _INFO("account_manager_account_get_total_count_from_db end");
1048
1049         return_code = _account_db_close();
1050         if (return_code != ACCOUNT_ERROR_NONE)
1051         {
1052                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1053                 return_code = ACCOUNT_ERROR_NONE;
1054         }
1055
1056         return true;
1057 }
1058
1059 gboolean account_manager_handle_account_query_account_by_account_id(AccountManager *object, GDBusMethodInvocation *invocation,
1060                 gchar *account_db_path, gint account_db_id)
1061 {
1062         _INFO("account_manager_handle_account_query_account_by_account_id start");
1063         GVariant* account_variant = NULL;
1064
1065         guint pid = _get_client_pid(invocation);
1066
1067         _INFO("client Id = [%u]", pid);
1068
1069         int return_code = _check_priviliege_account_read(invocation);
1070         if (return_code != ACCOUNT_ERROR_NONE)
1071         {
1072                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1073                 goto RETURN;
1074         }
1075
1076         return_code = _account_db_open(0, (const char*)account_db_path);
1077         if (return_code != ACCOUNT_ERROR_NONE)
1078         {
1079                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1080
1081                 goto RETURN;
1082         }
1083
1084         account_s* account_data = create_empty_account_instance();
1085         if (account_data == NULL)
1086         {
1087                 _ERR("out of memory");
1088                 return_code = ACCOUNT_ERROR_DB_FAILED;
1089                 goto RETURN;
1090         }
1091         _INFO("before _account_query_account_by_account_id");
1092         return_code = _account_query_account_by_account_id(pid, account_db_id, account_data);
1093         _INFO("after _account_query_account_by_return_code=[%d]", return_code);
1094         _INFO("user_name = %s, user_data_txt[0] = %s, user_data_int[1] = %d", account_data->user_name, account_data->user_data_txt[0], account_data->user_data_int[1]);
1095
1096         if (return_code == ACCOUNT_ERROR_NONE)
1097         {
1098                 account_variant = marshal_account(account_data);
1099         }
1100
1101         if (return_code != ACCOUNT_ERROR_NONE)
1102         {
1103                 _ERR("_account_type_query_label_by_locale error");
1104                 goto RETURN;
1105         }
1106
1107 RETURN:
1108         if (account_variant == NULL || return_code != ACCOUNT_ERROR_NONE)
1109         {
1110                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1111                 g_dbus_method_invocation_return_gerror (invocation, error);
1112         }
1113         else
1114         {
1115                 account_manager_complete_account_query_account_by_account_id(object, invocation, account_variant);
1116         }
1117         _INFO("account_manager_handle_account_query_account_by_account_id end");
1118
1119         return_code = _account_db_close();
1120         if (return_code != ACCOUNT_ERROR_NONE)
1121         {
1122                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1123                 return_code = ACCOUNT_ERROR_NONE;
1124         }
1125
1126         return true;
1127 }
1128
1129 gboolean
1130 account_manager_handle_account_query_account_by_user_name(AccountManager *obj,
1131                                                                                                                   GDBusMethodInvocation *invocation,
1132                                                                                                                   gchar *account_db_path, const gchar *user_name)
1133 {
1134         _INFO("account_manager_handle_account_query_account_by_user_name start");
1135
1136         GVariant* account_list_variant = NULL;
1137         guint pid = _get_client_pid(invocation);
1138
1139         _INFO("client Id = [%u]", pid);
1140
1141         int return_code = _check_priviliege_account_read(invocation);
1142         if (return_code != ACCOUNT_ERROR_NONE)
1143         {
1144                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1145                 goto RETURN;
1146         }
1147
1148         return_code = _account_db_open(0, (const char*)account_db_path);
1149         if (return_code != ACCOUNT_ERROR_NONE)
1150         {
1151                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1152
1153                 goto RETURN;
1154         }
1155
1156         //Mode checking not required, since default mode is read.
1157
1158         GList* account_list = NULL;
1159
1160         account_list = _account_query_account_by_user_name(pid, user_name, &return_code);
1161
1162         if (account_list == NULL)
1163         {
1164                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1165                 _ERR("No account found.");
1166                 goto RETURN;
1167         }
1168
1169         _INFO("account_list length= [%d]", g_list_length(account_list));
1170
1171         _INFO("before calling marshal_account_list_double");
1172         account_list_variant = marshal_account_list_double(account_list);
1173         _INFO("after calling marshal_account_list_double");
1174
1175         if (return_code != ACCOUNT_ERROR_NONE)
1176         {
1177                 _ERR("_account_query_account_by_user_name error");
1178                 goto RETURN;
1179         }
1180
1181 RETURN:
1182         if (account_list_variant == NULL)
1183         {
1184                 GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound");
1185                 g_dbus_method_invocation_return_gerror (invocation, error);
1186         }
1187         else
1188         {
1189                 account_manager_complete_account_query_account_by_user_name(obj, invocation, account_list_variant);
1190         }
1191         _INFO("account_manager_handle_account_query_account_by_user_name end");
1192
1193         return_code = _account_db_close();
1194         if (return_code != ACCOUNT_ERROR_NONE)
1195         {
1196                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1197                 return_code = ACCOUNT_ERROR_NONE;
1198         }
1199
1200         return true;
1201 }
1202
1203 gboolean
1204 account_manager_handle_account_query_account_by_package_name(AccountManager *obj,
1205                                                                                                                   GDBusMethodInvocation *invocation,
1206                                                                                                                   gchar *account_db_path, const gchar *package_name)
1207 {
1208         _INFO("account_manager_handle_account_query_account_by_package_name start");
1209
1210         GVariant* account_list_variant = NULL;
1211         guint pid = _get_client_pid(invocation);
1212
1213         _INFO("client Id = [%u]", pid);
1214
1215         int return_code = _check_priviliege_account_read(invocation);
1216         if (return_code != ACCOUNT_ERROR_NONE)
1217         {
1218                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1219                 goto RETURN;
1220         }
1221
1222         return_code = _account_db_open(0, (const char*)account_db_path);
1223         if (return_code != ACCOUNT_ERROR_NONE)
1224         {
1225                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1226
1227                 goto RETURN;
1228         }
1229
1230         //Mode checking not required, since default mode is read.
1231
1232         GList* account_list = NULL;
1233
1234         account_list = _account_query_account_by_package_name(pid, package_name, &return_code);
1235
1236         if (account_list == NULL)
1237         {
1238                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1239                 _ERR("No account found.");
1240                 goto RETURN;
1241         }
1242
1243         _INFO("account_list length= [%d]", g_list_length(account_list));
1244
1245         account_list_variant = marshal_account_list_double(account_list);
1246
1247         if (return_code != ACCOUNT_ERROR_NONE)
1248         {
1249                 _ERR("_account_query_account_by_package_name error");
1250                 goto RETURN;
1251         }
1252
1253 RETURN:
1254
1255         if (account_list_variant == NULL)
1256         {
1257                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1258                 _INFO("sending error Domain[%d] Message[%s] Code[%d]", error->domain, error->message, error->code);
1259                 g_dbus_method_invocation_return_gerror (invocation, error);
1260         }
1261         else
1262         {
1263                 account_manager_complete_account_query_account_by_package_name(obj, invocation, account_list_variant);
1264         }
1265         _INFO("account_manager_handle_account_query_account_by_package_name start");
1266
1267         return_code = _account_db_close();
1268         if (return_code != ACCOUNT_ERROR_NONE)
1269         {
1270                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1271                 return_code = ACCOUNT_ERROR_NONE;
1272         }
1273
1274         return true;
1275 }
1276
1277 gboolean
1278 account_manager_handle_account_query_account_by_capability(AccountManager *obj,
1279                                                                                                                   GDBusMethodInvocation *invocation,
1280                                                                                                                   gchar *account_db_path,
1281                                                                                                                   const gchar *capability_type,
1282                                                                                                                   gint capability_value)
1283 {
1284         _INFO("account_manager_handle_account_query_account_by_capability start");
1285
1286         GVariant* account_list_variant = NULL;
1287
1288         guint pid = _get_client_pid(invocation);
1289
1290         _INFO("client Id = [%u]", pid);
1291
1292         int return_code = _check_priviliege_account_read(invocation);
1293         if (return_code != ACCOUNT_ERROR_NONE)
1294         {
1295                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1296                 goto RETURN;
1297         }
1298
1299         return_code = _account_db_open(0, (const char*)account_db_path);
1300         if (return_code != ACCOUNT_ERROR_NONE)
1301         {
1302                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1303
1304                 goto RETURN;
1305         }
1306
1307         //Mode checking not required, since default mode is read.
1308
1309         GList* account_list = NULL;
1310
1311         _INFO("before _account_query_account_by_capability");
1312         account_list = _account_query_account_by_capability(pid, capability_type, capability_value, &return_code);
1313         _INFO("after _account_query_account_by_capability");
1314
1315         if (account_list == NULL)
1316         {
1317                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1318                 _ERR("No account found.");
1319                 goto RETURN;
1320         }
1321
1322         _INFO("account_list length= [%d]", g_list_length(account_list));
1323
1324         account_list_variant = marshal_account_list_double(account_list);
1325
1326         if (return_code != ACCOUNT_ERROR_NONE)
1327         {
1328                 _ERR("_account_query_account_by_capability error");
1329                 goto RETURN;
1330         }
1331
1332 RETURN:
1333
1334         if (account_list_variant == NULL)
1335         {
1336                 GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound");
1337                 g_dbus_method_invocation_return_gerror (invocation, error);
1338         }
1339         else
1340         {
1341                 account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant);
1342         }
1343         _INFO("account_manager_handle_account_query_account_by_capability end");
1344
1345         return_code = _account_db_close();
1346         if (return_code != ACCOUNT_ERROR_NONE)
1347         {
1348                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1349                 return_code = ACCOUNT_ERROR_NONE;
1350         }
1351
1352         return true;
1353 }
1354
1355 gboolean
1356 account_manager_handle_account_query_account_by_capability_type(AccountManager *obj,
1357                                                                                                                   GDBusMethodInvocation *invocation,
1358                                                                                                                   gchar *account_db_path,
1359                                                                                                                   const gchar *capability_type)
1360 {
1361         _INFO("account_manager_handle_account_query_account_by_capability_type start");
1362
1363         GVariant* account_list_variant = NULL;
1364
1365         guint pid = _get_client_pid(invocation);
1366
1367         _INFO("client Id = [%u]", pid);
1368
1369         int return_code = _check_priviliege_account_read(invocation);
1370         if (return_code != ACCOUNT_ERROR_NONE)
1371         {
1372                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1373                 goto RETURN;
1374         }
1375
1376         return_code = _account_db_open(0, (const char*)account_db_path);
1377         if (return_code != ACCOUNT_ERROR_NONE)
1378         {
1379                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1380
1381                 goto RETURN;
1382         }
1383
1384         //Mode checking not required, since default mode is read.
1385
1386         GList* account_list = NULL;
1387
1388         account_list = _account_query_account_by_capability_type(pid, capability_type, &return_code);
1389
1390         if (account_list == NULL)
1391         {
1392                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1393                 _ERR("No account found.");
1394                 goto RETURN;
1395         }
1396
1397         _INFO("account_list length= [%d]", g_list_length(account_list));
1398
1399         _INFO("before calling marshal_account_list_double");
1400         account_list_variant = marshal_account_list_double(account_list);
1401         _INFO("after calling marshal_account_list_double");
1402
1403         if (return_code != ACCOUNT_ERROR_NONE)
1404         {
1405                 _ERR("_account_query_account_by_capability_type error");
1406                 goto RETURN;
1407         }
1408
1409 RETURN:
1410
1411         if (account_list_variant == NULL)
1412         {
1413                 GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound");
1414                 g_dbus_method_invocation_return_gerror (invocation, error);
1415         }
1416         else
1417         {
1418                 account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant);
1419         }
1420         _INFO("account_manager_handle_account_query_account_by_capability_type end");
1421
1422         return_code = _account_db_close();
1423         if (return_code != ACCOUNT_ERROR_NONE)
1424         {
1425                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1426                 return_code = ACCOUNT_ERROR_NONE;
1427         }
1428
1429         return true;
1430 }
1431
1432 gboolean
1433 account_manager_handle_account_query_capability_by_account_id(AccountManager *obj,
1434                                                                                                                   GDBusMethodInvocation *invocation,
1435                                                                                                                   gchar *account_db_path,
1436                                                                                                                   const int account_id)
1437 {
1438         _INFO("account_manager_handle_account_query_capability_by_account_id start");
1439
1440         GVariant* capability_list_variant = NULL;
1441
1442         guint pid = _get_client_pid(invocation);
1443
1444         _INFO("client Id = [%u]", pid);
1445
1446         int return_code = _check_priviliege_account_read(invocation);
1447         if (return_code != ACCOUNT_ERROR_NONE)
1448         {
1449                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1450                 goto RETURN;
1451         }
1452
1453
1454         return_code = _account_db_open(0, (const char*)account_db_path);
1455         if (return_code != ACCOUNT_ERROR_NONE)
1456         {
1457                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1458
1459                 goto RETURN;
1460         }
1461
1462         //Mode checking not required, since default mode is read.
1463
1464         GSList* capability_list = NULL;
1465
1466         capability_list = _account_get_capability_list_by_account_id(account_id, &return_code);
1467
1468         if (capability_list == NULL)
1469         {
1470                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1471                 _ERR("No capability found.");
1472                 goto RETURN;
1473         }
1474
1475         _INFO("capability_list length= [%d]", g_slist_length(capability_list));
1476
1477         _INFO("before calling marshal_capability_list");
1478         capability_list_variant = marshal_capability_list(capability_list);
1479         _INFO("after calling marshal_capability_list");
1480
1481         if (return_code != ACCOUNT_ERROR_NONE)
1482         {
1483                 _ERR("_account_get_capability_list_by_account_id error");
1484                 goto RETURN;
1485         }
1486
1487 RETURN:
1488
1489         if (capability_list_variant == NULL)
1490         {
1491                 GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound");
1492                 g_dbus_method_invocation_return_gerror (invocation, error);
1493         }
1494         else
1495         {
1496                 account_manager_complete_account_query_capability_by_account_id(obj, invocation, capability_list_variant);
1497         }
1498         _INFO("account_manager_handle_account_query_capability_by_account_id end");
1499
1500         return_code = _account_db_close();
1501         if (return_code != ACCOUNT_ERROR_NONE)
1502         {
1503                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1504                 return_code = ACCOUNT_ERROR_NONE;
1505         }
1506
1507         return true;
1508 }
1509
1510 gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager *object,
1511                                                                                                                         GDBusMethodInvocation *invocation,
1512                                                                                                                         gchar *account_db_path,
1513                                                                                                                         const int account_db_id,
1514                                                                                                                         const int sync_status)
1515 {
1516         _INFO("account_manager_handle_account_update_sync_status_by_id start");
1517         guint pid = _get_client_pid(invocation);
1518
1519         _INFO("client Id = [%u]", pid);
1520
1521         int return_code = _check_priviliege_account_read(invocation);
1522         if (return_code != ACCOUNT_ERROR_NONE)
1523         {
1524                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1525                 goto RETURN;
1526         }
1527         return_code = _check_priviliege_account_write(invocation);
1528         if (return_code != ACCOUNT_ERROR_NONE)
1529         {
1530                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
1531                 goto RETURN;
1532         }
1533
1534         return_code = _account_db_open(1, (const char*)account_db_path);
1535         if (return_code != ACCOUNT_ERROR_NONE)
1536         {
1537                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1538
1539                 goto RETURN;
1540         }
1541
1542         _INFO("before _account_update_sync_status_by_id");
1543         return_code = _account_update_sync_status_by_id(account_db_id, sync_status);
1544         _INFO("after _account_update_sync_status_by_id=[%d]", return_code);
1545
1546         if (return_code != ACCOUNT_ERROR_NONE)
1547         {
1548                 _ERR("_account_update_sync_status_by_id error");
1549                 goto RETURN;
1550         }
1551
1552 RETURN:
1553
1554         if (return_code != ACCOUNT_ERROR_NONE)
1555         {
1556                 _ERR("Account SVC is returning error [%d]", return_code);
1557                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1558                 g_dbus_method_invocation_return_gerror (invocation, error);
1559         }
1560         else
1561         {
1562                 account_manager_complete_account_update_sync_status_by_id(object, invocation);
1563         }
1564         _INFO("account_manager_handle_account_update_sync_status_by_id end");
1565
1566         return_code = _account_db_close();
1567         if (return_code != ACCOUNT_ERROR_NONE)
1568         {
1569                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1570                 return_code = ACCOUNT_ERROR_NONE;
1571         }
1572
1573         return true;
1574 }
1575
1576 gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(AccountManager *obj,
1577                                                                                                                         GDBusMethodInvocation *invocation,
1578                                                                                                                         gchar *account_db_path,
1579                                                                                                                         const gchar* app_id)
1580 {
1581         GSList* feature_record_list = NULL;
1582         GVariant* feature_record_list_variant = NULL;
1583
1584         _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id start");
1585
1586         guint pid = _get_client_pid(invocation);
1587
1588         _INFO("client Id = [%u]", pid);
1589
1590         int return_code = _check_priviliege_account_read(invocation);
1591         if (return_code != ACCOUNT_ERROR_NONE)
1592         {
1593                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1594                 goto RETURN;
1595         }
1596
1597         return_code = _account_db_open(0, (const char*)account_db_path);
1598         if (return_code != ACCOUNT_ERROR_NONE)
1599         {
1600                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1601
1602                 goto RETURN;
1603         }
1604
1605         _INFO("before _account_type_query_provider_feature_by_app_id");
1606         feature_record_list = _account_type_query_provider_feature_by_app_id(app_id, &return_code);
1607         _INFO("after account_type_query_provider_feature_by_app_id=[%d]", return_code);
1608
1609         if (feature_record_list == NULL)
1610         {
1611                 _ERR("account feature_record_list is NULL");
1612                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1613                 goto RETURN;
1614         }
1615
1616         feature_record_list_variant = provider_feature_list_to_variant(feature_record_list);
1617         _INFO("%s", g_variant_print(feature_record_list_variant, true));
1618
1619         if (return_code != ACCOUNT_ERROR_NONE)
1620         {
1621                 _ERR("_account_type_query_provider_feature_by_app_id error");
1622                 goto RETURN;
1623         }
1624
1625 RETURN:
1626         if (return_code != ACCOUNT_ERROR_NONE)
1627         {
1628                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1629                 g_dbus_method_invocation_return_gerror (invocation, error);
1630         }
1631         else
1632         {
1633                 _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id");
1634                 account_manager_complete_account_type_query_provider_feature_by_app_id(obj, invocation, feature_record_list_variant);
1635         }
1636         _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id end");
1637
1638         return_code = _account_db_close();
1639         if (return_code != ACCOUNT_ERROR_NONE)
1640         {
1641                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1642                 return_code = ACCOUNT_ERROR_NONE;
1643         }
1644
1645         return true;
1646 }
1647
1648 gboolean account_manager_handle_account_type_query_supported_feature(AccountManager *obj,
1649                                                                                                                         GDBusMethodInvocation *invocation,
1650                                                                                                                         gchar *account_db_path,
1651                                                                                                                         const gchar* app_id,
1652                                                                                                                         const gchar* capability)
1653 {
1654         int is_supported = 0;
1655
1656         _INFO("account_manager_handle_account_type_query_supported_feature start");
1657         guint pid = _get_client_pid(invocation);
1658
1659         _INFO("client Id = [%u]", pid);
1660
1661         int return_code = _check_priviliege_account_read(invocation);
1662         if (return_code != ACCOUNT_ERROR_NONE)
1663         {
1664                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1665                 goto RETURN;
1666         }
1667
1668         return_code = _account_db_open(0, (const char*)account_db_path);
1669         if (return_code != ACCOUNT_ERROR_NONE)
1670         {
1671                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1672
1673                 goto RETURN;
1674         }
1675
1676         _INFO("before _account_type_query_supported_feature");
1677         is_supported = _account_type_query_supported_feature(app_id, capability, &return_code);
1678         _INFO("after _account_type_query_supported_feature=[%d]", return_code);
1679
1680         if (return_code != ACCOUNT_ERROR_NONE)
1681         {
1682                 _ERR("_account_type_query_supported_feature error");
1683                 goto RETURN;
1684         }
1685
1686 RETURN:
1687         if (return_code != ACCOUNT_ERROR_NONE)
1688         {
1689                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1690                 g_dbus_method_invocation_return_gerror (invocation, error);
1691         }
1692         else
1693         {
1694                 _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id");
1695                 account_manager_complete_account_type_query_supported_feature(obj, invocation, is_supported);
1696         }
1697         _INFO("account_manager_handle_account_type_query_supported_feature end");
1698
1699         return_code = _account_db_close();
1700         if (return_code != ACCOUNT_ERROR_NONE)
1701         {
1702                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1703                 return_code = ACCOUNT_ERROR_NONE;
1704         }
1705
1706         return true;
1707 }
1708
1709 gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountManager *obj,
1710                                                                                                                         GDBusMethodInvocation *invocation,
1711                                                                                                                         gchar *account_db_path,
1712                                                                                                                         GVariant *account_type_variant,
1713                                                                                                                         const gchar *app_id)
1714 {
1715         _INFO("account_manager_handle_account_type_update_to_db_by_app_id start");
1716
1717         guint pid = _get_client_pid(invocation);
1718
1719         _INFO("client Id = [%u]", pid);
1720
1721         int return_code = _check_priviliege_account_read(invocation);
1722         if (return_code != ACCOUNT_ERROR_NONE)
1723         {
1724                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1725                 goto RETURN;
1726         }
1727         return_code = _check_priviliege_account_write(invocation);
1728         if (return_code != ACCOUNT_ERROR_NONE)
1729         {
1730                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
1731                 goto RETURN;
1732         }
1733
1734         return_code = _account_db_open(1, (const char*)account_db_path);
1735         if (return_code != ACCOUNT_ERROR_NONE)
1736         {
1737                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1738
1739                 goto RETURN;
1740         }
1741
1742         account_type_s* account_type = umarshal_account_type(account_type_variant);
1743
1744         _INFO("before _account_type_update_to_db_by_app_id");
1745         return_code = _account_type_update_to_db_by_app_id(account_type, app_id);
1746         _INFO("after _account_type_update_to_db_by_app_id=[%d]", return_code);
1747
1748         if (return_code != ACCOUNT_ERROR_NONE)
1749         {
1750                 _ERR("_account_type_update_to_db_by_app_id error");
1751                 goto RETURN;
1752         }
1753
1754 RETURN:
1755         if (return_code != ACCOUNT_ERROR_NONE)
1756         {
1757                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1758                 g_dbus_method_invocation_return_gerror (invocation, error);
1759         }
1760         else
1761         {
1762                 _INFO("Calling account_manager_complete_account_type_update_to_db_by_app_id");
1763                 account_manager_complete_account_type_update_to_db_by_app_id(obj, invocation);
1764         }
1765         _INFO("account_manager_handle_account_type_update_to_db_by_app_id end");
1766
1767         return_code = _account_db_close();
1768         if (return_code != ACCOUNT_ERROR_NONE)
1769         {
1770                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1771                 return_code = ACCOUNT_ERROR_NONE;
1772         }
1773
1774         return true;
1775 }
1776
1777 gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *obj,
1778                                                                                                                         GDBusMethodInvocation *invocation,
1779                                                                                                                         gchar *account_db_path,
1780                                                                                                                         const gchar *app_id)
1781 {
1782         _INFO("account_manager_handle_account_type_delete_by_app_id start");
1783
1784         guint pid = _get_client_pid(invocation);
1785
1786         _INFO("client Id = [%u]", pid);
1787
1788         int return_code = _check_priviliege_account_read(invocation);
1789         if (return_code != ACCOUNT_ERROR_NONE)
1790         {
1791                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1792                 goto RETURN;
1793         }
1794         return_code = _check_priviliege_account_write(invocation);
1795         if (return_code != ACCOUNT_ERROR_NONE)
1796         {
1797                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
1798                 goto RETURN;
1799         }
1800
1801         return_code = _account_db_open(1, (const char*)account_db_path);
1802         if (return_code != ACCOUNT_ERROR_NONE)
1803         {
1804                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1805
1806                 goto RETURN;
1807         }
1808
1809         _INFO("before _account_type_delete_by_app_id");
1810         return_code = _account_type_delete_by_app_id (app_id);
1811         _INFO("after _account_type_delete_by_app_id=[%d]", return_code);
1812
1813         if (return_code != ACCOUNT_ERROR_NONE)
1814         {
1815                 _ERR("_account_type_delete_by_app_id error");
1816                 goto RETURN;
1817         }
1818
1819 RETURN:
1820         if (return_code != ACCOUNT_ERROR_NONE)
1821         {
1822                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1823                 g_dbus_method_invocation_return_gerror (invocation, error);
1824         }
1825         else
1826         {
1827                 _ERR("Calling account_manager_complete_account_type_update_to_db_by_app_id");
1828                 account_manager_complete_account_type_delete_by_app_id (obj, invocation);
1829         }
1830         _INFO("account_manager_handle_account_type_delete_by_app_id end");
1831
1832         return_code = _account_db_close();
1833         if (return_code != ACCOUNT_ERROR_NONE)
1834         {
1835                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1836                 return_code = ACCOUNT_ERROR_NONE;
1837         }
1838
1839         return true;
1840 }
1841
1842 gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManager *obj,
1843                                                                                                                         GDBusMethodInvocation *invocation,
1844                                                                                                                         gchar *account_db_path,
1845                                                                                                                         const gchar *app_id)
1846 {
1847         _INFO("account_manager_handle_account_type_query_label_by_app_id start");
1848         GSList* label_list = NULL;
1849         GVariant* label_list_variant = NULL;
1850
1851         guint pid = _get_client_pid(invocation);
1852
1853         _INFO("client Id = [%u]", pid);
1854
1855         int return_code = _check_priviliege_account_read(invocation);
1856         if (return_code != ACCOUNT_ERROR_NONE)
1857         {
1858                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1859                 goto RETURN;
1860         }
1861
1862         return_code = _account_db_open(0, (const char*)account_db_path);
1863         if (return_code != ACCOUNT_ERROR_NONE)
1864         {
1865                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1866
1867                 goto RETURN;
1868         }
1869
1870         _INFO("before _account_type_get_label_list_by_app_id");
1871         label_list = _account_type_get_label_list_by_app_id (app_id, &return_code);
1872         _INFO("after _account_type_get_label_list_by_app_id=[%d]", return_code);
1873
1874         if (return_code != ACCOUNT_ERROR_NONE)
1875         {
1876                 _ERR("_account_type_get_label_list_by_app_id = [%d]", return_code);
1877                 goto RETURN;
1878         }
1879
1880         label_list_variant = label_list_to_variant (label_list);
1881
1882 RETURN:
1883         if (return_code != ACCOUNT_ERROR_NONE)
1884         {
1885                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1886                 g_dbus_method_invocation_return_gerror (invocation, error);
1887         }
1888         else
1889         {
1890                 _ERR("Calling account_manager_complete_account_type_query_label_by_app_id");
1891                 account_manager_complete_account_type_query_label_by_app_id (obj, invocation, label_list_variant);
1892         }
1893         _INFO("account_manager_handle_account_type_query_label_by_app_id end");
1894
1895         return_code = _account_db_close();
1896         if (return_code != ACCOUNT_ERROR_NONE)
1897         {
1898                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1899                 return_code = ACCOUNT_ERROR_NONE;
1900         }
1901
1902         return true;
1903 }
1904
1905 gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *obj,
1906                                                                                                                         GDBusMethodInvocation *invocation,
1907                                                                                                                         gchar *account_db_path,
1908                                                                                                                         const gchar *app_id)
1909 {
1910         _INFO("account_manager_handle_account_type_query_by_app_id start");
1911         GVariant* account_type_variant = NULL;
1912
1913         guint pid = _get_client_pid(invocation);
1914
1915         _INFO("client Id = [%u]", pid);
1916
1917         int return_code = _check_priviliege_account_read(invocation);
1918         if (return_code != ACCOUNT_ERROR_NONE)
1919         {
1920                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1921                 goto RETURN;
1922         }
1923
1924         return_code = _account_db_open(0, (const char*)account_db_path);
1925         if (return_code != ACCOUNT_ERROR_NONE)
1926         {
1927                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1928
1929                 goto RETURN;
1930         }
1931
1932         _INFO("before _account_type_query_by_app_id");
1933         account_type_s* account_type = NULL;
1934         return_code = _account_type_query_by_app_id (app_id, &account_type);
1935         _INFO("after _account_type_query_by_app_id=[%d]", return_code);
1936
1937         if (return_code != ACCOUNT_ERROR_NONE)
1938         {
1939                 _ERR("_account_type_query_by_app_id = [%d]", return_code);
1940                 goto RETURN;
1941         }
1942         if (account_type == NULL)
1943         {
1944                 _ERR("account_type read is NULL");
1945                 return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
1946                 goto RETURN;
1947         }
1948
1949         account_type_variant = marshal_account_type( account_type);
1950
1951 RETURN:
1952         if (return_code != ACCOUNT_ERROR_NONE)
1953         {
1954                 _ERR("Account SVC is returning error [%d]", return_code);
1955                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
1956                 g_dbus_method_invocation_return_gerror (invocation, error);
1957         }
1958         else
1959         {
1960                 _INFO("Calling account_manager_complete_account_type_query_by_app_id");
1961                 account_manager_complete_account_type_query_by_app_id (obj, invocation, account_type_variant);
1962         }
1963         _INFO("account_manager_handle_account_type_query_by_app_id end");
1964
1965         return_code = _account_db_close();
1966         if (return_code != ACCOUNT_ERROR_NONE)
1967         {
1968                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1969                 return_code = ACCOUNT_ERROR_NONE;
1970         }
1971
1972         return true;
1973 }
1974
1975 gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager *obj,
1976                                                                                                                         GDBusMethodInvocation *invocation,
1977                                                                                                                         gchar *account_db_path, const gchar *app_id)
1978 {
1979         _INFO("account_manager_handle_account_type_query_app_id_exist start");
1980         guint pid = _get_client_pid(invocation);
1981
1982         _INFO("client Id = [%u]", pid);
1983
1984         int return_code = _check_priviliege_account_read(invocation);
1985         if (return_code != ACCOUNT_ERROR_NONE)
1986         {
1987                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
1988                 goto RETURN;
1989         }
1990
1991         return_code = _account_db_open(0, (const char*)account_db_path);
1992         if (return_code != ACCOUNT_ERROR_NONE)
1993         {
1994                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
1995
1996                 goto RETURN;
1997         }
1998
1999         _INFO("before _account_type_query_app_id_exist");
2000         return_code = _account_type_query_app_id_exist (app_id);
2001         _INFO("after _account_type_query_app_id_exist=[%d]", return_code);
2002
2003         if (return_code != ACCOUNT_ERROR_NONE)
2004         {
2005                 _ERR("_account_type_query_app_id_exist = [%d]", return_code);
2006                 goto RETURN;
2007         }
2008
2009 RETURN:
2010         if (return_code != ACCOUNT_ERROR_NONE)
2011         {
2012                 _ERR("Account SVC is returning error [%d]", return_code);
2013                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
2014                 g_dbus_method_invocation_return_gerror (invocation, error);
2015         }
2016         else
2017         {
2018                 _INFO("Calling account_manager_complete_account_type_query_by_app_id");
2019                 account_manager_complete_account_type_query_app_id_exist (obj, invocation);
2020         }
2021         _INFO("account_manager_handle_account_type_query_app_id_exist end");
2022
2023         return_code = _account_db_close();
2024         if (return_code != ACCOUNT_ERROR_NONE)
2025         {
2026                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
2027                 return_code = ACCOUNT_ERROR_NONE;
2028         }
2029
2030         return true;
2031 }
2032
2033 gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *obj,
2034                                                                                                                         GDBusMethodInvocation *invocation,
2035                                                                                                                         gchar *account_db_path,
2036                                                                                                                         GVariant *account_data,
2037                                                                                                                         gint account_id)
2038 {
2039         _INFO("account_manager_handle_account_update_to_db_by_id_ex start");
2040         guint pid = _get_client_pid(invocation);
2041
2042         _INFO("client Id = [%u]", pid);
2043
2044         int return_code = _check_priviliege_account_read(invocation);
2045         if (return_code != ACCOUNT_ERROR_NONE)
2046         {
2047                 _ERR("_check_priviliege_account_read failed, ret = %d", return_code);
2048                 goto RETURN;
2049         }
2050         return_code = _check_priviliege_account_write(invocation);
2051         if (return_code != ACCOUNT_ERROR_NONE)
2052         {
2053                 _ERR("_check_priviliege_account_write failed, ret = %d", return_code);
2054                 goto RETURN;
2055         }
2056
2057         return_code = _account_db_open(1, (const char*)account_db_path);
2058         if (return_code != ACCOUNT_ERROR_NONE)
2059         {
2060                 _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code);
2061
2062                 goto RETURN;
2063         }
2064
2065         account_s* account = umarshal_account(account_data);
2066         if (account == NULL)
2067         {
2068                 _ERR("Unmarshal failed");
2069                 return_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2070                 goto RETURN;
2071         }
2072
2073         _INFO("before _account_update_to_db_by_id_ex");
2074         return_code = _account_update_to_db_by_id_ex (account, account_id);
2075         _INFO("after _account_update_to_db_by_id_ex()=[%d]", return_code);
2076
2077         if (return_code != ACCOUNT_ERROR_NONE)
2078         {
2079                 _ERR("_account_update_to_db_by_id_ex error");
2080                 goto RETURN;
2081         }
2082
2083 RETURN:
2084         if (return_code != ACCOUNT_ERROR_NONE)
2085         {
2086                 _ERR("Account SVC is returning error [%d]", return_code);
2087                 GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound");
2088                 g_dbus_method_invocation_return_gerror (invocation, error);
2089         }
2090         else
2091         {
2092                 _INFO("Calling account_manager_complete_account_update_to_db_by_id_ex");
2093                 account_manager_complete_account_update_to_db_by_id_ex (obj, invocation);
2094         }
2095         _INFO("in account_manager_handle_account_update_to_db_by_id_ex_p end");
2096
2097         return_code = _account_db_close();
2098         if (return_code != ACCOUNT_ERROR_NONE)
2099         {
2100                 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
2101                 return_code = ACCOUNT_ERROR_NONE;
2102         }
2103
2104         return true;
2105 }
2106
2107 static void
2108 on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
2109 {
2110                 _INFO("on_bus_acquired [%s]", name);
2111
2112                 GDBusInterfaceSkeleton* interface = NULL;
2113                 account_mgr_server_obj = account_manager_skeleton_new();
2114                 if (account_mgr_server_obj == NULL)
2115                 {
2116                         _ERR("account_mgr_server_obj NULL!!");
2117                         return;
2118                 }
2119
2120         interface = G_DBUS_INTERFACE_SKELETON(account_mgr_server_obj);
2121                 if (!g_dbus_interface_skeleton_export(interface, connection, ACCOUNT_MGR_DBUS_PATH, NULL))
2122                 {
2123                         _ERR("export failed!!");
2124                         return;
2125                 }
2126
2127                 _INFO("connecting account signals start");
2128
2129                 g_signal_connect(account_mgr_server_obj, "handle_account_add",
2130                                                 G_CALLBACK(account_manager_account_add), NULL);
2131
2132                 g_signal_connect(account_mgr_server_obj, "handle_account_query_all",
2133                                                 G_CALLBACK(account_manager_account_query_all), NULL);
2134
2135                 g_signal_connect(account_mgr_server_obj, "handle_account_type_add",
2136                                                 G_CALLBACK(account_manager_account_type_add), NULL);
2137
2138                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_all",
2139                                                 G_CALLBACK(account_manager_account_type_query_all), NULL);
2140
2141                 g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_id",
2142                                                 G_CALLBACK(account_manager_account_delete_from_db_by_id), NULL);
2143
2144                 g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_user_name",
2145                                                 G_CALLBACK(account_manager_account_delete_from_db_by_user_name), NULL);
2146
2147                 g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_package_name",
2148                                                 G_CALLBACK(account_manager_account_delete_from_db_by_package_name), NULL);
2149
2150                 g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id",
2151                                                 G_CALLBACK(account_manager_account_update_to_db_by_id), NULL);
2152
2153                 g_signal_connect(account_mgr_server_obj, "handle_account_get_total_count_from_db",
2154                                                 G_CALLBACK(account_manager_account_get_total_count_from_db), NULL);
2155
2156                 g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_account_id",
2157                                                 G_CALLBACK(account_manager_handle_account_query_account_by_account_id), NULL);
2158
2159                 g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_user_name",
2160                                                 G_CALLBACK(account_manager_handle_account_update_to_db_by_user_name), NULL);
2161
2162                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_locale",
2163                                                 G_CALLBACK(account_manager_handle_account_type_query_label_by_locale), NULL);
2164
2165                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_provider_feature",
2166                                                 G_CALLBACK(account_manager_handle_account_type_query_by_provider_feature), NULL);
2167
2168                 g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_user_name",
2169                                                 G_CALLBACK(account_manager_handle_account_query_account_by_user_name), NULL);
2170
2171                 g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_package_name",
2172                                                 G_CALLBACK(account_manager_handle_account_query_account_by_package_name), NULL);
2173
2174                 g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability",
2175                                                 G_CALLBACK(account_manager_handle_account_query_account_by_capability), NULL);
2176
2177                 g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability_type",
2178                                                 G_CALLBACK(account_manager_handle_account_query_account_by_capability_type), NULL);
2179
2180                 g_signal_connect(account_mgr_server_obj, "handle_account_query_capability_by_account_id",
2181                                                 G_CALLBACK(account_manager_handle_account_query_capability_by_account_id), NULL);
2182
2183                 g_signal_connect(account_mgr_server_obj, "handle_account_update_sync_status_by_id",
2184                                                 G_CALLBACK(account_manager_handle_account_update_sync_status_by_id), NULL);
2185
2186                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_provider_feature_by_app_id",
2187                                                 G_CALLBACK(account_manager_handle_account_type_query_provider_feature_by_app_id), NULL);
2188
2189
2190                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_supported_feature",
2191                                                 G_CALLBACK(account_manager_handle_account_type_query_supported_feature), NULL);
2192
2193                 g_signal_connect(account_mgr_server_obj, "handle_account_type_update_to_db_by_app_id",
2194                                                 G_CALLBACK(account_manager_handle_account_type_update_to_db_by_app_id), NULL);
2195
2196                 g_signal_connect(account_mgr_server_obj, "handle_account_type_delete_by_app_id",
2197                                                 G_CALLBACK(account_manager_handle_account_type_delete_by_app_id), NULL);
2198
2199                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_app_id",
2200                                                 G_CALLBACK(account_manager_handle_account_type_query_label_by_app_id), NULL);
2201
2202                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_app_id",
2203                                                 G_CALLBACK(account_manager_handle_account_type_query_by_app_id), NULL);
2204
2205
2206                 g_signal_connect(account_mgr_server_obj, "handle_account_type_query_app_id_exist",
2207                                                 G_CALLBACK(account_manager_handle_account_type_query_app_id_exist), NULL);
2208
2209                 g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id_ex",
2210                                                                 G_CALLBACK(account_manager_handle_account_update_to_db_by_id_ex), NULL);
2211
2212                 _INFO("connecting account signals end");
2213
2214                 g_dbus_object_manager_server_set_connection(account_mgr_server_mgr, connection);
2215 }
2216
2217 static void
2218 on_name_acquired (GDBusConnection *connection,
2219                                                 const gchar     *name,
2220                                                 gpointer         user_data)
2221 {
2222                 _INFO("on_name_acquired");
2223
2224 }
2225
2226 static void
2227 on_name_lost (GDBusConnection *connection,
2228                                                 const gchar     *name,
2229                                                 gpointer         user_data)
2230 {
2231                 _INFO("on_name_lost");
2232                 exit (1);
2233 }
2234
2235 static bool _initialize_dbus()
2236 {
2237         _INFO("__initialize_dbus Enter");
2238
2239         owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
2240                              "org.tizen.account.manager",
2241                              G_BUS_NAME_OWNER_FLAGS_NONE,
2242                              on_bus_acquired,
2243                              on_name_acquired,
2244                              on_name_lost,
2245                              NULL,
2246                              NULL);
2247
2248         _INFO("owner_id=[%d]", owner_id);
2249
2250     if(owner_id == 0)
2251     {
2252                         _INFO("gdbus own failed!!");
2253                         return false;
2254         }
2255
2256         _INFO("g_bus_own_name SUCCESS");
2257         return true;
2258 }
2259
2260
2261
2262 static void _initialize()
2263 {
2264 #if !GLIB_CHECK_VERSION(2,35,0)
2265         g_type_init();
2266 #endif
2267         int ret = -1;
2268
2269         if (_initialize_dbus() == false)
2270         {       /* because dbus's initialize
2271                                         failed, we cannot continue any more. */
2272                 _ERR("DBUS Initialization Failed");
2273                 exit(1);
2274         }
2275
2276         ret = cynara_initialize(&p_cynara, NULL);
2277         if(ret != CYNARA_API_SUCCESS) {
2278                 _ERR("CYNARA Initialization fail");
2279                 exit(1);
2280         }
2281 }
2282
2283 int main()
2284 {
2285         GMainLoop *mainloop = NULL;
2286
2287         _INFO("Starting Accounts SVC");
2288
2289         mainloop = g_main_loop_new(NULL, FALSE);
2290
2291         _initialize();
2292
2293         g_main_loop_run(mainloop);
2294
2295         cynara_finish(p_cynara);
2296
2297         _INFO("Ending Accounts SVC");
2298         return 0;
2299 }