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