Update the livebox_service_provider_name function.
[platform/framework/web/livebox-service.git] / src / livebox-service.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.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 <vconf.h>
35 #include <vconf-keys.h>
36
37 #include "dlist.h"
38 #include "util.h"
39 #include "debug.h"
40 #include "livebox-service.h"
41
42 #define EAPI __attribute__((visibility("default")))
43 #define DEFAULT_TIMEOUT 2.0
44 #define MAX_COLUMN 80
45
46 static struct supported_size_list {
47         int w;
48         int h;
49 } SIZE_LIST[NR_OF_SIZE_LIST] = {
50         { 172, 172 }, /*!< 1x1 */
51         { 348, 172 }, /*!< 2x1 */
52         { 348, 348 }, /*!< 2x2 */
53         { 700, 172 }, /*!< 4x1 */
54         { 700, 348 }, /*!< 4x2 */
55         { 700, 520 }, /*!< 4x3 */
56         { 700, 700 }, /*!< 4x4 */
57         { 207, 207 }, /*!< 21x21 */
58         { 645, 207 }, /*!< 23x21 */
59         { 645, 645 }, /*!< 23x23 */
60 };
61
62 static struct info {
63         sqlite3 *handle;
64         const char *dbfile;
65         const char *conf_file;
66         int init_count;
67         int res_resolved;
68 } s_info = {
69         .handle = NULL,
70         .dbfile = "/opt/dbspace/.livebox.db", 
71         .conf_file = "/usr/share/data-provider-master/resolution.ini",
72         .init_count = 0,
73         .res_resolved = 0,
74 };
75
76 static inline int update_info(int width_type, int height_type, int width, int height)
77 {
78         int idx;
79
80         if (width_type == 1 && height_type == 1) {
81                 DbgPrint("1x1 Updated to %dx%d\n", width, height);
82                 idx = 0;
83         } else if (width_type == 2 && height_type == 1) {
84                 DbgPrint("2x1 Updated to %dx%d\n", width, height);
85                 idx = 1;
86         } else if (width_type == 2 && height_type == 2) {
87                 DbgPrint("2x2 Updated to %dx%d\n", width, height);
88                 idx = 2;
89         } else if (width_type == 4 && height_type == 1) {
90                 DbgPrint("4x1 Updated to %dx%d\n", width, height);
91                 idx = 3;
92         } else if (width_type == 4 && height_type == 2) {
93                 DbgPrint("4x2 Updated to %dx%d\n", width, height);
94                 idx = 4;
95         } else if (width_type == 4 && height_type == 3) {
96                 DbgPrint("4x3 Updated to %dx%d\n", width, height);
97                 idx = 5;
98         } else if (width_type == 4 && height_type == 4) {
99                 DbgPrint("4x4 Updated to %dx%d\n", width, height);
100                 idx = 6;
101         } else if (width_type == 21 && height_type == 21) {
102                 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
103                 idx = 7;
104         } else if (width_type == 23 && height_type == 21) {
105                 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
106                 idx = 8;
107         } else if (width_type == 23 && height_type == 23) {
108                 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
109                 idx = 9;
110         } else {
111                 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
112                 return 0;
113         }
114
115         SIZE_LIST[idx].w = width;
116         SIZE_LIST[idx].h = height;
117         return 1;
118 }
119
120 static inline int update_from_file(void)
121 {
122         FILE *fp;
123         int updated;
124         int width_type;
125         int height_type;
126         int width;
127         int height;
128         char buffer[MAX_COLUMN];
129         int ch;
130         int idx;
131         enum status {
132                 START = 0x0,
133                 TYPE = 0x01,
134                 SIZE = 0x02,
135                 COMMENT = 0x03,
136                 ERROR = 0x04,
137                 EOL = 0x05,
138                 TYPE_END = 0x06,
139                 SIZE_START = 0x07,
140         } status;
141
142         fp = fopen(s_info.conf_file, "r");
143         if (!fp) {
144                 ErrPrint("Open failed: %s\n", strerror(errno));
145                 return -EIO;
146         }
147
148         updated = 0;
149         status = START;
150         idx = 0;
151         do {
152                 ch = fgetc(fp);
153
154                 if (idx == MAX_COLUMN) {
155                         ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
156                         status = ERROR;
157                 }
158
159                 switch (status) {
160                 case START:
161                         if (isspace(ch) || ch == EOF)
162                                 continue;
163
164                         if (ch == '#') {
165                                 status = COMMENT;
166                         } else {
167                                 status = TYPE;
168                                 idx = 0;
169                                 ungetc(ch, fp);
170                         }
171                         break;
172                 case TYPE:
173                         if (isblank(ch)) {
174                                 buffer[idx] = '\0';
175                                 status = TYPE_END;
176                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
177                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
178                                         status = ERROR;
179                                 }
180                                 break;
181                         } else if (ch == '=') {
182                                 buffer[idx] = '\0';
183                                 status = SIZE_START;
184                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
185                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
186                                         status = ERROR;
187                                 }
188                                 break;
189                         } else if (ch == EOF) {
190                                 ErrPrint("Invalid Syntax\n");
191                                 status = ERROR;
192                                 continue;
193                         }
194                         buffer[idx++] = ch;
195                         break;
196                 case TYPE_END:
197                         if (ch == '=')
198                                 status = SIZE_START;
199                         break;
200                 case SIZE_START:
201                         if (isspace(ch) || ch == EOF)
202                                 continue;
203
204                         status = SIZE;
205                         idx = 0;
206                         ungetc(ch, fp);
207                         break;
208                 case SIZE:
209                         if (isspace(ch) || ch == EOF) {
210                                 buffer[idx] = '\0';
211                                 status = EOL;
212
213                                 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
214                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
215                                         status = ERROR;
216                                 } else if (ch == EOF) {
217                                         updated += update_info(width_type, height_type, width, height);
218                                 }
219                                 break;
220                         }
221                         buffer[idx++] = ch;
222                         break;
223                 case EOL:
224                         updated += update_info(width_type, height_type, width, height);
225                         status = START;
226                         ungetc(ch, fp);
227                         break;
228                 case ERROR:
229                         if (ch == '\n' || ch == '\r' || ch == '\f')
230                                 status = START;
231                         break;
232                 case COMMENT:
233                         if (ch == '\n' || ch == '\r' || ch == '\f')
234                                 status = START;
235                         break;
236                 default:
237                         ErrPrint("Unknown status. couldn't be reach to here\n");
238                         break;
239                 }
240         } while (!feof(fp));
241         fclose(fp);
242
243         return NR_OF_SIZE_LIST - updated;
244 }
245
246 static int update_resolution(void)
247 {
248         Display *disp;
249         Window root;
250         Window dummy;
251         int x, y;
252         unsigned int width;
253         unsigned int height;
254         unsigned int border;
255         unsigned int depth;
256         register int i;
257
258         if (s_info.res_resolved)
259                 return 0;
260
261         disp = XOpenDisplay(NULL);
262         if (!disp) {
263                 ErrPrint("Failed to open a display\n");
264                 return -EFAULT;
265         }
266
267         root = XDefaultRootWindow(disp);
268         if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
269                 XCloseDisplay(disp);
270                 return -EFAULT;
271         }
272
273         if (update_from_file() == 0)
274                 DbgPrint("Resolution info is all updated by file\n");
275
276         DbgPrint("Screen resolution: %dx%d\n", width, height);
277         for (i = 0; i < NR_OF_SIZE_LIST; i++) {
278                 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
279                 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)height / 1280.0f);
280                 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
281         }
282
283         XCloseDisplay(disp);
284         s_info.res_resolved = 1;
285         return 0;
286 }
287
288 static inline sqlite3 *open_db(void)
289 {
290         sqlite3 *handle;
291
292         if (!s_info.handle) {
293                 int ret;
294
295                 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
296                 if (ret != SQLITE_OK) {
297                         ErrPrint("Failed to open a DB\n");
298                         return NULL;
299                 }
300         } else {
301                 handle = s_info.handle;
302         }
303
304         return handle;
305 }
306
307 static inline void close_db(sqlite3 *handle)
308 {
309         if (!s_info.handle)
310                 db_util_close(handle);
311 }
312
313 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
314 {
315         int idx;
316
317         switch (type) {
318         case LB_SIZE_TYPE_1x1: /*!< 172x172 */
319                 idx = 0;
320                 break;
321         case LB_SIZE_TYPE_2x1: /*!< 348x172 */
322                 idx = 1;
323                 break;
324         case LB_SIZE_TYPE_2x2: /*!< 348x348 */
325                 idx = 2;
326                 break;
327         case LB_SIZE_TYPE_4x1: /*!< 700x172 */
328                 idx = 3;
329                 break;
330         case LB_SIZE_TYPE_4x2: /*!< 700x348 */
331                 idx = 4;
332                 break;
333         case LB_SIZE_TYPE_4x3: /*!< 700x520 */
334                 idx = 5;
335                 break;
336         case LB_SIZE_TYPE_4x4: /*!< 700x700 */
337                 idx = 6;
338                 break;
339         case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
340                 idx = 7;
341                 break;
342         case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
343                 idx = 8;
344                 break;
345         case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
346                 idx = 9;
347                 break;
348         default:
349                 return -EINVAL;
350         }
351
352         if (update_resolution() < 0)
353                 ErrPrint("Failed to update resolution\n");
354
355         *width = SIZE_LIST[idx].w;
356         *height = SIZE_LIST[idx].h;
357         return 0;
358 }
359
360 EAPI int livebox_service_trigger_update(const char *pkgname, const char *cluster, const char *category)
361 {
362         struct packet *packet;
363         struct packet *result;
364         int ret;
365
366         if (!pkgname) {
367                 ErrPrint("Invalid argument\n");
368                 return -EINVAL;
369         }
370
371         if (access("/tmp/.live.paused", R_OK) == 0) {
372                 DbgPrint("Provider is paused\n");
373                 return -ECANCELED;
374         }
375
376         if (!cluster)
377                 cluster = "user,created";
378
379         if (!category)
380                 category = "default";
381
382         packet = packet_create("service_update", "sss", pkgname, cluster, category);
383         if (!packet) {
384                 ErrPrint("Failed to create a packet for service_update\n");
385                 return -EFAULT;
386         }
387
388         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
389         packet_unref(packet);
390
391         if (result) {
392                 if (packet_get(result, "i", &ret) != 1) {
393                         ErrPrint("Failed to parse a result packet\n");
394                         ret = -EINVAL;
395                 }
396
397                 packet_unref(result);
398         } else {
399                 ErrPrint("Failed to get result packet\n");
400                 ret = -EFAULT;
401         }
402
403         return ret;
404 }
405
406 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
407 {
408         int ret;
409         sqlite3_stmt *stmt;
410         char *appid;
411         char *pkgid;
412         int is_prime;
413         sqlite3 *handle;
414
415         if (!cb)
416                 return -EINVAL;
417
418         handle = open_db();
419         if (!handle)
420                 return -EIO;
421
422         ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
423         if (ret != SQLITE_OK) {
424                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
425                 ret = -EIO;
426                 goto out;
427         }
428
429         ret = 0;
430         while (sqlite3_step(stmt) == SQLITE_ROW) {
431                 appid = (char *)sqlite3_column_text(stmt, 0);
432                 if (!appid || !strlen(appid)) {
433                         ErrPrint("APPID is not valid\n");
434                         continue;
435                 }
436
437                 pkgid = (char *)sqlite3_column_text(stmt, 1);
438                 if (!pkgid || !strlen(pkgid)) {
439                         ErrPrint("pkgid is not valid\n");
440                         continue;
441                 }
442
443                 is_prime = sqlite3_column_int(stmt, 2);
444
445                 ret++;
446
447                 if (cb(appid, pkgid, is_prime, data) < 0) {
448                         DbgPrint("Callback stopped package crawling\n");
449                         break;
450                 }
451         }
452
453         sqlite3_reset(stmt);
454         sqlite3_finalize(stmt);
455
456 out:
457         close_db(handle);
458         return ret;
459 }
460
461 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
462 {
463         sqlite3_stmt *stmt;
464         sqlite3 *handle;
465         int size;
466         int ret;
467
468         if (!types || !cnt || !pkgid)
469                 return -EINVAL;
470
471         handle = open_db();
472         if (!handle)
473                 return -EIO;
474
475         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
476         if (ret != SQLITE_OK) {
477                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
478                 ret = -EIO;
479                 goto out;
480         }
481
482         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
483         if (ret != SQLITE_OK) {
484                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
485                 sqlite3_reset(stmt);
486                 sqlite3_finalize(stmt);
487                 ret = -EIO;
488                 goto out;
489         }
490
491         if (*cnt > NR_OF_SIZE_LIST)
492                 *cnt = NR_OF_SIZE_LIST;
493
494         ret = 0;
495         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
496                 size = sqlite3_column_int(stmt, 0);
497                 types[ret] = size;
498                 ret++;
499         }
500
501         *cnt = ret;
502         sqlite3_reset(stmt);
503         sqlite3_finalize(stmt);
504         ret = 0;
505 out:
506         close_db(handle);
507         return ret;
508 }
509
510 static inline char *cur_locale(void)
511 {
512         char *language;
513         language = vconf_get_str(VCONFKEY_LANGSET);
514         if (language) {
515                 char *ptr;
516
517                 ptr = language;
518                 while (*ptr) {
519                         if (*ptr == '.') {
520                                 *ptr = '\0';
521                                 break;
522                         }
523
524                         if (*ptr == '_')
525                                 *ptr = '-';
526
527                         ptr++;
528                 }
529         } else {
530                 language = strdup("en-us");
531                 if (!language)
532                         ErrPrint("Heap: %s\n", strerror(errno));
533         }
534
535         return language;
536 }
537
538 static inline char *get_default_name(const char *pkgid)
539 {
540         sqlite3_stmt *stmt;
541         sqlite3 *handle;
542         char *name = NULL;
543         int ret;
544
545         handle = open_db();
546         if (!handle)
547                 return NULL;
548
549         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
550         if (ret != SQLITE_OK) {
551                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
552                 close_db(handle);
553                 return NULL;
554         }
555
556         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
557         if (ret != SQLITE_OK) {
558                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
559                 goto out;
560         }
561
562         ret = sqlite3_step(stmt);
563         if (ret ==  SQLITE_ROW) {
564                 const char *tmp;
565
566                 tmp = (const char *)sqlite3_column_text(stmt, 0);
567                 if (tmp && strlen(tmp)) {
568                         name = strdup(tmp);
569                         if (!name)
570                                 ErrPrint("Heap: %s\n", strerror(errno));
571                 }
572         }
573
574 out:
575         sqlite3_reset(stmt);
576         sqlite3_finalize(stmt);
577         close_db(handle);
578         return name;
579 }
580
581 static inline char *get_default_icon(const char *pkgid)
582 {
583         sqlite3_stmt *stmt;
584         sqlite3 *handle;
585         char *icon = NULL;
586         int ret;
587
588         handle = open_db();
589         if (!handle)
590                 return NULL;
591
592         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
593         if (ret != SQLITE_OK) {
594                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
595                 close_db(handle);
596                 return NULL;
597         }
598
599         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
600         if (ret != SQLITE_OK) {
601                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
602                 goto out;
603         }
604
605         ret = sqlite3_step(stmt);
606         if (ret == SQLITE_ROW) {
607                 const char *tmp;
608
609                 tmp = (const char *)sqlite3_column_text(stmt, 0);
610                 if (tmp && strlen(tmp)) {
611                         icon = strdup(tmp);
612                         if (!icon)
613                                 ErrPrint("Heap: %s\n", strerror(errno));
614                 }
615         }
616
617 out:
618         sqlite3_reset(stmt);
619         sqlite3_finalize(stmt);
620         close_db(handle);
621         return icon;
622 }
623
624 EAPI char *livebox_service_content(const char *pkgid)
625 {
626         sqlite3_stmt *stmt;
627         sqlite3 *handle;
628         char *content = NULL;
629         int ret;
630
631         handle = open_db();
632         if (!handle)
633                 return NULL;
634
635         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
636         if (ret != SQLITE_OK) {
637                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
638                 close_db(handle);
639                 return NULL;
640         }
641
642         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
643         if (ret != SQLITE_OK) {
644                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
645                 goto out;
646         }
647
648         ret = sqlite3_step(stmt);
649         if (ret == SQLITE_ROW) {
650                 const char *tmp;
651
652                 tmp = (const char *)sqlite3_column_text(stmt, 0);
653                 if (tmp && strlen(tmp)) {
654                         content = strdup(tmp);
655                         if (!content)
656                                 ErrPrint("Heap: %s\n", strerror(errno));
657                 }
658         }
659
660 out:
661         sqlite3_reset(stmt);
662         sqlite3_finalize(stmt);
663         close_db(handle);
664         return content;
665 }
666
667 EAPI char *livebox_service_setup_appid(const char *lbid)
668 {
669         sqlite3_stmt *stmt;
670         sqlite3 *handle;
671         int ret;
672         char *appid;
673
674         handle = open_db();
675         if (!handle)
676                 return NULL;
677
678         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
679         if (ret != SQLITE_OK) {
680                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
681                 close_db(handle);
682                 return NULL;
683         }
684
685         appid = NULL;
686         ret = sqlite3_bind_text(stmt, 1, lbid, -1, NULL);
687         if (ret != SQLITE_OK) {
688                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
689                 goto out;
690         }
691
692         ret = sqlite3_step(stmt);
693         if (ret == SQLITE_ROW) {
694                 const char *tmp;
695
696                 tmp = (const char *)sqlite3_column_text(stmt, 0);
697                 if (!tmp || !strlen(tmp))
698                         goto out;
699
700                 appid = strdup(tmp);
701                 if (!appid)
702                         ErrPrint("Error: %s\n", strerror(errno));
703         }
704
705 out:
706         sqlite3_reset(stmt);
707         sqlite3_finalize(stmt);
708         close_db(handle);
709         return appid;
710 }
711
712 EAPI int livebox_service_nodisplay(const char *pkgid)
713 {
714         sqlite3_stmt *stmt;
715         sqlite3 *handle;
716         int ret;
717
718         handle = open_db();
719         if (!handle)
720                 return 0;
721
722         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
723         if (ret != SQLITE_OK) {
724                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
725                 close_db(handle);
726                 return 0;
727         }
728
729         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
730         if (ret != SQLITE_OK) {
731                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
732                 ret = 0;
733                 goto out;
734         }
735
736         ret = sqlite3_step(stmt);
737         if (ret == SQLITE_ROW) 
738                 ret = !!sqlite3_column_int(stmt, 0);
739         else
740                 ret = 0;
741
742 out:
743         sqlite3_reset(stmt);
744         sqlite3_finalize(stmt);
745         close_db(handle);
746         return ret;
747 }
748
749 EAPI int livebox_service_mouse_event(const char *pkgid)
750 {
751         sqlite3_stmt *stmt;
752         sqlite3 *handle;
753         int ret;
754
755         handle = open_db();
756         if (!handle)
757                 return 0;
758
759         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
760         if (ret != SQLITE_OK) {
761                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
762                 close_db(handle);
763                 return 0;
764         }
765
766         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
767         if (ret != SQLITE_OK) {
768                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
769                 ret = 0;
770                 goto out;
771         }
772
773         ret = sqlite3_step(stmt);
774         if (ret == SQLITE_ROW)
775                 ret = !!sqlite3_column_int(stmt, 0);
776         else
777                 ret = 0;
778
779 out:
780         sqlite3_reset(stmt);
781         sqlite3_finalize(stmt);
782         close_db(handle);
783         return ret;
784 }
785
786 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
787 {
788         sqlite3_stmt *stmt;
789         sqlite3 *handle;
790         int ret;
791         char *preview = NULL;
792
793         handle = open_db();
794         if (!handle)
795                 return NULL;
796
797         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
798         if (ret != SQLITE_OK) {
799                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
800                 close_db(handle);
801                 return NULL;
802         }
803
804         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
805         if (ret != SQLITE_OK) {
806                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
807                 goto out;
808         }
809
810         ret = sqlite3_bind_int(stmt, 2, size_type);
811         if (ret != SQLITE_OK) {
812                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
813                 goto out;
814         }
815
816         ret = sqlite3_step(stmt);
817         if (ret == SQLITE_ROW) {
818                 const char *tmp;
819                 tmp = (const char *)sqlite3_column_text(stmt, 0);
820                 if (tmp && strlen(tmp)) {
821                         preview = strdup(tmp);
822                         if (!preview)
823                                 ErrPrint("Heap: %s\n", strerror(errno));
824                 }
825         }
826
827 out:
828         sqlite3_reset(stmt);
829         sqlite3_finalize(stmt);
830         close_db(handle);
831         return preview;
832 }
833
834 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
835 {
836         sqlite3_stmt *stmt;
837         sqlite3 *handle;
838         char *language;
839         char *icon = NULL;
840         int ret;
841
842         if (lang) {
843                 language = strdup(lang);
844                 if (!language) {
845                         ErrPrint("Heap: %s\n", strerror(errno));
846                         return NULL;
847                 }
848         } else {
849                 language = cur_locale();
850                 if (!language)
851                         return NULL;
852         }
853
854         handle = open_db();
855         if (!handle) {
856                 free(language);
857                 return NULL;
858         }
859
860         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
861         if (ret != SQLITE_OK) {
862                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
863                 close_db(handle);
864                 free(language);
865                 return NULL;
866         }
867
868         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
869         if (ret != SQLITE_OK) {
870                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
871                 goto out;
872         }
873
874         ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
875         if (ret != SQLITE_OK) {
876                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
877                 goto out;
878         }
879
880         ret = sqlite3_step(stmt);
881         if (ret == SQLITE_ROW) {
882                 const char *tmp;
883                 tmp = (const char *)sqlite3_column_text(stmt, 0);
884                 if (!tmp || !strlen(tmp)) {
885                         icon = get_default_icon(pkgid);
886                 } else {
887                         icon = strdup(tmp);
888                         if (!icon)
889                                 ErrPrint("Heap: %s\n", strerror(errno));
890                 }
891         } else {
892                 icon = get_default_icon(pkgid);
893         }
894
895 out:
896         sqlite3_reset(stmt);
897         sqlite3_finalize(stmt);
898         close_db(handle);
899         free(language);
900         return icon;
901 }
902
903 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
904 {
905         sqlite3_stmt *stmt;
906         sqlite3 *handle;
907         char *language;
908         char *name = NULL;
909         int ret;
910
911         if (lang) {
912                 language = strdup(lang);
913                 if (!language) {
914                         ErrPrint("Error: %s\n", strerror(errno));
915                         return NULL;
916                 }
917         } else {
918                 language = cur_locale();
919                 if (!language)
920                         return NULL;
921         }
922
923         handle = open_db();
924         if (!handle) {
925                 free(language);
926                 return NULL;
927         }
928
929         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
930         if (ret != SQLITE_OK) {
931                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
932                 close_db(handle);
933                 free(language);
934                 return NULL;
935         }
936
937         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
938         if (ret != SQLITE_OK) {
939                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
940                 goto out;
941         }
942
943         ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
944         if (ret != SQLITE_OK) {
945                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
946                 goto out;
947         }
948
949         ret = sqlite3_step(stmt);
950         if (ret == SQLITE_ROW) {
951                 const char *tmp;
952                 tmp = (const char *)sqlite3_column_text(stmt, 0);
953                 if (!tmp || !strlen(tmp)) {
954                         name = get_default_name(pkgid);
955                 } else {
956                         name = strdup(tmp);
957                         if (!name)
958                                 ErrPrint("Heap: %s\n", strerror(errno));
959                 }
960         } else {
961                 name = get_default_name(pkgid);
962         }
963
964 out:
965         sqlite3_reset(stmt);
966         sqlite3_finalize(stmt);
967         close_db(handle);
968         free(language);
969         return name;
970 }
971
972 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
973 {
974         sqlite3_stmt *stmt;
975         sqlite3 *handle;
976         int size;
977         int ret;
978
979         if (!w || !h || !cnt || !pkgid)
980                 return -EINVAL;
981
982         handle = open_db();
983         if (!handle)
984                 return -EIO;
985
986         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
987         if (ret != SQLITE_OK) {
988                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
989                 ret = -EIO;
990                 goto out;
991         }
992
993         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
994         if (ret != SQLITE_OK) {
995                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
996                 sqlite3_reset(stmt);
997                 sqlite3_finalize(stmt);
998                 ret = -EIO;
999                 goto out;
1000         }
1001
1002         if (*cnt > NR_OF_SIZE_LIST)
1003                 *cnt = NR_OF_SIZE_LIST;
1004
1005         ret = 0;
1006         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1007                 size = sqlite3_column_int(stmt, 0);
1008                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1009         }
1010
1011         *cnt = ret;
1012         sqlite3_reset(stmt);
1013         sqlite3_finalize(stmt);
1014         ret = 0;
1015 out:
1016         close_db(handle);
1017         return ret;
1018 }
1019
1020 EAPI char *livebox_service_libexec(const char *pkgid)
1021 {
1022         sqlite3_stmt *stmt;
1023         sqlite3 *handle;
1024         int ret;
1025         char *libexec;
1026         char *appid;
1027         char *path;
1028
1029         if (!pkgid)
1030                 return NULL;
1031
1032         libexec = NULL;
1033         handle = open_db();
1034         if (!handle)
1035                 return NULL;
1036
1037         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1038         if (ret != SQLITE_OK) {
1039                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1040                 goto out;
1041         }
1042
1043         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1044         if (ret != SQLITE_OK) {
1045                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1046                 sqlite3_finalize(stmt);
1047                 goto out;
1048         }
1049
1050         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1051         if (ret != SQLITE_OK) {
1052                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1053                 sqlite3_finalize(stmt);
1054                 goto out;
1055         }
1056
1057         if (sqlite3_step(stmt) != SQLITE_ROW) {
1058                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1059                 sqlite3_reset(stmt);
1060                 sqlite3_finalize(stmt);
1061
1062                 libexec = util_conf_get_libexec(pkgid);
1063                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1064                 goto out;
1065         }
1066
1067         appid = (char *)sqlite3_column_text(stmt, 0);
1068         if (!appid || !strlen(appid)) {
1069                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1070                 sqlite3_reset(stmt);
1071                 sqlite3_finalize(stmt);
1072                 goto out;
1073         }
1074
1075         path = (char *)sqlite3_column_text(stmt, 1);
1076         if (!path || !strlen(path)) {
1077                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1078                 sqlite3_reset(stmt);
1079                 sqlite3_finalize(stmt);
1080                 goto out;
1081         }
1082
1083         libexec = strdup(path);
1084         if (!libexec) {
1085                 ErrPrint("Heap: %s\n", strerror(errno));
1086                 sqlite3_reset(stmt);
1087                 sqlite3_finalize(stmt);
1088                 goto out;
1089         }
1090
1091         DbgPrint("libexec: %s\n", libexec);
1092
1093         sqlite3_reset(stmt);
1094         sqlite3_finalize(stmt);
1095 out:
1096         close_db(handle);
1097         return libexec;
1098 }
1099
1100 static inline char *get_lb_pkgname_by_appid(const char *appid)
1101 {
1102         sqlite3_stmt *stmt;
1103         char *pkgid;
1104         char *tmp;
1105         sqlite3 *handle;
1106         int ret;
1107
1108         if (!appid)
1109                 return NULL;
1110
1111         pkgid = NULL;
1112         handle = open_db();
1113         if (!handle)
1114                 return NULL;
1115
1116         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1117         if (ret != SQLITE_OK) {
1118                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1119                 close_db(handle);
1120                 return NULL;
1121         }
1122
1123         ret = sqlite3_bind_text(stmt, 1, appid, -1, NULL);
1124         if (ret != SQLITE_OK) {
1125                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1126                 goto out;
1127         }
1128
1129         ret = sqlite3_bind_text(stmt, 2, appid, -1, NULL);
1130         if (ret != SQLITE_OK) {
1131                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1132                 goto out;
1133         }
1134
1135         if (sqlite3_step(stmt) != SQLITE_ROW) {
1136                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1137                 goto out;
1138         }
1139
1140         tmp = (char *)sqlite3_column_text(stmt, 0);
1141         if (tmp && strlen(tmp)) {
1142                 pkgid = strdup(tmp);
1143                 if (!pkgid)
1144                         ErrPrint("Heap: %s\n", strerror(errno));
1145         }
1146
1147 out:
1148         sqlite3_reset(stmt);
1149         sqlite3_finalize(stmt);
1150         close_db(handle);
1151         return pkgid;
1152 }
1153
1154 EAPI char *livebox_service_pkgname(const char *appid)
1155 {
1156         char *lb_pkgname;
1157         pkgmgr_appinfo_h handle;
1158         int ret;
1159         char *new_appid;
1160
1161         if (!appid)
1162                 return NULL;
1163
1164         lb_pkgname = get_lb_pkgname_by_appid(appid);
1165         if (lb_pkgname)
1166                 return lb_pkgname;
1167
1168         /*!
1169          * \note
1170          * Try to get the package id using given appid
1171          */
1172         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1173         if (ret != PKGMGR_R_OK) {
1174                 ErrPrint("Failed to get appinfo\n");
1175                 return NULL;
1176         }
1177
1178         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1179         if (ret != PKGMGR_R_OK) {
1180                 pkgmgr_appinfo_destroy_appinfo(handle);
1181                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1182                 return NULL;
1183         }
1184
1185         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1186         pkgmgr_appinfo_destroy_appinfo(handle);
1187
1188         if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1189                 return strdup(appid);
1190
1191         return lb_pkgname;
1192 }
1193
1194 EAPI char *livebox_service_provider_name(const char *lbid)
1195 {
1196         char *ret;
1197         int stage = 0;
1198         int seq = 0;
1199         int idx = 0;
1200         char *str = "com.samsung.";
1201
1202         if (!lbid)
1203                 return NULL;
1204
1205         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1206                 idx++;
1207                 if (seq < 2 && lbid[idx] == '.') {
1208                         stage = idx;
1209                         seq++;
1210                 }
1211         }
1212
1213         if (!str[idx] && lbid[idx]) {
1214                 /* Inhouse */
1215                 return strdup(lbid);
1216         } else if (seq < 2) {
1217                 while (seq < 2) {
1218                         if (lbid[idx] == '.') {
1219                                 seq++;
1220                         } else if (!lbid[idx]) {
1221                                 ErrPrint("Invalid lbid: %s\n", lbid);
1222                                 return NULL;
1223                         }
1224
1225                         idx++;
1226                 }
1227
1228                 stage = idx;
1229         } else {
1230                 stage++;
1231         }
1232
1233         ret = strdup(lbid + stage);
1234         if (!ret) {
1235                 ErrPrint("Error: %s\n", strerror(errno));
1236                 return NULL;
1237         }
1238
1239         return ret;
1240 }
1241
1242 EAPI char *livebox_service_appid(const char *pkgname)
1243 {
1244         sqlite3_stmt *stmt;
1245         char *appid;
1246         char *tmp;
1247         sqlite3 *handle;
1248         int is_prime __attribute__((__unused__));
1249         int ret;
1250
1251         if (!pkgname)
1252                 return NULL;
1253
1254         appid = NULL;
1255         handle = open_db();
1256         if (!handle)
1257                 return NULL;
1258
1259         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1260         if (ret != SQLITE_OK) {
1261                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1262                 goto out;
1263         }
1264
1265         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
1266         if (ret != SQLITE_OK) {
1267                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1268                 sqlite3_reset(stmt);
1269                 sqlite3_finalize(stmt);
1270                 goto out;
1271         }
1272
1273         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, NULL);
1274         if (ret != SQLITE_OK) {
1275                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1276                 sqlite3_reset(stmt);
1277                 sqlite3_finalize(stmt);
1278                 goto out;
1279         }
1280
1281         ret = sqlite3_step(stmt);
1282         if (ret != SQLITE_ROW) {
1283                 pkgmgr_appinfo_h pkg_handle;
1284                 char *new_appid;
1285
1286                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1287                 sqlite3_reset(stmt);
1288                 sqlite3_finalize(stmt);
1289
1290                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1291                 if (ret != PKGMGR_R_OK) {
1292                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
1293                         goto out;
1294                 }
1295
1296                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1297                 if (ret != PKGMGR_R_OK) {
1298                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
1299                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1300                         goto out;
1301                 }
1302
1303                 appid = strdup(new_appid);
1304                 if (!appid)
1305                         ErrPrint("Heap: %s\n", strerror(errno));
1306
1307                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1308                 goto out;
1309         }
1310
1311         tmp = (char *)sqlite3_column_text(stmt, 0);
1312         if (!tmp || !strlen(tmp)) {
1313                 ErrPrint("APPID is NIL\n");
1314                 sqlite3_reset(stmt);
1315                 sqlite3_finalize(stmt);
1316                 goto out;
1317         }
1318
1319         appid = strdup(tmp);
1320         if (!appid) {
1321                 ErrPrint("Heap: %s\n", strerror(errno));
1322                 sqlite3_reset(stmt);
1323                 sqlite3_finalize(stmt);
1324                 goto out;
1325         }
1326
1327         is_prime = sqlite3_column_int(stmt, 1);
1328
1329         sqlite3_reset(stmt);
1330         sqlite3_finalize(stmt);
1331 out:
1332         close_db(handle);
1333         return appid;
1334 }
1335
1336 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1337 {
1338         sqlite3_stmt *stmt;
1339         sqlite3 *handle;
1340         int ret;
1341         char *path;
1342         char *appid;
1343         char *lb_src;
1344
1345         if (!pkgid)
1346                 return NULL;
1347
1348         path = NULL;
1349         handle = open_db();
1350         if (!handle)
1351                 return NULL;
1352
1353         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1354         if (ret != SQLITE_OK) {
1355                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1356                 goto out;
1357         }
1358
1359         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1360         if (ret != SQLITE_OK) {
1361                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1362                 sqlite3_finalize(stmt);
1363                 goto out;
1364         }
1365
1366         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1367         if (ret != SQLITE_OK) {
1368                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1369                 sqlite3_finalize(stmt);
1370                 goto out;
1371         }
1372
1373         ret = sqlite3_step(stmt);
1374         if (ret != SQLITE_ROW) {
1375                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1376                 sqlite3_reset(stmt);
1377                 sqlite3_finalize(stmt);
1378                 goto out;
1379         }
1380
1381         appid = (char *)sqlite3_column_text(stmt, 0);
1382         if (!appid || !strlen(appid)) {
1383                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1384                 sqlite3_reset(stmt);
1385                 sqlite3_finalize(stmt);
1386                 goto out;
1387         }
1388
1389         lb_src = (char *)sqlite3_column_text(stmt, 1);
1390         if (!lb_src || !strlen(lb_src)) {
1391                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1392                 sqlite3_reset(stmt);
1393                 sqlite3_finalize(stmt);
1394                 goto out;
1395         }
1396
1397         path = strdup(lb_src);
1398         if (!path) {
1399                 ErrPrint("Heap: %s\n", strerror(errno));
1400                 sqlite3_reset(stmt);
1401                 sqlite3_finalize(stmt);
1402                 goto out;
1403         }
1404
1405         DbgPrint("LB Src: %s\n", path);
1406
1407         sqlite3_reset(stmt);
1408         sqlite3_finalize(stmt);
1409 out:
1410         close_db(handle);
1411         return path;
1412 }
1413
1414 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1415 {
1416         sqlite3_stmt *stmt;
1417         sqlite3 *handle;
1418         int ret;
1419         char *group;
1420         char *tmp;
1421
1422         if (!pkgid)
1423                 return NULL;
1424
1425         group = NULL;
1426         handle = open_db();
1427         if (!handle)
1428                 return NULL;
1429
1430         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1431         if (ret != SQLITE_OK) {
1432                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1433                 goto out;
1434         }
1435
1436         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1437         if (ret != SQLITE_OK) {
1438                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1439                 sqlite3_finalize(stmt);
1440                 goto out;
1441         }
1442
1443         ret = sqlite3_step(stmt);
1444         if (ret != SQLITE_ROW) {
1445                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1446                 sqlite3_reset(stmt);
1447                 sqlite3_finalize(stmt);
1448                 goto out;
1449         }
1450
1451         tmp = (char *)sqlite3_column_text(stmt, 0);
1452         if (tmp && strlen(tmp)) {
1453                 group = strdup(tmp);
1454                 if (!group)
1455                         ErrPrint("Heap: %s\n", strerror(errno));
1456         }
1457
1458         sqlite3_reset(stmt);
1459         sqlite3_finalize(stmt);
1460 out:
1461         close_db(handle);
1462         return group;
1463 }
1464
1465 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1466 {
1467         sqlite3_stmt *stmt;
1468         sqlite3 *handle;
1469         int ret;
1470         char *path;
1471         char *pd_src;
1472         const char *appid;
1473
1474         if (!pkgid)
1475                 return NULL;
1476
1477         path = NULL;
1478         handle = open_db();
1479         if (!handle)
1480                 return NULL;
1481
1482         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1483         if (ret != SQLITE_OK) {
1484                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1485                 goto out;
1486         }
1487
1488         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1489         if (ret != SQLITE_OK) {
1490                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1491                 sqlite3_finalize(stmt);
1492                 goto out;
1493         }
1494
1495         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1496         if (ret != SQLITE_OK) {
1497                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1498                 sqlite3_finalize(stmt);
1499                 goto out;
1500         }
1501
1502         ret = sqlite3_step(stmt);
1503         if (ret != SQLITE_ROW) {
1504                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1505                 sqlite3_reset(stmt);
1506                 sqlite3_finalize(stmt);
1507                 goto out;
1508         }
1509
1510         appid = (char *)sqlite3_column_text(stmt, 0);
1511         if (!appid || !strlen(appid)) {
1512                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1513                 sqlite3_reset(stmt);
1514                 sqlite3_finalize(stmt);
1515                 goto out;
1516         }
1517
1518         pd_src = (char *)sqlite3_column_text(stmt, 1);
1519         if (!pd_src || !strlen(pd_src)) {
1520                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1521                 sqlite3_reset(stmt);
1522                 sqlite3_finalize(stmt);
1523                 goto out;
1524         }
1525
1526         path = strdup(pd_src);
1527         if (!path) {
1528                 ErrPrint("Heap: %s\n", strerror(errno));
1529                 sqlite3_reset(stmt);
1530                 sqlite3_finalize(stmt);
1531                 goto out;
1532         }
1533
1534         DbgPrint("PD Src: %s\n", path);
1535         sqlite3_reset(stmt);
1536         sqlite3_finalize(stmt);
1537 out:
1538         close_db(handle);
1539         return path;
1540 }
1541
1542 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1543 {
1544         sqlite3_stmt *stmt;
1545         sqlite3 *handle;
1546         int ret;
1547         char *group;
1548         char *tmp;
1549
1550         if (!pkgid)
1551                 return NULL;
1552
1553         group = NULL;
1554         handle = open_db();
1555         if (!handle)
1556                 return NULL;
1557
1558         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1559         if (ret != SQLITE_OK) {
1560                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1561                 goto out;
1562         }
1563
1564         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1565         if (ret != SQLITE_OK) {
1566                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1567                 sqlite3_finalize(stmt);
1568                 goto out;
1569         }
1570
1571         ret = sqlite3_step(stmt);
1572         if (ret != SQLITE_ROW) {
1573                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1574                 sqlite3_reset(stmt);
1575                 sqlite3_finalize(stmt);
1576                 goto out;
1577         }
1578
1579         tmp = (char *)sqlite3_column_text(stmt, 0);
1580         if (tmp && strlen(tmp)) {
1581                 group = strdup(tmp);
1582                 if (!group)
1583                         ErrPrint("Heap: %s\n", strerror(errno));
1584         }
1585         sqlite3_reset(stmt);
1586         sqlite3_finalize(stmt);
1587 out:
1588         close_db(handle);
1589         return group;
1590 }
1591
1592 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1593 {
1594         sqlite3_stmt *stmt;
1595         sqlite3 *handle;
1596         const char *cluster;
1597         int cnt;
1598         int ret;
1599
1600         if (!cb)
1601                 return -EINVAL;
1602
1603         handle = open_db();
1604         if (!handle)
1605                 return -EIO;
1606
1607         cnt = 0;
1608         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1609         if (ret != SQLITE_OK) {
1610                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1611                 cnt = -EIO;
1612                 goto out;
1613         }
1614
1615         while (sqlite3_step(stmt) == SQLITE_ROW) {
1616                 cluster = (const char *)sqlite3_column_text(stmt, 0);
1617                 if (!cluster || !strlen(cluster))
1618                         continue;
1619
1620                 if (cb(cluster, data) < 0)
1621                         break;
1622
1623                 cnt++;
1624         }
1625
1626         sqlite3_reset(stmt);
1627         sqlite3_finalize(stmt);
1628 out:
1629         close_db(handle);
1630         return cnt;
1631 }
1632
1633 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1634 {
1635         sqlite3_stmt *stmt;
1636         sqlite3 *handle;
1637         const char *category;
1638         int cnt;
1639         int ret;
1640
1641         if (!cluster || !cb)
1642                 return -EINVAL;
1643
1644         handle = open_db();
1645         if (!handle)
1646                 return -EIO;
1647
1648         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1649         if (ret != SQLITE_OK) {
1650                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1651                 cnt = -EIO;
1652                 goto out;
1653         }
1654
1655         cnt = 0;
1656         while (sqlite3_step(stmt) == SQLITE_ROW) {
1657                 category = (const char *)sqlite3_column_text(stmt, 0);
1658                 if (!category || !strlen(category))
1659                         continue;
1660
1661                 if (cb(cluster, category, data) < 0)
1662                         break;
1663
1664                 cnt++;
1665         }
1666
1667         sqlite3_reset(stmt);
1668         sqlite3_finalize(stmt);
1669 out:
1670         close_db(handle);
1671         return cnt;
1672 }
1673
1674 EAPI int livebox_service_init(void)
1675 {
1676         if (s_info.handle) {
1677                 DbgPrint("Already initialized\n");
1678                 s_info.init_count++;
1679                 return 0;
1680         }
1681
1682         s_info.handle = open_db();
1683         if (s_info.handle) {
1684                 s_info.init_count++;
1685                 return 0;
1686         }
1687
1688         return -EIO;
1689 }
1690
1691 EAPI int livebox_service_fini(void)
1692 {
1693         if (!s_info.handle || s_info.init_count <= 0) {
1694                 ErrPrint("Service is not initialized\n");
1695                 return -EIO;
1696         }
1697
1698         s_info.init_count--;
1699         if (s_info.init_count > 0) {
1700                 DbgPrint("Init count %d\n", s_info.init_count);
1701                 return 0;
1702         }
1703
1704         db_util_close(s_info.handle);
1705         s_info.handle = NULL;
1706         return 0;
1707 }
1708
1709 EAPI int livebox_service_get_size(int type, int *width, int *height)
1710 {
1711         int _width;
1712         int _height;
1713
1714         if (!width)
1715                 width = &_width;
1716
1717         if (!height)
1718                 height = &_height;
1719
1720         return convert_size_from_type(type, width, height);
1721 }
1722
1723 EAPI int livebox_service_size_type(int width, int height)
1724 {
1725         int idx;
1726
1727         if (update_resolution() < 0)
1728                 ErrPrint("Failed to update the size list\n");
1729
1730         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
1731                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
1732                         break;
1733         }
1734
1735         switch (idx) {
1736         case 0:
1737                 return LB_SIZE_TYPE_1x1;
1738         case 1:
1739                 return LB_SIZE_TYPE_2x1;
1740         case 2:
1741                 return LB_SIZE_TYPE_2x2;
1742         case 3:
1743                 return LB_SIZE_TYPE_4x1;
1744         case 4:
1745                 return LB_SIZE_TYPE_4x2;
1746         case 5:
1747                 return LB_SIZE_TYPE_4x3;
1748         case 6:
1749                 return LB_SIZE_TYPE_4x4;
1750         case 7:
1751                 return LB_SIZE_TYPE_EASY_1x1;
1752         case 8:
1753                 return LB_SIZE_TYPE_EASY_3x1;
1754         case 9:
1755                 return LB_SIZE_TYPE_EASY_3x3;
1756         default:
1757                 break;
1758         }
1759
1760         return LB_SIZE_TYPE_UNKNOWN;
1761 }
1762
1763 /* End of a file */