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