Sync with 3.0
[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(const char *pkgname, const char *id, const char *cluster, const char *category, 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", "ssss", pkgname, uri, cluster, category);
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 /*!
507  * pkgid == Package Id (not the livebox id)
508  */
509 EAPI struct pkglist_handle *livebox_service_pkglist_create(const char *pkgid, struct pkglist_handle *handle)
510 {
511         int ret;
512
513         if (handle) {
514                 if (handle->type != PKGLIST_TYPE_LB_LIST) {
515                         ErrPrint("Invalid handle\n");
516                         return NULL;
517                 }
518
519                 if (pkgid) {
520                         ErrPrint("pkgid should be NULL\n");
521                         return NULL;
522                 }
523
524                 sqlite3_reset(handle->stmt);
525                 return handle;
526         }
527
528         handle = calloc(1, sizeof(*handle));
529         if (!handle) {
530                 ErrPrint("Heap: %s\n", strerror(errno));
531                 return NULL;
532         }
533
534         handle->type = PKGLIST_TYPE_LB_LIST;
535
536         handle->handle = open_db();
537         if (!handle->handle) {
538                 free(handle);
539                 return NULL;
540         }
541
542         if (!pkgid) {
543                 ret = sqlite3_prepare_v2(handle->handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &handle->stmt, NULL);
544                 if (ret != SQLITE_OK) {
545                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
546                         close_db(handle->handle);
547                         free(handle);
548                         return NULL;
549                 }
550         } else {
551                 ret = sqlite3_prepare_v2(handle->handle, "SELECT appid, pkgid, prime FROM pkgmap WHERE appid = ?", -1, &handle->stmt, NULL);
552                 if (ret != SQLITE_OK) {
553                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
554                         close_db(handle->handle);
555                         free(handle);
556                         return NULL;
557                 }
558
559                 ret = sqlite3_bind_text(handle->stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
560                 if (ret != SQLITE_OK) {
561                         ErrPrint("Error: %s\n", sqlite3_errmsg(handle->handle));
562                         sqlite3_finalize(handle->stmt);
563                         close_db(handle->handle);
564                         free(handle);
565                         return NULL;
566                 }
567         }
568
569         return handle;
570 }
571
572 EAPI int livebox_service_get_pkglist_item(struct pkglist_handle *handle, char **appid, char **pkgname, int *is_prime)
573 {
574         const char *tmp;
575         char *_appid = NULL;
576         char *_pkgname = NULL;
577
578         if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
579                 return LB_STATUS_ERROR_INVALID;
580         }
581
582         if (sqlite3_step(handle->stmt) != SQLITE_ROW) {
583                 return LB_STATUS_ERROR_NOT_EXIST;
584         }
585
586         if (appid) {
587                 tmp = (const char *)sqlite3_column_text(handle->stmt, 0);
588                 if (tmp && strlen(tmp)) {
589                         _appid = strdup(tmp);
590                         if (!_appid) {
591                                 ErrPrint("Heap: %s\n", strerror(errno));
592                                 return LB_STATUS_ERROR_MEMORY;
593                         }
594                 }
595         }
596
597         if (pkgname) {
598                 tmp = (const char *)sqlite3_column_text(handle->stmt, 1);
599                 if (tmp && strlen(tmp)) {
600                         _pkgname = strdup(tmp);
601                         if (!_pkgname) {
602                                 ErrPrint("Heap: %s\n", strerror(errno));
603                                 free(_appid);
604                                 return LB_STATUS_ERROR_MEMORY;
605                         }
606                 }
607         }
608
609         if (is_prime) {
610                 *is_prime = sqlite3_column_int(handle->stmt, 2);
611         }
612
613         if (appid) {
614                 *appid = _appid;
615         }
616
617         if (pkgname) {
618                 *pkgname = _pkgname;
619         }
620
621         return LB_STATUS_SUCCESS;
622 }
623
624 EAPI int livebox_service_pkglist_destroy(struct pkglist_handle *handle)
625 {
626         if (!handle || handle->type != PKGLIST_TYPE_LB_LIST) {
627                 return LB_STATUS_ERROR_INVALID;
628         }
629
630         handle->type = PKGLIST_TYPE_UNKNOWN;
631         sqlite3_reset(handle->stmt);
632         sqlite3_finalize(handle->stmt);
633         close_db(handle->handle);
634         free(handle);
635         return LB_STATUS_SUCCESS;
636 }
637
638 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
639 {
640         int ret;
641         sqlite3_stmt *stmt;
642         char *appid;
643         char *pkgid;
644         int is_prime;
645         sqlite3 *handle;
646
647         if (!cb) {
648                 return LB_STATUS_ERROR_INVALID;
649         }
650
651         handle = open_db();
652         if (!handle) {
653                 return LB_STATUS_ERROR_IO;
654         }
655
656         ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
657         if (ret != SQLITE_OK) {
658                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
659                 ret = LB_STATUS_ERROR_IO;
660                 goto out;
661         }
662
663         ret = 0;
664         while (sqlite3_step(stmt) == SQLITE_ROW) {
665                 appid = (char *)sqlite3_column_text(stmt, 0);
666                 if (!appid || !strlen(appid)) {
667                         ErrPrint("APPID is not valid\n");
668                         continue;
669                 }
670
671                 pkgid = (char *)sqlite3_column_text(stmt, 1);
672                 if (!pkgid || !strlen(pkgid)) {
673                         ErrPrint("pkgid is not valid\n");
674                         continue;
675                 }
676
677                 is_prime = sqlite3_column_int(stmt, 2);
678
679                 ret++;
680
681                 if (cb(appid, pkgid, is_prime, data) < 0) {
682                         DbgPrint("Callback stopped package crawling\n");
683                         break;
684                 }
685         }
686
687         sqlite3_reset(stmt);
688         sqlite3_finalize(stmt);
689
690 out:
691         close_db(handle);
692         return ret;
693 }
694
695 EAPI int livebox_service_get_pkglist_by_pkgid(const char *pkgid, int (*cb)(const char *lbid, int is_prime, void *data), void *data)
696 {
697         int ret;
698         sqlite3_stmt *stmt;
699         const char *lbid;
700         int is_prime;
701         sqlite3 *handle;
702
703         if (!cb) {
704                 return LB_STATUS_ERROR_INVALID;
705         }
706
707         handle = open_db();
708         if (!handle) {
709                 return LB_STATUS_ERROR_IO;
710         }
711
712         ret = sqlite3_prepare_v2(handle, "SELECT pkgid, prime FROM pkgmap WHERE appid = ?", -1, &stmt, NULL);
713         if (ret != SQLITE_OK) {
714                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
715                 ret = LB_STATUS_ERROR_IO;
716                 goto out;
717         }
718
719         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
720         if (ret != SQLITE_OK) {
721                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
722                 sqlite3_reset(stmt);
723                 sqlite3_finalize(stmt);
724                 ret = LB_STATUS_ERROR_IO;
725                 goto out;
726         }
727
728         ret = 0;
729         while (sqlite3_step(stmt) == SQLITE_ROW) {
730                 lbid = (const char *)sqlite3_column_text(stmt, 0);
731                 if (!lbid || !strlen(lbid)) {
732                         ErrPrint("LBID is not valid\n");
733                         continue;
734                 }
735
736                 is_prime = sqlite3_column_int(stmt, 1);
737
738                 ret++;
739
740                 if (cb(lbid, is_prime, data) < 0) {
741                         DbgPrint("Callback stopped package crawling\n");
742                         break;
743                 }
744         }
745
746         sqlite3_reset(stmt);
747         sqlite3_finalize(stmt);
748
749 out:
750         close_db(handle);
751         return ret;
752 }
753
754 struct pkgmgr_cbdata {
755         const char *lbid;
756         void (*cb)(const char *lbid, const char *appid, void *data);
757         void *cbdata;
758 };
759
760 static int pkgmgr_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
761 {
762         struct pkgmgr_cbdata *cbdata = (struct pkgmgr_cbdata *)user_data;
763         char *appid;
764         int ret;
765
766         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
767         if (ret < 0) {
768                 ErrPrint("Unable to get appid\n");
769         } else {
770                 cbdata->cb(cbdata->lbid, appid, cbdata->cbdata);
771         }
772
773         return 0;
774 }
775
776 static inline char *pkgmgr_get_mainapp(const char *pkgid)
777 {
778         pkgmgrinfo_pkginfo_h handle;
779         char *ret = NULL;
780
781         if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle) != PMINFO_R_OK) {
782                 ErrPrint("Unable to get mainapp: %s\n", pkgid);
783                 return NULL;
784         }
785
786         if (pkgmgrinfo_pkginfo_get_mainappid(handle, &ret) == PMINFO_R_OK) {
787                 ret = strdup(ret);
788         } else {
789                 ErrPrint("Failed to get mainappid\n");
790                 ret = NULL; /* I cannot believe the pkgmgrinfo_pkginfo_get_mainappid. it maybe able to touch my "ret" even though it fails */
791         
792         }
793
794         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
795         return ret;
796 }
797
798 static inline int pkgmgr_get_applist(const char *pkgid, const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
799 {
800         struct pkgmgr_cbdata cbdata;
801         pkgmgrinfo_pkginfo_h handle;
802         int ret;
803
804         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
805         if (ret < 0) {
806                 ErrPrint("Unable to get pkginfo: %s\n", pkgid);
807                 return ret;
808         }
809
810         cbdata.lbid = lbid;
811         cbdata.cb = cb;
812         cbdata.cbdata = data;
813
814         ret = pkgmgrinfo_appinfo_get_list(handle, PM_UI_APP, pkgmgr_cb, &cbdata);
815         if (ret < 0) {
816                 ErrPrint("Failed to get applist\n");
817         }
818
819         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
820         return ret;
821 }
822
823 EAPI int livebox_service_get_applist(const char *lbid, void (*cb)(const char *lbid, const char *appid, void *data), void *data)
824 {
825         sqlite3_stmt *stmt;
826         const char *tmp;
827         char *pkgid;
828         sqlite3 *handle;
829         int ret;
830
831         if (!lbid || !cb) {
832                 return LB_STATUS_ERROR_INVALID;
833         }
834
835         handle = open_db();
836         if (!handle) {
837                 return LB_STATUS_ERROR_IO;
838         }
839
840         ret = sqlite3_prepare_v2(handle, "SELECT appid FROM pkgmap WHERE (pkgid = ?) or (appid = ?)", -1, &stmt, NULL);
841         if (ret != SQLITE_OK) {
842                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
843                 ret = LB_STATUS_ERROR_IO;
844                 goto out;
845         }
846
847         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
848         if (ret != SQLITE_OK) {
849                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
850                 ret = LB_STATUS_ERROR_IO;
851                 goto out;
852         }
853
854         ret = sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT);
855         if (ret != SQLITE_OK) {
856                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
857                 ret = LB_STATUS_ERROR_IO;
858                 goto out;
859         }
860
861         if (sqlite3_step(stmt) != SQLITE_ROW) {
862                 ret = LB_STATUS_ERROR_INVALID;
863                 sqlite3_reset(stmt);
864                 sqlite3_finalize(stmt);
865                 goto out;
866         }
867
868         tmp = (const char *)sqlite3_column_text(stmt, 0);
869         if (!tmp || !strlen(tmp)) {
870                 ErrPrint("Invalid package name (%s)\n", lbid);
871                 ret = LB_STATUS_ERROR_INVALID;
872                 sqlite3_reset(stmt);
873                 sqlite3_finalize(stmt);
874                 goto out;
875         }
876
877         pkgid = strdup(tmp);
878         if (!pkgid) {
879                 ErrPrint("Error: %s\n", strerror(errno));
880                 ret = LB_STATUS_ERROR_MEMORY;
881                 sqlite3_reset(stmt);
882                 sqlite3_finalize(stmt);
883                 goto out;
884         }
885
886         sqlite3_reset(stmt);
887         sqlite3_finalize(stmt);
888
889         ret = pkgmgr_get_applist(pkgid, lbid, cb, data);
890         free(pkgid);
891
892         switch (ret) {
893         case PMINFO_R_EINVAL:
894                 ret = LB_STATUS_ERROR_INVALID;
895                 break;
896         case PMINFO_R_OK:
897                 ret = LB_STATUS_SUCCESS;
898                 break;
899         case PMINFO_R_ERROR:
900         default:
901                 ret = LB_STATUS_ERROR_FAULT;
902                 break;
903         }
904
905 out:
906         close_db(handle);
907         return ret;
908 }
909
910 EAPI char *livebox_service_mainappid(const char *lbid)
911 {
912         sqlite3_stmt *stmt;
913         const char *tmp;
914         const char *pkgid;
915         sqlite3 *handle;
916         char *ret = NULL;
917
918         if (!lbid) {
919                 return NULL;
920         }
921
922         handle = open_db();
923         if (!handle) {
924                 return NULL;
925         }
926
927         if (sqlite3_prepare_v2(handle, "SELECT appid, uiapp FROM pkgmap WHERE (pkgid = ?) or (appid = ? and prime = 1)", -1, &stmt, NULL) != SQLITE_OK) {
928                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
929                 goto out;
930         }
931
932         if (sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
933                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
934                 goto out;
935         }
936
937         if (sqlite3_bind_text(stmt, 2, lbid, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
938                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
939                 goto out;
940         }
941
942         if (sqlite3_step(stmt) != SQLITE_ROW) {
943                 sqlite3_reset(stmt);
944                 sqlite3_finalize(stmt);
945                 goto out;
946         }
947
948         tmp = (const char *)sqlite3_column_text(stmt, 0);
949         if (!tmp || !strlen(tmp)) {
950                 ErrPrint("Invalid package name (%s)\n", lbid);
951                 sqlite3_reset(stmt);
952                 sqlite3_finalize(stmt);
953                 goto out;
954         }
955
956         pkgid = (const char *)sqlite3_column_text(stmt, 1);
957         if (!pkgid || !strlen(pkgid)) {
958                 /*
959                  * This record has no uiapp.
960                  * Try to find the main ui-app id.
961                  */
962                 ret = pkgmgr_get_mainapp(tmp);
963         } else {
964                 ret = strdup(pkgid);
965                 if (!ret) {
966                         ErrPrint("Error: %s\n", strerror(errno));
967                 }
968         }
969
970         sqlite3_reset(stmt);
971         sqlite3_finalize(stmt);
972
973 out:
974         close_db(handle);
975         return ret;
976 }
977
978 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
979 {
980         sqlite3_stmt *stmt;
981         sqlite3 *handle;
982         int size;
983         int ret;
984
985         if (!types || !cnt || !pkgid) {
986                 return LB_STATUS_ERROR_INVALID;
987         }
988
989         handle = open_db();
990         if (!handle) {
991                 return LB_STATUS_ERROR_IO;
992         }
993
994         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
995         if (ret != SQLITE_OK) {
996                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
997                 ret = LB_STATUS_ERROR_IO;
998                 goto out;
999         }
1000
1001         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1002         if (ret != SQLITE_OK) {
1003                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1004                 sqlite3_reset(stmt);
1005                 sqlite3_finalize(stmt);
1006                 ret = LB_STATUS_ERROR_IO;
1007                 goto out;
1008         }
1009
1010         if (*cnt > NR_OF_SIZE_LIST) {
1011                 *cnt = NR_OF_SIZE_LIST;
1012         }
1013
1014         ret = 0;
1015         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1016                 size = sqlite3_column_int(stmt, 0);
1017                 types[ret] = size;
1018                 ret++;
1019         }
1020
1021         *cnt = ret;
1022         sqlite3_reset(stmt);
1023         sqlite3_finalize(stmt);
1024         ret = LB_STATUS_SUCCESS;
1025 out:
1026         close_db(handle);
1027         return ret;
1028 }
1029
1030 static inline char *cur_locale(void)
1031 {
1032         char *language;
1033         language = vconf_get_str(VCONFKEY_LANGSET);
1034         if (language) {
1035                 char *ptr;
1036
1037                 ptr = language;
1038                 while (*ptr) {
1039                         if (*ptr == '.') {
1040                                 *ptr = '\0';
1041                                 break;
1042                         }
1043
1044                         if (*ptr == '_') {
1045                                 *ptr = '-';
1046                         }
1047
1048                         ptr++;
1049                 }
1050         } else {
1051                 language = strdup("en-us");
1052                 if (!language) {
1053                         ErrPrint("Heap: %s\n", strerror(errno));
1054                 }
1055         }
1056
1057         return language;
1058 }
1059
1060 static inline char *get_default_name(const char *pkgid)
1061 {
1062         sqlite3_stmt *stmt;
1063         sqlite3 *handle;
1064         char *name = NULL;
1065         int ret;
1066
1067         handle = open_db();
1068         if (!handle) {
1069                 return NULL;
1070         }
1071
1072         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1073         if (ret != SQLITE_OK) {
1074                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1075                 close_db(handle);
1076                 return NULL;
1077         }
1078
1079         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1080         if (ret != SQLITE_OK) {
1081                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1082                 goto out;
1083         }
1084
1085         ret = sqlite3_step(stmt);
1086         if (ret ==  SQLITE_ROW) {
1087                 const char *tmp;
1088
1089                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1090                 if (tmp && strlen(tmp)) {
1091                         name = strdup(tmp);
1092                         if (!name) {
1093                                 ErrPrint("Heap: %s\n", strerror(errno));
1094                         }
1095                 }
1096         }
1097
1098 out:
1099         sqlite3_reset(stmt);
1100         sqlite3_finalize(stmt);
1101         close_db(handle);
1102         return name;
1103 }
1104
1105 static inline char *get_default_icon(const char *pkgid)
1106 {
1107         sqlite3_stmt *stmt;
1108         sqlite3 *handle;
1109         char *icon = NULL;
1110         int ret;
1111
1112         handle = open_db();
1113         if (!handle) {
1114                 return NULL;
1115         }
1116
1117         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1118         if (ret != SQLITE_OK) {
1119                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1120                 close_db(handle);
1121                 return NULL;
1122         }
1123
1124         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1125         if (ret != SQLITE_OK) {
1126                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1127                 goto out;
1128         }
1129
1130         ret = sqlite3_step(stmt);
1131         if (ret == SQLITE_ROW) {
1132                 const char *tmp;
1133
1134                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1135                 if (tmp && strlen(tmp)) {
1136                         icon = strdup(tmp);
1137                         if (!icon) {
1138                                 ErrPrint("Heap: %s\n", strerror(errno));
1139                         }
1140                 }
1141         }
1142
1143 out:
1144         sqlite3_reset(stmt);
1145         sqlite3_finalize(stmt);
1146         close_db(handle);
1147         return icon;
1148 }
1149
1150 EAPI char *livebox_service_content(const char *pkgid)
1151 {
1152         sqlite3_stmt *stmt;
1153         sqlite3 *handle;
1154         char *content = NULL;
1155         int ret;
1156
1157         handle = open_db();
1158         if (!handle) {
1159                 return NULL;
1160         }
1161
1162         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1163         if (ret != SQLITE_OK) {
1164                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1165                 close_db(handle);
1166                 return NULL;
1167         }
1168
1169         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1170         if (ret != SQLITE_OK) {
1171                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1172                 goto out;
1173         }
1174
1175         ret = sqlite3_step(stmt);
1176         if (ret == SQLITE_ROW) {
1177                 const char *tmp;
1178
1179                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1180                 if (tmp && strlen(tmp)) {
1181                         content = strdup(tmp);
1182                         if (!content) {
1183                                 ErrPrint("Heap: %s\n", strerror(errno));
1184                         }
1185                 }
1186         }
1187
1188 out:
1189         sqlite3_reset(stmt);
1190         sqlite3_finalize(stmt);
1191         close_db(handle);
1192         return content;
1193 }
1194
1195 EAPI char *livebox_service_setup_appid(const char *lbid)
1196 {
1197         sqlite3_stmt *stmt;
1198         sqlite3 *handle;
1199         int ret;
1200         char *appid;
1201
1202         handle = open_db();
1203         if (!handle) {
1204                 return NULL;
1205         }
1206
1207         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1208         if (ret != SQLITE_OK) {
1209                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1210                 close_db(handle);
1211                 return NULL;
1212         }
1213
1214         appid = NULL;
1215         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1216         if (ret != SQLITE_OK) {
1217                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1218                 goto out;
1219         }
1220
1221         ret = sqlite3_step(stmt);
1222         if (ret == SQLITE_ROW) {
1223                 const char *tmp;
1224
1225                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1226                 if (!tmp || !strlen(tmp)) {
1227                         goto out;
1228                 }
1229
1230                 appid = strdup(tmp);
1231                 if (!appid) {
1232                         ErrPrint("Error: %s\n", strerror(errno));
1233                 }
1234         }
1235
1236 out:
1237         sqlite3_reset(stmt);
1238         sqlite3_finalize(stmt);
1239         close_db(handle);
1240         return appid;
1241 }
1242
1243 EAPI int livebox_service_nodisplay(const char *pkgid)
1244 {
1245         sqlite3_stmt *stmt;
1246         sqlite3 *handle;
1247         int ret;
1248
1249         handle = open_db();
1250         if (!handle) {
1251                 return 0;
1252         }
1253
1254         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1255         if (ret != SQLITE_OK) {
1256                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1257                 close_db(handle);
1258                 return 0;
1259         }
1260
1261         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1262         if (ret != SQLITE_OK) {
1263                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1264                 ret = 0;
1265                 goto out;
1266         }
1267
1268         ret = sqlite3_step(stmt);
1269         if (ret == SQLITE_ROW) {
1270                 ret = !!sqlite3_column_int(stmt, 0);
1271         } else {
1272                 ret = 0;
1273         }
1274
1275 out:
1276         sqlite3_reset(stmt);
1277         sqlite3_finalize(stmt);
1278         close_db(handle);
1279         return ret;
1280 }
1281
1282 static inline char *get_lb_pkgname_by_appid(const char *appid)
1283 {
1284         sqlite3_stmt *stmt;
1285         char *pkgid;
1286         char *tmp;
1287         sqlite3 *handle;
1288         int ret;
1289
1290         if (!appid) {
1291                 return NULL;
1292         }
1293
1294         pkgid = NULL;
1295         handle = open_db();
1296         if (!handle) {
1297                 return NULL;
1298         }
1299
1300         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
1301         if (ret != SQLITE_OK) {
1302                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1303                 close_db(handle);
1304                 return NULL;
1305         }
1306
1307         ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
1308         if (ret != SQLITE_OK) {
1309                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1310                 goto out;
1311         }
1312
1313         ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
1314         if (ret != SQLITE_OK) {
1315                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1316                 goto out;
1317         }
1318
1319         if (sqlite3_step(stmt) != SQLITE_ROW) {
1320                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
1321                 goto out;
1322         }
1323
1324         tmp = (char *)sqlite3_column_text(stmt, 0);
1325         if (tmp && strlen(tmp)) {
1326                 pkgid = strdup(tmp);
1327                 if (!pkgid) {
1328                         ErrPrint("Heap: %s\n", strerror(errno));
1329                 }
1330         }
1331
1332 out:
1333         sqlite3_reset(stmt);
1334         sqlite3_finalize(stmt);
1335         close_db(handle);
1336         return pkgid;
1337 }
1338
1339 EAPI int livebox_service_need_frame(const char *pkgid, int size_type)
1340 {
1341         char *lbid;
1342         sqlite3_stmt *stmt;
1343         sqlite3 *handle;
1344         int ret;
1345
1346         handle = open_db();
1347         if (!handle) {
1348                 ErrPrint("Unable to open a DB\n");
1349                 return 0;
1350         }
1351
1352         ret = sqlite3_prepare_v2(handle, "SELECT need_frame FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1353         if (ret != SQLITE_OK) {
1354                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1355                 close_db(handle);
1356                 return 0;
1357         }
1358
1359         /*!
1360          */
1361         lbid = livebox_service_pkgname(pkgid);
1362         if (!lbid) {
1363                 ErrPrint("Invalid appid (%s)\n", pkgid);
1364                 ret = 0;
1365                 goto out;
1366         }
1367
1368         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1369         free(lbid);
1370         if (ret != SQLITE_OK) {
1371                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1372                 ret = 0;
1373                 goto out;
1374         }
1375
1376         ret = sqlite3_bind_int(stmt, 2, size_type);
1377         if (ret != SQLITE_OK) {
1378                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1379                 ret = 0;
1380                 goto out;
1381         }
1382
1383         ret = sqlite3_step(stmt);
1384         if (ret == SQLITE_ROW) {
1385                 ret = !!sqlite3_column_int(stmt, 0);
1386         } else {
1387                 ret = 0;
1388                 ErrPrint("There is no such result\n");
1389         }
1390 out:
1391         sqlite3_reset(stmt);
1392         sqlite3_finalize(stmt);
1393         close_db(handle);
1394         return ret;
1395 }
1396
1397 EAPI int livebox_service_touch_effect(const char *pkgid, int size_type)
1398 {
1399         char *lbid;
1400         sqlite3_stmt *stmt;
1401         sqlite3 *handle;
1402         int ret;
1403
1404         handle = open_db();
1405         if (!handle) {
1406                 ErrPrint("Unable to open a DB\n");
1407                 return 1;
1408         }
1409
1410         ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1411         if (ret != SQLITE_OK) {
1412                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1413                 close_db(handle);
1414                 return 1;
1415         }
1416
1417         /*!
1418          * \note
1419          * This function will validate the "pkgid"
1420          * call the exported API in the exported API is not recomended
1421          * but... I used.
1422          */
1423         lbid = livebox_service_pkgname(pkgid);
1424         if (!lbid) {
1425                 ErrPrint("Invalid appid (%s)\n", pkgid);
1426                 ret = 1;
1427                 goto out;
1428         }
1429
1430         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1431         free(lbid);
1432         if (ret != SQLITE_OK) {
1433                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1434                 ret = 1;
1435                 goto out;
1436         }
1437
1438         ret = sqlite3_bind_int(stmt, 2, size_type);
1439         if (ret != SQLITE_OK) {
1440                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1441                 ret = 1;
1442                 goto out;
1443         }
1444
1445         ret = sqlite3_step(stmt);
1446         if (ret == SQLITE_ROW) {
1447                 ret = !!sqlite3_column_int(stmt, 0);
1448         } else {
1449                 ret = 1; /*!< Default true: In this case the DB is corrupted. */
1450                 ErrPrint("There is no result\n");
1451         }
1452
1453 out:
1454         sqlite3_reset(stmt);
1455         sqlite3_finalize(stmt);
1456         close_db(handle);
1457         return ret;
1458 }
1459
1460 EAPI int livebox_service_mouse_event(const char *pkgid)
1461 {
1462         sqlite3_stmt *stmt;
1463         sqlite3 *handle;
1464         char *lbid;
1465         int ret;
1466
1467         handle = open_db();
1468         if (!handle) {
1469                 return 0;
1470         }
1471
1472         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1473         if (ret != SQLITE_OK) {
1474                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1475                 close_db(handle);
1476                 return 0;
1477         }
1478
1479         lbid = livebox_service_pkgname(pkgid);
1480         if (!lbid) {
1481                 ErrPrint("Failed to get lbid: %s\n", pkgid);
1482                 ret = 0;
1483                 goto out;
1484         }
1485
1486         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1487         free(lbid);
1488         if (ret != SQLITE_OK) {
1489                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1490                 ret = 0;
1491                 goto out;
1492         }
1493
1494         ret = sqlite3_step(stmt);
1495         if (ret == SQLITE_ROW) {
1496                 ret = !!sqlite3_column_int(stmt, 0);
1497         } else {
1498                 ret = 0; /*!< Default is false, In this case the DB is corrupted */
1499                 ErrPrint("There is no result.\n");
1500         }
1501
1502 out:
1503         sqlite3_reset(stmt);
1504         sqlite3_finalize(stmt);
1505         close_db(handle);
1506         return ret;
1507 }
1508
1509 static inline int update_lang_info(void)
1510 {
1511         char *syslang;
1512         UErrorCode err;
1513
1514         syslang = vconf_get_str(VCONFKEY_LANGSET);
1515         if (!syslang) {
1516                 ErrPrint("Failed to get vconf-lang\n");
1517                 return -EFAULT;
1518         }
1519
1520         if (s_info.syslang && !strcmp(s_info.syslang, syslang)) {
1521                 DbgPrint("Syslang is not changed: %s\n", syslang);
1522                 free(syslang);
1523                 return 0;
1524         }
1525
1526         free(s_info.syslang);
1527         s_info.syslang = syslang;
1528
1529         err = U_ZERO_ERROR;
1530         uloc_setDefault((const char *)s_info.syslang, &err);
1531         if (!U_SUCCESS(err)) {
1532                 ErrPrint("Failed to set default lang: %s\n", u_errorName(err));
1533                 free(s_info.syslang);
1534                 s_info.syslang = NULL;
1535                 return -EFAULT;
1536         }
1537
1538         s_info.iso3lang = uloc_getISO3Language(uloc_getDefault());
1539         if (!s_info.iso3lang || !strlen(s_info.iso3lang)) {
1540                 ErrPrint("Failed to get iso3lang\n");
1541                 free(s_info.syslang);
1542                 s_info.syslang = NULL;
1543                 return -EFAULT;
1544         }
1545
1546         err = U_ZERO_ERROR;
1547         s_info.country_len = uloc_getCountry(uloc_getDefault(), s_info.country, ULOC_COUNTRY_CAPACITY, &err);
1548         if (!U_SUCCESS(err) || s_info.country_len <= 0) {
1549                 ErrPrint("Failed to get locale: %s, %s, %d (%s)\n", u_errorName(err), s_info.iso3lang, s_info.country_len, s_info.country);
1550                 free(s_info.syslang);
1551                 s_info.syslang = NULL;
1552                 return -EFAULT;
1553         }
1554
1555         return 0;
1556 }
1557
1558 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
1559 {
1560         sqlite3_stmt *stmt;
1561         sqlite3 *handle;
1562         int ret;
1563         char *preview = NULL;
1564         const char *tmp;
1565         int tmp_len;
1566         int buf_len;
1567         register int i;
1568         int printed;
1569
1570         handle = open_db();
1571         if (!handle) {
1572                 return NULL;
1573         }
1574
1575         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1576         if (ret != SQLITE_OK) {
1577                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1578                 close_db(handle);
1579                 return NULL;
1580         }
1581
1582         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1583         if (ret != SQLITE_OK) {
1584                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1585                 goto out;
1586         }
1587
1588         ret = sqlite3_bind_int(stmt, 2, size_type);
1589         if (ret != SQLITE_OK) {
1590                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1591                 goto out;
1592         }
1593
1594         ret = sqlite3_step(stmt);
1595         if (ret != SQLITE_ROW) {
1596                 ErrPrint("Error: %s, %s\n", sqlite3_errmsg(handle), pkgid);
1597                 goto out;
1598         }
1599
1600         tmp = (const char *)sqlite3_column_text(stmt, 0);
1601         if (!tmp || !(tmp_len = strlen(tmp))) {
1602                 ErrPrint("Failed to get data (%s)\n", pkgid);
1603                 goto out;
1604         }
1605
1606         if (update_lang_info() != 0) {
1607                 preview = strdup(tmp);
1608                 if (!preview) {
1609                         ErrPrint("Heap: %s\n", strerror(errno));
1610                 }
1611                 goto out;
1612         }
1613
1614         buf_len = tmp_len + strlen(s_info.iso3lang) + s_info.country_len + 3; /* '/' '-' '/' */
1615         preview = malloc(buf_len + 1);
1616         if (!preview) {
1617                 ErrPrint("Heap: %s\n", strerror(errno));
1618                 goto out;
1619         }
1620
1621         for (i = tmp_len; i >= 0 && tmp[i] != '/'; i--);
1622         i++; /* Skip '/' */
1623
1624         strncpy(preview, tmp, i);
1625         printed = snprintf(preview + i, buf_len - i, "%s-%s/%s", s_info.iso3lang, s_info.country, tmp + i);
1626         if (preview[i + printed] != '\0') {
1627                 ErrPrint("Path is truncated\n");
1628                 preview[i + printed] = '\0';
1629         }
1630
1631         if (access(preview, R_OK) != 0) {
1632                 DbgPrint("Access failed: %s, %s\n", preview, strerror(errno));
1633                 free(preview);
1634
1635                 preview = strdup(tmp);
1636                 if (!preview) {
1637                         ErrPrint("Heap: %s\n", strerror(errno));
1638                 }
1639         }
1640
1641 out:
1642         sqlite3_reset(stmt);
1643         sqlite3_finalize(stmt);
1644         close_db(handle);
1645         return preview;
1646 }
1647
1648 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1649 {
1650         sqlite3_stmt *stmt;
1651         sqlite3 *handle;
1652         char *language;
1653         char *icon = NULL;
1654         int ret;
1655
1656         if (lang) {
1657                 language = strdup(lang);
1658                 if (!language) {
1659                         ErrPrint("Heap: %s\n", strerror(errno));
1660                         return NULL;
1661                 }
1662         } else {
1663                 language = cur_locale();
1664                 if (!language) {
1665                         return NULL;
1666                 }
1667         }
1668
1669         handle = open_db();
1670         if (!handle) {
1671                 free(language);
1672                 return NULL;
1673         }
1674
1675         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1676         if (ret != SQLITE_OK) {
1677                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1678                 close_db(handle);
1679                 free(language);
1680                 return NULL;
1681         }
1682
1683         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1684         if (ret != SQLITE_OK) {
1685                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1686                 goto out;
1687         }
1688
1689         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1690         if (ret != SQLITE_OK) {
1691                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1692                 goto out;
1693         }
1694
1695         ret = sqlite3_step(stmt);
1696         if (ret == SQLITE_ROW) {
1697                 const char *tmp;
1698                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1699                 if (!tmp || !strlen(tmp)) {
1700                         icon = get_default_icon(pkgid);
1701                 } else {
1702                         icon = strdup(tmp);
1703                         if (!icon) {
1704                                 ErrPrint("Heap: %s\n", strerror(errno));
1705                         }
1706                 }
1707         } else {
1708                 icon = get_default_icon(pkgid);
1709         }
1710
1711 out:
1712         sqlite3_reset(stmt);
1713         sqlite3_finalize(stmt);
1714         close_db(handle);
1715         free(language);
1716         return icon;
1717 }
1718
1719 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1720 {
1721         sqlite3_stmt *stmt;
1722         sqlite3 *handle;
1723         char *language;
1724         char *name = NULL;
1725         int ret;
1726
1727         if (lang) {
1728                 language = strdup(lang);
1729                 if (!language) {
1730                         ErrPrint("Error: %s\n", strerror(errno));
1731                         return NULL;
1732                 }
1733         } else {
1734                 language = cur_locale();
1735                 if (!language) {
1736                         return NULL;
1737                 }
1738         }
1739
1740         handle = open_db();
1741         if (!handle) {
1742                 free(language);
1743                 return NULL;
1744         }
1745
1746         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1747         if (ret != SQLITE_OK) {
1748                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1749                 close_db(handle);
1750                 free(language);
1751                 return NULL;
1752         }
1753
1754         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1755         if (ret != SQLITE_OK) {
1756                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1757                 goto out;
1758         }
1759
1760         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1761         if (ret != SQLITE_OK) {
1762                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1763                 goto out;
1764         }
1765
1766         ret = sqlite3_step(stmt);
1767         if (ret == SQLITE_ROW) {
1768                 const char *tmp;
1769                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1770                 if (!tmp || !strlen(tmp)) {
1771                         name = get_default_name(pkgid);
1772                 } else {
1773                         name = strdup(tmp);
1774                         if (!name) {
1775                                 ErrPrint("Heap: %s\n", strerror(errno));
1776                         }
1777                 }
1778         } else {
1779                 name = get_default_name(pkgid);
1780         }
1781
1782 out:
1783         sqlite3_reset(stmt);
1784         sqlite3_finalize(stmt);
1785         close_db(handle);
1786         free(language);
1787         return name;
1788 }
1789
1790 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1791 {
1792         sqlite3_stmt *stmt;
1793         sqlite3 *handle;
1794         int size;
1795         int ret;
1796
1797         if (!w || !h || !cnt || !pkgid) {
1798                 return LB_STATUS_ERROR_INVALID;
1799         }
1800
1801         handle = open_db();
1802         if (!handle) {
1803                 return LB_STATUS_ERROR_IO;
1804         }
1805
1806         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1807         if (ret != SQLITE_OK) {
1808                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1809                 ret = LB_STATUS_ERROR_IO;
1810                 goto out;
1811         }
1812
1813         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1814         if (ret != SQLITE_OK) {
1815                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1816                 sqlite3_reset(stmt);
1817                 sqlite3_finalize(stmt);
1818                 ret = LB_STATUS_ERROR_IO;
1819                 goto out;
1820         }
1821
1822         if (*cnt > NR_OF_SIZE_LIST) {
1823                 *cnt = NR_OF_SIZE_LIST;
1824         }
1825
1826         ret = 0;
1827         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1828                 size = sqlite3_column_int(stmt, 0);
1829                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1830         }
1831
1832         *cnt = ret;
1833         sqlite3_reset(stmt);
1834         sqlite3_finalize(stmt);
1835         ret = 0;
1836 out:
1837         close_db(handle);
1838         return ret;
1839 }
1840
1841 EAPI char *livebox_service_libexec(const char *pkgid)
1842 {
1843         sqlite3_stmt *stmt;
1844         sqlite3 *handle;
1845         int ret;
1846         char *libexec;
1847         char *appid;
1848         char *path;
1849
1850         if (!pkgid) {
1851                 return NULL;
1852         }
1853
1854         libexec = NULL;
1855         handle = open_db();
1856         if (!handle) {
1857                 return NULL;
1858         }
1859
1860         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1861         if (ret != SQLITE_OK) {
1862                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1863                 goto out;
1864         }
1865
1866         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1867         if (ret != SQLITE_OK) {
1868                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1869                 sqlite3_finalize(stmt);
1870                 goto out;
1871         }
1872
1873         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1874         if (ret != SQLITE_OK) {
1875                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1876                 sqlite3_finalize(stmt);
1877                 goto out;
1878         }
1879
1880         if (sqlite3_step(stmt) != SQLITE_ROW) {
1881                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1882                 sqlite3_reset(stmt);
1883                 sqlite3_finalize(stmt);
1884
1885                 libexec = util_conf_get_libexec(pkgid);
1886                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1887                 goto out;
1888         }
1889
1890         appid = (char *)sqlite3_column_text(stmt, 0);
1891         if (!appid || !strlen(appid)) {
1892                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1893                 sqlite3_reset(stmt);
1894                 sqlite3_finalize(stmt);
1895                 goto out;
1896         }
1897
1898         path = (char *)sqlite3_column_text(stmt, 1);
1899         if (!path || !strlen(path)) {
1900                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1901                 sqlite3_reset(stmt);
1902                 sqlite3_finalize(stmt);
1903                 goto out;
1904         }
1905
1906         libexec = strdup(path);
1907         if (!libexec) {
1908                 ErrPrint("Heap: %s\n", strerror(errno));
1909                 sqlite3_reset(stmt);
1910                 sqlite3_finalize(stmt);
1911                 goto out;
1912         }
1913
1914         DbgPrint("libexec: %s\n", libexec);
1915
1916         sqlite3_reset(stmt);
1917         sqlite3_finalize(stmt);
1918 out:
1919         close_db(handle);
1920         return libexec;
1921 }
1922
1923 EAPI char *livebox_service_pkgname(const char *appid)
1924 {
1925         char *lb_pkgname;
1926         pkgmgr_appinfo_h handle;
1927         int ret;
1928         char *new_appid;
1929
1930         if (!appid) {
1931                 return NULL;
1932         }
1933
1934         lb_pkgname = get_lb_pkgname_by_appid(appid);
1935         if (lb_pkgname) {
1936                 return lb_pkgname;
1937         }
1938
1939         /*!
1940          * \note
1941          * Try to get the package id using given appid
1942          */
1943         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1944         if (ret != PKGMGR_R_OK) {
1945                 ErrPrint("Failed to get appinfo\n");
1946                 return NULL;
1947         }
1948
1949         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1950         if (ret != PKGMGR_R_OK) {
1951                 pkgmgr_appinfo_destroy_appinfo(handle);
1952                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1953                 return NULL;
1954         }
1955
1956         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1957         pkgmgr_appinfo_destroy_appinfo(handle);
1958
1959         if (!lb_pkgname && util_validate_livebox_package(appid) == 0) {
1960                 return strdup(appid);
1961         }
1962
1963         return lb_pkgname;
1964 }
1965
1966 EAPI char *livebox_service_provider_name(const char *lbid)
1967 {
1968         char *ret;
1969         int stage = 0;
1970         int seq = 0;
1971         int idx = 0;
1972         char *str = SAMSUNG_PREFIX;
1973
1974         if (!lbid) {
1975                 return NULL;
1976         }
1977
1978         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1979                 idx++;
1980                 if (seq < 2 && lbid[idx] == '.') {
1981                         stage = idx;
1982                         seq++;
1983                 }
1984         }
1985
1986         if (!str[idx] && lbid[idx]) {
1987                 /* Inhouse */
1988                 return strdup(lbid);
1989         } else if (seq < 2) {
1990                 while (seq < 2) {
1991                         if (lbid[idx] == '.') {
1992                                 seq++;
1993                         } else if (!lbid[idx]) {
1994                                 ErrPrint("Invalid lbid: %s\n", lbid);
1995                                 return NULL;
1996                         }
1997
1998                         idx++;
1999                 }
2000
2001                 stage = idx;
2002         } else {
2003                 stage++;
2004         }
2005
2006         ret = strdup(lbid + stage);
2007         if (!ret) {
2008                 ErrPrint("Error: %s\n", strerror(errno));
2009                 return NULL;
2010         }
2011
2012         return ret;
2013 }
2014
2015 EAPI int livebox_service_is_enabled(const char *lbid)
2016 {
2017         return 1;
2018         /*
2019         ail_appinfo_h ai;
2020         char *pkgname;
2021         bool enabled;
2022         int ret;
2023
2024         pkgname = livebox_service_appid(lbid);
2025         if (!pkgname)
2026                 return 0;
2027
2028         ret = ail_get_appinfo(pkgname, &ai);
2029         if (ret != AIL_ERROR_OK) {
2030                 free(pkgname);
2031                 return 0;
2032         }
2033
2034         if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
2035                 enabled = false;
2036
2037         ail_destroy_appinfo(ai);
2038         free(pkgname);
2039         return enabled == true;
2040         */
2041 }
2042
2043 EAPI int livebox_service_is_primary(const char *lbid)
2044 {
2045         sqlite3_stmt *stmt;
2046         sqlite3 *handle;
2047         int ret = 0;
2048
2049         if (!lbid) {
2050                 return 0;
2051         }
2052
2053         handle = open_db();
2054         if (!handle) {
2055                 return 0;
2056         }
2057
2058         ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
2059         if (ret != SQLITE_OK) {
2060                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2061                 close_db(handle);
2062                 return 0;
2063         }
2064
2065         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
2066         if (ret != SQLITE_OK) {
2067                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2068                 goto out;
2069         }
2070
2071         ret = sqlite3_step(stmt);
2072         if (ret != SQLITE_ROW) {
2073                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2074                 goto out;
2075         }
2076
2077         ret = sqlite3_column_int(stmt, 0);
2078
2079 out:
2080         sqlite3_reset(stmt);
2081         sqlite3_finalize(stmt);
2082         close_db(handle);
2083         return ret;
2084 }
2085
2086 EAPI char *livebox_service_appid(const char *pkgname)
2087 {
2088         sqlite3_stmt *stmt;
2089         char *appid;
2090         char *tmp;
2091         sqlite3 *handle;
2092         int is_prime __attribute__((__unused__));
2093         int ret;
2094
2095         if (!pkgname) {
2096                 return NULL;
2097         }
2098
2099         appid = NULL;
2100         handle = open_db();
2101         if (!handle) {
2102                 return NULL;
2103         }
2104
2105         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
2106         if (ret != SQLITE_OK) {
2107                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2108                 goto out;
2109         }
2110
2111         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
2112         if (ret != SQLITE_OK) {
2113                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2114                 sqlite3_reset(stmt);
2115                 sqlite3_finalize(stmt);
2116                 goto out;
2117         }
2118
2119         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
2120         if (ret != SQLITE_OK) {
2121                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2122                 sqlite3_reset(stmt);
2123                 sqlite3_finalize(stmt);
2124                 goto out;
2125         }
2126
2127         ret = sqlite3_step(stmt);
2128         if (ret != SQLITE_ROW) {
2129                 pkgmgr_appinfo_h pkg_handle;
2130                 char *new_appid;
2131
2132                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
2133                 sqlite3_reset(stmt);
2134                 sqlite3_finalize(stmt);
2135
2136                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
2137                 if (ret != PKGMGR_R_OK) {
2138                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
2139                         goto out;
2140                 }
2141
2142                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
2143                 if (ret != PKGMGR_R_OK) {
2144                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
2145                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
2146                         goto out;
2147                 }
2148
2149                 appid = strdup(new_appid);
2150                 if (!appid) {
2151                         ErrPrint("Heap: %s\n", strerror(errno));
2152                 }
2153
2154                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
2155                 goto out;
2156         }
2157
2158         tmp = (char *)sqlite3_column_text(stmt, 0);
2159         if (!tmp || !strlen(tmp)) {
2160                 ErrPrint("APPID is NIL\n");
2161                 sqlite3_reset(stmt);
2162                 sqlite3_finalize(stmt);
2163                 goto out;
2164         }
2165
2166         appid = strdup(tmp);
2167         if (!appid) {
2168                 ErrPrint("Heap: %s\n", strerror(errno));
2169                 sqlite3_reset(stmt);
2170                 sqlite3_finalize(stmt);
2171                 goto out;
2172         }
2173
2174         is_prime = sqlite3_column_int(stmt, 1);
2175
2176         sqlite3_reset(stmt);
2177         sqlite3_finalize(stmt);
2178 out:
2179         close_db(handle);
2180         return appid;
2181 }
2182
2183 EAPI char *livebox_service_lb_script_path(const char *pkgid)
2184 {
2185         sqlite3_stmt *stmt;
2186         sqlite3 *handle;
2187         int ret;
2188         char *path;
2189         char *appid;
2190         char *lb_src;
2191
2192         if (!pkgid) {
2193                 return NULL;
2194         }
2195
2196         path = NULL;
2197         handle = open_db();
2198         if (!handle) {
2199                 return NULL;
2200         }
2201
2202         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
2203         if (ret != SQLITE_OK) {
2204                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2205                 goto out;
2206         }
2207
2208         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2209         if (ret != SQLITE_OK) {
2210                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2211                 sqlite3_finalize(stmt);
2212                 goto out;
2213         }
2214
2215         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
2216         if (ret != SQLITE_OK) {
2217                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2218                 sqlite3_finalize(stmt);
2219                 goto out;
2220         }
2221
2222         ret = sqlite3_step(stmt);
2223         if (ret != SQLITE_ROW) {
2224                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2225                 sqlite3_reset(stmt);
2226                 sqlite3_finalize(stmt);
2227                 goto out;
2228         }
2229
2230         appid = (char *)sqlite3_column_text(stmt, 0);
2231         if (!appid || !strlen(appid)) {
2232                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
2233                 sqlite3_reset(stmt);
2234                 sqlite3_finalize(stmt);
2235                 goto out;
2236         }
2237
2238         lb_src = (char *)sqlite3_column_text(stmt, 1);
2239         if (!lb_src || !strlen(lb_src)) {
2240                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
2241                 sqlite3_reset(stmt);
2242                 sqlite3_finalize(stmt);
2243                 goto out;
2244         }
2245
2246         path = strdup(lb_src);
2247         if (!path) {
2248                 ErrPrint("Heap: %s\n", strerror(errno));
2249                 sqlite3_reset(stmt);
2250                 sqlite3_finalize(stmt);
2251                 goto out;
2252         }
2253
2254         DbgPrint("LB Src: %s\n", path);
2255         sqlite3_reset(stmt);
2256         sqlite3_finalize(stmt);
2257 out:
2258         close_db(handle);
2259         return path;
2260 }
2261
2262 EAPI char *livebox_service_lb_script_group(const char *pkgid)
2263 {
2264         sqlite3_stmt *stmt;
2265         sqlite3 *handle;
2266         int ret;
2267         char *group;
2268         char *tmp;
2269
2270         if (!pkgid) {
2271                 return NULL;
2272         }
2273
2274         group = NULL;
2275         handle = open_db();
2276         if (!handle) {
2277                 return NULL;
2278         }
2279
2280         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2281         if (ret != SQLITE_OK) {
2282                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2283                 goto out;
2284         }
2285
2286         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2287         if (ret != SQLITE_OK) {
2288                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2289                 sqlite3_finalize(stmt);
2290                 goto out;
2291         }
2292
2293         ret = sqlite3_step(stmt);
2294         if (ret != SQLITE_ROW) {
2295                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2296                 sqlite3_reset(stmt);
2297                 sqlite3_finalize(stmt);
2298                 goto out;
2299         }
2300
2301         tmp = (char *)sqlite3_column_text(stmt, 0);
2302         if (tmp && strlen(tmp)) {
2303                 group = strdup(tmp);
2304                 if (!group) {
2305                         ErrPrint("Heap: %s\n", strerror(errno));
2306                 }
2307         }
2308
2309         sqlite3_reset(stmt);
2310         sqlite3_finalize(stmt);
2311 out:
2312         close_db(handle);
2313         return group;
2314 }
2315
2316 EAPI char *livebox_service_pd_script_path(const char *pkgid)
2317 {
2318         sqlite3_stmt *stmt;
2319         sqlite3 *handle;
2320         int ret;
2321         char *path;
2322         char *pd_src;
2323         const char *appid;
2324
2325         if (!pkgid) {
2326                 return NULL;
2327         }
2328
2329         path = NULL;
2330         handle = open_db();
2331         if (!handle) {
2332                 return NULL;
2333         }
2334
2335         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
2336         if (ret != SQLITE_OK) {
2337                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2338                 goto out;
2339         }
2340
2341         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2342         if (ret != SQLITE_OK) {
2343                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2344                 sqlite3_finalize(stmt);
2345                 goto out;
2346         }
2347
2348         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
2349         if (ret != SQLITE_OK) {
2350                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2351                 sqlite3_finalize(stmt);
2352                 goto out;
2353         }
2354
2355         ret = sqlite3_step(stmt);
2356         if (ret != SQLITE_ROW) {
2357                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2358                 sqlite3_reset(stmt);
2359                 sqlite3_finalize(stmt);
2360                 goto out;
2361         }
2362
2363         appid = (char *)sqlite3_column_text(stmt, 0);
2364         if (!appid || !strlen(appid)) {
2365                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2366                 sqlite3_reset(stmt);
2367                 sqlite3_finalize(stmt);
2368                 goto out;
2369         }
2370
2371         pd_src = (char *)sqlite3_column_text(stmt, 1);
2372         if (!pd_src || !strlen(pd_src)) {
2373                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2374                 sqlite3_reset(stmt);
2375                 sqlite3_finalize(stmt);
2376                 goto out;
2377         }
2378
2379         path = strdup(pd_src);
2380         if (!path) {
2381                 ErrPrint("Heap: %s\n", strerror(errno));
2382                 sqlite3_reset(stmt);
2383                 sqlite3_finalize(stmt);
2384                 goto out;
2385         }
2386
2387         DbgPrint("PD Src: %s\n", path);
2388         sqlite3_reset(stmt);
2389         sqlite3_finalize(stmt);
2390 out:
2391         close_db(handle);
2392         return path;
2393 }
2394
2395 EAPI char *livebox_service_pd_script_group(const char *pkgid)
2396 {
2397         sqlite3_stmt *stmt;
2398         sqlite3 *handle;
2399         int ret;
2400         char *group;
2401         char *tmp;
2402
2403         if (!pkgid) {
2404                 return NULL;
2405         }
2406
2407         group = NULL;
2408         handle = open_db();
2409         if (!handle) {
2410                 return NULL;
2411         }
2412
2413         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
2414         if (ret != SQLITE_OK) {
2415                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2416                 goto out;
2417         }
2418
2419         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
2420         if (ret != SQLITE_OK) {
2421                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2422                 sqlite3_finalize(stmt);
2423                 goto out;
2424         }
2425
2426         ret = sqlite3_step(stmt);
2427         if (ret != SQLITE_ROW) {
2428                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2429                 sqlite3_reset(stmt);
2430                 sqlite3_finalize(stmt);
2431                 goto out;
2432         }
2433
2434         tmp = (char *)sqlite3_column_text(stmt, 0);
2435         if (tmp && strlen(tmp)) {
2436                 group = strdup(tmp);
2437                 if (!group) {
2438                         ErrPrint("Heap: %s\n", strerror(errno));
2439                 }
2440         }
2441         sqlite3_reset(stmt);
2442         sqlite3_finalize(stmt);
2443 out:
2444         close_db(handle);
2445         return group;
2446 }
2447
2448 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
2449 {
2450         sqlite3_stmt *stmt;
2451         sqlite3 *handle;
2452         const char *cluster;
2453         int cnt;
2454         int ret;
2455
2456         if (!cb) {
2457                 return LB_STATUS_ERROR_INVALID;
2458         }
2459
2460         handle = open_db();
2461         if (!handle) {
2462                 return LB_STATUS_ERROR_IO;
2463         }
2464
2465         cnt = 0;
2466         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
2467         if (ret != SQLITE_OK) {
2468                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2469                 cnt = LB_STATUS_ERROR_IO;
2470                 goto out;
2471         }
2472
2473         while (sqlite3_step(stmt) == SQLITE_ROW) {
2474                 cluster = (const char *)sqlite3_column_text(stmt, 0);
2475                 if (!cluster || !strlen(cluster)) {
2476                         continue;
2477                 }
2478
2479                 if (cb(cluster, data) < 0) {
2480                         break;
2481                 }
2482
2483                 cnt++;
2484         }
2485
2486         sqlite3_reset(stmt);
2487         sqlite3_finalize(stmt);
2488 out:
2489         close_db(handle);
2490         return cnt;
2491 }
2492
2493 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
2494 {
2495         sqlite3_stmt *stmt;
2496         sqlite3 *handle;
2497         const char *category;
2498         int cnt;
2499         int ret;
2500
2501         if (!cluster || !cb) {
2502                 return LB_STATUS_ERROR_INVALID;
2503         }
2504
2505         handle = open_db();
2506         if (!handle) {
2507                 return LB_STATUS_ERROR_IO;
2508         }
2509
2510         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
2511         if (ret != SQLITE_OK) {
2512                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
2513                 cnt = LB_STATUS_ERROR_IO;
2514                 goto out;
2515         }
2516
2517         cnt = 0;
2518         while (sqlite3_step(stmt) == SQLITE_ROW) {
2519                 category = (const char *)sqlite3_column_text(stmt, 0);
2520                 if (!category || !strlen(category)) {
2521                         continue;
2522                 }
2523
2524                 if (cb(cluster, category, data) < 0) {
2525                         break;
2526                 }
2527
2528                 cnt++;
2529         }
2530
2531         sqlite3_reset(stmt);
2532         sqlite3_finalize(stmt);
2533 out:
2534         close_db(handle);
2535         return cnt;
2536 }
2537
2538 EAPI int livebox_service_init(void)
2539 {
2540         if (s_info.handle) {
2541                 DbgPrint("Already initialized\n");
2542                 s_info.init_count++;
2543                 return 0;
2544         }
2545
2546         s_info.handle = open_db();
2547         if (s_info.handle) {
2548                 s_info.init_count++;
2549                 return 0;
2550         }
2551
2552         return LB_STATUS_ERROR_IO;
2553 }
2554
2555 EAPI int livebox_service_fini(void)
2556 {
2557         if (!s_info.handle || s_info.init_count <= 0) {
2558                 ErrPrint("Service is not initialized\n");
2559                 return LB_STATUS_ERROR_IO;
2560         }
2561
2562         s_info.init_count--;
2563         if (s_info.init_count > 0) {
2564                 DbgPrint("Init count %d\n", s_info.init_count);
2565                 return 0;
2566         }
2567
2568         db_util_close(s_info.handle);
2569         s_info.handle = NULL;
2570         return 0;
2571 }
2572
2573 EAPI int livebox_service_get_size(int type, int *width, int *height)
2574 {
2575         int _width;
2576         int _height;
2577
2578         if (!width) {
2579                 width = &_width;
2580         }
2581
2582         if (!height) {
2583                 height = &_height;
2584         }
2585
2586         return convert_size_from_type(type, width, height);
2587 }
2588
2589 EAPI int livebox_service_size_type(int width, int height)
2590 {
2591         int idx;
2592
2593         if (update_resolution() < 0) {
2594                 ErrPrint("Failed to update the size list\n");
2595         }
2596
2597         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
2598                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height) {
2599                         break;
2600                 }
2601         }
2602
2603         switch (idx) {
2604         case 0:
2605                 return LB_SIZE_TYPE_1x1;
2606         case 1:
2607                 return LB_SIZE_TYPE_2x1;
2608         case 2:
2609                 return LB_SIZE_TYPE_2x2;
2610         case 3:
2611                 return LB_SIZE_TYPE_4x1;
2612         case 4:
2613                 return LB_SIZE_TYPE_4x2;
2614         case 5:
2615                 return LB_SIZE_TYPE_4x3;
2616         case 6:
2617                 return LB_SIZE_TYPE_4x4;
2618         case 7:
2619                 return LB_SIZE_TYPE_4x5;
2620         case 8:
2621                 return LB_SIZE_TYPE_4x6;
2622         case 9:
2623                 return LB_SIZE_TYPE_EASY_1x1;
2624         case 10:
2625                 return LB_SIZE_TYPE_EASY_3x1;
2626         case 11:
2627                 return LB_SIZE_TYPE_EASY_3x3;
2628         case 12:
2629                 return LB_SIZE_TYPE_0x0;
2630         default:
2631                 break;
2632         }
2633
2634         return LB_SIZE_TYPE_UNKNOWN;
2635 }
2636
2637 /* End of a file */