Add touch_effect retrieve 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_touch_effect(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 touch_effect 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 = 1;
778
779 out:
780         sqlite3_reset(stmt);
781         sqlite3_finalize(stmt);
782         close_db(handle);
783         return ret;
784 }
785
786 EAPI int livebox_service_mouse_event(const char *pkgid)
787 {
788         sqlite3_stmt *stmt;
789         sqlite3 *handle;
790         int ret;
791
792         handle = open_db();
793         if (!handle)
794                 return 0;
795
796         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
797         if (ret != SQLITE_OK) {
798                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
799                 close_db(handle);
800                 return 0;
801         }
802
803         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
804         if (ret != SQLITE_OK) {
805                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
806                 ret = 0;
807                 goto out;
808         }
809
810         ret = sqlite3_step(stmt);
811         if (ret == SQLITE_ROW)
812                 ret = !!sqlite3_column_int(stmt, 0);
813         else
814                 ret = 0;
815
816 out:
817         sqlite3_reset(stmt);
818         sqlite3_finalize(stmt);
819         close_db(handle);
820         return ret;
821 }
822
823 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
824 {
825         sqlite3_stmt *stmt;
826         sqlite3 *handle;
827         int ret;
828         char *preview = NULL;
829
830         handle = open_db();
831         if (!handle)
832                 return NULL;
833
834         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
835         if (ret != SQLITE_OK) {
836                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
837                 close_db(handle);
838                 return NULL;
839         }
840
841         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
842         if (ret != SQLITE_OK) {
843                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
844                 goto out;
845         }
846
847         ret = sqlite3_bind_int(stmt, 2, size_type);
848         if (ret != SQLITE_OK) {
849                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
850                 goto out;
851         }
852
853         ret = sqlite3_step(stmt);
854         if (ret == SQLITE_ROW) {
855                 const char *tmp;
856                 tmp = (const char *)sqlite3_column_text(stmt, 0);
857                 if (tmp && strlen(tmp)) {
858                         preview = strdup(tmp);
859                         if (!preview)
860                                 ErrPrint("Heap: %s\n", strerror(errno));
861                 }
862         }
863
864 out:
865         sqlite3_reset(stmt);
866         sqlite3_finalize(stmt);
867         close_db(handle);
868         return preview;
869 }
870
871 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
872 {
873         sqlite3_stmt *stmt;
874         sqlite3 *handle;
875         char *language;
876         char *icon = NULL;
877         int ret;
878
879         if (lang) {
880                 language = strdup(lang);
881                 if (!language) {
882                         ErrPrint("Heap: %s\n", strerror(errno));
883                         return NULL;
884                 }
885         } else {
886                 language = cur_locale();
887                 if (!language)
888                         return NULL;
889         }
890
891         handle = open_db();
892         if (!handle) {
893                 free(language);
894                 return NULL;
895         }
896
897         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
898         if (ret != SQLITE_OK) {
899                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
900                 close_db(handle);
901                 free(language);
902                 return NULL;
903         }
904
905         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
906         if (ret != SQLITE_OK) {
907                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
908                 goto out;
909         }
910
911         ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
912         if (ret != SQLITE_OK) {
913                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
914                 goto out;
915         }
916
917         ret = sqlite3_step(stmt);
918         if (ret == SQLITE_ROW) {
919                 const char *tmp;
920                 tmp = (const char *)sqlite3_column_text(stmt, 0);
921                 if (!tmp || !strlen(tmp)) {
922                         icon = get_default_icon(pkgid);
923                 } else {
924                         icon = strdup(tmp);
925                         if (!icon)
926                                 ErrPrint("Heap: %s\n", strerror(errno));
927                 }
928         } else {
929                 icon = get_default_icon(pkgid);
930         }
931
932 out:
933         sqlite3_reset(stmt);
934         sqlite3_finalize(stmt);
935         close_db(handle);
936         free(language);
937         return icon;
938 }
939
940 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
941 {
942         sqlite3_stmt *stmt;
943         sqlite3 *handle;
944         char *language;
945         char *name = NULL;
946         int ret;
947
948         if (lang) {
949                 language = strdup(lang);
950                 if (!language) {
951                         ErrPrint("Error: %s\n", strerror(errno));
952                         return NULL;
953                 }
954         } else {
955                 language = cur_locale();
956                 if (!language)
957                         return NULL;
958         }
959
960         handle = open_db();
961         if (!handle) {
962                 free(language);
963                 return NULL;
964         }
965
966         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
967         if (ret != SQLITE_OK) {
968                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
969                 close_db(handle);
970                 free(language);
971                 return NULL;
972         }
973
974         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
975         if (ret != SQLITE_OK) {
976                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
977                 goto out;
978         }
979
980         ret = sqlite3_bind_text(stmt, 2, language, -1, NULL);
981         if (ret != SQLITE_OK) {
982                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
983                 goto out;
984         }
985
986         ret = sqlite3_step(stmt);
987         if (ret == SQLITE_ROW) {
988                 const char *tmp;
989                 tmp = (const char *)sqlite3_column_text(stmt, 0);
990                 if (!tmp || !strlen(tmp)) {
991                         name = get_default_name(pkgid);
992                 } else {
993                         name = strdup(tmp);
994                         if (!name)
995                                 ErrPrint("Heap: %s\n", strerror(errno));
996                 }
997         } else {
998                 name = get_default_name(pkgid);
999         }
1000
1001 out:
1002         sqlite3_reset(stmt);
1003         sqlite3_finalize(stmt);
1004         close_db(handle);
1005         free(language);
1006         return name;
1007 }
1008
1009 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1010 {
1011         sqlite3_stmt *stmt;
1012         sqlite3 *handle;
1013         int size;
1014         int ret;
1015
1016         if (!w || !h || !cnt || !pkgid)
1017                 return -EINVAL;
1018
1019         handle = open_db();
1020         if (!handle)
1021                 return -EIO;
1022
1023         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1024         if (ret != SQLITE_OK) {
1025                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1026                 ret = -EIO;
1027                 goto out;
1028         }
1029
1030         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1031         if (ret != SQLITE_OK) {
1032                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1033                 sqlite3_reset(stmt);
1034                 sqlite3_finalize(stmt);
1035                 ret = -EIO;
1036                 goto out;
1037         }
1038
1039         if (*cnt > NR_OF_SIZE_LIST)
1040                 *cnt = NR_OF_SIZE_LIST;
1041
1042         ret = 0;
1043         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1044                 size = sqlite3_column_int(stmt, 0);
1045                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1046         }
1047
1048         *cnt = ret;
1049         sqlite3_reset(stmt);
1050         sqlite3_finalize(stmt);
1051         ret = 0;
1052 out:
1053         close_db(handle);
1054         return ret;
1055 }
1056
1057 EAPI char *livebox_service_libexec(const char *pkgid)
1058 {
1059         sqlite3_stmt *stmt;
1060         sqlite3 *handle;
1061         int ret;
1062         char *libexec;
1063         char *appid;
1064         char *path;
1065
1066         if (!pkgid)
1067                 return NULL;
1068
1069         libexec = NULL;
1070         handle = open_db();
1071         if (!handle)
1072                 return NULL;
1073
1074         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1075         if (ret != SQLITE_OK) {
1076                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1077                 goto out;
1078         }
1079
1080         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1081         if (ret != SQLITE_OK) {
1082                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1083                 sqlite3_finalize(stmt);
1084                 goto out;
1085         }
1086
1087         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1088         if (ret != SQLITE_OK) {
1089                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1090                 sqlite3_finalize(stmt);
1091                 goto out;
1092         }
1093
1094         if (sqlite3_step(stmt) != SQLITE_ROW) {
1095                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1096                 sqlite3_reset(stmt);
1097                 sqlite3_finalize(stmt);
1098
1099                 libexec = util_conf_get_libexec(pkgid);
1100                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1101                 goto out;
1102         }
1103
1104         appid = (char *)sqlite3_column_text(stmt, 0);
1105         if (!appid || !strlen(appid)) {
1106                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1107                 sqlite3_reset(stmt);
1108                 sqlite3_finalize(stmt);
1109                 goto out;
1110         }
1111
1112         path = (char *)sqlite3_column_text(stmt, 1);
1113         if (!path || !strlen(path)) {
1114                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1115                 sqlite3_reset(stmt);
1116                 sqlite3_finalize(stmt);
1117                 goto out;
1118         }
1119
1120         libexec = strdup(path);
1121         if (!libexec) {
1122                 ErrPrint("Heap: %s\n", strerror(errno));
1123                 sqlite3_reset(stmt);
1124                 sqlite3_finalize(stmt);
1125                 goto out;
1126         }
1127
1128         DbgPrint("libexec: %s\n", libexec);
1129
1130         sqlite3_reset(stmt);
1131         sqlite3_finalize(stmt);
1132 out:
1133         close_db(handle);
1134         return libexec;
1135 }
1136
1137 static inline char *get_lb_pkgname_by_appid(const char *appid)
1138 {
1139         sqlite3_stmt *stmt;
1140         char *pkgid;
1141         char *tmp;
1142         sqlite3 *handle;
1143         int ret;
1144
1145         if (!appid)
1146                 return NULL;
1147
1148         pkgid = NULL;
1149         handle = open_db();
1150         if (!handle)
1151                 return NULL;
1152
1153         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1154         if (ret != SQLITE_OK) {
1155                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1156                 close_db(handle);
1157                 return NULL;
1158         }
1159
1160         ret = sqlite3_bind_text(stmt, 1, appid, -1, NULL);
1161         if (ret != SQLITE_OK) {
1162                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1163                 goto out;
1164         }
1165
1166         ret = sqlite3_bind_text(stmt, 2, appid, -1, NULL);
1167         if (ret != SQLITE_OK) {
1168                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1169                 goto out;
1170         }
1171
1172         if (sqlite3_step(stmt) != SQLITE_ROW) {
1173                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1174                 goto out;
1175         }
1176
1177         tmp = (char *)sqlite3_column_text(stmt, 0);
1178         if (tmp && strlen(tmp)) {
1179                 pkgid = strdup(tmp);
1180                 if (!pkgid)
1181                         ErrPrint("Heap: %s\n", strerror(errno));
1182         }
1183
1184 out:
1185         sqlite3_reset(stmt);
1186         sqlite3_finalize(stmt);
1187         close_db(handle);
1188         return pkgid;
1189 }
1190
1191 EAPI char *livebox_service_pkgname(const char *appid)
1192 {
1193         char *lb_pkgname;
1194         pkgmgr_appinfo_h handle;
1195         int ret;
1196         char *new_appid;
1197
1198         if (!appid)
1199                 return NULL;
1200
1201         lb_pkgname = get_lb_pkgname_by_appid(appid);
1202         if (lb_pkgname)
1203                 return lb_pkgname;
1204
1205         /*!
1206          * \note
1207          * Try to get the package id using given appid
1208          */
1209         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1210         if (ret != PKGMGR_R_OK) {
1211                 ErrPrint("Failed to get appinfo\n");
1212                 return NULL;
1213         }
1214
1215         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1216         if (ret != PKGMGR_R_OK) {
1217                 pkgmgr_appinfo_destroy_appinfo(handle);
1218                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1219                 return NULL;
1220         }
1221
1222         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1223         pkgmgr_appinfo_destroy_appinfo(handle);
1224
1225         if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1226                 return strdup(appid);
1227
1228         return lb_pkgname;
1229 }
1230
1231 EAPI char *livebox_service_provider_name(const char *lbid)
1232 {
1233         char *ret;
1234         int stage = 0;
1235         int seq = 0;
1236         int idx = 0;
1237         char *str = "com.samsung.";
1238
1239         if (!lbid)
1240                 return NULL;
1241
1242         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1243                 idx++;
1244                 if (seq < 2 && lbid[idx] == '.') {
1245                         stage = idx;
1246                         seq++;
1247                 }
1248         }
1249
1250         if (!str[idx] && lbid[idx]) {
1251                 /* Inhouse */
1252                 return strdup(lbid);
1253         } else if (seq < 2) {
1254                 while (seq < 2) {
1255                         if (lbid[idx] == '.') {
1256                                 seq++;
1257                         } else if (!lbid[idx]) {
1258                                 ErrPrint("Invalid lbid: %s\n", lbid);
1259                                 return NULL;
1260                         }
1261
1262                         idx++;
1263                 }
1264
1265                 stage = idx;
1266         } else {
1267                 stage++;
1268         }
1269
1270         ret = strdup(lbid + stage);
1271         if (!ret) {
1272                 ErrPrint("Error: %s\n", strerror(errno));
1273                 return NULL;
1274         }
1275
1276         return ret;
1277 }
1278
1279 EAPI char *livebox_service_appid(const char *pkgname)
1280 {
1281         sqlite3_stmt *stmt;
1282         char *appid;
1283         char *tmp;
1284         sqlite3 *handle;
1285         int is_prime __attribute__((__unused__));
1286         int ret;
1287
1288         if (!pkgname)
1289                 return NULL;
1290
1291         appid = NULL;
1292         handle = open_db();
1293         if (!handle)
1294                 return NULL;
1295
1296         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1297         if (ret != SQLITE_OK) {
1298                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1299                 goto out;
1300         }
1301
1302         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, NULL);
1303         if (ret != SQLITE_OK) {
1304                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1305                 sqlite3_reset(stmt);
1306                 sqlite3_finalize(stmt);
1307                 goto out;
1308         }
1309
1310         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, NULL);
1311         if (ret != SQLITE_OK) {
1312                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1313                 sqlite3_reset(stmt);
1314                 sqlite3_finalize(stmt);
1315                 goto out;
1316         }
1317
1318         ret = sqlite3_step(stmt);
1319         if (ret != SQLITE_ROW) {
1320                 pkgmgr_appinfo_h pkg_handle;
1321                 char *new_appid;
1322
1323                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1324                 sqlite3_reset(stmt);
1325                 sqlite3_finalize(stmt);
1326
1327                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1328                 if (ret != PKGMGR_R_OK) {
1329                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
1330                         goto out;
1331                 }
1332
1333                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1334                 if (ret != PKGMGR_R_OK) {
1335                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
1336                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1337                         goto out;
1338                 }
1339
1340                 appid = strdup(new_appid);
1341                 if (!appid)
1342                         ErrPrint("Heap: %s\n", strerror(errno));
1343
1344                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1345                 goto out;
1346         }
1347
1348         tmp = (char *)sqlite3_column_text(stmt, 0);
1349         if (!tmp || !strlen(tmp)) {
1350                 ErrPrint("APPID is NIL\n");
1351                 sqlite3_reset(stmt);
1352                 sqlite3_finalize(stmt);
1353                 goto out;
1354         }
1355
1356         appid = strdup(tmp);
1357         if (!appid) {
1358                 ErrPrint("Heap: %s\n", strerror(errno));
1359                 sqlite3_reset(stmt);
1360                 sqlite3_finalize(stmt);
1361                 goto out;
1362         }
1363
1364         is_prime = sqlite3_column_int(stmt, 1);
1365
1366         sqlite3_reset(stmt);
1367         sqlite3_finalize(stmt);
1368 out:
1369         close_db(handle);
1370         return appid;
1371 }
1372
1373 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1374 {
1375         sqlite3_stmt *stmt;
1376         sqlite3 *handle;
1377         int ret;
1378         char *path;
1379         char *appid;
1380         char *lb_src;
1381
1382         if (!pkgid)
1383                 return NULL;
1384
1385         path = NULL;
1386         handle = open_db();
1387         if (!handle)
1388                 return NULL;
1389
1390         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1391         if (ret != SQLITE_OK) {
1392                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1393                 goto out;
1394         }
1395
1396         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1397         if (ret != SQLITE_OK) {
1398                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1399                 sqlite3_finalize(stmt);
1400                 goto out;
1401         }
1402
1403         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1404         if (ret != SQLITE_OK) {
1405                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1406                 sqlite3_finalize(stmt);
1407                 goto out;
1408         }
1409
1410         ret = sqlite3_step(stmt);
1411         if (ret != SQLITE_ROW) {
1412                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1413                 sqlite3_reset(stmt);
1414                 sqlite3_finalize(stmt);
1415                 goto out;
1416         }
1417
1418         appid = (char *)sqlite3_column_text(stmt, 0);
1419         if (!appid || !strlen(appid)) {
1420                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1421                 sqlite3_reset(stmt);
1422                 sqlite3_finalize(stmt);
1423                 goto out;
1424         }
1425
1426         lb_src = (char *)sqlite3_column_text(stmt, 1);
1427         if (!lb_src || !strlen(lb_src)) {
1428                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1429                 sqlite3_reset(stmt);
1430                 sqlite3_finalize(stmt);
1431                 goto out;
1432         }
1433
1434         path = strdup(lb_src);
1435         if (!path) {
1436                 ErrPrint("Heap: %s\n", strerror(errno));
1437                 sqlite3_reset(stmt);
1438                 sqlite3_finalize(stmt);
1439                 goto out;
1440         }
1441
1442         DbgPrint("LB Src: %s\n", path);
1443
1444         sqlite3_reset(stmt);
1445         sqlite3_finalize(stmt);
1446 out:
1447         close_db(handle);
1448         return path;
1449 }
1450
1451 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1452 {
1453         sqlite3_stmt *stmt;
1454         sqlite3 *handle;
1455         int ret;
1456         char *group;
1457         char *tmp;
1458
1459         if (!pkgid)
1460                 return NULL;
1461
1462         group = NULL;
1463         handle = open_db();
1464         if (!handle)
1465                 return NULL;
1466
1467         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1468         if (ret != SQLITE_OK) {
1469                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1470                 goto out;
1471         }
1472
1473         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1474         if (ret != SQLITE_OK) {
1475                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1476                 sqlite3_finalize(stmt);
1477                 goto out;
1478         }
1479
1480         ret = sqlite3_step(stmt);
1481         if (ret != SQLITE_ROW) {
1482                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1483                 sqlite3_reset(stmt);
1484                 sqlite3_finalize(stmt);
1485                 goto out;
1486         }
1487
1488         tmp = (char *)sqlite3_column_text(stmt, 0);
1489         if (tmp && strlen(tmp)) {
1490                 group = strdup(tmp);
1491                 if (!group)
1492                         ErrPrint("Heap: %s\n", strerror(errno));
1493         }
1494
1495         sqlite3_reset(stmt);
1496         sqlite3_finalize(stmt);
1497 out:
1498         close_db(handle);
1499         return group;
1500 }
1501
1502 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1503 {
1504         sqlite3_stmt *stmt;
1505         sqlite3 *handle;
1506         int ret;
1507         char *path;
1508         char *pd_src;
1509         const char *appid;
1510
1511         if (!pkgid)
1512                 return NULL;
1513
1514         path = NULL;
1515         handle = open_db();
1516         if (!handle)
1517                 return NULL;
1518
1519         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1520         if (ret != SQLITE_OK) {
1521                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1522                 goto out;
1523         }
1524
1525         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1526         if (ret != SQLITE_OK) {
1527                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1528                 sqlite3_finalize(stmt);
1529                 goto out;
1530         }
1531
1532         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, NULL);
1533         if (ret != SQLITE_OK) {
1534                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1535                 sqlite3_finalize(stmt);
1536                 goto out;
1537         }
1538
1539         ret = sqlite3_step(stmt);
1540         if (ret != SQLITE_ROW) {
1541                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1542                 sqlite3_reset(stmt);
1543                 sqlite3_finalize(stmt);
1544                 goto out;
1545         }
1546
1547         appid = (char *)sqlite3_column_text(stmt, 0);
1548         if (!appid || !strlen(appid)) {
1549                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1550                 sqlite3_reset(stmt);
1551                 sqlite3_finalize(stmt);
1552                 goto out;
1553         }
1554
1555         pd_src = (char *)sqlite3_column_text(stmt, 1);
1556         if (!pd_src || !strlen(pd_src)) {
1557                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1558                 sqlite3_reset(stmt);
1559                 sqlite3_finalize(stmt);
1560                 goto out;
1561         }
1562
1563         path = strdup(pd_src);
1564         if (!path) {
1565                 ErrPrint("Heap: %s\n", strerror(errno));
1566                 sqlite3_reset(stmt);
1567                 sqlite3_finalize(stmt);
1568                 goto out;
1569         }
1570
1571         DbgPrint("PD Src: %s\n", path);
1572         sqlite3_reset(stmt);
1573         sqlite3_finalize(stmt);
1574 out:
1575         close_db(handle);
1576         return path;
1577 }
1578
1579 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1580 {
1581         sqlite3_stmt *stmt;
1582         sqlite3 *handle;
1583         int ret;
1584         char *group;
1585         char *tmp;
1586
1587         if (!pkgid)
1588                 return NULL;
1589
1590         group = NULL;
1591         handle = open_db();
1592         if (!handle)
1593                 return NULL;
1594
1595         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1596         if (ret != SQLITE_OK) {
1597                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1598                 goto out;
1599         }
1600
1601         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, NULL);
1602         if (ret != SQLITE_OK) {
1603                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1604                 sqlite3_finalize(stmt);
1605                 goto out;
1606         }
1607
1608         ret = sqlite3_step(stmt);
1609         if (ret != SQLITE_ROW) {
1610                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1611                 sqlite3_reset(stmt);
1612                 sqlite3_finalize(stmt);
1613                 goto out;
1614         }
1615
1616         tmp = (char *)sqlite3_column_text(stmt, 0);
1617         if (tmp && strlen(tmp)) {
1618                 group = strdup(tmp);
1619                 if (!group)
1620                         ErrPrint("Heap: %s\n", strerror(errno));
1621         }
1622         sqlite3_reset(stmt);
1623         sqlite3_finalize(stmt);
1624 out:
1625         close_db(handle);
1626         return group;
1627 }
1628
1629 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1630 {
1631         sqlite3_stmt *stmt;
1632         sqlite3 *handle;
1633         const char *cluster;
1634         int cnt;
1635         int ret;
1636
1637         if (!cb)
1638                 return -EINVAL;
1639
1640         handle = open_db();
1641         if (!handle)
1642                 return -EIO;
1643
1644         cnt = 0;
1645         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1646         if (ret != SQLITE_OK) {
1647                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1648                 cnt = -EIO;
1649                 goto out;
1650         }
1651
1652         while (sqlite3_step(stmt) == SQLITE_ROW) {
1653                 cluster = (const char *)sqlite3_column_text(stmt, 0);
1654                 if (!cluster || !strlen(cluster))
1655                         continue;
1656
1657                 if (cb(cluster, data) < 0)
1658                         break;
1659
1660                 cnt++;
1661         }
1662
1663         sqlite3_reset(stmt);
1664         sqlite3_finalize(stmt);
1665 out:
1666         close_db(handle);
1667         return cnt;
1668 }
1669
1670 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1671 {
1672         sqlite3_stmt *stmt;
1673         sqlite3 *handle;
1674         const char *category;
1675         int cnt;
1676         int ret;
1677
1678         if (!cluster || !cb)
1679                 return -EINVAL;
1680
1681         handle = open_db();
1682         if (!handle)
1683                 return -EIO;
1684
1685         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1686         if (ret != SQLITE_OK) {
1687                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1688                 cnt = -EIO;
1689                 goto out;
1690         }
1691
1692         cnt = 0;
1693         while (sqlite3_step(stmt) == SQLITE_ROW) {
1694                 category = (const char *)sqlite3_column_text(stmt, 0);
1695                 if (!category || !strlen(category))
1696                         continue;
1697
1698                 if (cb(cluster, category, data) < 0)
1699                         break;
1700
1701                 cnt++;
1702         }
1703
1704         sqlite3_reset(stmt);
1705         sqlite3_finalize(stmt);
1706 out:
1707         close_db(handle);
1708         return cnt;
1709 }
1710
1711 EAPI int livebox_service_init(void)
1712 {
1713         if (s_info.handle) {
1714                 DbgPrint("Already initialized\n");
1715                 s_info.init_count++;
1716                 return 0;
1717         }
1718
1719         s_info.handle = open_db();
1720         if (s_info.handle) {
1721                 s_info.init_count++;
1722                 return 0;
1723         }
1724
1725         return -EIO;
1726 }
1727
1728 EAPI int livebox_service_fini(void)
1729 {
1730         if (!s_info.handle || s_info.init_count <= 0) {
1731                 ErrPrint("Service is not initialized\n");
1732                 return -EIO;
1733         }
1734
1735         s_info.init_count--;
1736         if (s_info.init_count > 0) {
1737                 DbgPrint("Init count %d\n", s_info.init_count);
1738                 return 0;
1739         }
1740
1741         db_util_close(s_info.handle);
1742         s_info.handle = NULL;
1743         return 0;
1744 }
1745
1746 EAPI int livebox_service_get_size(int type, int *width, int *height)
1747 {
1748         int _width;
1749         int _height;
1750
1751         if (!width)
1752                 width = &_width;
1753
1754         if (!height)
1755                 height = &_height;
1756
1757         return convert_size_from_type(type, width, height);
1758 }
1759
1760 EAPI int livebox_service_size_type(int width, int height)
1761 {
1762         int idx;
1763
1764         if (update_resolution() < 0)
1765                 ErrPrint("Failed to update the size list\n");
1766
1767         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
1768                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
1769                         break;
1770         }
1771
1772         switch (idx) {
1773         case 0:
1774                 return LB_SIZE_TYPE_1x1;
1775         case 1:
1776                 return LB_SIZE_TYPE_2x1;
1777         case 2:
1778                 return LB_SIZE_TYPE_2x2;
1779         case 3:
1780                 return LB_SIZE_TYPE_4x1;
1781         case 4:
1782                 return LB_SIZE_TYPE_4x2;
1783         case 5:
1784                 return LB_SIZE_TYPE_4x3;
1785         case 6:
1786                 return LB_SIZE_TYPE_4x4;
1787         case 7:
1788                 return LB_SIZE_TYPE_EASY_1x1;
1789         case 8:
1790                 return LB_SIZE_TYPE_EASY_3x1;
1791         case 9:
1792                 return LB_SIZE_TYPE_EASY_3x3;
1793         default:
1794                 break;
1795         }
1796
1797         return LB_SIZE_TYPE_UNKNOWN;
1798 }
1799
1800 /* End of a file */