Add new API for getting the mainappid using DBox Id.
[platform/framework/web/livebox-service.git] / src / livebox-service.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <stdio.h>
18 #include <errno.h>
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup, strerror */
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <sqlite3.h>
25 #include <X11/X.h>
26 #include <X11/Xlib.h>
27 #include <ctype.h>
28
29 #include <com-core_packet.h>
30 #include <packet.h>
31 #include <dlog.h>
32 #include <db-util.h>
33 #include <package-manager.h>
34 #include <pkgmgr-info.h>
35 #include <vconf.h>
36 #include <vconf-keys.h>
37 #include <ail.h>
38
39 #include "dlist.h"
40 #include "util.h"
41 #include "debug.h"
42 #include "livebox-service.h"
43 #include "livebox-errno.h"
44
45 #define SAMSUNG_PREFIX  "com.samsung."
46 #define EAPI __attribute__((visibility("default")))
47 #define DEFAULT_TIMEOUT 2.0
48 #define MAX_COLUMN 80
49
50 static struct supported_size_list {
51         int w;
52         int h;
53 } SIZE_LIST[NR_OF_SIZE_LIST] = {
54         { 175, 175 }, /*!< 1x1 */
55         { 354, 175 }, /*!< 2x1 */
56         { 354, 354 }, /*!< 2x2 */
57         { 712, 175 }, /*!< 4x1 */
58         { 712, 354 }, /*!< 4x2 */
59         { 712, 533 }, /*!< 4x3 */
60         { 712, 712 }, /*!< 4x4 */
61         { 712, 891 }, /*!< 4x5 */
62         { 712, 1070 }, /*!< 4x6 */
63         { 207, 207 }, /*!< 21x21 */
64         { 645, 207 }, /*!< 23x21 */
65         { 645, 645 }, /*!< 23x23 */
66         { 720, 1280 }, /*!< 0x0 */
67 };
68
69 static struct info {
70         sqlite3 *handle;
71         const char *dbfile;
72         const char *conf_file;
73         int init_count;
74         int res_resolved;
75 } s_info = {
76         .handle = NULL,
77         .dbfile = "/opt/dbspace/.livebox.db", 
78         .conf_file = "/usr/share/data-provider-master/resolution.ini",
79         .init_count = 0,
80         .res_resolved = 0,
81 };
82
83 static inline int update_info(int width_type, int height_type, int width, int height)
84 {
85         int idx;
86
87         if (width_type == 1 && height_type == 1) {
88                 DbgPrint("1x1 Updated to %dx%d\n", width, height);
89                 idx = 0;
90         } else if (width_type == 2 && height_type == 1) {
91                 DbgPrint("2x1 Updated to %dx%d\n", width, height);
92                 idx = 1;
93         } else if (width_type == 2 && height_type == 2) {
94                 DbgPrint("2x2 Updated to %dx%d\n", width, height);
95                 idx = 2;
96         } else if (width_type == 4 && height_type == 1) {
97                 DbgPrint("4x1 Updated to %dx%d\n", width, height);
98                 idx = 3;
99         } else if (width_type == 4 && height_type == 2) {
100                 DbgPrint("4x2 Updated to %dx%d\n", width, height);
101                 idx = 4;
102         } else if (width_type == 4 && height_type == 3) {
103                 DbgPrint("4x3 Updated to %dx%d\n", width, height);
104                 idx = 5;
105         } else if (width_type == 4 && height_type == 4) {
106                 DbgPrint("4x4 Updated to %dx%d\n", width, height);
107                 idx = 6;
108         } else if (width_type == 4 && height_type == 5) {
109                 DbgPrint("4x5 Updated to %dx%d\n", width, height);
110                 idx = 7;
111         } else if (width_type == 4 && height_type == 6) {
112                 DbgPrint("4x6 Updated to %dx%d\n", width, height);
113                 idx = 8;
114         } else if (width_type == 21 && height_type == 21) {
115                 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
116                 idx = 9;
117         } else if (width_type == 23 && height_type == 21) {
118                 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
119                 idx = 10;
120         } else if (width_type == 23 && height_type == 23) {
121                 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
122                 idx = 11;
123         } else if (width_type == 0 && height_type == 0) {
124                 DbgPrint("Special 0x0 Updated to %dx%d\n", width, height);
125                 idx = 12;
126         } else {
127                 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
128                 return 0;
129         }
130
131         SIZE_LIST[idx].w = width;
132         SIZE_LIST[idx].h = height;
133         return 1;
134 }
135
136 static inline int update_from_file(void)
137 {
138         FILE *fp;
139         int updated;
140         int width_type;
141         int height_type;
142         int width;
143         int height;
144         char buffer[MAX_COLUMN];
145         int ch;
146         int idx;
147         enum status {
148                 START = 0x0,
149                 TYPE = 0x01,
150                 SIZE = 0x02,
151                 COMMENT = 0x03,
152                 ERROR = 0x04,
153                 EOL = 0x05,
154                 TYPE_END = 0x06,
155                 SIZE_START = 0x07,
156         } status;
157
158         fp = fopen(s_info.conf_file, "r");
159         if (!fp) {
160                 ErrPrint("Open failed: %s\n", strerror(errno));
161                 return LB_STATUS_ERROR_IO;
162         }
163
164         updated = 0;
165         status = START;
166         idx = 0;
167         do {
168                 ch = fgetc(fp);
169
170                 if (idx == MAX_COLUMN) {
171                         ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
172                         status = ERROR;
173                 }
174
175                 switch (status) {
176                 case START:
177                         if (isspace(ch) || ch == EOF)
178                                 continue;
179
180                         if (ch == '#') {
181                                 status = COMMENT;
182                         } else {
183                                 status = TYPE;
184                                 idx = 0;
185                                 ungetc(ch, fp);
186                         }
187                         break;
188                 case TYPE:
189                         if (isblank(ch)) {
190                                 buffer[idx] = '\0';
191                                 status = TYPE_END;
192                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
193                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
194                                         status = ERROR;
195                                 }
196                                 break;
197                         } else if (ch == '=') {
198                                 buffer[idx] = '\0';
199                                 status = SIZE_START;
200                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
201                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
202                                         status = ERROR;
203                                 }
204                                 break;
205                         } else if (ch == EOF) {
206                                 ErrPrint("Invalid Syntax\n");
207                                 status = ERROR;
208                                 continue;
209                         }
210                         buffer[idx++] = ch;
211                         break;
212                 case TYPE_END:
213                         if (ch == '=')
214                                 status = SIZE_START;
215                         break;
216                 case SIZE_START:
217                         if (isspace(ch) || ch == EOF)
218                                 continue;
219
220                         status = SIZE;
221                         idx = 0;
222                         ungetc(ch, fp);
223                         break;
224                 case SIZE:
225                         if (isspace(ch) || ch == EOF) {
226                                 buffer[idx] = '\0';
227                                 status = EOL;
228
229                                 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
230                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
231                                         status = ERROR;
232                                 } else if (ch == EOF) {
233                                         updated += update_info(width_type, height_type, width, height);
234                                 }
235                                 break;
236                         }
237                         buffer[idx++] = ch;
238                         break;
239                 case EOL:
240                         updated += update_info(width_type, height_type, width, height);
241                         status = START;
242                         ungetc(ch, fp);
243                         break;
244                 case ERROR:
245                         if (ch == '\n' || ch == '\r' || ch == '\f')
246                                 status = START;
247                         break;
248                 case COMMENT:
249                         if (ch == '\n' || ch == '\r' || ch == '\f')
250                                 status = START;
251                         break;
252                 default:
253                         ErrPrint("Unknown status. couldn't be reach to here\n");
254                         break;
255                 }
256         } while (!feof(fp));
257         fclose(fp);
258
259         return NR_OF_SIZE_LIST - updated;
260 }
261
262 static int update_resolution(void)
263 {
264         Display *disp;
265         Window root;
266         Window dummy;
267         int x, y;
268         unsigned int width;
269         unsigned int height;
270         unsigned int border;
271         unsigned int depth;
272         register int i;
273
274         if (s_info.res_resolved)
275                 return LB_STATUS_SUCCESS;
276
277         disp = XOpenDisplay(NULL);
278         if (!disp) {
279                 ErrPrint("Failed to open a display\n");
280                 return LB_STATUS_ERROR_FAULT;
281         }
282
283         root = XDefaultRootWindow(disp);
284         if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
285                 XCloseDisplay(disp);
286                 return LB_STATUS_ERROR_FAULT;
287         }
288
289         if (update_from_file() == 0)
290                 DbgPrint("Resolution info is all updated by file\n");
291
292         DbgPrint("Screen resolution: %dx%d\n", width, height);
293         for (i = 0; i < NR_OF_SIZE_LIST; i++) {
294                 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
295                 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / 720.0f);
296                 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
297         }
298
299         XCloseDisplay(disp);
300         s_info.res_resolved = 1;
301         return LB_STATUS_SUCCESS;
302 }
303
304 static inline sqlite3 *open_db(void)
305 {
306         sqlite3 *handle;
307
308         if (!s_info.handle) {
309                 int ret;
310
311                 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
312                 if (ret != SQLITE_OK) {
313                         ErrPrint("Failed to open a DB\n");
314                         return NULL;
315                 }
316         } else {
317                 handle = s_info.handle;
318         }
319
320         return handle;
321 }
322
323 static inline void close_db(sqlite3 *handle)
324 {
325         if (!s_info.handle)
326                 db_util_close(handle);
327 }
328
329 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
330 {
331         int idx;
332
333         switch (type) {
334         case LB_SIZE_TYPE_1x1: /*!< 175x175 */
335                 idx = 0;
336                 break;
337         case LB_SIZE_TYPE_2x1: /*!< 354x175 */
338                 idx = 1;
339                 break;
340         case LB_SIZE_TYPE_2x2: /*!< 354x354 */
341                 idx = 2;
342                 break;
343         case LB_SIZE_TYPE_4x1: /*!< 712x175 */
344                 idx = 3;
345                 break;
346         case LB_SIZE_TYPE_4x2: /*!< 712x354 */
347                 idx = 4;
348                 break;
349         case LB_SIZE_TYPE_4x3: /*!< 712x533 */
350                 idx = 5;
351                 break;
352         case LB_SIZE_TYPE_4x4: /*!< 712x712 */
353                 idx = 6;
354                 break;
355         case LB_SIZE_TYPE_4x5: /*!< 712x891 */
356                 idx = 7;
357                 break;
358         case LB_SIZE_TYPE_4x6: /*!< 712x1070 */
359                 idx = 8;
360                 break;
361         case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
362                 idx = 9;
363                 break;
364         case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
365                 idx = 10;
366                 break;
367         case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
368                 idx = 11;
369                 break;
370         case LB_SIZE_TYPE_0x0: /*!< 720x1280 */
371                 idx = 12;
372                 break;
373         default:
374                 return LB_STATUS_ERROR_INVALID;
375         }
376
377         if (update_resolution() < 0)
378                 ErrPrint("Failed to update resolution\n");
379
380         *width = SIZE_LIST[idx].w;
381         *height = SIZE_LIST[idx].h;
382         return LB_STATUS_SUCCESS;
383 }
384
385 EAPI int livebox_service_change_period(const char *pkgname, const char *id, double period)
386 {
387         struct packet *packet;
388         struct packet *result;
389         char *uri;
390         int ret;
391
392         if (!pkgname || !id || period < 0.0f) {
393                 ErrPrint("Invalid argument\n");
394                 return LB_STATUS_ERROR_INVALID;
395         }
396
397         uri = util_id_to_uri(id);
398         if (!uri)
399                 return LB_STATUS_ERROR_MEMORY;
400
401         packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
402         free(uri);
403         if (!packet) {
404                 ErrPrint("Failed to create a packet for period changing\n");
405                 return LB_STATUS_ERROR_FAULT;
406         }
407
408         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
409         packet_unref(packet);
410
411         if (result) {
412                 if (packet_get(result, "i", &ret) != 1) {
413                         ErrPrint("Failed to parse a result packet\n");
414                         ret = LB_STATUS_ERROR_INVALID;
415                 }
416                 packet_unref(result);
417         } else {
418                 ErrPrint("Failed to get result packet\n");
419                 ret = LB_STATUS_ERROR_FAULT;
420         }
421
422         return ret;
423 }
424
425 EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
426 {
427         struct packet *packet;
428         struct packet *result;
429         char *uri;
430         int ret;
431
432         if (!pkgname) {
433                 ErrPrint("Invalid argument\n");
434                 return LB_STATUS_ERROR_INVALID;
435         }
436
437         if (!force && access("/tmp/.live.paused", R_OK) == 0) {
438                 DbgPrint("Provider is paused\n");
439                 return LB_STATUS_ERROR_CANCEL;
440         }
441
442         uri = util_id_to_uri(id);
443         if (!uri)
444                 return LB_STATUS_ERROR_MEMORY;
445
446         if (!cluster)
447                 cluster = "user,created";
448
449         if (!category)
450                 category = "default";
451
452         packet = packet_create("service_update", "ssss", pkgname, uri, cluster, category);
453         free(uri);
454         if (!packet) {
455                 ErrPrint("Failed to create a packet for service_update\n");
456                 return LB_STATUS_ERROR_FAULT;
457         }
458
459         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
460         packet_unref(packet);
461
462         if (result) {
463                 if (packet_get(result, "i", &ret) != 1) {
464                         ErrPrint("Failed to parse a result packet\n");
465                         ret = LB_STATUS_ERROR_INVALID;
466                 }
467
468                 packet_unref(result);
469         } else {
470                 ErrPrint("Failed to get result packet\n");
471                 ret = LB_STATUS_ERROR_FAULT;
472         }
473
474         return ret;
475 }
476
477 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
478 {
479         int ret;
480         sqlite3_stmt *stmt;
481         char *appid;
482         char *pkgid;
483         int is_prime;
484         sqlite3 *handle;
485
486         if (!cb)
487                 return LB_STATUS_ERROR_INVALID;
488
489         handle = open_db();
490         if (!handle)
491                 return LB_STATUS_ERROR_IO;
492
493         ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
494         if (ret != SQLITE_OK) {
495                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
496                 ret = LB_STATUS_ERROR_IO;
497                 goto out;
498         }
499
500         ret = 0;
501         while (sqlite3_step(stmt) == SQLITE_ROW) {
502                 appid = (char *)sqlite3_column_text(stmt, 0);
503                 if (!appid || !strlen(appid)) {
504                         ErrPrint("APPID is not valid\n");
505                         continue;
506                 }
507
508                 pkgid = (char *)sqlite3_column_text(stmt, 1);
509                 if (!pkgid || !strlen(pkgid)) {
510                         ErrPrint("pkgid is not valid\n");
511                         continue;
512                 }
513
514                 is_prime = sqlite3_column_int(stmt, 2);
515
516                 ret++;
517
518                 if (cb(appid, pkgid, is_prime, data) < 0) {
519                         DbgPrint("Callback stopped package crawling\n");
520                         break;
521                 }
522         }
523
524         sqlite3_reset(stmt);
525         sqlite3_finalize(stmt);
526
527 out:
528         close_db(handle);
529         return ret;
530 }
531
532 EAPI int livebox_service_get_pkglist_by_pkgid(const char *pkgid, int (*cb)(const char *lbid, int is_prime, void *data), void *data)
533 {
534         int ret;
535         sqlite3_stmt *stmt;
536         const char *lbid;
537         int is_prime;
538         sqlite3 *handle;
539
540         if (!cb)
541                 return LB_STATUS_ERROR_INVALID;
542
543         handle = open_db();
544         if (!handle)
545                 return LB_STATUS_ERROR_IO;
546
547         ret = sqlite3_prepare_v2(handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
548         if (ret != SQLITE_OK) {
549                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
550                 ret = LB_STATUS_ERROR_IO;
551                 goto out;
552         }
553
554         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
555         if (ret != SQLITE_OK) {
556                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
557                 sqlite3_reset(stmt);
558                 sqlite3_finalize(stmt);
559                 ret = LB_STATUS_ERROR_IO;
560                 goto out;
561         }
562
563         ret = 0;
564         while (sqlite3_step(stmt) == SQLITE_ROW) {
565                 lbid = (const char *)sqlite3_column_text(stmt, 0);
566                 if (!lbid || !strlen(lbid)) {
567                         ErrPrint("LBID is not valid\n");
568                         continue;
569                 }
570
571                 is_prime = sqlite3_column_int(stmt, 1);
572
573                 ret++;
574
575                 if (cb(lbid, is_prime, data) < 0) {
576                         DbgPrint("Callback stopped package crawling\n");
577                         break;
578                 }
579         }
580
581         sqlite3_reset(stmt);
582         sqlite3_finalize(stmt);
583
584 out:
585         close_db(handle);
586         return ret;
587 }
588
589 struct pkgmgr_cbdata {
590         const char *lbid;
591         void (*cb)(const char *lbid, const char *appid, void *data);
592         void *cbdata;
593 };
594
595 static int pkgmgr_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
596 {
597         struct pkgmgr_cbdata *cbdata = (struct pkgmgr_cbdata *)user_data;
598         char *appid;
599         int ret;
600
601         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
602         if (ret < 0)
603                 ErrPrint("Unable to get appid\n");
604         else
605                 cbdata->cb(cbdata->lbid, appid, cbdata->cbdata);
606
607         return 0;
608 }
609
610 static inline char *pkgmgr_get_mainapp(const char *pkgid)
611 {
612         pkgmgrinfo_pkginfo_h handle;
613         char *ret = NULL;
614
615         if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle) != PMINFO_R_OK) {
616                 ErrPrint("Unable to get mainapp: %s\n", pkgid);
617                 return NULL;
618         }
619
620         if (pkgmgrinfo_pkginfo_get_mainappid(handle, &ret) == PMINFO_R_OK) {
621                 ret = strdup(ret);
622         } else {
623                 ErrPrint("Failed to get mainappid\n");
624                 ret = NULL; /* I cannot believe the pkgmgrinfo_pkginfo_get_mainappid. it maybe able to touch my "ret" even though it fails */
625         }
626
627         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
628         return ret;
629 }
630
631 static inline int pkgmgr_get_applist(const char *pkgid, const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
632 {
633         struct pkgmgr_cbdata cbdata;
634         pkgmgrinfo_pkginfo_h handle;
635         int ret;
636
637         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
638         if (ret < 0) {
639                 ErrPrint("Unable to get pkginfo: %s\n", pkgid);
640                 return ret;
641         }
642
643         cbdata.lbid = lbid;
644         cbdata.cb = cb;
645         cbdata.cbdata = data;
646
647         ret = pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, pkgmgr_cb, &cbdata);
648         if (ret < 0)
649                 ErrPrint("Failed to get applist\n");
650
651         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
652         return ret;
653 }
654
655 EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
656 {
657         sqlite3_stmt *stmt;
658         const char *tmp;
659         char *pkgid;
660         sqlite3 *handle;
661         int ret;
662
663         if (!lbid || !cb)
664                 return LB_STATUS_ERROR_INVALID;
665
666         handle = open_db();
667         if (!handle)
668                 return LB_STATUS_ERROR_IO;
669
670         ret = sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL);
671         if (ret != SQLITE_OK) {
672                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
673                 ret = LB_STATUS_ERROR_IO;
674                 goto out;
675         }
676
677         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
678         if (ret != SQLITE_OK) {
679                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
680                 ret = LB_STATUS_ERROR_IO;
681                 goto out;
682         }
683
684         ret = sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT);
685         if (ret != SQLITE_OK) {
686                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
687                 ret = LB_STATUS_ERROR_IO;
688                 goto out;
689         }
690
691         if (sqlite3_step(stmt) != SQLITE_ROW) {
692                 ret = LB_STATUS_ERROR_INVALID;
693                 sqlite3_reset(stmt);
694                 sqlite3_finalize(stmt);
695                 goto out;
696         }
697
698         tmp = (const char *)sqlite3_column_text(stmt, 0);
699         if (!tmp || !strlen(tmp)) {
700                 ErrPrint("Invalid package name (%s)\n", lbid);
701                 ret = LB_STATUS_ERROR_INVALID;
702                 sqlite3_reset(stmt);
703                 sqlite3_finalize(stmt);
704                 goto out;
705         }
706
707         pkgid = strdup(tmp);
708         if (!pkgid) {
709                 ErrPrint("Error: %s\n", strerror(errno));
710                 ret = LB_STATUS_ERROR_MEMORY;
711                 sqlite3_reset(stmt);
712                 sqlite3_finalize(stmt);
713                 goto out;
714         }
715
716         sqlite3_reset(stmt);
717         sqlite3_finalize(stmt);
718
719         ret = pkgmgr_get_applist(pkgid, lbid, cb, data);
720         free(pkgid);
721
722 out:
723         close_db(handle);
724         return ret;
725 }
726
727 EAPI char *livebox_service_mainappid(const char *lbid)
728 {
729         sqlite3_stmt *stmt;
730         const char *tmp;
731         char *pkgid;
732         sqlite3 *handle;
733         char *ret = NULL;
734
735         if (!lbid)
736                 return NULL;
737
738         handle = open_db();
739         if (!handle)
740                 return NULL;
741
742         if (sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL) != SQLITE_OK) {
743                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
744                 goto out;
745         }
746
747         if (sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
748                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
749                 goto out;
750         }
751
752         if (sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
753                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
754                 goto out;
755         }
756
757         if (sqlite3_step(stmt) != SQLITE_ROW) {
758                 sqlite3_reset(stmt);
759                 sqlite3_finalize(stmt);
760                 goto out;
761         }
762
763         tmp = (const char *)sqlite3_column_text(stmt, 0);
764         if (!tmp || !strlen(tmp)) {
765                 ErrPrint("Invalid package name (%s)\n", lbid);
766                 sqlite3_reset(stmt);
767                 sqlite3_finalize(stmt);
768                 goto out;
769         }
770
771         pkgid = strdup(tmp);
772         if (!pkgid) {
773                 ErrPrint("Error: %s\n", strerror(errno));
774                 sqlite3_reset(stmt);
775                 sqlite3_finalize(stmt);
776                 goto out;
777         }
778
779         sqlite3_reset(stmt);
780         sqlite3_finalize(stmt);
781
782         ret = pkgmgr_get_mainapp(pkgid);
783         free(pkgid);
784
785 out:
786         close_db(handle);
787         return ret;
788 }
789
790 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
791 {
792         sqlite3_stmt *stmt;
793         sqlite3 *handle;
794         int size;
795         int ret;
796
797         if (!types || !cnt || !pkgid)
798                 return LB_STATUS_ERROR_INVALID;
799
800         handle = open_db();
801         if (!handle)
802                 return LB_STATUS_ERROR_IO;
803
804         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
805         if (ret != SQLITE_OK) {
806                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
807                 ret = LB_STATUS_ERROR_IO;
808                 goto out;
809         }
810
811         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
812         if (ret != SQLITE_OK) {
813                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
814                 sqlite3_reset(stmt);
815                 sqlite3_finalize(stmt);
816                 ret = LB_STATUS_ERROR_IO;
817                 goto out;
818         }
819
820         if (*cnt > NR_OF_SIZE_LIST)
821                 *cnt = NR_OF_SIZE_LIST;
822
823         ret = 0;
824         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
825                 size = sqlite3_column_int(stmt, 0);
826                 types[ret] = size;
827                 ret++;
828         }
829
830         *cnt = ret;
831         sqlite3_reset(stmt);
832         sqlite3_finalize(stmt);
833         ret = 0;
834 out:
835         close_db(handle);
836         return ret;
837 }
838
839 static inline char *cur_locale(void)
840 {
841         char *language;
842         language = vconf_get_str(VCONFKEY_LANGSET);
843         if (language) {
844                 char *ptr;
845
846                 ptr = language;
847                 while (*ptr) {
848                         if (*ptr == '.') {
849                                 *ptr = '\0';
850                                 break;
851                         }
852
853                         if (*ptr == '_')
854                                 *ptr = '-';
855
856                         ptr++;
857                 }
858         } else {
859                 language = strdup("en-us");
860                 if (!language)
861                         ErrPrint("Heap: %s\n", strerror(errno));
862         }
863
864         return language;
865 }
866
867 static inline char *get_default_name(const char *pkgid)
868 {
869         sqlite3_stmt *stmt;
870         sqlite3 *handle;
871         char *name = NULL;
872         int ret;
873
874         handle = open_db();
875         if (!handle)
876                 return NULL;
877
878         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
879         if (ret != SQLITE_OK) {
880                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
881                 close_db(handle);
882                 return NULL;
883         }
884
885         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
886         if (ret != SQLITE_OK) {
887                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
888                 goto out;
889         }
890
891         ret = sqlite3_step(stmt);
892         if (ret ==  SQLITE_ROW) {
893                 const char *tmp;
894
895                 tmp = (const char *)sqlite3_column_text(stmt, 0);
896                 if (tmp && strlen(tmp)) {
897                         name = strdup(tmp);
898                         if (!name)
899                                 ErrPrint("Heap: %s\n", strerror(errno));
900                 }
901         }
902
903 out:
904         sqlite3_reset(stmt);
905         sqlite3_finalize(stmt);
906         close_db(handle);
907         return name;
908 }
909
910 static inline char *get_default_icon(const char *pkgid)
911 {
912         sqlite3_stmt *stmt;
913         sqlite3 *handle;
914         char *icon = NULL;
915         int ret;
916
917         handle = open_db();
918         if (!handle)
919                 return NULL;
920
921         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
922         if (ret != SQLITE_OK) {
923                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
924                 close_db(handle);
925                 return NULL;
926         }
927
928         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
929         if (ret != SQLITE_OK) {
930                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
931                 goto out;
932         }
933
934         ret = sqlite3_step(stmt);
935         if (ret == SQLITE_ROW) {
936                 const char *tmp;
937
938                 tmp = (const char *)sqlite3_column_text(stmt, 0);
939                 if (tmp && strlen(tmp)) {
940                         icon = strdup(tmp);
941                         if (!icon)
942                                 ErrPrint("Heap: %s\n", strerror(errno));
943                 }
944         }
945
946 out:
947         sqlite3_reset(stmt);
948         sqlite3_finalize(stmt);
949         close_db(handle);
950         return icon;
951 }
952
953 EAPI char *livebox_service_content(const char *pkgid)
954 {
955         sqlite3_stmt *stmt;
956         sqlite3 *handle;
957         char *content = NULL;
958         int ret;
959
960         handle = open_db();
961         if (!handle)
962                 return NULL;
963
964         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
965         if (ret != SQLITE_OK) {
966                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
967                 close_db(handle);
968                 return NULL;
969         }
970
971         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
972         if (ret != SQLITE_OK) {
973                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
974                 goto out;
975         }
976
977         ret = sqlite3_step(stmt);
978         if (ret == SQLITE_ROW) {
979                 const char *tmp;
980
981                 tmp = (const char *)sqlite3_column_text(stmt, 0);
982                 if (tmp && strlen(tmp)) {
983                         content = strdup(tmp);
984                         if (!content)
985                                 ErrPrint("Heap: %s\n", strerror(errno));
986                 }
987         }
988
989 out:
990         sqlite3_reset(stmt);
991         sqlite3_finalize(stmt);
992         close_db(handle);
993         return content;
994 }
995
996 EAPI char *livebox_service_setup_appid(const char *lbid)
997 {
998         sqlite3_stmt *stmt;
999         sqlite3 *handle;
1000         int ret;
1001         char *appid;
1002
1003         handle = open_db();
1004         if (!handle)
1005                 return NULL;
1006
1007         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1008         if (ret != SQLITE_OK) {
1009                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1010                 close_db(handle);
1011                 return NULL;
1012         }
1013
1014         appid = NULL;
1015         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1016         if (ret != SQLITE_OK) {
1017                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1018                 goto out;
1019         }
1020
1021         ret = sqlite3_step(stmt);
1022         if (ret == SQLITE_ROW) {
1023                 const char *tmp;
1024
1025                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1026                 if (!tmp || !strlen(tmp))
1027                         goto out;
1028
1029                 appid = strdup(tmp);
1030                 if (!appid)
1031                         ErrPrint("Error: %s\n", strerror(errno));
1032         }
1033
1034 out:
1035         sqlite3_reset(stmt);
1036         sqlite3_finalize(stmt);
1037         close_db(handle);
1038         return appid;
1039 }
1040
1041 EAPI int livebox_service_nodisplay(const char *pkgid)
1042 {
1043         sqlite3_stmt *stmt;
1044         sqlite3 *handle;
1045         int ret;
1046
1047         handle = open_db();
1048         if (!handle)
1049                 return 0;
1050
1051         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1052         if (ret != SQLITE_OK) {
1053                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1054                 close_db(handle);
1055                 return 0;
1056         }
1057
1058         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1059         if (ret != SQLITE_OK) {
1060                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1061                 ret = 0;
1062                 goto out;
1063         }
1064
1065         ret = sqlite3_step(stmt);
1066         if (ret == SQLITE_ROW)
1067                 ret = !!sqlite3_column_int(stmt, 0);
1068         else
1069                 ret = 0;
1070
1071 out:
1072         sqlite3_reset(stmt);
1073         sqlite3_finalize(stmt);
1074         close_db(handle);
1075         return ret;
1076 }
1077
1078 static inline char *get_lb_pkgname_by_appid(const char *appid)
1079 {
1080         sqlite3_stmt *stmt;
1081         char *pkgid;
1082         char *tmp;
1083         sqlite3 *handle;
1084         int ret;
1085
1086         if (!appid)
1087                 return NULL;
1088
1089         pkgid = NULL;
1090         handle = open_db();
1091         if (!handle)
1092                 return NULL;
1093
1094         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1095         if (ret != SQLITE_OK) {
1096                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1097                 close_db(handle);
1098                 return NULL;
1099         }
1100
1101         ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
1102         if (ret != SQLITE_OK) {
1103                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1104                 goto out;
1105         }
1106
1107         ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
1108         if (ret != SQLITE_OK) {
1109                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1110                 goto out;
1111         }
1112
1113         if (sqlite3_step(stmt) != SQLITE_ROW) {
1114                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1115                 goto out;
1116         }
1117
1118         tmp = (char *)sqlite3_column_text(stmt, 0);
1119         if (tmp && strlen(tmp)) {
1120                 pkgid = strdup(tmp);
1121                 if (!pkgid)
1122                         ErrPrint("Heap: %s\n", strerror(errno));
1123         }
1124
1125 out:
1126         sqlite3_reset(stmt);
1127         sqlite3_finalize(stmt);
1128         close_db(handle);
1129         return pkgid;
1130 }
1131
1132 EAPI int livebox_service_need_frame(const char *pkgid, int size_type)
1133 {
1134         char *lbid;
1135         sqlite3_stmt *stmt;
1136         sqlite3 *handle;
1137         int ret;
1138
1139         handle = open_db();
1140         if (!handle) {
1141                 ErrPrint("Unable to open a DB\n");
1142                 return 0;
1143         }
1144
1145         ret = sqlite3_prepare_v2(handle, "SELECT need_frame FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1146         if (ret != SQLITE_OK) {
1147                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1148                 close_db(handle);
1149                 return 0;
1150         }
1151
1152         /*!
1153          */
1154         lbid = livebox_service_pkgname(pkgid);
1155         if (!lbid) {
1156                 ErrPrint("Invalid appid (%s)\n", pkgid);
1157                 ret = 0;
1158                 goto out;
1159         }
1160
1161         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1162         free(lbid);
1163         if (ret != SQLITE_OK) {
1164                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1165                 ret = 0;
1166                 goto out;
1167         }
1168
1169         ret = sqlite3_bind_int(stmt, 2, size_type);
1170         if (ret != SQLITE_OK) {
1171                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1172                 ret = 0;
1173                 goto out;
1174         }
1175
1176         ret = sqlite3_step(stmt);
1177         if (ret == SQLITE_ROW) {
1178                 ret = !!sqlite3_column_int(stmt, 0);
1179         } else {
1180                 ret = 0;
1181                 ErrPrint("There is no such result\n");
1182         }
1183 out:
1184         sqlite3_reset(stmt);
1185         sqlite3_finalize(stmt);
1186         close_db(handle);
1187         return ret;
1188 }
1189
1190 EAPI int livebox_service_touch_effect(const char *pkgid, int size_type)
1191 {
1192         char *lbid;
1193         sqlite3_stmt *stmt;
1194         sqlite3 *handle;
1195         int ret;
1196
1197         handle = open_db();
1198         if (!handle) {
1199                 ErrPrint("Unable to open a DB\n");
1200                 return 1;
1201         }
1202
1203         ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1204         if (ret != SQLITE_OK) {
1205                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1206                 close_db(handle);
1207                 return 1;
1208         }
1209
1210         /*!
1211          * \note
1212          * This function will validate the "pkgid"
1213          * call the exported API in the exported API is not recomended
1214          * but... I used.
1215          */
1216         lbid = livebox_service_pkgname(pkgid);
1217         if (!lbid) {
1218                 ErrPrint("Invalid appid (%s)\n", pkgid);
1219                 ret = 1;
1220                 goto out;
1221         }
1222
1223         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1224         free(lbid);
1225         if (ret != SQLITE_OK) {
1226                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1227                 ret = 1;
1228                 goto out;
1229         }
1230
1231         ret = sqlite3_bind_int(stmt, 2, size_type);
1232         if (ret != SQLITE_OK) {
1233                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1234                 ret = 1;
1235                 goto out;
1236         }
1237
1238         ret = sqlite3_step(stmt);
1239         if (ret == SQLITE_ROW) {
1240                 ret = !!sqlite3_column_int(stmt, 0);
1241         } else {
1242                 ret = 1; /*!< Default true: In this case the DB is corrupted. */
1243                 ErrPrint("There is no result\n");
1244         }
1245
1246 out:
1247         sqlite3_reset(stmt);
1248         sqlite3_finalize(stmt);
1249         close_db(handle);
1250         return ret;
1251 }
1252
1253 EAPI int livebox_service_mouse_event(const char *pkgid)
1254 {
1255         sqlite3_stmt *stmt;
1256         sqlite3 *handle;
1257         char *lbid;
1258         int ret;
1259
1260         handle = open_db();
1261         if (!handle)
1262                 return 0;
1263
1264         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1265         if (ret != SQLITE_OK) {
1266                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1267                 close_db(handle);
1268                 return 0;
1269         }
1270
1271         lbid = livebox_service_pkgname(pkgid);
1272         if (!lbid) {
1273                 ErrPrint("Failed to get lbid: %s\n", pkgid);
1274                 ret = 0;
1275                 goto out;
1276         }
1277
1278         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1279         free(lbid);
1280         if (ret != SQLITE_OK) {
1281                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1282                 ret = 0;
1283                 goto out;
1284         }
1285
1286         ret = sqlite3_step(stmt);
1287         if (ret == SQLITE_ROW) {
1288                 ret = !!sqlite3_column_int(stmt, 0);
1289         } else {
1290                 ret = 0; /*!< Default is false, In this case the DB is corrupted */
1291                 ErrPrint("There is no result.\n");
1292         }
1293
1294 out:
1295         sqlite3_reset(stmt);
1296         sqlite3_finalize(stmt);
1297         close_db(handle);
1298         return ret;
1299 }
1300
1301 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
1302 {
1303         sqlite3_stmt *stmt;
1304         sqlite3 *handle;
1305         int ret;
1306         char *preview = NULL;
1307
1308         handle = open_db();
1309         if (!handle)
1310                 return NULL;
1311
1312         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1313         if (ret != SQLITE_OK) {
1314                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1315                 close_db(handle);
1316                 return NULL;
1317         }
1318
1319         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1320         if (ret != SQLITE_OK) {
1321                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1322                 goto out;
1323         }
1324
1325         ret = sqlite3_bind_int(stmt, 2, size_type);
1326         if (ret != SQLITE_OK) {
1327                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1328                 goto out;
1329         }
1330
1331         ret = sqlite3_step(stmt);
1332         if (ret == SQLITE_ROW) {
1333                 const char *tmp;
1334                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1335                 if (tmp && strlen(tmp)) {
1336                         preview = strdup(tmp);
1337                         if (!preview)
1338                                 ErrPrint("Heap: %s\n", strerror(errno));
1339                 }
1340         }
1341
1342 out:
1343         sqlite3_reset(stmt);
1344         sqlite3_finalize(stmt);
1345         close_db(handle);
1346         return preview;
1347 }
1348
1349 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1350 {
1351         sqlite3_stmt *stmt;
1352         sqlite3 *handle;
1353         char *language;
1354         char *icon = NULL;
1355         int ret;
1356
1357         if (lang) {
1358                 language = strdup(lang);
1359                 if (!language) {
1360                         ErrPrint("Heap: %s\n", strerror(errno));
1361                         return NULL;
1362                 }
1363         } else {
1364                 language = cur_locale();
1365                 if (!language)
1366                         return NULL;
1367         }
1368
1369         handle = open_db();
1370         if (!handle) {
1371                 free(language);
1372                 return NULL;
1373         }
1374
1375         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1376         if (ret != SQLITE_OK) {
1377                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1378                 close_db(handle);
1379                 free(language);
1380                 return NULL;
1381         }
1382
1383         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1384         if (ret != SQLITE_OK) {
1385                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1386                 goto out;
1387         }
1388
1389         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1390         if (ret != SQLITE_OK) {
1391                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1392                 goto out;
1393         }
1394
1395         ret = sqlite3_step(stmt);
1396         if (ret == SQLITE_ROW) {
1397                 const char *tmp;
1398                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1399                 if (!tmp || !strlen(tmp)) {
1400                         icon = get_default_icon(pkgid);
1401                 } else {
1402                         icon = strdup(tmp);
1403                         if (!icon)
1404                                 ErrPrint("Heap: %s\n", strerror(errno));
1405                 }
1406         } else {
1407                 icon = get_default_icon(pkgid);
1408         }
1409
1410 out:
1411         sqlite3_reset(stmt);
1412         sqlite3_finalize(stmt);
1413         close_db(handle);
1414         free(language);
1415         return icon;
1416 }
1417
1418 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1419 {
1420         sqlite3_stmt *stmt;
1421         sqlite3 *handle;
1422         char *language;
1423         char *name = NULL;
1424         int ret;
1425
1426         if (lang) {
1427                 language = strdup(lang);
1428                 if (!language) {
1429                         ErrPrint("Error: %s\n", strerror(errno));
1430                         return NULL;
1431                 }
1432         } else {
1433                 language = cur_locale();
1434                 if (!language)
1435                         return NULL;
1436         }
1437
1438         handle = open_db();
1439         if (!handle) {
1440                 free(language);
1441                 return NULL;
1442         }
1443
1444         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1445         if (ret != SQLITE_OK) {
1446                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1447                 close_db(handle);
1448                 free(language);
1449                 return NULL;
1450         }
1451
1452         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1453         if (ret != SQLITE_OK) {
1454                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1455                 goto out;
1456         }
1457
1458         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1459         if (ret != SQLITE_OK) {
1460                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1461                 goto out;
1462         }
1463
1464         ret = sqlite3_step(stmt);
1465         if (ret == SQLITE_ROW) {
1466                 const char *tmp;
1467                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1468                 if (!tmp || !strlen(tmp)) {
1469                         name = get_default_name(pkgid);
1470                 } else {
1471                         name = strdup(tmp);
1472                         if (!name)
1473                                 ErrPrint("Heap: %s\n", strerror(errno));
1474                 }
1475         } else {
1476                 name = get_default_name(pkgid);
1477         }
1478
1479 out:
1480         sqlite3_reset(stmt);
1481         sqlite3_finalize(stmt);
1482         close_db(handle);
1483         free(language);
1484         return name;
1485 }
1486
1487 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1488 {
1489         sqlite3_stmt *stmt;
1490         sqlite3 *handle;
1491         int size;
1492         int ret;
1493
1494         if (!w || !h || !cnt || !pkgid)
1495                 return LB_STATUS_ERROR_INVALID;
1496
1497         handle = open_db();
1498         if (!handle)
1499                 return LB_STATUS_ERROR_IO;
1500
1501         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1502         if (ret != SQLITE_OK) {
1503                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1504                 ret = LB_STATUS_ERROR_IO;
1505                 goto out;
1506         }
1507
1508         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1509         if (ret != SQLITE_OK) {
1510                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1511                 sqlite3_reset(stmt);
1512                 sqlite3_finalize(stmt);
1513                 ret = LB_STATUS_ERROR_IO;
1514                 goto out;
1515         }
1516
1517         if (*cnt > NR_OF_SIZE_LIST)
1518                 *cnt = NR_OF_SIZE_LIST;
1519
1520         ret = 0;
1521         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1522                 size = sqlite3_column_int(stmt, 0);
1523                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1524         }
1525
1526         *cnt = ret;
1527         sqlite3_reset(stmt);
1528         sqlite3_finalize(stmt);
1529         ret = 0;
1530 out:
1531         close_db(handle);
1532         return ret;
1533 }
1534
1535 EAPI char *livebox_service_libexec(const char *pkgid)
1536 {
1537         sqlite3_stmt *stmt;
1538         sqlite3 *handle;
1539         int ret;
1540         char *libexec;
1541         char *appid;
1542         char *path;
1543
1544         if (!pkgid)
1545                 return NULL;
1546
1547         libexec = NULL;
1548         handle = open_db();
1549         if (!handle)
1550                 return NULL;
1551
1552         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1553         if (ret != SQLITE_OK) {
1554                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1555                 goto out;
1556         }
1557
1558         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1559         if (ret != SQLITE_OK) {
1560                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1561                 sqlite3_finalize(stmt);
1562                 goto out;
1563         }
1564
1565         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1566         if (ret != SQLITE_OK) {
1567                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1568                 sqlite3_finalize(stmt);
1569                 goto out;
1570         }
1571
1572         if (sqlite3_step(stmt) != SQLITE_ROW) {
1573                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1574                 sqlite3_reset(stmt);
1575                 sqlite3_finalize(stmt);
1576
1577                 libexec = util_conf_get_libexec(pkgid);
1578                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1579                 goto out;
1580         }
1581
1582         appid = (char *)sqlite3_column_text(stmt, 0);
1583         if (!appid || !strlen(appid)) {
1584                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1585                 sqlite3_reset(stmt);
1586                 sqlite3_finalize(stmt);
1587                 goto out;
1588         }
1589
1590         path = (char *)sqlite3_column_text(stmt, 1);
1591         if (!path || !strlen(path)) {
1592                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1593                 sqlite3_reset(stmt);
1594                 sqlite3_finalize(stmt);
1595                 goto out;
1596         }
1597
1598         libexec = strdup(path);
1599         if (!libexec) {
1600                 ErrPrint("Heap: %s\n", strerror(errno));
1601                 sqlite3_reset(stmt);
1602                 sqlite3_finalize(stmt);
1603                 goto out;
1604         }
1605
1606         DbgPrint("libexec: %s\n", libexec);
1607
1608         sqlite3_reset(stmt);
1609         sqlite3_finalize(stmt);
1610 out:
1611         close_db(handle);
1612         return libexec;
1613 }
1614
1615 EAPI char *livebox_service_pkgname(const char *appid)
1616 {
1617         char *lb_pkgname;
1618         pkgmgr_appinfo_h handle;
1619         int ret;
1620         char *new_appid;
1621
1622         if (!appid)
1623                 return NULL;
1624
1625         lb_pkgname = get_lb_pkgname_by_appid(appid);
1626         if (lb_pkgname)
1627                 return lb_pkgname;
1628
1629         /*!
1630          * \note
1631          * Try to get the package id using given appid
1632          */
1633         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1634         if (ret != PKGMGR_R_OK) {
1635                 ErrPrint("Failed to get appinfo\n");
1636                 return NULL;
1637         }
1638
1639         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1640         if (ret != PKGMGR_R_OK) {
1641                 pkgmgr_appinfo_destroy_appinfo(handle);
1642                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1643                 return NULL;
1644         }
1645
1646         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1647         pkgmgr_appinfo_destroy_appinfo(handle);
1648
1649         if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1650                 return strdup(appid);
1651
1652         return lb_pkgname;
1653 }
1654
1655 EAPI char *livebox_service_provider_name(const char *lbid)
1656 {
1657         char *ret;
1658         int stage = 0;
1659         int seq = 0;
1660         int idx = 0;
1661         char *str = SAMSUNG_PREFIX;
1662
1663         if (!lbid)
1664                 return NULL;
1665
1666         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1667                 idx++;
1668                 if (seq < 2 && lbid[idx] == '.') {
1669                         stage = idx;
1670                         seq++;
1671                 }
1672         }
1673
1674         if (!str[idx] && lbid[idx]) {
1675                 /* Inhouse */
1676                 return strdup(lbid);
1677         } else if (seq < 2) {
1678                 while (seq < 2) {
1679                         if (lbid[idx] == '.') {
1680                                 seq++;
1681                         } else if (!lbid[idx]) {
1682                                 ErrPrint("Invalid lbid: %s\n", lbid);
1683                                 return NULL;
1684                         }
1685
1686                         idx++;
1687                 }
1688
1689                 stage = idx;
1690         } else {
1691                 stage++;
1692         }
1693
1694         ret = strdup(lbid + stage);
1695         if (!ret) {
1696                 ErrPrint("Error: %s\n", strerror(errno));
1697                 return NULL;
1698         }
1699
1700         return ret;
1701 }
1702
1703 EAPI int livebox_service_is_enabled(const char *lbid)
1704 {
1705         return 1;
1706         /*
1707         ail_appinfo_h ai;
1708         char *pkgname;
1709         bool enabled;
1710         int ret;
1711
1712         pkgname = livebox_service_appid(lbid);
1713         if (!pkgname)
1714                 return 0;
1715
1716         ret = ail_get_appinfo(pkgname, &ai);
1717         if (ret != AIL_ERROR_OK) {
1718                 free(pkgname);
1719                 return 0;
1720         }
1721
1722         if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
1723                 enabled = false;
1724
1725         ail_destroy_appinfo(ai);
1726         free(pkgname);
1727         return enabled == true;
1728         */
1729 }
1730
1731 EAPI int livebox_service_is_primary(const char *lbid)
1732 {
1733         sqlite3_stmt *stmt;
1734         sqlite3 *handle;
1735         int ret = 0;
1736
1737         if (!lbid)
1738                 return 0;
1739
1740         handle = open_db();
1741         if (!handle)
1742                 return 0;
1743
1744         ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
1745         if (ret != SQLITE_OK) {
1746                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1747                 close_db(handle);
1748                 return 0;
1749         }
1750
1751         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1752         if (ret != SQLITE_OK) {
1753                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1754                 goto out;
1755         }
1756
1757         ret = sqlite3_step(stmt);
1758         if (ret != SQLITE_ROW) {
1759                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1760                 goto out;
1761         }
1762
1763         ret = sqlite3_column_int(stmt, 0);
1764
1765 out:
1766         sqlite3_reset(stmt);
1767         sqlite3_finalize(stmt);
1768         close_db(handle);
1769         return ret;
1770 }
1771
1772 /*!
1773  * appid == Package ID
1774  * pkgid == Livebox ID
1775  */
1776 EAPI char *livebox_service_appid(const char *pkgname)
1777 {
1778         sqlite3_stmt *stmt;
1779         char *appid;
1780         char *tmp;
1781         sqlite3 *handle;
1782         int is_prime __attribute__((__unused__));
1783         int ret;
1784
1785         if (!pkgname)
1786                 return NULL;
1787
1788         appid = NULL;
1789         handle = open_db();
1790         if (!handle)
1791                 return NULL;
1792
1793         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1794         if (ret != SQLITE_OK) {
1795                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1796                 goto out;
1797         }
1798
1799         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
1800         if (ret != SQLITE_OK) {
1801                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1802                 sqlite3_reset(stmt);
1803                 sqlite3_finalize(stmt);
1804                 goto out;
1805         }
1806
1807         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
1808         if (ret != SQLITE_OK) {
1809                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1810                 sqlite3_reset(stmt);
1811                 sqlite3_finalize(stmt);
1812                 goto out;
1813         }
1814
1815         ret = sqlite3_step(stmt);
1816         if (ret != SQLITE_ROW) {
1817                 pkgmgr_appinfo_h pkg_handle;
1818                 char *new_appid;
1819
1820                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1821                 sqlite3_reset(stmt);
1822                 sqlite3_finalize(stmt);
1823
1824                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1825                 if (ret != PKGMGR_R_OK) {
1826                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
1827                         goto out;
1828                 }
1829
1830                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1831                 if (ret != PKGMGR_R_OK) {
1832                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
1833                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1834                         goto out;
1835                 }
1836
1837                 appid = strdup(new_appid);
1838                 if (!appid)
1839                         ErrPrint("Heap: %s\n", strerror(errno));
1840
1841                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1842                 goto out;
1843         }
1844
1845         tmp = (char *)sqlite3_column_text(stmt, 0);
1846         if (!tmp || !strlen(tmp)) {
1847                 ErrPrint("APPID is NIL\n");
1848                 sqlite3_reset(stmt);
1849                 sqlite3_finalize(stmt);
1850                 goto out;
1851         }
1852
1853         appid = strdup(tmp);
1854         if (!appid) {
1855                 ErrPrint("Heap: %s\n", strerror(errno));
1856                 sqlite3_reset(stmt);
1857                 sqlite3_finalize(stmt);
1858                 goto out;
1859         }
1860
1861         is_prime = sqlite3_column_int(stmt, 1);
1862
1863         sqlite3_reset(stmt);
1864         sqlite3_finalize(stmt);
1865 out:
1866         close_db(handle);
1867         return appid;
1868 }
1869
1870 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1871 {
1872         sqlite3_stmt *stmt;
1873         sqlite3 *handle;
1874         int ret;
1875         char *path;
1876         char *appid;
1877         char *lb_src;
1878
1879         if (!pkgid)
1880                 return NULL;
1881
1882         path = NULL;
1883         handle = open_db();
1884         if (!handle)
1885                 return NULL;
1886
1887         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1888         if (ret != SQLITE_OK) {
1889                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1890                 goto out;
1891         }
1892
1893         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1894         if (ret != SQLITE_OK) {
1895                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1896                 sqlite3_finalize(stmt);
1897                 goto out;
1898         }
1899
1900         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1901         if (ret != SQLITE_OK) {
1902                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1903                 sqlite3_finalize(stmt);
1904                 goto out;
1905         }
1906
1907         ret = sqlite3_step(stmt);
1908         if (ret != SQLITE_ROW) {
1909                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1910                 sqlite3_reset(stmt);
1911                 sqlite3_finalize(stmt);
1912                 goto out;
1913         }
1914
1915         appid = (char *)sqlite3_column_text(stmt, 0);
1916         if (!appid || !strlen(appid)) {
1917                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1918                 sqlite3_reset(stmt);
1919                 sqlite3_finalize(stmt);
1920                 goto out;
1921         }
1922
1923         lb_src = (char *)sqlite3_column_text(stmt, 1);
1924         if (!lb_src || !strlen(lb_src)) {
1925                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1926                 sqlite3_reset(stmt);
1927                 sqlite3_finalize(stmt);
1928                 goto out;
1929         }
1930
1931         path = strdup(lb_src);
1932         if (!path) {
1933                 ErrPrint("Heap: %s\n", strerror(errno));
1934                 sqlite3_reset(stmt);
1935                 sqlite3_finalize(stmt);
1936                 goto out;
1937         }
1938
1939         DbgPrint("LB Src: %s\n", path);
1940
1941         sqlite3_reset(stmt);
1942         sqlite3_finalize(stmt);
1943 out:
1944         close_db(handle);
1945         return path;
1946 }
1947
1948 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1949 {
1950         sqlite3_stmt *stmt;
1951         sqlite3 *handle;
1952         int ret;
1953         char *group;
1954         char *tmp;
1955
1956         if (!pkgid)
1957                 return NULL;
1958
1959         group = NULL;
1960         handle = open_db();
1961         if (!handle)
1962                 return NULL;
1963
1964         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1965         if (ret != SQLITE_OK) {
1966                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1967                 goto out;
1968         }
1969
1970         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1971         if (ret != SQLITE_OK) {
1972                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1973                 sqlite3_finalize(stmt);
1974                 goto out;
1975         }
1976
1977         ret = sqlite3_step(stmt);
1978         if (ret != SQLITE_ROW) {
1979                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1980                 sqlite3_reset(stmt);
1981                 sqlite3_finalize(stmt);
1982                 goto out;
1983         }
1984
1985         tmp = (char *)sqlite3_column_text(stmt, 0);
1986         if (tmp && strlen(tmp)) {
1987                 group = strdup(tmp);
1988                 if (!group)
1989                         ErrPrint("Heap: %s\n", strerror(errno));
1990         }
1991
1992         sqlite3_reset(stmt);
1993         sqlite3_finalize(stmt);
1994 out:
1995         close_db(handle);
1996         return group;
1997 }
1998
1999 EAPI char *livebox_service_pd_script_path(const char *pkgid)
2000 {
2001         sqlite3_stmt *stmt;
2002         sqlite3 *handle;
2003         int ret;
2004         char *path;
2005         char *pd_src;
2006         const char *appid;
2007
2008         if (!pkgid)
2009                 return NULL;
2010
2011         path = NULL;
2012         handle = open_db();
2013         if (!handle)
2014                 return NULL;
2015
2016         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
2017         if (ret != SQLITE_OK) {
2018                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2019                 goto out;
2020         }
2021
2022         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2023         if (ret != SQLITE_OK) {
2024                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2025                 sqlite3_finalize(stmt);
2026                 goto out;
2027         }
2028
2029         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
2030         if (ret != SQLITE_OK) {
2031                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2032                 sqlite3_finalize(stmt);
2033                 goto out;
2034         }
2035
2036         ret = sqlite3_step(stmt);
2037         if (ret != SQLITE_ROW) {
2038                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2039                 sqlite3_reset(stmt);
2040                 sqlite3_finalize(stmt);
2041                 goto out;
2042         }
2043
2044         appid = (char *)sqlite3_column_text(stmt, 0);
2045         if (!appid || !strlen(appid)) {
2046                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2047                 sqlite3_reset(stmt);
2048                 sqlite3_finalize(stmt);
2049                 goto out;
2050         }
2051
2052         pd_src = (char *)sqlite3_column_text(stmt, 1);
2053         if (!pd_src || !strlen(pd_src)) {
2054                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2055                 sqlite3_reset(stmt);
2056                 sqlite3_finalize(stmt);
2057                 goto out;
2058         }
2059
2060         path = strdup(pd_src);
2061         if (!path) {
2062                 ErrPrint("Heap: %s\n", strerror(errno));
2063                 sqlite3_reset(stmt);
2064                 sqlite3_finalize(stmt);
2065                 goto out;
2066         }
2067
2068         DbgPrint("PD Src: %s\n", path);
2069         sqlite3_reset(stmt);
2070         sqlite3_finalize(stmt);
2071 out:
2072         close_db(handle);
2073         return path;
2074 }
2075
2076 EAPI char *livebox_service_pd_script_group(const char *pkgid)
2077 {
2078         sqlite3_stmt *stmt;
2079         sqlite3 *handle;
2080         int ret;
2081         char *group;
2082         char *tmp;
2083
2084         if (!pkgid)
2085                 return NULL;
2086
2087         group = NULL;
2088         handle = open_db();
2089         if (!handle)
2090                 return NULL;
2091
2092         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2093         if (ret != SQLITE_OK) {
2094                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2095                 goto out;
2096         }
2097
2098         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2099         if (ret != SQLITE_OK) {
2100                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2101                 sqlite3_finalize(stmt);
2102                 goto out;
2103         }
2104
2105         ret = sqlite3_step(stmt);
2106         if (ret != SQLITE_ROW) {
2107                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2108                 sqlite3_reset(stmt);
2109                 sqlite3_finalize(stmt);
2110                 goto out;
2111         }
2112
2113         tmp = (char *)sqlite3_column_text(stmt, 0);
2114         if (tmp && strlen(tmp)) {
2115                 group = strdup(tmp);
2116                 if (!group)
2117                         ErrPrint("Heap: %s\n", strerror(errno));
2118         }
2119         sqlite3_reset(stmt);
2120         sqlite3_finalize(stmt);
2121 out:
2122         close_db(handle);
2123         return group;
2124 }
2125
2126 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
2127 {
2128         sqlite3_stmt *stmt;
2129         sqlite3 *handle;
2130         const char *cluster;
2131         int cnt;
2132         int ret;
2133
2134         if (!cb)
2135                 return LB_STATUS_ERROR_INVALID;
2136
2137         handle = open_db();
2138         if (!handle)
2139                 return LB_STATUS_ERROR_IO;
2140
2141         cnt = 0;
2142         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
2143         if (ret != SQLITE_OK) {
2144                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2145                 cnt = LB_STATUS_ERROR_IO;
2146                 goto out;
2147         }
2148
2149         while (sqlite3_step(stmt) == SQLITE_ROW) {
2150                 cluster = (const char *)sqlite3_column_text(stmt, 0);
2151                 if (!cluster || !strlen(cluster))
2152                         continue;
2153
2154                 if (cb(cluster, data) < 0)
2155                         break;
2156
2157                 cnt++;
2158         }
2159
2160         sqlite3_reset(stmt);
2161         sqlite3_finalize(stmt);
2162 out:
2163         close_db(handle);
2164         return cnt;
2165 }
2166
2167 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
2168 {
2169         sqlite3_stmt *stmt;
2170         sqlite3 *handle;
2171         const char *category;
2172         int cnt;
2173         int ret;
2174
2175         if (!cluster || !cb)
2176                 return LB_STATUS_ERROR_INVALID;
2177
2178         handle = open_db();
2179         if (!handle)
2180                 return LB_STATUS_ERROR_IO;
2181
2182         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
2183         if (ret != SQLITE_OK) {
2184                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2185                 cnt = LB_STATUS_ERROR_IO;
2186                 goto out;
2187         }
2188
2189         cnt = 0;
2190         while (sqlite3_step(stmt) == SQLITE_ROW) {
2191                 category = (const char *)sqlite3_column_text(stmt, 0);
2192                 if (!category || !strlen(category))
2193                         continue;
2194
2195                 if (cb(cluster, category, data) < 0)
2196                         break;
2197
2198                 cnt++;
2199         }
2200
2201         sqlite3_reset(stmt);
2202         sqlite3_finalize(stmt);
2203 out:
2204         close_db(handle);
2205         return cnt;
2206 }
2207
2208 EAPI int livebox_service_init(void)
2209 {
2210         if (s_info.handle) {
2211                 DbgPrint("Already initialized\n");
2212                 s_info.init_count++;
2213                 return 0;
2214         }
2215
2216         s_info.handle = open_db();
2217         if (s_info.handle) {
2218                 s_info.init_count++;
2219                 return 0;
2220         }
2221
2222         return LB_STATUS_ERROR_IO;
2223 }
2224
2225 EAPI int livebox_service_fini(void)
2226 {
2227         if (!s_info.handle || s_info.init_count <= 0) {
2228                 ErrPrint("Service is not initialized\n");
2229                 return LB_STATUS_ERROR_IO;
2230         }
2231
2232         s_info.init_count--;
2233         if (s_info.init_count > 0) {
2234                 DbgPrint("Init count %d\n", s_info.init_count);
2235                 return 0;
2236         }
2237
2238         db_util_close(s_info.handle);
2239         s_info.handle = NULL;
2240         return 0;
2241 }
2242
2243 EAPI int livebox_service_get_size(int type, int *width, int *height)
2244 {
2245         int _width;
2246         int _height;
2247
2248         if (!width)
2249                 width = &_width;
2250
2251         if (!height)
2252                 height = &_height;
2253
2254         return convert_size_from_type(type, width, height);
2255 }
2256
2257 EAPI int livebox_service_size_type(int width, int height)
2258 {
2259         int idx;
2260
2261         if (update_resolution() < 0)
2262                 ErrPrint("Failed to update the size list\n");
2263
2264         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
2265                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
2266                         break;
2267         }
2268
2269         switch (idx) {
2270         case 0:
2271                 return LB_SIZE_TYPE_1x1;
2272         case 1:
2273                 return LB_SIZE_TYPE_2x1;
2274         case 2:
2275                 return LB_SIZE_TYPE_2x2;
2276         case 3:
2277                 return LB_SIZE_TYPE_4x1;
2278         case 4:
2279                 return LB_SIZE_TYPE_4x2;
2280         case 5:
2281                 return LB_SIZE_TYPE_4x3;
2282         case 6:
2283                 return LB_SIZE_TYPE_4x4;
2284         case 7:
2285                 return LB_SIZE_TYPE_4x5;
2286         case 8:
2287                 return LB_SIZE_TYPE_4x6;
2288         case 9:
2289                 return LB_SIZE_TYPE_EASY_1x1;
2290         case 10:
2291                 return LB_SIZE_TYPE_EASY_3x1;
2292         case 11:
2293                 return LB_SIZE_TYPE_EASY_3x3;
2294         case 12:
2295                 return LB_SIZE_TYPE_0x0;
2296         default:
2297                 break;
2298         }
2299
2300         return LB_SIZE_TYPE_UNKNOWN;
2301 }
2302
2303 /* End of a file */