From 4a67c30be6dc4fcc6e3662a32c98a28dcdf8b0ab Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Mon, 23 Mar 2015 21:47:33 +0900 Subject: [PATCH 2/2] merged a service daemon of daemonized account from tizen_2.4. Change-Id: Ic90c628a72a255a0fbde9faa11f90debb7fec10e Signed-off-by: jiseob.jang --- CMakeLists.txt | 11 + INSTALL | 33 + LICENSE | 206 ++ libaccounts-svc.manifest | 29 + packaging/account-manager.spec | 128 + packaging/accounts-service.service | 16 + src/accounts/CMakeLists.txt | 1 + src/accounts/account_mgr.xml | 166 + src/accounts/server/CMakeLists.txt | 30 + src/accounts/server/account-server-db.c | 6082 +++++++++++++++++++++++++++++++ src/accounts/server/account-server-db.h | 131 + src/accounts/server/account-server.c | 2139 +++++++++++ src/accounts/server/account-svcd.sh | 1 + 13 files changed, 8973 insertions(+) create mode 100755 CMakeLists.txt create mode 100755 INSTALL create mode 100755 LICENSE create mode 100755 libaccounts-svc.manifest create mode 100755 packaging/account-manager.spec create mode 100644 packaging/accounts-service.service create mode 100755 src/accounts/CMakeLists.txt create mode 100644 src/accounts/account_mgr.xml create mode 100644 src/accounts/server/CMakeLists.txt create mode 100644 src/accounts/server/account-server-db.c create mode 100644 src/accounts/server/account-server-db.h create mode 100644 src/accounts/server/account-server.c create mode 100755 src/accounts/server/account-svcd.sh diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..64ce5f5 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +PROJECT(account-manager C) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR "\${prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include ") +SET(VERSION_MAJOR 0) +SET(VERSION "${VERSION_MAJOR}.0.1") + +ADD_SUBDIRECTORY(src/accounts) diff --git a/INSTALL b/INSTALL new file mode 100755 index 0000000..ddf4e78 --- /dev/null +++ b/INSTALL @@ -0,0 +1,33 @@ +1. make the build directory + + ex) + + $ mkdir build + + +2. change the working directory to the build directory + + ex) + + $ cd build + + +3. run 'cmake' + + $ cmake ${SOURCE_DIR} -DCMAKE_INSTALL_PREFIX=/usr + + ex) + + $ cmake .. -DCMAKE_INSTALL_PREFIX=/usr + + or + + $ cmake .. + + +4. make & make install + + ex) + + $ make -j 2 && make install + diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..f94008a --- /dev/null +++ b/LICENSE @@ -0,0 +1,206 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + diff --git a/libaccounts-svc.manifest b/libaccounts-svc.manifest new file mode 100755 index 0000000..7232dc1 --- /dev/null +++ b/libaccounts-svc.manifest @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packaging/account-manager.spec b/packaging/account-manager.spec new file mode 100755 index 0000000..989b243 --- /dev/null +++ b/packaging/account-manager.spec @@ -0,0 +1,128 @@ + +Name: account-manager +Summary: Account Manager +Version: 0.0.1 +Release: 1 +Group: Social & Content/Other +License: Apache-2.0 +Source0: account-manager-%{version}.tar.gz +Source1: accounts-service.service + +BuildRequires: cmake +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(db-util) +BuildRequires: pkgconfig(dbus-1) +BuildRequires: pkgconfig(capi-base-common) +BuildRequires: pkgconfig(pkgmgr-info) +BuildRequires: pkgconfig(aul) +BuildRequires: pkgconfig(glib-2.0) >= 2.26 +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(gio-unix-2.0) +BuildRequires: pkgconfig(security-server) +BuildRequires: pkgconfig(accounts-svc) +BuildRequires: dbus-python +BuildRequires: python-xml +BuildRequires: python-devel + +Requires(post): /sbin/ldconfig +Requires(post): /usr/bin/sqlite3 +Requires(postun): /sbin/ldconfig + +%description +Account Daemon: no + +%package devel +Summary: Development files for %{name} +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} +%description devel +Development files for %{name} + +%prep +%setup -q + +%build +#export CFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default" +#export CXXFLAGS+=" -Wextra -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings -Wswitch-default -Wnon-virtual-dtor -Wno-c++0x-compat" +#export CFLAGS+=" -Wno-unused-parameter -Wno-empty-body" +#export CXXFLAGS+=" -Wno-unused-parameter -Wno-empty-body" + +#export CFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow -fno-common" +#export CXXFLAGS+=" -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict-overflow" + +export CFLAGS="${CFLAGS} -fPIC -fvisibility=hidden" +cmake . -DCMAKE_INSTALL_PREFIX=/usr + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants +install -m 0644 %SOURCE1 %{buildroot}%{_libdir}/systemd/system/accounts-service.service +ln -s ../accounts-service.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service + +rm -rf %{buildroot}/usr/lib/account-manager + +%post +/sbin/ldconfig +if [ ! -d /opt/usr/dbspace ] +then + mkdir -p /opt/usr/dbspace +fi +if [ ! -f /opt/usr/dbspace/.account.db ] +#rm -rf /opt/usr/dbspace/.account.db* +then + sqlite3 /opt/usr/dbspace/.account.db 'PRAGMA journal_mode = PERSIST; + CREATE TABLE if not exists label (AppId TEXT, Label TEXT, Locale TEXT); + CREATE TABLE if not exists account_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, AppId TEXT, + ServiceProviderId TEXT, IconPath TEXT, SmallIconPath TEXT, MultipleAccountSupport INT); + CREATE TABLE if not exists account_custom (AccountId INTEGER, AppId TEXT, Key TEXT, Value TEXT); + CREATE TABLE if not exists account (id INTEGER PRIMARY KEY AUTOINCREMENT, user_name TEXT, email_address TEXT, display_name TEXT, icon_path TEXT, + source TEXT, package_name TEXT, access_token TEXT, domain_name TEXT, auth_type INTEGER, secret INTEGER, sync_support INTEGER, + txt_custom0 TEXT, txt_custom1 TEXT, txt_custom2 TEXT, txt_custom3 TEXT, txt_custom4 TEXT, + int_custom0 INTEGER, int_custom1 INTEGER, int_custom2 INTEGER, int_custom3 INTEGER, int_custom4 INTEGER); + CREATE TABLE if not exists capability (_id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value INTEGER, + package_name TEXT, user_name TEXT, account_id INTEGER, FOREIGN KEY (account_id) REFERENCES account(id)); + CREATE TABLE if not exists provider_feature (app_id TEXT, key TEXT); +' +fi + +mkdir -p /opt/usr/share/account +chown system:system /opt/usr/dbspace/.account.db +chown system:system /opt/usr/dbspace/.account.db-journal + +chmod 600 /opt/usr/dbspace/.account.db +chmod 600 /opt/usr/dbspace/.account.db-journal + +#set message key value to NULL +#vconftool set -t string db/account/msg '' -g 6514 +vconftool set -tf string db/account/msg '' -s libaccounts-svc -u 200 -g 5000 + +#smack labeling +if [ -f /usr/lib/rpm-plugins/msm.so ] +then + chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db-journal + chsmack -a 'libaccounts-svc::db' /opt/usr/dbspace/.account.db +fi + + +%postun -p /sbin/ldconfig + + + +%files +%manifest libaccounts-svc.manifest +%defattr(-,system,system,-) +%attr(0700,system,system) %{_bindir}/account-svcd +%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service +%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service + +%files devel +%defattr(-,system,system,-) +%attr(0700,system,system) %{_bindir}/account-svcd +%attr(0600,system,system) %{_libdir}/systemd/system/accounts-service.service +%attr(0600,system,system) %{_libdir}/systemd/system/multi-user.target.wants/accounts-service.service diff --git a/packaging/accounts-service.service b/packaging/accounts-service.service new file mode 100644 index 0000000..5d16913 --- /dev/null +++ b/packaging/accounts-service.service @@ -0,0 +1,16 @@ +[Unit] +Description=Accounts service +After=tizen-runtime.target +Requires=tizen-runtime.target + +[Service] +User=system +Gruop=system +SmackProcessLabel=libaccounts-svc +Type=simple +ExecStart=/usr/bin/account-svcd +Restart=always +RestartSec=1 + +[Install] +WantedBy=multi-user.target diff --git a/src/accounts/CMakeLists.txt b/src/accounts/CMakeLists.txt new file mode 100755 index 0000000..7c2af46 --- /dev/null +++ b/src/accounts/CMakeLists.txt @@ -0,0 +1 @@ +ADD_SUBDIRECTORY(server) diff --git a/src/accounts/account_mgr.xml b/src/accounts/account_mgr.xml new file mode 100644 index 0000000..32cf6bf --- /dev/null +++ b/src/accounts/account_mgr.xml @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/accounts/server/CMakeLists.txt b/src/accounts/server/CMakeLists.txt new file mode 100644 index 0000000..2272a1f --- /dev/null +++ b/src/accounts/server/CMakeLists.txt @@ -0,0 +1,30 @@ +SET(DAEMON account-svcd) + +INCLUDE(FindPkgConfig) +pkg_check_modules(pkgs REQUIRED python sqlite3 dlog db-util glib-2.0 gobject-2.0 capi-base-common pkgmgr-info aul dbus-1 gio-2.0 gio-unix-2.0 security-server accounts-svc) + +FOREACH(flag ${pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +SET(SERVER_SRCS + account-server.c + account-server-db.c +) + +ADD_CUSTOM_COMMAND( + WORKING_DIRECTORY + OUTPUT account-mgr-stub.c + COMMAND gdbus-codegen --interface-prefix org.tizen. + --generate-c-code account-mgr-stub ../account_mgr.xml + COMMENT "Generating Account SVC GDBus .c/.h") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -Werror") +SET(CMAKE_LDFLAGS "-Wl,-zdefs") + +ADD_EXECUTABLE(${DAEMON} ${SERVER_SRCS} account-mgr-stub.c) + +TARGET_LINK_LIBRARIES(${DAEMON} ${pkgs_LDFLAGS}) + +INSTALL(TARGETS ${DAEMON} DESTINATION bin) + diff --git a/src/accounts/server/account-server-db.c b/src/accounts/server/account-server-db.c new file mode 100644 index 0000000..39d4ac1 --- /dev/null +++ b/src/accounts/server/account-server-db.c @@ -0,0 +1,6082 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "account-private.h" +#include "account-server-db.h" +#include "dbg.h" +#include "account_ipc_marshal.h" + +typedef sqlite3_stmt* account_stmt; + +#define TEST_APP_ID "org.tizen.MyAccountCoreTest" +#define EAS_CMDLINE "/usr/bin/eas-engine" +#define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service" +#define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv" +#define IMS_AGENT_CMDLINE "/usr/bin/ims-agent" +#define MDM_SERVER_CMDLINE "/usr/bin/mdm-server" + +#define RCS_APPID "com.samsung.rcs-im" +#define IMS_SERVICE_APPID "ims-service" +#define ACTIVESYNC_APPID "activesync-ui" +#define EMAIL_APPID "email-setting-efl" +#define SYNCHRONISE_APPID "setting-synchronise-efl" +#define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent" + +#define FACEBOOK_SDK_APPID "com.samsung.facebook-service" +#define FACEBOOK_APPID "com.samsung.facebook" + +#define ACCOUNT_DB_OPEN_READONLY 0 +#define ACCOUNT_DB_OPEN_READWRITE 1 + +#define _TIZEN_PUBLIC_ +#ifndef _TIZEN_PUBLIC_ +#include + +#endif + +static sqlite3* g_hAccountDB = NULL; +static sqlite3* g_hAccountDB2 = NULL; +pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER; + +static char *_account_get_text(const char *text_data); +static int _account_gslist_free(GSList* list); +static int _account_glist_free(GList* list); +static const char *_account_query_table_column_text(account_stmt pStmt, int pos); +static int _account_insert_custom(account_s *account, int account_id); +static int _account_update_custom(account_s *account, int account_id); +static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ); +static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id); + +int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data ); + +static void _account_insert_delete_update_notification_send(char *noti_name) +{ + if (!noti_name) { + _ERR("Noti Name is NULL!!!!!!\n"); + return; + } + + _INFO("noti_type = %s", noti_name); + + if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) { + _ERR("Vconf MSG Str set FAILED !!!!!!\n");; + } +} + +int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data) +{ + char* appid = NULL; + char* item = NULL; + GSList** appid_list = (GSList**)user_data; + int pkgmgr_ret = -1; + + pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid); + + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret); + } + + item = _account_get_text(appid); + *appid_list = g_slist_append(*appid_list, item); + + return 0; +} + +static inline int __read_proc(const char *path, char *buf, int size) +{ + int fd = 0, ret = 0; + + if (buf == NULL || path == NULL) { + ACCOUNT_ERROR("path and buffer is mandatory\n"); + return -1; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + ACCOUNT_ERROR("fd open error(%d)\n", fd); + return -1; + } + + ret = read(fd, buf, size - 1); + if (ret <= 0) { + ACCOUNT_ERROR("fd read error(%d)\n", fd); + close(fd); + return -1; + } else + buf[ret] = 0; + + close(fd); + + return ret; +} + +char *_account_get_proc_cmdline_bypid(int pid) +{ + char buf[128]; + int ret = 0; + + ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid); + ret = __read_proc(buf, buf, sizeof(buf)); + if (ret <= 0) { + ACCOUNT_DEBUG("No proc directory (%d)\n", pid); + return NULL; + } + + return strdup(buf); +} + + +static char* _account_get_current_appid(int pid) +{ + _INFO("getting caller appid with pid=[%d]", pid); + + int ret=0; + char appid[128]={0,}; + char* appid_ret = NULL; + + ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid)); + + if(ret < 0){ + ACCOUNT_ERROR("fail to get current appid\n"); + } + + _INFO(""); + + /* SLP platform core exception */ + if(strlen(appid) == 0){ + _INFO(""); + char* cmdline = NULL; + cmdline = _account_get_proc_cmdline_bypid(pid); + ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline); + if(!g_strcmp0(cmdline, EAS_CMDLINE)) { + appid_ret = _account_get_text(ACTIVESYNC_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) { + appid_ret = _account_get_text(EMAIL_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) { + if(_account_type_query_app_id_exist(RCS_APPID)==ACCOUNT_ERROR_NONE){ + appid_ret = _account_get_text(RCS_APPID); + } else if(_account_type_query_app_id_exist(IMS_SERVICE_APPID)==ACCOUNT_ERROR_NONE){ + appid_ret = _account_get_text(IMS_SERVICE_APPID); + } else { + appid_ret = _account_get_text(RCS_APPID); + } + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) { + appid_ret = _account_get_text(SYNCHRONISE_APPID); + _ACCOUNT_FREE(cmdline); + return appid_ret; + } else { + ACCOUNT_DEBUG("No app id\n"); + _ACCOUNT_FREE(cmdline); + return NULL; + } + } + + _INFO(""); + /* temporary exception */ + if(!g_strcmp0(appid, "com.samsung.gallery")){ + appid_ret = _account_get_text("com.samsung.facebook"); + } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){ + appid_ret = _account_get_text(FACEBOOK_APPID); + } else { + appid_ret = _account_get_text(appid); + } + + return appid_ret; +} + +static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid) +{ + int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + pkgmgrinfo_appinfo_h ahandle=NULL; + pkgmgrinfo_pkginfo_h phandle=NULL; + char* package_id = NULL; + GSList* appid_list = NULL; + GSList* iter = NULL; + + if(!appid){ + ACCOUNT_ERROR("input param is null\n"); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + if(!verified_appid){ + ACCOUNT_ERROR("output param is null\n"); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + if(!strcmp(appid, "com.samsung.setting")){ + ACCOUNT_DEBUG("Setting exception\n"); + *verified_appid = _account_get_text("com.samsung.setting"); + return ACCOUNT_ERROR_NONE; + } + + if(!strcmp(appid, "com.samsung.samsung-account-front")){ + ACCOUNT_DEBUG("Setting exception\n"); + *verified_appid = _account_get_text("com.samsung.samsung-account-front"); + return ACCOUNT_ERROR_NONE; + } + + if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){ + ACCOUNT_DEBUG("ims service exception\n"); + *verified_appid = _account_get_text(appid); + return ACCOUNT_ERROR_NONE; + } + + /* Get app id family which is stored in account database */ + int pkgmgr_ret = -1; + pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); + } + + /* Compare current app id with the stored app id family */ + for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ + char* tmp = (char*)iter->data; + if(tmp) { + if(_account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) { + *verified_appid = _account_get_text(tmp); + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else { + ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); + } + } + _ACCOUNT_FREE(tmp); + } + + g_slist_free(appid_list); + pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); + } + + pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); + } + + return error_code; +} + +static int _account_check_appid_group_with_package_name(const char* appid, char* package_name) +{ + int error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + pkgmgrinfo_appinfo_h ahandle=NULL; + pkgmgrinfo_pkginfo_h phandle=NULL; + char* package_id = NULL; + GSList* appid_list = NULL; + GSList* iter = NULL; + + if(!appid){ + ACCOUNT_ERROR("input param -appid is null\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!package_name){ + ACCOUNT_ERROR("input param - package name is null\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + /* ims-service Exception */ + if ( strcmp(appid, "ims-service") == 0 && strcmp(package_name, "ims-service") == 0 ) { + ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log. + return ACCOUNT_ERROR_NONE; + } + + /* Get app id family which is stored in account database */ + int pkgmgr_ret = -1; + pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret); + } + pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret); + } + + /* Compare current app id with the stored app id family */ + for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){ + char* tmp = (char*)iter->data; + if(tmp) { + //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log. + if( strcmp(tmp, package_name) == 0) { + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 && + strcmp(package_name, "gr47by21a5.SamsungAccount") == 0 ) { + /* Samung Account Exception */ + error_code = ACCOUNT_ERROR_NONE; + _ACCOUNT_FREE(tmp); + break; + } else { + ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid); + } + } + _ACCOUNT_FREE(tmp); + } + + g_slist_free(appid_list); + pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret); + } + + pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle); + if( pkgmgr_ret != PMINFO_R_OK ){ + ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret); + } + + return error_code; +} + +static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account) +{ + if (account == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (account->package_name) + { + char *caller_package_name = _account_get_current_appid(caller_pid); + if (caller_package_name == NULL) + { + _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (g_strcmp0(caller_package_name, account->package_name) != 0) + { + // packages dont match, so remove sensitive info + _INFO("Removing sensitive info from account id [%d]", account->id); + free (account->access_token); + account->access_token = NULL; + + } + return ACCOUNT_ERROR_NONE; + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list) +{ + if (account_list == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GList *list_iter = NULL; + for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter)) + { + account_s *account = (account_s *) list_iter->data; + return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list) +{ + if (account_list == NULL) + { + _ERR("Null input"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList *list_iter = NULL; + for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter)) + { + account_s *account = (account_s *) list_iter->data; + return _remove_sensitive_info_from_non_owning_account(caller_pid, account); + } + return ACCOUNT_ERROR_INVALID_PARAMETER; +} + +static const char *_account_db_err_msg() +{ + return sqlite3_errmsg(g_hAccountDB); +} + +static int _account_db_err_code() +{ + return sqlite3_errcode(g_hAccountDB); +} + +static int _account_get_record_count(char* query) +{ + _INFO("_account_get_record_count"); + + int rc = -1; + int ncount = 0; + account_stmt pStmt = NULL; + + if(!query){ + _ERR("NULL query\n"); + return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + } + + if(!g_hAccountDB){ + _ERR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + + if (SQLITE_BUSY == rc){ + _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (SQLITE_OK != rc) { + _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + if (SQLITE_BUSY == rc) { + _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (SQLITE_ROW != rc) { + _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + ncount = sqlite3_column_int(pStmt, 0); + + _INFO("account record count [%d]", ncount); + sqlite3_finalize(pStmt); + + return ncount; +} + +static int _account_execute_query(const char *query) +{ + int rc = -1; + char* pszErrorMsg = NULL; + + if(!query){ + ACCOUNT_ERROR("NULL query\n"); + return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR; + } + + if(!g_hAccountDB){ + ACCOUNT_ERROR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg); + if (SQLITE_OK != rc) { + ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg); + sqlite3_free(pszErrorMsg); + } + + return rc; +} + +static int _account_begin_transaction(void) +{ + ACCOUNT_DEBUG("_account_begin_transaction start"); + int ret = -1; + + ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION"); + + if (ret == SQLITE_BUSY){ + ACCOUNT_ERROR(" sqlite3 busy = %d", ret); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if(ret != SQLITE_OK) { + ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret); + return ACCOUNT_ERROR_DB_FAILED; + } + + ACCOUNT_DEBUG("_account_begin_transaction end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_end_transaction(bool is_success) +{ + ACCOUNT_DEBUG("_account_end_transaction start"); + + int ret = -1; + + if (is_success == true) { + ret = _account_execute_query("COMMIT TRANSACTION"); + ACCOUNT_DEBUG("_account_end_transaction COMMIT"); + } else { + ret = _account_execute_query("ROLLBACK TRANSACTION"); + ACCOUNT_DEBUG("_account_end_transaction ROLLBACK"); + } + + if(ret == SQLITE_PERM){ + ACCOUNT_ERROR("Account permission denied :: %d", ret); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (ret == SQLITE_BUSY){ + ACCOUNT_DEBUG(" sqlite3 busy = %d", ret); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (ret != SQLITE_OK) { + ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret); + return ACCOUNT_ERROR_DB_FAILED; + } + + ACCOUNT_DEBUG("_account_end_transaction end"); + return ACCOUNT_ERROR_NONE; +} + +static bool _account_check_add_more_account(const char* app_id) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id); + rc = _account_get_record_count(query); + + /* multiple account support case */ + if(rc > 0) { + ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc); + return TRUE; + } + + /* multiple account not support case */ + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id); + rc = _account_get_record_count(query); + + if(rc <= 0) { + ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id); + return TRUE; + } + + return FALSE; +} + +//TODO: Need to enable creating db on the first connect for +//a) multi-user cases +//b) to ensure db exist in every connect call + +//static int _account_create_all_tables(void) +//{ +// int rc = -1; +// int error_code = ACCOUNT_ERROR_NONE; +// char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + +// ACCOUNT_DEBUG("create all table - BEGIN"); +// ACCOUNT_MEMSET(query, 0, sizeof(query)); + +// /*Create the account table*/ +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_SCHEMA, rc, _account_db_err_msg())); + +//#ifndef _TIZEN_PUBLIC_ +// if (CSC_FEATURE_BOOL_TRUE == csc_feature_get_bool(CSC_FEATURE_DEF_BOOL_CONTACTS_DOCOMO_SOCIAL_PHONEBOOK)) { +// /* NTT docomo specific area */ +// rc = _account_execute_query(DOCOMO_DEFAULT_VAL_INSERT_QUERY); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", DOCOMO_DEFAULT_VAL_INSERT_QUERY, rc, _account_db_err_msg())); +// /* END of NTT docomo specific area */ +// } +//#endif +// } + +// /*Create capability table*/ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(CAPABILITY_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", CAPABILITY_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create account custom table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg())); +// } + +// /* Create account type table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", ACCOUNT_TYPE_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create label table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(LABEL_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", LABEL_SCHEMA, rc, _account_db_err_msg())); +// } + +// /* Create account feature table */ +// ACCOUNT_MEMSET(query, 0, sizeof(query)); +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE); +// rc = _account_get_record_count(query); +// if (rc <= 0) { +// rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA); +// if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY; +// ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", PROVIDER_FEATURE_SCHEMA, rc, _account_db_err_msg())); +// } + +// ACCOUNT_DEBUG("create all table - END"); +// return error_code; +//} + +//static int _account_check_is_all_table_exists() +//{ +// int rc = 0; +// char query[ACCOUNT_SQL_LEN_MAX] = {0,}; +// ACCOUNT_MEMSET(query, 0, sizeof(query)); + +// ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')", +// ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE); +// rc = _account_get_record_count(query); + +// if (rc != ACCOUNT_TABLE_TOTAL_COUNT) { +// ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc); +// } + +// return rc; +//} + +int _account_db_handle_close(sqlite3* hDB) +{ + int rc = 0; + int ret = ACCOUNT_ERROR_NONE; + if(hDB) + { + rc = db_util_close(hDB); + if( rc == SQLITE_OK ) + ret = ACCOUNT_ERROR_NONE; + else if( rc == SQLITE_PERM ) + ret = ACCOUNT_ERROR_PERMISSION_DENIED; + else if ( rc == SQLITE_BUSY ) + ret = ACCOUNT_ERROR_DATABASE_BUSY; + else + ret = ACCOUNT_ERROR_DB_FAILED; + } + return ret; +} + +int _account_db_open(int mode, const char* account_db_path) +{ + int rc = 0; + int ret = -1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + if( g_hAccountDB ) { + ACCOUNT_ERROR( "Account database is using in another app. %x", g_hAccountDB ); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ret = _account_db_handle_close(g_hAccountDB2); + if( ret != ACCOUNT_ERROR_NONE ) + ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret); + + ACCOUNT_DEBUG( "before db_util_open()"); + if(mode == ACCOUNT_DB_OPEN_READWRITE) + rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD); + else if(mode == ACCOUNT_DB_OPEN_READONLY) + rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL); + else + return ACCOUNT_ERROR_DB_NOT_OPENED; + ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc); + + if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) { + ACCOUNT_ERROR( "Account permission denied"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( rc == SQLITE_BUSY ) { + ACCOUNT_ERROR( "busy handler fail."); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if( rc != SQLITE_OK ) { + ACCOUNT_ERROR( "The database isn't connected." ); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + return ACCOUNT_ERROR_NONE; +} + +int _account_db_close(void) +{ + ACCOUNT_DEBUG( "start db_util_close()"); + int ret = -1; +/* + ret = _account_db_handle_close(g_hAccountDB2); + if( ret != ACCOUNT_ERROR_NONE ) + ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret); +*/ + ret = _account_db_handle_close(g_hAccountDB); + if( ret != ACCOUNT_ERROR_NONE ) + { + ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret); + g_hAccountDB2 = g_hAccountDB; + } + g_hAccountDB = NULL; + + return ret; +} + +static int _account_free_capability_items(account_capability_s *data) +{ + _ACCOUNT_FREE(data->type); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->user_name); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_custom_item_free(account_custom_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + _ACCOUNT_FREE(data->value); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_custom_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_custom_s *custom_data = (account_custom_s*)iter->data; + _account_custom_item_free(custom_data); + _ACCOUNT_FREE(custom_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_free_account_items(account_s *data) +{ + _ACCOUNT_FREE(data->user_name); + _ACCOUNT_FREE(data->email_address); + _ACCOUNT_FREE(data->display_name); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->source); + _ACCOUNT_FREE(data->package_name); + _ACCOUNT_FREE(data->domain_name); + _ACCOUNT_FREE(data->access_token); + + int i; + for(i=0;iuser_data_txt[i]); + + _account_gslist_free(data->capablity_list); + _account_glist_free(data->account_list); + _account_custom_gslist_free(data->custom_list); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_gslist_free(GSList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + account_capability_s *cap_data = (account_capability_s*)iter->data; + _account_free_capability_items(cap_data); + _ACCOUNT_FREE(cap_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_glist_free(GList* list) +{ + if(!list){ + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_s *account_record = (account_s*)iter->data; + _account_free_account_items(account_record); + _ACCOUNT_FREE(account_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_check_duplicated(account_s *data, const char* verified_appid) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int count = 0; + int ret = -1; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where package_name='%s' and (user_name='%s' or display_name='%s' or email_address='%s')" + , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address); + + count = _account_get_record_count(query); + + if (count<=0) { + return ACCOUNT_ERROR_NONE; + } + + //check whether duplicated account or not. + //1. check user_name + //2. check display_name + //3. check email_address + GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret); + if (account_list_temp == NULL) + { + _ERR("_account_query_account_by_package_name returned NULL"); + return ACCOUNT_ERROR_DB_FAILED; + } + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if(ret != ACCOUNT_ERROR_NONE){ + return ret; + } + + account_list_temp = g_list_first(account_list_temp); + _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp)); + + GList* iter = NULL; + for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter)) + { + _INFO("iterating account_list_temp"); + account_s *account = NULL; + _INFO("Before iter->data"); + account = (account_s*)iter->data; + _INFO("After iter->data"); + if (account != NULL) + { + if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0) + { + _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name); + return ACCOUNT_ERROR_DUPLICATED; + } + //when user_name is not NULL and display_name is same. + if(account->user_name==NULL && data->user_name==NULL && account->display_name!=NULL && data->display_name!=NULL && strcmp(account->display_name, data->display_name)==0) + { + _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name); + return ACCOUNT_ERROR_DUPLICATED; + } + //when user_name and display_name are not NULL and email_address is same. + if(account->user_name==NULL && data->user_name==NULL && account->display_name==NULL && data->display_name==NULL && account->email_address!=NULL && data->email_address!=NULL && strcmp(account->email_address, data->email_address)==0) + { + _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address); + return ACCOUNT_ERROR_DUPLICATED; + } + } + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_get_next_sequence(const char *pszName) +{ + int rc = 0; + account_stmt pStmt = NULL; + int max_seq = 0; + char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,}; + + ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery)); + ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName); + rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL); + if (SQLITE_OK != rc) { + ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + max_seq = sqlite3_column_int(pStmt, 0); + max_seq++; + + /*Finalize Statement*/ + rc = sqlite3_finalize(pStmt); + pStmt = NULL; + + return max_seq; +} + +static account_stmt _account_prepare_query(char *query) +{ + int rc = -1; + account_stmt pStmt = NULL; + + ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL")); + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + + ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg())); + + return pStmt; +} + +static int _account_query_bind_int(account_stmt pStmt, int pos, int num) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return -1; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return -1; + } + + return sqlite3_bind_int(pStmt, pos, num); +} + +static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str) +{ + _INFO("_account_query_bind_text"); + + if(!pStmt) + { + _ERR("statement is null"); + return -1; + } + + if(str) + { + _INFO("sqlite3_bind_text"); + return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC); + } + else + { + _INFO("sqlite3_bind_null"); + return sqlite3_bind_null(pStmt, pos); + } +} + +static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value) +{ + _INFO("start"); + + int count = 1; + + /*Caution : Keep insert query orders.*/ + + /* 1. user name*/ + _account_query_bind_text(hstmt, count++, (char*)account->user_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name); + + /* 2. email address*/ + _account_query_bind_text(hstmt, count++, (char*)account->email_address); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address); + + /* 3. display name*/ + _account_query_bind_text(hstmt, count++, (char*)account->display_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name); + + /* 4. icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account->icon_path); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path); + + /* 5. source*/ + _account_query_bind_text(hstmt, count++, (char*)account->source); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source); + + /* 6. package name*/ + _account_query_bind_text(hstmt, count++, (char*)account->package_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name); + + /* 7. access token*/ + _account_query_bind_text(hstmt, count++, (char*)account->access_token); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token); + + /* 8. domain name*/ + _account_query_bind_text(hstmt, count++, (char*)account->domain_name); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name); + + /* 9. auth type*/ + _account_query_bind_int(hstmt, count++, account->auth_type); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type); + + /* 10. secret */ + _account_query_bind_int(hstmt, count++, account->secret); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret); + + /* 11. sync_support */ + _account_query_bind_int(hstmt, count++, account->sync_support); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support); + + int i; + + /* 12. user text*/ + for(i=0; i< USER_TXT_CNT; i++) + _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]); + + /* 13. user integer */ + for(i=0; i< USER_INT_CNT; i++) + { + _account_query_bind_int(hstmt, count++, account->user_data_int[i]); + _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]); + } + + _INFO("end"); + + return count; +} + +static int _account_query_finalize(account_stmt pStmt) +{ + int rc = -1; + + if (!pStmt) { + ACCOUNT_ERROR( "pStmt is NULL"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + rc = sqlite3_finalize(pStmt); + if (rc == SQLITE_BUSY){ + ACCOUNT_ERROR(" sqlite3 busy = %d", rc); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_OK) { + ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_step(account_stmt pStmt) +{ + if(!pStmt){ + ACCOUNT_ERROR( "pStmt is NULL"); + return -1; + } + + return sqlite3_step(pStmt); +} + +static int _account_execute_insert_query(account_s *account) +{ + _INFO("_account_execute_insert_query start"); + + int rc = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + /* check whether app id exist in account type db */ + + if (!account->user_name && !account->display_name && !account->email_address) { + _INFO(""); + ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + _INFO(""); + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , " + "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, " + "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + _INFO(""); + _account_convert_account_to_sql(account, hstmt, query); + + _INFO(""); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + _INFO(""); + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + + if( _account_db_err_code() == SQLITE_PERM ) + error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + else + error_code = ACCOUNT_ERROR_DB_FAILED; + } + + _INFO(""); + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO("_account_execute_insert_query end"); + return error_code; +} + +static int _account_insert_capability(account_s *account, int account_id) +{ + _INFO("_account_insert_capability start"); + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_DEBUG( "_account_insert_capability, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + _INFO("_account_insert_capability _account_get_record_count [%s]", query); + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _ERR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value); + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + _INFO("_account_insert_capability _account_query_step[%d]", rc); + + if (rc != SQLITE_DONE) { + _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + _INFO("_account_insert_capability end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_capability(account_s *account, int account_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_ERROR( "_account_update_capability, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if (rc <= 0) { + ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_int(hstmt, count++, (int)account_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name ) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->capablity_list)==0) { + ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name); + + rc = _account_get_record_count(query); + + if (rc <= 0) { + ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_text(hstmt, count++, (char*)account->package_name); + _account_query_bind_text(hstmt, count++, (char*)account->user_name); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES " + "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_capability_s* cap_data = NULL; + cap_data = (account_capability_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, cap_data->type); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, cap_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_int(hstmt, count++, (int)account->id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_table_column_int(account_stmt pStmt, int pos) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return -1; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return -1; + } + + return sqlite3_column_int(pStmt, pos); +} + +static const char *_account_query_table_column_text(account_stmt pStmt, int pos) +{ + if(!pStmt){ + ACCOUNT_ERROR("statement is null"); + return NULL; + } + + if(pos < 0){ + ACCOUNT_ERROR("invalid pos"); + return NULL; + } + + return (const char*)sqlite3_column_text(pStmt, pos); +} + +static void _account_db_data_to_text(const char *textbuf, char **output) +{ + if (textbuf && strlen(textbuf)>0) { + if (*output) { + free(*output); + *output = NULL; + } + *output = strdup(textbuf); + } +} + +static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record) +{ + const char *textbuf = NULL; + + account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID); + ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME); + _account_db_data_to_text(textbuf, &(account_record->user_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS); + _account_db_data_to_text(textbuf, &(account_record->email_address)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME); + _account_db_data_to_text(textbuf, &(account_record->display_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_record->icon_path)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE); + _account_db_data_to_text(textbuf, &(account_record->source)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME); + _account_db_data_to_text(textbuf, &(account_record->package_name)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN); + _account_db_data_to_text(textbuf, &(account_record->access_token)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME); + _account_db_data_to_text(textbuf, &(account_record->domain_name)); + + account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE); + + account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET); + + account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3])); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4); + _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4])); + + account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0); + account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1); + account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2); + account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3); + account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4); +} + +static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record) +{ + const char *textbuf = NULL; + + _INFO("start _account_convert_column_to_capability()"); + capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY); + _account_db_data_to_text(textbuf, &(capability_record->type)); + + capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME); + _account_db_data_to_text(textbuf, &(capability_record->package_name)); + + textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME); + _account_db_data_to_text(textbuf, &(capability_record->user_name)); + + capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID); + _INFO("type = %s, value = %d", capability_record->type, capability_record->value); + _INFO("end _account_convert_column_to_capability()"); +} + +static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record) +{ + _INFO("start _account_convert_column_to_custom()"); + const char *textbuf = NULL; + + custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(custom_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY); + _account_db_data_to_text(textbuf, &(custom_record->key)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE); + _account_db_data_to_text(textbuf, &(custom_record->value)); + _INFO("key = %s, value = %s", custom_record->key, custom_record->value); + _INFO("end _account_convert_column_to_custom()"); +} + +bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data) +{ + account_s *data = (account_s*)user_data; + + account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s)); + + if (cap_data == NULL) + return FALSE; + ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s)); + + cap_data->type = _account_get_text(capability_type); + cap_data->value = capability_value; + _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value); + + data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data); + + return TRUE; +} + + +bool _account_get_custom_text_cb(char* key, char* value, void *user_data) +{ + account_s *data = (account_s*)user_data; + + account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s)); + + if (custom_data == NULL) { + ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n"); + return FALSE; + } + ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s)); + + custom_data->account_id = data->id; + custom_data->app_id = _account_get_text(data->package_name); + custom_data->key = _account_get_text(key); + custom_data->value = _account_get_text(value); + _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value); + + data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data); + + return TRUE; +} + + +static char *_account_get_text(const char *text_data) +{ + char *text_value = NULL; + + if (text_data != NULL) { + text_value = strdup(text_data); + } + return text_value; +} + +static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + if(!old_account) { + ACCOUNT_FATAL("Memory alloc fail\n"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get capability + error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); + + // get custom text + error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); + + // compare + new_account->id = old_account->id; + + //user name + if(!new_account->user_name) { + if(old_account->user_name) + new_account->user_name = _account_get_text(old_account->user_name); + } + + // display name + if(!new_account->display_name) { + if(old_account->display_name) + new_account->display_name = _account_get_text(old_account->display_name); + } + + // email address + if(!new_account->email_address) { + if(old_account->email_address) + new_account->email_address = _account_get_text(old_account->email_address); + } + + // domain name + if(!new_account->domain_name) { + if(old_account->domain_name) + new_account->domain_name = _account_get_text(old_account->domain_name); + } + + // icon path + if(!new_account->icon_path) { + if(old_account->icon_path) + new_account->icon_path = _account_get_text(old_account->icon_path); + } + + // source + if(!new_account->source) { + if(old_account->source) + new_account->source = _account_get_text(old_account->source); + } + + _ACCOUNT_FREE(new_account->package_name); + new_account->package_name = _account_get_text(old_account->package_name); + + // access token + if(!new_account->access_token) { + if(old_account->access_token) + new_account->access_token = _account_get_text(old_account->access_token); + } + + // auth type + if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { + new_account->auth_type = old_account->auth_type; + } + + //secret + if(new_account->secret== ACCOUNT_SECRECY_INVALID) { + new_account->secret = old_account->secret; + } + + // sync support + if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { + new_account->sync_support = old_account->sync_support; + } + + // TODO user text + int i; + for(i=0;iuser_data_txt[i]) { + if(old_account->user_data_txt[i]) + new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); + } + } + + // TODO user int + for(i=0;iuser_data_int[i] == 0) { + new_account->user_data_int[i] = old_account->user_data_int[i]; + } + } + + // capability + + // user custom table + +CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + + + +static int _account_update_account_by_user_name(int pid, account_s *account, const char *user_name, const char *package_name) +{ + int rc = 0, binding_count = 0, count = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n")); + ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n")); + + char* current_appid = NULL; + char* verified_appid = NULL; + + current_appid = _account_get_current_appid(pid); + error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(verified_appid); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to update\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_compare_old_record_by_user_name(account, user_name, package_name); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'" + , ACCOUNT_TABLE, user_name, package_name); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n", + count, user_name, package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB) + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); + + binding_count = _account_convert_account_to_sql(account, hstmt, query); + + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /*update capability*/ + error_code = _account_update_capability_by_user_name(account, user_name, package_name); + + /* update custom */ + error_code = _account_update_custom(account, account->id); + + return error_code; +} + +int _account_insert_to_db(account_s* account, int pid, int *account_id) +{ + _INFO(""); + int error_code = ACCOUNT_ERROR_NONE; + int ret_transaction = 0; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL")); + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_s *data = (account_s*)account; + ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__); + + pthread_mutex_lock(&account_mutex); + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if(_account_db_err_code() == SQLITE_PERM){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) { + ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + _INFO(""); + *account_id = _account_get_next_sequence(ACCOUNT_TABLE); + data->id = *account_id; + + char* appid = NULL; + appid = _account_get_current_appid(pid); + _INFO(""); + + if(!appid) + { + _INFO(""); + // API caller cannot be recognized + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER; + } + + _INFO(""); + char* verified_appid = NULL; + error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX + if(error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction); + _ACCOUNT_FREE(verified_appid); + _ACCOUNT_FREE(appid); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + if(verified_appid) + { + _INFO(""); + error_code = _account_check_duplicated(data, verified_appid); + if (error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + if(!_account_check_add_more_account(verified_appid)) { + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + _ACCOUNT_FREE(verified_appid); + return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; + } + + _ACCOUNT_FREE(data->package_name); + data->package_name = _account_get_text(verified_appid); + _ACCOUNT_FREE(verified_appid); + } + + if(!_account_check_add_more_account(data->package_name)) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE; + } + + error_code = _account_execute_insert_query(data); + + if (error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + error_code = _account_insert_capability(data, *account_id); + if (error_code != ACCOUNT_ERROR_NONE) + { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + error_code = _account_insert_custom(data, *account_id); + if (error_code != ACCOUNT_ERROR_NONE) + { + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction); + *account_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + _INFO(""); + + pthread_mutex_unlock(&account_mutex); + _account_end_transaction(TRUE); + ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id); + _account_insert_delete_update_notification_send(buf); + _INFO("(%s)-(%d) account _notification_send."); + + return ACCOUNT_ERROR_NONE; + +} + +int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_capability_s* capability_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + capability_record = (account_capability_s*) malloc(sizeof(account_capability_s)); + + if (capability_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s)); + + _account_convert_column_to_capability(hstmt, capability_record); + + cb_ret = callback(capability_record->type, capability_record->value, user_data); + + _account_free_capability_items(capability_record); + _ACCOUNT_FREE(capability_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList* capability_list = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_capability_s* capability_record = NULL; + + while (rc == SQLITE_ROW) { + capability_record = (account_capability_s*) malloc(sizeof(account_capability_s)); + + if (capability_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s)); + + _account_convert_column_to_capability(hstmt, capability_record); + + //cb_ret = callback(capability_record->type, capability_record->value, user_data); + + //_account_free_capability_items(capability_record); + //_ACCOUNT_FREE(capability_record); + + //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + capability_list = g_slist_append(capability_list, capability_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return capability_list; +} + +static int _account_compare_old_record(account_s *new_account, int account_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get capability + error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error")); + + // get custom text + error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account); + ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error")); + + // compare + + new_account->id = old_account->id; + + //user name + if(!new_account->user_name) { + if(old_account->user_name) + new_account->user_name = _account_get_text(old_account->user_name); + } + + // display name + if(!new_account->display_name) { + if(old_account->display_name) + new_account->display_name = _account_get_text(old_account->display_name); + } + + // email address + if(!new_account->email_address) { + if(old_account->email_address) + new_account->email_address = _account_get_text(old_account->email_address); + } + + // domain name + if(!new_account->domain_name) { + if(old_account->domain_name) + new_account->domain_name = _account_get_text(old_account->domain_name); + } + + // icon path + if(!new_account->icon_path) { + if(old_account->icon_path) + new_account->icon_path = _account_get_text(old_account->icon_path); + } + + // source + if(!new_account->source) { + if(old_account->source) + new_account->source = _account_get_text(old_account->source); + } + + _ACCOUNT_FREE(new_account->package_name); + new_account->package_name = _account_get_text(old_account->package_name); + + // access token + if(!new_account->access_token) { + if(old_account->access_token) + new_account->access_token = _account_get_text(old_account->access_token); + } + + // user text + int i; + for(i=0;iuser_data_txt[i]) { + if(old_account->user_data_txt[i]) + new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]); + } + } + + // auth type + if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) { + new_account->auth_type = old_account->auth_type; + } + + //secret + if(new_account->secret== ACCOUNT_SECRECY_INVALID) { + new_account->secret = old_account->secret; + } + + // sync support + if(new_account->sync_support == ACCOUNT_SYNC_INVALID) { + new_account->sync_support = old_account->sync_support; + } + + // user int + for(i=0;iuser_data_int[i] == 0) { + new_account->user_data_int[i] = old_account->user_data_int[i]; + } + } + + // capability + + // user custom table + +CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_get_package_name_from_account_id(int account_id, char **package_name) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *old_account = NULL; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + old_account = (account_s*)calloc(1, sizeof(account_s)); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, old_account); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + // get package name. + *package_name = _account_get_text(old_account->package_name); + + + CATCH: + if (old_account) { + _account_free_account_items(old_account); + _ACCOUNT_FREE(old_account); + } + + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return error_code; + +} + +static int _account_update_account(int pid, account_s *account, int account_id) +{ + int rc = 0, binding_count =0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; + account_stmt hstmt = NULL; + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + /* Check permission of requested appid */ + char* current_appid = NULL; + char *package_name = NULL; + + current_appid = _account_get_current_appid(pid); + error_code = _account_get_package_name_from_account_id(account_id, &package_name); + + if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){ + ACCOUNT_ERROR("No package name with account_id\n"); + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name); + ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later. + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to update\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_compare_old_record(account, account_id); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + if (count <= 0) { + ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE))); + + binding_count = _account_convert_account_to_sql(account, hstmt, query); + _account_query_bind_int(hstmt, binding_count++, account_id); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO("update query=%s", query); + + /*update capability*/ + error_code = _account_update_capability(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + + /* update custom */ + error_code = _account_update_custom(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + + ret_transaction = _account_end_transaction(TRUE); + + _INFO("update end"); + return error_code; +} + + +static int _account_update_account_ex(account_s *account, int account_id) +{ + int rc = 0, binding_count =0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0; + account_stmt hstmt = NULL; + + if (!account->package_name) { + ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + _account_compare_old_record(account, account_id); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (!account->user_name && !account->display_name && !account->email_address) { + ACCOUNT_ERROR("One field should be set among user name, display name, email address\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id = %d ", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + if (count <= 0) { + ACCOUNT_DEBUG(" Account record not found, count = %d\n", count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, " + "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?," + "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, " + "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE id=? ", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s)(%x).\n", _account_db_err_msg(), _account_end_transaction(FALSE))); + + _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name); + binding_count = _account_convert_account_to_sql(account, hstmt, query); + _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name); + _INFO("account_update_to_db_by_id_ex_p : before bind()"); + rc = _account_query_bind_int(hstmt, binding_count++, account_id); + _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc); + + _INFO("account_update_to_db_by_id_ex_p : before update_capability()"); + /*update capability*/ + error_code = _account_update_capability(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + _INFO("account_update_to_db_by_id_ex_p : after update_capability()"); + + _INFO("account_update_to_db_by_id_ex_p : before update_custom()"); + /* update custom */ + error_code = _account_update_custom(account, account_id); + if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){ + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction); + return error_code; + } + _INFO("account_update_to_db_by_id_ex_p : after update_custom()"); + + ret_transaction = _account_end_transaction(TRUE); + + return error_code; +} + + +int _account_update_to_db_by_id(int pid, account_s* account, int account_id) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + int error_code = ACCOUNT_ERROR_NONE; + account_s* data = (account_s*)account; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_update_account(pid, data, account_id); + + if(error_code != ACCOUNT_ERROR_NONE) { + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); + _account_insert_delete_update_notification_send(buf); + + return ACCOUNT_ERROR_NONE; +} + +int _account_update_to_db_by_id_ex(account_s* account, int account_id) +{ + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + int error_code = ACCOUNT_ERROR_NONE; + account_s* data = account; + + pthread_mutex_lock(&account_mutex); + + _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name); + error_code = _account_update_account_ex(data, account_id); + _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name); + + if(error_code != ACCOUNT_ERROR_NONE) { + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id); + _account_insert_delete_update_notification_send(buf); + + return ACCOUNT_ERROR_NONE; +} + + +int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name) +{ + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int error_code = ACCOUNT_ERROR_NONE; + account_s *data = (account_s*)account; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_update_account_by_user_name(pid, data, user_name, package_name); + + pthread_mutex_unlock(&account_mutex); + + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id); + _account_insert_delete_update_notification_send(buf); + + return error_code; +} + +GSList* _account_db_query_all(int pid) +{ + //int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_list = NULL; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return NULL; + } + + rc = _account_query_step(hstmt); + + account_s *account_record = NULL; + + if (rc != SQLITE_ROW) + { + _ERR("The record isn't found"); + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + _account_convert_column_to_account(hstmt, account_record); + account_list = g_slist_append(account_list, account_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) { + account_s *account = NULL; + account = (account_s*)iter->data; + _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account); + } + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error")); + hstmt = NULL; + } + if (account_list) + { + _remove_sensitive_info_from_non_owning_account_slist(pid, account_list); + } + return account_list; +} + +int _account_update_sync_status_by_id(int account_db_id, const int sync_status) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int count =1; + + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) { + ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + pthread_mutex_lock(&account_mutex); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_db_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE id = %d", ACCOUNT_TABLE, account_db_id); + hstmt = _account_prepare_query(query); + + _account_query_bind_int(hstmt, count, sync_status); + + rc = _account_query_step(hstmt); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED, + ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg())); + + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("_account_query_finalize error"); + pthread_mutex_unlock(&account_mutex); + return rc; + } + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id); + _account_insert_delete_update_notification_send(buf); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record) +{ + ACCOUNT_DEBUG("account_db_id=[%d]", account_db_id); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_DEBUG("starting db operations"); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id = %d", ACCOUNT_TABLE, account_db_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_DEBUG("before _account_query_step"); + rc = _account_query_step(hstmt); + ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + while (rc == SQLITE_ROW) { + ACCOUNT_DEBUG("before _account_convert_column_to_account"); + _account_convert_column_to_account(hstmt, account_record); + ACCOUNT_DEBUG("after _account_convert_column_to_account"); + ACCOUNT_DEBUG("user_name = %s, user_txt[0] = %s, user_int[1] = %d", account_record->user_name, account_record->user_data_txt[0], account_record->user_data_int[1]); + rc = _account_query_step(hstmt); + } + + ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + + ACCOUNT_DEBUG("before _account_query_capability_by_account_id"); + _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); + ACCOUNT_DEBUG("after _account_query_capability_by_account_id"); + + ACCOUNT_DEBUG("before _account_query_custom_by_account_id"); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); + ACCOUNT_DEBUG("after _account_query_custom_by_account_id"); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + if (account_record) + { + _remove_sensitive_info_from_non_owning_account(pid, account_record); + } + pthread_mutex_unlock(&account_mutex); + ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code); + return error_code; +} + +GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + account_s *account_head = NULL; + + if (user_name == NULL) + { + _ERR("USER NAME IS NULL"); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto CATCH; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if (_account_db_err_code() == SQLITE_PERM) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + goto CATCH; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_ROW) + { + _ERR("The record isn't found"); + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto CATCH; + } + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + goto CATCH; + } + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + goto CATCH; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + goto CATCH; + } + + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_h account; + account = (account_h)iter->data; + + account_s *testaccount = (account_s*)account; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) + { + _ERR("finalize error"); + *error_code = rc; + } + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* +_account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL")); + + if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) { + ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + return NULL; + } + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, capability_type); + _account_query_bind_int(hstmt, binding_count++, capability_value); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_h account = NULL; + account = (account_h)iter->data; + account_s* testaccount = (account_s*)account; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, + NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, capability_type); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_s* testaccount = (account_s*)iter->data; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + if (account_head) + { + _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code) +{ + _INFO("_account_query_account_by_package_name"); + + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + + account_s* account_head = NULL; + + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name)); + + int tmp = 0; + + account_head = (account_s*) malloc(sizeof(account_s)); + if (account_head == NULL) { + ACCOUNT_FATAL("malloc Failed"); + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY; + return NULL; + } + ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s)); + + while (rc == SQLITE_ROW) { + account_s* account_record = NULL; + + account_record = (account_s*) malloc(sizeof(account_s)); + + if (account_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s)); + + _account_convert_column_to_account(hstmt, account_record); + + _INFO("Adding account_list"); + account_head->account_list = g_list_append(account_head->account_list, account_record); + + rc = _account_query_step(hstmt); + tmp++; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GList *iter; + + tmp = g_list_length(account_head->account_list); + + for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) { + account_s* testaccount = (account_s*)iter->data; + + _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount); + _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount); + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL) + { + _INFO("Returning account_list"); + _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list); + return account_head->account_list; + } + return NULL; +} + +int _account_delete(int pid, int account_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int count = -1; + /* Check requested ID to delete */ + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE id=%d", ACCOUNT_TABLE, account_id); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* Check permission of requested appid */ + char* current_appid = NULL; + char *package_name = NULL; + + current_appid = _account_get_current_appid(pid); + + error_code = _account_get_package_name_from_account_id(account_id, &package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No package name with account_id\n"); + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE id = %d", ACCOUNT_TABLE, account_id); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete custom data */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc)); + hstmt = NULL; + + is_success = TRUE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if(rc != ACCOUNT_ERROR_NONE ){ + ACCOUNT_ERROR("rc (%d)", rc); + is_success = FALSE; + } + + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); + _account_insert_delete_update_notification_send(buf); + } + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; + +} + +static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account) +{ + //FIXME + //return -1; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int binding_count = 1; + + ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL")); + ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, username); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_s *account_record = (account_s *)(*account); + + while (rc == SQLITE_ROW) { + _account_convert_column_to_account(hstmt, account_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record); + _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_create(account_h *account) +{ + if (!account) { + ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_s *data = (account_s*)malloc(sizeof(account_s)); + + if (data == NULL) { + ACCOUNT_FATAL("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + ACCOUNT_MEMSET(data, 0, sizeof(account_s)); + + /*Setting account as visible by default*/ + data->secret = ACCOUNT_SECRECY_VISIBLE; + + /*Setting account as not supporting sync by default*/ + data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT; + + *account = (account_h)data; + + return ACCOUNT_ERROR_NONE; +} + +int _account_destroy(account_h account) +{ + account_s *data = (account_s*)account; + + ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!")); + + _account_free_account_items(data); + _ACCOUNT_FREE(data); + + return ACCOUNT_ERROR_NONE; +} + +int _account_get_account_id(account_s* account, int *account_id) +{ + if (!account) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (!account_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + *account_id = account->id; + + return ACCOUNT_ERROR_NONE; +} + +int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name) +{ + _INFO("[%s][%s]", user_name, package_name); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + account_h account = NULL; + int binding_count = 1; + int account_id = -1; + + ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!")); + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + /* Check permission of requested appid */ + char* current_appid = NULL; + char* package_name_temp = NULL; + + current_appid = _account_get_current_appid(pid); + + package_name_temp = _account_get_text(package_name); + + ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name_temp); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_create(&account); + rc = _account_query_account_by_username_and_package(user_name, package_name, &account); + + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ) + { + _account_destroy(account); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + account_s* account_data = (account_s*)account; + + rc = _account_get_account_id(account_data, &account_id); + + rc = _account_destroy(account); + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ) + { + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + /* delete custom data */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_int(hstmt, binding_count++, account_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete capability */ + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + _INFO(""); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _INFO(""); + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, user_name); + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. user_name=%s, package_name=%s, rc=%d\n", user_name, package_name, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id); + _account_insert_delete_update_notification_send(buf); + } + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission) +{ + _INFO("_account_delete_from_db_by_package_name"); + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + int ret_transaction = 0; + bool is_success = FALSE; + int binding_count = 1; + GSList *account_id_list = NULL; + int ret = -1; + + ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + /* Check permission of requested appid */ + if(permission){ + char* current_appid = NULL; + char* package_name_temp = NULL; + + current_appid = _account_get_current_appid(pid); + + package_name_temp = _account_get_text(package_name); + + ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp); + + error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp); + + _ACCOUNT_FREE(current_appid); + _ACCOUNT_FREE(package_name_temp); + + if(error_code != ACCOUNT_ERROR_NONE){ + ACCOUNT_ERROR("No permission to delete\n"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + } + + // It only needs list of ids, does not need to query sensitive info. So sending 0 + GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret); + if (account_list_temp == NULL) + { + _ERR("_account_query_account_by_package_name returned NULL"); + return ACCOUNT_ERROR_DB_FAILED; + } + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if(ret != ACCOUNT_ERROR_NONE){ + return ret; + } + + account_list_temp = g_list_first(account_list_temp); + _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp)); + + GList* iter = NULL; + for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter)) + { + _INFO("iterating account_list_temp"); + account_s *account = NULL; + _INFO("Before iter->data"); + account = (account_s*)iter->data; + _INFO("After iter->data"); + if (account != NULL) + { + char* id = (char*) calloc(1, sizeof(account->id)); + + sprintf(id, "%d", account->id); + + _INFO("Adding account id [%s]", id); + account_id_list = g_slist_append(account_id_list, g_strdup(id)); + } + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + /* delete custom table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + _account_end_transaction(FALSE); + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete capability table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /* delete account table */ + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, package_name); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. package_name=%s, rc=%d\n", package_name, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } else { + if (is_success == true) { + GSList* gs_iter = NULL; + for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) { + char* p_tmpid = NULL; + p_tmpid = (char*)gs_iter->data; + char buf[64]={0,}; + ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid); + ACCOUNT_SLOGD("%s", buf); + _account_insert_delete_update_notification_send(buf); + _ACCOUNT_FREE(p_tmpid); + } + g_slist_free(account_id_list); + } + } + + pthread_mutex_unlock(&account_mutex); + + _INFO("_account_delete_from_db_by_package_name end"); + return error_code; +} + +int _account_get_total_count_from_db(gboolean include_hidden, int *count) +{ + if (!count) { + ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!g_hAccountDB){ + ACCOUNT_ERROR("DB is not opened\n"); + return ACCOUNT_ERROR_DB_NOT_OPENED; + } + + char query[1024] = {0, }; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + if (include_hidden) + { + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE); + } + else + { + ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE); + } + + *count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + int rc = -1; + int ncount = 0; + account_stmt pStmt = NULL; + + rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL); + if (SQLITE_OK != rc) { + ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg()); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = sqlite3_step(pStmt); + if (SQLITE_ROW != rc) { + ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n"); + sqlite3_finalize(pStmt); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ncount = sqlite3_column_int(pStmt, 0); + + *count = ncount; + + sqlite3_finalize(pStmt); + + if (ncount < 0) { + ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount); + return ACCOUNT_ERROR_DB_FAILED; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_label_items(label_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->label); + _ACCOUNT_FREE(data->locale); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_feature_items(provider_feature_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->key); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_gslist_free(GSList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL")); + + GSList* iter; + + for (iter = list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = (label_s*)iter->data; + _account_type_free_label_items(label_data); + _ACCOUNT_FREE(label_data); + } + + g_slist_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_item_free(account_type_s *data) +{ + _ACCOUNT_FREE(data->app_id); + _ACCOUNT_FREE(data->service_provider_id); + _ACCOUNT_FREE(data->icon_path); + _ACCOUNT_FREE(data->small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_glist_free(GList* list) +{ + ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL")); + + GList* iter; + + for (iter = list; iter != NULL; iter = g_list_next(iter)) { + account_type_s *account_type_record = (account_type_s*)iter->data; + _account_type_item_free(account_type_record); + _ACCOUNT_FREE(account_type_record); + } + + g_list_free(list); + list = NULL; + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_free_account_type_items(account_type_s *data) +{ + _account_type_item_free(data); + + _account_type_gslist_free(data->label_list); + _account_type_glist_free(data->account_type_list); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_create(account_type_h *account_type) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s)); + + if (data == NULL) { + ACCOUNT_ERROR("Memory Allocation Failed"); + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + + ACCOUNT_MEMSET(data, 0, sizeof(account_type_s)); + + *account_type = (account_type_h)data; + + return ACCOUNT_ERROR_NONE; +} + +int account_type_destroy(account_type_h account_type) +{ + account_type_s *data = (account_type_s*)account_type; + + ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!")); + + _account_type_free_account_type_items(data); + _ACCOUNT_FREE(data); + + return ACCOUNT_ERROR_NONE; +} + +//app_id mandatory field +int account_type_set_app_id(account_type_h account_type, const char *app_id) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->app_id); + data->app_id = _account_get_text(app_id); + + return ACCOUNT_ERROR_NONE; +} + +//service_provider_id mandatory field +int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->service_provider_id); + data->service_provider_id = _account_get_text(service_provider_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_icon_path(account_type_h account_type, const char *icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->icon_path); + data->icon_path = _account_get_text(icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + _ACCOUNT_FREE(data->small_icon_path); + data->small_icon_path = _account_get_text(small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__)); + + account_type_s *data = (account_type_s*)account_type; + + data->multiple_account_support = multiple_account_support; + + return ACCOUNT_ERROR_NONE; +} + +// unset? +int account_type_set_label(account_type_h account_type, const char* label, const char* locale) +{ + if (!account_type) { + ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if(!label || !locale) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + label_s *label_data = (label_s*)malloc(sizeof(label_s)); + + if (label_data == NULL) { + return ACCOUNT_ERROR_OUT_OF_MEMORY; + } + ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + + label_data->label = _account_get_text(label); + label_data->locale = _account_get_text(locale); + + data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_app_id(account_type_h account_type, char **app_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*app_id) = NULL; + *app_id = _account_get_text(data->app_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!service_provider_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*service_provider_id) = NULL; + *service_provider_id = _account_get_text(data->service_provider_id); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_icon_path(account_type_h account_type, char **icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*icon_path) = NULL; + *icon_path = _account_get_text(data->icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!small_icon_path) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + (*small_icon_path) = NULL; + *small_icon_path = _account_get_text(data->small_icon_path); + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support) +{ + if (!account_type) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + if (!multiple_account_support) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + account_type_s *data = (account_type_s*)account_type; + + *multiple_account_support = data->multiple_account_support; + + return ACCOUNT_ERROR_NONE; +} + +int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; + + label_data = (label_s*)iter->data; + + *label = NULL; + + if(!strcmp(locale, label_data->locale)) { + *label = _account_get_text(label_data->label); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_RECORD_NOT_FOUND; +} + +int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) { + label_s *label_data = NULL; + + label_data = (label_s*)iter->data; + + if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_NONE; +} + +static gboolean _account_type_check_duplicated(account_type_s *data) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int count = 0; + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'" + , ACCOUNT_TYPE_TABLE, data->app_id); + + count = _account_get_record_count(query); + if (count > 0) { + return TRUE; + } + + return FALSE; +} + +static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value) +{ + _INFO(""); + + int count = 1; + + /*Caution : Keep insert query orders.*/ + + /* 1. app id*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->app_id); + + /* 2. service provider id*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id); + + /* 3. icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path); + + /* 4. small icon path*/ + _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path); + + /* 5. multiple accont support*/ + _account_query_bind_int(hstmt, count++, account_type->multiple_account_support); + + _INFO(""); + + return count; +} + + +static int _account_type_execute_insert_query(account_type_s *account_type) +{ + _INFO(""); + + int rc = 0; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + /* check mandatory field */ + // app id & service provider id + if (!account_type->app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values " + "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE); + + _INFO(""); + hstmt = _account_prepare_query(query); + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + _INFO(""); + _account_type_convert_account_to_sql(account_type, hstmt, query); + _INFO(""); + + rc = _account_query_step(hstmt); + if (rc == SQLITE_BUSY) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + error_code = ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + error_code = ACCOUNT_ERROR_DB_FAILED; + } + + _INFO(""); + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + _INFO(""); + return error_code; +} + +static int _account_type_insert_label(account_type_s *account_type) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->label_list)==0) { + ACCOUNT_ERROR( "_account_type_insert_label, no label\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + GSList *iter; + + for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " + "(?, ?, ?) ", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + label_s* label_data = NULL; + label_data = (label_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->label); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record) +{ + const char *textbuf = NULL; + + textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(feature_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY); + _account_db_data_to_text(textbuf, &(feature_record->key)); + +} + +GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code) +{ + _INFO("_account_type_query_provider_feature_by_app_id"); + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + GSList* feature_list = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE); + _INFO("account query=[%s]", query); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n")); + + provider_feature_s* feature_record = NULL; + + while (rc == SQLITE_ROW) { + + feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s)); + + if (feature_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s)); + + _account_type_convert_column_to_provider_feature(hstmt, feature_record); + + _INFO("Adding account feature_list"); + feature_list = g_slist_append(feature_list, feature_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + + _INFO("Returning account feature_list"); + return feature_list; +} + +int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + provider_feature_s* feature_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s)); + + if (feature_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s)); + + _account_type_convert_column_to_provider_feature(hstmt, feature_record); + + cb_ret = callback(feature_record->app_id, feature_record->key, user_data); + + _account_type_free_feature_items(feature_record); + _ACCOUNT_FREE(feature_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code) +{ + _INFO("_account_type_query_supported_feature start"); + + *error_code = ACCOUNT_ERROR_NONE; + + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int record_count = 0; + + if (app_id == NULL || capability == NULL) + { + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + return false; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability); + + record_count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return false; + } + + if (record_count <= 0) + { + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + return false; + } + + _INFO("_account_type_query_supported_feature end"); + return true; + +} + + +int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + + GSList *iter; + account_type_s *data = (account_type_s*)account_type; + + for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; + + feature_data = (provider_feature_s*)iter->data; + + if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) { + ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n"); + return ACCOUNT_ERROR_NONE; + } + } + + return ACCOUNT_ERROR_NONE; +} + +int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null")); + ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null")); + + account_type_s *data = (account_type_s*)account_type; + + GSList *iter = NULL; + bool b_is_new = TRUE; + + for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + provider_feature_s *feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + if(!strcmp(feature_data->key, provider_feature)) { + b_is_new = FALSE; + break; + } + } + + if(b_is_new) { + provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + + if (feature_data == NULL) + return ACCOUNT_ERROR_OUT_OF_MEMORY; + ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); + + feature_data->key = _account_get_text(provider_feature); + data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + + if (g_slist_length( account_type->provider_feature_list)==0) { + ACCOUNT_ERROR( "no capability\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " + "(?, ?) ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + provider_feature_s* feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, feature_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id) +{ + _INFO(""); + + int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL")); + ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL")); + + account_type_s *data = (account_type_s*)account_type; + + pthread_mutex_lock(&account_mutex); + + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + _INFO(""); + + if( _account_db_err_code() == SQLITE_PERM ){ + pthread_mutex_unlock(&account_mutex); + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _INFO(""); + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + _INFO(""); + if (_account_type_check_duplicated(data)) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction); + *account_type_id = -1; + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DUPLICATED; + } else { + _INFO(""); + *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE); + + error_code = _account_type_execute_insert_query(data); + + if (error_code != ACCOUNT_ERROR_NONE){ + error_code = ACCOUNT_ERROR_DUPLICATED; + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction); + *account_type_id = -1; + pthread_mutex_unlock(&account_mutex); + return error_code; + } + } + + _INFO(""); + error_code = _account_type_insert_provider_feature(data, data->app_id); + if(error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + _INFO(""); + error_code = _account_type_insert_label(data); + if(error_code != ACCOUNT_ERROR_NONE) { + _INFO(""); + ret_transaction = _account_end_transaction(FALSE); + ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction); + pthread_mutex_unlock(&account_mutex); + return error_code; + } + + ret_transaction = _account_end_transaction(TRUE); + _INFO(""); + pthread_mutex_unlock(&account_mutex); + + _INFO(""); + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->provider_feature_list)==0) { + ACCOUNT_ERROR( "no feature\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_DEBUG( "app id", app_id); + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + count = 1; + _account_query_bind_text(hstmt, count++, app_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES " + "(?, ?) ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + provider_feature_s* feature_data = NULL; + feature_data = (provider_feature_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, feature_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_type_update_label(account_type_s *account_type, const char* app_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account_type->label_list)==0) { + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + count = 1; + _account_query_bind_text(hstmt, count++, app_id); + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES " + "(?, ?, ?) ", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + label_s* label_data = NULL; + label_data = (label_s*)iter->data; + + ret = _account_query_bind_text(hstmt, count++, account_type->app_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->label); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, label_data->locale); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + return ACCOUNT_ERROR_NONE; +} + + +static int _account_type_update_account(account_type_s *account_type, const char* app_id) +{ + int rc = 0, binding_count =1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + + if (!account_type->app_id) { + ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n"); + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, " + "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if (_account_db_err_code() == SQLITE_BUSY){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg())); + + binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query); + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + /*update label*/ + error_code = _account_type_update_label(account_type, app_id); + /* update provider feature */ + error_code = _account_type_update_provider_feature(account_type, app_id); + + return error_code; +} + +int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id) +{ + ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + int error_code = ACCOUNT_ERROR_NONE; + account_type_s* data = account_type; + + pthread_mutex_lock(&account_mutex); + + error_code = _account_type_update_account(data, app_id); + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +int _account_type_delete_by_app_id(const char* app_id) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, count = -1; + int ret_transaction = 0; + int binding_count = 1; + bool is_success = FALSE; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected.")); + + /* Check requested ID to delete */ + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); + + count = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (count <= 0) { + ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* transaction control required*/ + ret_transaction = _account_begin_transaction(); + + if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + }else if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction); + pthread_mutex_unlock(&account_mutex); + return ret_transaction; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + binding_count = 1; + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + + binding_count = 1; + ACCOUNT_MEMSET(query, 0, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE); + + hstmt = _account_prepare_query(query); + ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED, + ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg())); + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc)); + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + is_success = TRUE; + + hstmt = NULL; + + CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + ret_transaction = _account_end_transaction(is_success); + + if (ret_transaction != ACCOUNT_ERROR_NONE) { + ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success); + } + + pthread_mutex_unlock(&account_mutex); + + return error_code; +} + +static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record) +{ + const char *textbuf = NULL; + + account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(account_type_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID); + _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_type_record->icon_path)); + + textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH); + _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path)); + + account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT); + +} + +static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record) +{ + const char *textbuf = NULL; + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID); + _account_db_data_to_text(textbuf, &(label_record->app_id)); + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL); + _account_db_data_to_text(textbuf, &(label_record->label)); + + textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE); + _account_db_data_to_text(textbuf, &(label_record->locale)); + +} + +GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code ) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + GSList* label_list = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + return NULL; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt, label_record); + + _INFO("Adding account label_list"); + label_list = g_slist_append (label_list, label_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("Returning account label_list"); + return label_list; +} + +int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt, label_record); + + cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data); + + _account_type_free_label_items(label_record); + _ACCOUNT_FREE(label_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} + +int _account_type_label_get_app_id(label_h label, char **app_id) +{ + if (!label) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + if (!app_id) { + return ACCOUNT_ERROR_INVALID_PARAMETER; + } + + label_s *data = (label_s*)label; + + (*app_id) = NULL; + + *app_id = _account_get_text(data->app_id); + + return ACCOUNT_ERROR_NONE; +} + +bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data) +{ + account_type_s *data = (account_type_s*)user_data; + + label_s *label_data = (label_s*)malloc(sizeof(label_s)); + + if (label_data == NULL) { + ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n"); + return FALSE; + } + ACCOUNT_MEMSET(label_data, 0, sizeof(label_s)); + + label_data->app_id = _account_get_text(app_id); + label_data->label = _account_get_text(label); + label_data->locale = _account_get_text(locale); + + data->label_list = g_slist_append(data->label_list, (gpointer)label_data); + + return TRUE; +} + +bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data) +{ + account_type_s *data = (account_type_s*)user_data; + + provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s)); + + if (feature_data == NULL) { + ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n"); + return FALSE; + } + ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s)); + + feature_data->app_id = _account_get_text(app_id); + feature_data->key = _account_get_text(key); + + data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data); + + return TRUE; +} + +int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record) +{ + _INFO("_account_type_query_by_app_id start"); + + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + *account_type_record = create_empty_account_type_instance(); + + while (rc == SQLITE_ROW) { + _account_type_convert_column_to_account_type(hstmt, *account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record)); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record)); + + hstmt = NULL; + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("_account_type_query_by_app_id end [%d]", error_code); + return error_code; +} + +int _account_type_query_app_id_exist(const char* app_id) +{ + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id); + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + return ACCOUNT_ERROR_NONE; +} + +GSList* _account_type_query_by_provider_feature(const char* key, int *error_code) +{ + *error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_type_list = NULL; + + if(key == NULL) + { + ACCOUNT_ERROR("capability_type IS NULL."); + *error_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto CATCH; + } + + if(g_hAccountDB == NULL) + { + ACCOUNT_ERROR("The database isn't connected."); + *error_code = ACCOUNT_ERROR_DB_NOT_OPENED; + goto CATCH; + } + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ) + { + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + *error_code = ACCOUNT_ERROR_PERMISSION_DENIED; + goto CATCH; + } + + int binding_count = 1; + _account_query_bind_text(hstmt, binding_count++, key); + + rc = _account_query_step(hstmt); + + account_type_s *account_type_record = NULL; + + if(rc != SQLITE_ROW) + { + ACCOUNT_ERROR("The record isn't found."); + *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + + if (account_type_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); + _account_type_convert_column_to_account_type(hstmt, account_type_record); + account_type_list = g_slist_append(account_type_list, account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE ) + { + _account_type_gslist_free(account_type_list); + ACCOUNT_ERROR("finalize error(%s)", rc); + *error_code = rc; + goto CATCH; + } + hstmt = NULL; + + GSList* iter; + + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) { + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; + account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); + } + + *error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + if (rc != ACCOUNT_ERROR_NONE) + { + *error_code = rc; + return NULL; + } + hstmt = NULL; + } + + return account_type_list; +} + + +GSList* _account_type_query_all(void) +{ + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + GSList *account_type_list = NULL; + + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE); + hstmt = _account_prepare_query(query); + + rc = _account_query_step(hstmt); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return NULL; + } + + account_type_s *account_type_record = NULL; + + if (rc != SQLITE_ROW) + { + _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found."); + goto CATCH; + } + + while(rc == SQLITE_ROW) { + account_type_record = (account_type_s*) malloc(sizeof(account_type_s)); + + if (account_type_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s)); + _account_type_convert_column_to_account_type(hstmt, account_type_record); + account_type_list = g_slist_append(account_type_list, account_type_record); + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error")); + hstmt = NULL; + + GSList* iter; + + for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) { + account_type_s *account_type = NULL; + account_type = (account_type_s*)iter->data; + account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type); + account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type); + } + +CATCH: + if (hstmt != NULL) + { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error")); + hstmt = NULL; + } + + return account_type_list; +} + +// output parameter label must be free +int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0, binding_count = 1; + char* converted_locale = NULL; + + ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null")); + ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null")); + //Making label newly created + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + converted_locale = _account_get_text(locale); + gchar** tokens = g_strsplit(converted_locale, "-", 2); + + if(tokens != NULL) { + if((char*)(tokens[1]) != NULL) { + char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1])); + if(upper_token != NULL) { + _ACCOUNT_FREE(converted_locale); + converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token); + } + _ACCOUNT_FREE(upper_token); + } + } + g_strfreev(tokens); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale); + _ACCOUNT_FREE(converted_locale); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + _account_query_bind_text(hstmt, binding_count++, app_id); + + rc = _account_query_step(hstmt); + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + label_s* label_record = NULL; + + while (rc == SQLITE_ROW) { + label_record = (label_s*) malloc(sizeof(label_s)); + + if (label_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s)); + + _account_type_convert_column_to_label(hstmt,label_record); + + _ACCOUNT_FREE(*label); + //Making label newly created + *label = _account_get_text(label_record->label); + + _account_type_free_label_items(label_record); + _ACCOUNT_FREE(label_record); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + + CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code); + return error_code; +} + +static int _account_insert_custom(account_s *account, int account_id) +{ + _INFO("_account_insert_custom start"); + + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->custom_list)==0) { + ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + /* insert query*/ + + GSList *iter; + + for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES " + "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_custom_s* custom_data = NULL; + custom_data = (account_custom_s*)iter->data; + + ret = _account_query_bind_int(hstmt, count++, account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); + ret = _account_query_bind_text(hstmt, count++, account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + _INFO("_account_insert_custom end"); + return ACCOUNT_ERROR_NONE; +} + +static int _account_update_custom(account_s *account, int account_id) +{ + int rc, count = 1; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + account_stmt hstmt = NULL; + + ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL")); + + if (g_slist_length( account->custom_list)==0) { + ACCOUNT_DEBUG( "_account_update_custom, no custom data\n"); + return ACCOUNT_ERROR_NONE; + } + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where id=%d", ACCOUNT_TABLE, account_id); + + rc = _account_get_record_count(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } else if( _account_db_err_code() == SQLITE_BUSY ){ + ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg()); + pthread_mutex_unlock(&account_mutex); + return ACCOUNT_ERROR_DATABASE_BUSY; + } + + if (rc <= 0) { + ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_RECORD_NOT_FOUND; + } + + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + + ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE); + hstmt = _account_prepare_query(query); + count = 1; + _account_query_bind_int(hstmt, count++, (int)account_id); + rc = _account_query_step(hstmt); + + if (rc == SQLITE_BUSY) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DATABASE_BUSY; + } else if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + return ACCOUNT_ERROR_DB_FAILED; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + GSList *iter; + + for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) { + int ret; + count = 1; + ACCOUNT_MEMSET(query, 0x00, sizeof(query)); + ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES " + "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE); + + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg())); + + account_custom_s* custom_data = NULL; + custom_data = (account_custom_s*)iter->data; + + ret = _account_query_bind_int(hstmt, count++, (int)account_id); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value); + ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail")); + + rc = _account_query_step(hstmt); + + if (rc != SQLITE_DONE) { + ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg()); + break; + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + } + + return ACCOUNT_ERROR_NONE; +} + +static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data ) +{ + int error_code = ACCOUNT_ERROR_NONE; + account_stmt hstmt = NULL; + char query[ACCOUNT_SQL_LEN_MAX] = {0, }; + int rc = 0; + + ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0")); + ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION")); + ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected.")); + + ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX); + + ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id); + hstmt = _account_prepare_query(query); + + if( _account_db_err_code() == SQLITE_PERM ){ + ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg()); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + rc = _account_query_step(hstmt); + + ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n")); + + account_custom_s* custom_record = NULL; + + while (rc == SQLITE_ROW) { + bool cb_ret = FALSE; + custom_record = (account_custom_s*) malloc(sizeof(account_custom_s)); + + if (custom_record == NULL) { + ACCOUNT_FATAL("malloc Failed"); + break; + } + + ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s)); + + _account_convert_column_to_custom(hstmt, custom_record); + + cb_ret = callback(custom_record->key, custom_record->value, user_data); + + _account_custom_item_free(custom_record); + _ACCOUNT_FREE(custom_record); + + ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n")); + + rc = _account_query_step(hstmt); + } + + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + + error_code = ACCOUNT_ERROR_NONE; + +CATCH: + if (hstmt != NULL) { + rc = _account_query_finalize(hstmt); + ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error")); + hstmt = NULL; + } + + pthread_mutex_unlock(&account_mutex); + return error_code; +} diff --git a/src/accounts/server/account-server-db.h b/src/accounts/server/account-server-db.h new file mode 100644 index 0000000..41b3e6c --- /dev/null +++ b/src/accounts/server/account-server-db.h @@ -0,0 +1,131 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __ACC_SERVER_DB_H__ + +#include +#include "account-private.h" + +typedef sqlite3_stmt* account_stmt; + +//ACCOUNT_TABLE +#define ACCOUNT_SCHEMA "create table account \n"\ + "(\n"\ +"id INTEGER PRIMARY KEY, "\ +"user_name TEXT, "\ +"email_address TEXT, "\ +"display_name TEXT, "\ +"icon_path TEXT, "\ +"source TEXT, "\ +"package_name TEXT, "\ +"access_token TEXT, "\ +"domain_name TEXT, "\ +"auth_method TEXT, "\ +"auth_type INTEGER, "\ +"secret INTEGER, "\ +"sync_support INTEGER, "\ +"txt_custom0 TEXT, "\ +"txt_custom1 TEXT, "\ +"txt_custom2 TEXT, "\ +"txt_custom3 TEXT, "\ +"txt_custom4 TEXT, "\ +"int_custom0 INTEGER, "\ +"int_custom1 INTEGER, "\ +"int_custom2 INTEGER, "\ +"int_custom3 INTEGER, "\ +"int_custom4 INTEGER "\ +");" + +//CAPABILITY_TABLE +#define CAPABILITY_SCHEMA "create table capability \n"\ + "(\n"\ +"_id INTEGER PRIMARY KEY AUTOINCREMENT, "\ +"key TEXT, "\ +"value INTEGER, "\ +"package_name TEXT, "\ +"user_name TEXT, "\ +"account_id INTEGER "\ +");" + +//ACCOUNT_CUSTOM_TABLE +#define ACCOUNT_CUSTOM_SCHEMA "create table account_custom \n"\ + "(\n"\ +"AccountId INTEGER, "\ +"AppId TEXT, "\ +"Key TEXT, "\ +"Value TEXT "\ +");" + +//ACCOUNT_TYPE_TABLE +#define ACCOUNT_TYPE_SCHEMA "create table account_type \n"\ + "(\n"\ +"_id INTEGER PRIMARY KEY AUTOINCREMENT, "\ +"AppId TEXT, "\ +"ServiceProviderId TEXT, "\ +"IconPath TEXT, "\ +"SmallIconPath TEXT, "\ +"MultipleAccountSupport INTEGER "\ +");" + +//LABEL_TABLE +#define LABEL_SCHEMA "create table label \n"\ + "(\n"\ +"AppId TEXT, "\ +"Label TEXT, "\ +"Locale TEXT"\ +");" + +//PROVIDER_FEATURE_TABLE +#define PROVIDER_FEATURE_SCHEMA "create table provider_feature \n"\ + "(\n"\ +"app_id TEXT, "\ +"key TEXT "\ +");" + + +int _account_insert_to_db(account_s* account, int pid, int *account_id); +int _account_db_open(int mode, const char* account_db_path); +int _account_db_close(void); +int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id); +GSList* _account_db_query_all(int pid); +GSList* _account_type_query_all(void); +int _account_delete(int pid, int account_id); +int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name); +int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission); +int _account_update_to_db_by_id(int pid, account_s *account, int account_id); +int _account_get_total_count_from_db(gboolean include_hidden, int *count); +int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record); +int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name); +int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label); +GSList* _account_type_query_by_provider_feature(const char* key, int *error_code); +GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code); +GList* _account_query_account_by_package_name(int pid, const char* package_name, int *error_code); +GList* _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code); +GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code); +GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code); +int _account_update_sync_status_by_id(int account_db_id, const int sync_status); +GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code); +bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code); +int _account_type_update_to_db_by_app_id(account_type_s *account_type, const char* app_id); +int _account_type_delete_by_app_id(const char* app_id); +GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code ); +int _account_type_query_by_app_id(const char* app_id, account_type_s **account_type_record); +int _account_type_query_app_id_exist(const char* app_id); +int _account_update_to_db_by_id_ex(account_s *account, int account_id); + +#endif diff --git a/src/accounts/server/account-server.c b/src/accounts/server/account-server.c new file mode 100644 index 0000000..f582772 --- /dev/null +++ b/src/accounts/server/account-server.c @@ -0,0 +1,2139 @@ +/* + * + * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#define _DEFAULT_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if !GLIB_CHECK_VERSION (2, 31, 0) +#include +#endif +#include + +#include +#include "dbg.h" +#include "account-server-db.h" +#include "account_ipc_marshal.h" +#include "account-mgr-stub.h" +#include "account-private.h" +#include "account-error.h" + +#define _CHECK_READ_LABEL "libaccounts-svc::check_read" +#define _DB_LABEL "libaccounts-svc::db" +#define _READ_LABEL "r" +#define _WRITE_LABEL "w" +#define _READ_WRITE_LABEL "rw" + +#define ACCOUNT_MGR_DBUS_PATH "/org/tizen/account/manager" +static guint owner_id = 0; +GDBusObjectManagerServer *account_mgr_server_mgr = NULL; +static AccountManager* account_mgr_server_obj = NULL; +//static gboolean has_owner = FALSE; + +// pid-mode, TODO: make it sessionId-mode, were session id is mix of pid and some rand no, so that +// one client can have multiple connections having different modes +//static GHashTable* mode_table = NULL; + +GDBusErrorEntry _account_svc_errors[] = +{ + {ACCOUNT_ERROR_NONE, _ACCOUNT_SVC_ERROR_PREFIX".NoError"}, + {ACCOUNT_ERROR_OUT_OF_MEMORY, _ACCOUNT_SVC_ERROR_PREFIX".OutOfMemory"}, + {ACCOUNT_ERROR_INVALID_PARAMETER, _ACCOUNT_SVC_ERROR_PREFIX".InvalidParameter"}, + {ACCOUNT_ERROR_DUPLICATED, _ACCOUNT_SVC_ERROR_PREFIX".Duplicated"}, + {ACCOUNT_ERROR_NO_DATA, _ACCOUNT_SVC_ERROR_PREFIX".NoData"}, + {ACCOUNT_ERROR_RECORD_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".RecordNotFound"}, + {ACCOUNT_ERROR_DB_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".DBFailed"}, + {ACCOUNT_ERROR_DB_NOT_OPENED, _ACCOUNT_SVC_ERROR_PREFIX".DBNotOpened"}, + {ACCOUNT_ERROR_QUERY_SYNTAX_ERROR, _ACCOUNT_SVC_ERROR_PREFIX".QuerySynTaxError"}, + {ACCOUNT_ERROR_ITERATOR_END, _ACCOUNT_SVC_ERROR_PREFIX".IteratorEnd"}, + {ACCOUNT_ERROR_NOTI_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".NotiFalied"}, + {ACCOUNT_ERROR_PERMISSION_DENIED, _ACCOUNT_SVC_ERROR_PREFIX".PermissionDenied"}, + {ACCOUNT_ERROR_XML_PARSE_FAILED, _ACCOUNT_SVC_ERROR_PREFIX".XMLParseFailed"}, + {ACCOUNT_ERROR_XML_FILE_NOT_FOUND, _ACCOUNT_SVC_ERROR_PREFIX".FileNotFound"}, + {ACCOUNT_ERROR_EVENT_SUBSCRIPTION_FAIL, _ACCOUNT_SVC_ERROR_PREFIX".SubscriptionFailed"}, + {ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, _ACCOUNT_SVC_ERROR_PREFIX".NotRegisteredProvider"}, + {ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, _ACCOUNT_SVC_ERROR_PREFIX".NotAllowMultiple"}, + {ACCOUNT_ERROR_DATABASE_BUSY, _ACCOUNT_SVC_ERROR_PREFIX".database_busy"}, +}; + +static guint +_get_client_pid(GDBusMethodInvocation* invoc) +{ + const char *name = NULL; + name = g_dbus_method_invocation_get_sender(invoc); + if (name == NULL) + { + _ERR("g_dbus_method_invocation_get_sender failed"); + return -1; + } + _INFO("sender=[%s]", name); + + + guint pid = -1; + GError *error = NULL; + GVariant *_ret; + + _INFO("calling GetConnectionUnixProcessID"); + + GDBusConnection* conn = g_dbus_method_invocation_get_connection(invoc); + _ret = g_dbus_connection_call_sync(conn, + "org.freedesktop.DBus", + "/org/freedesktop/DBus", + "org.freedesktop.DBus", + "GetConnectionUnixProcessID", + g_variant_new("(s)", name), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (_ret != NULL) + { + g_variant_get(_ret, "(u)", &pid); + g_variant_unref(_ret); + } + + _INFO("process Id = [%u]", pid); + return pid; +} + +static GQuark +_account_error_quark (void) +{ + static volatile gsize quark_volatile = 0; + + g_dbus_error_register_error_domain (_ACCOUNT_SVC_ERROR_DOMAIN, + &quark_volatile, + _account_svc_errors, + G_N_ELEMENTS (_account_svc_errors)); + + return (GQuark) quark_volatile; +} + +static int _check_privilege_by_cookie(char *e_cookie, const char *label, const char *access_perm, bool check_root, int pid) { + guchar *cookie = NULL; + gsize size = 0; + int retval = 0; + char buf[128] = {0,}; + FILE *fp = NULL; + char title[128] = {0,}; + int uid = -1; + + if (check_root) { + // Gets the userID from /proc/pid/status to check if the process is the root or not. + snprintf(buf, sizeof(buf), "/proc/%d/status", pid); + fp = fopen(buf, "r"); + if(fp) { + while (fgets(buf, sizeof(buf), fp) != NULL) { + if(strncmp(buf, "Uid:", 4) == 0) { + sscanf(buf, "%s %d", title, &uid); + break; + } + } + fclose(fp); + } + + _INFO("uid : %d", uid); + } + + if (uid != 0) { // Checks the cookie only when the process is not the root + cookie = g_base64_decode(e_cookie, &size); + if (cookie == NULL) { + _ERR("Unable to decode cookie!!!"); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + + retval = security_server_check_privilege_by_cookie((const char *)cookie, label, access_perm); + g_free(cookie); + + if (retval < 0) { + if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) { + _ERR("Access to account-svcd has been denied by smack."); + } + _ERR("Error has occurred in security_server_check_privilege_by_cookie() : %d.", retval); + return ACCOUNT_ERROR_PERMISSION_DENIED; + } + } + + _INFO("The process(%d) was authenticated successfully.", pid); + return ACCOUNT_ERROR_NONE; +} + +gboolean account_manager_account_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar* account_db_path, GVariant* account_data, gchar *cookie, gpointer user_data) +{ + _INFO("account_manager_account_add start"); + int db_id = -1; + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("account unmarshalling failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + + goto RETURN; + } + + return_code = _account_insert_to_db(account, pid, &db_id); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_insert_to_db() error"); + + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_add(obj, invocation, db_id); + } + _INFO("account_manager_account_add end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +{ + _INFO("account_manager_account_query_all start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_list = NULL; + account_list = _account_db_query_all(pid); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_slist_length(account_list)); + + return_code = 0; + _INFO("before calling marshal_account_list"); + account_list_variant = marshal_account_list(account_list); + _INFO("after calling marshal_account_list"); + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_all(obj, invocation, account_list_variant); + } + _INFO("account_manager_account_query_all end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_type_query_all(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, gchar *cookie) +{ + _INFO("account_manager_account_query_all start"); + + GVariant* account_type_list_variant = NULL; + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_type_list = NULL; + account_type_list = _account_type_query_all(); + + if (account_type_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account type found."); + goto RETURN; + } + + _INFO("account_type_list length= [%d]", g_slist_length(account_type_list)); + + return_code = 0; + _INFO("before calling marshal_account_type_list"); + account_type_list_variant = marshal_account_type_list(account_type_list); + _INFO("after calling marshal_account_type_list"); + +RETURN: + + if (account_type_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_all(obj, invocation, account_type_list_variant); + } + _INFO("account_manager_account_query_all end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_type_add(AccountManager *obj, GDBusMethodInvocation *invocation, gchar *account_db_path, GVariant *account_type_data, gchar *cookie, gpointer user_data) +{ + int db_id = -1; + + _INFO("account_manager_account_type_add start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_type_s* account_type = umarshal_account_type(account_type_data); + if (account_type == NULL) + { + _ERR("account_type unmarshalling failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before _account_type_insert_to_db"); + return_code = _account_type_insert_to_db(account_type, &db_id); + _INFO("after _account_type_insert_to_db"); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_insert_to_db error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_add(obj, invocation, db_id); + } + _INFO("account_manager_account_type_add end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + gint account_db_id, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_id start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_delete"); + return_code = _account_delete(pid, account_db_id); + _INFO("after _account_delete=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_id(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_user_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *user_name, + const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_user_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_delete_from_db_by_user_name"); + return_code = _account_delete_from_db_by_user_name(pid, user_name, package_name); + _INFO("after _account_delete_from_db_by_user_name=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete_from_db_by_user_name error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_id(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_delete_from_db_by_package_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *package_name, gboolean permission, gchar *cookie) +{ + _INFO("account_manager_account_delete_from_db_by_package_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before account_delete_from_db_by_package_name"); + return_code = _account_delete_from_db_by_package_name(pid, package_name, permission); + _INFO("after account_delete_from_db_by_package_name=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_delete_from_db_by_package_name error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_delete_from_db_by_package_name(object, invocation); + } + _INFO("account_manager_account_delete_from_db_by_package_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_update_to_db_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + gint account_id, gchar *cookie) +{ + _INFO("account_manager_account_update_to_db_by_id start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before account_update_to_db_by_id"); + return_code = _account_update_to_db_by_id(pid, account, account_id); + _INFO("after account_update_to_db_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_update_to_db_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_to_db_by_id(object, invocation); + } + _INFO("account_manager_account_update_to_db_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_to_db_by_user_name(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + const gchar *user_name, + const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_to_db_by_user_name start"); + + guint pid = _get_client_pid(invocation); + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + + _INFO("before account_update_to_db_by_id"); + return_code = _account_update_to_db_by_user_name(pid, account, user_name, package_name); + _INFO("after account_update_to_db_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_to_db_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_to_db_by_id(object, invocation); + } + _INFO("account_manager_handle_account_update_to_db_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_type_query_label_by_locale(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, + const gchar *locale, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_label_by_locale start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_label_by_locale"); + char *label_name = NULL; + return_code = _account_type_query_label_by_locale(app_id, locale, &label_name); + _INFO("after _account_type_query_label_by_locale=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_label_by_locale error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_label_by_locale(object, invocation, label_name); + } + _INFO("account_manager_handle_account_type_query_label_by_locale end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_type_query_by_provider_feature(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *key, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_by_provider_feature start"); + GVariant* account_type_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* account_type_list = NULL; + account_type_list = _account_type_query_by_provider_feature(key, &return_code); + if (return_code != 0) + { + _ERR("_account_type_query_by_provider_feature=[%d]", return_code); + goto RETURN; + } + + if (account_type_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account type found."); + goto RETURN; + } + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_by_provider_feature error"); + goto RETURN; + } + + _INFO("account_type_list length= [%d]", g_slist_length(account_type_list)); + + _INFO("before calling marshal_account_type_list"); + account_type_list_variant = marshal_account_type_list(account_type_list); + _INFO("after calling marshal_account_type_list"); + +RETURN: + + if (account_type_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_type_query_by_provider_feature(obj, invocation, account_type_list_variant); + } + _INFO("account_manager_handle_account_type_query_by_provider_feature end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_account_get_total_count_from_db(AccountManager *object, GDBusMethodInvocation *invocation, gchar *account_db_path, gboolean include_hidden, gchar *cookie) +{ + _INFO("account_manager_account_get_total_count_from_db start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + + _INFO("before account_get_total_count_from_db"); + int count = -1; + return_code = _account_get_total_count_from_db(include_hidden, &count); + _INFO("before account_get_total_count_from_db=[%d], return_code"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_get_total_count_from_db error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_get_total_count_from_db(object, invocation, count); + } + _INFO("account_manager_account_get_total_count_from_db end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_query_account_by_account_id(AccountManager *object, GDBusMethodInvocation *invocation, + gchar *account_db_path, gint account_db_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_account_id start"); + GVariant* account_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account_data = create_empty_account_instance(); + if (account_data == NULL) + { + _ERR("out of memory"); + return_code = ACCOUNT_ERROR_DB_FAILED; + goto RETURN; + } + _INFO("before _account_query_account_by_account_id"); + return_code = _account_query_account_by_account_id(pid, account_db_id, account_data); + _INFO("after _account_query_account_by_return_code=[%d]", return_code); + _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]); + + if (return_code == ACCOUNT_ERROR_NONE) + { + account_variant = marshal_account(account_data); + } + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_label_by_locale error"); + goto RETURN; + } + +RETURN: + if (account_variant == NULL || return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_account_id(object, invocation, account_variant); + } + _INFO("account_manager_handle_account_query_account_by_account_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_user_name(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *user_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_user_name start"); + + GVariant* account_list_variant = NULL; + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_user_name(pid, user_name, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + _INFO("before calling marshal_account_list_double"); + account_list_variant = marshal_account_list_double(account_list); + _INFO("after calling marshal_account_list_double"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_user_name error"); + goto RETURN; + } + +RETURN: + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_user_name(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_user_name end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_package_name(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *package_name, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_package_name start"); + + GVariant* account_list_variant = NULL; + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_package_name(pid, package_name, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + account_list_variant = marshal_account_list_double(account_list); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_package_name error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + _INFO("sending error Domain[%d] Message[%s] Code[%d]", error->domain, error->message, error->code); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_package_name(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_package_name start"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_capability(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *capability_type, + gint capability_value, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_capability start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + _INFO("before _account_query_account_by_capability"); + account_list = _account_query_account_by_capability(pid, capability_type, capability_value, &return_code); + _INFO("after _account_query_account_by_capability"); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + account_list_variant = marshal_account_list_double(account_list); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_capability error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_capability end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_account_by_capability_type(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *capability_type, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_account_by_capability_type start"); + + GVariant* account_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GList* account_list = NULL; + + account_list = _account_query_account_by_capability_type(pid, capability_type, &return_code); + + if (account_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No account found."); + goto RETURN; + } + + _INFO("account_list length= [%d]", g_list_length(account_list)); + + _INFO("before calling marshal_account_list_double"); + account_list_variant = marshal_account_list_double(account_list); + _INFO("after calling marshal_account_list_double"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_query_account_by_capability_type error"); + goto RETURN; + } + +RETURN: + + if (account_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_account_by_capability(obj, invocation, account_list_variant); + } + _INFO("account_manager_handle_account_query_account_by_capability_type end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean +account_manager_handle_account_query_capability_by_account_id(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const int account_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_query_capability_by_account_id start"); + + GVariant* capability_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + //Mode checking not required, since default mode is read. + + GSList* capability_list = NULL; + + capability_list = _account_get_capability_list_by_account_id(account_id, &return_code); + + if (capability_list == NULL) + { + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + _ERR("No capability found."); + goto RETURN; + } + + _INFO("capability_list length= [%d]", g_slist_length(capability_list)); + + _INFO("before calling marshal_capability_list"); + capability_list_variant = marshal_capability_list(capability_list); + _INFO("after calling marshal_capability_list"); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_get_capability_list_by_account_id error"); + goto RETURN; + } + +RETURN: + + if (capability_list_variant == NULL) + { + GError* error = g_error_new (_account_error_quark(), ACCOUNT_ERROR_RECORD_NOT_FOUND, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_query_capability_by_account_id(obj, invocation, capability_list_variant); + } + _INFO("account_manager_handle_account_query_capability_by_account_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_sync_status_by_id(AccountManager *object, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const int account_db_id, + const int sync_status, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_sync_status_by_id start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_update_sync_status_by_id"); + return_code = _account_update_sync_status_by_id(account_db_id, sync_status); + _INFO("after _account_update_sync_status_by_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_sync_status_by_id error"); + goto RETURN; + } + +RETURN: + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + account_manager_complete_account_update_sync_status_by_id(object, invocation); + } + _INFO("account_manager_handle_account_update_sync_status_by_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_provider_feature_by_app_id(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar* app_id, gchar *cookie) +{ + GSList* feature_record_list = NULL; + GVariant* feature_record_list_variant = NULL; + + _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_provider_feature_by_app_id"); + feature_record_list = _account_type_query_provider_feature_by_app_id(app_id, &return_code); + _INFO("after account_type_query_provider_feature_by_app_id=[%d]", return_code); + + if (feature_record_list == NULL) + { + _ERR("account feature_record_list is NULL"); + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto RETURN; + } + + feature_record_list_variant = provider_feature_list_to_variant(feature_record_list); + _INFO("%s", g_variant_print(feature_record_list_variant, true)); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_provider_feature_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id"); + account_manager_complete_account_type_query_provider_feature_by_app_id(obj, invocation, feature_record_list_variant); + } + _INFO("account_manager_handle_account_type_query_provider_feature_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_supported_feature(AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar* app_id, + const gchar* capability, gchar *cookie) +{ + int is_supported = 0; + + _INFO("account_manager_handle_account_type_query_supported_feature start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_supported_feature"); + is_supported = _account_type_query_supported_feature(app_id, capability, &return_code); + _INFO("after _account_type_query_supported_feature=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_supported_feature error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_provider_feature_by_app_id"); + account_manager_complete_account_type_query_supported_feature(obj, invocation, is_supported); + } + _INFO("account_manager_handle_account_type_query_supported_feature end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_update_to_db_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_type_variant, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_update_to_db_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_type_s* account_type = umarshal_account_type(account_type_variant); + + _INFO("before _account_type_update_to_db_by_app_id"); + return_code = _account_type_update_to_db_by_app_id(account_type, app_id); + _INFO("after _account_type_update_to_db_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_update_to_db_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_update_to_db_by_app_id"); + account_manager_complete_account_type_update_to_db_by_app_id(obj, invocation); + } + _INFO("account_manager_handle_account_type_update_to_db_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_delete_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_delete_by_app_id start"); + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_delete_by_app_id"); + return_code = _account_type_delete_by_app_id (app_id); + _INFO("after _account_type_delete_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_delete_by_app_id error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _ERR("Calling account_manager_complete_account_type_update_to_db_by_app_id"); + account_manager_complete_account_type_delete_by_app_id (obj, invocation); + } + _INFO("account_manager_handle_account_type_delete_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_label_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_label_by_app_id start"); + GSList* label_list = NULL; + GVariant* label_list_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_get_label_list_by_app_id"); + label_list = _account_type_get_label_list_by_app_id (app_id, &return_code); + _INFO("after _account_type_get_label_list_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_get_label_list_by_app_id = [%d]", return_code); + goto RETURN; + } + + label_list_variant = label_list_to_variant (label_list); + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _ERR("Calling account_manager_complete_account_type_query_label_by_app_id"); + account_manager_complete_account_type_query_label_by_app_id (obj, invocation, label_list_variant); + } + _INFO("account_manager_handle_account_type_query_label_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_by_app_id (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_by_app_id start"); + GVariant* account_type_variant = NULL; + + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_by_app_id"); + account_type_s* account_type = NULL; + return_code = _account_type_query_by_app_id (app_id, &account_type); + _INFO("after _account_type_query_by_app_id=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_by_app_id = [%d]", return_code); + goto RETURN; + } + if (account_type == NULL) + { + _ERR("account_type read is NULL"); + return_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; + goto RETURN; + } + + account_type_variant = marshal_account_type( account_type); + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_by_app_id"); + account_manager_complete_account_type_query_by_app_id (obj, invocation, account_type_variant); + } + _INFO("account_manager_handle_account_type_query_by_app_id end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_type_query_app_id_exist (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, const gchar *app_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_type_query_app_id_exist start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _CHECK_READ_LABEL, _READ_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(0, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + _INFO("before _account_type_query_app_id_exist"); + return_code = _account_type_query_app_id_exist (app_id); + _INFO("after _account_type_query_app_id_exist=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_type_query_app_id_exist = [%d]", return_code); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_type_query_by_app_id"); + account_manager_complete_account_type_query_app_id_exist (obj, invocation); + } + _INFO("account_manager_handle_account_type_query_app_id_exist end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +gboolean account_manager_handle_account_update_to_db_by_id_ex (AccountManager *obj, + GDBusMethodInvocation *invocation, + gchar *account_db_path, + GVariant *account_data, + gint account_id, gchar *cookie) +{ + _INFO("account_manager_handle_account_update_to_db_by_id_ex start"); + guint pid = _get_client_pid(invocation); + + _INFO("client Id = [%u]", pid); + + int return_code = _check_privilege_by_cookie(cookie, _DB_LABEL, _READ_WRITE_LABEL, true, pid); + if (return_code != ACCOUNT_ERROR_NONE) + { + goto RETURN; + } + + return_code = _account_db_open(1, (const char*)account_db_path); + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_db_open() error, db_path = %s, ret = %d", (char*)account_db_path, return_code); + + goto RETURN; + } + + account_s* account = umarshal_account(account_data); + if (account == NULL) + { + _ERR("Unmarshal failed"); + return_code = ACCOUNT_ERROR_INVALID_PARAMETER; + goto RETURN; + } + + _INFO("before _account_update_to_db_by_id_ex"); + return_code = _account_update_to_db_by_id_ex (account, account_id); + _INFO("after _account_update_to_db_by_id_ex()=[%d]", return_code); + + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("_account_update_to_db_by_id_ex error"); + goto RETURN; + } + +RETURN: + if (return_code != ACCOUNT_ERROR_NONE) + { + _ERR("Account SVC is returning error [%d]", return_code); + GError* error = g_error_new (_account_error_quark(), return_code, "RecordNotFound"); + g_dbus_method_invocation_return_gerror (invocation, error); + } + else + { + _INFO("Calling account_manager_complete_account_update_to_db_by_id_ex"); + account_manager_complete_account_update_to_db_by_id_ex (obj, invocation); + } + _INFO("in account_manager_handle_account_update_to_db_by_id_ex_p end"); + + return_code = _account_db_close(); + if (return_code != ACCOUNT_ERROR_NONE) + { + ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code); + return_code = ACCOUNT_ERROR_NONE; + } + + return true; +} + +static void +on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data) +{ + _INFO("on_bus_acquired [%s]", name); + + GDBusInterfaceSkeleton* interface = NULL; + account_mgr_server_obj = account_manager_skeleton_new(); + if (account_mgr_server_obj == NULL) + { + _ERR("account_mgr_server_obj NULL!!"); + return; + } + + interface = G_DBUS_INTERFACE_SKELETON(account_mgr_server_obj); + if (!g_dbus_interface_skeleton_export(interface, connection, ACCOUNT_MGR_DBUS_PATH, NULL)) + { + _ERR("export failed!!"); + return; + } + + _INFO("connecting account signals start"); + + g_signal_connect(account_mgr_server_obj, "handle_account_add", + G_CALLBACK(account_manager_account_add), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_all", + G_CALLBACK(account_manager_account_query_all), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_add", + G_CALLBACK(account_manager_account_type_add), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_all", + G_CALLBACK(account_manager_account_type_query_all), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_id", + G_CALLBACK(account_manager_account_delete_from_db_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_user_name", + G_CALLBACK(account_manager_account_delete_from_db_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_delete_from_db_by_package_name", + G_CALLBACK(account_manager_account_delete_from_db_by_package_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id", + G_CALLBACK(account_manager_account_update_to_db_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_get_total_count_from_db", + G_CALLBACK(account_manager_account_get_total_count_from_db), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_account_id", + G_CALLBACK(account_manager_handle_account_query_account_by_account_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_user_name", + G_CALLBACK(account_manager_handle_account_update_to_db_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_locale", + G_CALLBACK(account_manager_handle_account_type_query_label_by_locale), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_provider_feature", + G_CALLBACK(account_manager_handle_account_type_query_by_provider_feature), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_user_name", + G_CALLBACK(account_manager_handle_account_query_account_by_user_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_package_name", + G_CALLBACK(account_manager_handle_account_query_account_by_package_name), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability", + G_CALLBACK(account_manager_handle_account_query_account_by_capability), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_account_by_capability_type", + G_CALLBACK(account_manager_handle_account_query_account_by_capability_type), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_query_capability_by_account_id", + G_CALLBACK(account_manager_handle_account_query_capability_by_account_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_sync_status_by_id", + G_CALLBACK(account_manager_handle_account_update_sync_status_by_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_provider_feature_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_provider_feature_by_app_id), NULL); + + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_supported_feature", + G_CALLBACK(account_manager_handle_account_type_query_supported_feature), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_update_to_db_by_app_id", + G_CALLBACK(account_manager_handle_account_type_update_to_db_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_delete_by_app_id", + G_CALLBACK(account_manager_handle_account_type_delete_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_label_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_label_by_app_id), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_by_app_id", + G_CALLBACK(account_manager_handle_account_type_query_by_app_id), NULL); + + + g_signal_connect(account_mgr_server_obj, "handle_account_type_query_app_id_exist", + G_CALLBACK(account_manager_handle_account_type_query_app_id_exist), NULL); + + g_signal_connect(account_mgr_server_obj, "handle_account_update_to_db_by_id_ex", + G_CALLBACK(account_manager_handle_account_update_to_db_by_id_ex), NULL); + + _INFO("connecting account signals end"); + + g_dbus_object_manager_server_set_connection(account_mgr_server_mgr, connection); +} + +static void +on_name_acquired (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + _INFO("on_name_acquired"); + +} + +static void +on_name_lost (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + _INFO("on_name_lost"); + exit (1); +} + +static bool _initialize_dbus() +{ + _INFO("__initialize_dbus Enter"); + + owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM, + "org.tizen.account.manager", + G_BUS_NAME_OWNER_FLAGS_NONE, + on_bus_acquired, + on_name_acquired, + on_name_lost, + NULL, + NULL); + + _INFO("owner_id=[%d]", owner_id); + + if(owner_id == 0) + { + _INFO("gdbus own failed!!"); + return false; + } + + _INFO("g_bus_own_name SUCCESS"); + return true; +} + +static void _initialize() +{ +#if !GLIB_CHECK_VERSION(2,35,0) + g_type_init(); +#endif + + if (_initialize_dbus() == false) + { /* because dbus's initialize + failed, we cannot continue any more. */ + _ERR("DBUS Initialization Failed"); + exit(1); + } +} + +int main() +{ + GMainLoop *mainloop = NULL; + + _INFO("Starting Accounts SVC"); + + mainloop = g_main_loop_new(NULL, FALSE); + + _initialize(); + + g_main_loop_run(mainloop); + + _INFO("Ending Accounts SVC"); + return 0; +} diff --git a/src/accounts/server/account-svcd.sh b/src/accounts/server/account-svcd.sh new file mode 100755 index 0000000..a1b3be2 --- /dev/null +++ b/src/accounts/server/account-svcd.sh @@ -0,0 +1 @@ +/usr/bin/account-svcd & -- 2.7.4