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