Add new API for getting the UI App list using lbid.
[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 int pkgmgr_get_applist(const char *pkgid, const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
611 {
612         struct pkgmgr_cbdata cbdata;
613         pkgmgrinfo_pkginfo_h handle;
614         int ret;
615
616         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
617         if (ret < 0) {
618                 ErrPrint("Unable to get pkginfo: %s\n", pkgid);
619                 return ret;
620         }
621
622         cbdata.lbid = lbid;
623         cbdata.cb = cb;
624         cbdata.cbdata = data;
625
626         ret = pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, pkgmgr_cb, &cbdata);
627         if (ret < 0)
628                 ErrPrint("Failed to get applist\n");
629
630         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
631         return ret;
632 }
633
634 EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
635 {
636         sqlite3_stmt *stmt;
637         const char *tmp;
638         char *pkgid;
639         sqlite3 *handle;
640         int ret;
641
642         if (!lbid || !cb)
643                 return LB_STATUS_ERROR_INVALID;
644
645         handle = open_db();
646         if (!handle)
647                 return LB_STATUS_ERROR_IO;
648
649         ret = sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL);
650         if (ret != SQLITE_OK) {
651                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
652                 ret = LB_STATUS_ERROR_IO;
653                 goto out;
654         }
655
656         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
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, 2, 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         if (sqlite3_step(stmt) != SQLITE_ROW) {
671                 ret = LB_STATUS_ERROR_INVALID;
672                 sqlite3_reset(stmt);
673                 sqlite3_finalize(stmt);
674                 goto out;
675         }
676
677         tmp = (const char *)sqlite3_column_text(stmt, 0);
678         if (!tmp || !strlen(tmp)) {
679                 ErrPrint("Invalid package name (%s)\n", lbid);
680                 ret = LB_STATUS_ERROR_INVALID;
681                 sqlite3_reset(stmt);
682                 sqlite3_finalize(stmt);
683                 goto out;
684         }
685
686         pkgid = strdup(tmp);
687         if (!pkgid) {
688                 ErrPrint("Error: %s\n", strerror(errno));
689                 ret = LB_STATUS_ERROR_MEMORY;
690                 sqlite3_reset(stmt);
691                 sqlite3_finalize(stmt);
692                 goto out;
693         }
694
695         sqlite3_reset(stmt);
696         sqlite3_finalize(stmt);
697
698         ret = pkgmgr_get_applist(pkgid, lbid, cb, data);
699         free(pkgid);
700
701 out:
702         close_db(handle);
703         return ret;
704 }
705
706 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
707 {
708         sqlite3_stmt *stmt;
709         sqlite3 *handle;
710         int size;
711         int ret;
712
713         if (!types || !cnt || !pkgid)
714                 return LB_STATUS_ERROR_INVALID;
715
716         handle = open_db();
717         if (!handle)
718                 return LB_STATUS_ERROR_IO;
719
720         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
721         if (ret != SQLITE_OK) {
722                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
723                 ret = LB_STATUS_ERROR_IO;
724                 goto out;
725         }
726
727         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
728         if (ret != SQLITE_OK) {
729                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
730                 sqlite3_reset(stmt);
731                 sqlite3_finalize(stmt);
732                 ret = LB_STATUS_ERROR_IO;
733                 goto out;
734         }
735
736         if (*cnt > NR_OF_SIZE_LIST)
737                 *cnt = NR_OF_SIZE_LIST;
738
739         ret = 0;
740         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
741                 size = sqlite3_column_int(stmt, 0);
742                 types[ret] = size;
743                 ret++;
744         }
745
746         *cnt = ret;
747         sqlite3_reset(stmt);
748         sqlite3_finalize(stmt);
749         ret = 0;
750 out:
751         close_db(handle);
752         return ret;
753 }
754
755 static inline char *cur_locale(void)
756 {
757         char *language;
758         language = vconf_get_str(VCONFKEY_LANGSET);
759         if (language) {
760                 char *ptr;
761
762                 ptr = language;
763                 while (*ptr) {
764                         if (*ptr == '.') {
765                                 *ptr = '\0';
766                                 break;
767                         }
768
769                         if (*ptr == '_')
770                                 *ptr = '-';
771
772                         ptr++;
773                 }
774         } else {
775                 language = strdup("en-us");
776                 if (!language)
777                         ErrPrint("Heap: %s\n", strerror(errno));
778         }
779
780         return language;
781 }
782
783 static inline char *get_default_name(const char *pkgid)
784 {
785         sqlite3_stmt *stmt;
786         sqlite3 *handle;
787         char *name = NULL;
788         int ret;
789
790         handle = open_db();
791         if (!handle)
792                 return NULL;
793
794         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
795         if (ret != SQLITE_OK) {
796                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
797                 close_db(handle);
798                 return NULL;
799         }
800
801         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
802         if (ret != SQLITE_OK) {
803                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
804                 goto out;
805         }
806
807         ret = sqlite3_step(stmt);
808         if (ret ==  SQLITE_ROW) {
809                 const char *tmp;
810
811                 tmp = (const char *)sqlite3_column_text(stmt, 0);
812                 if (tmp && strlen(tmp)) {
813                         name = strdup(tmp);
814                         if (!name)
815                                 ErrPrint("Heap: %s\n", strerror(errno));
816                 }
817         }
818
819 out:
820         sqlite3_reset(stmt);
821         sqlite3_finalize(stmt);
822         close_db(handle);
823         return name;
824 }
825
826 static inline char *get_default_icon(const char *pkgid)
827 {
828         sqlite3_stmt *stmt;
829         sqlite3 *handle;
830         char *icon = NULL;
831         int ret;
832
833         handle = open_db();
834         if (!handle)
835                 return NULL;
836
837         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
838         if (ret != SQLITE_OK) {
839                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
840                 close_db(handle);
841                 return NULL;
842         }
843
844         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
845         if (ret != SQLITE_OK) {
846                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
847                 goto out;
848         }
849
850         ret = sqlite3_step(stmt);
851         if (ret == SQLITE_ROW) {
852                 const char *tmp;
853
854                 tmp = (const char *)sqlite3_column_text(stmt, 0);
855                 if (tmp && strlen(tmp)) {
856                         icon = strdup(tmp);
857                         if (!icon)
858                                 ErrPrint("Heap: %s\n", strerror(errno));
859                 }
860         }
861
862 out:
863         sqlite3_reset(stmt);
864         sqlite3_finalize(stmt);
865         close_db(handle);
866         return icon;
867 }
868
869 EAPI char *livebox_service_content(const char *pkgid)
870 {
871         sqlite3_stmt *stmt;
872         sqlite3 *handle;
873         char *content = NULL;
874         int ret;
875
876         handle = open_db();
877         if (!handle)
878                 return NULL;
879
880         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
881         if (ret != SQLITE_OK) {
882                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
883                 close_db(handle);
884                 return NULL;
885         }
886
887         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
888         if (ret != SQLITE_OK) {
889                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
890                 goto out;
891         }
892
893         ret = sqlite3_step(stmt);
894         if (ret == SQLITE_ROW) {
895                 const char *tmp;
896
897                 tmp = (const char *)sqlite3_column_text(stmt, 0);
898                 if (tmp && strlen(tmp)) {
899                         content = strdup(tmp);
900                         if (!content)
901                                 ErrPrint("Heap: %s\n", strerror(errno));
902                 }
903         }
904
905 out:
906         sqlite3_reset(stmt);
907         sqlite3_finalize(stmt);
908         close_db(handle);
909         return content;
910 }
911
912 EAPI char *livebox_service_setup_appid(const char *lbid)
913 {
914         sqlite3_stmt *stmt;
915         sqlite3 *handle;
916         int ret;
917         char *appid;
918
919         handle = open_db();
920         if (!handle)
921                 return NULL;
922
923         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
924         if (ret != SQLITE_OK) {
925                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
926                 close_db(handle);
927                 return NULL;
928         }
929
930         appid = NULL;
931         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
932         if (ret != SQLITE_OK) {
933                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
934                 goto out;
935         }
936
937         ret = sqlite3_step(stmt);
938         if (ret == SQLITE_ROW) {
939                 const char *tmp;
940
941                 tmp = (const char *)sqlite3_column_text(stmt, 0);
942                 if (!tmp || !strlen(tmp))
943                         goto out;
944
945                 appid = strdup(tmp);
946                 if (!appid)
947                         ErrPrint("Error: %s\n", strerror(errno));
948         }
949
950 out:
951         sqlite3_reset(stmt);
952         sqlite3_finalize(stmt);
953         close_db(handle);
954         return appid;
955 }
956
957 EAPI int livebox_service_nodisplay(const char *pkgid)
958 {
959         sqlite3_stmt *stmt;
960         sqlite3 *handle;
961         int ret;
962
963         handle = open_db();
964         if (!handle)
965                 return 0;
966
967         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
968         if (ret != SQLITE_OK) {
969                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
970                 close_db(handle);
971                 return 0;
972         }
973
974         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
975         if (ret != SQLITE_OK) {
976                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
977                 ret = 0;
978                 goto out;
979         }
980
981         ret = sqlite3_step(stmt);
982         if (ret == SQLITE_ROW)
983                 ret = !!sqlite3_column_int(stmt, 0);
984         else
985                 ret = 0;
986
987 out:
988         sqlite3_reset(stmt);
989         sqlite3_finalize(stmt);
990         close_db(handle);
991         return ret;
992 }
993
994 static inline char *get_lb_pkgname_by_appid(const char *appid)
995 {
996         sqlite3_stmt *stmt;
997         char *pkgid;
998         char *tmp;
999         sqlite3 *handle;
1000         int ret;
1001
1002         if (!appid)
1003                 return NULL;
1004
1005         pkgid = NULL;
1006         handle = open_db();
1007         if (!handle)
1008                 return NULL;
1009
1010         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1011         if (ret != SQLITE_OK) {
1012                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1013                 close_db(handle);
1014                 return NULL;
1015         }
1016
1017         ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
1018         if (ret != SQLITE_OK) {
1019                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1020                 goto out;
1021         }
1022
1023         ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
1024         if (ret != SQLITE_OK) {
1025                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1026                 goto out;
1027         }
1028
1029         if (sqlite3_step(stmt) != SQLITE_ROW) {
1030                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1031                 goto out;
1032         }
1033
1034         tmp = (char *)sqlite3_column_text(stmt, 0);
1035         if (tmp && strlen(tmp)) {
1036                 pkgid = strdup(tmp);
1037                 if (!pkgid)
1038                         ErrPrint("Heap: %s\n", strerror(errno));
1039         }
1040
1041 out:
1042         sqlite3_reset(stmt);
1043         sqlite3_finalize(stmt);
1044         close_db(handle);
1045         return pkgid;
1046 }
1047
1048 EAPI int livebox_service_need_frame(const char *pkgid, int size_type)
1049 {
1050         char *lbid;
1051         sqlite3_stmt *stmt;
1052         sqlite3 *handle;
1053         int ret;
1054
1055         handle = open_db();
1056         if (!handle) {
1057                 ErrPrint("Unable to open a DB\n");
1058                 return 0;
1059         }
1060
1061         ret = sqlite3_prepare_v2(handle, "SELECT need_frame FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1062         if (ret != SQLITE_OK) {
1063                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1064                 close_db(handle);
1065                 return 0;
1066         }
1067
1068         /*!
1069          */
1070         lbid = livebox_service_pkgname(pkgid);
1071         if (!lbid) {
1072                 ErrPrint("Invalid appid (%s)\n", pkgid);
1073                 ret = 0;
1074                 goto out;
1075         }
1076
1077         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1078         free(lbid);
1079         if (ret != SQLITE_OK) {
1080                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1081                 ret = 0;
1082                 goto out;
1083         }
1084
1085         ret = sqlite3_bind_int(stmt, 2, size_type);
1086         if (ret != SQLITE_OK) {
1087                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1088                 ret = 0;
1089                 goto out;
1090         }
1091
1092         ret = sqlite3_step(stmt);
1093         if (ret == SQLITE_ROW) {
1094                 ret = !!sqlite3_column_int(stmt, 0);
1095         } else {
1096                 ret = 0;
1097                 ErrPrint("There is no such result\n");
1098         }
1099 out:
1100         sqlite3_reset(stmt);
1101         sqlite3_finalize(stmt);
1102         close_db(handle);
1103         return ret;
1104 }
1105
1106 EAPI int livebox_service_touch_effect(const char *pkgid, int size_type)
1107 {
1108         char *lbid;
1109         sqlite3_stmt *stmt;
1110         sqlite3 *handle;
1111         int ret;
1112
1113         handle = open_db();
1114         if (!handle) {
1115                 ErrPrint("Unable to open a DB\n");
1116                 return 1;
1117         }
1118
1119         ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1120         if (ret != SQLITE_OK) {
1121                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1122                 close_db(handle);
1123                 return 1;
1124         }
1125
1126         /*!
1127          * \note
1128          * This function will validate the "pkgid"
1129          * call the exported API in the exported API is not recomended
1130          * but... I used.
1131          */
1132         lbid = livebox_service_pkgname(pkgid);
1133         if (!lbid) {
1134                 ErrPrint("Invalid appid (%s)\n", pkgid);
1135                 ret = 1;
1136                 goto out;
1137         }
1138
1139         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1140         free(lbid);
1141         if (ret != SQLITE_OK) {
1142                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1143                 ret = 1;
1144                 goto out;
1145         }
1146
1147         ret = sqlite3_bind_int(stmt, 2, size_type);
1148         if (ret != SQLITE_OK) {
1149                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1150                 ret = 1;
1151                 goto out;
1152         }
1153
1154         ret = sqlite3_step(stmt);
1155         if (ret == SQLITE_ROW) {
1156                 ret = !!sqlite3_column_int(stmt, 0);
1157         } else {
1158                 ret = 1; /*!< Default true: In this case the DB is corrupted. */
1159                 ErrPrint("There is no result\n");
1160         }
1161
1162 out:
1163         sqlite3_reset(stmt);
1164         sqlite3_finalize(stmt);
1165         close_db(handle);
1166         return ret;
1167 }
1168
1169 EAPI int livebox_service_mouse_event(const char *pkgid)
1170 {
1171         sqlite3_stmt *stmt;
1172         sqlite3 *handle;
1173         char *lbid;
1174         int ret;
1175
1176         handle = open_db();
1177         if (!handle)
1178                 return 0;
1179
1180         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1181         if (ret != SQLITE_OK) {
1182                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1183                 close_db(handle);
1184                 return 0;
1185         }
1186
1187         lbid = livebox_service_pkgname(pkgid);
1188         if (!lbid) {
1189                 ErrPrint("Failed to get lbid: %s\n", pkgid);
1190                 ret = 0;
1191                 goto out;
1192         }
1193
1194         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1195         free(lbid);
1196         if (ret != SQLITE_OK) {
1197                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1198                 ret = 0;
1199                 goto out;
1200         }
1201
1202         ret = sqlite3_step(stmt);
1203         if (ret == SQLITE_ROW) {
1204                 ret = !!sqlite3_column_int(stmt, 0);
1205         } else {
1206                 ret = 0; /*!< Default is false, In this case the DB is corrupted */
1207                 ErrPrint("There is no result.\n");
1208         }
1209
1210 out:
1211         sqlite3_reset(stmt);
1212         sqlite3_finalize(stmt);
1213         close_db(handle);
1214         return ret;
1215 }
1216
1217 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
1218 {
1219         sqlite3_stmt *stmt;
1220         sqlite3 *handle;
1221         int ret;
1222         char *preview = NULL;
1223
1224         handle = open_db();
1225         if (!handle)
1226                 return NULL;
1227
1228         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1229         if (ret != SQLITE_OK) {
1230                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1231                 close_db(handle);
1232                 return NULL;
1233         }
1234
1235         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1236         if (ret != SQLITE_OK) {
1237                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1238                 goto out;
1239         }
1240
1241         ret = sqlite3_bind_int(stmt, 2, size_type);
1242         if (ret != SQLITE_OK) {
1243                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1244                 goto out;
1245         }
1246
1247         ret = sqlite3_step(stmt);
1248         if (ret == SQLITE_ROW) {
1249                 const char *tmp;
1250                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1251                 if (tmp && strlen(tmp)) {
1252                         preview = strdup(tmp);
1253                         if (!preview)
1254                                 ErrPrint("Heap: %s\n", strerror(errno));
1255                 }
1256         }
1257
1258 out:
1259         sqlite3_reset(stmt);
1260         sqlite3_finalize(stmt);
1261         close_db(handle);
1262         return preview;
1263 }
1264
1265 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1266 {
1267         sqlite3_stmt *stmt;
1268         sqlite3 *handle;
1269         char *language;
1270         char *icon = NULL;
1271         int ret;
1272
1273         if (lang) {
1274                 language = strdup(lang);
1275                 if (!language) {
1276                         ErrPrint("Heap: %s\n", strerror(errno));
1277                         return NULL;
1278                 }
1279         } else {
1280                 language = cur_locale();
1281                 if (!language)
1282                         return NULL;
1283         }
1284
1285         handle = open_db();
1286         if (!handle) {
1287                 free(language);
1288                 return NULL;
1289         }
1290
1291         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1292         if (ret != SQLITE_OK) {
1293                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1294                 close_db(handle);
1295                 free(language);
1296                 return NULL;
1297         }
1298
1299         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1300         if (ret != SQLITE_OK) {
1301                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1302                 goto out;
1303         }
1304
1305         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1306         if (ret != SQLITE_OK) {
1307                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1308                 goto out;
1309         }
1310
1311         ret = sqlite3_step(stmt);
1312         if (ret == SQLITE_ROW) {
1313                 const char *tmp;
1314                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1315                 if (!tmp || !strlen(tmp)) {
1316                         icon = get_default_icon(pkgid);
1317                 } else {
1318                         icon = strdup(tmp);
1319                         if (!icon)
1320                                 ErrPrint("Heap: %s\n", strerror(errno));
1321                 }
1322         } else {
1323                 icon = get_default_icon(pkgid);
1324         }
1325
1326 out:
1327         sqlite3_reset(stmt);
1328         sqlite3_finalize(stmt);
1329         close_db(handle);
1330         free(language);
1331         return icon;
1332 }
1333
1334 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1335 {
1336         sqlite3_stmt *stmt;
1337         sqlite3 *handle;
1338         char *language;
1339         char *name = NULL;
1340         int ret;
1341
1342         if (lang) {
1343                 language = strdup(lang);
1344                 if (!language) {
1345                         ErrPrint("Error: %s\n", strerror(errno));
1346                         return NULL;
1347                 }
1348         } else {
1349                 language = cur_locale();
1350                 if (!language)
1351                         return NULL;
1352         }
1353
1354         handle = open_db();
1355         if (!handle) {
1356                 free(language);
1357                 return NULL;
1358         }
1359
1360         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1361         if (ret != SQLITE_OK) {
1362                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1363                 close_db(handle);
1364                 free(language);
1365                 return NULL;
1366         }
1367
1368         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1369         if (ret != SQLITE_OK) {
1370                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1371                 goto out;
1372         }
1373
1374         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1375         if (ret != SQLITE_OK) {
1376                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1377                 goto out;
1378         }
1379
1380         ret = sqlite3_step(stmt);
1381         if (ret == SQLITE_ROW) {
1382                 const char *tmp;
1383                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1384                 if (!tmp || !strlen(tmp)) {
1385                         name = get_default_name(pkgid);
1386                 } else {
1387                         name = strdup(tmp);
1388                         if (!name)
1389                                 ErrPrint("Heap: %s\n", strerror(errno));
1390                 }
1391         } else {
1392                 name = get_default_name(pkgid);
1393         }
1394
1395 out:
1396         sqlite3_reset(stmt);
1397         sqlite3_finalize(stmt);
1398         close_db(handle);
1399         free(language);
1400         return name;
1401 }
1402
1403 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1404 {
1405         sqlite3_stmt *stmt;
1406         sqlite3 *handle;
1407         int size;
1408         int ret;
1409
1410         if (!w || !h || !cnt || !pkgid)
1411                 return LB_STATUS_ERROR_INVALID;
1412
1413         handle = open_db();
1414         if (!handle)
1415                 return LB_STATUS_ERROR_IO;
1416
1417         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1418         if (ret != SQLITE_OK) {
1419                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1420                 ret = LB_STATUS_ERROR_IO;
1421                 goto out;
1422         }
1423
1424         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1425         if (ret != SQLITE_OK) {
1426                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1427                 sqlite3_reset(stmt);
1428                 sqlite3_finalize(stmt);
1429                 ret = LB_STATUS_ERROR_IO;
1430                 goto out;
1431         }
1432
1433         if (*cnt > NR_OF_SIZE_LIST)
1434                 *cnt = NR_OF_SIZE_LIST;
1435
1436         ret = 0;
1437         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1438                 size = sqlite3_column_int(stmt, 0);
1439                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1440         }
1441
1442         *cnt = ret;
1443         sqlite3_reset(stmt);
1444         sqlite3_finalize(stmt);
1445         ret = 0;
1446 out:
1447         close_db(handle);
1448         return ret;
1449 }
1450
1451 EAPI char *livebox_service_libexec(const char *pkgid)
1452 {
1453         sqlite3_stmt *stmt;
1454         sqlite3 *handle;
1455         int ret;
1456         char *libexec;
1457         char *appid;
1458         char *path;
1459
1460         if (!pkgid)
1461                 return NULL;
1462
1463         libexec = NULL;
1464         handle = open_db();
1465         if (!handle)
1466                 return NULL;
1467
1468         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1469         if (ret != SQLITE_OK) {
1470                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1471                 goto out;
1472         }
1473
1474         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1475         if (ret != SQLITE_OK) {
1476                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1477                 sqlite3_finalize(stmt);
1478                 goto out;
1479         }
1480
1481         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1482         if (ret != SQLITE_OK) {
1483                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1484                 sqlite3_finalize(stmt);
1485                 goto out;
1486         }
1487
1488         if (sqlite3_step(stmt) != SQLITE_ROW) {
1489                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1490                 sqlite3_reset(stmt);
1491                 sqlite3_finalize(stmt);
1492
1493                 libexec = util_conf_get_libexec(pkgid);
1494                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1495                 goto out;
1496         }
1497
1498         appid = (char *)sqlite3_column_text(stmt, 0);
1499         if (!appid || !strlen(appid)) {
1500                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1501                 sqlite3_reset(stmt);
1502                 sqlite3_finalize(stmt);
1503                 goto out;
1504         }
1505
1506         path = (char *)sqlite3_column_text(stmt, 1);
1507         if (!path || !strlen(path)) {
1508                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1509                 sqlite3_reset(stmt);
1510                 sqlite3_finalize(stmt);
1511                 goto out;
1512         }
1513
1514         libexec = strdup(path);
1515         if (!libexec) {
1516                 ErrPrint("Heap: %s\n", strerror(errno));
1517                 sqlite3_reset(stmt);
1518                 sqlite3_finalize(stmt);
1519                 goto out;
1520         }
1521
1522         DbgPrint("libexec: %s\n", libexec);
1523
1524         sqlite3_reset(stmt);
1525         sqlite3_finalize(stmt);
1526 out:
1527         close_db(handle);
1528         return libexec;
1529 }
1530
1531 EAPI char *livebox_service_pkgname(const char *appid)
1532 {
1533         char *lb_pkgname;
1534         pkgmgr_appinfo_h handle;
1535         int ret;
1536         char *new_appid;
1537
1538         if (!appid)
1539                 return NULL;
1540
1541         lb_pkgname = get_lb_pkgname_by_appid(appid);
1542         if (lb_pkgname)
1543                 return lb_pkgname;
1544
1545         /*!
1546          * \note
1547          * Try to get the package id using given appid
1548          */
1549         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1550         if (ret != PKGMGR_R_OK) {
1551                 ErrPrint("Failed to get appinfo\n");
1552                 return NULL;
1553         }
1554
1555         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1556         if (ret != PKGMGR_R_OK) {
1557                 pkgmgr_appinfo_destroy_appinfo(handle);
1558                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1559                 return NULL;
1560         }
1561
1562         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1563         pkgmgr_appinfo_destroy_appinfo(handle);
1564
1565         if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1566                 return strdup(appid);
1567
1568         return lb_pkgname;
1569 }
1570
1571 EAPI char *livebox_service_provider_name(const char *lbid)
1572 {
1573         char *ret;
1574         int stage = 0;
1575         int seq = 0;
1576         int idx = 0;
1577         char *str = SAMSUNG_PREFIX;
1578
1579         if (!lbid)
1580                 return NULL;
1581
1582         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1583                 idx++;
1584                 if (seq < 2 && lbid[idx] == '.') {
1585                         stage = idx;
1586                         seq++;
1587                 }
1588         }
1589
1590         if (!str[idx] && lbid[idx]) {
1591                 /* Inhouse */
1592                 return strdup(lbid);
1593         } else if (seq < 2) {
1594                 while (seq < 2) {
1595                         if (lbid[idx] == '.') {
1596                                 seq++;
1597                         } else if (!lbid[idx]) {
1598                                 ErrPrint("Invalid lbid: %s\n", lbid);
1599                                 return NULL;
1600                         }
1601
1602                         idx++;
1603                 }
1604
1605                 stage = idx;
1606         } else {
1607                 stage++;
1608         }
1609
1610         ret = strdup(lbid + stage);
1611         if (!ret) {
1612                 ErrPrint("Error: %s\n", strerror(errno));
1613                 return NULL;
1614         }
1615
1616         return ret;
1617 }
1618
1619 EAPI int livebox_service_is_enabled(const char *lbid)
1620 {
1621         return 1;
1622         /*
1623         ail_appinfo_h ai;
1624         char *pkgname;
1625         bool enabled;
1626         int ret;
1627
1628         pkgname = livebox_service_appid(lbid);
1629         if (!pkgname)
1630                 return 0;
1631
1632         ret = ail_get_appinfo(pkgname, &ai);
1633         if (ret != AIL_ERROR_OK) {
1634                 free(pkgname);
1635                 return 0;
1636         }
1637
1638         if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
1639                 enabled = false;
1640
1641         ail_destroy_appinfo(ai);
1642         free(pkgname);
1643         return enabled == true;
1644         */
1645 }
1646
1647 EAPI int livebox_service_is_primary(const char *lbid)
1648 {
1649         sqlite3_stmt *stmt;
1650         sqlite3 *handle;
1651         int ret = 0;
1652
1653         if (!lbid)
1654                 return 0;
1655
1656         handle = open_db();
1657         if (!handle)
1658                 return 0;
1659
1660         ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
1661         if (ret != SQLITE_OK) {
1662                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1663                 close_db(handle);
1664                 return 0;
1665         }
1666
1667         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1668         if (ret != SQLITE_OK) {
1669                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1670                 goto out;
1671         }
1672
1673         ret = sqlite3_step(stmt);
1674         if (ret != SQLITE_ROW) {
1675                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1676                 goto out;
1677         }
1678
1679         ret = sqlite3_column_int(stmt, 0);
1680
1681 out:
1682         sqlite3_reset(stmt);
1683         sqlite3_finalize(stmt);
1684         close_db(handle);
1685         return ret;
1686 }
1687
1688 /*!
1689  * appid == Package ID
1690  * pkgid == Livebox ID
1691  */
1692 EAPI char *livebox_service_appid(const char *pkgname)
1693 {
1694         sqlite3_stmt *stmt;
1695         char *appid;
1696         char *tmp;
1697         sqlite3 *handle;
1698         int is_prime __attribute__((__unused__));
1699         int ret;
1700
1701         if (!pkgname)
1702                 return NULL;
1703
1704         appid = NULL;
1705         handle = open_db();
1706         if (!handle)
1707                 return NULL;
1708
1709         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1710         if (ret != SQLITE_OK) {
1711                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1712                 goto out;
1713         }
1714
1715         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
1716         if (ret != SQLITE_OK) {
1717                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1718                 sqlite3_reset(stmt);
1719                 sqlite3_finalize(stmt);
1720                 goto out;
1721         }
1722
1723         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
1724         if (ret != SQLITE_OK) {
1725                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1726                 sqlite3_reset(stmt);
1727                 sqlite3_finalize(stmt);
1728                 goto out;
1729         }
1730
1731         ret = sqlite3_step(stmt);
1732         if (ret != SQLITE_ROW) {
1733                 pkgmgr_appinfo_h pkg_handle;
1734                 char *new_appid;
1735
1736                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1737                 sqlite3_reset(stmt);
1738                 sqlite3_finalize(stmt);
1739
1740                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1741                 if (ret != PKGMGR_R_OK) {
1742                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
1743                         goto out;
1744                 }
1745
1746                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1747                 if (ret != PKGMGR_R_OK) {
1748                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
1749                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1750                         goto out;
1751                 }
1752
1753                 appid = strdup(new_appid);
1754                 if (!appid)
1755                         ErrPrint("Heap: %s\n", strerror(errno));
1756
1757                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1758                 goto out;
1759         }
1760
1761         tmp = (char *)sqlite3_column_text(stmt, 0);
1762         if (!tmp || !strlen(tmp)) {
1763                 ErrPrint("APPID is NIL\n");
1764                 sqlite3_reset(stmt);
1765                 sqlite3_finalize(stmt);
1766                 goto out;
1767         }
1768
1769         appid = strdup(tmp);
1770         if (!appid) {
1771                 ErrPrint("Heap: %s\n", strerror(errno));
1772                 sqlite3_reset(stmt);
1773                 sqlite3_finalize(stmt);
1774                 goto out;
1775         }
1776
1777         is_prime = sqlite3_column_int(stmt, 1);
1778
1779         sqlite3_reset(stmt);
1780         sqlite3_finalize(stmt);
1781 out:
1782         close_db(handle);
1783         return appid;
1784 }
1785
1786 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1787 {
1788         sqlite3_stmt *stmt;
1789         sqlite3 *handle;
1790         int ret;
1791         char *path;
1792         char *appid;
1793         char *lb_src;
1794
1795         if (!pkgid)
1796                 return NULL;
1797
1798         path = NULL;
1799         handle = open_db();
1800         if (!handle)
1801                 return NULL;
1802
1803         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1804         if (ret != SQLITE_OK) {
1805                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1806                 goto out;
1807         }
1808
1809         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1810         if (ret != SQLITE_OK) {
1811                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1812                 sqlite3_finalize(stmt);
1813                 goto out;
1814         }
1815
1816         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1817         if (ret != SQLITE_OK) {
1818                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1819                 sqlite3_finalize(stmt);
1820                 goto out;
1821         }
1822
1823         ret = sqlite3_step(stmt);
1824         if (ret != SQLITE_ROW) {
1825                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1826                 sqlite3_reset(stmt);
1827                 sqlite3_finalize(stmt);
1828                 goto out;
1829         }
1830
1831         appid = (char *)sqlite3_column_text(stmt, 0);
1832         if (!appid || !strlen(appid)) {
1833                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1834                 sqlite3_reset(stmt);
1835                 sqlite3_finalize(stmt);
1836                 goto out;
1837         }
1838
1839         lb_src = (char *)sqlite3_column_text(stmt, 1);
1840         if (!lb_src || !strlen(lb_src)) {
1841                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1842                 sqlite3_reset(stmt);
1843                 sqlite3_finalize(stmt);
1844                 goto out;
1845         }
1846
1847         path = strdup(lb_src);
1848         if (!path) {
1849                 ErrPrint("Heap: %s\n", strerror(errno));
1850                 sqlite3_reset(stmt);
1851                 sqlite3_finalize(stmt);
1852                 goto out;
1853         }
1854
1855         DbgPrint("LB Src: %s\n", path);
1856
1857         sqlite3_reset(stmt);
1858         sqlite3_finalize(stmt);
1859 out:
1860         close_db(handle);
1861         return path;
1862 }
1863
1864 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1865 {
1866         sqlite3_stmt *stmt;
1867         sqlite3 *handle;
1868         int ret;
1869         char *group;
1870         char *tmp;
1871
1872         if (!pkgid)
1873                 return NULL;
1874
1875         group = NULL;
1876         handle = open_db();
1877         if (!handle)
1878                 return NULL;
1879
1880         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1881         if (ret != SQLITE_OK) {
1882                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1883                 goto out;
1884         }
1885
1886         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1887         if (ret != SQLITE_OK) {
1888                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1889                 sqlite3_finalize(stmt);
1890                 goto out;
1891         }
1892
1893         ret = sqlite3_step(stmt);
1894         if (ret != SQLITE_ROW) {
1895                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1896                 sqlite3_reset(stmt);
1897                 sqlite3_finalize(stmt);
1898                 goto out;
1899         }
1900
1901         tmp = (char *)sqlite3_column_text(stmt, 0);
1902         if (tmp && strlen(tmp)) {
1903                 group = strdup(tmp);
1904                 if (!group)
1905                         ErrPrint("Heap: %s\n", strerror(errno));
1906         }
1907
1908         sqlite3_reset(stmt);
1909         sqlite3_finalize(stmt);
1910 out:
1911         close_db(handle);
1912         return group;
1913 }
1914
1915 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1916 {
1917         sqlite3_stmt *stmt;
1918         sqlite3 *handle;
1919         int ret;
1920         char *path;
1921         char *pd_src;
1922         const char *appid;
1923
1924         if (!pkgid)
1925                 return NULL;
1926
1927         path = NULL;
1928         handle = open_db();
1929         if (!handle)
1930                 return NULL;
1931
1932         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1933         if (ret != SQLITE_OK) {
1934                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1935                 goto out;
1936         }
1937
1938         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1939         if (ret != SQLITE_OK) {
1940                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1941                 sqlite3_finalize(stmt);
1942                 goto out;
1943         }
1944
1945         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1946         if (ret != SQLITE_OK) {
1947                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1948                 sqlite3_finalize(stmt);
1949                 goto out;
1950         }
1951
1952         ret = sqlite3_step(stmt);
1953         if (ret != SQLITE_ROW) {
1954                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1955                 sqlite3_reset(stmt);
1956                 sqlite3_finalize(stmt);
1957                 goto out;
1958         }
1959
1960         appid = (char *)sqlite3_column_text(stmt, 0);
1961         if (!appid || !strlen(appid)) {
1962                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1963                 sqlite3_reset(stmt);
1964                 sqlite3_finalize(stmt);
1965                 goto out;
1966         }
1967
1968         pd_src = (char *)sqlite3_column_text(stmt, 1);
1969         if (!pd_src || !strlen(pd_src)) {
1970                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1971                 sqlite3_reset(stmt);
1972                 sqlite3_finalize(stmt);
1973                 goto out;
1974         }
1975
1976         path = strdup(pd_src);
1977         if (!path) {
1978                 ErrPrint("Heap: %s\n", strerror(errno));
1979                 sqlite3_reset(stmt);
1980                 sqlite3_finalize(stmt);
1981                 goto out;
1982         }
1983
1984         DbgPrint("PD Src: %s\n", path);
1985         sqlite3_reset(stmt);
1986         sqlite3_finalize(stmt);
1987 out:
1988         close_db(handle);
1989         return path;
1990 }
1991
1992 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1993 {
1994         sqlite3_stmt *stmt;
1995         sqlite3 *handle;
1996         int ret;
1997         char *group;
1998         char *tmp;
1999
2000         if (!pkgid)
2001                 return NULL;
2002
2003         group = NULL;
2004         handle = open_db();
2005         if (!handle)
2006                 return NULL;
2007
2008         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2009         if (ret != SQLITE_OK) {
2010                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2011                 goto out;
2012         }
2013
2014         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2015         if (ret != SQLITE_OK) {
2016                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2017                 sqlite3_finalize(stmt);
2018                 goto out;
2019         }
2020
2021         ret = sqlite3_step(stmt);
2022         if (ret != SQLITE_ROW) {
2023                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2024                 sqlite3_reset(stmt);
2025                 sqlite3_finalize(stmt);
2026                 goto out;
2027         }
2028
2029         tmp = (char *)sqlite3_column_text(stmt, 0);
2030         if (tmp && strlen(tmp)) {
2031                 group = strdup(tmp);
2032                 if (!group)
2033                         ErrPrint("Heap: %s\n", strerror(errno));
2034         }
2035         sqlite3_reset(stmt);
2036         sqlite3_finalize(stmt);
2037 out:
2038         close_db(handle);
2039         return group;
2040 }
2041
2042 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
2043 {
2044         sqlite3_stmt *stmt;
2045         sqlite3 *handle;
2046         const char *cluster;
2047         int cnt;
2048         int ret;
2049
2050         if (!cb)
2051                 return LB_STATUS_ERROR_INVALID;
2052
2053         handle = open_db();
2054         if (!handle)
2055                 return LB_STATUS_ERROR_IO;
2056
2057         cnt = 0;
2058         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
2059         if (ret != SQLITE_OK) {
2060                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2061                 cnt = LB_STATUS_ERROR_IO;
2062                 goto out;
2063         }
2064
2065         while (sqlite3_step(stmt) == SQLITE_ROW) {
2066                 cluster = (const char *)sqlite3_column_text(stmt, 0);
2067                 if (!cluster || !strlen(cluster))
2068                         continue;
2069
2070                 if (cb(cluster, data) < 0)
2071                         break;
2072
2073                 cnt++;
2074         }
2075
2076         sqlite3_reset(stmt);
2077         sqlite3_finalize(stmt);
2078 out:
2079         close_db(handle);
2080         return cnt;
2081 }
2082
2083 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
2084 {
2085         sqlite3_stmt *stmt;
2086         sqlite3 *handle;
2087         const char *category;
2088         int cnt;
2089         int ret;
2090
2091         if (!cluster || !cb)
2092                 return LB_STATUS_ERROR_INVALID;
2093
2094         handle = open_db();
2095         if (!handle)
2096                 return LB_STATUS_ERROR_IO;
2097
2098         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
2099         if (ret != SQLITE_OK) {
2100                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2101                 cnt = LB_STATUS_ERROR_IO;
2102                 goto out;
2103         }
2104
2105         cnt = 0;
2106         while (sqlite3_step(stmt) == SQLITE_ROW) {
2107                 category = (const char *)sqlite3_column_text(stmt, 0);
2108                 if (!category || !strlen(category))
2109                         continue;
2110
2111                 if (cb(cluster, category, data) < 0)
2112                         break;
2113
2114                 cnt++;
2115         }
2116
2117         sqlite3_reset(stmt);
2118         sqlite3_finalize(stmt);
2119 out:
2120         close_db(handle);
2121         return cnt;
2122 }
2123
2124 EAPI int livebox_service_init(void)
2125 {
2126         if (s_info.handle) {
2127                 DbgPrint("Already initialized\n");
2128                 s_info.init_count++;
2129                 return 0;
2130         }
2131
2132         s_info.handle = open_db();
2133         if (s_info.handle) {
2134                 s_info.init_count++;
2135                 return 0;
2136         }
2137
2138         return LB_STATUS_ERROR_IO;
2139 }
2140
2141 EAPI int livebox_service_fini(void)
2142 {
2143         if (!s_info.handle || s_info.init_count <= 0) {
2144                 ErrPrint("Service is not initialized\n");
2145                 return LB_STATUS_ERROR_IO;
2146         }
2147
2148         s_info.init_count--;
2149         if (s_info.init_count > 0) {
2150                 DbgPrint("Init count %d\n", s_info.init_count);
2151                 return 0;
2152         }
2153
2154         db_util_close(s_info.handle);
2155         s_info.handle = NULL;
2156         return 0;
2157 }
2158
2159 EAPI int livebox_service_get_size(int type, int *width, int *height)
2160 {
2161         int _width;
2162         int _height;
2163
2164         if (!width)
2165                 width = &_width;
2166
2167         if (!height)
2168                 height = &_height;
2169
2170         return convert_size_from_type(type, width, height);
2171 }
2172
2173 EAPI int livebox_service_size_type(int width, int height)
2174 {
2175         int idx;
2176
2177         if (update_resolution() < 0)
2178                 ErrPrint("Failed to update the size list\n");
2179
2180         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
2181                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
2182                         break;
2183         }
2184
2185         switch (idx) {
2186         case 0:
2187                 return LB_SIZE_TYPE_1x1;
2188         case 1:
2189                 return LB_SIZE_TYPE_2x1;
2190         case 2:
2191                 return LB_SIZE_TYPE_2x2;
2192         case 3:
2193                 return LB_SIZE_TYPE_4x1;
2194         case 4:
2195                 return LB_SIZE_TYPE_4x2;
2196         case 5:
2197                 return LB_SIZE_TYPE_4x3;
2198         case 6:
2199                 return LB_SIZE_TYPE_4x4;
2200         case 7:
2201                 return LB_SIZE_TYPE_4x5;
2202         case 8:
2203                 return LB_SIZE_TYPE_4x6;
2204         case 9:
2205                 return LB_SIZE_TYPE_EASY_1x1;
2206         case 10:
2207                 return LB_SIZE_TYPE_EASY_3x1;
2208         case 11:
2209                 return LB_SIZE_TYPE_EASY_3x3;
2210         case 12:
2211                 return LB_SIZE_TYPE_0x0;
2212         default:
2213                 break;
2214         }
2215
2216         return LB_SIZE_TYPE_UNKNOWN;
2217 }
2218
2219 /* End of a file */