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