0c3592ff91ac4f90660d693f9ecbfb0d046279d3
[profile/ivi/libaccounts-svc.git] / src / account.c
1 /*
2  * libaccounts-svc
3  *
4  * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Tarun Kumar <tarun.kr@samsung.com>, Sukumar Moharana <msukumar@samsung.com>, Wonyoung Lee <wy1115.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <account.h>
26 #include <glib.h>
27 #include <db-util.h>
28 #include <pthread.h>
29 #include <assert.h>
30 #include <account-private.h>
31 #include <dlog.h>
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "ACCOUNT"
38
39 #define ACCOUNT_DLOG_USE
40 #ifdef ACCOUNT_DLOG_USE
41 #define ACCOUNT_DEBUG(fmt, arg...) \
42                 LOGD(": " fmt "\n", ##arg);
43 #else
44 #define ACCOUNT_DEBUG(fmt, arg...) \
45                 printf(": " fmt "\n", ##arg);
46 #endif
47
48 #define ACCOUNT_RETURN_VAL(eval, expr, ret_val, X)\
49         if (!(eval)) \
50 {\
51         expr; \
52         if (1)\
53         {ACCOUNT_DEBUG X;}\
54         return ret_val;\
55 } else {;}
56
57 #define ACCOUNT_SNPRINTF(dest,size,format,arg...)       \
58         do { \
59                         snprintf(dest,size-1,format,##arg); \
60         }while(0)
61         /*      If the same pointer is passed to free twice,    known as a double free. To avoid this, set pointers to
62 NULL after passing      them to free: free(NULL) is safe (it does nothing).
63          */
64
65 #define ACCOUNT_MEMSET(dest,value,size) \
66         do { \
67                         memset(dest,value,size); \
68         }while(0)
69
70 #define ACCOUNT_CATCH_ERROR(eval, expr, error_val, X) \
71         if (!(eval)) \
72 {\
73         expr; \
74         error_code = (error_val);\
75         if (1)\
76         {ACCOUNT_DEBUG X;}\
77         goto CATCH;\
78 } else {;}
79
80
81 static sqlite3* g_hAccountDB = NULL;
82 static int              g_refCntDB = 0;
83 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
84
85 static int _account_gslist_free(GSList* list);
86 static int _account_glist_free(GList* list);
87
88 static const char *_account_db_err_msg()
89 {
90         assert(NULL != g_hAccountDB);
91         return sqlite3_errmsg(g_hAccountDB);
92 }
93
94 static int _account_get_record_count(char* query)
95 {
96         int rc = -1;
97         int ncount = 0;
98         account_stmt pStmt = NULL;
99
100         assert(NULL != query);
101         assert(NULL != g_hAccountDB);
102         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
103
104         rc = sqlite3_step(pStmt);
105         if (SQLITE_ROW != rc) {
106                 ACCOUNT_DEBUG("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
107                 sqlite3_finalize(pStmt);
108                 return ACCOUNT_ERROR_DB_FAILED;
109         }
110
111         ncount = sqlite3_column_int(pStmt, 0);
112
113         ACCOUNT_DEBUG("count : %d, End", ncount);
114         sqlite3_finalize(pStmt);
115
116         return ncount;
117 }
118
119 static int _account_execute_query(char *query)
120 {
121         int rc = -1;
122         char* pszErrorMsg = NULL;
123
124         assert(NULL != query);
125         assert(NULL != g_hAccountDB);
126
127         ACCOUNT_DEBUG("query : %s", query);
128
129         rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
130         if (SQLITE_OK != rc) {
131                 ACCOUNT_DEBUG("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
132                 sqlite3_free(pszErrorMsg);
133         }
134
135         return rc;
136 }
137
138 static int _account_begin_transaction(void)
139 {
140         int ret = -1;
141
142         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
143
144         if (ret != SQLITE_OK) {
145                 ACCOUNT_DEBUG("_account_svc_begin_transaction fail :: %d", ret);
146                 return ACCOUNT_ERROR_DB_FAILED;
147         }
148
149    return ACCOUNT_ERROR_NONE;
150 }
151
152 static int _account_end_transaction(bool is_success)
153 {
154         int ret = -1;
155
156         if (is_success == true) {
157                 ret = _account_execute_query("COMMIT TRANSACTION");
158         } else {
159                 ret = _account_execute_query("ROLLBACK TRANSACTION");
160         }
161
162         if (ret != SQLITE_OK) {
163                 ACCOUNT_DEBUG("_account_svc_end_transaction fail :: %d", ret);
164                 return ACCOUNT_ERROR_DB_FAILED;
165         }
166
167    return ACCOUNT_ERROR_NONE;
168 }
169
170 static int _account_create_all_tables(void)
171 {
172         int rc = -1;
173         int error_code = ACCOUNT_ERROR_NONE;
174         char    query[ACCOUNT_SQL_LEN_MAX] = {0, };
175
176
177         ACCOUNT_MEMSET(query, 0, sizeof(query));
178
179         ACCOUNT_DEBUG("_account_create_all_tables begin");
180
181         /*Create the account table*/
182         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
183         rc = _account_get_record_count(query);
184         ACCOUNT_DEBUG("rc = %d \n", rc);
185
186         if (rc <= 0) {
187                 ACCOUNT_MEMSET(query, 0, sizeof(query));
188                 ACCOUNT_SNPRINTF(query, sizeof(query),  ACCOUNT_SCHEMA, ACCOUNT_TABLE);
189                 rc = _account_execute_query(query);
190                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
191         }
192
193         ACCOUNT_MEMSET(query, 0, sizeof(query));
194         /*Create capability table*/
195         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
196         rc = _account_get_record_count(query);
197         ACCOUNT_DEBUG("rc = %d \n", rc);
198
199         if (rc <= 0) {
200                 ACCOUNT_MEMSET(query, 0, sizeof(query));
201                 ACCOUNT_SNPRINTF(query, sizeof(query),  CAPABILITY_SCHEMA, CAPABILITY_TABLE);
202                 rc = _account_execute_query(query);
203
204                 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
205         }
206
207         return error_code;
208 }
209
210
211 static bool _account_check_is_all_table_exists()
212 {
213         int     rc = 0;
214         char    query[ACCOUNT_SQL_LEN_MAX] = {0,};
215         ACCOUNT_MEMSET(query, 0, sizeof(query));
216
217         ACCOUNT_DEBUG("_account_check_is_all_table_exists");
218
219         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s')",
220                         ACCOUNT_TABLE, CAPABILITY_TABLE);
221         rc = _account_get_record_count(query);
222         ACCOUNT_DEBUG("rc = %d \n", rc);
223
224         if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
225                 ACCOUNT_DEBUG("Table count is not matched rc=%d\n", rc);
226                 return FALSE;
227         }
228
229         ACCOUNT_DEBUG("END of _account_check_is_all_table_exists\n");
230
231         return TRUE;
232 }
233
234 static int _account_db_open(void)
235 {
236         int     rc = 0;
237         bool is_success = false;
238         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
239
240         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
241
242         if (!g_hAccountDB) {
243                 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
244                 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
245
246                 g_refCntDB++;
247                 is_success = true;
248                 ACCOUNT_DEBUG("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
249         } else {
250                 g_refCntDB++;
251                 is_success = true;
252                 ACCOUNT_DEBUG("The database already connected. refcnt=%d ", g_refCntDB);
253         }
254
255         return ACCOUNT_ERROR_NONE;
256 }
257
258 static int _account_db_close(void)
259 {
260         int     rc = 0;
261
262         if (g_hAccountDB) {
263                 if (g_refCntDB > 0) {
264                         g_refCntDB--;
265                         ACCOUNT_DEBUG("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
266                 }
267
268                 if (g_refCntDB == 0) {
269                         rc = db_util_close(g_hAccountDB);
270                         ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
271
272                         g_hAccountDB = NULL;
273                         ACCOUNT_DEBUG( "_account_svc_db_close: The database disconnected really. ");
274                 }
275         } else {
276                 ACCOUNT_DEBUG( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
277         }
278
279         return ACCOUNT_ERROR_NONE;
280 }
281
282 static int _account_connect(void)
283 {
284         int error_code = ACCOUNT_ERROR_NONE;
285
286         pthread_mutex_lock(&account_mutex);
287
288         ACCOUNT_DEBUG("db path = %s\n", ACCOUNT_DB_NAME);
289
290         error_code = _account_db_open();
291         if (ACCOUNT_ERROR_NONE != error_code) {
292                 ACCOUNT_DEBUG("The database isn't connected.\n");
293                 pthread_mutex_unlock(&account_mutex);
294                 return ACCOUNT_ERROR_DB_NOT_OPENED;
295         }
296
297         if (FALSE == _account_check_is_all_table_exists())
298                 error_code = _account_create_all_tables();
299
300         pthread_mutex_unlock(&account_mutex);
301         return ACCOUNT_ERROR_NONE;
302 }
303
304 int account_connect (void)
305 {
306         return _account_connect();
307 }
308
309 static int _account_disconnect(void)
310 {
311         int error_code = ACCOUNT_ERROR_NONE;
312
313         pthread_mutex_lock(&account_mutex);
314         ACCOUNT_DEBUG("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
315
316         error_code = _account_db_close();
317         pthread_mutex_unlock(&account_mutex);
318
319         return error_code;
320 }
321
322 int account_disconnect (void)
323 {
324         return _account_disconnect();
325 }
326
327 static int _account_free_capability_items(account_capability_s *data)
328 {
329         _ACCOUNT_FREE(data->package_name);
330         _ACCOUNT_FREE(data->user_name);
331
332         return ACCOUNT_ERROR_NONE;
333 }
334
335 static int _account_free_account_items(account_s *data)
336 {
337         _ACCOUNT_FREE(data->user_name);
338         _ACCOUNT_FREE(data->email_address);
339         _ACCOUNT_FREE(data->display_name);
340         _ACCOUNT_FREE(data->icon_path);
341         _ACCOUNT_FREE(data->source);
342         _ACCOUNT_FREE(data->package_name);
343         _ACCOUNT_FREE(data->domain_name);
344
345         int i;
346         for(i=0;i<USER_TXT_CNT;i++)
347                 _ACCOUNT_FREE(data->user_data_txt[i]);
348
349         _account_gslist_free(data->capablity_list);
350         _account_glist_free(data->account_list);
351
352         return ACCOUNT_ERROR_NONE;
353 }
354
355 static int _account_gslist_free(GSList* list)
356 {
357         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
358
359         GSList* iter;
360
361         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
362                 account_capability_s *cap_data = (account_capability_s*)iter->data;
363                 _account_free_capability_items(cap_data);
364         }
365
366         g_slist_free(list);
367         list = NULL;
368
369         return ACCOUNT_ERROR_NONE;
370 }
371
372 static int _account_glist_free(GList* list)
373 {
374         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
375
376         GList* iter;
377
378         for (iter = list; iter != NULL; iter = g_list_next(iter)) {
379                 account_s *account_record = (account_s*)iter->data;
380                 _account_free_account_items(account_record);
381         }
382
383         g_list_free(list);
384         list = NULL;
385
386         return ACCOUNT_ERROR_NONE;
387 }
388
389 static gboolean _account_check_duplicated(account_s *data)
390 {
391         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
392         int count = 0;
393
394         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
395
396         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and domain_name='%s'"
397                         , ACCOUNT_TABLE, data->user_name, data->domain_name);
398
399         count = _account_get_record_count(query);
400         if (count > 0) {
401                 ACCOUNT_DEBUG("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
402                         count, data->user_name, data->domain_name );
403                 return TRUE;
404         }
405
406         return FALSE;
407 }
408
409 static int _account_get_next_sequence(char *pszName)
410 {
411         int                     rc = 0;
412         account_stmt    pStmt = NULL;
413         int                     max_seq = 0;
414         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
415
416         ACCOUNT_DEBUG( "[Enter] pszName:%s\n", pszName);
417
418         ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
419         ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery),  "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
420         rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
421
422         rc = sqlite3_step(pStmt);
423         max_seq = sqlite3_column_int(pStmt, 0);
424         max_seq++;
425
426         ACCOUNT_DEBUG( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
427
428         /*Finalize Statement*/
429         rc = sqlite3_finalize(pStmt);
430         pStmt = NULL;
431
432         return max_seq;
433 }
434
435 static account_stmt _account_prepare_query(char *query)
436 {
437         int                     rc = -1;
438         account_stmt    pStmt = NULL;
439
440         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
441
442         ACCOUNT_DEBUG( "prepare query : %s", query);
443
444         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
445         ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
446
447         return pStmt;
448 }
449
450 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
451 {
452         assert(NULL != pStmt);
453         assert(pos > -1);
454         return sqlite3_bind_int(pStmt, pos, num);
455 }
456
457 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
458 {
459         assert(NULL != pStmt);
460
461         if(str)
462                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
463         else
464                 return sqlite3_bind_null(pStmt, pos);
465 }
466
467 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
468 {
469         ACCOUNT_DEBUG( "_account_convert_account_to_sql");
470         int count = 1;
471
472         /*Caution : Keep insert query orders.*/
473
474         /* 1. user name*/
475         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
476
477         /* 2. email address*/
478         _account_query_bind_text(hstmt, count++, (char*)account->email_address);
479
480         /* 3. display name*/
481         _account_query_bind_text(hstmt, count++, (char*)account->display_name);
482
483         /* 4. icon path*/
484         _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
485
486         /* 5. source*/
487         _account_query_bind_text(hstmt, count++, (char*)account->source);
488
489         /* 6. package name*/
490         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
491
492         /* 7. access token*/
493         _account_query_bind_text(hstmt, count++, (char*)account->access_token);
494
495         /* 8. domain name*/
496         _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
497
498         /* 9. auth type*/
499         _account_query_bind_int(hstmt, count++, account->auth_type);
500
501         /* 10. secret */
502         _account_query_bind_int(hstmt, count++, account->secret);
503
504         int i;
505
506         /* 11. user text*/
507         for(i=0; i< USER_TXT_CNT; i++)
508                 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
509
510         /* 12. user integer     */
511         for(i=0; i< USER_INT_CNT; i++)
512                 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
513
514         return count;
515 }
516
517
518 static void _account_query_finalize(account_stmt pStmt)
519 {
520         int rc = -1;
521
522         if (!pStmt) {
523                 ACCOUNT_DEBUG( "pStmt is NULL");
524                 return;
525         }
526
527         rc = sqlite3_finalize(pStmt);
528         if (rc != SQLITE_OK) {
529                 ACCOUNT_DEBUG( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
530         }
531
532         ACCOUNT_DEBUG( "sqlite3_finalize finish");
533 }
534
535
536 static int _account_query_step(account_stmt pStmt)
537 {
538         assert(NULL != pStmt);
539         return sqlite3_step(pStmt);
540 }
541
542
543 static int _account_execute_insert_query(account_s *account)
544 {
545         int                             rc = 0;
546         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
547         int                             error_code = ACCOUNT_ERROR_NONE;
548         account_stmt    hstmt = NULL;
549
550         if (!account->user_name && !account->display_name && !account->email_address) {
551                 ACCOUNT_DEBUG("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
552                 return ACCOUNT_ERROR_INVALID_PARAMETER;
553         }
554
555         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
556         ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
557                         "access_token , domain_name , auth_type , secret , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
558                         "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
559                         "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?)",     ACCOUNT_TABLE);
560
561         hstmt = _account_prepare_query(query);
562         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
563
564         _account_convert_account_to_sql(account, hstmt, query);
565
566         rc = _account_query_step(hstmt);
567         if (rc != SQLITE_DONE) {
568                 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
569                 error_code = ACCOUNT_ERROR_DB_FAILED;
570         }
571
572         _account_query_finalize(hstmt);
573         hstmt = NULL;
574
575         return error_code;
576 }
577
578 static int _account_insert_capability(account_s *account, int account_id)
579 {
580         int                     rc, count = 1;
581         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
582         account_stmt    hstmt = NULL;
583
584         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
585
586         if (g_slist_length( account->capablity_list)==0) {
587                 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
588                 return ACCOUNT_ERROR_NONE;
589         }
590
591         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
592
593         rc = _account_get_record_count(query);
594
595         if (rc <= 0) {
596                 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
597                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
598         }
599
600         /* insert query*/
601
602         GSList *iter;
603
604         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
605                 int rc, ret;
606                 count = 1;
607                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
608                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
609                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
610
611                 hstmt = _account_prepare_query(query);
612
613                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
614
615                 account_capability_s* cap_data = NULL;
616                 cap_data = (account_capability_s*)iter->data;
617                 ACCOUNT_DEBUG( "@@@@@@@@@@@@@cap_data->type = %d, cap_data->value = %d \n @@@@@@@@@@", cap_data->type, cap_data->value);
618
619                 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
620                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
621                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
622                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
623                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
624                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
625                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
626                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
627                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
628                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
629
630                 rc = _account_query_step(hstmt);
631
632                 if (rc != SQLITE_DONE) {
633                         ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
634                         break;
635                 }
636
637                 _account_query_finalize(hstmt);
638                 hstmt = NULL;
639
640         }
641
642         ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
643
644         return ACCOUNT_ERROR_NONE;
645 }
646
647
648 static int _account_update_capability(account_s *account, int account_id)
649 {
650         int                     rc, count = 1;
651         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
652         account_stmt    hstmt = NULL;
653
654         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
655
656         if (g_slist_length( account->capablity_list)==0) {
657                 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
658                 return ACCOUNT_ERROR_NONE;
659         }
660
661         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
662
663         rc = _account_get_record_count(query);
664
665         if (rc <= 0) {
666                 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
667                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
668         }
669
670         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
671
672         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
673         hstmt = _account_prepare_query(query);
674         count = 1;
675         _account_query_bind_int(hstmt, count++, (int)account_id);
676         _account_query_step(hstmt);
677         _account_query_finalize(hstmt);
678         hstmt = NULL;
679
680         GSList *iter;
681
682         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
683                 int rc, ret;
684                 count = 1;
685                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
686                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
687                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
688
689                 hstmt = _account_prepare_query(query);
690
691                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
692
693                 account_capability_s* cap_data = NULL;
694                 cap_data = (account_capability_s*)iter->data;
695
696                 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
697                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
698                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
699                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
700                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
701                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
702                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
703                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
704                 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
705                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
706
707                 rc = _account_query_step(hstmt);
708
709                 if (rc != SQLITE_DONE) {
710                         ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
711                         break;
712                 }
713
714                 _account_query_finalize(hstmt);
715                 hstmt = NULL;
716
717         }
718
719         ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
720
721         return ACCOUNT_ERROR_NONE;
722 }
723
724 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
725 {
726         int                     rc, count = 1;
727         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
728         account_stmt    hstmt = NULL;
729
730         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
731
732         if (g_slist_length( account->capablity_list)==0) {
733                 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
734                 return ACCOUNT_ERROR_NONE;
735         }
736
737         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
738
739         rc = _account_get_record_count(query);
740
741         if (rc <= 0) {
742                 ACCOUNT_DEBUG( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
743                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
744         }
745
746         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
747
748         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
749         hstmt = _account_prepare_query(query);
750         count = 1;
751         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
752         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
753         _account_query_step(hstmt);
754         _account_query_finalize(hstmt);
755         hstmt = NULL;
756
757         GSList* iter;
758
759         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
760                 int rc, ret;
761                 count = 1;
762                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
763                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
764                                 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
765
766                 hstmt = _account_prepare_query(query);
767
768                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
769
770                 account_capability_s* cap_data = NULL;
771                 cap_data = (account_capability_s*)iter->data;
772
773                 ret = _account_query_bind_int(hstmt, count++, cap_data->type);
774                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
775                 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
776                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
777                 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
778                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
779                 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
780                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
781                 /*ret = _account_query_bind_int(hstmt, count++, (int)account_id);
782                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));*/
783
784                 rc = _account_query_step(hstmt);
785
786                 if (rc != SQLITE_DONE) {
787                         ACCOUNT_DEBUG( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
788                         break;
789                 }
790
791                 _account_query_finalize(hstmt);
792                 hstmt = NULL;
793
794         }
795
796         ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
797
798         return ACCOUNT_ERROR_NONE;
799 }
800
801 static int _account_query_table_column_int(account_stmt pStmt, int pos)
802 {
803         assert(NULL != pStmt);
804         assert(pos > -1);
805         return sqlite3_column_int(pStmt, pos);
806 }
807
808 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
809 {
810         assert(NULL != pStmt);
811         assert(pos > -1);
812         return (char *)sqlite3_column_text(pStmt, pos);
813 }
814
815 static void _account_db_data_to_text(char *textbuf, char **output)
816 {
817         if (textbuf && strlen(textbuf)>0) {
818                 if (*output) {
819                         free(*output);
820                         *output = NULL;
821                 }
822                 *output = strdup(textbuf);
823         } else {
824                 ACCOUNT_DEBUG("_account_db_data_to_text : no text");
825         }
826 }
827
828 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
829 {
830         char    *textbuf = NULL;
831
832         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
833
834         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
835         _account_db_data_to_text(textbuf, &(account_record->user_name));
836
837         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
838         _account_db_data_to_text(textbuf, &(account_record->email_address));
839
840         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
841         _account_db_data_to_text(textbuf, &(account_record->display_name));
842
843         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
844         _account_db_data_to_text(textbuf, &(account_record->icon_path));
845
846         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
847         _account_db_data_to_text(textbuf, &(account_record->source));
848
849         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
850         _account_db_data_to_text(textbuf, &(account_record->package_name));
851
852         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
853         _account_db_data_to_text(textbuf, &(account_record->access_token));
854
855         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
856         _account_db_data_to_text(textbuf, &(account_record->domain_name));
857
858         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
859
860         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
861
862         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
863         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
864
865         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
866         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
867
868         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
869         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
870
871         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
872         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
873
874         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
875         _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
876
877         account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
878         account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
879         account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
880         account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
881         account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
882
883         ACCOUNT_DEBUG("END _account_convert_column_to_account");
884 }
885
886
887 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
888 {
889         char *textbuf = NULL;
890
891         capability_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
892
893         capability_record->type = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_KEY);
894
895         capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
896
897         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
898         _account_db_data_to_text(textbuf, &(capability_record->package_name));
899
900         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
901         _account_db_data_to_text(textbuf, &(capability_record->user_name));
902
903         capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
904
905         ACCOUNT_DEBUG("END _account_convert_column_to_capability");
906 }
907
908 bool _account_get_capability_text_cb(account_capability_type_e capability_type, account_capability_state_e capability_value, void *user_data)
909 {
910         account_s *data = (account_s*)user_data;
911
912         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
913
914         if (cap_data == NULL)
915                 return FALSE;
916         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
917
918         cap_data->type = capability_type;
919         cap_data->value = capability_value;
920
921         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
922
923         ACCOUNT_DEBUG("_account_get_capability_text_cb :: %d\n", capability_type);
924
925         return TRUE;
926 }
927
928 static char *_account_get_text(char *text_data)
929 {
930         char *text_value = NULL;
931
932         if (text_data != NULL) {
933                 text_value = strdup(text_data);
934                 ACCOUNT_DEBUG("text_value = %s", text_value);
935         }
936         return text_value;
937 }
938
939 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
940 {
941         int                             rc = 0, binding_count =0;
942         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
943         int                             error_code = ACCOUNT_ERROR_NONE;
944         account_stmt    hstmt = NULL;
945
946         ACCOUNT_RETURN_VAL((account->user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
947         ACCOUNT_RETURN_VAL((account->email_address != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("email_address is NULL.\n"));
948
949         if (!account->user_name && !account->display_name && !account->email_address) {
950                 ACCOUNT_DEBUG("One field should be set among user name, display name, email address\n");
951                 return ACCOUNT_ERROR_INVALID_PARAMETER;
952         }
953
954         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
955         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
956                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?,"
957                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
958                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
959
960         hstmt = _account_prepare_query(query);
961         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
962
963         binding_count = _account_convert_account_to_sql(account, hstmt, query);
964
965         _account_query_bind_text(hstmt, binding_count++, user_name);
966         _account_query_bind_text(hstmt, binding_count++, package_name);
967         ACCOUNT_DEBUG("_account_query_step : user_name = %s, package_name=%s\n", user_name, package_name);
968         rc = _account_query_step(hstmt);
969         ACCOUNT_DEBUG("_account_query_step return rc=%d\n", rc);
970         if (rc != SQLITE_DONE) {
971                 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
972         }
973
974         _account_query_finalize(hstmt);
975         hstmt = NULL;
976
977         /*update capability*/
978         _account_update_capability_by_user_name(account, user_name, package_name);
979
980         return error_code;
981 }
982
983 int account_insert_to_db(account_h account, int *account_id)
984 {
985         int ret = ACCOUNT_ERROR_NONE;
986         int             error_code = ACCOUNT_ERROR_NONE;
987
988         if(!g_hAccountDB) {
989                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
990         }
991
992         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
993
994         account_s *data = (account_s*)account;
995
996         pthread_mutex_lock(&account_mutex);
997
998         if (_account_check_duplicated(data)) {
999                 error_code = ACCOUNT_ERROR_DUPLICATED;
1000         } else {
1001                 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1002
1003                 error_code = _account_execute_insert_query(data);
1004
1005                 if (error_code != ACCOUNT_ERROR_NONE)
1006                         *account_id = -1;
1007         }
1008
1009         ACCOUNT_DEBUG( "_account_execute_insert_query, insert error_code : %d", error_code);
1010
1011         _account_insert_capability(data, *account_id);
1012
1013         pthread_mutex_unlock(&account_mutex);
1014
1015         return ACCOUNT_ERROR_NONE;
1016
1017 }
1018
1019 int account_create(account_h *account)
1020 {
1021         if (!account) {
1022                 ACCOUNT_DEBUG("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1023                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1024         }
1025
1026         account_s *data = (account_s*)malloc(sizeof(account_s));
1027
1028         if (data == NULL)
1029                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1030         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1031
1032         ACCOUNT_DEBUG("create handle=%p\n", *account);
1033
1034         *account = (account_h)data;
1035
1036         return ACCOUNT_ERROR_NONE;
1037 }
1038
1039 int account_set_user_name(account_h account, const char *user_name)
1040 {
1041         if (!account) {
1042                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1043                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1044         }
1045
1046         if (!user_name) {
1047                 ACCOUNT_DEBUG("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1048                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1049         }
1050
1051         account_s *data = (account_s*)account;
1052
1053         ACCOUNT_DEBUG("account=%p\n", account);
1054
1055         _ACCOUNT_FREE(data->user_name);
1056         data->user_name = strdup(user_name);
1057
1058         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1059
1060         return ACCOUNT_ERROR_NONE;
1061 }
1062
1063
1064 int account_set_display_name(account_h account, const char *display_name)
1065 {
1066         if (!account) {
1067                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1068                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1069         }
1070
1071         if (!display_name) {
1072                 ACCOUNT_DEBUG("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1073                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1074         }
1075
1076         account_s *data = (account_s*)account;
1077
1078         _ACCOUNT_FREE(data->display_name);
1079         data->display_name = strdup(display_name);
1080
1081         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1082
1083         return ACCOUNT_ERROR_NONE;
1084 }
1085
1086 int account_set_email_address(account_h account, const char *email_address)
1087 {
1088         if (!account) {
1089                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1090                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1091         }
1092
1093         if (!email_address) {
1094                 ACCOUNT_DEBUG("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1095                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1096         }
1097
1098         account_s *data = (account_s*)account;
1099
1100         _ACCOUNT_FREE(data->email_address);
1101         data->email_address = strdup(email_address);
1102
1103         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1104
1105         return ACCOUNT_ERROR_NONE;
1106 }
1107
1108 int account_set_icon_path(account_h account, const char *icon_path)
1109 {
1110         if (!account) {
1111                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1112                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1113         }
1114
1115         if (!icon_path) {
1116                 ACCOUNT_DEBUG("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1117                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1118         }
1119
1120         account_s *data = (account_s*)account;
1121
1122         _ACCOUNT_FREE(data->icon_path);
1123         data->icon_path = strdup(icon_path);
1124
1125         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1126
1127         return ACCOUNT_ERROR_NONE;
1128 }
1129
1130
1131 int account_set_source(account_h account, const char *source)
1132 {
1133         if (!account) {
1134                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1135                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1136         }
1137
1138         if (!source) {
1139                 ACCOUNT_DEBUG("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1140                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1141         }
1142         account_s *data = (account_s*)account;
1143
1144         _ACCOUNT_FREE(data->source);
1145         data->source = strdup(source);
1146
1147         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1148
1149         return ACCOUNT_ERROR_NONE;
1150 }
1151
1152 int account_set_package_name(account_h account, const char *package_name)
1153 {
1154         if (!account) {
1155                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1156                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1157         }
1158
1159         if (!package_name) {
1160                 ACCOUNT_DEBUG("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1161                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1162         }
1163
1164         account_s *data = (account_s*)account;
1165
1166         _ACCOUNT_FREE(data->package_name);
1167         data->package_name = strdup(package_name);
1168
1169         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1170
1171         return ACCOUNT_ERROR_NONE;
1172 }
1173
1174
1175 int account_set_domain_name(account_h account, const char *domain_name)
1176 {
1177         if (!account) {
1178                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1179                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1180         }
1181
1182         if (!domain_name) {
1183                 ACCOUNT_DEBUG("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1184                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1185         }
1186         account_s *data = (account_s*)account;
1187
1188         _ACCOUNT_FREE(data->domain_name);
1189         data->domain_name = strdup(domain_name);
1190
1191         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1192
1193         return ACCOUNT_ERROR_NONE;
1194 }
1195
1196
1197 int account_set_access_token(account_h account, const char *access_token)
1198 {
1199         if (!account) {
1200                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1201                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1202         }
1203
1204         if (!access_token) {
1205                 ACCOUNT_DEBUG("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1206                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1207         }
1208
1209         account_s *data = (account_s*)account;
1210
1211         _ACCOUNT_FREE(data->access_token);
1212         data->access_token = strdup(access_token);
1213
1214         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1215
1216         return ACCOUNT_ERROR_NONE;
1217 }
1218
1219 int account_set_user_text(account_h account, int index, const char *user_txt)
1220 {
1221         if (!account) {
1222                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1223                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1224         }
1225
1226         if (!user_txt) {
1227                 ACCOUNT_DEBUG("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1228                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1229         }
1230         if (index >= USER_TXT_CNT) {
1231                 ACCOUNT_DEBUG("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1232                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1233         }
1234
1235         account_s *data = (account_s*)account;
1236
1237         _ACCOUNT_FREE(data->user_data_txt[index]);
1238         data->user_data_txt[index] = strdup(user_txt);
1239
1240         ACCOUNT_DEBUG("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1241
1242         return ACCOUNT_ERROR_NONE;
1243 }
1244
1245 int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1246 {
1247         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
1248
1249         if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1250                 ACCOUNT_DEBUG("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1251                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1252         }
1253
1254         account_s *data = (account_s*)account;
1255
1256         data->auth_type = (int)auth_type;
1257
1258         return ACCOUNT_ERROR_NONE;
1259 }
1260
1261 int account_set_secret(account_h account, const account_secrecy_state_e secret)
1262 {
1263         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
1264
1265         if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1266                 ACCOUNT_DEBUG("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1267                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1268         }
1269
1270         account_s *data = (account_s*)account;
1271
1272         data->secret = (int)secret;
1273
1274         return ACCOUNT_ERROR_NONE;
1275 }
1276
1277 int account_set_user_int(account_h account, int index, const int user_int)
1278 {
1279         if (!account) {
1280                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1281                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1282         }
1283
1284         if (index >= USER_INT_CNT) {
1285                 ACCOUNT_DEBUG("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1286                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1287         }
1288
1289         account_s *data = (account_s*)account;
1290
1291         data->user_data_int[index] = user_int;
1292
1293         return ACCOUNT_ERROR_NONE;
1294 }
1295
1296
1297 int account_set_capability(account_h account, account_capability_type_e capability_type, account_capability_state_e capability_value)
1298 {
1299         if (!account) {
1300                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1301                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1302         }
1303
1304         if ( (capability_type < 0) || (capability_type > ACCOUNT_CAPABILITY_MOBILE_TRACKER)) {
1305                 ACCOUNT_DEBUG("(%s)-(%d) capability_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1306                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1307         }
1308
1309         if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1310                 ACCOUNT_DEBUG("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1311                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1312         }
1313
1314         account_s *data = (account_s*)account;
1315
1316         account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1317
1318         if (cap_data == NULL)
1319                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1320         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1321
1322         cap_data->type = capability_type;
1323         cap_data->value = capability_value;
1324         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1325
1326         return ACCOUNT_ERROR_NONE;
1327 }
1328
1329 int account_get_user_name(account_h account, char **user_name)
1330 {
1331         if (!account) {
1332                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1333                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1334         }
1335
1336         if (!user_name) {
1337                 ACCOUNT_DEBUG("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1338                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1339         }
1340
1341         account_s *data = (account_s*)account;
1342
1343         (*user_name) = NULL;
1344         *user_name = _account_get_text(data->user_name);
1345
1346         return ACCOUNT_ERROR_NONE;
1347 }
1348
1349 int account_get_display_name(account_h account, char **display_name)
1350 {
1351         if (!account) {
1352                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1353                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1354         }
1355
1356         if (!display_name) {
1357                 ACCOUNT_DEBUG("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
1358                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1359         }
1360
1361         account_s *data = (account_s*)account;
1362
1363         (*display_name) = NULL;
1364
1365         *display_name = _account_get_text(data->display_name);
1366
1367         return ACCOUNT_ERROR_NONE;
1368 }
1369
1370 int account_get_email_address(account_h account,char **email_address)
1371 {
1372         if (!account) {
1373                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1374                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1375         }
1376
1377         if (!email_address) {
1378                 ACCOUNT_DEBUG("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
1379                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1380         }
1381
1382         account_s *data = (account_s*)account;
1383
1384         (*email_address) = NULL;
1385
1386         *email_address = _account_get_text(data->email_address);
1387
1388         return ACCOUNT_ERROR_NONE;
1389 }
1390
1391 int  account_get_icon_path(account_h account, char **icon_path)
1392 {
1393         if (!account) {
1394                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1395                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1396         }
1397
1398         if (!icon_path) {
1399                 ACCOUNT_DEBUG("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
1400                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1401         }
1402
1403         account_s *data = (account_s*)account;
1404
1405         (*icon_path) = NULL;
1406
1407         *icon_path = _account_get_text(data->icon_path);
1408
1409         return ACCOUNT_ERROR_NONE;
1410 }
1411
1412 int account_get_source(account_h account, char **source)
1413 {
1414         if (!account) {
1415                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1416                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1417         }
1418
1419         if (!source) {
1420                 ACCOUNT_DEBUG("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1421                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1422         }
1423
1424         account_s *data = (account_s*)account;
1425
1426         (*source) = NULL;
1427
1428         *source = _account_get_text(data->source);
1429
1430         return ACCOUNT_ERROR_NONE;
1431 }
1432
1433 int account_get_package_name(account_h account, char **package_name)
1434 {
1435         if (!account) {
1436                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1437                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1438         }
1439
1440         if (!package_name) {
1441                 ACCOUNT_DEBUG("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
1442                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1443         }
1444
1445         account_s *data = (account_s*)account;
1446
1447         (*package_name) = NULL;
1448
1449         *package_name = _account_get_text(data->package_name);
1450
1451         return ACCOUNT_ERROR_NONE;
1452 }
1453
1454 int account_get_domain_name(account_h account, char **domain_name)
1455 {
1456         if (!account) {
1457                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1458                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1459         }
1460
1461         if (!domain_name) {
1462                 ACCOUNT_DEBUG("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
1463                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1464         }
1465
1466         account_s *data = (account_s*)account;
1467
1468         (*domain_name) = NULL;
1469
1470         *domain_name = _account_get_text(data->domain_name);
1471
1472         return ACCOUNT_ERROR_NONE;
1473 }
1474
1475 int account_get_access_token(account_h account, char **access_token)
1476 {
1477         if (!account) {
1478                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1479                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1480         }
1481
1482         if (!access_token) {
1483                 ACCOUNT_DEBUG("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
1484                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1485         }
1486
1487         account_s *data = (account_s*)account;
1488
1489         (*access_token) = NULL;
1490
1491         *access_token = _account_get_text(data->access_token);
1492
1493         return ACCOUNT_ERROR_NONE;
1494 }
1495
1496 int account_get_user_text(account_h account, int user_text_index, char **text)
1497 {
1498         if (!account) {
1499                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1500                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1501         }
1502
1503         if (!text) {
1504                 ACCOUNT_DEBUG("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
1505                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1506         }
1507         ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1508
1509         account_s *data = (account_s*)account;
1510
1511         (*text) = NULL;
1512
1513         *text = _account_get_text(data->user_data_txt[user_text_index]);
1514
1515         return ACCOUNT_ERROR_NONE;
1516 }
1517
1518 int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
1519 {
1520         if (!account) {
1521                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1522                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1523         }
1524         if (!auth_type) {
1525                 ACCOUNT_DEBUG("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
1526                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1527         }
1528
1529         account_s* data = (account_s*)account;
1530
1531         *auth_type = data->auth_type;
1532
1533         return ACCOUNT_ERROR_NONE;
1534 }
1535
1536 int account_get_secret(account_h account, account_secrecy_state_e *secret)
1537 {
1538         if (!account) {
1539                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1540                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1541         }
1542         if (!secret) {
1543                 ACCOUNT_DEBUG("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
1544                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1545         }
1546
1547         account_s* data = (account_s*)account;
1548
1549         *secret = data->secret;
1550
1551         return ACCOUNT_ERROR_NONE;
1552 }
1553
1554 int account_get_account_id(account_h account, int *account_id)
1555 {
1556         if (!account) {
1557                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1558                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1559         }
1560         if (!account_id) {
1561                 ACCOUNT_DEBUG("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
1562                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1563         }
1564
1565         account_s *data = (account_s*)account;
1566
1567         *account_id = data->id;
1568
1569         return ACCOUNT_ERROR_NONE;
1570 }
1571
1572 int account_get_user_int(account_h account, int user_int_index, int *integer)
1573 {
1574         if (!account) {
1575                 ACCOUNT_DEBUG("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1576                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1577         }
1578
1579         ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1580
1581         if (!integer) {
1582                 ACCOUNT_DEBUG("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
1583                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1584         }
1585
1586         account_s *data = (account_s*)account;
1587
1588         *integer = data->user_data_int[user_int_index];
1589
1590         return ACCOUNT_ERROR_NONE;
1591 }
1592
1593 int account_get_capability(account_h account, capability_cb cb_func, void *user_data)
1594 {
1595         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1596         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1597
1598         GSList *iter;
1599         account_s *data = (account_s*)account;
1600
1601         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1602                 account_capability_s *cap_data = NULL;
1603
1604                 cap_data = (account_capability_s*)iter->data;
1605
1606                 ACCOUNT_DEBUG("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1607
1608                 cb_func(cap_data->type, cap_data->value, user_data);
1609         }
1610
1611         return ACCOUNT_ERROR_NONE;
1612 }
1613
1614 int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
1615 {
1616         int                     error_code = ACCOUNT_ERROR_NONE;
1617         account_stmt    hstmt = NULL;
1618         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1619         int                     rc = 0;
1620
1621         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1622
1623         int ret = ACCOUNT_ERROR_NONE;
1624
1625         if (!g_hAccountDB) {
1626                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1627         }
1628
1629         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1630
1631         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1632         hstmt = _account_prepare_query(query);
1633
1634         rc = _account_query_step(hstmt);
1635         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1636
1637         account_capability_s* capability_record = NULL;
1638
1639         while (rc == SQLITE_ROW) {
1640                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1641
1642                 if (capability_record == NULL) {
1643                         ACCOUNT_DEBUG("malloc Failed");
1644                         break;
1645                 }
1646
1647                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1648
1649                 _account_convert_column_to_capability(hstmt, capability_record);
1650
1651                 cb_func(capability_record->type, capability_record->value, user_data);
1652
1653                 _account_free_capability_items(capability_record);
1654                 _ACCOUNT_FREE(capability_record);
1655
1656                 rc = _account_query_step(hstmt);
1657         }
1658
1659         _account_query_finalize(hstmt);
1660         hstmt = NULL;
1661
1662         error_code = ACCOUNT_ERROR_NONE;
1663
1664 CATCH:
1665         if (hstmt != NULL) {
1666                 _account_query_finalize(hstmt);
1667                 hstmt = NULL;
1668         }
1669
1670         pthread_mutex_unlock(&account_mutex);
1671         return error_code;
1672 }
1673
1674 static int _account_update_account(account_s *account, int account_id)
1675 {
1676         int                             rc = 0, binding_count =0;
1677         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1678         int                             error_code = ACCOUNT_ERROR_NONE;
1679         account_stmt    hstmt = NULL;
1680
1681         ACCOUNT_RETURN_VAL((account->user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1682         ACCOUNT_RETURN_VAL((account->email_address != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("email_address is NULL.\n"));
1683
1684         if (!account->user_name && !account->display_name && !account->email_address) {
1685                 ACCOUNT_DEBUG("One field should be set among user name, display name, email address\n");
1686                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1687         }
1688
1689         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1690         ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1691                         "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?,"
1692                         "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1693                         "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
1694
1695         hstmt = _account_prepare_query(query);
1696         ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1697
1698         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1699         _account_query_bind_int(hstmt, binding_count++, account_id);
1700
1701         rc = _account_query_step(hstmt);
1702         ACCOUNT_DEBUG("_account_query_step return rc=%d\n", rc);
1703         if (rc != SQLITE_DONE) {
1704                 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1705         }
1706
1707         _account_query_finalize(hstmt);
1708         hstmt = NULL;
1709
1710         /*update capability*/
1711         _account_update_capability(account, account_id);
1712
1713         return error_code;
1714 }
1715
1716 int account_update_to_db_by_id(const account_h account, int account_id)
1717 {
1718         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1719         ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
1720         int     error_code = ACCOUNT_ERROR_NONE;
1721         account_s* data = (account_s*)account;
1722
1723         int ret = ACCOUNT_ERROR_NONE;
1724
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));
1727         }
1728
1729         pthread_mutex_lock(&account_mutex);
1730
1731         error_code = _account_update_account(data, account_id);
1732
1733         pthread_mutex_unlock(&account_mutex);
1734
1735         return ACCOUNT_ERROR_NONE;
1736 }
1737
1738 int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
1739 {
1740         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
1741         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1742         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1743
1744         int     error_code = ACCOUNT_ERROR_NONE;
1745         account_s *data = (account_s*)account;
1746
1747         int ret = ACCOUNT_ERROR_NONE;
1748
1749         if (!g_hAccountDB) {
1750                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1751         }
1752         pthread_mutex_lock(&account_mutex);
1753
1754         error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
1755
1756         pthread_mutex_unlock(&account_mutex);
1757
1758         return ACCOUNT_ERROR_NONE;
1759 }
1760
1761 int account_foreach_account_from_db(account_cb callback, void *user_data)
1762 {
1763         int                     error_code = ACCOUNT_ERROR_NONE;
1764         account_stmt    hstmt = NULL;
1765         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1766         int                     rc = 0;
1767         GList                   *account_list = NULL;
1768
1769         ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
1770
1771         int ret = ACCOUNT_ERROR_NONE;
1772
1773         if (!g_hAccountDB) {
1774                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1775         }
1776
1777         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1778
1779         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
1780         hstmt = _account_prepare_query(query);
1781
1782         rc = _account_query_step(hstmt);
1783
1784         account_s *account_record = NULL;
1785
1786         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1787
1788         int k=0;
1789         while(rc == SQLITE_ROW) {
1790                 account_record = (account_s*) malloc(sizeof(account_s));
1791
1792                 if (account_record == NULL) {
1793                         ACCOUNT_DEBUG("malloc Failed");
1794                         break;
1795                 }
1796                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1797
1798                 _account_convert_column_to_account(hstmt, account_record);
1799
1800                 account_list = g_list_append(account_list, account_record);
1801
1802                 ACCOUNT_DEBUG("##### URUSA account_record(%d) = %p\n", k, account_record);
1803
1804                 rc = _account_query_step(hstmt);
1805                 k++;
1806         }
1807
1808         _account_query_finalize(hstmt);
1809         hstmt = NULL;
1810
1811         GList* iter;
1812         k = 0;
1813         for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
1814                 account_s *account = NULL;
1815                 account = (account_s*)iter->data;
1816                 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
1817                 callback((account_h)account, user_data);
1818                 k++;
1819         }
1820
1821         error_code = ACCOUNT_ERROR_NONE;
1822
1823 CATCH:
1824         if (hstmt != NULL) {
1825                 _account_query_finalize(hstmt);
1826                 hstmt = NULL;
1827         }
1828         if (account_list) {
1829                 _account_glist_free(account_list);
1830                 account_list = NULL;
1831         }
1832
1833         return error_code;
1834 }
1835
1836
1837 int account_query_account_by_account_id(int account_db_id, account_h *account)
1838 {
1839         int                             error_code = ACCOUNT_ERROR_NONE;
1840         account_stmt    hstmt = NULL;
1841         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1842         int                             rc = 0;
1843
1844         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1845
1846         int ret = ACCOUNT_ERROR_NONE;
1847
1848         if (!g_hAccountDB) {
1849                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1850         }
1851
1852         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1853
1854         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
1855         hstmt = _account_prepare_query(query);
1856
1857         rc = _account_query_step(hstmt);
1858         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1859
1860         account_s *account_record = (account_s *)(*account);
1861
1862         while (rc == SQLITE_ROW) {
1863                 _account_convert_column_to_account(hstmt, account_record);
1864                 ACCOUNT_DEBUG("get account info by id %p\n", account_record);
1865                 rc = _account_query_step(hstmt);
1866         }
1867
1868         _account_query_finalize(hstmt);
1869         account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
1870
1871         hstmt = NULL;
1872         error_code = ACCOUNT_ERROR_NONE;
1873
1874 CATCH:
1875         if (hstmt != NULL) {
1876                 _account_query_finalize(hstmt);
1877                 hstmt = NULL;
1878         }
1879
1880         pthread_mutex_unlock(&account_mutex);
1881         return error_code;
1882 }
1883
1884 int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
1885 {
1886         int                             error_code = ACCOUNT_ERROR_NONE;
1887         account_stmt    hstmt = NULL;
1888         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1889         int                             rc = 0;
1890
1891         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1892
1893
1894         int ret = ACCOUNT_ERROR_NONE;
1895
1896         if (!g_hAccountDB) {
1897                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
1898         }
1899
1900         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1901
1902         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
1903
1904         hstmt = _account_prepare_query(query);
1905
1906         int binding_count = 1;
1907         _account_query_bind_text(hstmt, binding_count++, user_name);
1908
1909         rc = _account_query_step(hstmt);
1910
1911         account_s *account_head = NULL;
1912
1913         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1914
1915         int tmp = 0;
1916
1917         account_head = (account_s*) malloc(sizeof(account_s));
1918         if (account_head == NULL) {
1919                 ACCOUNT_DEBUG("malloc Failed");
1920                 if (hstmt != NULL) {
1921                         _account_query_finalize(hstmt);
1922                         hstmt = NULL;
1923                 }
1924                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1925         }
1926         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1927
1928         while (rc == SQLITE_ROW) {
1929                 account_s* account_record = NULL;
1930
1931                 account_record = (account_s*) malloc(sizeof(account_s));
1932
1933                 if (account_record == NULL) {
1934                         ACCOUNT_DEBUG("malloc Failed");
1935                         break;
1936                 }
1937                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1938
1939                 _account_convert_column_to_account(hstmt, account_record);
1940
1941                 account_head->account_list = g_list_append(account_head->account_list, account_record);
1942
1943                 rc = _account_query_step(hstmt);
1944                 tmp++;
1945                 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
1946         }
1947
1948         _account_query_finalize(hstmt);
1949         hstmt = NULL;
1950
1951         GList *iter;
1952
1953
1954         tmp = g_list_length(account_head->account_list);
1955         ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
1956
1957         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1958                 account_h account;
1959                 account = (account_h)iter->data;
1960
1961                 account_s *testaccount = (account_s*)account;
1962
1963                 ACCOUNT_DEBUG("id = %d", testaccount->id);
1964                 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
1965                 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
1966                 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
1967                 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
1968                 ACCOUNT_DEBUG("source = %s", testaccount->source);
1969                 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
1970                 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
1971                 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
1972                 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
1973                 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
1974                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
1975                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
1976                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
1977                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
1978                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
1979                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
1980                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
1981                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
1982                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
1983                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
1984
1985                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
1986
1987                 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
1988
1989                 cb_func(account, user_data);
1990
1991         }
1992
1993         error_code = ACCOUNT_ERROR_NONE;
1994
1995 CATCH:
1996         if (hstmt != NULL) {
1997                 _account_query_finalize(hstmt);
1998                 hstmt = NULL;
1999         }
2000         if (account_head) {
2001                 if (account_head->account_list) {
2002                         _account_glist_free(account_head->account_list);
2003                         account_head->account_list = NULL;
2004                         _account_free_account_items(account_head);
2005                         _ACCOUNT_FREE(account_head);
2006                 }
2007         }
2008
2009         pthread_mutex_unlock(&account_mutex);
2010         return error_code;
2011 }
2012
2013 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)
2014 {
2015         int                     error_code = ACCOUNT_ERROR_NONE;
2016         account_stmt    hstmt = NULL;
2017         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2018         int                     rc = 0;
2019
2020         if ( (capability_type < 0) || (capability_type > ACCOUNT_CAPABILITY_MOBILE_TRACKER)) {
2021                 ACCOUNT_DEBUG("(%s)-(%d) capability_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
2022                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2023         }
2024
2025         if ((capability_value  < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2026                 ACCOUNT_DEBUG("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2027                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2028         }
2029
2030         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2031
2032         int ret = ACCOUNT_ERROR_NONE;
2033
2034         if (!g_hAccountDB) {
2035                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2036         }
2037
2038         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2039
2040         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2041
2042         hstmt = _account_prepare_query(query);
2043
2044         int binding_count = 1;
2045         _account_query_bind_int(hstmt, binding_count++, (int)capability_type);
2046         _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
2047
2048         rc = _account_query_step(hstmt);
2049
2050         account_s* account_head = NULL;
2051
2052         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2053
2054         int tmp = 0;
2055
2056         account_head = (account_s*) malloc(sizeof(account_s));
2057         if (account_head == NULL) {
2058                 ACCOUNT_DEBUG("malloc Failed");
2059                 if (hstmt != NULL) {
2060                         _account_query_finalize(hstmt);
2061                         hstmt = NULL;
2062                 }
2063                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2064         }
2065         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2066
2067         while (rc == SQLITE_ROW) {
2068                 account_s* account_record = NULL;
2069
2070                 account_record = (account_s*) malloc(sizeof(account_s));
2071
2072                 if (account_record == NULL) {
2073                         ACCOUNT_DEBUG("malloc Failed");
2074                         break;
2075                 }
2076                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2077
2078                 _account_convert_column_to_account(hstmt, account_record);
2079
2080                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2081
2082                 rc = _account_query_step(hstmt);
2083                 tmp++;
2084                 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2085         }
2086
2087         _account_query_finalize(hstmt);
2088         hstmt = NULL;
2089
2090         GList *iter;
2091
2092
2093         tmp = g_list_length(account_head->account_list);
2094         ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2095
2096         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2097                 account_h account = NULL;
2098                 account = (account_h)iter->data;
2099
2100
2101                 account_s* testaccount = (account_s*)account;
2102
2103                 ACCOUNT_DEBUG("id = %d", testaccount->id);
2104                 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
2105                 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
2106                 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
2107                 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
2108                 ACCOUNT_DEBUG("source = %s", testaccount->source);
2109                 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
2110                 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
2111                 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
2112                 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
2113                 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
2114                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
2115                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
2116                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
2117                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
2118                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
2119                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
2120                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
2121                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
2122                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
2123                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
2124
2125                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2126
2127                 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
2128
2129                 cb_func(account, user_data);
2130
2131         }
2132
2133
2134         error_code = ACCOUNT_ERROR_NONE;
2135
2136 CATCH:
2137         if (hstmt != NULL) {
2138                 _account_query_finalize(hstmt);
2139                 hstmt = NULL;
2140         }
2141         if (account_head) {
2142                 if (account_head->account_list) {
2143                         _account_glist_free(account_head->account_list);
2144                         account_head->account_list = NULL;
2145                         _account_free_account_items(account_head);
2146                         _ACCOUNT_FREE(account_head);
2147                 }
2148         }
2149
2150         pthread_mutex_unlock(&account_mutex);
2151         return error_code;
2152 }
2153
2154
2155 int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
2156 {
2157         int                     error_code = ACCOUNT_ERROR_NONE;
2158         account_stmt    hstmt = NULL;
2159         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2160         int                     rc = 0;
2161
2162         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2163         ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2164
2165         int ret = ACCOUNT_ERROR_NONE;
2166
2167         if (!g_hAccountDB) {
2168                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2169         }
2170
2171         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2172
2173         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
2174
2175         hstmt = _account_prepare_query(query);
2176
2177         int binding_count = 1;
2178         _account_query_bind_text(hstmt, binding_count++, package_name);
2179
2180         rc = _account_query_step(hstmt);
2181
2182         account_s* account_head = NULL;
2183
2184         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2185
2186         int tmp = 0;
2187
2188         account_head = (account_s*) malloc(sizeof(account_s));
2189         if (account_head == NULL) {
2190                 ACCOUNT_DEBUG("malloc Failed");
2191                 if (hstmt != NULL) {
2192                         _account_query_finalize(hstmt);
2193                         hstmt = NULL;
2194                 }
2195                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2196         }
2197         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2198
2199         while (rc == SQLITE_ROW) {
2200                 account_s* account_record = NULL;
2201
2202                 account_record = (account_s*) malloc(sizeof(account_s));
2203
2204                 if (account_record == NULL) {
2205                         ACCOUNT_DEBUG("malloc Failed");
2206                         break;
2207                 }
2208                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2209
2210                 _account_convert_column_to_account(hstmt, account_record);
2211
2212                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2213
2214                 rc = _account_query_step(hstmt);
2215                 tmp++;
2216                 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2217         }
2218
2219         _account_query_finalize(hstmt);
2220         hstmt = NULL;
2221
2222         GList *iter;
2223
2224
2225         tmp = g_list_length(account_head->account_list);
2226         ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2227
2228         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2229                 account_h account = NULL;
2230                 account = (account_h)iter->data;
2231
2232
2233                 account_s* testaccount = (account_s*)account;
2234
2235                 ACCOUNT_DEBUG("id = %d", testaccount->id);
2236                 ACCOUNT_DEBUG("user_name = %s", testaccount->user_name);
2237                 ACCOUNT_DEBUG("email_address = %s", testaccount->email_address);
2238                 ACCOUNT_DEBUG("display_name = %s", testaccount->display_name);
2239                 ACCOUNT_DEBUG("icon_path = %s", testaccount->icon_path);
2240                 ACCOUNT_DEBUG("source = %s", testaccount->source);
2241                 ACCOUNT_DEBUG("package_name = %s", testaccount->package_name);
2242                 ACCOUNT_DEBUG("access_token = %s", testaccount->access_token);
2243                 ACCOUNT_DEBUG("domain_name = %s", testaccount->domain_name);
2244                 ACCOUNT_DEBUG("auth_type = %d", testaccount->auth_type);
2245                 ACCOUNT_DEBUG("secret = %d", testaccount->secret);
2246                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[0]);
2247                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[1]);
2248                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[2]);
2249                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[3]);
2250                 ACCOUNT_DEBUG("user text = %s", testaccount->user_data_txt[4]);
2251                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[0]);
2252                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[1]);
2253                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[2]);
2254                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[3]);
2255                 ACCOUNT_DEBUG("user int = %d", testaccount->user_data_int[4]);
2256
2257                 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2258
2259                 ACCOUNT_DEBUG("capability_list address = %p", testaccount->capablity_list);
2260
2261                 cb_func(account, user_data);
2262
2263         }
2264
2265         error_code = ACCOUNT_ERROR_NONE;
2266
2267 CATCH:
2268         if (hstmt != NULL) {
2269                 _account_query_finalize(hstmt);
2270                 hstmt = NULL;
2271         }
2272         if (account_head) {
2273                 if (account_head->account_list) {
2274                         _account_glist_free(account_head->account_list);
2275                         account_head->account_list = NULL;
2276                         _account_free_account_items(account_head);
2277                         _ACCOUNT_FREE(account_head);
2278                 }
2279         }
2280
2281         pthread_mutex_unlock(&account_mutex);
2282         return error_code;
2283 }
2284
2285 int account_delete(int account_id)
2286 {
2287         int                             error_code = ACCOUNT_ERROR_NONE;
2288         account_stmt    hstmt = NULL;
2289         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2290         int                             rc = 0;
2291         int                             ret_transaction = 0, ret =0;
2292         bool                    is_success = FALSE;
2293
2294         if (!g_hAccountDB) {
2295                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2296         }
2297
2298         /* transaction control required*/
2299         ret_transaction = _account_begin_transaction();
2300
2301         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2302                 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2303                 pthread_mutex_unlock(&account_mutex);
2304                 return ret_transaction;
2305         }
2306
2307         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2308
2309         hstmt = _account_prepare_query(query);
2310         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2311                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2312
2313         rc = _account_query_step(hstmt);
2314         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2315
2316         _account_query_finalize(hstmt);
2317         hstmt = NULL;
2318
2319         ACCOUNT_MEMSET(query, 0, sizeof(query));
2320
2321         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2322
2323         hstmt = _account_prepare_query(query);
2324         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2325                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2326
2327         rc = _account_query_step(hstmt);
2328         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2329
2330         _account_query_finalize(hstmt);
2331         is_success = TRUE;
2332
2333         hstmt = NULL;
2334
2335 CATCH:
2336         if (hstmt != NULL) {
2337                 _account_query_finalize(hstmt);
2338                 hstmt = NULL;
2339         }
2340
2341         ret_transaction = _account_end_transaction(is_success);
2342
2343         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2344                 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2345         }
2346
2347         pthread_mutex_unlock(&account_mutex);
2348
2349         return error_code;
2350
2351 }
2352
2353 int account_delete_from_db_by_id(int account_db_id)
2354 {
2355         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
2356
2357         account_delete(account_db_id);
2358 }
2359
2360 int account_delete_from_db_by_user_name(char *user_name, char *package_name)
2361 {
2362         int                     error_code = ACCOUNT_ERROR_NONE;
2363         account_stmt    hstmt = NULL;
2364         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2365         int                     rc = 0;
2366         int                     ret_transaction = 0, ret =0;
2367         bool                    is_success = FALSE;
2368
2369         ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2370         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2371
2372         if (!g_hAccountDB) {
2373                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2374         }
2375
2376         /* transaction control required*/
2377         ret_transaction = _account_begin_transaction();
2378
2379         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2380                 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2381                 pthread_mutex_unlock(&account_mutex);
2382                 return ret_transaction;
2383         }
2384
2385         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2386
2387         hstmt = _account_prepare_query(query);
2388
2389         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2390                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2391
2392         int binding_count = 1;
2393         _account_query_bind_text(hstmt, binding_count++, user_name);
2394         _account_query_bind_text(hstmt, binding_count++, package_name);
2395
2396         rc = _account_query_step(hstmt);
2397         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2398
2399         _account_query_finalize(hstmt);
2400         hstmt = NULL;
2401
2402         ACCOUNT_MEMSET(query, 0, sizeof(query));
2403
2404         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2405
2406         hstmt = _account_prepare_query(query);
2407         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2408                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2409
2410         binding_count = 1;
2411         _account_query_bind_text(hstmt, binding_count++, user_name);
2412         _account_query_bind_text(hstmt, binding_count++, package_name);
2413
2414         rc = _account_query_step(hstmt);
2415         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));
2416
2417         _account_query_finalize(hstmt);
2418         is_success = TRUE;
2419
2420         hstmt = NULL;
2421
2422 CATCH:
2423         if (hstmt != NULL) {
2424                 _account_query_finalize(hstmt);
2425                 hstmt = NULL;
2426         }
2427
2428         ret_transaction = _account_end_transaction(is_success);
2429
2430         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2431                 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2432         }
2433
2434         pthread_mutex_unlock(&account_mutex);
2435
2436         return error_code;
2437 }
2438
2439 int account_delete_from_db_by_package_name(char *package_name)
2440 {
2441         int                     error_code = ACCOUNT_ERROR_NONE;
2442         account_stmt    hstmt = NULL;
2443         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2444         int                     rc = 0;
2445         int                     ret_transaction = 0, ret =0;
2446         bool                    is_success = FALSE;
2447
2448         ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2449
2450         if (!g_hAccountDB) {
2451                 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", ret));
2452         }
2453
2454         /* transaction control required*/
2455         ret_transaction = _account_begin_transaction();
2456
2457         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2458                 ACCOUNT_DEBUG("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2459                 pthread_mutex_unlock(&account_mutex);
2460                 return ret_transaction;
2461         }
2462
2463         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
2464
2465         hstmt = _account_prepare_query(query);
2466
2467         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2468                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2469
2470         int binding_count = 1;
2471         _account_query_bind_text(hstmt, binding_count++, package_name);
2472
2473         rc = _account_query_step(hstmt);
2474         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2475
2476         _account_query_finalize(hstmt);
2477         hstmt = NULL;
2478
2479         ACCOUNT_MEMSET(query, 0, sizeof(query));
2480
2481         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
2482
2483         hstmt = _account_prepare_query(query);
2484         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2485                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2486
2487         binding_count = 1;
2488         _account_query_bind_text(hstmt, binding_count++, package_name);
2489
2490         rc = _account_query_step(hstmt);
2491         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));
2492
2493         _account_query_finalize(hstmt);
2494         is_success = TRUE;
2495
2496         hstmt = NULL;
2497
2498 CATCH:
2499         if (hstmt != NULL) {
2500                 _account_query_finalize(hstmt);
2501                 hstmt = NULL;
2502         }
2503
2504         ret_transaction = _account_end_transaction(is_success);
2505
2506         if (ret_transaction != ACCOUNT_ERROR_NONE) {
2507                 ACCOUNT_DEBUG("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2508         }
2509
2510         pthread_mutex_unlock(&account_mutex);
2511
2512         return error_code;
2513 }
2514
2515 int account_get_total_count_from_db(int *count)
2516 {
2517         if (!count) {
2518                 ACCOUNT_DEBUG("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
2519                 return ACCOUNT_ERROR_INVALID_PARAMETER;
2520         }
2521
2522         char query[1024] = {0, };
2523         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2524         ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
2525
2526         *count = _account_get_record_count(query);
2527         int rc = -1;
2528         int ncount = 0;
2529         account_stmt pStmt = NULL;
2530
2531         assert(NULL != g_hAccountDB);
2532         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
2533
2534         rc = sqlite3_step(pStmt);
2535         if (SQLITE_ROW != rc) {
2536                 ACCOUNT_DEBUG("[ERROR] sqlite3_step() failed\n");
2537                 sqlite3_finalize(pStmt);
2538                 return ACCOUNT_ERROR_DB_FAILED;
2539         }
2540
2541         ncount = sqlite3_column_int(pStmt, 0);
2542
2543         *count = ncount;
2544
2545         ACCOUNT_DEBUG("Number of account : %d", ncount);
2546         sqlite3_finalize(pStmt);
2547
2548         if (ncount < 0) {
2549                 ACCOUNT_DEBUG("[ERROR] Number of account : %d, End", ncount);
2550                 return ACCOUNT_ERROR_DB_FAILED;
2551         }
2552
2553         return ACCOUNT_ERROR_NONE;
2554 }
2555
2556 int account_destroy(account_h account)
2557 {
2558         account_s *data = (account_s*)account;
2559
2560         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
2561
2562         ACCOUNT_DEBUG("destroy handle=%p\n", data);
2563
2564         _account_free_account_items(data);
2565         _ACCOUNT_FREE(data);
2566
2567         return ACCOUNT_ERROR_NONE;
2568 }
2569
2570