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