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