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