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