4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Tarun Kumar <tarun.kr@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
30 #include <account-private.h>
32 #include <dbus/dbus.h>
38 #define LOG_TAG "ACCOUNT"
40 #define ACCOUNT_DLOG_USE
41 #ifdef ACCOUNT_DLOG_USE
42 #define ACCOUNT_DEBUG(fmt, arg...) \
43 LOGD(": " fmt "\n", ##arg);
45 #define ACCOUNT_DEBUG(fmt, arg...) \
46 printf(": " fmt "\n", ##arg);
49 #define ACCOUNT_RETURN_VAL(eval, expr, ret_val, X)\
58 #define ACCOUNT_SNPRINTF(dest,size,format,arg...) \
60 snprintf(dest,size-1,format,##arg); \
62 /* If the same pointer is passed to free twice, known as a double free. To avoid this, set pointers to
63 NULL after passing them to free: free(NULL) is safe (it does nothing).
66 #define ACCOUNT_MEMSET(dest,value,size) \
68 memset(dest,value,size); \
71 #define ACCOUNT_CATCH_ERROR(eval, expr, error_val, X) \
75 error_code = (error_val);\
82 static sqlite3* g_hAccountDB = NULL;
83 static int g_refCntDB = 0;
84 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
86 static int _account_gslist_free(GSList* list);
87 static int _account_glist_free(GList* list);
89 static const char *_account_db_err_msg()
91 assert(NULL != g_hAccountDB);
92 return sqlite3_errmsg(g_hAccountDB);
95 static void _account_insert_delete_update_dbus_notification_send(char *noti_name)
100 dbus_uint32_t serial = 0;
103 ACCOUNT_DEBUG("Noti Name is NULL!!!!!!\n");
107 ACCOUNT_DEBUG("Sending signal with value %s\n", noti_name);
109 /* initialise the error value*/
110 dbus_error_init(&err);
112 /* connect to the DBUS system bus, and check for errors*/
113 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
114 if (dbus_error_is_set(&err)) {
115 ACCOUNT_DEBUG("Connection Error (%s)\n", err.message);
116 dbus_error_free(&err);
119 ACCOUNT_DEBUG("Dbus connection is NULL \n");
122 /* create a signal & check for errors */
123 msg = dbus_message_new_signal("/account/signal/Object", /*object name of the signal*/
124 ACCOUNT_DBUS_SIGNAL_INTERFACE, /*interface name of the signal*/
125 noti_name); /* name of the signal*/
127 ACCOUNT_DEBUG("Message Null\n");
131 /* send the message and flush the connection*/
132 if (!dbus_connection_send(conn, msg, &serial)) {
133 ACCOUNT_DEBUG("Out Of Memory!\n");
136 dbus_connection_flush(conn);
137 ACCOUNT_DEBUG("Signal Sent\n");
139 /* free the message*/
141 dbus_message_unref(msg);
144 static int _account_get_record_count(char* query)
148 account_stmt pStmt = NULL;
150 assert(NULL != query);
151 assert(NULL != g_hAccountDB);
152 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
154 rc = sqlite3_step(pStmt);
155 if (SQLITE_ROW != rc) {
156 ACCOUNT_DEBUG("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
157 sqlite3_finalize(pStmt);
158 return ACCOUNT_ERROR_DB_FAILED;
161 ncount = sqlite3_column_int(pStmt, 0);
163 ACCOUNT_DEBUG("count : %d, End", ncount);
164 sqlite3_finalize(pStmt);
169 static int _account_execute_query(char *query)
172 char* pszErrorMsg = NULL;
174 assert(NULL != query);
175 assert(NULL != g_hAccountDB);
177 ACCOUNT_DEBUG("query : %s", query);
179 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
180 if (SQLITE_OK != rc) {
181 ACCOUNT_DEBUG("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
182 sqlite3_free(pszErrorMsg);
188 static int _account_begin_transaction(void)
192 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
194 if (ret != SQLITE_OK) {
195 ACCOUNT_DEBUG("_account_svc_begin_transaction fail :: %d", ret);
196 return ACCOUNT_ERROR_DB_FAILED;
199 return ACCOUNT_ERROR_NONE;
202 static int _account_end_transaction(bool is_success)
206 if (is_success == true) {
207 ret = _account_execute_query("COMMIT TRANSACTION");
209 ret = _account_execute_query("ROLLBACK TRANSACTION");
212 if (ret != SQLITE_OK) {
213 ACCOUNT_DEBUG("_account_svc_end_transaction fail :: %d", ret);
214 return ACCOUNT_ERROR_DB_FAILED;
217 return ACCOUNT_ERROR_NONE;
220 static int _account_create_all_tables(void)
223 int error_code = ACCOUNT_ERROR_NONE;
224 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
227 ACCOUNT_MEMSET(query, 0, sizeof(query));
229 ACCOUNT_DEBUG("_account_create_all_tables begin");
231 /*Create the account table*/
232 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
233 rc = _account_get_record_count(query);
234 ACCOUNT_DEBUG("rc = %d \n", rc);
237 ACCOUNT_MEMSET(query, 0, sizeof(query));
238 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
239 rc = _account_execute_query(query);
240 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
243 ACCOUNT_MEMSET(query, 0, sizeof(query));
244 /*Create capability table*/
245 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
246 rc = _account_get_record_count(query);
247 ACCOUNT_DEBUG("rc = %d \n", rc);
250 ACCOUNT_MEMSET(query, 0, sizeof(query));
251 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
252 rc = _account_execute_query(query);
254 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
261 static bool _account_check_is_all_table_exists()
264 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
265 ACCOUNT_MEMSET(query, 0, sizeof(query));
267 ACCOUNT_DEBUG("_account_check_is_all_table_exists");
269 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s')",
270 ACCOUNT_TABLE, CAPABILITY_TABLE);
271 rc = _account_get_record_count(query);
272 ACCOUNT_DEBUG("rc = %d \n", rc);
274 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
275 ACCOUNT_DEBUG("Table count is not matched rc=%d\n", rc);
279 ACCOUNT_DEBUG("END of _account_check_is_all_table_exists\n");
284 static int _account_db_open(void)
287 bool is_success = false;
288 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
290 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
293 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
294 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
298 ACCOUNT_DEBUG("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
302 ACCOUNT_DEBUG("The database already connected. refcnt=%d ", g_refCntDB);
305 return ACCOUNT_ERROR_NONE;
308 static int _account_db_close(void)
313 if (g_refCntDB > 0) {
315 ACCOUNT_DEBUG("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
318 if (g_refCntDB == 0) {
319 rc = db_util_close(g_hAccountDB);
320 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
323 ACCOUNT_DEBUG( "_account_svc_db_close: The database disconnected really. ");
326 ACCOUNT_DEBUG( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
329 return ACCOUNT_ERROR_NONE;
332 static int _account_connect(void)
334 int error_code = ACCOUNT_ERROR_NONE;
336 pthread_mutex_lock(&account_mutex);
338 ACCOUNT_DEBUG("db path = %s\n", ACCOUNT_DB_NAME);
340 error_code = _account_db_open();
341 if (ACCOUNT_ERROR_NONE != error_code) {
342 ACCOUNT_DEBUG("The database isn't connected.\n");
343 pthread_mutex_unlock(&account_mutex);
344 return ACCOUNT_ERROR_DB_NOT_OPENED;
347 if (FALSE == _account_check_is_all_table_exists())
348 error_code = _account_create_all_tables();
350 pthread_mutex_unlock(&account_mutex);
351 return ACCOUNT_ERROR_NONE;
354 int account_connect (void)
356 return _account_connect();
359 static int _account_disconnect(void)
361 int error_code = ACCOUNT_ERROR_NONE;
363 pthread_mutex_lock(&account_mutex);
364 ACCOUNT_DEBUG("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
366 error_code = _account_db_close();
367 pthread_mutex_unlock(&account_mutex);
372 int account_disconnect (void)
374 return _account_disconnect();
377 static int _account_free_capability_items(account_capability_s *data)
379 _ACCOUNT_FREE(data->package_name);
380 _ACCOUNT_FREE(data->user_name);
382 return ACCOUNT_ERROR_NONE;
385 static int _account_free_account_items(account_s *data)
387 _ACCOUNT_FREE(data->user_name);
388 _ACCOUNT_FREE(data->email_address);
389 _ACCOUNT_FREE(data->display_name);
390 _ACCOUNT_FREE(data->icon_path);
391 _ACCOUNT_FREE(data->source);
392 _ACCOUNT_FREE(data->package_name);
393 _ACCOUNT_FREE(data->domain_name);
396 for(i=0;i<USER_TXT_CNT;i++)
397 _ACCOUNT_FREE(data->user_data_txt[i]);
399 _account_gslist_free(data->capablity_list);
400 _account_glist_free(data->account_list);
402 return ACCOUNT_ERROR_NONE;
405 static int _account_gslist_free(GSList* list)
407 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
411 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
412 account_capability_s *cap_data = (account_capability_s*)iter->data;
413 _account_free_capability_items(cap_data);
419 return ACCOUNT_ERROR_NONE;
422 static int _account_glist_free(GList* list)
424 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
428 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
429 account_s *account_record = (account_s*)iter->data;
430 _account_free_account_items(account_record);
436 return ACCOUNT_ERROR_NONE;
439 static gboolean _account_check_duplicated(account_s *data)
441 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
444 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
446 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and domain_name='%s'"
447 , ACCOUNT_TABLE, data->user_name, data->domain_name);
449 count = _account_get_record_count(query);
451 ACCOUNT_DEBUG("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
452 count, data->user_name, data->domain_name );
459 static int _account_get_next_sequence(char *pszName)
462 account_stmt pStmt = NULL;
464 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
466 ACCOUNT_DEBUG( "[Enter] pszName:%s\n", pszName);
468 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
469 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
470 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
472 rc = sqlite3_step(pStmt);
473 max_seq = sqlite3_column_int(pStmt, 0);
476 ACCOUNT_DEBUG( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
478 /*Finalize Statement*/
479 rc = sqlite3_finalize(pStmt);
485 static account_stmt _account_prepare_query(char *query)
488 account_stmt pStmt = NULL;
490 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
492 ACCOUNT_DEBUG( "prepare query : %s", query);
494 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
495 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
500 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
502 assert(NULL != pStmt);
504 return sqlite3_bind_int(pStmt, pos, num);
507 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
509 assert(NULL != pStmt);
512 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
514 return sqlite3_bind_null(pStmt, pos);
517 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
519 ACCOUNT_DEBUG( "_account_convert_account_to_sql");
522 /*Caution : Keep insert query orders.*/
525 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
527 /* 2. email address*/
528 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
531 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
534 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
537 _account_query_bind_text(hstmt, count++, (char*)account->source);
540 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
543 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
546 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
549 _account_query_bind_int(hstmt, count++, account->auth_type);
552 _account_query_bind_int(hstmt, count++, account->secret);
554 /* 11. sync_support */
555 _account_query_bind_int(hstmt, count++, account->sync_support);
560 for(i=0; i< USER_TXT_CNT; i++)
561 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
563 /* 13. user integer */
564 for(i=0; i< USER_INT_CNT; i++)
565 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
571 static void _account_query_finalize(account_stmt pStmt)
576 ACCOUNT_DEBUG( "pStmt is NULL");
580 rc = sqlite3_finalize(pStmt);
581 if (rc != SQLITE_OK) {
582 ACCOUNT_DEBUG( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
585 ACCOUNT_DEBUG( "sqlite3_finalize finish");
589 static int _account_query_step(account_stmt pStmt)
591 assert(NULL != pStmt);
592 return sqlite3_step(pStmt);
596 static int _account_execute_insert_query(account_s *account)
599 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
600 int error_code = ACCOUNT_ERROR_NONE;
601 account_stmt hstmt = NULL;
603 if (!account->user_name && !account->display_name && !account->email_address) {
604 ACCOUNT_DEBUG("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
605 return ACCOUNT_ERROR_INVALID_PARAMETER;
608 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
609 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
610 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
611 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
612 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
614 hstmt = _account_prepare_query(query);
615 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
617 _account_convert_account_to_sql(account, hstmt, query);
619 rc = _account_query_step(hstmt);
620 if (rc != SQLITE_DONE) {
621 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
622 error_code = ACCOUNT_ERROR_DB_FAILED;
625 _account_query_finalize(hstmt);
631 static int _account_insert_capability(account_s *account, int account_id)
634 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
635 account_stmt hstmt = NULL;
637 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
639 if (g_slist_length( account->capablity_list)==0) {
640 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
641 return ACCOUNT_ERROR_NONE;
644 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
646 rc = _account_get_record_count(query);
649 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
650 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
657 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
660 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
661 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
662 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
664 hstmt = _account_prepare_query(query);
666 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
668 account_capability_s* cap_data = NULL;
669 cap_data = (account_capability_s*)iter->data;
670 ACCOUNT_DEBUG( "@@@@@@@@@@@@@cap_data->type = %d, cap_data->value = %d \n @@@@@@@@@@", cap_data->type, cap_data->value);
672 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
673 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
674 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
675 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
676 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
677 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
678 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
679 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
680 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
681 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
683 rc = _account_query_step(hstmt);
685 if (rc != SQLITE_DONE) {
686 ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
690 _account_query_finalize(hstmt);
695 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
697 return ACCOUNT_ERROR_NONE;
701 static int _account_update_capability(account_s *account, int account_id)
704 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
705 account_stmt hstmt = NULL;
707 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
709 if (g_slist_length( account->capablity_list)==0) {
710 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
711 return ACCOUNT_ERROR_NONE;
714 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
716 rc = _account_get_record_count(query);
719 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
720 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
723 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
725 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
726 hstmt = _account_prepare_query(query);
728 _account_query_bind_int(hstmt, count++, (int)account_id);
729 _account_query_step(hstmt);
730 _account_query_finalize(hstmt);
735 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
738 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
739 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
740 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
742 hstmt = _account_prepare_query(query);
744 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
746 account_capability_s* cap_data = NULL;
747 cap_data = (account_capability_s*)iter->data;
749 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
750 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
751 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
752 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
753 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
754 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
755 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
756 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
757 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
758 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
760 rc = _account_query_step(hstmt);
762 if (rc != SQLITE_DONE) {
763 ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
767 _account_query_finalize(hstmt);
772 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
774 return ACCOUNT_ERROR_NONE;
777 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
780 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
781 account_stmt hstmt = NULL;
783 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
785 if (g_slist_length( account->capablity_list)==0) {
786 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
787 return ACCOUNT_ERROR_NONE;
790 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
792 rc = _account_get_record_count(query);
795 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
796 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
799 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
801 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
802 hstmt = _account_prepare_query(query);
804 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
805 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
806 _account_query_step(hstmt);
807 _account_query_finalize(hstmt);
812 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
815 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
816 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
817 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
819 hstmt = _account_prepare_query(query);
821 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
823 account_capability_s* cap_data = NULL;
824 cap_data = (account_capability_s*)iter->data;
826 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
827 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
828 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
829 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
830 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
831 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
832 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
833 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
834 /*ret = _account_query_bind_int(hstmt, count++, (int)account_id);
835 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));*/
837 rc = _account_query_step(hstmt);
839 if (rc != SQLITE_DONE) {
840 ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
844 _account_query_finalize(hstmt);
849 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
851 return ACCOUNT_ERROR_NONE;
854 static int _account_query_table_column_int(account_stmt pStmt, int pos)
856 assert(NULL != pStmt);
858 return sqlite3_column_int(pStmt, pos);
861 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
863 assert(NULL != pStmt);
865 return (char *)sqlite3_column_text(pStmt, pos);
868 static void _account_db_data_to_text(char *textbuf, char **output)
870 if (textbuf && strlen(textbuf)>0) {
875 *output = strdup(textbuf);
877 ACCOUNT_DEBUG("_account_db_data_to_text : no text");
881 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
883 char *textbuf = NULL;
885 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
887 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
888 _account_db_data_to_text(textbuf, &(account_record->user_name));
890 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
891 _account_db_data_to_text(textbuf, &(account_record->email_address));
893 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
894 _account_db_data_to_text(textbuf, &(account_record->display_name));
896 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
897 _account_db_data_to_text(textbuf, &(account_record->icon_path));
899 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
900 _account_db_data_to_text(textbuf, &(account_record->source));
902 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
903 _account_db_data_to_text(textbuf, &(account_record->package_name));
905 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
906 _account_db_data_to_text(textbuf, &(account_record->access_token));
908 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
909 _account_db_data_to_text(textbuf, &(account_record->domain_name));
911 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
913 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
915 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
917 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
918 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
920 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
921 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
923 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
924 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
926 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
927 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
929 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
930 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
932 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
933 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
934 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
935 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
936 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
938 ACCOUNT_DEBUG("END _account_convert_column_to_account");
942 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
944 char *textbuf = NULL;
946 capability_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
948 capability_record->type = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_KEY);
950 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
952 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
953 _account_db_data_to_text(textbuf, &(capability_record->package_name));
955 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
956 _account_db_data_to_text(textbuf, &(capability_record->user_name));
958 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
960 ACCOUNT_DEBUG("END _account_convert_column_to_capability");
963 bool _account_get_capability_text_cb(account_capability_type_e capability_type, account_capability_state_e capability_value, void *user_data)
965 account_s *data = (account_s*)user_data;
967 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
969 if (cap_data == NULL)
971 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
973 cap_data->type = capability_type;
974 cap_data->value = capability_value;
976 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
978 ACCOUNT_DEBUG("_account_get_capability_text_cb :: %d\n", capability_type);
983 static char *_account_get_text(char *text_data)
985 char *text_value = NULL;
987 if (text_data != NULL) {
988 text_value = strdup(text_data);
989 ACCOUNT_DEBUG("text_value = %s", text_value);
994 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
996 int rc = 0, binding_count =0;
997 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
998 int error_code = ACCOUNT_ERROR_NONE;
999 account_stmt hstmt = NULL;
1001 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1002 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1004 if (!account->user_name && !account->display_name && !account->email_address) {
1005 ACCOUNT_DEBUG("One field should be set among user name, display name, email address\n");
1006 return ACCOUNT_ERROR_INVALID_PARAMETER;
1009 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1010 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1011 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1012 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1013 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1015 hstmt = _account_prepare_query(query);
1016 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1018 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1020 _account_query_bind_text(hstmt, binding_count++, user_name);
1021 _account_query_bind_text(hstmt, binding_count++, package_name);
1022 ACCOUNT_DEBUG("_account_query_step : user_name = %s, package_name=%s\n", user_name, package_name);
1023 rc = _account_query_step(hstmt);
1024 ACCOUNT_DEBUG("_account_query_step return rc=%d\n", rc);
1025 if (rc != SQLITE_DONE) {
1026 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1029 _account_query_finalize(hstmt);
1032 /*update capability*/
1033 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1038 int account_insert_to_db(account_h account, int *account_id)
1040 int ret = ACCOUNT_ERROR_NONE;
1041 int error_code = ACCOUNT_ERROR_NONE;
1044 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1047 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1048 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1050 account_s *data = (account_s*)account;
1052 pthread_mutex_lock(&account_mutex);
1054 if (_account_check_duplicated(data)) {
1055 error_code = ACCOUNT_ERROR_DUPLICATED;
1057 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1059 error_code = _account_execute_insert_query(data);
1061 if (error_code != ACCOUNT_ERROR_NONE)
1065 ACCOUNT_DEBUG( "_account_execute_insert_query, insert error_code : %d", error_code);
1067 _account_insert_capability(data, *account_id);
1069 pthread_mutex_unlock(&account_mutex);
1070 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_INSERT);
1072 return ACCOUNT_ERROR_NONE;
1076 int account_create(account_h *account)
1079 ACCOUNT_DEBUG("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1080 return ACCOUNT_ERROR_INVALID_PARAMETER;
1083 account_s *data = (account_s*)malloc(sizeof(account_s));
1086 ACCOUNT_DEBUG("Memory Allocation Failed");
1087 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1089 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1091 ACCOUNT_DEBUG("create handle=%p\n", *account);
1093 /*Setting account as visible by default*/
1094 data->secret = ACCOUNT_SECRECY_VISIBLE;
1096 /*Setting account as not supporting sync by default*/
1097 data->sync_support = ACCOUNT_NOT_SUPPORTS_SYNC;
1099 *account = (account_h)data;
1101 return ACCOUNT_ERROR_NONE;
1104 int account_set_user_name(account_h account, const char *user_name)
1107 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1108 return ACCOUNT_ERROR_INVALID_PARAMETER;
1112 ACCOUNT_DEBUG("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1113 return ACCOUNT_ERROR_INVALID_PARAMETER;
1116 account_s *data = (account_s*)account;
1118 ACCOUNT_DEBUG("account=%p\n", account);
1120 _ACCOUNT_FREE(data->user_name);
1121 data->user_name = strdup(user_name);
1123 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1125 return ACCOUNT_ERROR_NONE;
1129 int account_set_display_name(account_h account, const char *display_name)
1132 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1133 return ACCOUNT_ERROR_INVALID_PARAMETER;
1136 if (!display_name) {
1137 ACCOUNT_DEBUG("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1138 return ACCOUNT_ERROR_INVALID_PARAMETER;
1141 account_s *data = (account_s*)account;
1143 _ACCOUNT_FREE(data->display_name);
1144 data->display_name = strdup(display_name);
1146 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1148 return ACCOUNT_ERROR_NONE;
1151 int account_set_email_address(account_h account, const char *email_address)
1154 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1155 return ACCOUNT_ERROR_INVALID_PARAMETER;
1158 if (!email_address) {
1159 ACCOUNT_DEBUG("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1160 return ACCOUNT_ERROR_INVALID_PARAMETER;
1163 account_s *data = (account_s*)account;
1165 _ACCOUNT_FREE(data->email_address);
1166 data->email_address = strdup(email_address);
1168 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1170 return ACCOUNT_ERROR_NONE;
1173 int account_set_icon_path(account_h account, const char *icon_path)
1176 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1177 return ACCOUNT_ERROR_INVALID_PARAMETER;
1181 ACCOUNT_DEBUG("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1182 return ACCOUNT_ERROR_INVALID_PARAMETER;
1185 account_s *data = (account_s*)account;
1187 _ACCOUNT_FREE(data->icon_path);
1188 data->icon_path = strdup(icon_path);
1190 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1192 return ACCOUNT_ERROR_NONE;
1196 int account_set_source(account_h account, const char *source)
1199 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1200 return ACCOUNT_ERROR_INVALID_PARAMETER;
1204 ACCOUNT_DEBUG("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1205 return ACCOUNT_ERROR_INVALID_PARAMETER;
1207 account_s *data = (account_s*)account;
1209 _ACCOUNT_FREE(data->source);
1210 data->source = strdup(source);
1212 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1214 return ACCOUNT_ERROR_NONE;
1217 int account_set_package_name(account_h account, const char *package_name)
1220 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1221 return ACCOUNT_ERROR_INVALID_PARAMETER;
1224 if (!package_name) {
1225 ACCOUNT_DEBUG("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1226 return ACCOUNT_ERROR_INVALID_PARAMETER;
1229 account_s *data = (account_s*)account;
1231 _ACCOUNT_FREE(data->package_name);
1232 data->package_name = strdup(package_name);
1234 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1236 return ACCOUNT_ERROR_NONE;
1240 int account_set_domain_name(account_h account, const char *domain_name)
1243 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1244 return ACCOUNT_ERROR_INVALID_PARAMETER;
1248 ACCOUNT_DEBUG("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1249 return ACCOUNT_ERROR_INVALID_PARAMETER;
1251 account_s *data = (account_s*)account;
1253 _ACCOUNT_FREE(data->domain_name);
1254 data->domain_name = strdup(domain_name);
1256 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1258 return ACCOUNT_ERROR_NONE;
1262 int account_set_access_token(account_h account, const char *access_token)
1265 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1266 return ACCOUNT_ERROR_INVALID_PARAMETER;
1269 if (!access_token) {
1270 ACCOUNT_DEBUG("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1271 return ACCOUNT_ERROR_INVALID_PARAMETER;
1274 account_s *data = (account_s*)account;
1276 _ACCOUNT_FREE(data->access_token);
1277 data->access_token = strdup(access_token);
1279 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1281 return ACCOUNT_ERROR_NONE;
1284 int account_set_user_text(account_h account, int index, const char *user_txt)
1287 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1288 return ACCOUNT_ERROR_INVALID_PARAMETER;
1292 ACCOUNT_DEBUG("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1293 return ACCOUNT_ERROR_INVALID_PARAMETER;
1295 if (index >= USER_TXT_CNT || index < 0) {
1296 ACCOUNT_DEBUG("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1297 return ACCOUNT_ERROR_INVALID_PARAMETER;
1300 account_s *data = (account_s*)account;
1302 _ACCOUNT_FREE(data->user_data_txt[index]);
1303 data->user_data_txt[index] = strdup(user_txt);
1305 ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1307 return ACCOUNT_ERROR_NONE;
1310 int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1312 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1314 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1315 ACCOUNT_DEBUG("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1316 return ACCOUNT_ERROR_INVALID_PARAMETER;
1319 account_s *data = (account_s*)account;
1321 data->auth_type = (int)auth_type;
1323 return ACCOUNT_ERROR_NONE;
1326 int account_set_secret(account_h account, const account_secrecy_state_e secret)
1328 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1330 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1331 ACCOUNT_DEBUG("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1332 return ACCOUNT_ERROR_INVALID_PARAMETER;
1335 account_s *data = (account_s*)account;
1337 data->secret = (int)secret;
1339 return ACCOUNT_ERROR_NONE;
1342 int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1344 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1346 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1347 ACCOUNT_DEBUG("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1348 return ACCOUNT_ERROR_INVALID_PARAMETER;
1351 account_s *data = (account_s*)account;
1353 data->sync_support= (int)sync_support;
1355 return ACCOUNT_ERROR_NONE;
1358 int account_set_user_int(account_h account, int index, const int user_int)
1361 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1362 return ACCOUNT_ERROR_INVALID_PARAMETER;
1365 if (index >= USER_INT_CNT ||index < 0) {
1366 ACCOUNT_DEBUG("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1367 return ACCOUNT_ERROR_INVALID_PARAMETER;
1370 account_s *data = (account_s*)account;
1372 data->user_data_int[index] = user_int;
1374 return ACCOUNT_ERROR_NONE;
1378 int account_set_capability(account_h account, account_capability_type_e capability_type, account_capability_state_e capability_value)
1381 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1382 return ACCOUNT_ERROR_INVALID_PARAMETER;
1385 if ( (capability_type < 0) || (capability_type > ACCOUNT_CAPABILITY_MOBILE_TRACKER)) {
1386 ACCOUNT_DEBUG("(%s)-(%d) capability_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1387 return ACCOUNT_ERROR_INVALID_PARAMETER;
1390 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1391 ACCOUNT_DEBUG("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1392 return ACCOUNT_ERROR_INVALID_PARAMETER;
1395 account_s *data = (account_s*)account;
1397 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1399 if (cap_data == NULL)
1400 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1401 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1403 cap_data->type = capability_type;
1404 cap_data->value = capability_value;
1405 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1407 return ACCOUNT_ERROR_NONE;
1410 int account_get_user_name(account_h account, char **user_name)
1413 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1414 return ACCOUNT_ERROR_INVALID_PARAMETER;
1418 ACCOUNT_DEBUG("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1419 return ACCOUNT_ERROR_INVALID_PARAMETER;
1422 account_s *data = (account_s*)account;
1424 (*user_name) = NULL;
1425 *user_name = _account_get_text(data->user_name);
1427 return ACCOUNT_ERROR_NONE;
1430 int account_get_display_name(account_h account, char **display_name)
1433 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1434 return ACCOUNT_ERROR_INVALID_PARAMETER;
1437 if (!display_name) {
1438 ACCOUNT_DEBUG("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
1439 return ACCOUNT_ERROR_INVALID_PARAMETER;
1442 account_s *data = (account_s*)account;
1444 (*display_name) = NULL;
1446 *display_name = _account_get_text(data->display_name);
1448 return ACCOUNT_ERROR_NONE;
1451 int account_get_email_address(account_h account,char **email_address)
1454 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1455 return ACCOUNT_ERROR_INVALID_PARAMETER;
1458 if (!email_address) {
1459 ACCOUNT_DEBUG("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
1460 return ACCOUNT_ERROR_INVALID_PARAMETER;
1463 account_s *data = (account_s*)account;
1465 (*email_address) = NULL;
1467 *email_address = _account_get_text(data->email_address);
1469 return ACCOUNT_ERROR_NONE;
1472 int account_get_icon_path(account_h account, char **icon_path)
1475 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1476 return ACCOUNT_ERROR_INVALID_PARAMETER;
1480 ACCOUNT_DEBUG("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
1481 return ACCOUNT_ERROR_INVALID_PARAMETER;
1484 account_s *data = (account_s*)account;
1486 (*icon_path) = NULL;
1488 *icon_path = _account_get_text(data->icon_path);
1490 return ACCOUNT_ERROR_NONE;
1493 int account_get_source(account_h account, char **source)
1496 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1497 return ACCOUNT_ERROR_INVALID_PARAMETER;
1501 ACCOUNT_DEBUG("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1502 return ACCOUNT_ERROR_INVALID_PARAMETER;
1505 account_s *data = (account_s*)account;
1509 *source = _account_get_text(data->source);
1511 return ACCOUNT_ERROR_NONE;
1514 int account_get_package_name(account_h account, char **package_name)
1517 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1518 return ACCOUNT_ERROR_INVALID_PARAMETER;
1521 if (!package_name) {
1522 ACCOUNT_DEBUG("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
1523 return ACCOUNT_ERROR_INVALID_PARAMETER;
1526 account_s *data = (account_s*)account;
1528 (*package_name) = NULL;
1530 *package_name = _account_get_text(data->package_name);
1532 return ACCOUNT_ERROR_NONE;
1535 int account_get_domain_name(account_h account, char **domain_name)
1538 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1539 return ACCOUNT_ERROR_INVALID_PARAMETER;
1543 ACCOUNT_DEBUG("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
1544 return ACCOUNT_ERROR_INVALID_PARAMETER;
1547 account_s *data = (account_s*)account;
1549 (*domain_name) = NULL;
1551 *domain_name = _account_get_text(data->domain_name);
1553 return ACCOUNT_ERROR_NONE;
1556 int account_get_access_token(account_h account, char **access_token)
1559 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1560 return ACCOUNT_ERROR_INVALID_PARAMETER;
1563 if (!access_token) {
1564 ACCOUNT_DEBUG("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
1565 return ACCOUNT_ERROR_INVALID_PARAMETER;
1568 account_s *data = (account_s*)account;
1570 (*access_token) = NULL;
1572 *access_token = _account_get_text(data->access_token);
1574 return ACCOUNT_ERROR_NONE;
1577 int account_get_user_text(account_h account, int user_text_index, char **text)
1580 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1581 return ACCOUNT_ERROR_INVALID_PARAMETER;
1585 ACCOUNT_DEBUG("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
1586 return ACCOUNT_ERROR_INVALID_PARAMETER;
1588 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1590 account_s *data = (account_s*)account;
1594 *text = _account_get_text(data->user_data_txt[user_text_index]);
1596 return ACCOUNT_ERROR_NONE;
1599 int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
1602 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1603 return ACCOUNT_ERROR_INVALID_PARAMETER;
1606 ACCOUNT_DEBUG("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
1607 return ACCOUNT_ERROR_INVALID_PARAMETER;
1610 account_s* data = (account_s*)account;
1612 *auth_type = data->auth_type;
1614 return ACCOUNT_ERROR_NONE;
1617 int account_get_secret(account_h account, account_secrecy_state_e *secret)
1620 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1621 return ACCOUNT_ERROR_INVALID_PARAMETER;
1624 ACCOUNT_DEBUG("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
1625 return ACCOUNT_ERROR_INVALID_PARAMETER;
1628 account_s* data = (account_s*)account;
1630 *secret = data->secret;
1632 return ACCOUNT_ERROR_NONE;
1635 int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
1638 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1639 return ACCOUNT_ERROR_INVALID_PARAMETER;
1641 if (!sync_support) {
1642 ACCOUNT_DEBUG("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
1643 return ACCOUNT_ERROR_INVALID_PARAMETER;
1646 account_s* data = (account_s*)account;
1648 *sync_support = data->sync_support;
1650 return ACCOUNT_ERROR_NONE;
1653 int account_get_account_id(account_h account, int *account_id)
1656 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1657 return ACCOUNT_ERROR_INVALID_PARAMETER;
1660 ACCOUNT_DEBUG("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
1661 return ACCOUNT_ERROR_INVALID_PARAMETER;
1664 account_s *data = (account_s*)account;
1666 *account_id = data->id;
1668 return ACCOUNT_ERROR_NONE;
1671 int account_get_user_int(account_h account, int user_int_index, int *integer)
1674 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1675 return ACCOUNT_ERROR_INVALID_PARAMETER;
1678 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1681 ACCOUNT_DEBUG("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
1682 return ACCOUNT_ERROR_INVALID_PARAMETER;
1685 account_s *data = (account_s*)account;
1687 *integer = data->user_data_int[user_int_index];
1689 return ACCOUNT_ERROR_NONE;
1692 int account_get_capability(account_h account, capability_cb cb_func, void *user_data)
1694 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1695 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1698 account_s *data = (account_s*)account;
1700 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1701 account_capability_s *cap_data = NULL;
1703 cap_data = (account_capability_s*)iter->data;
1705 ACCOUNT_DEBUG("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1707 cb_func(cap_data->type, cap_data->value, user_data);
1710 return ACCOUNT_ERROR_NONE;
1713 int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
1715 int error_code = ACCOUNT_ERROR_NONE;
1716 account_stmt hstmt = NULL;
1717 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1720 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1721 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1723 int ret = ACCOUNT_ERROR_NONE;
1725 if (!g_hAccountDB) {
1726 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1729 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1731 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1732 hstmt = _account_prepare_query(query);
1734 rc = _account_query_step(hstmt);
1735 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1737 account_capability_s* capability_record = NULL;
1739 while (rc == SQLITE_ROW) {
1740 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1742 if (capability_record == NULL) {
1743 ACCOUNT_DEBUG("malloc Failed");
1747 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1749 _account_convert_column_to_capability(hstmt, capability_record);
1751 cb_func(capability_record->type, capability_record->value, user_data);
1753 _account_free_capability_items(capability_record);
1754 _ACCOUNT_FREE(capability_record);
1756 rc = _account_query_step(hstmt);
1759 _account_query_finalize(hstmt);
1762 error_code = ACCOUNT_ERROR_NONE;
1765 if (hstmt != NULL) {
1766 _account_query_finalize(hstmt);
1770 pthread_mutex_unlock(&account_mutex);
1774 static int _account_update_account(account_s *account, int account_id)
1776 int rc = 0, binding_count =0;
1777 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1778 int error_code = ACCOUNT_ERROR_NONE;
1779 account_stmt hstmt = NULL;
1781 if (!account->user_name && !account->display_name && !account->email_address) {
1782 ACCOUNT_DEBUG("One field should be set among user name, display name, email address\n");
1783 return ACCOUNT_ERROR_INVALID_PARAMETER;
1786 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1787 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1788 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1789 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1790 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1792 hstmt = _account_prepare_query(query);
1793 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1795 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1796 _account_query_bind_int(hstmt, binding_count++, account_id);
1798 rc = _account_query_step(hstmt);
1799 ACCOUNT_DEBUG("_account_query_step return rc=%d\n", rc);
1800 if (rc != SQLITE_DONE) {
1801 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1804 _account_query_finalize(hstmt);
1807 /*update capability*/
1808 error_code = _account_update_capability(account, account_id);
1813 int account_update_to_db_by_id(const account_h account, int account_id)
1815 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1816 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1817 int error_code = ACCOUNT_ERROR_NONE;
1818 account_s* data = (account_s*)account;
1820 int ret = ACCOUNT_ERROR_NONE;
1822 if (!g_hAccountDB) {
1823 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1826 pthread_mutex_lock(&account_mutex);
1828 error_code = _account_update_account(data, account_id);
1830 pthread_mutex_unlock(&account_mutex);
1831 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_UPDATE);
1833 return ACCOUNT_ERROR_NONE;
1836 int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
1838 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1839 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1840 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1842 int error_code = ACCOUNT_ERROR_NONE;
1843 account_s *data = (account_s*)account;
1845 int ret = ACCOUNT_ERROR_NONE;
1847 if (!g_hAccountDB) {
1848 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1850 pthread_mutex_lock(&account_mutex);
1852 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
1854 pthread_mutex_unlock(&account_mutex);
1855 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_UPDATE);
1857 return ACCOUNT_ERROR_NONE;
1860 int account_foreach_account_from_db(account_cb callback, void *user_data)
1862 int error_code = ACCOUNT_ERROR_NONE;
1863 account_stmt hstmt = NULL;
1864 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1866 GList *account_list = NULL;
1868 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
1870 int ret = ACCOUNT_ERROR_NONE;
1872 if (!g_hAccountDB) {
1873 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1876 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1878 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1879 hstmt = _account_prepare_query(query);
1881 rc = _account_query_step(hstmt);
1883 account_s *account_record = NULL;
1885 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1888 while(rc == SQLITE_ROW) {
1889 account_record = (account_s*) malloc(sizeof(account_s));
1891 if (account_record == NULL) {
1892 ACCOUNT_DEBUG("malloc Failed");
1895 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1897 _account_convert_column_to_account(hstmt, account_record);
1899 account_list = g_list_append(account_list, account_record);
1901 ACCOUNT_DEBUG("##### URUSA account_record(%d) = %p\n", k, account_record);
1903 rc = _account_query_step(hstmt);
1907 _account_query_finalize(hstmt);
1912 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
1913 account_s *account = NULL;
1914 account = (account_s*)iter->data;
1915 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
1916 callback((account_h)account, user_data);
1920 error_code = ACCOUNT_ERROR_NONE;
1923 if (hstmt != NULL) {
1924 _account_query_finalize(hstmt);
1928 _account_glist_free(account_list);
1929 account_list = NULL;
1936 int account_query_account_by_account_id(int account_db_id, account_h *account)
1938 int error_code = ACCOUNT_ERROR_NONE;
1939 account_stmt hstmt = NULL;
1940 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1943 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1944 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1946 int ret = ACCOUNT_ERROR_NONE;
1948 if (!g_hAccountDB) {
1949 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1952 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1954 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1955 hstmt = _account_prepare_query(query);
1957 rc = _account_query_step(hstmt);
1958 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1960 account_s *account_record = (account_s *)(*account);
1962 while (rc == SQLITE_ROW) {
1963 _account_convert_column_to_account(hstmt, account_record);
1964 ACCOUNT_DEBUG("get account info by id %p\n", account_record);
1965 rc = _account_query_step(hstmt);
1968 _account_query_finalize(hstmt);
1969 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
1972 error_code = ACCOUNT_ERROR_NONE;
1975 if (hstmt != NULL) {
1976 _account_query_finalize(hstmt);
1980 pthread_mutex_unlock(&account_mutex);
1984 int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
1986 int error_code = ACCOUNT_ERROR_NONE;
1987 account_stmt hstmt = NULL;
1988 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1991 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1992 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
1995 int ret = ACCOUNT_ERROR_NONE;
1997 if (!g_hAccountDB) {
1998 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2001 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2003 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2005 hstmt = _account_prepare_query(query);
2007 int binding_count = 1;
2008 _account_query_bind_text(hstmt, binding_count++, user_name);
2010 rc = _account_query_step(hstmt);
2012 account_s *account_head = NULL;
2014 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2018 account_head = (account_s*) malloc(sizeof(account_s));
2019 if (account_head == NULL) {
2020 ACCOUNT_DEBUG("malloc Failed");
2021 if (hstmt != NULL) {
2022 _account_query_finalize(hstmt);
2025 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2027 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2029 while (rc == SQLITE_ROW) {
2030 account_s* account_record = NULL;
2032 account_record = (account_s*) malloc(sizeof(account_s));
2034 if (account_record == NULL) {
2035 ACCOUNT_DEBUG("malloc Failed");
2038 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2040 _account_convert_column_to_account(hstmt, account_record);
2042 account_head->account_list = g_list_append(account_head->account_list, account_record);
2044 rc = _account_query_step(hstmt);
2046 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2049 _account_query_finalize(hstmt);
2055 tmp = g_list_length(account_head->account_list);
2056 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2058 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2060 account = (account_h)iter->data;
2062 account_s *testaccount = (account_s*)account;
2064 ACCOUNT_DEBUG("id = %d", testaccount->id);
2065 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
2066 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
2067 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
2068 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
2069 ACCOUNT_DEBUG("source = %s", testaccount->source);
2070 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
2071 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
2072 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
2073 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
2074 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
2075 ACCOUNT_DEBUG("sync_support = %d", testaccount->sync_support);
2076 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
2077 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
2078 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
2079 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
2080 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
2081 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
2082 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
2083 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
2084 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
2085 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
2087 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2089 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
2091 cb_func(account, user_data);
2095 error_code = ACCOUNT_ERROR_NONE;
2098 if (hstmt != NULL) {
2099 _account_query_finalize(hstmt);
2103 if (account_head->account_list) {
2104 _account_glist_free(account_head->account_list);
2105 account_head->account_list = NULL;
2106 _account_free_account_items(account_head);
2107 _ACCOUNT_FREE(account_head);
2111 pthread_mutex_unlock(&account_mutex);
2115 int account_query_account_by_capability(account_cb cb_func, account_capability_type_e capability_type, account_capability_state_e capability_value, void* user_data)
2117 int error_code = ACCOUNT_ERROR_NONE;
2118 account_stmt hstmt = NULL;
2119 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2122 if ( (capability_type < 0) || (capability_type > ACCOUNT_CAPABILITY_MOBILE_TRACKER)) {
2123 ACCOUNT_DEBUG("(%s)-(%d) capability_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
2124 return ACCOUNT_ERROR_INVALID_PARAMETER;
2127 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2128 ACCOUNT_DEBUG("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2129 return ACCOUNT_ERROR_INVALID_PARAMETER;
2132 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2134 int ret = ACCOUNT_ERROR_NONE;
2136 if (!g_hAccountDB) {
2137 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2140 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2142 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2144 hstmt = _account_prepare_query(query);
2146 int binding_count = 1;
2147 _account_query_bind_int(hstmt, binding_count++, (int)capability_type);
2148 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
2150 rc = _account_query_step(hstmt);
2152 account_s* account_head = NULL;
2154 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2158 account_head = (account_s*) malloc(sizeof(account_s));
2159 if (account_head == NULL) {
2160 ACCOUNT_DEBUG("malloc Failed");
2161 if (hstmt != NULL) {
2162 _account_query_finalize(hstmt);
2165 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2167 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2169 while (rc == SQLITE_ROW) {
2170 account_s* account_record = NULL;
2172 account_record = (account_s*) malloc(sizeof(account_s));
2174 if (account_record == NULL) {
2175 ACCOUNT_DEBUG("malloc Failed");
2178 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2180 _account_convert_column_to_account(hstmt, account_record);
2182 account_head->account_list = g_list_append(account_head->account_list, account_record);
2184 rc = _account_query_step(hstmt);
2186 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2189 _account_query_finalize(hstmt);
2195 tmp = g_list_length(account_head->account_list);
2196 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2198 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2199 account_h account = NULL;
2200 account = (account_h)iter->data;
2203 account_s* testaccount = (account_s*)account;
2205 ACCOUNT_DEBUG("id = %d", testaccount->id);
2206 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
2207 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
2208 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
2209 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
2210 ACCOUNT_DEBUG("source = %s", testaccount->source);
2211 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
2212 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
2213 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
2214 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
2215 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
2216 ACCOUNT_DEBUG("sync_support = %d", testaccount->sync_support);
2217 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
2218 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
2219 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
2220 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
2221 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
2222 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
2223 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
2224 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
2225 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
2226 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
2228 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2230 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
2232 cb_func(account, user_data);
2237 error_code = ACCOUNT_ERROR_NONE;
2240 if (hstmt != NULL) {
2241 _account_query_finalize(hstmt);
2245 if (account_head->account_list) {
2246 _account_glist_free(account_head->account_list);
2247 account_head->account_list = NULL;
2248 _account_free_account_items(account_head);
2249 _ACCOUNT_FREE(account_head);
2253 pthread_mutex_unlock(&account_mutex);
2258 int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
2260 int error_code = ACCOUNT_ERROR_NONE;
2261 account_stmt hstmt = NULL;
2262 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2265 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2266 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2268 int ret = ACCOUNT_ERROR_NONE;
2270 if (!g_hAccountDB) {
2271 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2274 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2276 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
2278 hstmt = _account_prepare_query(query);
2280 int binding_count = 1;
2281 _account_query_bind_text(hstmt, binding_count++, package_name);
2283 rc = _account_query_step(hstmt);
2285 account_s* account_head = NULL;
2287 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2291 account_head = (account_s*) malloc(sizeof(account_s));
2292 if (account_head == NULL) {
2293 ACCOUNT_DEBUG("malloc Failed");
2294 if (hstmt != NULL) {
2295 _account_query_finalize(hstmt);
2298 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2300 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2302 while (rc == SQLITE_ROW) {
2303 account_s* account_record = NULL;
2305 account_record = (account_s*) malloc(sizeof(account_s));
2307 if (account_record == NULL) {
2308 ACCOUNT_DEBUG("malloc Failed");
2311 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2313 _account_convert_column_to_account(hstmt, account_record);
2315 account_head->account_list = g_list_append(account_head->account_list, account_record);
2317 rc = _account_query_step(hstmt);
2319 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2322 _account_query_finalize(hstmt);
2328 tmp = g_list_length(account_head->account_list);
2329 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2331 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2332 account_h account = NULL;
2333 account = (account_h)iter->data;
2336 account_s* testaccount = (account_s*)account;
2338 ACCOUNT_DEBUG("id = %d", testaccount->id);
2339 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
2340 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
2341 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
2342 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
2343 ACCOUNT_DEBUG("source = %s", testaccount->source);
2344 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
2345 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
2346 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
2347 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
2348 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
2349 ACCOUNT_DEBUG("sync_support = %d", testaccount->sync_support);
2350 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
2351 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
2352 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
2353 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
2354 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
2355 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
2356 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
2357 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
2358 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
2359 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
2361 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2363 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
2365 cb_func(account, user_data);
2369 error_code = ACCOUNT_ERROR_NONE;
2372 if (hstmt != NULL) {
2373 _account_query_finalize(hstmt);
2377 if (account_head->account_list) {
2378 _account_glist_free(account_head->account_list);
2379 account_head->account_list = NULL;
2380 _account_free_account_items(account_head);
2381 _ACCOUNT_FREE(account_head);
2385 pthread_mutex_unlock(&account_mutex);
2389 int account_delete(int account_id)
2391 int error_code = ACCOUNT_ERROR_NONE;
2392 account_stmt hstmt = NULL;
2393 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2395 int ret_transaction = 0, ret =0;
2396 bool is_success = FALSE;
2398 if (!g_hAccountDB) {
2399 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2402 /* transaction control required*/
2403 ret_transaction = _account_begin_transaction();
2405 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2406 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2407 pthread_mutex_unlock(&account_mutex);
2408 return ret_transaction;
2411 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2413 hstmt = _account_prepare_query(query);
2414 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2415 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2417 rc = _account_query_step(hstmt);
2418 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2420 _account_query_finalize(hstmt);
2423 ACCOUNT_MEMSET(query, 0, sizeof(query));
2425 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2427 hstmt = _account_prepare_query(query);
2428 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2429 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2431 rc = _account_query_step(hstmt);
2432 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2434 _account_query_finalize(hstmt);
2440 if (hstmt != NULL) {
2441 _account_query_finalize(hstmt);
2445 ret_transaction = _account_end_transaction(is_success);
2447 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2448 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2450 if (is_success == true)
2451 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_DELETE);
2454 pthread_mutex_unlock(&account_mutex);
2460 int account_delete_from_db_by_id(int account_db_id)
2462 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
2464 account_delete(account_db_id);
2467 int account_delete_from_db_by_user_name(char *user_name, char *package_name)
2469 int error_code = ACCOUNT_ERROR_NONE;
2470 account_stmt hstmt = NULL;
2471 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2473 int ret_transaction = 0, ret =0;
2474 bool is_success = FALSE;
2476 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2477 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2479 if (!g_hAccountDB) {
2480 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2483 /* transaction control required*/
2484 ret_transaction = _account_begin_transaction();
2486 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2487 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2488 pthread_mutex_unlock(&account_mutex);
2489 return ret_transaction;
2492 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2494 hstmt = _account_prepare_query(query);
2496 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2497 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2499 int binding_count = 1;
2500 _account_query_bind_text(hstmt, binding_count++, user_name);
2501 _account_query_bind_text(hstmt, binding_count++, package_name);
2503 rc = _account_query_step(hstmt);
2504 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2506 _account_query_finalize(hstmt);
2509 ACCOUNT_MEMSET(query, 0, sizeof(query));
2511 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2513 hstmt = _account_prepare_query(query);
2514 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2515 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2518 _account_query_bind_text(hstmt, binding_count++, user_name);
2519 _account_query_bind_text(hstmt, binding_count++, package_name);
2521 rc = _account_query_step(hstmt);
2522 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));
2524 _account_query_finalize(hstmt);
2530 if (hstmt != NULL) {
2531 _account_query_finalize(hstmt);
2535 ret_transaction = _account_end_transaction(is_success);
2537 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2538 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2540 if (is_success == true)
2541 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_DELETE);
2544 pthread_mutex_unlock(&account_mutex);
2549 int account_delete_from_db_by_package_name(char *package_name)
2551 int error_code = ACCOUNT_ERROR_NONE;
2552 account_stmt hstmt = NULL;
2553 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2555 int ret_transaction = 0, ret =0;
2556 bool is_success = FALSE;
2558 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2560 if (!g_hAccountDB) {
2561 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2564 /* transaction control required*/
2565 ret_transaction = _account_begin_transaction();
2567 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2568 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2569 pthread_mutex_unlock(&account_mutex);
2570 return ret_transaction;
2573 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
2575 hstmt = _account_prepare_query(query);
2577 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2578 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2580 int binding_count = 1;
2581 _account_query_bind_text(hstmt, binding_count++, package_name);
2583 rc = _account_query_step(hstmt);
2584 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2586 _account_query_finalize(hstmt);
2589 ACCOUNT_MEMSET(query, 0, sizeof(query));
2591 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
2593 hstmt = _account_prepare_query(query);
2594 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2595 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2598 _account_query_bind_text(hstmt, binding_count++, package_name);
2600 rc = _account_query_step(hstmt);
2601 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));
2603 _account_query_finalize(hstmt);
2609 if (hstmt != NULL) {
2610 _account_query_finalize(hstmt);
2614 ret_transaction = _account_end_transaction(is_success);
2616 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2617 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2619 if (is_success == true)
2620 _account_insert_delete_update_dbus_notification_send(ACCOUNT_DBUS_NOTI_NAME_DELETE);
2623 pthread_mutex_unlock(&account_mutex);
2628 int account_get_total_count_from_db(int *count)
2631 ACCOUNT_DEBUG("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2632 return ACCOUNT_ERROR_INVALID_PARAMETER;
2635 char query[1024] = {0, };
2636 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2637 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2639 *count = _account_get_record_count(query);
2642 account_stmt pStmt = NULL;
2644 assert(NULL != g_hAccountDB);
2645 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2647 rc = sqlite3_step(pStmt);
2648 if (SQLITE_ROW != rc) {
2649 ACCOUNT_DEBUG("[ERROR] sqlite3_step() failed\n");
2650 sqlite3_finalize(pStmt);
2651 return ACCOUNT_ERROR_DB_FAILED;
2654 ncount = sqlite3_column_int(pStmt, 0);
2658 ACCOUNT_DEBUG("Number of account : %d", ncount);
2659 sqlite3_finalize(pStmt);
2662 ACCOUNT_DEBUG("[ERROR] Number of account : %d, End", ncount);
2663 return ACCOUNT_ERROR_DB_FAILED;
2666 return ACCOUNT_ERROR_NONE;
2669 int account_destroy(account_h account)
2671 account_s *data = (account_s*)account;
2673 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2675 ACCOUNT_DEBUG("destroy handle=%p\n", data);
2677 _account_free_account_items(data);
2678 _ACCOUNT_FREE(data);
2680 return ACCOUNT_ERROR_NONE;