Update trigger_update for sending the new content
[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.1 (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 <pkgmgr-info.h>
35 #include <vconf.h>
36 #include <vconf-keys.h>
37 #include <ail.h>
38 #include <unicode/uloc.h>
39
40 #include "dlist.h"
41 #include "util.h"
42 #include "debug.h"
43 #include "livebox-service.h"
44 #include "livebox-errno.h"
45
46 #define SAMSUNG_PREFIX  "com.samsung."
47 #define EAPI __attribute__((visibility("default")))
48 #define DEFAULT_TIMEOUT 2.0
49 #define MAX_COLUMN 80
50
51 static struct supported_size_list {
52         int w;
53         int h;
54 } SIZE_LIST[NR_OF_SIZE_LIST] = {
55         { 175, 175 }, /*!< 1x1 */
56         { 354, 175 }, /*!< 2x1 */
57         { 354, 354 }, /*!< 2x2 */
58         { 712, 175 }, /*!< 4x1 */
59         { 712, 354 }, /*!< 4x2 */
60         { 712, 533 }, /*!< 4x3 */
61         { 712, 712 }, /*!< 4x4 */
62         { 712, 891 }, /*!< 4x5 */
63         { 712, 1070 }, /*!< 4x6 */
64         { 224, 215 }, /*!< 21x21 */
65         { 680, 215 }, /*!< 23x21 */
66         { 680, 653 }, /*!< 23x23 */
67         { 720, 1280 }, /*!< 0x0 */
68 };
69
70 struct pkglist_handle {
71         enum pkglist_type {
72                 PKGLIST_TYPE_LB_LIST = 0x00beef00,
73                 PKGLIST_TYPE_UNKNOWN = 0x00dead00
74         } type;
75         sqlite3 *handle;
76         sqlite3_stmt *stmt;
77 };
78
79 static struct info {
80         sqlite3 *handle;
81         const char *dbfile;
82         const char *conf_file;
83         int init_count;
84         int res_resolved;
85
86         const char *iso3lang;
87         char country[ULOC_COUNTRY_CAPACITY];
88         char *syslang;
89         int country_len;
90 } s_info = {
91         .handle = NULL,
92         .dbfile = "/opt/dbspace/.livebox.db", 
93         .conf_file = "/usr/share/data-provider-master/resolution.ini",
94         .init_count = 0,
95         .res_resolved = 0,
96
97         .iso3lang = NULL,
98         .country = { 0, },
99         .syslang = NULL,
100         .country_len = 0,
101 };
102
103 static inline int update_info(int width_type, int height_type, int width, int height)
104 {
105         int idx;
106
107         if (width_type == 1 && height_type == 1) {
108                 idx = 0;
109         } else if (width_type == 2 && height_type == 1) {
110                 idx = 1;
111         } else if (width_type == 2 && height_type == 2) {
112                 idx = 2;
113         } else if (width_type == 4 && height_type == 1) {
114                 idx = 3;
115         } else if (width_type == 4 && height_type == 2) {
116                 idx = 4;
117         } else if (width_type == 4 && height_type == 3) {
118                 idx = 5;
119         } else if (width_type == 4 && height_type == 4) {
120                 idx = 6;
121         } else if (width_type == 4 && height_type == 5) {
122                 idx = 7;
123         } else if (width_type == 4 && height_type == 6) {
124                 idx = 8;
125         } else if (width_type == 21 && height_type == 21) {
126                 idx = 9;
127         } else if (width_type == 23 && height_type == 21) {
128                 idx = 10;
129         } else if (width_type == 23 && height_type == 23) {
130                 idx = 11;
131         } else if (width_type == 0 && height_type == 0) {
132                 idx = 12;
133         } else {
134                 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
135                 return 0;
136         }
137
138         SIZE_LIST[idx].w = width;
139         SIZE_LIST[idx].h = height;
140         return 1;
141 }
142
143 static inline int update_from_file(void)
144 {
145         FILE *fp;
146         int updated;
147         int width_type;
148         int height_type;
149         int width;
150         int height;
151         char buffer[MAX_COLUMN];
152         int ch;
153         int idx;
154         enum status {
155                 START = 0x0,
156                 TYPE = 0x01,
157                 SIZE = 0x02,
158                 COMMENT = 0x03,
159                 ERROR = 0x04,
160                 EOL = 0x05,
161                 TYPE_END = 0x06,
162                 SIZE_START = 0x07
163         } status;
164
165         fp = fopen(s_info.conf_file, "r");
166         if (!fp) {
167                 ErrPrint("Open failed: %s\n", strerror(errno));
168                 return LB_STATUS_ERROR_IO;
169         }
170
171         updated = 0;
172         status = START;
173         idx = 0;
174         do {
175                 ch = fgetc(fp);
176
177                 if (idx == MAX_COLUMN) {
178                         ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
179                         status = ERROR;
180                 }
181
182                 switch (status) {
183                 case START:
184                         if (isspace(ch) || ch == EOF) {
185                                 continue;
186                         }
187
188                         if (ch == '#') {
189                                 status = COMMENT;
190                         } else {
191                                 status = TYPE;
192                                 idx = 0;
193                                 ungetc(ch, fp);
194                         }
195                         break;
196                 case TYPE:
197                         if (isblank(ch)) {
198                                 buffer[idx] = '\0';
199                                 status = TYPE_END;
200                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
201                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
202                                         status = ERROR;
203                                 }
204                                 break;
205                         } else if (ch == '=') {
206                                 buffer[idx] = '\0';
207                                 status = SIZE_START;
208                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
209                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
210                                         status = ERROR;
211                                 }
212                                 break;
213                         } else if (ch == EOF) {
214                                 ErrPrint("Invalid Syntax\n");
215                                 status = ERROR;
216                                 continue;
217                         }
218                         buffer[idx++] = ch;
219                         break;
220                 case TYPE_END:
221                         if (ch == '=') {
222                                 status = SIZE_START;
223                         }
224                         break;
225                 case SIZE_START:
226                         if (isspace(ch) || ch == EOF) {
227                                 continue;
228                         }
229
230                         status = SIZE;
231                         idx = 0;
232                         ungetc(ch, fp);
233                         break;
234                 case SIZE:
235                         if (isspace(ch) || ch == EOF) {
236                                 buffer[idx] = '\0';
237                                 status = EOL;
238
239                                 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
240                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
241                                         status = ERROR;
242                                 } else if (ch == EOF) {
243                                         updated += update_info(width_type, height_type, width, height);
244                                 }
245                                 break;
246                         }
247                         buffer[idx++] = ch;
248                         break;
249                 case EOL:
250                         updated += update_info(width_type, height_type, width, height);
251                         status = START;
252                         ungetc(ch, fp);
253                         break;
254                 case ERROR:
255                         if (ch == '\n' || ch == '\r' || ch == '\f') {
256                                 status = START;
257                         }
258                         break;
259                 case COMMENT:
260                         if (ch == '\n' || ch == '\r' || ch == '\f') {
261                                 status = START;
262                         }
263                         break;
264                 default:
265                         ErrPrint("Unknown status. couldn't be reach to here\n");
266                         break;
267                 }
268         } while (!feof(fp));
269
270         if (fclose(fp) != 0) {
271                 ErrPrint("fclose: %s\n", strerror(errno));
272         }
273
274         return NR_OF_SIZE_LIST - updated;
275 }
276
277 static int update_resolution(void)
278 {
279         Display *disp;
280         Window root;
281         Window dummy;
282         int x, y;
283         unsigned int width;
284         unsigned int height;
285         unsigned int border;
286         unsigned int depth;
287         register int i;
288
289         if (s_info.res_resolved) {
290                 return LB_STATUS_SUCCESS;
291         }
292
293         disp = XOpenDisplay(NULL);
294         if (!disp) {
295                 ErrPrint("Failed to open a display\n");
296                 return LB_STATUS_ERROR_FAULT;
297         }
298
299         root = XDefaultRootWindow(disp);
300         if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
301                 XCloseDisplay(disp);
302                 return LB_STATUS_ERROR_FAULT;
303         }
304
305         if (update_from_file() == 0) {
306                 DbgPrint("Resolution info is all updated by file\n");
307         }
308
309         for (i = 0; i < NR_OF_SIZE_LIST; i++) {
310                 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
311                 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / 720.0f);
312         }
313
314         XCloseDisplay(disp);
315         s_info.res_resolved = 1;
316         return LB_STATUS_SUCCESS;
317 }
318
319 static sqlite3 *open_db(void)
320 {
321         sqlite3 *handle;
322
323         if (!s_info.handle) {
324                 int ret;
325
326                 ret = db_util_open_with_options(s_info.dbfile, &handle, SQLITE_OPEN_READONLY, NULL);
327                 if (ret != SQLITE_OK) {
328                         ErrPrint("Failed to open a DB\n");
329                         return NULL;
330                 }
331         } else {
332                 handle = s_info.handle;
333         }
334
335         return handle;
336 }
337
338 static inline __attribute__((always_inline)) void close_db(sqlite3 *handle)
339 {
340         if (!s_info.handle) {
341                 db_util_close(handle);
342         }
343 }
344
345 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
346 {
347         int idx;
348
349         switch (type) {
350         case LB_SIZE_TYPE_1x1: /*!< 175x175 */
351                 idx = 0;
352                 break;
353         case LB_SIZE_TYPE_2x1: /*!< 354x175 */
354                 idx = 1;
355                 break;
356         case LB_SIZE_TYPE_2x2: /*!< 354x354 */
357                 idx = 2;
358                 break;
359         case LB_SIZE_TYPE_4x1: /*!< 712x175 */
360                 idx = 3;
361                 break;
362         case LB_SIZE_TYPE_4x2: /*!< 712x354 */
363                 idx = 4;
364                 break;
365         case LB_SIZE_TYPE_4x3: /*!< 712x533 */
366                 idx = 5;
367                 break;
368         case LB_SIZE_TYPE_4x4: /*!< 712x712 */
369                 idx = 6;
370                 break;
371         case LB_SIZE_TYPE_4x5: /*!< 712x891 */
372                 idx = 7;
373                 break;
374         case LB_SIZE_TYPE_4x6: /*!< 712x1070 */
375                 idx = 8;
376                 break;
377         case LB_SIZE_TYPE_EASY_1x1: /*< 224x215 */
378                 idx = 9;
379                 break;
380         case LB_SIZE_TYPE_EASY_3x1: /*!< 680x215 */
381                 idx = 10;
382                 break;
383         case LB_SIZE_TYPE_EASY_3x3: /*!< 680x653 */
384                 idx = 11;
385                 break;
386         case LB_SIZE_TYPE_0x0: /*!< 720x1280 */
387                 idx = 12;
388                 break;
389         default:
390                 return LB_STATUS_ERROR_INVALID;
391         }
392
393         if (update_resolution() < 0) {
394                 ErrPrint("Failed to update resolution\n");
395         }
396
397         *width = SIZE_LIST[idx].w;
398         *height = SIZE_LIST[idx].h;
399         return LB_STATUS_SUCCESS;
400 }
401
402 EAPI int livebox_service_change_period(const char *pkgname, const char *id, double period)
403 {
404         struct packet *packet;
405         struct packet *result;
406         char *uri;
407         int ret;
408
409         if (!pkgname || !id || period < 0.0f) {
410                 ErrPrint("Invalid argument\n");
411                 return LB_STATUS_ERROR_INVALID;
412         }
413
414         uri = util_id_to_uri(id);
415         if (!uri) {
416                 return LB_STATUS_ERROR_MEMORY;
417         }
418
419         packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
420         free(uri);
421         if (!packet) {
422                 ErrPrint("Failed to create a packet for period changing\n");
423                 return LB_STATUS_ERROR_FAULT;
424         }
425
426         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
427         packet_unref(packet);
428
429         if (result) {
430                 if (packet_get(result, "i", &ret) != 1) {
431                         ErrPrint("Failed to parse a result packet\n");
432                         ret = LB_STATUS_ERROR_INVALID;
433                 }
434                 packet_unref(result);
435         } else {
436                 ErrPrint("Failed to get result packet\n");
437                 ret = LB_STATUS_ERROR_FAULT;
438         }
439
440         return ret;
441 }
442
443 EAPI int livebox_service_trigger_update_with_content(const char *pkgname, const char *id, const char *cluster, const char *category, const char *content, int force)
444 {
445         struct packet *packet;
446         struct packet *result;
447         char *uri;
448         int ret;
449
450         if (!pkgname) {
451                 ErrPrint("Invalid argument\n");
452                 return LB_STATUS_ERROR_INVALID;
453         }
454
455         if (!force && access("/tmp/.live.paused", R_OK) == 0) {
456                 DbgPrint("Provider is paused\n");
457                 return LB_STATUS_ERROR_CANCEL;
458         }
459
460         if (id) {
461                 uri = util_id_to_uri(id);
462                 if (!uri) {
463                         return LB_STATUS_ERROR_MEMORY;
464                 }
465         } else {
466                 uri = NULL;
467         }
468
469         if (!cluster) {
470                 cluster = "user,created";
471         }
472
473         if (!category) {
474                 category = "default";
475         }
476
477         packet = packet_create("service_update", "sssss", pkgname, uri, cluster, category, content);
478         /*!
479          * \note
480          * "free" function accepts NULL
481          */
482         free(uri);
483         if (!packet) {
484                 ErrPrint("Failed to create a packet for service_update\n");
485                 return LB_STATUS_ERROR_FAULT;
486         }
487
488         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
489         packet_unref(packet);
490
491         if (result) {
492                 if (packet_get(result, "i", &ret) != 1) {
493                         ErrPrint("Failed to parse a result packet\n");
494                         ret = LB_STATUS_ERROR_INVALID;
495                 }
496
497                 packet_unref(result);
498         } else {
499                 ErrPrint("Failed to get result packet\n");
500                 ret = LB_STATUS_ERROR_FAULT;
501         }
502
503         return ret;
504 }
505
506 EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
507 {
508         return livebox_service_trigger_update_with_content(pkgname, id, cluster, category, NULL, force);
509 }
510
511 /*!
512  * pkgid == Package Id (not the livebox id)
513  */
514 EAPI struct pkglist_handle *livebox_service_pkglist_create(const char *pkgid, struct pkglist_handle *handle)
515 {
516         int ret;
517
518         if (handle) {
519                 if (handle->type != PKGLIST_TYPE_LB_LIST) {
520                         ErrPrint("Invalid handle\n");
521                         return NULL;
522                 }
523
524                 if (pkgid) {
525                         ErrPrint("pkgid should be NULL\n");
526                         return NULL;
527                 }
528
529                 sqlite3_reset(handle->stmt);
530                 return handle;
531         }
532
533         handle = calloc(1, sizeof(*handle));
534         if (!handle) {
535                 ErrPrint("Heap: %s\n", strerror(errno));
536                 return NULL;
537         }
538
539         handle->type = PKGLIST_TYPE_LB_LIST;
540
541         handle->handle = open_db();
542         if (!handle->handle) {
543                 free(handle);
544                 return NULL;
545         }
546
547         if (!pkgid) {
548                 ret = sqlite3_prepare_v2(handle->handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &handle->stmt, NULL);
549                 if (ret != SQLITE_OK) {
550                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
551                         close_db(handle->handle);
552                         free(handle);
553                         return NULL;
554                 }
555         } else {
556                 ret = sqlite3_prepare_v2(handle->handle, "SELECT appid, pkgid, prime FROM pkgmap WHERE appid = ?", -1, &handle->stmt, NULL);
557                 if (ret != SQLITE_OK) {
558                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
559                         close_db(handle->handle);
560                         free(handle);
561                         return NULL;
562                 }
563
564                 ret = sqlite3_bind_text(handle->stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
565                 if (ret != SQLITE_OK) {
566                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
567                         sqlite3_finalize(handle->stmt);
568                         close_db(handle->handle);
569                         free(handle);
570                         return NULL;
571                 }
572         }
573
574         return handle;
575 }
576
577 EAPI int livebox_service_get_pkglist_item(struct pkglist_handle *handle, char **appid, char **pkgname, int *is_prime)
578 {
579         const char *tmp;
580         char *_appid = NULL;
581         char *_pkgname = NULL;
582
583         if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
584                 return LB_STATUS_ERROR_INVALID;
585         }
586
587         if (sqlite3_step(handle->stmt) != SQLITE_ROW) {
588                 return LB_STATUS_ERROR_NOT_EXIST;
589         }
590
591         if (appid) {
592                 tmp = (const char *)sqlite3_column_text(handle->stmt, 0);
593                 if (tmp && strlen(tmp)) {
594                         _appid = strdup(tmp);
595                         if (!_appid) {
596                                 ErrPrint("Heap: %s\n", strerror(errno));
597                                 return LB_STATUS_ERROR_MEMORY;
598                         }
599                 }
600         }
601
602         if (pkgname) {
603                 tmp = (const char *)sqlite3_column_text(handle->stmt, 1);
604                 if (tmp && strlen(tmp)) {
605                         _pkgname = strdup(tmp);
606                         if (!_pkgname) {
607                                 ErrPrint("Heap: %s\n", strerror(errno));
608                                 free(_appid);
609                                 return LB_STATUS_ERROR_MEMORY;
610                         }
611                 }
612         }
613
614         if (is_prime) {
615                 *is_prime = sqlite3_column_int(handle->stmt, 2);
616         }
617
618         if (appid) {
619                 *appid = _appid;
620         }
621
622         if (pkgname) {
623                 *pkgname = _pkgname;
624         }
625
626         return LB_STATUS_SUCCESS;
627 }
628
629 EAPI int livebox_service_pkglist_destroy(struct pkglist_handle *handle)
630 {
631         if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
632                 return LB_STATUS_ERROR_INVALID;
633         }
634
635         handle->type = PKGLIST_TYPE_UNKNOWN;
636         sqlite3_reset(handle->stmt);
637         sqlite3_finalize(handle->stmt);
638         close_db(handle->handle);
639         free(handle);
640         return LB_STATUS_SUCCESS;
641 }
642
643 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
644 {
645         int ret;
646         sqlite3_stmt *stmt;
647         char *appid;
648         char *pkgid;
649         int is_prime;
650         sqlite3 *handle;
651
652         if (!cb) {
653                 return LB_STATUS_ERROR_INVALID;
654         }
655
656         handle = open_db();
657         if (!handle) {
658                 return LB_STATUS_ERROR_IO;
659         }
660
661         ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
662         if (ret != SQLITE_OK) {
663                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
664                 ret = LB_STATUS_ERROR_IO;
665                 goto out;
666         }
667
668         ret = 0;
669         while (sqlite3_step(stmt) == SQLITE_ROW) {
670                 appid = (char *)sqlite3_column_text(stmt, 0);
671                 if (!appid || !strlen(appid)) {
672                         ErrPrint("APPID is not valid\n");
673                         continue;
674                 }
675
676                 pkgid = (char *)sqlite3_column_text(stmt, 1);
677                 if (!pkgid || !strlen(pkgid)) {
678                         ErrPrint("pkgid is not valid\n");
679                         continue;
680                 }
681
682                 is_prime = sqlite3_column_int(stmt, 2);
683
684                 ret++;
685
686                 if (cb(appid, pkgid, is_prime, data) < 0) {
687                         DbgPrint("Callback stopped package crawling\n");
688                         break;
689                 }
690         }
691
692         sqlite3_reset(stmt);
693         sqlite3_finalize(stmt);
694
695 out:
696         close_db(handle);
697         return ret;
698 }
699
700 EAPI int livebox_service_get_pkglist_by_pkgid(const char *pkgid, int (*cb)(const char *lbid, int is_prime, void *data), void *data)
701 {
702         int ret;
703         sqlite3_stmt *stmt;
704         const char *lbid;
705         int is_prime;
706         sqlite3 *handle;
707
708         if (!cb) {
709                 return LB_STATUS_ERROR_INVALID;
710         }
711
712         handle = open_db();
713         if (!handle) {
714                 return LB_STATUS_ERROR_IO;
715         }
716
717         ret = sqlite3_prepare_v2(handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
718         if (ret != SQLITE_OK) {
719                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
720                 ret = LB_STATUS_ERROR_IO;
721                 goto out;
722         }
723
724         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
725         if (ret != SQLITE_OK) {
726                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
727                 sqlite3_reset(stmt);
728                 sqlite3_finalize(stmt);
729                 ret = LB_STATUS_ERROR_IO;
730                 goto out;
731         }
732
733         ret = 0;
734         while (sqlite3_step(stmt) == SQLITE_ROW) {
735                 lbid = (const char *)sqlite3_column_text(stmt, 0);
736                 if (!lbid || !strlen(lbid)) {
737                         ErrPrint("LBID is not valid\n");
738                         continue;
739                 }
740
741                 is_prime = sqlite3_column_int(stmt, 1);
742
743                 ret++;
744
745                 if (cb(lbid, is_prime, data) < 0) {
746                         DbgPrint("Callback stopped package crawling\n");
747                         break;
748                 }
749         }
750
751         sqlite3_reset(stmt);
752         sqlite3_finalize(stmt);
753
754 out:
755         close_db(handle);
756         return ret;
757 }
758
759 struct pkgmgr_cbdata {
760         const char *lbid;
761         void (*cb)(const char *lbid, const char *appid, void *data);
762         void *cbdata;
763 };
764
765 static int pkgmgr_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
766 {
767         struct pkgmgr_cbdata *cbdata = (struct pkgmgr_cbdata *)user_data;
768         char *appid;
769         int ret;
770
771         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
772         if (ret < 0) {
773                 ErrPrint("Unable to get appid\n");
774         } else {
775                 cbdata->cb(cbdata->lbid, appid, cbdata->cbdata);
776         }
777
778         return 0;
779 }
780
781 static inline char *pkgmgr_get_mainapp(const char *pkgid)
782 {
783         pkgmgrinfo_pkginfo_h handle;
784         char *ret = NULL;
785
786         if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle) != PMINFO_R_OK) {
787                 ErrPrint("Unable to get mainapp: %s\n", pkgid);
788                 return NULL;
789         }
790
791         if (pkgmgrinfo_pkginfo_get_mainappid(handle, &ret) == PMINFO_R_OK) {
792                 ret = strdup(ret);
793         } else {
794                 ErrPrint("Failed to get mainappid\n");
795                 ret = NULL; /* I cannot believe the pkgmgrinfo_pkginfo_get_mainappid. it maybe able to touch my "ret" even though it fails */
796         
797         }
798
799         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
800         return ret;
801 }
802
803 static inline int pkgmgr_get_applist(const char *pkgid, const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
804 {
805         struct pkgmgr_cbdata cbdata;
806         pkgmgrinfo_pkginfo_h handle;
807         int ret;
808
809         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
810         if (ret < 0) {
811                 ErrPrint("Unable to get pkginfo: %s\n", pkgid);
812                 return ret;
813         }
814
815         cbdata.lbid = lbid;
816         cbdata.cb = cb;
817         cbdata.cbdata = data;
818
819         ret = pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, pkgmgr_cb, &cbdata);
820         if (ret < 0) {
821                 ErrPrint("Failed to get applist\n");
822         }
823
824         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
825         return ret;
826 }
827
828 EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
829 {
830         sqlite3_stmt *stmt;
831         const char *tmp;
832         char *pkgid;
833         sqlite3 *handle;
834         int ret;
835
836         if (!lbid || !cb) {
837                 return LB_STATUS_ERROR_INVALID;
838         }
839
840         handle = open_db();
841         if (!handle) {
842                 return LB_STATUS_ERROR_IO;
843         }
844
845         ret = sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL);
846         if (ret != SQLITE_OK) {
847                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
848                 ret = LB_STATUS_ERROR_IO;
849                 goto out;
850         }
851
852         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
853         if (ret != SQLITE_OK) {
854                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
855                 ret = LB_STATUS_ERROR_IO;
856                 goto out;
857         }
858
859         ret = sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT);
860         if (ret != SQLITE_OK) {
861                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
862                 ret = LB_STATUS_ERROR_IO;
863                 goto out;
864         }
865
866         if (sqlite3_step(stmt) != SQLITE_ROW) {
867                 ret = LB_STATUS_ERROR_INVALID;
868                 sqlite3_reset(stmt);
869                 sqlite3_finalize(stmt);
870                 goto out;
871         }
872
873         tmp = (const char *)sqlite3_column_text(stmt, 0);
874         if (!tmp || !strlen(tmp)) {
875                 ErrPrint("Invalid package name (%s)\n", lbid);
876                 ret = LB_STATUS_ERROR_INVALID;
877                 sqlite3_reset(stmt);
878                 sqlite3_finalize(stmt);
879                 goto out;
880         }
881
882         pkgid = strdup(tmp);
883         if (!pkgid) {
884                 ErrPrint("Error: %s\n", strerror(errno));
885                 ret = LB_STATUS_ERROR_MEMORY;
886                 sqlite3_reset(stmt);
887                 sqlite3_finalize(stmt);
888                 goto out;
889         }
890
891         sqlite3_reset(stmt);
892         sqlite3_finalize(stmt);
893
894         ret = pkgmgr_get_applist(pkgid, lbid, cb, data);
895         free(pkgid);
896
897         switch (ret) {
898         case PMINFO_R_EINVAL:
899                 ret = LB_STATUS_ERROR_INVALID;
900                 break;
901         case PMINFO_R_OK:
902                 ret = LB_STATUS_SUCCESS;
903                 break;
904         case PMINFO_R_ERROR:
905         default:
906                 ret = LB_STATUS_ERROR_FAULT;
907                 break;
908         }
909
910 out:
911         close_db(handle);
912         return ret;
913 }
914
915 EAPI char *livebox_service_mainappid(const char *lbid)
916 {
917         sqlite3_stmt *stmt;
918         const char *tmp;
919         const char *pkgid;
920         sqlite3 *handle;
921         char *ret = NULL;
922
923         if (!lbid) {
924                 return NULL;
925         }
926
927         handle = open_db();
928         if (!handle) {
929                 return NULL;
930         }
931
932         if (sqlite3_prepare_v2(handle, "SELECT appid, uiapp FROM pkgmap WHERE (pkgid = ?) or (appid = ? and prime = 1)", -1, &stmt, NULL) != SQLITE_OK) {
933                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
934                 goto out;
935         }
936
937         if (sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
938                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
939                 goto out;
940         }
941
942         if (sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
943                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
944                 goto out;
945         }
946
947         if (sqlite3_step(stmt) != SQLITE_ROW) {
948                 sqlite3_reset(stmt);
949                 sqlite3_finalize(stmt);
950                 goto out;
951         }
952
953         tmp = (const char *)sqlite3_column_text(stmt, 0);
954         if (!tmp || !strlen(tmp)) {
955                 ErrPrint("Invalid package name (%s)\n", lbid);
956                 sqlite3_reset(stmt);
957                 sqlite3_finalize(stmt);
958                 goto out;
959         }
960
961         pkgid = (const char *)sqlite3_column_text(stmt, 1);
962         if (!pkgid || !strlen(pkgid)) {
963                 /*
964                  * This record has no uiapp.
965                  * Try to find the main ui-app id.
966                  */
967                 ret = pkgmgr_get_mainapp(tmp);
968         } else {
969                 ret = strdup(pkgid);
970                 if (!ret) {
971                         ErrPrint("Error: %s\n", strerror(errno));
972                 }
973         }
974
975         sqlite3_reset(stmt);
976         sqlite3_finalize(stmt);
977
978 out:
979         close_db(handle);
980         return ret;
981 }
982
983 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
984 {
985         sqlite3_stmt *stmt;
986         sqlite3 *handle;
987         int size;
988         int ret;
989
990         if (!types || !cnt || !pkgid) {
991                 return LB_STATUS_ERROR_INVALID;
992         }
993
994         handle = open_db();
995         if (!handle) {
996                 return LB_STATUS_ERROR_IO;
997         }
998
999         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1000         if (ret != SQLITE_OK) {
1001                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1002                 ret = LB_STATUS_ERROR_IO;
1003                 goto out;
1004         }
1005
1006         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1007         if (ret != SQLITE_OK) {
1008                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1009                 sqlite3_reset(stmt);
1010                 sqlite3_finalize(stmt);
1011                 ret = LB_STATUS_ERROR_IO;
1012                 goto out;
1013         }
1014
1015         if (*cnt > NR_OF_SIZE_LIST) {
1016                 *cnt = NR_OF_SIZE_LIST;
1017         }
1018
1019         ret = 0;
1020         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1021                 size = sqlite3_column_int(stmt, 0);
1022                 types[ret] = size;
1023                 ret++;
1024         }
1025
1026         *cnt = ret;
1027         sqlite3_reset(stmt);
1028         sqlite3_finalize(stmt);
1029         ret = LB_STATUS_SUCCESS;
1030 out:
1031         close_db(handle);
1032         return ret;
1033 }
1034
1035 static inline char *cur_locale(void)
1036 {
1037         char *language;
1038         language = vconf_get_str(VCONFKEY_LANGSET);
1039         if (language) {
1040                 char *ptr;
1041
1042                 ptr = language;
1043                 while (*ptr) {
1044                         if (*ptr == '.') {
1045                                 *ptr = '\0';
1046                                 break;
1047                         }
1048
1049                         if (*ptr == '_') {
1050                                 *ptr = '-';
1051                         }
1052
1053                         ptr++;
1054                 }
1055         } else {
1056                 language = strdup("en-us");
1057                 if (!language) {
1058                         ErrPrint("Heap: %s\n", strerror(errno));
1059                 }
1060         }
1061
1062         return language;
1063 }
1064
1065 static inline char *get_default_name(const char *pkgid)
1066 {
1067         sqlite3_stmt *stmt;
1068         sqlite3 *handle;
1069         char *name = NULL;
1070         int ret;
1071
1072         handle = open_db();
1073         if (!handle) {
1074                 return NULL;
1075         }
1076
1077         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1078         if (ret != SQLITE_OK) {
1079                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1080                 close_db(handle);
1081                 return NULL;
1082         }
1083
1084         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1085         if (ret != SQLITE_OK) {
1086                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1087                 goto out;
1088         }
1089
1090         ret = sqlite3_step(stmt);
1091         if (ret ==  SQLITE_ROW) {
1092                 const char *tmp;
1093
1094                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1095                 if (tmp && strlen(tmp)) {
1096                         name = strdup(tmp);
1097                         if (!name) {
1098                                 ErrPrint("Heap: %s\n", strerror(errno));
1099                         }
1100                 }
1101         }
1102
1103 out:
1104         sqlite3_reset(stmt);
1105         sqlite3_finalize(stmt);
1106         close_db(handle);
1107         return name;
1108 }
1109
1110 static inline char *get_default_icon(const char *pkgid)
1111 {
1112         sqlite3_stmt *stmt;
1113         sqlite3 *handle;
1114         char *icon = NULL;
1115         int ret;
1116
1117         handle = open_db();
1118         if (!handle) {
1119                 return NULL;
1120         }
1121
1122         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1123         if (ret != SQLITE_OK) {
1124                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1125                 close_db(handle);
1126                 return NULL;
1127         }
1128
1129         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1130         if (ret != SQLITE_OK) {
1131                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1132                 goto out;
1133         }
1134
1135         ret = sqlite3_step(stmt);
1136         if (ret == SQLITE_ROW) {
1137                 const char *tmp;
1138
1139                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1140                 if (tmp && strlen(tmp)) {
1141                         icon = strdup(tmp);
1142                         if (!icon) {
1143                                 ErrPrint("Heap: %s\n", strerror(errno));
1144                         }
1145                 }
1146         }
1147
1148 out:
1149         sqlite3_reset(stmt);
1150         sqlite3_finalize(stmt);
1151         close_db(handle);
1152         return icon;
1153 }
1154
1155 EAPI char *livebox_service_content(const char *pkgid)
1156 {
1157         sqlite3_stmt *stmt;
1158         sqlite3 *handle;
1159         char *content = NULL;
1160         int ret;
1161
1162         handle = open_db();
1163         if (!handle) {
1164                 return NULL;
1165         }
1166
1167         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1168         if (ret != SQLITE_OK) {
1169                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1170                 close_db(handle);
1171                 return NULL;
1172         }
1173
1174         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1175         if (ret != SQLITE_OK) {
1176                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1177                 goto out;
1178         }
1179
1180         ret = sqlite3_step(stmt);
1181         if (ret == SQLITE_ROW) {
1182                 const char *tmp;
1183
1184                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1185                 if (tmp && strlen(tmp)) {
1186                         content = strdup(tmp);
1187                         if (!content) {
1188                                 ErrPrint("Heap: %s\n", strerror(errno));
1189                         }
1190                 }
1191         }
1192
1193 out:
1194         sqlite3_reset(stmt);
1195         sqlite3_finalize(stmt);
1196         close_db(handle);
1197         return content;
1198 }
1199
1200 EAPI char *livebox_service_setup_appid(const char *lbid)
1201 {
1202         sqlite3_stmt *stmt;
1203         sqlite3 *handle;
1204         int ret;
1205         char *appid;
1206
1207         handle = open_db();
1208         if (!handle) {
1209                 return NULL;
1210         }
1211
1212         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1213         if (ret != SQLITE_OK) {
1214                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1215                 close_db(handle);
1216                 return NULL;
1217         }
1218
1219         appid = NULL;
1220         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1221         if (ret != SQLITE_OK) {
1222                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1223                 goto out;
1224         }
1225
1226         ret = sqlite3_step(stmt);
1227         if (ret == SQLITE_ROW) {
1228                 const char *tmp;
1229
1230                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1231                 if (!tmp || !strlen(tmp)) {
1232                         goto out;
1233                 }
1234
1235                 appid = strdup(tmp);
1236                 if (!appid) {
1237                         ErrPrint("Error: %s\n", strerror(errno));
1238                 }
1239         }
1240
1241 out:
1242         sqlite3_reset(stmt);
1243         sqlite3_finalize(stmt);
1244         close_db(handle);
1245         return appid;
1246 }
1247
1248 EAPI int livebox_service_nodisplay(const char *pkgid)
1249 {
1250         sqlite3_stmt *stmt;
1251         sqlite3 *handle;
1252         int ret;
1253
1254         handle = open_db();
1255         if (!handle) {
1256                 return 0;
1257         }
1258
1259         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1260         if (ret != SQLITE_OK) {
1261                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1262                 close_db(handle);
1263                 return 0;
1264         }
1265
1266         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1267         if (ret != SQLITE_OK) {
1268                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1269                 ret = 0;
1270                 goto out;
1271         }
1272
1273         ret = sqlite3_step(stmt);
1274         if (ret == SQLITE_ROW) {
1275                 ret = !!sqlite3_column_int(stmt, 0);
1276         } else {
1277                 ret = 0;
1278         }
1279
1280 out:
1281         sqlite3_reset(stmt);
1282         sqlite3_finalize(stmt);
1283         close_db(handle);
1284         return ret;
1285 }
1286
1287 static inline char *get_lb_pkgname_by_appid(const char *appid)
1288 {
1289         sqlite3_stmt *stmt;
1290         char *pkgid;
1291         char *tmp;
1292         sqlite3 *handle;
1293         int ret;
1294
1295         if (!appid) {
1296                 return NULL;
1297         }
1298
1299         pkgid = NULL;
1300         handle = open_db();
1301         if (!handle) {
1302                 return NULL;
1303         }
1304
1305         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1306         if (ret != SQLITE_OK) {
1307                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1308                 close_db(handle);
1309                 return NULL;
1310         }
1311
1312         ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
1313         if (ret != SQLITE_OK) {
1314                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1315                 goto out;
1316         }
1317
1318         ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
1319         if (ret != SQLITE_OK) {
1320                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1321                 goto out;
1322         }
1323
1324         if (sqlite3_step(stmt) != SQLITE_ROW) {
1325                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1326                 goto out;
1327         }
1328
1329         tmp = (char *)sqlite3_column_text(stmt, 0);
1330         if (tmp && strlen(tmp)) {
1331                 pkgid = strdup(tmp);
1332                 if (!pkgid) {
1333                         ErrPrint("Heap: %s\n", strerror(errno));
1334                 }
1335         }
1336
1337 out:
1338         sqlite3_reset(stmt);
1339         sqlite3_finalize(stmt);
1340         close_db(handle);
1341         return pkgid;
1342 }
1343
1344 EAPI int livebox_service_need_frame(const char *pkgid, int size_type)
1345 {
1346         char *lbid;
1347         sqlite3_stmt *stmt;
1348         sqlite3 *handle;
1349         int ret;
1350
1351         handle = open_db();
1352         if (!handle) {
1353                 ErrPrint("Unable to open a DB\n");
1354                 return 0;
1355         }
1356
1357         ret = sqlite3_prepare_v2(handle, "SELECT need_frame FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1358         if (ret != SQLITE_OK) {
1359                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1360                 close_db(handle);
1361                 return 0;
1362         }
1363
1364         /*!
1365          */
1366         lbid = livebox_service_pkgname(pkgid);
1367         if (!lbid) {
1368                 ErrPrint("Invalid appid (%s)\n", pkgid);
1369                 ret = 0;
1370                 goto out;
1371         }
1372
1373         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1374         free(lbid);
1375         if (ret != SQLITE_OK) {
1376                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1377                 ret = 0;
1378                 goto out;
1379         }
1380
1381         ret = sqlite3_bind_int(stmt, 2, size_type);
1382         if (ret != SQLITE_OK) {
1383                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1384                 ret = 0;
1385                 goto out;
1386         }
1387
1388         ret = sqlite3_step(stmt);
1389         if (ret == SQLITE_ROW) {
1390                 ret = !!sqlite3_column_int(stmt, 0);
1391         } else {
1392                 ret = 0;
1393                 ErrPrint("There is no such result\n");
1394         }
1395 out:
1396         sqlite3_reset(stmt);
1397         sqlite3_finalize(stmt);
1398         close_db(handle);
1399         return ret;
1400 }
1401
1402 EAPI int livebox_service_touch_effect(const char *pkgid, int size_type)
1403 {
1404         char *lbid;
1405         sqlite3_stmt *stmt;
1406         sqlite3 *handle;
1407         int ret;
1408
1409         handle = open_db();
1410         if (!handle) {
1411                 ErrPrint("Unable to open a DB\n");
1412                 return 1;
1413         }
1414
1415         ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1416         if (ret != SQLITE_OK) {
1417                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1418                 close_db(handle);
1419                 return 1;
1420         }
1421
1422         /*!
1423          * \note
1424          * This function will validate the "pkgid"
1425          * call the exported API in the exported API is not recomended
1426          * but... I used.
1427          */
1428         lbid = livebox_service_pkgname(pkgid);
1429         if (!lbid) {
1430                 ErrPrint("Invalid appid (%s)\n", pkgid);
1431                 ret = 1;
1432                 goto out;
1433         }
1434
1435         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1436         free(lbid);
1437         if (ret != SQLITE_OK) {
1438                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1439                 ret = 1;
1440                 goto out;
1441         }
1442
1443         ret = sqlite3_bind_int(stmt, 2, size_type);
1444         if (ret != SQLITE_OK) {
1445                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1446                 ret = 1;
1447                 goto out;
1448         }
1449
1450         ret = sqlite3_step(stmt);
1451         if (ret == SQLITE_ROW) {
1452                 ret = !!sqlite3_column_int(stmt, 0);
1453         } else {
1454                 ret = 1; /*!< Default true: In this case the DB is corrupted. */
1455                 ErrPrint("There is no result\n");
1456         }
1457
1458 out:
1459         sqlite3_reset(stmt);
1460         sqlite3_finalize(stmt);
1461         close_db(handle);
1462         return ret;
1463 }
1464
1465 EAPI int livebox_service_mouse_event(const char *pkgid)
1466 {
1467         sqlite3_stmt *stmt;
1468         sqlite3 *handle;
1469         char *lbid;
1470         int ret;
1471
1472         handle = open_db();
1473         if (!handle) {
1474                 return 0;
1475         }
1476
1477         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1478         if (ret != SQLITE_OK) {
1479                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1480                 close_db(handle);
1481                 return 0;
1482         }
1483
1484         lbid = livebox_service_pkgname(pkgid);
1485         if (!lbid) {
1486                 ErrPrint("Failed to get lbid: %s\n", pkgid);
1487                 ret = 0;
1488                 goto out;
1489         }
1490
1491         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1492         free(lbid);
1493         if (ret != SQLITE_OK) {
1494                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1495                 ret = 0;
1496                 goto out;
1497         }
1498
1499         ret = sqlite3_step(stmt);
1500         if (ret == SQLITE_ROW) {
1501                 ret = !!sqlite3_column_int(stmt, 0);
1502         } else {
1503                 ret = 0; /*!< Default is false, In this case the DB is corrupted */
1504                 ErrPrint("There is no result.\n");
1505         }
1506
1507 out:
1508         sqlite3_reset(stmt);
1509         sqlite3_finalize(stmt);
1510         close_db(handle);
1511         return ret;
1512 }
1513
1514 static inline int update_lang_info(void)
1515 {
1516         char *syslang;
1517         UErrorCode err;
1518
1519         syslang = vconf_get_str(VCONFKEY_LANGSET);
1520         if (!syslang) {
1521                 ErrPrint("Failed to get vconf-lang\n");
1522                 return -EFAULT;
1523         }
1524
1525         if (s_info.syslang && !strcmp(s_info.syslang, syslang)) {
1526                 DbgPrint("Syslang is not changed: %s\n", syslang);
1527                 free(syslang);
1528                 return 0;
1529         }
1530
1531         free(s_info.syslang);
1532         s_info.syslang = syslang;
1533
1534         err = U_ZERO_ERROR;
1535         uloc_setDefault((const char *)s_info.syslang, &err);
1536         if (!U_SUCCESS(err)) {
1537                 ErrPrint("Failed to set default lang: %s\n", u_errorName(err));
1538                 free(s_info.syslang);
1539                 s_info.syslang = NULL;
1540                 return -EFAULT;
1541         }
1542
1543         s_info.iso3lang = uloc_getISO3Language(uloc_getDefault());
1544         if (!s_info.iso3lang || !strlen(s_info.iso3lang)) {
1545                 ErrPrint("Failed to get iso3lang\n");
1546                 free(s_info.syslang);
1547                 s_info.syslang = NULL;
1548                 return -EFAULT;
1549         }
1550
1551         err = U_ZERO_ERROR;
1552         s_info.country_len = uloc_getCountry(uloc_getDefault(), s_info.country, ULOC_COUNTRY_CAPACITY, &err);
1553         if (!U_SUCCESS(err) || s_info.country_len <= 0) {
1554                 ErrPrint("Failed to get locale: %s, %s, %d (%s)\n", u_errorName(err), s_info.iso3lang, s_info.country_len, s_info.country);
1555                 free(s_info.syslang);
1556                 s_info.syslang = NULL;
1557                 return -EFAULT;
1558         }
1559
1560         return 0;
1561 }
1562
1563 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
1564 {
1565         sqlite3_stmt *stmt;
1566         sqlite3 *handle;
1567         int ret;
1568         char *preview = NULL;
1569         const char *tmp;
1570         int tmp_len;
1571         int buf_len;
1572         register int i;
1573         int printed;
1574
1575         handle = open_db();
1576         if (!handle) {
1577                 return NULL;
1578         }
1579
1580         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1581         if (ret != SQLITE_OK) {
1582                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1583                 close_db(handle);
1584                 return NULL;
1585         }
1586
1587         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1588         if (ret != SQLITE_OK) {
1589                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1590                 goto out;
1591         }
1592
1593         ret = sqlite3_bind_int(stmt, 2, size_type);
1594         if (ret != SQLITE_OK) {
1595                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1596                 goto out;
1597         }
1598
1599         ret = sqlite3_step(stmt);
1600         if (ret != SQLITE_ROW) {
1601                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1602                 goto out;
1603         }
1604
1605         tmp = (const char *)sqlite3_column_text(stmt, 0);
1606         if (!tmp || !(tmp_len = strlen(tmp))) {
1607                 ErrPrint("Failed to get data (%s)\n", pkgid);
1608                 goto out;
1609         }
1610
1611         if (update_lang_info() != 0) {
1612                 preview = strdup(tmp);
1613                 if (!preview) {
1614                         ErrPrint("Heap: %s\n", strerror(errno));
1615                 }
1616                 goto out;
1617         }
1618
1619         buf_len = tmp_len + strlen(s_info.iso3lang) + s_info.country_len + 3; /* '/' '-' '/' */
1620         preview = malloc(buf_len + 1);
1621         if (!preview) {
1622                 ErrPrint("Heap: %s\n", strerror(errno));
1623                 goto out;
1624         }
1625
1626         for (i = tmp_len; i >= 0 && tmp[i] != '/'; i--);
1627         i++; /* Skip '/' */
1628
1629         strncpy(preview, tmp, i);
1630         printed = snprintf(preview + i, buf_len - i, "%s-%s/%s", s_info.iso3lang, s_info.country, tmp + i);
1631         if (preview[i + printed] != '\0') {
1632                 ErrPrint("Path is truncated\n");
1633                 preview[i + printed] = '\0';
1634         }
1635
1636         if (access(preview, R_OK) != 0) {
1637                 DbgPrint("Access failed: %s, %s\n", preview, strerror(errno));
1638                 free(preview);
1639
1640                 preview = strdup(tmp);
1641                 if (!preview) {
1642                         ErrPrint("Heap: %s\n", strerror(errno));
1643                 }
1644         }
1645
1646 out:
1647         sqlite3_reset(stmt);
1648         sqlite3_finalize(stmt);
1649         close_db(handle);
1650         return preview;
1651 }
1652
1653 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1654 {
1655         sqlite3_stmt *stmt;
1656         sqlite3 *handle;
1657         char *language;
1658         char *icon = NULL;
1659         int ret;
1660
1661         if (lang) {
1662                 language = strdup(lang);
1663                 if (!language) {
1664                         ErrPrint("Heap: %s\n", strerror(errno));
1665                         return NULL;
1666                 }
1667         } else {
1668                 language = cur_locale();
1669                 if (!language) {
1670                         return NULL;
1671                 }
1672         }
1673
1674         handle = open_db();
1675         if (!handle) {
1676                 free(language);
1677                 return NULL;
1678         }
1679
1680         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1681         if (ret != SQLITE_OK) {
1682                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1683                 close_db(handle);
1684                 free(language);
1685                 return NULL;
1686         }
1687
1688         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1689         if (ret != SQLITE_OK) {
1690                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1691                 goto out;
1692         }
1693
1694         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1695         if (ret != SQLITE_OK) {
1696                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1697                 goto out;
1698         }
1699
1700         ret = sqlite3_step(stmt);
1701         if (ret == SQLITE_ROW) {
1702                 const char *tmp;
1703                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1704                 if (!tmp || !strlen(tmp)) {
1705                         icon = get_default_icon(pkgid);
1706                 } else {
1707                         icon = strdup(tmp);
1708                         if (!icon) {
1709                                 ErrPrint("Heap: %s\n", strerror(errno));
1710                         }
1711                 }
1712         } else {
1713                 icon = get_default_icon(pkgid);
1714         }
1715
1716 out:
1717         sqlite3_reset(stmt);
1718         sqlite3_finalize(stmt);
1719         close_db(handle);
1720         free(language);
1721         return icon;
1722 }
1723
1724 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1725 {
1726         sqlite3_stmt *stmt;
1727         sqlite3 *handle;
1728         char *language;
1729         char *name = NULL;
1730         int ret;
1731
1732         if (lang) {
1733                 language = strdup(lang);
1734                 if (!language) {
1735                         ErrPrint("Error: %s\n", strerror(errno));
1736                         return NULL;
1737                 }
1738         } else {
1739                 language = cur_locale();
1740                 if (!language) {
1741                         return NULL;
1742                 }
1743         }
1744
1745         handle = open_db();
1746         if (!handle) {
1747                 free(language);
1748                 return NULL;
1749         }
1750
1751         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1752         if (ret != SQLITE_OK) {
1753                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1754                 close_db(handle);
1755                 free(language);
1756                 return NULL;
1757         }
1758
1759         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1760         if (ret != SQLITE_OK) {
1761                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1762                 goto out;
1763         }
1764
1765         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1766         if (ret != SQLITE_OK) {
1767                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1768                 goto out;
1769         }
1770
1771         ret = sqlite3_step(stmt);
1772         if (ret == SQLITE_ROW) {
1773                 const char *tmp;
1774                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1775                 if (!tmp || !strlen(tmp)) {
1776                         name = get_default_name(pkgid);
1777                 } else {
1778                         name = strdup(tmp);
1779                         if (!name) {
1780                                 ErrPrint("Heap: %s\n", strerror(errno));
1781                         }
1782                 }
1783         } else {
1784                 name = get_default_name(pkgid);
1785         }
1786
1787 out:
1788         sqlite3_reset(stmt);
1789         sqlite3_finalize(stmt);
1790         close_db(handle);
1791         free(language);
1792         return name;
1793 }
1794
1795 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1796 {
1797         sqlite3_stmt *stmt;
1798         sqlite3 *handle;
1799         int size;
1800         int ret;
1801
1802         if (!w || !h || !cnt || !pkgid) {
1803                 return LB_STATUS_ERROR_INVALID;
1804         }
1805
1806         handle = open_db();
1807         if (!handle) {
1808                 return LB_STATUS_ERROR_IO;
1809         }
1810
1811         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1812         if (ret != SQLITE_OK) {
1813                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1814                 ret = LB_STATUS_ERROR_IO;
1815                 goto out;
1816         }
1817
1818         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1819         if (ret != SQLITE_OK) {
1820                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1821                 sqlite3_reset(stmt);
1822                 sqlite3_finalize(stmt);
1823                 ret = LB_STATUS_ERROR_IO;
1824                 goto out;
1825         }
1826
1827         if (*cnt > NR_OF_SIZE_LIST) {
1828                 *cnt = NR_OF_SIZE_LIST;
1829         }
1830
1831         ret = 0;
1832         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1833                 size = sqlite3_column_int(stmt, 0);
1834                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1835         }
1836
1837         *cnt = ret;
1838         sqlite3_reset(stmt);
1839         sqlite3_finalize(stmt);
1840         ret = 0;
1841 out:
1842         close_db(handle);
1843         return ret;
1844 }
1845
1846 EAPI char *livebox_service_libexec(const char *pkgid)
1847 {
1848         sqlite3_stmt *stmt;
1849         sqlite3 *handle;
1850         int ret;
1851         char *libexec;
1852         char *appid;
1853         char *path;
1854
1855         if (!pkgid) {
1856                 return NULL;
1857         }
1858
1859         libexec = NULL;
1860         handle = open_db();
1861         if (!handle) {
1862                 return NULL;
1863         }
1864
1865         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1866         if (ret != SQLITE_OK) {
1867                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1868                 goto out;
1869         }
1870
1871         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1872         if (ret != SQLITE_OK) {
1873                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1874                 sqlite3_finalize(stmt);
1875                 goto out;
1876         }
1877
1878         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1879         if (ret != SQLITE_OK) {
1880                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1881                 sqlite3_finalize(stmt);
1882                 goto out;
1883         }
1884
1885         if (sqlite3_step(stmt) != SQLITE_ROW) {
1886                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1887                 sqlite3_reset(stmt);
1888                 sqlite3_finalize(stmt);
1889
1890                 libexec = util_conf_get_libexec(pkgid);
1891                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1892                 goto out;
1893         }
1894
1895         appid = (char *)sqlite3_column_text(stmt, 0);
1896         if (!appid || !strlen(appid)) {
1897                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1898                 sqlite3_reset(stmt);
1899                 sqlite3_finalize(stmt);
1900                 goto out;
1901         }
1902
1903         path = (char *)sqlite3_column_text(stmt, 1);
1904         if (!path || !strlen(path)) {
1905                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1906                 sqlite3_reset(stmt);
1907                 sqlite3_finalize(stmt);
1908                 goto out;
1909         }
1910
1911         libexec = strdup(path);
1912         if (!libexec) {
1913                 ErrPrint("Heap: %s\n", strerror(errno));
1914                 sqlite3_reset(stmt);
1915                 sqlite3_finalize(stmt);
1916                 goto out;
1917         }
1918
1919         DbgPrint("libexec: %s\n", libexec);
1920
1921         sqlite3_reset(stmt);
1922         sqlite3_finalize(stmt);
1923 out:
1924         close_db(handle);
1925         return libexec;
1926 }
1927
1928 EAPI char *livebox_service_pkgname(const char *appid)
1929 {
1930         char *lb_pkgname;
1931         pkgmgr_appinfo_h handle;
1932         int ret;
1933         char *new_appid;
1934
1935         if (!appid) {
1936                 return NULL;
1937         }
1938
1939         lb_pkgname = get_lb_pkgname_by_appid(appid);
1940         if (lb_pkgname) {
1941                 return lb_pkgname;
1942         }
1943
1944         /*!
1945          * \note
1946          * Try to get the package id using given appid
1947          */
1948         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1949         if (ret != PKGMGR_R_OK) {
1950                 ErrPrint("Failed to get appinfo\n");
1951                 return NULL;
1952         }
1953
1954         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1955         if (ret != PKGMGR_R_OK) {
1956                 pkgmgr_appinfo_destroy_appinfo(handle);
1957                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1958                 return NULL;
1959         }
1960
1961         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1962         pkgmgr_appinfo_destroy_appinfo(handle);
1963
1964         if (!lb_pkgname && util_validate_livebox_package(appid) == 0) {
1965                 return strdup(appid);
1966         }
1967
1968         return lb_pkgname;
1969 }
1970
1971 EAPI char *livebox_service_provider_name(const char *lbid)
1972 {
1973         char *ret;
1974         int stage = 0;
1975         int seq = 0;
1976         int idx = 0;
1977         char *str = SAMSUNG_PREFIX;
1978
1979         if (!lbid) {
1980                 return NULL;
1981         }
1982
1983         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1984                 idx++;
1985                 if (seq < 2 && lbid[idx] == '.') {
1986                         stage = idx;
1987                         seq++;
1988                 }
1989         }
1990
1991         if (!str[idx] && lbid[idx]) {
1992                 /* Inhouse */
1993                 return strdup(lbid);
1994         } else if (seq < 2) {
1995                 while (seq < 2) {
1996                         if (lbid[idx] == '.') {
1997                                 seq++;
1998                         } else if (!lbid[idx]) {
1999                                 ErrPrint("Invalid lbid: %s\n", lbid);
2000                                 return NULL;
2001                         }
2002
2003                         idx++;
2004                 }
2005
2006                 stage = idx;
2007         } else {
2008                 stage++;
2009         }
2010
2011         ret = strdup(lbid + stage);
2012         if (!ret) {
2013                 ErrPrint("Error: %s\n", strerror(errno));
2014                 return NULL;
2015         }
2016
2017         return ret;
2018 }
2019
2020 EAPI int livebox_service_is_enabled(const char *lbid)
2021 {
2022         return 1;
2023         /*
2024         ail_appinfo_h ai;
2025         char *pkgname;
2026         bool enabled;
2027         int ret;
2028
2029         pkgname = livebox_service_appid(lbid);
2030         if (!pkgname)
2031                 return 0;
2032
2033         ret = ail_get_appinfo(pkgname, &ai);
2034         if (ret != AIL_ERROR_OK) {
2035                 free(pkgname);
2036                 return 0;
2037         }
2038
2039         if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
2040                 enabled = false;
2041
2042         ail_destroy_appinfo(ai);
2043         free(pkgname);
2044         return enabled == true;
2045         */
2046 }
2047
2048 EAPI int livebox_service_is_primary(const char *lbid)
2049 {
2050         sqlite3_stmt *stmt;
2051         sqlite3 *handle;
2052         int ret = 0;
2053
2054         if (!lbid) {
2055                 return 0;
2056         }
2057
2058         handle = open_db();
2059         if (!handle) {
2060                 return 0;
2061         }
2062
2063         ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
2064         if (ret != SQLITE_OK) {
2065                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2066                 close_db(handle);
2067                 return 0;
2068         }
2069
2070         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
2071         if (ret != SQLITE_OK) {
2072                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2073                 goto out;
2074         }
2075
2076         ret = sqlite3_step(stmt);
2077         if (ret != SQLITE_ROW) {
2078                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2079                 goto out;
2080         }
2081
2082         ret = sqlite3_column_int(stmt, 0);
2083
2084 out:
2085         sqlite3_reset(stmt);
2086         sqlite3_finalize(stmt);
2087         close_db(handle);
2088         return ret;
2089 }
2090
2091 EAPI char *livebox_service_appid(const char *pkgname)
2092 {
2093         sqlite3_stmt *stmt;
2094         char *appid;
2095         char *tmp;
2096         sqlite3 *handle;
2097         int is_prime __attribute__((__unused__));
2098         int ret;
2099
2100         if (!pkgname) {
2101                 return NULL;
2102         }
2103
2104         appid = NULL;
2105         handle = open_db();
2106         if (!handle) {
2107                 return NULL;
2108         }
2109
2110         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
2111         if (ret != SQLITE_OK) {
2112                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2113                 goto out;
2114         }
2115
2116         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
2117         if (ret != SQLITE_OK) {
2118                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2119                 sqlite3_reset(stmt);
2120                 sqlite3_finalize(stmt);
2121                 goto out;
2122         }
2123
2124         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
2125         if (ret != SQLITE_OK) {
2126                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2127                 sqlite3_reset(stmt);
2128                 sqlite3_finalize(stmt);
2129                 goto out;
2130         }
2131
2132         ret = sqlite3_step(stmt);
2133         if (ret != SQLITE_ROW) {
2134                 pkgmgr_appinfo_h pkg_handle;
2135                 char *new_appid;
2136
2137                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
2138                 sqlite3_reset(stmt);
2139                 sqlite3_finalize(stmt);
2140
2141                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
2142                 if (ret != PKGMGR_R_OK) {
2143                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
2144                         goto out;
2145                 }
2146
2147                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
2148                 if (ret != PKGMGR_R_OK) {
2149                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
2150                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
2151                         goto out;
2152                 }
2153
2154                 appid = strdup(new_appid);
2155                 if (!appid) {
2156                         ErrPrint("Heap: %s\n", strerror(errno));
2157                 }
2158
2159                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
2160                 goto out;
2161         }
2162
2163         tmp = (char *)sqlite3_column_text(stmt, 0);
2164         if (!tmp || !strlen(tmp)) {
2165                 ErrPrint("APPID is NIL\n");
2166                 sqlite3_reset(stmt);
2167                 sqlite3_finalize(stmt);
2168                 goto out;
2169         }
2170
2171         appid = strdup(tmp);
2172         if (!appid) {
2173                 ErrPrint("Heap: %s\n", strerror(errno));
2174                 sqlite3_reset(stmt);
2175                 sqlite3_finalize(stmt);
2176                 goto out;
2177         }
2178
2179         is_prime = sqlite3_column_int(stmt, 1);
2180
2181         sqlite3_reset(stmt);
2182         sqlite3_finalize(stmt);
2183 out:
2184         close_db(handle);
2185         return appid;
2186 }
2187
2188 EAPI char *livebox_service_lb_script_path(const char *pkgid)
2189 {
2190         sqlite3_stmt *stmt;
2191         sqlite3 *handle;
2192         int ret;
2193         char *path;
2194         char *appid;
2195         char *lb_src;
2196
2197         if (!pkgid) {
2198                 return NULL;
2199         }
2200
2201         path = NULL;
2202         handle = open_db();
2203         if (!handle) {
2204                 return NULL;
2205         }
2206
2207         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
2208         if (ret != SQLITE_OK) {
2209                 ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2210                 goto out;
2211         }
2212
2213         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2214         if (ret != SQLITE_OK) {
2215                 ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2216                 sqlite3_finalize(stmt);
2217                 goto out;
2218         }
2219
2220         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
2221         if (ret != SQLITE_OK) {
2222                 ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2223                 sqlite3_finalize(stmt);
2224                 goto out;
2225         }
2226
2227         ret = sqlite3_step(stmt);
2228         if (ret != SQLITE_ROW) {
2229                 ErrPrint("Error: %s, pkgid(%s), ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2230                 sqlite3_reset(stmt);
2231                 sqlite3_finalize(stmt);
2232                 goto out;
2233         }
2234
2235         appid = (char *)sqlite3_column_text(stmt, 0);
2236         if (!appid || !strlen(appid)) {
2237                 ErrPrint("Invalid appid : %s, pkgid(%s)\n", sqlite3_errmsg(handle), pkgid);
2238                 sqlite3_reset(stmt);
2239                 sqlite3_finalize(stmt);
2240                 goto out;
2241         }
2242
2243         lb_src = (char *)sqlite3_column_text(stmt, 1);
2244         if (!lb_src || !strlen(lb_src)) {
2245                 ErrPrint("No records for lb src : %s, pkgid(%s), appid(%s)\n", sqlite3_errmsg(handle), pkgid, appid);
2246                 sqlite3_reset(stmt);
2247                 sqlite3_finalize(stmt);
2248                 goto out;
2249         }
2250
2251         path = strdup(lb_src);
2252         if (!path) {
2253                 ErrPrint("Heap: %s\n", strerror(errno));
2254                 sqlite3_reset(stmt);
2255                 sqlite3_finalize(stmt);
2256                 goto out;
2257         }
2258
2259         DbgPrint("LB Src: %s\n", path);
2260         sqlite3_reset(stmt);
2261         sqlite3_finalize(stmt);
2262 out:
2263         close_db(handle);
2264         return path;
2265 }
2266
2267 EAPI char *livebox_service_lb_script_group(const char *pkgid)
2268 {
2269         sqlite3_stmt *stmt;
2270         sqlite3 *handle;
2271         int ret;
2272         char *group;
2273         char *tmp;
2274
2275         if (!pkgid) {
2276                 return NULL;
2277         }
2278
2279         group = NULL;
2280         handle = open_db();
2281         if (!handle) {
2282                 return NULL;
2283         }
2284
2285         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2286         if (ret != SQLITE_OK) {
2287                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2288                 goto out;
2289         }
2290
2291         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2292         if (ret != SQLITE_OK) {
2293                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2294                 sqlite3_finalize(stmt);
2295                 goto out;
2296         }
2297
2298         ret = sqlite3_step(stmt);
2299         if (ret != SQLITE_ROW) {
2300                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2301                 sqlite3_reset(stmt);
2302                 sqlite3_finalize(stmt);
2303                 goto out;
2304         }
2305
2306         tmp = (char *)sqlite3_column_text(stmt, 0);
2307         if (tmp && strlen(tmp)) {
2308                 group = strdup(tmp);
2309                 if (!group) {
2310                         ErrPrint("Heap: %s\n", strerror(errno));
2311                 }
2312         }
2313
2314         sqlite3_reset(stmt);
2315         sqlite3_finalize(stmt);
2316 out:
2317         close_db(handle);
2318         return group;
2319 }
2320
2321 EAPI char *livebox_service_pd_script_path(const char *pkgid)
2322 {
2323         sqlite3_stmt *stmt;
2324         sqlite3 *handle;
2325         int ret;
2326         char *path;
2327         char *pd_src;
2328         const char *appid;
2329
2330         if (!pkgid) {
2331                 return NULL;
2332         }
2333
2334         path = NULL;
2335         handle = open_db();
2336         if (!handle) {
2337                 return NULL;
2338         }
2339
2340         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
2341         if (ret != SQLITE_OK) {
2342                 ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2343                 goto out;
2344         }
2345
2346         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2347         if (ret != SQLITE_OK) {
2348                 ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2349                 sqlite3_finalize(stmt);
2350                 goto out;
2351         }
2352
2353         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
2354         if (ret != SQLITE_OK) {
2355                 ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2356                 sqlite3_finalize(stmt);
2357                 goto out;
2358         }
2359
2360         ret = sqlite3_step(stmt);
2361         if (ret != SQLITE_ROW) {
2362                 ErrPrint("Error: %s pkgid(%s) ret(%d)\n", sqlite3_errmsg(handle), pkgid, ret);
2363                 sqlite3_reset(stmt);
2364                 sqlite3_finalize(stmt);
2365                 goto out;
2366         }
2367
2368         appid = (char *)sqlite3_column_text(stmt, 0);
2369         if (!appid || !strlen(appid)) {
2370                 ErrPrint("Error: %s pkgid(%s)\n", sqlite3_errmsg(handle), pkgid);
2371                 sqlite3_reset(stmt);
2372                 sqlite3_finalize(stmt);
2373                 goto out;
2374         }
2375
2376         pd_src = (char *)sqlite3_column_text(stmt, 1);
2377         if (!pd_src || !strlen(pd_src)) {
2378                 ErrPrint("Error: %s pkgid(%s) appid(%s)\n", sqlite3_errmsg(handle), pkgid, appid);
2379                 sqlite3_reset(stmt);
2380                 sqlite3_finalize(stmt);
2381                 goto out;
2382         }
2383
2384         path = strdup(pd_src);
2385         if (!path) {
2386                 ErrPrint("Heap: %s\n", strerror(errno));
2387                 sqlite3_reset(stmt);
2388                 sqlite3_finalize(stmt);
2389                 goto out;
2390         }
2391
2392         DbgPrint("PD Src: %s\n", path);
2393         sqlite3_reset(stmt);
2394         sqlite3_finalize(stmt);
2395 out:
2396         close_db(handle);
2397         return path;
2398 }
2399
2400 EAPI char *livebox_service_pd_script_group(const char *pkgid)
2401 {
2402         sqlite3_stmt *stmt;
2403         sqlite3 *handle;
2404         int ret;
2405         char *group;
2406         char *tmp;
2407
2408         if (!pkgid) {
2409                 return NULL;
2410         }
2411
2412         group = NULL;
2413         handle = open_db();
2414         if (!handle) {
2415                 return NULL;
2416         }
2417
2418         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2419         if (ret != SQLITE_OK) {
2420                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2421                 goto out;
2422         }
2423
2424         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2425         if (ret != SQLITE_OK) {
2426                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2427                 sqlite3_finalize(stmt);
2428                 goto out;
2429         }
2430
2431         ret = sqlite3_step(stmt);
2432         if (ret != SQLITE_ROW) {
2433                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2434                 sqlite3_reset(stmt);
2435                 sqlite3_finalize(stmt);
2436                 goto out;
2437         }
2438
2439         tmp = (char *)sqlite3_column_text(stmt, 0);
2440         if (tmp && strlen(tmp)) {
2441                 group = strdup(tmp);
2442                 if (!group) {
2443                         ErrPrint("Heap: %s\n", strerror(errno));
2444                 }
2445         }
2446         sqlite3_reset(stmt);
2447         sqlite3_finalize(stmt);
2448 out:
2449         close_db(handle);
2450         return group;
2451 }
2452
2453 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
2454 {
2455         sqlite3_stmt *stmt;
2456         sqlite3 *handle;
2457         const char *cluster;
2458         int cnt;
2459         int ret;
2460
2461         if (!cb) {
2462                 return LB_STATUS_ERROR_INVALID;
2463         }
2464
2465         handle = open_db();
2466         if (!handle) {
2467                 return LB_STATUS_ERROR_IO;
2468         }
2469
2470         cnt = 0;
2471         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
2472         if (ret != SQLITE_OK) {
2473                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2474                 cnt = LB_STATUS_ERROR_IO;
2475                 goto out;
2476         }
2477
2478         while (sqlite3_step(stmt) == SQLITE_ROW) {
2479                 cluster = (const char *)sqlite3_column_text(stmt, 0);
2480                 if (!cluster || !strlen(cluster)) {
2481                         continue;
2482                 }
2483
2484                 if (cb(cluster, data) < 0) {
2485                         break;
2486                 }
2487
2488                 cnt++;
2489         }
2490
2491         sqlite3_reset(stmt);
2492         sqlite3_finalize(stmt);
2493 out:
2494         close_db(handle);
2495         return cnt;
2496 }
2497
2498 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
2499 {
2500         sqlite3_stmt *stmt;
2501         sqlite3 *handle;
2502         const char *category;
2503         int cnt;
2504         int ret;
2505
2506         if (!cluster || !cb) {
2507                 return LB_STATUS_ERROR_INVALID;
2508         }
2509
2510         handle = open_db();
2511         if (!handle) {
2512                 return LB_STATUS_ERROR_IO;
2513         }
2514
2515         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
2516         if (ret != SQLITE_OK) {
2517                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2518                 cnt = LB_STATUS_ERROR_IO;
2519                 goto out;
2520         }
2521
2522         cnt = 0;
2523         while (sqlite3_step(stmt) == SQLITE_ROW) {
2524                 category = (const char *)sqlite3_column_text(stmt, 0);
2525                 if (!category || !strlen(category)) {
2526                         continue;
2527                 }
2528
2529                 if (cb(cluster, category, data) < 0) {
2530                         break;
2531                 }
2532
2533                 cnt++;
2534         }
2535
2536         sqlite3_reset(stmt);
2537         sqlite3_finalize(stmt);
2538 out:
2539         close_db(handle);
2540         return cnt;
2541 }
2542
2543 EAPI int livebox_service_init(void)
2544 {
2545         if (s_info.handle) {
2546                 DbgPrint("Already initialized\n");
2547                 s_info.init_count++;
2548                 return 0;
2549         }
2550
2551         s_info.handle = open_db();
2552         if (s_info.handle) {
2553                 s_info.init_count++;
2554                 return 0;
2555         }
2556
2557         return LB_STATUS_ERROR_IO;
2558 }
2559
2560 EAPI int livebox_service_fini(void)
2561 {
2562         if (!s_info.handle || s_info.init_count <= 0) {
2563                 ErrPrint("Service is not initialized\n");
2564                 return LB_STATUS_ERROR_IO;
2565         }
2566
2567         s_info.init_count--;
2568         if (s_info.init_count > 0) {
2569                 DbgPrint("Init count %d\n", s_info.init_count);
2570                 return 0;
2571         }
2572
2573         db_util_close(s_info.handle);
2574         s_info.handle = NULL;
2575         return 0;
2576 }
2577
2578 EAPI int livebox_service_get_size(int type, int *width, int *height)
2579 {
2580         int _width;
2581         int _height;
2582
2583         if (!width) {
2584                 width = &_width;
2585         }
2586
2587         if (!height) {
2588                 height = &_height;
2589         }
2590
2591         return convert_size_from_type(type, width, height);
2592 }
2593
2594 EAPI int livebox_service_size_type(int width, int height)
2595 {
2596         int idx;
2597
2598         if (update_resolution() < 0) {
2599                 ErrPrint("Failed to update the size list\n");
2600         }
2601
2602         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
2603                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height) {
2604                         break;
2605                 }
2606         }
2607
2608         switch (idx) {
2609         case 0:
2610                 return LB_SIZE_TYPE_1x1;
2611         case 1:
2612                 return LB_SIZE_TYPE_2x1;
2613         case 2:
2614                 return LB_SIZE_TYPE_2x2;
2615         case 3:
2616                 return LB_SIZE_TYPE_4x1;
2617         case 4:
2618                 return LB_SIZE_TYPE_4x2;
2619         case 5:
2620                 return LB_SIZE_TYPE_4x3;
2621         case 6:
2622                 return LB_SIZE_TYPE_4x4;
2623         case 7:
2624                 return LB_SIZE_TYPE_4x5;
2625         case 8:
2626                 return LB_SIZE_TYPE_4x6;
2627         case 9:
2628                 return LB_SIZE_TYPE_EASY_1x1;
2629         case 10:
2630                 return LB_SIZE_TYPE_EASY_3x1;
2631         case 11:
2632                 return LB_SIZE_TYPE_EASY_3x3;
2633         case 12:
2634                 return LB_SIZE_TYPE_0x0;
2635         default:
2636                 break;
2637         }
2638
2639         return LB_SIZE_TYPE_UNKNOWN;
2640 }
2641
2642 /* End of a file */