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