Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Account / AccountWrapper.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file        AccountWrapper.cpp
19  * @author      Jihwa Park (jh7979.park@samsung.com)
20  * @author      Sangtai Kim
21  * @version     0.1
22  */
23
24 #include <string.h>
25 #include <algorithm>
26 #include <pcrecpp.h>
27 #include <dpl/log/log.h>
28 #include <Commons/Exception.h>
29 #include "AccountWrapper.h"
30 //#include "AccountService.h"
31 #include "account.h"
32
33 #include "db-util.h"
34
35 using namespace TizenApis::Api::Account;
36 using namespace WrtDeviceApis::Commons;
37 //using namespace WrtDeviceApis::CommonsJavaScript;
38
39 // For Test
40
41 #define _QUERY_DB_NAME "/opt/dbspace/.test-accounts.db"
42 #define _QUERY_SQL_STRING_LEN           4096    //short sql string length
43 #define _USER_ACCOUNT_TABLE_NAME "user_account"
44 #define _SERVICES_TABLE_NAME "service"
45 #define _SERVICETYPE_TABLE_NAME "service_type"
46 #define _PROVIDER_LIST_TABLE_NAME "provider_list"
47 #define _TAGS_MIME_TABLE_NAME "tags_mime"
48 #define _RELATION_TABLE_NAME "relation"
49 #define _REL_PROVIDER_TABLE_NAME "provider_to_svctype"
50
51 #define QUERY_MAX_LEN 4096
52 #define SUB_QUERY_LEN 2048
53 #define TABLE_LEN 512
54
55 static sqlite3 *test_hDBCt;
56 typedef sqlite3_stmt* stmt;
57
58 #define __USER_ACCOUNT_SCHEMA  "create table %s \n"\
59 "(\n"\
60         "user_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
61         "displayname TEXT, "\
62         "icon TEXT, "\
63         "account_id TEXT, "\
64         "enable INTEGER DEFAULT 0, "\
65         "credential_id REAL, "\
66         "settings TEXT, "\
67         "provider_id TEXT, "\
68         "login_id TEXT, "\
69         "_list_id INTEGER "\
70 ");"
71
72 #define __SERVICE_SCHEMA  "create table %s \n"\
73 "(\n"\
74         "_svc_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
75         "service_id TEXT, "\
76         "service_name TEXT, "\
77         "application_id TEXT, "\
78         "display_name TEXT, "\
79         "icon TEXT, "\
80         "serviceType_id TEXT,  "\
81         "provider_id TEXT,  "\
82         "setting TEXT, "\
83         "enable INTEGER DEFAULT 0"\
84 ");"
85
86 #define __SERVICETYPE_SCHEMA  "create table %s \n"\
87 "(\n"\
88         "type_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
89         "serviceType_id TEXT, "\
90         "display_name TEXT, "\
91         "icon TEXT "\
92 ");"
93
94 #define __TAGS_MIMETYPE_SCHEMA  "create table %s \n"\
95 "(\n"\
96         "tags_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
97         "tag_name TEXT "\
98 ");"
99
100 #define __RELATION_SCHEMA  "create table %s \n"\
101 "(\n"\
102         "relation_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
103         "tags_id INTEGER, "\
104         "_svc_id INTEGER, "\
105         "type_id INTEGER, "\
106         "tag_name TEXT "\
107 ");"
108
109 #define __REL_PROVIDER_SCHEMA  "create table %s \n"\
110 "(\n"\
111         "_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
112         "_list_id INTEGER, "\
113         "type_id INTEGER "\
114 ");"
115
116 #define __PROVIDER_LIST_SCHEMA  "create table %s \n"\
117 "(\n"\
118         "_list_id INTEGER PRIMARY KEY AUTOINCREMENT, "\
119         "provider_id TEXT, "\
120         "display_name TEXT, "\
121         "icon TEXT "\
122 ");"
123
124 stmt __query_prepare(char *query) {
125         int rc = -1;
126         stmt pStmt = NULL;
127
128         printf("!! query : %s \n", query);
129
130         rc = sqlite3_prepare_v2(test_hDBCt, query, strlen(query), &pStmt, NULL);
131
132         if (SQLITE_OK != rc) {
133                 return NULL;
134         }
135
136         return pStmt;
137 }
138
139 int _query_exec(char *query) {
140         int rc = -1;
141         char* pszErrorMsg = NULL;
142
143         rc = sqlite3_exec(test_hDBCt, query, NULL, NULL, &pszErrorMsg);
144         if (SQLITE_OK != rc) {
145                 sqlite3_free(pszErrorMsg);
146         }
147
148         return rc;
149 }
150
151 int _query_column_int(stmt pStmt, int pos)
152 {
153    return sqlite3_column_int(pStmt, pos);
154 }
155
156 char* _query_column_text(stmt pStmt, int pos)
157 {
158    return (char *)sqlite3_column_text(pStmt, pos);
159 }
160
161 double _query_column_double(stmt pStmt, int pos)
162 {
163    return sqlite3_column_double(pStmt, pos);
164 }
165
166 int
167 _create_all_tables(void)
168 {
169         int rc = -1;
170         int error_code = 0;
171         char query[_QUERY_SQL_STRING_LEN + 1] = { 0, };
172
173         //Create test table
174         {
175                 memset(query, 0, sizeof(query));
176                 snprintf(query, sizeof(query) - 1, __USER_ACCOUNT_SCHEMA, _USER_ACCOUNT_TABLE_NAME);
177                 rc = _query_exec(query);
178
179                 memset(query, 0, sizeof(query));
180                 snprintf(query, sizeof(query) - 1, __SERVICE_SCHEMA, _SERVICES_TABLE_NAME);
181                 rc = _query_exec(query);
182
183                 memset(query, 0, sizeof(query));
184                 snprintf(query, sizeof(query) - 1, __SERVICETYPE_SCHEMA, _SERVICETYPE_TABLE_NAME);
185                 rc = _query_exec(query);
186
187                 memset(query, 0, sizeof(query));
188                 snprintf(query, sizeof(query) - 1, __TAGS_MIMETYPE_SCHEMA, _TAGS_MIME_TABLE_NAME);
189                 rc = _query_exec(query);
190
191                 memset(query, 0, sizeof(query));
192                 snprintf(query, sizeof(query) - 1, __RELATION_SCHEMA, _RELATION_TABLE_NAME);
193                 rc = _query_exec(query);
194
195                 memset(query, 0, sizeof(query));
196                 snprintf(query, sizeof(query) - 1, __REL_PROVIDER_SCHEMA, _REL_PROVIDER_TABLE_NAME);
197                 rc = _query_exec(query);
198
199                 memset(query, 0, sizeof(query));
200                 snprintf(query, sizeof(query) - 1, __PROVIDER_LIST_SCHEMA, _PROVIDER_LIST_TABLE_NAME);
201                 rc = _query_exec(query);
202
203         }
204
205         return error_code;
206 }
207
208 int _query_bind_text(stmt pStmt, int pos, char* str)
209 {
210         int len = 0;
211         if (str != NULL) {
212                 len = strlen(str);
213                 return sqlite3_bind_text(pStmt, pos, (const char*) str, len, SQLITE_STATIC);
214         }
215         return -1;
216 }
217
218 int _query_step(stmt pStmt) {
219         return sqlite3_step(pStmt);
220 }
221
222 void _query_finalize(stmt pStmt) {
223         int rc = -1;
224
225         if (!pStmt)
226                 return;
227
228         rc = sqlite3_finalize(pStmt);
229         if (rc != SQLITE_OK) {
230
231         }
232 }
233
234 int _query_bind_int(stmt pStmt, int pos, int num) {
235         return sqlite3_bind_int(pStmt, pos, num);
236 }
237 #if 1
238 void insert_dummy_data()
239 {
240         int rc = 0;
241         char query[_QUERY_SQL_STRING_LEN + 1] = {0,};
242         stmt hstmt = NULL;
243         int i = 0;
244
245         _create_all_tables();
246
247 // insert to tag_name to tag_mime table
248         memset(query, 0x00, sizeof(query));
249         sprintf(query, "insert into %s(tag_name) values (?) ", _TAGS_MIME_TABLE_NAME);
250
251         const char* tag_name[] =
252         {
253                 "call.voice",
254                 "call.video",
255                 "call.emergency",
256                 "call",
257                 "sync.contact",
258                 "sync.calendar",
259                 "messaging.email"
260         };
261
262         for(i = 0; i < 7; i++)
263         {
264                 hstmt = __query_prepare(query);
265
266                 _query_bind_text(hstmt, 1, (char*)tag_name[i]);
267                 rc = _query_step(hstmt);
268                 if(rc != SQLITE_DONE)
269                 {
270                         printf("\n !! rc : %d \n", rc);
271                 }
272
273                 _query_finalize(hstmt);
274                 hstmt = NULL;
275         }
276
277 //    rc = sqlite3_exec(test_hDBCt, "BEGIN IMMEDIATE TRANSACTION", NULL, NULL, &pszErrorMsg);
278
279 // insert to _USER_ACCOUNT_TABLE_NAME
280         memset(query, 0x00, sizeof(query));
281         sprintf(query, "insert into %s(displayname, icon, account_id, provider_id, login_id) values "
282                         "(?, ?, ?, ?, ?) ", _USER_ACCOUNT_TABLE_NAME);
283
284         const char* displayname[] =
285         {
286                 "Jamie's google account",
287                 "William's facebook account",
288                 "Craig's twitter account",
289                 "Lucille's account"
290         };
291
292         const char* icon[] =
293         {
294                 "/opt/icon/image1.jpg",
295                 "",
296                 "/opt/icon/image1.jpg",
297                 "/opt/icon/image2.jpg"
298         };
299
300         const char* account_id[] =
301         {
302                 "com.google:Jamie@gmail.com",
303                 "com.facebook:William@facebook.com",
304                 "com.twitter:Craig@twitter.com",
305                 "com.custom:Lucille@gmail.com"
306         };
307
308         const char* provider_id[] =
309         {
310                 "com.google",
311                 "com.facebook",
312                 "com.twitter",
313                 "com.custom"
314         };
315
316         const char* login_id[] =
317         {
318                 "Jamie@gmail.com",
319                 "William@facebook.com",
320                 "Craig@twitter.com",
321                 "Lucille@gmail.com"
322         };
323
324         for(i = 0; i < 4; i++)
325         {
326                 hstmt = __query_prepare(query);
327
328                 _query_bind_text(hstmt, 1, (char*)displayname[i]);
329                 _query_bind_text(hstmt, 2, (char*)icon[i]);
330                 _query_bind_text(hstmt, 3, (char*)account_id[i]);
331                 _query_bind_text(hstmt, 4, (char*)provider_id[i]);
332                 _query_bind_text(hstmt, 5, (char*)login_id[i]);
333
334                 rc = _query_step(hstmt);
335                 if(rc != SQLITE_DONE)
336                 {
337                         printf("\n !! rc : %d \n", rc);
338                 }
339
340                 _query_finalize(hstmt);
341                 hstmt = NULL;
342         }
343
344 // insert to _SERVICETYPE_TABLE_NAME
345         memset(query, 0x00, sizeof(query));
346         sprintf(query, "insert into %s(serviceType_id, display_name) values (?, ?) ", _SERVICETYPE_TABLE_NAME);
347
348         const char* service_type_id[] =
349         {
350                 "tizen.tel",
351                 "tizen.xmpp",
352                 "tizen.sip"
353         };
354
355         const char* service_type_display_name[] =
356         {
357                 "service_type.tel",
358                 "service_type.xmpp",
359                 "service_type.sip"
360         };
361
362         for(i = 0; i < 3; i++)
363         {
364                 hstmt = __query_prepare(query);
365                 _query_bind_text(hstmt, 1, (char*)service_type_id[i]);
366                 _query_bind_text(hstmt, 2, (char*)service_type_display_name[i]);
367
368                 rc = _query_step(hstmt);
369                 if(rc != SQLITE_DONE)
370                 {
371                         printf("\n !! rc : %d \n", rc);
372                 }
373
374                 _query_finalize(hstmt);
375                 hstmt = NULL;
376         }
377
378 // insert to _SERVICE_TABLE_NAME
379         memset(query, 0x00, sizeof(query));
380         sprintf(query, "insert into %s(service_id, provider_id, serviceType_id, service_name, display_name) values "
381                 "(?, ?, ?, ?, ?) ", _SERVICES_TABLE_NAME);
382
383         const char* service_id[] =
384         {
385                 "com.google.gmail:Jamie@gmail.com",
386                 "com.google.gtalk:Jamie@gmail.com",
387                 "com.google.picasa:Jamie@gmail.com",
388                 "com.facebook.facebook:William@facebook.com",
389                 "com.twitter.twitter:Craig@twitter.com",
390                 "com.custom.custom:Lucille@gmail.com",
391         };
392
393         const char* service_name[] =
394         {
395                 "com.google.gmail",
396                 "com.google.gtalk",
397                 "com.google.picasa",
398                 "com.facebook.facebook",
399                 "com.twitter.twitter",
400                 "com.custom.custom",
401         };
402
403         const char* service_display_name[] =
404         {
405                 "google's gmail",
406                 "google's gtalk",
407                 "google's picasa",
408                 "facebook's service",
409                 "twitter's service",
410                 "custom's new service",
411         };
412
413         for(i = 0; i < 6; i++)
414         {
415                 hstmt = __query_prepare(query);
416
417                 _query_bind_text(hstmt, 1, (char*)service_id[i]);
418
419                 if(i < 3)
420                 {
421                         _query_bind_text(hstmt, 2, (char*)provider_id[0]);
422                         _query_bind_text(hstmt, 3, (char*)service_type_id[i]);
423                 } else
424                 {
425                         _query_bind_text(hstmt, 2, (char*)provider_id[i-2]);
426                         _query_bind_text(hstmt, 3, (char*)service_type_id[2]);
427                 }
428
429                 _query_bind_text(hstmt, 4, (char*)service_name[i]);
430                 _query_bind_text(hstmt, 5, (char*)service_display_name[i]);
431
432                 rc = _query_step(hstmt);
433                 if(rc != SQLITE_DONE)
434                 {
435                         printf("\n !! rc : %d \n", rc);
436                 }
437
438                 _query_finalize(hstmt);
439                 hstmt = NULL;
440         }
441
442 // insert to _PROVIDER_LIST_TABLE_NAME
443         memset(query, 0x00, sizeof(query));
444         sprintf(query, "insert into %s(provider_id, display_name) values (?, ?) ", _PROVIDER_LIST_TABLE_NAME);
445
446         const char* provider_display_name[] =
447         {
448                 "provider:com.google",
449                 "provider:com.facebook",
450                 "provider:com.twitter",
451                 "provider:com.custom",
452         };
453
454         for(i = 0; i < 4; i++)
455         {
456                 hstmt = __query_prepare(query);
457
458                 _query_bind_text(hstmt, 1, (char*)provider_id[i]);
459                 _query_bind_text(hstmt, 2, (char*)provider_display_name[i]);
460
461                 rc = _query_step(hstmt);
462                 if(rc != SQLITE_DONE)
463                 {
464                         printf("\n !! rc : %d \n", rc);
465                 }
466
467                 _query_finalize(hstmt);
468                 hstmt = NULL;
469         }
470
471 // insert to _RELATION_TABLE_NAME
472         memset(query, 0x00, sizeof(query));
473         sprintf(query, "insert into %s(tags_id, _svc_id, type_id, tag_name) values (?, ?, ?, ?) ", _RELATION_TABLE_NAME);
474
475         hstmt = __query_prepare(query);
476
477         _query_bind_int(hstmt, 1, 2);
478         _query_bind_int(hstmt, 2, 1);
479         _query_bind_int(hstmt, 3, 0);
480         _query_bind_text(hstmt, 4, (char*)tag_name[1]);
481
482         rc = _query_step(hstmt);
483         if(rc != SQLITE_DONE)
484         printf("\n !! rc : %d \n", rc);
485         _query_finalize(hstmt);
486         hstmt = NULL;
487
488         hstmt = __query_prepare(query);
489         _query_bind_int(hstmt, 1, 5);
490         _query_bind_int(hstmt, 2, 2);
491         _query_bind_int(hstmt, 3, 0);
492         _query_bind_text(hstmt, 4, (char*)tag_name[4]);
493
494         rc = _query_step(hstmt);
495         if(rc != SQLITE_DONE)
496         printf("\n !! rc : %d \n", rc);
497         _query_finalize(hstmt);
498         hstmt = NULL;
499
500         hstmt = __query_prepare(query);
501         _query_bind_int(hstmt, 1, 7);
502         _query_bind_int(hstmt, 2, 3);
503         _query_bind_int(hstmt, 3, 0);
504         _query_bind_text(hstmt, 4, (char*)tag_name[6]);
505
506         rc = _query_step(hstmt);
507         if(rc != SQLITE_DONE)
508         printf("\n !! rc : %d \n", rc);
509         _query_finalize(hstmt);
510         hstmt = NULL;
511
512         hstmt = __query_prepare(query);
513
514         _query_bind_int(hstmt, 1, 7);
515         _query_bind_int(hstmt, 2, 4);
516         _query_bind_int(hstmt, 3, 0);
517         _query_bind_text(hstmt, 4, (char*)tag_name[6]);
518
519         rc = _query_step(hstmt);
520         if(rc != SQLITE_DONE)
521         printf("\n !! rc : %d \n", rc);
522         _query_finalize(hstmt);
523         hstmt = NULL;
524
525         hstmt = __query_prepare(query);
526
527         _query_bind_int(hstmt, 1, 7);
528         _query_bind_int(hstmt, 2, 5);
529         _query_bind_int(hstmt, 3, 0);
530         _query_bind_text(hstmt, 4, (char*)tag_name[6]);
531
532         rc = _query_step(hstmt);
533         if(rc != SQLITE_DONE)
534         printf("\n !! rc : %d \n", rc);
535         _query_finalize(hstmt);
536         hstmt = NULL;
537
538         hstmt = __query_prepare(query);
539
540         _query_bind_int(hstmt, 1, 7);
541         _query_bind_int(hstmt, 2, 6);
542         _query_bind_int(hstmt, 3, 0);
543         _query_bind_text(hstmt, 4, (char*)tag_name[6]);
544
545         rc = _query_step(hstmt);
546         if(rc != SQLITE_DONE)
547         printf("\n !! rc : %d \n", rc);
548         _query_finalize(hstmt);
549         hstmt = NULL;
550
551         hstmt = __query_prepare(query);
552
553         _query_bind_int(hstmt, 1, 1);
554         _query_bind_int(hstmt, 2, 0);
555         _query_bind_int(hstmt, 3, 1);
556         _query_bind_text(hstmt, 4, (char*)tag_name[0]);
557
558         rc = _query_step(hstmt);
559         if(rc != SQLITE_DONE)
560         printf("\n !! rc : %d \n", rc);
561         _query_finalize(hstmt);
562         hstmt = NULL;
563
564         hstmt = __query_prepare(query);
565
566         _query_bind_int(hstmt, 1, 3);
567         _query_bind_int(hstmt, 2, 0);
568         _query_bind_int(hstmt, 3, 1);
569         _query_bind_text(hstmt, 4, (char*)tag_name[2]);
570
571         rc = _query_step(hstmt);
572         if(rc != SQLITE_DONE)
573         printf("\n !! rc : %d \n", rc);
574         _query_finalize(hstmt);
575         hstmt = NULL;
576
577         hstmt = __query_prepare(query);
578
579         _query_bind_int(hstmt, 1, 4);
580         _query_bind_int(hstmt, 2, 0);
581         _query_bind_int(hstmt, 3, 1);
582         _query_bind_text(hstmt, 4, (char*)tag_name[3]);
583
584         rc = _query_step(hstmt);
585         if(rc != SQLITE_DONE)
586         printf("\n !! rc : %d \n", rc);
587         _query_finalize(hstmt);
588         hstmt = NULL;
589
590         hstmt = __query_prepare(query);
591
592         _query_bind_int(hstmt, 1, 1);
593         _query_bind_int(hstmt, 2, 0);
594         _query_bind_int(hstmt, 3, 2);
595         _query_bind_text(hstmt, 4, (char*)tag_name[0]);
596
597         rc = _query_step(hstmt);
598         if(rc != SQLITE_DONE)
599         printf("\n !! rc : %d \n", rc);
600         _query_finalize(hstmt);
601         hstmt = NULL;
602
603         hstmt = __query_prepare(query);
604
605         _query_bind_int(hstmt, 1, 4);
606         _query_bind_int(hstmt, 2, 0);
607         _query_bind_int(hstmt, 3, 2);
608         _query_bind_text(hstmt, 4, (char*)tag_name[3]);
609
610         rc = _query_step(hstmt);
611         if(rc != SQLITE_DONE)
612         printf("\n !! rc : %d \n", rc);
613         _query_finalize(hstmt);
614         hstmt = NULL;
615
616         hstmt = __query_prepare(query);
617
618         _query_bind_int(hstmt, 1, 5);
619         _query_bind_int(hstmt, 2, 0);
620         _query_bind_int(hstmt, 3, 3);
621         _query_bind_text(hstmt, 4, (char*)tag_name[4]);
622
623         rc = _query_step(hstmt);
624         if(rc != SQLITE_DONE)
625         printf("\n !! rc : %d \n", rc);
626         _query_finalize(hstmt);
627         hstmt = NULL;
628
629         hstmt = __query_prepare(query);
630
631         _query_bind_int(hstmt, 1, 6);
632         _query_bind_int(hstmt, 2, 0);
633         _query_bind_int(hstmt, 3, 3);
634         _query_bind_text(hstmt, 4, (char*)tag_name[5]);
635
636         rc = _query_step(hstmt);
637         if(rc != SQLITE_DONE)
638         printf("\n !! rc : %d \n", rc);
639         _query_finalize(hstmt);
640         hstmt = NULL;
641
642 // insert to _REL_PROVIDER_TABLE_NAME
643         memset(query, 0x00, sizeof(query));
644         sprintf(query, "insert into %s(_list_id, type_id) values (?, ?) ", _REL_PROVIDER_TABLE_NAME);
645
646         for(i = 0; i < 6; i++)
647         {
648                 hstmt = __query_prepare(query);
649
650                 if(i<3)
651                 {
652                         _query_bind_int(hstmt, 1, 1);
653                         _query_bind_int(hstmt, 2, i+1);
654                 } else
655                 {
656                         _query_bind_int(hstmt, 1, i-1);
657                         _query_bind_int(hstmt, 2, 3);
658                 }
659
660                 rc = _query_step(hstmt);
661                 if(rc != SQLITE_DONE)
662                 {
663                         printf("\n !! rc : %d \n", rc);
664                 }
665
666                 _query_finalize(hstmt);
667                 hstmt = NULL;
668         }
669 //    rc = sqlite3_exec(test_hDBCt, "COMMIT TRANSACTION", NULL, NULL, &pszErrorMsg);
670
671 }
672 #endif
673 typedef enum
674 {
675         SERVICE_TYPE_ID,
676         TAGS,
677         PROVIDER_ID,
678         FILTER_ATTRIBUTE_MAX
679 }filter_e;
680
681 typedef enum
682 {
683         ENABLE,
684         DISABLE,
685         ALL
686 }status_e;
687
688 typedef struct
689 {
690         char* tag_info;
691         bool enabled;
692 }tags_info_t;
693
694 typedef struct
695 {
696         char* AccountServiceTypeId;
697         char* displayName;
698         char* icon;
699         GList* tags_info; // tags_info_t
700 }AccountServiceType_t;
701
702 typedef struct
703 {
704         char* AccountServiceId;
705         char* serviceName;
706         char* applicationId;
707         char* displayName;
708         char* icon;
709         char* AccountId;
710         char* AccountServiceTypeId;
711         char* AccountServiceProviderId;
712         char* setting;
713         GList* tags_info; // tags_info_t
714         bool enabled;
715 }AccountService_t;
716
717 typedef struct
718 {
719         char* AccountServiceProviderId;
720         char* displayName;
721         char* icon;
722 }provider_t;
723
724 typedef struct
725 {
726         char* displayName;
727         char* icon;
728 }UserProperties_t;
729
730 UserAccount_t user_account = {0, };
731 UserProperties_t property = {0, };
732 AccountServiceType_t serviceType = {0, };
733 provider_t provider = {0, };
734
735 int check_provider_activate(char* providerId, int* activate, char** package_name)
736 {
737         int error_code = 0;
738         int rc = 0;
739         char query[QUERY_MAX_LEN + 1] = {0, };
740
741         snprintf(query, sizeof(query) - 1, "select package_name, activate from provider_list where provider_id = ? ");
742
743         stmt hstmt = __query_prepare(query);
744
745         _query_bind_text(hstmt, 1, providerId);
746
747         rc = _query_step(hstmt);
748         if(rc == SQLITE_ROW)
749         {
750                 *package_name = g_strdup(_query_column_text(hstmt, 0));
751                 *activate = _query_column_int(hstmt, 1);
752                 rc = _query_step(hstmt);
753         }
754
755         _query_finalize(hstmt);
756         return error_code;
757 }
758
759 int _add_platform_to_web()
760 {
761
762 // update provider_list table
763
764 // update user_account table
765         return 0;
766 }
767
768 int addAccount(char* providerId, UserProperties_t* properties, UserAccount_t* account)
769 {
770         int error_code = 0;
771         int rc = 0;
772         char query[QUERY_MAX_LEN + 1] = {0, };
773
774         snprintf(query, sizeof(query) - 1, "insert into user_account(display_name, icon, provider_id) value (?, ?, ?)");
775
776         stmt hstmt = __query_prepare(query);
777
778         _query_bind_text(hstmt, 1, properties->displayName);
779         _query_bind_text(hstmt, 2, properties->icon);
780         _query_bind_text(hstmt, 3, providerId);
781
782         rc = _query_step(hstmt);
783         if(rc != SQLITE_DONE)
784         {
785
786         }
787
788         _query_finalize(hstmt);
789
790         return error_code;
791 }
792
793 typedef struct {
794                 GList* filter_list;
795                 status_e status;
796                 int tags_count;
797 } search_list_t;
798
799 typedef struct {
800                 filter_e filter_type;
801                 char* value;
802 } filter_value_t;
803
804 int set_account_filter(search_list_t* search_list, filter_e filter_type, const char* value)
805 {
806         if(value == NULL || value[0] == '\0')
807         {
808                 printf("value is null \n");
809                 return 0;
810         }
811         printf("value : %s \n", value);
812
813         filter_value_t* object = NULL;
814         object = g_new0(filter_value_t, 1);
815
816         object->filter_type = filter_type;
817         object->value = g_strdup(value);
818         if (filter_type == TAGS)
819                 search_list->tags_count++;
820         search_list->filter_list = g_list_append(search_list->filter_list, object);
821
822         return 0;
823 }
824
825 const static char* filter_attribute[FILTER_ATTRIBUTE_MAX] =
826 {
827         "serviceType_id",
828         "tag_name",
829         "provider_id"
830 };
831
832 typedef enum {
833         USE_FILTER_NONE = 0x00000000, USE_SERVICE_TYPE_ID = 0x00000001, USE_TAGS = 0x00000002, USE_PROVIDER_ID = 0x00000004
834 } used_filter_attr;
835
836 typedef enum {
837         TAG_SERVICE, TAG_SERVICE_TYPE
838 } tags_owner;
839
840 typedef enum {
841         FIND_ACCOUNT, FIND_SERVICE
842 } query_type_e;
843
844 void _get_tags(tags_owner owner, int id, GList** tags_list)
845 {
846         int rc = 0;
847         char query[QUERY_MAX_LEN + 1] = { 0, };
848
849         switch (owner) {
850                 case TAG_SERVICE:
851                         snprintf(query, sizeof(query) - 1, "select tag_name from relation where _svc_id = %d", id);
852                         break;
853                 case TAG_SERVICE_TYPE:
854                         snprintf(query, sizeof(query) - 1, "select tag_name from relation where type_id = %d", id);
855                 default:
856                         break;
857         }
858
859         stmt hstmt = __query_prepare(query);
860
861         rc = _query_step(hstmt);
862         while (rc == SQLITE_ROW) {
863                 tags_info_t* tag_info = NULL;
864                 tag_info = g_new0(tags_info_t, 1);
865                 tag_info->tag_info = g_strdup(_query_column_text(hstmt, 0));
866
867                 *tags_list = g_list_append(*tags_list, tag_info);
868                 rc = _query_step(hstmt);
869         }
870         _query_finalize(hstmt);
871 }
872
873 char* _make_condition_part(search_list_t* search_list, query_type_e type) {
874         char* query = NULL;
875         char tmp_query[SUB_QUERY_LEN + 1] = { 0, };
876         char pre_query[TABLE_LEN + 1] = { 0, };
877         char table[TABLE_LEN + 1] = { 0, };
878         used_filter_attr use_filter = USE_FILTER_NONE;
879         int filter_count = 0;
880         int tags_count = 0;
881
882         query = g_new0(char, QUERY_MAX_LEN);
883
884         if (type == FIND_ACCOUNT)
885                 snprintf(pre_query, sizeof(pre_query) - 1, "where user_account.provider_id in (select a.provider_id from service a ");
886         else
887                 snprintf(pre_query, sizeof(pre_query) - 1, "where service._svc_id in (select a._svc_id from service a ");
888
889         GList* tmp_filter_list = search_list->filter_list;
890         while (tmp_filter_list) {
891                 filter_value_t* object = (filter_value_t*) tmp_filter_list->data;
892
893                 if (object->value != NULL) {
894                         if (object->filter_type == SERVICE_TYPE_ID) {
895                                 use_filter = (used_filter_attr) (use_filter | USE_SERVICE_TYPE_ID);
896
897                                 if (tmp_query[0] == '\0')
898                                         sprintf(tmp_query, "a.%s = ? ", filter_attribute[object->filter_type]);
899                                 else
900                                         sprintf(tmp_query, "and %s a.%s = ? ", tmp_query, filter_attribute[object->filter_type]);
901
902                         } else if (object->filter_type == TAGS) {
903                                 use_filter = (used_filter_attr) (use_filter | USE_TAGS);
904                                 tags_count++;
905
906                                 if (tags_count == 1) {
907                                         if (table[0] == '\0')
908                                                 sprintf(table, ", relation b ");
909                                         else
910                                                 sprintf(table, "%s, relation b ", table);
911
912                                         if (tmp_query[0] == '\0') {
913                                                 if (search_list->tags_count > 1)
914                                                         sprintf(tmp_query, "(a._svc_id = b._svc_id) and (b.%s = ? ", filter_attribute[object->filter_type]);
915                                                 else
916                                                         sprintf(tmp_query, "(a._svc_id = b._svc_id) and b.%s = ? ", filter_attribute[object->filter_type]);
917                                         } else {
918                                                 if (search_list->tags_count > 1)
919                                                         sprintf(tmp_query, "%s and (a._svc_id = b._svc_id) and (b.%s = ? ", tmp_query, filter_attribute[object->filter_type]);
920                                                 else
921                                                         sprintf(tmp_query, "%s and (a._svc_id = b._svc_id) and b.%s = ? ", tmp_query, filter_attribute[object->filter_type]);
922                                         }
923                                 } else {
924                                         sprintf(tmp_query, "%s or b.%s = ? ", tmp_query, filter_attribute[object->filter_type]);
925                                         if (search_list->tags_count > 1 && search_list->tags_count == tags_count)
926                                                 sprintf(tmp_query, "%s) ", tmp_query);
927                                 }
928                         } else if (object->filter_type == PROVIDER_ID) {
929                                 use_filter = (used_filter_attr) (use_filter | USE_PROVIDER_ID);
930
931                                 if (tmp_query[0] == '\0')
932                                         sprintf(tmp_query, "a.%s = ? ", filter_attribute[object->filter_type]);
933                                 else
934                                         sprintf(tmp_query, "%s and a.%s = ? ", tmp_query, filter_attribute[object->filter_type]);
935                         }
936                         filter_count++;
937                 }
938                 tmp_filter_list = g_list_next(tmp_filter_list);
939         }
940
941         if (table[0] != '\0')
942                 snprintf(query, QUERY_MAX_LEN, "%s %s where %s)", pre_query, table, tmp_query);
943         else
944                 snprintf(query, QUERY_MAX_LEN, "%s where %s)", pre_query, tmp_query);
945
946         if (type == FIND_ACCOUNT) {
947                 if ((use_filter | USE_PROVIDER_ID) == USE_PROVIDER_ID && filter_count == 1) {
948                         memset(query, 0x00, QUERY_MAX_LEN);
949                         snprintf(query, QUERY_MAX_LEN, "where user_account.provider_id = ? ");
950                 } else if ((use_filter | USE_SERVICE_TYPE_ID) == USE_SERVICE_TYPE_ID && filter_count == 1) {
951                         memset(query, 0x00, QUERY_MAX_LEN);
952                         snprintf(query, QUERY_MAX_LEN, ",service where (user_account.provider_id = service.provider_id) and service.serviceType_id = ? ");
953                 }
954         } else {
955                 if ((use_filter | USE_PROVIDER_ID) == USE_PROVIDER_ID && filter_count == 1) {
956                         memset(query, 0x00, QUERY_MAX_LEN);
957                         snprintf(query, QUERY_MAX_LEN, ",user_account where (user_account.provider_id = service.provider_id) and user_account.provider_id = ? ");
958                 }else if((use_filter | USE_SERVICE_TYPE_ID) == USE_SERVICE_TYPE_ID && filter_count == 1)
959                 {
960                         memset(query, 0x00, QUERY_MAX_LEN);
961                         snprintf(query, QUERY_MAX_LEN, " where service.serviceType_id = ? ");
962                 }
963         }
964
965         if (filter_count == 0)
966                 return NULL;
967         else
968                 return query;
969 }
970
971 void _get_service_list_by_account(char* provider_id, GList** service_list) {
972         int rc = 0;
973         int id = 0;
974         char query[QUERY_MAX_LEN + 1] = { 0, };
975
976         snprintf(query, sizeof(query) - 1, "select service_id, service_name, application_id, display_name, icon, serviceType_id, provider_id, setting, enable, _svc_id "
977                 "from %s where provider_id = ?", _SERVICES_TABLE_NAME);
978
979         stmt hstmt = __query_prepare(query);
980
981         _query_bind_text(hstmt, 1, provider_id);
982
983         rc = _query_step(hstmt);
984         while (rc == SQLITE_ROW) {
985                 AccountService_t* service_info = NULL;
986                 service_info = g_new0(AccountService_t, 1);
987
988                 service_info->AccountServiceId = g_strdup(_query_column_text(hstmt, 0));
989                 service_info->serviceName = g_strdup(_query_column_text(hstmt, 1));
990                 service_info->applicationId = g_strdup(_query_column_text(hstmt, 2));
991                 service_info->displayName = g_strdup(_query_column_text(hstmt, 3));
992                 service_info->icon = g_strdup(_query_column_text(hstmt, 4));
993                 service_info->AccountServiceTypeId = g_strdup(_query_column_text(hstmt, 5));
994                 service_info->AccountServiceProviderId = g_strdup(_query_column_text(hstmt, 6));
995                 service_info->setting = g_strdup(_query_column_text(hstmt, 7));
996                 service_info->enabled = _query_column_int(hstmt, 8);
997                 id = _query_column_int(hstmt, 9);
998
999                 _get_tags(TAG_SERVICE, id, &(service_info->tags_info));
1000
1001                 *service_list = g_list_append(*service_list, service_info);
1002                 rc = _query_step(hstmt);
1003         }
1004         _query_finalize(hstmt);
1005 }
1006
1007 void _make_account_info(int user_id, UserAccount_t* account_info) {
1008         char query[QUERY_MAX_LEN + 1] = { 0, };
1009         int rc = 0;
1010
1011 // Get user_account
1012         snprintf(query, sizeof(query) - 1, "select displayname, icon, account_id, enable, credential_id, settings, provider_id "
1013                         "from %s where user_id = %d", _USER_ACCOUNT_TABLE_NAME, user_id);
1014
1015         stmt hstmt = __query_prepare(query);
1016
1017         rc = _query_step(hstmt);
1018         account_info->displayName = g_strdup(_query_column_text(hstmt, 0));
1019         account_info->icon = g_strdup(_query_column_text(hstmt, 1));
1020         account_info->AccountId = g_strdup(_query_column_text(hstmt, 2));
1021         account_info->enabled = _query_column_int(hstmt, 3);
1022         account_info->credentialId = _query_column_double(hstmt, 4);
1023         account_info->settings = g_strdup(_query_column_text(hstmt, 5));
1024         account_info->AccountServiceProviderId = g_strdup(_query_column_text(hstmt, 6));
1025
1026         _query_finalize(hstmt);
1027         hstmt = NULL;
1028
1029 // Get services
1030         _get_service_list_by_account(account_info->AccountServiceProviderId, &(account_info->services));
1031 }
1032
1033 void _make_service_info(int id, AccountService_t* service_info) {
1034         char query[QUERY_MAX_LEN + 1] = { 0, };
1035         int rc = 0;
1036         stmt hstmt = NULL;
1037
1038 // Get service
1039         snprintf(query, sizeof(query) - 1, "select service_id, service_name, application_id, display_name, icon, serviceType_id, provider_id, setting, enable "
1040                         "from %s where _svc_id = %d", _SERVICES_TABLE_NAME, id);
1041
1042         hstmt = __query_prepare(query);
1043
1044         rc = _query_step(hstmt);
1045         service_info->AccountServiceId = g_strdup(_query_column_text(hstmt, 0));
1046         service_info->serviceName = g_strdup(_query_column_text(hstmt, 1));
1047         service_info->applicationId = g_strdup(_query_column_text(hstmt, 2));
1048         service_info->displayName = g_strdup(_query_column_text(hstmt, 3));
1049         service_info->icon = g_strdup(_query_column_text(hstmt, 4));
1050         service_info->AccountServiceTypeId = g_strdup(_query_column_text(hstmt, 5));
1051         service_info->AccountServiceProviderId = g_strdup(_query_column_text(hstmt, 6));
1052         service_info->setting = g_strdup(_query_column_text(hstmt, 7));
1053         service_info->enabled = _query_column_int(hstmt, 8);
1054
1055         _query_finalize(hstmt);
1056         hstmt = NULL;
1057
1058 // Get account_id
1059         snprintf(query, sizeof(query) - 1, "select account_id from %s where provider_id = %s", _USER_ACCOUNT_TABLE_NAME, service_info->AccountServiceProviderId);
1060
1061         hstmt = __query_prepare(query);
1062
1063         rc = _query_step(hstmt);
1064         service_info->AccountId = g_strdup(_query_column_text(hstmt, 0));
1065
1066         _query_finalize(hstmt);
1067         hstmt = NULL;
1068
1069 // Get tags
1070         _get_tags(TAG_SERVICE, id, &(service_info->tags_info));
1071
1072 }
1073
1074 int find_accounts(search_list_t* search_list, status_e status, GList** account_list) {
1075         int error_code = 0;
1076         char query[QUERY_MAX_LEN + 1] = { 0, };
1077         int i = 1;
1078         int index_list[SUB_QUERY_LEN + 1] = { 0, };
1079         int index_count = 0;
1080         int rc = 0;
1081         stmt hstmt = NULL;
1082
1083         char* filter = _make_condition_part(search_list, FIND_ACCOUNT);
1084
1085         switch(status)
1086         {
1087                 case ENABLE :
1088                 {
1089                         if(filter)
1090                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account %s and user_account.enable = 1", filter);
1091                         else
1092                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account where user_account.enable = 1");
1093                 }
1094                         break;
1095                 case DISABLE :
1096                 {
1097                         if(filter)
1098                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account %s and user_account.enable = 0", filter);
1099                         else
1100                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account where user_account.enable = 0");
1101                 }
1102                         break;
1103                 case ALL :
1104                 default :
1105                 {
1106                         if(filter)
1107                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account %s", filter);
1108                         else
1109                                 snprintf(query, sizeof(query) - 1, "select distinct(user_account.user_id) from user_account");
1110                 }
1111                         break;
1112         }
1113
1114         hstmt = __query_prepare(query);
1115
1116         GList* filter_list_val = search_list->filter_list;
1117         while (filter_list_val) {
1118                 filter_value_t* object = (filter_value_t*) filter_list_val->data;
1119
1120                 if (object->value != NULL)
1121                         _query_bind_text(hstmt, i++, object->value);
1122                 filter_list_val = g_list_next(filter_list_val);
1123         }
1124
1125         rc = _query_step(hstmt);
1126         int ret = 0;
1127         while (rc == SQLITE_ROW) {
1128                 ret = _query_column_int(hstmt, 0);
1129                 index_list[index_count++] = ret;
1130                 rc = _query_step(hstmt);
1131         }
1132
1133         _query_finalize(hstmt);
1134
1135 // make account object by index_list
1136         for (i = 0; i < index_count; i++) {
1137 // Get account info
1138                 UserAccount_t* account_info = NULL;
1139                 account_info = g_new0(UserAccount_t, 1);
1140                 _make_account_info(index_list[i], account_info);
1141
1142 // Generate account list
1143                 *account_list = g_list_append(*account_list, account_info);
1144         }
1145
1146         if (filter)
1147                 free(filter);
1148
1149         return error_code;
1150 }
1151
1152 int find_services(search_list_t* search_list, status_e status, GList** service_list) {
1153         int error_code = 0;
1154         char query[QUERY_MAX_LEN + 1] = { 0, };
1155         int i = 1;
1156         int index_list[SUB_QUERY_LEN + 1] = { 0, };
1157         int index_count = 0;
1158         int rc = 0;
1159
1160         char* filter = _make_condition_part(search_list, FIND_SERVICE);
1161
1162         switch(status)
1163         {
1164                 case ENABLE :
1165                 {
1166                         if(filter)
1167                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service %s and service.enable = 1", filter);
1168                         else
1169                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service where service.enable = 1");
1170                 }
1171                         break;
1172                 case DISABLE :
1173                 {
1174                         if(filter)
1175                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service %s and service.enable = 0", filter);
1176                         else
1177                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service where service.enable = 0");
1178                 }
1179                         break;
1180                 case ALL :
1181                 default :
1182                 {
1183                         if(filter)
1184                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service %s", filter);
1185                         else
1186                                 snprintf(query, sizeof(query) - 1, "select distinct(service._svc_id) from service");
1187                 }
1188                         break;
1189         }
1190
1191         stmt hstmt = __query_prepare(query);
1192
1193         GList* filter_list_val = search_list->filter_list;
1194         while (filter_list_val) {
1195                 filter_value_t* object = (filter_value_t*) filter_list_val->data;
1196
1197                 if (object->value != NULL)
1198                         _query_bind_text(hstmt, i++, object->value);
1199
1200                 filter_list_val = g_list_next(filter_list_val);
1201         }
1202
1203         rc = _query_step(hstmt);
1204         while (rc == SQLITE_ROW) {
1205                 index_list[index_count++] = _query_column_int(hstmt, 0);
1206                 rc = _query_step(hstmt);
1207         }
1208
1209         _query_finalize(hstmt);
1210
1211         for (i = 0; i < index_count; i++) {
1212 // Get service info
1213                 AccountService_t* service_info = NULL;
1214                 service_info = g_new0(AccountService_t, 1);
1215                 _make_service_info(index_list[i], service_info);
1216
1217 // Generate service list
1218                 *service_list = g_list_append(*service_list, service_info);
1219         }
1220
1221         if (filter)
1222                 free(filter);
1223
1224         return error_code;
1225 }
1226
1227 int find_providers(char* serviceTypeId, GList** provider_list)
1228 {
1229         int error_code = 0;
1230         int rc = 0;
1231         char query[QUERY_MAX_LEN + 1] = { 0, };
1232         char sub_query[SUB_QUERY_LEN + 1] = { 0, };
1233
1234         if (serviceTypeId == NULL) {
1235                 snprintf(query, sizeof(query) - 1, "select provider_id, display_name, icon from provider_list");
1236         } else {
1237                 snprintf(sub_query, sizeof(sub_query) - 1, "select type_id from service_type where serviceType_id = ?");
1238                 snprintf(query, sizeof(query) - 1, "select a.provider_id, a.display_name, a.icon from provider_list a, provider_to_svctype b "
1239                                 "where (a._list_id = b._list_id) and b.type_id in (%s) ", sub_query);
1240         }
1241
1242         stmt hstmt = __query_prepare(query);
1243
1244         if(serviceTypeId != NULL)
1245                 _query_bind_text(hstmt, 1, serviceTypeId);
1246
1247         rc = _query_step(hstmt);
1248         while (rc == SQLITE_ROW) {
1249                 provider_t* provider = NULL;
1250                 provider = g_new0(provider_t, 1);
1251                 provider->AccountServiceProviderId = g_strdup(_query_column_text(hstmt, 0));
1252                 provider->displayName = g_strdup(_query_column_text(hstmt, 1));
1253                 provider->icon = g_strdup(_query_column_text(hstmt, 2));
1254
1255                 *provider_list = g_list_append(*provider_list, provider);
1256                 rc = _query_step(hstmt);
1257         }
1258
1259         _query_finalize(hstmt);
1260
1261         return error_code;
1262 }
1263
1264 int find_service_types(char* prefix, GList** servicetype_list)
1265 {
1266         int error_code = 0;
1267         int rc = 0;
1268         int type_id = 0;
1269         char query[QUERY_MAX_LEN + 1] = { 0, };
1270
1271         if (prefix == NULL)
1272                 snprintf(query, sizeof(query) - 1, "select type_id, serviceType_id, display_name, icon from service_type");
1273         else
1274                 snprintf(query, sizeof(query) - 1, "select type_id, serviceType_id, display_name, icon from service_type where serviceType_id like (? || '%%')");
1275
1276         stmt hstmt = __query_prepare(query);
1277
1278         if (prefix != NULL)
1279                 _query_bind_text(hstmt, 1, prefix);
1280
1281         rc = _query_step(hstmt);
1282         while (rc == SQLITE_ROW) {
1283                 type_id = _query_column_int(hstmt, 0);
1284
1285                 AccountServiceType_t* servicetype = NULL;
1286                 servicetype = g_new0(AccountServiceType_t, 1);
1287                 servicetype->AccountServiceTypeId = g_strdup(_query_column_text(hstmt, 1));
1288                 servicetype->displayName = g_strdup(_query_column_text(hstmt, 2));
1289                 servicetype->icon = g_strdup(_query_column_text(hstmt, 3));
1290
1291                 _get_tags(TAG_SERVICE_TYPE, type_id, &(servicetype->tags_info));
1292
1293                 *servicetype_list = g_list_append(*servicetype_list, servicetype);
1294                 rc = _query_step(hstmt);
1295         }
1296         _query_finalize(hstmt);
1297
1298         return error_code;
1299 }
1300
1301 // use mandatory input parameter
1302 int getAccountById(char* accountId, UserAccount_t* account) {
1303         int error_code = 0;
1304         int rc = 0;
1305         int user_id = 0;
1306         char query[QUERY_MAX_LEN + 1] = { 0, };
1307
1308         snprintf(query, sizeof(query) - 1, "select user_id from user_account where account_id = ?");
1309
1310         stmt hstmt = __query_prepare(query);
1311         _query_bind_text(hstmt, 1, accountId);
1312
1313         rc = _query_step(hstmt);
1314
1315         user_id = _query_column_int(hstmt, 0);
1316         _query_finalize(hstmt);
1317
1318         _make_account_info(user_id, account);
1319
1320         return error_code;
1321 }
1322
1323 int getServiceTypeById(char* serviceTypeId, AccountServiceType_t* serviceType) {
1324         int error_code = 0;
1325         int rc = 0;
1326         int type_id = 0;
1327         char query[QUERY_MAX_LEN + 1] = {0, };
1328         printf("serviceTypeid : %s \n", serviceTypeId);
1329
1330         snprintf(query, sizeof(query) - 1, "select type_id, serviceType_id, display_name, icon from service_type where serviceType_id = ?");
1331
1332         stmt hstmt = __query_prepare(query);
1333         _query_bind_text(hstmt, 1, serviceTypeId);
1334
1335         rc = _query_step(hstmt);
1336         printf("rc : %d \n", rc);
1337
1338         type_id = _query_column_int(hstmt, 0);
1339
1340         serviceType->AccountServiceTypeId = g_strdup(_query_column_text(hstmt, 1));
1341         serviceType->displayName = g_strdup(_query_column_text(hstmt, 2));
1342         serviceType->icon = g_strdup(_query_column_text(hstmt, 3));
1343
1344         _get_tags(TAG_SERVICE_TYPE, type_id, &(serviceType->tags_info));
1345
1346         _query_finalize(hstmt);
1347
1348         return error_code;
1349 }
1350
1351 void free_single_value(char* value)
1352 {
1353         if(value)
1354         {
1355                 free(value);
1356                 value = NULL;
1357         }
1358 }
1359
1360 void _free_tags_list_info(gpointer data, gpointer user_data)
1361 {
1362         free(((tags_info_t*)data)->tag_info);
1363         free(data);
1364 }
1365
1366 void free_tags_list(GList* list)
1367 {
1368         if(list)
1369         {
1370 //              g_list_foreach(list, _free_tags_list_info, NULL);
1371 //              g_list_free(list);
1372         }
1373 }
1374
1375 void _free_service_list_info(gpointer data, gpointer user_data)
1376 {
1377         free(((AccountService_t*)data)->AccountServiceId);
1378         free(((AccountService_t*)data)->serviceName);
1379         free(((AccountService_t*)data)->applicationId);
1380         free(((AccountService_t*)data)->displayName);
1381         free(((AccountService_t*)data)->icon);
1382         free(((AccountService_t*)data)->AccountId);
1383         free(((AccountService_t*)data)->AccountServiceTypeId);
1384         free(((AccountService_t*)data)->AccountServiceProviderId);
1385         free(((AccountService_t*)data)->setting);
1386         free_tags_list(((AccountService_t*)data)->tags_info);
1387
1388         free(data);
1389 }
1390
1391 void free_service_list(GList* list)
1392 {
1393         if(list)
1394         {
1395 //              g_list_foreach(list, _free_service_list_info, NULL);
1396 //              g_list_free(list);
1397         }
1398 }
1399
1400 void _free_serviceType_list_info(gpointer data, gpointer user_data)
1401 {
1402         free(((AccountServiceType_t*)data)->AccountServiceTypeId);
1403         free(((AccountServiceType_t*)data)->displayName);
1404         free(((AccountServiceType_t*)data)->icon);
1405         free_tags_list(((AccountService_t*)data)->tags_info);
1406         free(data);
1407 }
1408
1409 void free_serviceType_list(GList* list)
1410 {
1411         if(list)
1412         {
1413 //              g_list_foreach(list, _free_serviceType_list_info, NULL);
1414 //              g_list_free(list);
1415         }
1416 }
1417
1418 void _free_provider_list_info(gpointer data, gpointer user_data)
1419 {
1420         free(((provider_t*)data)->AccountServiceProviderId);
1421         free(((provider_t*)data)->displayName);
1422         free(((provider_t*)data)->icon);
1423         free(data);
1424 }
1425
1426 void free_provider_list(GList* list)
1427 {
1428         if(list)
1429         {
1430 //              g_list_foreach(list, _free_provider_list_info, NULL);
1431 //              g_list_free(list);
1432         }
1433 }
1434
1435 int getProviderById(char* serviceProviderId, provider_t* provider)
1436 {
1437         int error_code = 0;
1438         int rc = 0;
1439         char query[QUERY_MAX_LEN + 1] = {0, };
1440
1441         snprintf(query, sizeof(query) - 1, "select provider_id, display_name, icon from provider_list where provider_id = ?");
1442
1443         stmt hstmt = __query_prepare(query);
1444         _query_bind_text(hstmt, 1, serviceProviderId);
1445
1446         rc = _query_step(hstmt);
1447
1448         provider->AccountServiceProviderId = g_strdup(_query_column_text(hstmt, 0));
1449         provider->displayName = g_strdup(_query_column_text(hstmt, 1));
1450         provider->icon = g_strdup(_query_column_text(hstmt, 2));
1451
1452         _query_finalize(hstmt);
1453
1454         return error_code;
1455 }
1456
1457 int getServiceById(char* serviceId, AccountService_t* service_info)
1458 {
1459         int error_code = 0;
1460         int rc = 0;
1461         char query[QUERY_MAX_LEN + 1] = {0, };
1462
1463         snprintf(query, sizeof(query) - 1, "select _svc_id where service_id = ?");
1464
1465         stmt hstmt = __query_prepare(query);
1466         _query_bind_text(hstmt, 1, serviceId);
1467         rc = _query_step(hstmt);
1468         int index = _query_column_int(hstmt, 0);
1469         _query_finalize(hstmt);
1470
1471         _make_service_info(index, service_info);
1472
1473         return error_code;
1474 }
1475
1476 int getServiceByName(char* serviceName, AccountService_t* service_info)
1477 {
1478         int error_code = 0;
1479         int rc = 0;
1480         char query[QUERY_MAX_LEN + 1] = {0, };
1481
1482         snprintf(query, sizeof(query) - 1, "select _svc_id where service_name = ?");
1483
1484         stmt hstmt = __query_prepare(query);
1485         _query_bind_text(hstmt, 1, serviceName);
1486
1487         rc = _query_step(hstmt);
1488         int index = _query_column_int(hstmt, 0);
1489         _query_finalize(hstmt);
1490
1491         _make_service_info(index, service_info);
1492
1493         return error_code;
1494 }
1495
1496 void test_find_accounts()
1497 {
1498         GList* account_list = NULL;
1499
1500         search_list_t search_list = {0, };
1501         set_account_filter(&search_list, SERVICE_TYPE_ID, "tizen.sms");
1502         find_accounts(&search_list, DISABLE, &account_list);
1503         while(account_list)
1504         {
1505                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1506             printf("account_info->AccountId : %s \n", account_info->AccountId);
1507             printf("account_info->AccountServiceProviderId : %s \n", account_info->AccountServiceProviderId);
1508                 account_list = g_list_next(account_list);
1509         }
1510         printf("\n");
1511
1512         search_list_t search_list1 = {0, };
1513         set_account_filter(&search_list1, TAGS, "tizen.sharing");
1514         find_accounts(&search_list1, DISABLE, &account_list);
1515         while(account_list)
1516         {
1517                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1518             printf("account_info->AccountId : %s \n", account_info->AccountId);
1519             printf("account_info->AccountServiceProviderId : %s \n", account_info->AccountServiceProviderId);
1520                 account_list = g_list_next(account_list);
1521         }
1522         printf("\n");
1523
1524         search_list_t search_list2 = {0, };
1525         set_account_filter(&search_list2, PROVIDER_ID, "com.google");
1526         find_accounts(&search_list2, DISABLE, &account_list);
1527         while(account_list)
1528         {
1529                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1530             printf("account_info->AccountId : %s \n", account_info->AccountId);
1531             printf("account_info->AccountServiceProviderId : %s \n", account_info->AccountServiceProviderId);
1532                 account_list = g_list_next(account_list);
1533         }
1534         printf("\n");
1535
1536         search_list_t search_list3 = {0, };
1537         set_account_filter(&search_list3, SERVICE_TYPE_ID, "tizen.sms");
1538         set_account_filter(&search_list3, TAGS, "tizen.sharing");
1539         set_account_filter(&search_list3, TAGS, "tizen.email");
1540         set_account_filter(&search_list3, PROVIDER_ID, "com.google");
1541         find_accounts(&search_list3, DISABLE, &account_list);
1542         while(account_list)
1543         {
1544                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1545             printf("account_info->AccountId : %s \n", account_info->AccountId);
1546             printf("account_info->AccountServiceProviderId : %s \n", account_info->AccountServiceProviderId);
1547                 account_list = g_list_next(account_list);
1548         }
1549         printf("\n");
1550
1551         search_list_t search_list4 = {0, };
1552         find_accounts(&search_list4, DISABLE, &account_list);
1553         while(account_list)
1554         {
1555                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1556             printf("account_info->AccountId : %s \n", account_info->AccountId);
1557             printf("account_info->AccountServiceProviderId : %s \n\n\n", account_info->AccountServiceProviderId);
1558                 account_list = g_list_next(account_list);
1559         }
1560         printf("\n");
1561
1562         search_list_t search_list5 = {0, };
1563         set_account_filter(&search_list5, TAGS, "tizen.sms");
1564         set_account_filter(&search_list5, TAGS, "tizen.mms");
1565         find_accounts(&search_list5, DISABLE, &account_list);
1566
1567         while(account_list)
1568         {
1569                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
1570             printf("account_info->AccountId : %s \n", account_info->AccountId);
1571             printf("account_info->AccountServiceProviderId : %s \n", account_info->AccountServiceProviderId);
1572                 account_list = g_list_next(account_list);
1573         }
1574 }
1575
1576 void test_find_service_types()
1577 {
1578         GList* servicetype_list = NULL;
1579
1580         find_service_types((char*)"tizen", &servicetype_list);
1581         while(servicetype_list)
1582         {
1583                 AccountServiceType_t* service_type_info = (AccountServiceType_t*)servicetype_list->data;
1584             printf("service_type_info->AccountServiceTypeId : %s \n", service_type_info->AccountServiceTypeId);
1585             printf("service_type_info->displayName : %s \n", service_type_info->displayName);
1586             printf("service_type_info->icon : %s \n", service_type_info->icon);
1587
1588                 GList* tmp_tag_list = service_type_info->tags_info;
1589
1590                 while(tmp_tag_list)
1591                 {
1592                         tags_info_t* tag = (tags_info_t*)tmp_tag_list->data;
1593                     printf("tag->tag_info : %s \n", tag->tag_info);
1594
1595                         tmp_tag_list = g_list_next(tmp_tag_list);
1596                 }
1597
1598                 servicetype_list = g_list_next(servicetype_list);
1599         }
1600 }
1601
1602 void test_find_services()
1603 {
1604         GList* service_list = NULL;
1605         search_list_t search_list = {0, };
1606         set_account_filter(&search_list, SERVICE_TYPE_ID, "tizen.tel");
1607         find_services(&search_list, DISABLE, &service_list);
1608         while(service_list)
1609         {
1610                 AccountService_t* service_info = (AccountService_t*)service_list->data;
1611             printf("service_info->AccountServiceId : %s \n", service_info->AccountServiceId);
1612             printf("service_info->AccountServiceProviderId : %s \n", service_info->AccountServiceProviderId);
1613                 service_list = g_list_next(service_list);
1614         }
1615         printf("\n");
1616         search_list_t search_list1 = {0, };
1617         set_account_filter(&search_list1, PROVIDER_ID, "com.google");
1618         find_services(&search_list1, DISABLE, &service_list);
1619         while(service_list)
1620         {
1621                 AccountService_t* service_info = (AccountService_t*)service_list->data;
1622             printf("service_info->AccountServiceId : %s \n", service_info->AccountServiceId);
1623             printf("service_info->AccountServiceProviderId : %s \n", service_info->AccountServiceProviderId);
1624                 service_list = g_list_next(service_list);
1625         }
1626 }
1627
1628 void test_find_providers()
1629 {
1630         GList* provider_list = NULL;
1631
1632         find_providers(NULL, &provider_list);
1633         while(provider_list)
1634         {
1635                 provider_t* provider_info = (provider_t*)provider_list->data;
1636             printf("provider_info->AccountId : %s \n", provider_info->AccountServiceProviderId);
1637                 provider_list = g_list_next(provider_list);
1638         }
1639         printf("\n");
1640
1641         find_providers((char*)"tizen.sms", &provider_list);
1642         while(provider_list)
1643         {
1644                 provider_t* provider_info = (provider_t*)provider_list->data;
1645             printf("provider_info->AccountId : %s \n", provider_info->AccountServiceProviderId);
1646                 provider_list = g_list_next(provider_list);
1647         }
1648 }
1649
1650 void test_get_account_by_id()
1651 {
1652         UserAccount_t account = {0, };
1653         getAccountById((char*)"com.facebook:William@facebook.com", &account);
1654
1655     printf("account->AccountId : %s \n", account.AccountId);
1656 }
1657
1658 void test_get_service_type_by_id()
1659 {
1660         AccountServiceType_t serviceType = {0, };
1661
1662         getServiceTypeById((char*)"tizen.tel", &serviceType);
1663     printf("serviceType->AccountServiceTypeId : %s \n", serviceType.AccountServiceTypeId);
1664 }
1665
1666 void test_get_provider_by_id()
1667 {
1668         provider_t provider = {0, };
1669
1670         getProviderById((char*)"com.google", &provider);
1671     printf("provider->AccountServiceProviderId : %s \n", provider.AccountServiceProviderId);
1672 }
1673
1674 void test_get_service_by_id()
1675 {
1676         AccountService_t service = {0, };
1677
1678         getServiceById((char*)"com.google.gtalk", &service);
1679     printf("service->AccountServiceProviderId : %s \n", service.AccountServiceId);
1680 }
1681
1682 void test_get_service_by_name()
1683 {
1684         AccountService_t service = {0, };
1685
1686         getServiceByName((char*)"com.google.gmail", &service);
1687     printf("service->AccountServiceProviderId : %s \n", service.AccountServiceId);
1688 }
1689
1690 void test_apis()
1691 {
1692         db_util_open(_QUERY_DB_NAME, &test_hDBCt, 0);
1693 //      insert_dummy_data();
1694         printf("\n -------------------------------------------- test_find_accounts --------------------------------------------\n");
1695         test_find_accounts();
1696
1697         printf("\n -------------------------------------------- test_find_services --------------------------------------------\n");
1698         test_find_services();
1699
1700         printf("\n -------------------------------------------- test_find_providers -------------------------------------------\n");
1701         test_find_providers();
1702
1703         printf("\n -------------------------------------------- test_get_account_by_id ----------------------------------------\n");
1704         test_get_account_by_id();
1705
1706         printf("\n -------------------------------------------- test_get_service_type_by_id -----------------------------------\n");
1707         test_get_service_type_by_id();
1708
1709         printf("\n -------------------------------------------- test_get_provider_by_id ---------------------------------------\n");
1710         test_get_provider_by_id();
1711
1712         printf("\n -------------------------------------------- test_find_service_types--------------------------------------- \n");
1713         test_find_service_types();
1714
1715         printf("\n -------------------------------------------- test_get_service_by_id--------------------------------------- \n");
1716         test_get_service_by_id();
1717
1718         printf("\n -------------------------------------------- test_get_service_by_name--------------------------------------- \n");
1719         test_get_service_by_name();
1720
1721 }
1722
1723 namespace TizenApis {
1724 namespace Platform {
1725 namespace Account{
1726 AccountWrapper::AccountWrapper() : m_platformAccount(NULL), m_abstractAccount(NULL)
1727 {
1728     LogDebug("entered");
1729     m_abstractAccount = EventAccountPtr(new EventAccount());
1730     if (!m_abstractAccount) {
1731         ThrowMsg(UnknownException, "abstract object is not created");
1732     }
1733 }
1734
1735 AccountWrapper::AccountWrapper(const EventAccountPtr &event) : m_platformAccount(NULL), m_abstractAccount(event)
1736 {
1737     LogDebug("entered");
1738 }
1739
1740 AccountWrapper::~AccountWrapper()
1741 {
1742     LogDebug("entered");
1743     //TODO: After inserting ,It seems like F/W frees handle. I need to review this later.
1744     // After inserting and call freePlatformAccount, Segment fault occurs.
1745     //freePlatformAccount();
1746 }
1747
1748 int AccountWrapper::getIDFromPlatformAccount() const
1749 {
1750     LogDebug("Entered");
1751     int error_code = -1;
1752         int accountid = 0;
1753     if (m_platformAccount == NULL) {
1754         ThrowMsg(NullPointerException, "m_platformAccount is not set");
1755     }
1756
1757         error_code = account_get_account_id(m_platformAccount, &accountid);
1758
1759         if(ACCOUNT_ERROR_NONE != error_code)
1760         {
1761                 ThrowMsg(PlatformException, "Can't get a account id");
1762         }
1763     return accountid;
1764 }
1765
1766 //TODO: check if it works to dicriminate update/insert with account id.
1767 void AccountWrapper::saveAccount()
1768 {
1769     LogDebug("entered");
1770
1771     int accountID = 0;
1772     //Check if platform struct is set.
1773     //It could be set here, but forcing user to do it manually can help to avoid problems.
1774     if (m_platformAccount == NULL) {
1775         ThrowMsg(NullPointerException, "m_platformAccount is not set");
1776     }
1777
1778     accountID = m_abstractAccount->getID();
1779
1780     displayPlatformAccount();
1781
1782     int returnID;
1783
1784     //insert new record or update existing one
1785     if (accountID < 0) { // insert new account
1786         int err = 0;
1787 //          err = addAccount(m_abstractAccount->getProviderName().c_str(), &property, &user_account);
1788                 user_account.AccountId = g_strdup("com.google:myaccount1@gmail.com");
1789
1790 //        int err = account_insert_to_db(m_platformAccount, &returnID);
1791         if (ACCOUNT_ERROR_NONE != err) {
1792             LogError("Can't insert new account, error code: " << returnID);
1793             ThrowMsg(PlatformException, "Can't insert new account.");
1794         }
1795         m_abstractAccount->setID(returnID);
1796                 m_abstractAccount->setAccountId(user_account.AccountId);
1797
1798         LogInfo("New event inserted");
1799     } else { //update
1800
1801            //accountID = getIDFromPlatformAccount();
1802           LogDebug("accountID: " << accountID);
1803         if (ACCOUNT_ERROR_NONE != account_update_to_db_by_id(m_platformAccount, accountID)) {
1804             ThrowMsg(PlatformException, "Can't update new account.");
1805         }
1806         LogDebug("Account updated");
1807     }
1808
1809                         }
1810
1811 void AccountWrapper::loadAccount(int id)
1812 {
1813         LogDebug("Entered. ID of account to load: " << id);
1814
1815         freePlatformAccount();
1816
1817         if (ACCOUNT_ERROR_NONE != account_create(&m_platformAccount)) {
1818                 ThrowMsg(PlatformException, "Can't create handle");
1819         }
1820
1821         int errorCode = account_query_account_by_account_id(id, &m_platformAccount);
1822
1823         if (ACCOUNT_ERROR_NONE != errorCode) {
1824                 LogError("Can't get account with ID = " << id << ", error code: " << errorCode);
1825                 ThrowMsg(PlatformException, "Can't get account with ID = " << id << ", error code: " << errorCode);
1826         }
1827         convertPlatformAccountToAbstractAccount();
1828         displayAbstractAccount();
1829 }
1830
1831 void AccountWrapper::deleteAccount()
1832 {
1833     if (m_platformAccount == NULL) {
1834         ThrowMsg(NullPointerException, "Failed to delete event (m_platformAccount==NULL)");
1835     }
1836     int accountID = m_abstractAccount->getID();
1837     LogDebug("accountID : " << accountID);
1838     if (accountID < 0) {
1839         ThrowMsg(InvalidArgumentException, "Failed to delete account from account DB (account is not saved in account DB)");
1840     }
1841
1842     int err = -1;
1843     err = account_delete_from_db_by_id(accountID);
1844     if (ACCOUNT_ERROR_NONE != err) {
1845         ThrowMsg(PlatformException, "Can't delete account. Error code " << err);
1846     }
1847     //TODO: Is it necessary?
1848     //m_abstractAccount->resetId();
1849     //setIDToPlatformAccount();
1850 }
1851
1852 void AccountWrapper::getAccountbyId()
1853 {
1854         account = g_new0(UserAccount_t, 1);
1855
1856 //      int errorCode = getAccountById((char*)m_abstractAccount->getAccountId().c_str(), account);
1857         getAccountById((char*)m_abstractAccount->getAccountId().c_str(), account);
1858 /*
1859         if (ACCOUNT_ERROR_NONE != errorCode) {
1860                 LogError("Can't get account with ID = " << id << ", error code: " << errorCode);
1861                 ThrowMsg(PlatformException, "Can't get account with ID = " << id << ", error code: " << errorCode);
1862         }
1863 */
1864         if(account->AccountId)
1865                 m_abstractAccount->setAccountId(account->AccountId);
1866
1867         if(account->displayName)
1868                 m_abstractAccount->setDisplayName(account->displayName);
1869
1870         if(account->icon)
1871                 m_abstractAccount->setIconPath(account->icon);
1872
1873         if(account->AccountServiceProviderId)
1874                 m_abstractAccount->setProviderName(account->AccountServiceProviderId);
1875
1876         m_abstractAccount->setEnabled(account->enabled);
1877         std::stringstream sstream;
1878         sstream << account->credentialId;
1879         m_abstractAccount->setCredentailId(sstream.str());
1880
1881         if(account->settings)
1882                 m_abstractAccount->setSettings(account->settings);
1883
1884         getAccountServices();
1885 //      convertPlatformAccountToAbstractAccount();
1886
1887         free_single_value(account->AccountId);
1888         free_single_value(account->displayName);
1889         free_single_value(account->icon);
1890         free_single_value(account->AccountServiceProviderId);
1891         free_single_value(account->settings);
1892         free(account);
1893
1894 }
1895
1896 AccountServiceProviderPropertyArrayPtr AccountWrapper::findProviders(std::string serviceTypeId)
1897 {
1898         LogDebug("<<<");
1899
1900         AccountServiceProviderPropertyArrayPtr serviceProviderPropertiesPtr(new AccountServiceProviderPropertyArray);
1901
1902         GList* provider_list = NULL;
1903         GList* tmp_list = NULL;
1904
1905         find_providers((char*)serviceTypeId.c_str(), &provider_list);
1906         tmp_list = provider_list;
1907
1908         while(tmp_list)
1909         {
1910                 provider_t* provider = (provider_t*)tmp_list->data;
1911
1912                 AccountServiceProviderPropertyPtr serviceProviderPropertyPtr(new AccountServiceProviderProperty());
1913
1914                 if(provider->AccountServiceProviderId)
1915                         serviceProviderPropertyPtr->setId(provider->AccountServiceProviderId);
1916
1917                 if(provider->displayName)
1918                         serviceProviderPropertyPtr->setDisplayName(provider->displayName);
1919
1920                 if(provider->icon)
1921                         serviceProviderPropertyPtr->setIconPath(provider->icon);
1922
1923                 serviceProviderPropertiesPtr->push_back(serviceProviderPropertyPtr);
1924
1925                 tmp_list = g_list_next(tmp_list);
1926         }
1927         free_provider_list(provider_list);
1928
1929         LogDebug(">>>");
1930         return serviceProviderPropertiesPtr;
1931 }
1932
1933 void AccountWrapper::setTags(std::vector<std::string> &tagsVector, GList* tags_list)
1934 {
1935         GList* tmp_tags_list = tags_list;
1936         while(tmp_tags_list)
1937         {
1938                 tags_info_t* tag = (tags_info_t*)tmp_tags_list->data;
1939                 tagsVector.push_back(tag->tag_info);
1940                 tmp_tags_list = g_list_next(tmp_tags_list);
1941         }
1942 }
1943
1944 AccountServiceTypePropertyArrayPtr AccountWrapper::findServiceTypes()
1945 {
1946         AccountServiceTypePropertyArrayPtr servicetypes(new AccountServiceTypePropertyArray());
1947
1948         GList* servicetype_list = NULL;
1949         find_service_types((char*)m_abstractAccount->getprefix().c_str(), &servicetype_list);
1950
1951         GList* tmp_list = servicetype_list;
1952         while(tmp_list)
1953         {
1954                 AccountServiceType_t* serviceType_info = (AccountServiceType_t*)tmp_list->data;
1955
1956                 AccountServiceTypePropertyPtr serviceType(new AccountServiceTypeProperty());
1957
1958                 if(serviceType_info->AccountServiceTypeId)
1959                         serviceType->setId(serviceType_info->AccountServiceTypeId);
1960
1961                 if(serviceType_info->displayName)
1962                         serviceType->setDisplayName(serviceType_info->displayName);
1963
1964                 if(serviceType_info->icon)
1965                         serviceType->setIconPath(serviceType_info->icon);
1966
1967 // add tags
1968                 std::vector<std::string> tagsVector;
1969
1970                 setTags(tagsVector, serviceType_info->tags_info);
1971                 if(serviceType_info->tags_info)
1972                         serviceType->setTags(tagsVector);
1973
1974                 servicetypes->push_back(serviceType);
1975                 tmp_list = g_list_next(tmp_list);
1976         }
1977         free_serviceType_list(servicetype_list);
1978
1979         return servicetypes;
1980 }
1981
1982 char *AccountWrapper::getPlatformAccount() const
1983 {
1984     LogDebug("entered");
1985     return (char*)m_platformAccount;
1986 }
1987
1988                         EventAccountPtr AccountWrapper::getAbstractAccount() const {
1989                                 LogDebug("entered");
1990                                 return m_abstractAccount;
1991                         }
1992
1993                         void AccountWrapper::freePlatformAccount() {
1994                                 LogDebug("entered");
1995                                 if (m_platformAccount != NULL) {
1996                                         if (ACCOUNT_ERROR_NONE != account_destroy(m_platformAccount)) {
1997                                                 LogError("Can't free account handle.");
1998                                         }
1999                                         m_platformAccount = NULL;
2000                                 }
2001                         }
2002
2003                         char *AccountWrapper::convertAbstractAccountToPlatformAccount() {
2004                                 LogDebug("entered");
2005                                 freePlatformAccount();
2006
2007                                 if (ACCOUNT_ERROR_NONE != account_create(&m_platformAccount)) {
2008                                         ThrowMsg(PlatformException, "Can't create handle");
2009                                 }
2010
2011                                 property.displayName = g_strdup(m_abstractAccount->getDisplayName().c_str());
2012                                 property.icon = g_strdup(m_abstractAccount->getIconPath().c_str());
2013
2014                                 setDisplayNameToPlatformAccount();
2015                                 setIconPathToPlatformAccount();
2016                                 setProviderNameToPlatformAccount();
2017                                 /*
2018                                  setUserNameToPlatformAccount();
2019                                  setPackageNameToPlatformAccount();
2020
2021                                  //TODO: check if we need to have  user be able to set  the ID.
2022                                  //setIDToPlatformAccount();
2023                                  setEmailAddressToPlatformAccount();
2024                                  setDomainNameToPlatformAccount();
2025
2026                                  setAccountServices();
2027                                  */
2028                                 return getPlatformAccount();
2029                         }
2030
2031                         void AccountWrapper::setIDToPlatformAccount() {
2032                                 LogDebug("entered");
2033
2034 //TODO: check if we need to have  user be able to set  the ID.
2035
2036 #if 0
2037     if (!m_platformAccount) {
2038         ThrowMsg(UnknownException, "Null platform pointer.");
2039     }
2040     if (m_abstractAccount->getIdIsSet()) {
2041         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
2042                                                        CAL_VALUE_INT_INDEX,
2043                                                        m_abstractEvent->getId()))
2044         {
2045             ThrowMsg(PlatformException, "Can't set event ID.");
2046         }
2047     } else {
2048         if (CAL_SUCCESS != calendar_svc_struct_set_int(m_platformEvent,
2049                                                        CAL_VALUE_INT_INDEX,
2050                                                        NEW_EVENT_ID)) {
2051             ThrowMsg(PlatformException, "Can't set event ID.");
2052         }
2053     }
2054 #endif
2055 }
2056
2057                         void AccountWrapper::setDisplayNameToPlatformAccount()
2058                         {
2059                            LogDebug("entered");
2060                                 if (!m_platformAccount) {
2061                                         ThrowMsg(UnknownException, "Null platform pointer.");
2062                                 }
2063
2064                                 if (ACCOUNT_ERROR_NONE != account_set_display_name(m_platformAccount, m_abstractAccount->getDisplayName().c_str()))
2065                                 {
2066                                         ThrowMsg(PlatformException, "Can't set displayname.");
2067                                 }
2068
2069                         }
2070
2071                         void AccountWrapper::setIconPathToPlatformAccount() {
2072                                 LogDebug("entered");
2073                                 if (!m_platformAccount) {
2074                                         ThrowMsg(UnknownException, "Null platform pointer.");
2075                                 }
2076
2077                                 if (ACCOUNT_ERROR_NONE != account_set_icon_path(m_platformAccount, m_abstractAccount->getIconPath().c_str())) {
2078                                         ThrowMsg(PlatformException, "Can't set iconPath.");
2079                                 }
2080
2081                         }
2082
2083                         void AccountWrapper::setProviderNameToPlatformAccount()
2084                         {
2085                            LogDebug("entered");
2086                                 if (!m_platformAccount) {
2087                                         ThrowMsg(UnknownException, "Null platform pointer.");
2088                                 }
2089
2090                                 if (ACCOUNT_ERROR_NONE != account_set_source(m_platformAccount, m_abstractAccount->getProviderName().c_str()))
2091                                 {
2092                                         ThrowMsg(PlatformException, "Can't set providername.");
2093                                 }
2094                         }
2095
2096                         void AccountWrapper::setEmailAddressToPlatformAccount() {
2097                                 LogDebug("entered");
2098                                 if (!m_platformAccount) {
2099                                         ThrowMsg(UnknownException, "Null platform pointer.");
2100                                 }
2101
2102                                 if (ACCOUNT_ERROR_NONE != account_set_email_address(m_platformAccount, m_abstractAccount->getEmailAddress().c_str())) {
2103                                         ThrowMsg(PlatformException, "Can't set packagename.");
2104                                 }
2105                         }
2106
2107                         void AccountWrapper::setDomainNameToPlatformAccount() {
2108                                 LogDebug("entered");
2109                                 if (!m_platformAccount) {
2110                                         ThrowMsg(UnknownException, "Null platform pointer.");
2111                                 }
2112
2113                                 if (ACCOUNT_ERROR_NONE != account_set_domain_name(m_platformAccount, m_abstractAccount->getDomainName().c_str())) {
2114                                         ThrowMsg(PlatformException, "Can't set packagename.");
2115                                 }
2116                         }
2117
2118                         /*
2119                          void AccountWrapper::setHasContactFeatureToPlatformAccount()
2120                          {
2121                          LogDebug("entered");
2122                          if (!m_platformAccount) {
2123                          ThrowMsg(UnknownException, "Null platform pointer.");
2124                          }
2125                          account_set_capability(m_platformAccount, ACCOUNT_CAPABILITY_CONTACT, ACCOUNT_CAPABILITY_ENABLED);
2126                          }
2127                          */
2128
2129                         void AccountWrapper::setAccountServices() {
2130                                 LogDebug("entered");
2131                                 if (!m_platformAccount) {
2132                                         ThrowMsg(UnknownException, "Null platform pointer.");
2133                                 }
2134
2135                                 AccountServicesArrayPtr services = m_abstractAccount->getService();
2136                                 if ((services == NULL) || (services->size() == 0))
2137                                         return;
2138
2139                                 //add new items
2140                                 for (size_t i = 0; i < services->size(); ++i) {
2141                                         LogDebug("getServiceName : " << services->at(i)->getName().c_str());
2142 //          LogDebug("getServiceType : " << services->at(i)->getServiceType().c_str());
2143                                 }
2144
2145                         }
2146
2147 void AccountWrapper::getAccountServices()
2148 {
2149     LogDebug("entered");
2150     if (!account) {
2151         ThrowMsg(UnknownException, "Null platform pointer.");
2152     }
2153
2154         AccountServicesArrayPtr services(new AccountServicesArray());
2155
2156         GList* tmp_service_list = account->services;
2157         while(tmp_service_list)
2158         {
2159                 AccountService_t* service_info = (AccountService_t*)tmp_service_list->data;
2160
2161                 AccountServicesPtr service(new AccountServices());
2162
2163                 if(service_info->AccountServiceId)
2164                         service->setId(service_info->AccountServiceId);
2165
2166                 if(service_info->serviceName)
2167                         service->setName(service_info->serviceName);
2168
2169                 if(service_info->applicationId)
2170                         service->setApplicationId(service_info->applicationId);
2171
2172                 if(service_info->displayName)
2173                         service->setDisplayName(service_info->displayName);
2174
2175                 if(service_info->icon)
2176                         service->setIcon(service_info->icon);
2177
2178                 if(service_info->AccountId)
2179                         service->setAccountId(service_info->AccountId);
2180
2181                 if(service_info->AccountServiceTypeId)
2182                         service->setServiceTypeId(service_info->AccountServiceTypeId);
2183
2184                 if(service_info->AccountServiceProviderId)
2185                         service->setProviderId(service_info->AccountServiceProviderId);
2186
2187                 if(service_info->setting)
2188                         service->setSettings(service_info->setting);
2189
2190 // add tags
2191                 std::vector<std::string> tagsVector;
2192                 setTags(tagsVector, service_info->tags_info);
2193                 if(service_info->tags_info)
2194                         service->setTags(tagsVector);
2195
2196                 services->push_back(service);
2197                 tmp_service_list = g_list_next(tmp_service_list);
2198
2199         }
2200
2201         m_abstractAccount->setService(services);
2202
2203         free_service_list(account->services);
2204 }
2205
2206 AccountServiceTypePropertyPtr AccountWrapper::getAccountServiceTypebyId()
2207 {
2208         std::string serviceTypeId = m_abstractAccount->getServiceTypeId();
2209         AccountServiceTypePropertyPtr accountServiceTypePropertyPtr(new AccountServiceTypeProperty);
2210
2211         db_util_open(_QUERY_DB_NAME, &test_hDBCt, 0);
2212
2213         AccountServiceType_t serviceType = {0, };
2214         getServiceTypeById((char*)serviceTypeId.c_str(), &serviceType);
2215
2216         if(serviceType.AccountServiceTypeId)
2217                 accountServiceTypePropertyPtr->setId(serviceType.AccountServiceTypeId);
2218
2219         if(serviceType.displayName)
2220                 accountServiceTypePropertyPtr->setDisplayName(serviceType.displayName);
2221
2222         if(serviceType.icon)
2223                 accountServiceTypePropertyPtr->setIconPath(serviceType.icon);
2224
2225 // add tags
2226         std::vector<std::string> tagsVector;
2227         setTags(tagsVector, serviceType.tags_info);
2228         if(serviceType.tags_info)
2229                 accountServiceTypePropertyPtr->setTags(tagsVector);
2230
2231         free_single_value(serviceType.AccountServiceTypeId);
2232         free_single_value(serviceType.displayName);
2233         free_single_value(serviceType.icon);
2234         free_tags_list(serviceType.tags_info);
2235
2236         return accountServiceTypePropertyPtr;
2237 }
2238
2239 AccountServiceProviderPropertyPtr AccountWrapper::getAccountServiceProviderProperty()
2240 {
2241         std::string accountServiceProviderId = m_abstractAccount->getProviderId();
2242
2243         provider_t provider = {0, };
2244         getProviderById((char*)accountServiceProviderId.c_str(), &provider);
2245
2246         AccountServiceProviderPropertyPtr accountServiceProviderPropertyPtr(new AccountServiceProviderProperty);
2247
2248         if(provider.AccountServiceProviderId)
2249                 accountServiceProviderPropertyPtr->setId(provider.AccountServiceProviderId);
2250
2251         if(provider.displayName)
2252                 accountServiceProviderPropertyPtr->setDisplayName(provider.displayName);
2253
2254         if(provider.icon)
2255                 accountServiceProviderPropertyPtr->setIconPath(provider.icon);
2256
2257         free_single_value(provider.AccountServiceProviderId);
2258         free_single_value(provider.displayName);
2259         free_single_value(provider.icon);
2260
2261         return accountServiceProviderPropertyPtr;
2262 }
2263
2264 void AccountWrapper::setDummyServices(){
2265         LogDebug("<<<");
2266
2267         AccountServicesArrayPtr services(new AccountServicesArray());
2268         for (int i = 0; i < 2; i++) {
2269                 AccountServicesPtr service(new AccountServices());
2270
2271                 service->setId("dummy_id");
2272                 service->setName("dummy_name");
2273                 service->setApplicationId("dummy_applicationId");
2274                 service->setDisplayName("dummy_displayName");
2275                 service->setIcon("dummy_Icon");
2276                 service->setAccountId("dummy_accountId");
2277                 service->setServiceTypeId("dummy_serviceTypeId");
2278                 service->setProviderId("dummy_ProviderId");
2279 //              service->setTags("dummy_");
2280                                         service->setSettings("dummy_settings");
2281
2282                                         services->push_back(service);
2283                                 }
2284
2285                                 m_abstractAccount->setService(services);
2286
2287                                 LogDebug(">>>");
2288                         }
2289
2290 EventAccountListPtr AccountWrapper::findAccountsByFilter(AccountServiceFilterPropertyPtr filterPropertyPtr)
2291 {
2292         std::string provider = filterPropertyPtr->getProvider();
2293         std::string serviceTypeId = filterPropertyPtr->getServiceTypeId();
2294         std::vector<std::string> tags = filterPropertyPtr->getTags();
2295
2296         EventAccountListPtr retAccountListPtr(new EventAccountList);
2297
2298         GList* account_list = NULL;
2299         search_list_t search_list = {0, };
2300         set_account_filter(&search_list, PROVIDER_ID, (char*)provider.c_str());
2301         set_account_filter(&search_list, SERVICE_TYPE_ID, (char*)serviceTypeId.c_str());
2302         for (unsigned int i=0; i<tags.size(); i++)
2303                 set_account_filter(&search_list, TAGS, (char*)tags[i].c_str());
2304
2305         find_accounts(&search_list, DISABLE, &account_list);
2306         while(account_list)
2307         {
2308                 UserAccount_t* account_info = (UserAccount_t*)account_list->data;
2309                 EventAccountPtr account_ptr(new EventAccount());
2310
2311                 if(account_info->AccountId)
2312                         account_ptr->setAccountId(account_info->AccountId);
2313
2314                 if(account_info->displayName)
2315                         account_ptr->setDisplayName(account_info->displayName);
2316
2317                 if(account_info->icon)
2318                         account_ptr->setIconPath(account_info->icon);
2319
2320                 if(account_info->AccountServiceProviderId)
2321                         account_ptr->setProviderName(account_info->AccountServiceProviderId);
2322
2323                 account_ptr->setEnabled(account_info->enabled);
2324                 std::stringstream sstream;
2325                 sstream << account_info->credentialId;
2326                 account_ptr->setCredentailId(sstream.str());
2327
2328                 if(account_info->settings)
2329                         account_ptr->setSettings(account_info->settings);
2330
2331                 AccountServicesArrayPtr services(new AccountServicesArray());
2332                 GList* tmp_service_list = account_info->services;
2333                 while(tmp_service_list)
2334                 {
2335                         AccountService_t* service_info = (AccountService_t*)tmp_service_list->data;
2336
2337                         AccountServicesPtr service(new AccountServices());
2338
2339                         if(service_info->AccountServiceId)
2340                                 service->setId(service_info->AccountServiceId);
2341
2342                         if(service_info->serviceName)
2343                                 service->setName(service_info->serviceName);
2344
2345                         if(service_info->applicationId)
2346                                 service->setApplicationId(service_info->applicationId);
2347
2348                         if(service_info->displayName)
2349                                 service->setDisplayName(service_info->displayName);
2350
2351                         if(service_info->icon)
2352                                 service->setIcon(service_info->icon);
2353
2354                         if(service_info->AccountId)
2355                                 service->setAccountId(service_info->AccountId);
2356
2357                         if(service_info->AccountServiceTypeId)
2358                                 service->setServiceTypeId(service_info->AccountServiceTypeId);
2359
2360                         if(service_info->AccountServiceProviderId)
2361                                 service->setProviderId(service_info->AccountServiceProviderId);
2362
2363                         if(service_info->setting)
2364                                 service->setSettings(service_info->setting);
2365
2366 // add tags
2367                         std::vector<std::string> tagsVector;
2368                         setTags(tagsVector, service_info->tags_info);
2369                         if(service_info->tags_info)
2370                                 service->setTags(tagsVector);
2371
2372                         services->push_back(service);
2373                         tmp_service_list = g_list_next(tmp_service_list);
2374
2375                 }
2376                 account_ptr->setService(services);
2377                 free_service_list(account_info->services);
2378
2379                 free_single_value(account_info->AccountId);
2380                 free_single_value(account_info->displayName);
2381                 free_single_value(account_info->icon);
2382                 free_single_value(account_info->AccountServiceProviderId);
2383                 free_single_value(account_info->settings);
2384                 free(account_info);
2385
2386                 retAccountListPtr->push_back(account_ptr);
2387                 account_list = g_list_next(account_list);
2388         }
2389
2390         return retAccountListPtr;
2391 }
2392
2393 AccountServicesArrayPtr AccountWrapper::findServiceByFilter(AccountServiceFilterPropertyPtr filterPropertyPtr)
2394 {
2395         std::string provider = filterPropertyPtr->getProvider();
2396         std::string serviceTypeId = filterPropertyPtr->getServiceTypeId();
2397         std::vector<std::string> tags = filterPropertyPtr->getTags();
2398
2399         AccountServicesArrayPtr retServiceListPtr(new AccountServicesArray);
2400
2401         GList* service_list = NULL;
2402         GList* tmp_service_list = NULL;
2403         search_list_t search_list = {0, };
2404         set_account_filter(&search_list, PROVIDER_ID, (char*)provider.c_str());
2405         set_account_filter(&search_list, SERVICE_TYPE_ID, (char*)serviceTypeId.c_str());
2406         for (unsigned int i=0; i<tags.size(); i++)
2407                 set_account_filter(&search_list, TAGS, (char*)tags[i].c_str());
2408
2409         find_services(&search_list, DISABLE, &service_list);
2410         tmp_service_list = service_list;
2411         while(tmp_service_list)
2412         {
2413                 AccountService_t* service_info = (AccountService_t*)tmp_service_list->data;
2414
2415                 AccountServicesPtr service(new AccountServices());
2416
2417                 if(service_info->AccountServiceId)
2418                         service->setId(service_info->AccountServiceId);
2419
2420                 if(service_info->serviceName)
2421                         service->setName(service_info->serviceName);
2422
2423                 if(service_info->applicationId)
2424                         service->setApplicationId(service_info->applicationId);
2425
2426                 if(service_info->displayName)
2427                         service->setDisplayName(service_info->displayName);
2428
2429                 if(service_info->icon)
2430                         service->setIcon(service_info->icon);
2431
2432                 if(service_info->AccountId)
2433                         service->setAccountId(service_info->AccountId);
2434
2435                 if(service_info->AccountServiceTypeId)
2436                         service->setServiceTypeId(service_info->AccountServiceTypeId);
2437
2438                 if(service_info->AccountServiceProviderId)
2439                         service->setProviderId(service_info->AccountServiceProviderId);
2440
2441                 if(service_info->setting)
2442                         service->setSettings(service_info->setting);
2443
2444 // add tags
2445                 std::vector<std::string> tagsVector;
2446                 setTags(tagsVector, service_info->tags_info);
2447                 if(service_info->tags_info)
2448                         service->setTags(tagsVector);
2449
2450                 retServiceListPtr->push_back(service);
2451                 tmp_service_list = g_list_next(tmp_service_list);
2452         }
2453
2454         free_service_list(service_list);
2455
2456         return retServiceListPtr;
2457 }
2458
2459 EventAccountPtr AccountWrapper::convertPlatformAccountToAbstractAccount()
2460 {
2461     LogDebug("entered");
2462     setDisplayNameFromPlatformAccount();
2463         setIconPathFromPlatformAccount();
2464     setProviderNameFromPlatformAccount();
2465
2466 /*
2467     setUserNameFromPlatformAccount();
2468     setPackageNameFromPlatformAccount();
2469     setIDFromPlatformAccount();
2470     setEmailAddressFromPlatformAccount();
2471     setDomainNameFromPlatformAccount();
2472
2473         getAccountServices();
2474 */
2475     displayPlatformAccount();
2476     return getAbstractAccount();
2477 }
2478
2479 EventAccountPtr AccountWrapper::convertPlatformAccountToAbstractAccount(account_h account_info)
2480 {
2481     LogDebug("entered");
2482         m_platformAccount = account_info;
2483     setDisplayNameFromPlatformAccount();
2484         setIconPathFromPlatformAccount();
2485     setProviderNameFromPlatformAccount();
2486 /*
2487     setUserNameFromPlatformAccount();
2488     setPackageNameFromPlatformAccount();
2489     setIDFromPlatformAccount();
2490     setEmailAddressFromPlatformAccount();
2491     setDomainNameFromPlatformAccount();
2492
2493         getAccountServices();
2494 */
2495     displayPlatformAccount();
2496     return getAbstractAccount();
2497 }
2498
2499 void AccountWrapper::setIDFromPlatformAccount()
2500 {
2501     LogDebug("entered");
2502     if (!m_platformAccount) {
2503         ThrowMsg(UnknownException, "Null platform pointer.");
2504     }
2505     m_abstractAccount->setID(getIDFromPlatformAccount());
2506 }
2507
2508 void AccountWrapper::setDisplayNameFromPlatformAccount()
2509 {
2510     LogDebug("entered");
2511     if (!m_platformAccount) {
2512         ThrowMsg(UnknownException, "Null platform pointer.");
2513     }
2514
2515     char *displayname = NULL;
2516         account_get_display_name(m_platformAccount, &displayname);
2517     if (displayname) {
2518         m_abstractAccount->setDisplayName(displayname);
2519     }
2520 }
2521
2522 void AccountWrapper::setIconPathFromPlatformAccount()
2523 {
2524    LogDebug("entered");
2525    if (!m_platformAccount) {
2526        ThrowMsg(UnknownException, "Null platform pointer.");
2527    }
2528
2529    char *iconPath = NULL;
2530         account_get_icon_path(m_platformAccount, &iconPath);
2531    if (iconPath) {
2532        m_abstractAccount->setIconPath(iconPath);
2533    }
2534 }
2535
2536                         void AccountWrapper::setProviderNameFromPlatformAccount() {
2537                                 LogDebug("entered");
2538                                 if (!m_platformAccount) {
2539                                         ThrowMsg(UnknownException, "Null platform pointer.");
2540                                 }
2541
2542                                 char *providername = NULL;
2543                                 account_get_source(m_platformAccount, &providername);
2544                                 if (providername) {
2545                                         m_abstractAccount->setProviderName(providername);
2546                                 }
2547                         }
2548
2549                         void AccountWrapper::setEmailAddressFromPlatformAccount() {
2550                                 LogDebug("entered");
2551                                 if (!m_platformAccount) {
2552                                         ThrowMsg(UnknownException, "Null platform pointer.");
2553                                 }
2554
2555                                 char *emailaddress = NULL;
2556                                 account_get_email_address(m_platformAccount, &emailaddress);
2557                                 if (emailaddress) {
2558                                         m_abstractAccount->setEmailAddress(emailaddress);
2559                                 }
2560                         }
2561
2562                         void AccountWrapper::setDomainNameFromPlatformAccount() {
2563                                 LogDebug("entered");
2564                                 if (!m_platformAccount) {
2565                                         ThrowMsg(UnknownException, "Null platform pointer.");
2566                                 }
2567
2568                                 char *domainname = NULL;
2569                                 account_get_domain_name(m_platformAccount, &domainname);
2570                                 if (domainname) {
2571                                         m_abstractAccount->setDomainName(domainname);
2572                                 }
2573                         }
2574
2575                         void AccountWrapper::displayAbstractAccount() {
2576                                 LogDebug("account id : " << m_abstractAccount->getID());
2577                                 LogDebug("Display name  : " << m_abstractAccount->getDisplayName());
2578                         }
2579
2580                         void AccountWrapper::displayPlatformAccount() {
2581
2582                         }
2583
2584                 }
2585         }
2586 }