Fix the bug of trigger_update & set_period API.
[platform/framework/web/livebox-service.git] / src / livebox-service.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 <vconf.h>
35 #include <vconf-keys.h>
36 #include <ail.h>
37
38 #include "dlist.h"
39 #include "util.h"
40 #include "debug.h"
41 #include "livebox-service.h"
42
43 #define SAMSUNG_PREFIX  "com.samsung."
44 #define EAPI __attribute__((visibility("default")))
45 #define DEFAULT_TIMEOUT 2.0
46 #define MAX_COLUMN 80
47
48 static struct supported_size_list {
49         int w;
50         int h;
51 } SIZE_LIST[NR_OF_SIZE_LIST] = {
52         { 172, 172 }, /*!< 1x1 */
53         { 348, 172 }, /*!< 2x1 */
54         { 348, 348 }, /*!< 2x2 */
55         { 700, 172 }, /*!< 4x1 */
56         { 700, 348 }, /*!< 4x2 */
57         { 700, 520 }, /*!< 4x3 */
58         { 700, 700 }, /*!< 4x4 */
59         { 207, 207 }, /*!< 21x21 */
60         { 645, 207 }, /*!< 23x21 */
61         { 645, 645 }, /*!< 23x23 */
62 };
63
64 static struct info {
65         sqlite3 *handle;
66         const char *dbfile;
67         const char *conf_file;
68         int init_count;
69         int res_resolved;
70 } s_info = {
71         .handle = NULL,
72         .dbfile = "/opt/dbspace/.livebox.db", 
73         .conf_file = "/usr/share/data-provider-master/resolution.ini",
74         .init_count = 0,
75         .res_resolved = 0,
76 };
77
78 static inline int update_info(int width_type, int height_type, int width, int height)
79 {
80         int idx;
81
82         if (width_type == 1 && height_type == 1) {
83                 DbgPrint("1x1 Updated to %dx%d\n", width, height);
84                 idx = 0;
85         } else if (width_type == 2 && height_type == 1) {
86                 DbgPrint("2x1 Updated to %dx%d\n", width, height);
87                 idx = 1;
88         } else if (width_type == 2 && height_type == 2) {
89                 DbgPrint("2x2 Updated to %dx%d\n", width, height);
90                 idx = 2;
91         } else if (width_type == 4 && height_type == 1) {
92                 DbgPrint("4x1 Updated to %dx%d\n", width, height);
93                 idx = 3;
94         } else if (width_type == 4 && height_type == 2) {
95                 DbgPrint("4x2 Updated to %dx%d\n", width, height);
96                 idx = 4;
97         } else if (width_type == 4 && height_type == 3) {
98                 DbgPrint("4x3 Updated to %dx%d\n", width, height);
99                 idx = 5;
100         } else if (width_type == 4 && height_type == 4) {
101                 DbgPrint("4x4 Updated to %dx%d\n", width, height);
102                 idx = 6;
103         } else if (width_type == 21 && height_type == 21) {
104                 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
105                 idx = 7;
106         } else if (width_type == 23 && height_type == 21) {
107                 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
108                 idx = 8;
109         } else if (width_type == 23 && height_type == 23) {
110                 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
111                 idx = 9;
112         } else {
113                 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
114                 return 0;
115         }
116
117         SIZE_LIST[idx].w = width;
118         SIZE_LIST[idx].h = height;
119         return 1;
120 }
121
122 static inline int update_from_file(void)
123 {
124         FILE *fp;
125         int updated;
126         int width_type;
127         int height_type;
128         int width;
129         int height;
130         char buffer[MAX_COLUMN];
131         int ch;
132         int idx;
133         enum status {
134                 START = 0x0,
135                 TYPE = 0x01,
136                 SIZE = 0x02,
137                 COMMENT = 0x03,
138                 ERROR = 0x04,
139                 EOL = 0x05,
140                 TYPE_END = 0x06,
141                 SIZE_START = 0x07,
142         } status;
143
144         fp = fopen(s_info.conf_file, "r");
145         if (!fp) {
146                 ErrPrint("Open failed: %s\n", strerror(errno));
147                 return -EIO;
148         }
149
150         updated = 0;
151         status = START;
152         idx = 0;
153         do {
154                 ch = fgetc(fp);
155
156                 if (idx == MAX_COLUMN) {
157                         ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
158                         status = ERROR;
159                 }
160
161                 switch (status) {
162                 case START:
163                         if (isspace(ch) || ch == EOF)
164                                 continue;
165
166                         if (ch == '#') {
167                                 status = COMMENT;
168                         } else {
169                                 status = TYPE;
170                                 idx = 0;
171                                 ungetc(ch, fp);
172                         }
173                         break;
174                 case TYPE:
175                         if (isblank(ch)) {
176                                 buffer[idx] = '\0';
177                                 status = TYPE_END;
178                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
179                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
180                                         status = ERROR;
181                                 }
182                                 break;
183                         } else if (ch == '=') {
184                                 buffer[idx] = '\0';
185                                 status = SIZE_START;
186                                 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
187                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
188                                         status = ERROR;
189                                 }
190                                 break;
191                         } else if (ch == EOF) {
192                                 ErrPrint("Invalid Syntax\n");
193                                 status = ERROR;
194                                 continue;
195                         }
196                         buffer[idx++] = ch;
197                         break;
198                 case TYPE_END:
199                         if (ch == '=')
200                                 status = SIZE_START;
201                         break;
202                 case SIZE_START:
203                         if (isspace(ch) || ch == EOF)
204                                 continue;
205
206                         status = SIZE;
207                         idx = 0;
208                         ungetc(ch, fp);
209                         break;
210                 case SIZE:
211                         if (isspace(ch) || ch == EOF) {
212                                 buffer[idx] = '\0';
213                                 status = EOL;
214
215                                 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
216                                         ErrPrint("Invalid syntax: [%s]\n", buffer);
217                                         status = ERROR;
218                                 } else if (ch == EOF) {
219                                         updated += update_info(width_type, height_type, width, height);
220                                 }
221                                 break;
222                         }
223                         buffer[idx++] = ch;
224                         break;
225                 case EOL:
226                         updated += update_info(width_type, height_type, width, height);
227                         status = START;
228                         ungetc(ch, fp);
229                         break;
230                 case ERROR:
231                         if (ch == '\n' || ch == '\r' || ch == '\f')
232                                 status = START;
233                         break;
234                 case COMMENT:
235                         if (ch == '\n' || ch == '\r' || ch == '\f')
236                                 status = START;
237                         break;
238                 default:
239                         ErrPrint("Unknown status. couldn't be reach to here\n");
240                         break;
241                 }
242         } while (!feof(fp));
243         fclose(fp);
244
245         return NR_OF_SIZE_LIST - updated;
246 }
247
248 static int update_resolution(void)
249 {
250         Display *disp;
251         Window root;
252         Window dummy;
253         int x, y;
254         unsigned int width;
255         unsigned int height;
256         unsigned int border;
257         unsigned int depth;
258         register int i;
259
260         if (s_info.res_resolved)
261                 return 0;
262
263         disp = XOpenDisplay(NULL);
264         if (!disp) {
265                 ErrPrint("Failed to open a display\n");
266                 return -EFAULT;
267         }
268
269         root = XDefaultRootWindow(disp);
270         if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
271                 XCloseDisplay(disp);
272                 return -EFAULT;
273         }
274
275         if (update_from_file() == 0)
276                 DbgPrint("Resolution info is all updated by file\n");
277
278         DbgPrint("Screen resolution: %dx%d\n", width, height);
279         for (i = 0; i < NR_OF_SIZE_LIST; i++) {
280                 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
281                 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / 720.0f);
282                 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
283         }
284
285         XCloseDisplay(disp);
286         s_info.res_resolved = 1;
287         return 0;
288 }
289
290 static inline sqlite3 *open_db(void)
291 {
292         sqlite3 *handle;
293
294         if (!s_info.handle) {
295                 int ret;
296
297                 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
298                 if (ret != SQLITE_OK) {
299                         ErrPrint("Failed to open a DB\n");
300                         return NULL;
301                 }
302         } else {
303                 handle = s_info.handle;
304         }
305
306         return handle;
307 }
308
309 static inline void close_db(sqlite3 *handle)
310 {
311         if (!s_info.handle)
312                 db_util_close(handle);
313 }
314
315 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
316 {
317         int idx;
318
319         switch (type) {
320         case LB_SIZE_TYPE_1x1: /*!< 172x172 */
321                 idx = 0;
322                 break;
323         case LB_SIZE_TYPE_2x1: /*!< 348x172 */
324                 idx = 1;
325                 break;
326         case LB_SIZE_TYPE_2x2: /*!< 348x348 */
327                 idx = 2;
328                 break;
329         case LB_SIZE_TYPE_4x1: /*!< 700x172 */
330                 idx = 3;
331                 break;
332         case LB_SIZE_TYPE_4x2: /*!< 700x348 */
333                 idx = 4;
334                 break;
335         case LB_SIZE_TYPE_4x3: /*!< 700x520 */
336                 idx = 5;
337                 break;
338         case LB_SIZE_TYPE_4x4: /*!< 700x700 */
339                 idx = 6;
340                 break;
341         case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
342                 idx = 7;
343                 break;
344         case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
345                 idx = 8;
346                 break;
347         case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
348                 idx = 9;
349                 break;
350         default:
351                 return -EINVAL;
352         }
353
354         if (update_resolution() < 0)
355                 ErrPrint("Failed to update resolution\n");
356
357         *width = SIZE_LIST[idx].w;
358         *height = SIZE_LIST[idx].h;
359         return 0;
360 }
361
362 EAPI int livebox_service_change_period(const char *pkgname, const char *id, double period)
363 {
364         struct packet *packet;
365         struct packet *result;
366         char *uri;
367         int ret;
368
369         if (!pkgname || !id || period < 0.0f) {
370                 ErrPrint("Invalid argument\n");
371                 return -EINVAL;
372         }
373
374         uri = util_id_to_uri(id);
375         if (!uri)
376                 return -ENOMEM;
377
378         packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
379         free(uri);
380         if (!packet) {
381                 ErrPrint("Failed to create a packet for period changing\n");
382                 return -EFAULT;
383         }
384
385         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
386         packet_unref(packet);
387
388         if (result) {
389                 if (packet_get(result, "i", &ret) != 1) {
390                         ErrPrint("Failed to parse a result packet\n");
391                         ret = -EINVAL;
392                 }
393                 packet_unref(result);
394         } else {
395                 ErrPrint("Failed to get result packet\n");
396                 ret = -EFAULT;
397         }
398
399         return ret;
400 }
401
402 EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
403 {
404         struct packet *packet;
405         struct packet *result;
406         char *uri;
407         int ret;
408
409         if (!pkgname) {
410                 ErrPrint("Invalid argument\n");
411                 return -EINVAL;
412         }
413
414         if (!force && access("/tmp/.live.paused", R_OK) == 0) {
415                 DbgPrint("Provider is paused\n");
416                 return -ECANCELED;
417         }
418
419         uri = util_id_to_uri(id);
420         if (!uri)
421                 return -ENOMEM;
422
423         if (!cluster)
424                 cluster = "user,created";
425
426         if (!category)
427                 category = "default";
428
429         packet = packet_create("service_update", "ssss", pkgname, uri, cluster, category);
430         free(uri);
431         if (!packet) {
432                 ErrPrint("Failed to create a packet for service_update\n");
433                 return -EFAULT;
434         }
435
436         result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
437         packet_unref(packet);
438
439         if (result) {
440                 if (packet_get(result, "i", &ret) != 1) {
441                         ErrPrint("Failed to parse a result packet\n");
442                         ret = -EINVAL;
443                 }
444
445                 packet_unref(result);
446         } else {
447                 ErrPrint("Failed to get result packet\n");
448                 ret = -EFAULT;
449         }
450
451         return ret;
452 }
453
454 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
455 {
456         int ret;
457         sqlite3_stmt *stmt;
458         char *appid;
459         char *pkgid;
460         int is_prime;
461         sqlite3 *handle;
462
463         if (!cb)
464                 return -EINVAL;
465
466         handle = open_db();
467         if (!handle)
468                 return -EIO;
469
470         ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
471         if (ret != SQLITE_OK) {
472                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
473                 ret = -EIO;
474                 goto out;
475         }
476
477         ret = 0;
478         while (sqlite3_step(stmt) == SQLITE_ROW) {
479                 appid = (char *)sqlite3_column_text(stmt, 0);
480                 if (!appid || !strlen(appid)) {
481                         ErrPrint("APPID is not valid\n");
482                         continue;
483                 }
484
485                 pkgid = (char *)sqlite3_column_text(stmt, 1);
486                 if (!pkgid || !strlen(pkgid)) {
487                         ErrPrint("pkgid is not valid\n");
488                         continue;
489                 }
490
491                 is_prime = sqlite3_column_int(stmt, 2);
492
493                 ret++;
494
495                 if (cb(appid, pkgid, is_prime, data) < 0) {
496                         DbgPrint("Callback stopped package crawling\n");
497                         break;
498                 }
499         }
500
501         sqlite3_reset(stmt);
502         sqlite3_finalize(stmt);
503
504 out:
505         close_db(handle);
506         return ret;
507 }
508
509 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
510 {
511         sqlite3_stmt *stmt;
512         sqlite3 *handle;
513         int size;
514         int ret;
515
516         if (!types || !cnt || !pkgid)
517                 return -EINVAL;
518
519         handle = open_db();
520         if (!handle)
521                 return -EIO;
522
523         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
524         if (ret != SQLITE_OK) {
525                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
526                 ret = -EIO;
527                 goto out;
528         }
529
530         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
531         if (ret != SQLITE_OK) {
532                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
533                 sqlite3_reset(stmt);
534                 sqlite3_finalize(stmt);
535                 ret = -EIO;
536                 goto out;
537         }
538
539         if (*cnt > NR_OF_SIZE_LIST)
540                 *cnt = NR_OF_SIZE_LIST;
541
542         ret = 0;
543         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
544                 size = sqlite3_column_int(stmt, 0);
545                 types[ret] = size;
546                 ret++;
547         }
548
549         *cnt = ret;
550         sqlite3_reset(stmt);
551         sqlite3_finalize(stmt);
552         ret = 0;
553 out:
554         close_db(handle);
555         return ret;
556 }
557
558 static inline char *cur_locale(void)
559 {
560         char *language;
561         language = vconf_get_str(VCONFKEY_LANGSET);
562         if (language) {
563                 char *ptr;
564
565                 ptr = language;
566                 while (*ptr) {
567                         if (*ptr == '.') {
568                                 *ptr = '\0';
569                                 break;
570                         }
571
572                         if (*ptr == '_')
573                                 *ptr = '-';
574
575                         ptr++;
576                 }
577         } else {
578                 language = strdup("en-us");
579                 if (!language)
580                         ErrPrint("Heap: %s\n", strerror(errno));
581         }
582
583         return language;
584 }
585
586 static inline char *get_default_name(const char *pkgid)
587 {
588         sqlite3_stmt *stmt;
589         sqlite3 *handle;
590         char *name = NULL;
591         int ret;
592
593         handle = open_db();
594         if (!handle)
595                 return NULL;
596
597         ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
598         if (ret != SQLITE_OK) {
599                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
600                 close_db(handle);
601                 return NULL;
602         }
603
604         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
605         if (ret != SQLITE_OK) {
606                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
607                 goto out;
608         }
609
610         ret = sqlite3_step(stmt);
611         if (ret ==  SQLITE_ROW) {
612                 const char *tmp;
613
614                 tmp = (const char *)sqlite3_column_text(stmt, 0);
615                 if (tmp && strlen(tmp)) {
616                         name = strdup(tmp);
617                         if (!name)
618                                 ErrPrint("Heap: %s\n", strerror(errno));
619                 }
620         }
621
622 out:
623         sqlite3_reset(stmt);
624         sqlite3_finalize(stmt);
625         close_db(handle);
626         return name;
627 }
628
629 static inline char *get_default_icon(const char *pkgid)
630 {
631         sqlite3_stmt *stmt;
632         sqlite3 *handle;
633         char *icon = NULL;
634         int ret;
635
636         handle = open_db();
637         if (!handle)
638                 return NULL;
639
640         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
641         if (ret != SQLITE_OK) {
642                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
643                 close_db(handle);
644                 return NULL;
645         }
646
647         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
648         if (ret != SQLITE_OK) {
649                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
650                 goto out;
651         }
652
653         ret = sqlite3_step(stmt);
654         if (ret == SQLITE_ROW) {
655                 const char *tmp;
656
657                 tmp = (const char *)sqlite3_column_text(stmt, 0);
658                 if (tmp && strlen(tmp)) {
659                         icon = strdup(tmp);
660                         if (!icon)
661                                 ErrPrint("Heap: %s\n", strerror(errno));
662                 }
663         }
664
665 out:
666         sqlite3_reset(stmt);
667         sqlite3_finalize(stmt);
668         close_db(handle);
669         return icon;
670 }
671
672 EAPI char *livebox_service_content(const char *pkgid)
673 {
674         sqlite3_stmt *stmt;
675         sqlite3 *handle;
676         char *content = NULL;
677         int ret;
678
679         handle = open_db();
680         if (!handle)
681                 return NULL;
682
683         ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
684         if (ret != SQLITE_OK) {
685                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
686                 close_db(handle);
687                 return NULL;
688         }
689
690         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
691         if (ret != SQLITE_OK) {
692                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
693                 goto out;
694         }
695
696         ret = sqlite3_step(stmt);
697         if (ret == SQLITE_ROW) {
698                 const char *tmp;
699
700                 tmp = (const char *)sqlite3_column_text(stmt, 0);
701                 if (tmp && strlen(tmp)) {
702                         content = strdup(tmp);
703                         if (!content)
704                                 ErrPrint("Heap: %s\n", strerror(errno));
705                 }
706         }
707
708 out:
709         sqlite3_reset(stmt);
710         sqlite3_finalize(stmt);
711         close_db(handle);
712         return content;
713 }
714
715 EAPI char *livebox_service_setup_appid(const char *lbid)
716 {
717         sqlite3_stmt *stmt;
718         sqlite3 *handle;
719         int ret;
720         char *appid;
721
722         handle = open_db();
723         if (!handle)
724                 return NULL;
725
726         ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
727         if (ret != SQLITE_OK) {
728                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
729                 close_db(handle);
730                 return NULL;
731         }
732
733         appid = NULL;
734         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
735         if (ret != SQLITE_OK) {
736                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
737                 goto out;
738         }
739
740         ret = sqlite3_step(stmt);
741         if (ret == SQLITE_ROW) {
742                 const char *tmp;
743
744                 tmp = (const char *)sqlite3_column_text(stmt, 0);
745                 if (!tmp || !strlen(tmp))
746                         goto out;
747
748                 appid = strdup(tmp);
749                 if (!appid)
750                         ErrPrint("Error: %s\n", strerror(errno));
751         }
752
753 out:
754         sqlite3_reset(stmt);
755         sqlite3_finalize(stmt);
756         close_db(handle);
757         return appid;
758 }
759
760 EAPI int livebox_service_nodisplay(const char *pkgid)
761 {
762         sqlite3_stmt *stmt;
763         sqlite3 *handle;
764         int ret;
765
766         handle = open_db();
767         if (!handle)
768                 return 0;
769
770         ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
771         if (ret != SQLITE_OK) {
772                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
773                 close_db(handle);
774                 return 0;
775         }
776
777         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
778         if (ret != SQLITE_OK) {
779                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
780                 ret = 0;
781                 goto out;
782         }
783
784         ret = sqlite3_step(stmt);
785         if (ret == SQLITE_ROW)
786                 ret = !!sqlite3_column_int(stmt, 0);
787         else
788                 ret = 0;
789
790 out:
791         sqlite3_reset(stmt);
792         sqlite3_finalize(stmt);
793         close_db(handle);
794         return ret;
795 }
796
797 static inline char *get_lb_pkgname_by_appid(const char *appid)
798 {
799         sqlite3_stmt *stmt;
800         char *pkgid;
801         char *tmp;
802         sqlite3 *handle;
803         int ret;
804
805         if (!appid)
806                 return NULL;
807
808         pkgid = NULL;
809         handle = open_db();
810         if (!handle)
811                 return NULL;
812
813         ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
814         if (ret != SQLITE_OK) {
815                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
816                 close_db(handle);
817                 return NULL;
818         }
819
820         ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
821         if (ret != SQLITE_OK) {
822                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
823                 goto out;
824         }
825
826         ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
827         if (ret != SQLITE_OK) {
828                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
829                 goto out;
830         }
831
832         if (sqlite3_step(stmt) != SQLITE_ROW) {
833                 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
834                 goto out;
835         }
836
837         tmp = (char *)sqlite3_column_text(stmt, 0);
838         if (tmp && strlen(tmp)) {
839                 pkgid = strdup(tmp);
840                 if (!pkgid)
841                         ErrPrint("Heap: %s\n", strerror(errno));
842         }
843
844 out:
845         sqlite3_reset(stmt);
846         sqlite3_finalize(stmt);
847         close_db(handle);
848         return pkgid;
849 }
850
851 EAPI int livebox_service_touch_effect(const char *pkgid)
852 {
853         char *lbid;
854         sqlite3_stmt *stmt;
855         sqlite3 *handle;
856         int ret;
857
858         handle = open_db();
859         if (!handle) {
860                 ErrPrint("Unable to open a DB\n");
861                 return 1;
862         }
863
864         ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM client WHERE pkgid = ?", -1, &stmt, NULL);
865         if (ret != SQLITE_OK) {
866                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
867                 close_db(handle);
868                 return 1;
869         }
870
871         /*!
872          * \note
873          * This function will validate the "pkgid"
874          * call the exported API in the exported API is not recomended
875          * but... I used.
876          */
877         lbid = livebox_service_pkgname(pkgid);
878         if (!lbid) {
879                 ErrPrint("Invalid appid (%s)\n", pkgid);
880                 ret = 1;
881                 goto out;
882         }
883
884         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
885         free(lbid);
886         if (ret != SQLITE_OK) {
887                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
888                 ret = 1;
889                 goto out;
890         }
891
892         ret = sqlite3_step(stmt);
893         if (ret == SQLITE_ROW) {
894                 ret = !!sqlite3_column_int(stmt, 0);
895         } else {
896                 ret = 1; /*!< Default true: In this case the DB is corrupted. */
897                 ErrPrint("There is no result\n");
898         }
899
900 out:
901         sqlite3_reset(stmt);
902         sqlite3_finalize(stmt);
903         close_db(handle);
904         return ret;
905 }
906
907 EAPI int livebox_service_mouse_event(const char *pkgid)
908 {
909         sqlite3_stmt *stmt;
910         sqlite3 *handle;
911         char *lbid;
912         int ret;
913
914         handle = open_db();
915         if (!handle)
916                 return 0;
917
918         ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
919         if (ret != SQLITE_OK) {
920                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
921                 close_db(handle);
922                 return 0;
923         }
924
925         lbid = livebox_service_pkgname(pkgid);
926         if (!lbid) {
927                 ErrPrint("Failed to get lbid: %s\n", pkgid);
928                 ret = 0;
929                 goto out;
930         }
931
932         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
933         free(lbid);
934         if (ret != SQLITE_OK) {
935                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
936                 ret = 0;
937                 goto out;
938         }
939
940         ret = sqlite3_step(stmt);
941         if (ret == SQLITE_ROW) {
942                 ret = !!sqlite3_column_int(stmt, 0);
943         } else {
944                 ret = 0; /*!< Default is false, In this case the DB is corrupted */
945                 ErrPrint("There is no result.\n");
946         }
947
948 out:
949         sqlite3_reset(stmt);
950         sqlite3_finalize(stmt);
951         close_db(handle);
952         return ret;
953 }
954
955 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
956 {
957         sqlite3_stmt *stmt;
958         sqlite3 *handle;
959         int ret;
960         char *preview = NULL;
961
962         handle = open_db();
963         if (!handle)
964                 return NULL;
965
966         ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
967         if (ret != SQLITE_OK) {
968                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
969                 close_db(handle);
970                 return NULL;
971         }
972
973         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
974         if (ret != SQLITE_OK) {
975                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
976                 goto out;
977         }
978
979         ret = sqlite3_bind_int(stmt, 2, size_type);
980         if (ret != SQLITE_OK) {
981                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
982                 goto out;
983         }
984
985         ret = sqlite3_step(stmt);
986         if (ret == SQLITE_ROW) {
987                 const char *tmp;
988                 tmp = (const char *)sqlite3_column_text(stmt, 0);
989                 if (tmp && strlen(tmp)) {
990                         preview = strdup(tmp);
991                         if (!preview)
992                                 ErrPrint("Heap: %s\n", strerror(errno));
993                 }
994         }
995
996 out:
997         sqlite3_reset(stmt);
998         sqlite3_finalize(stmt);
999         close_db(handle);
1000         return preview;
1001 }
1002
1003 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1004 {
1005         sqlite3_stmt *stmt;
1006         sqlite3 *handle;
1007         char *language;
1008         char *icon = NULL;
1009         int ret;
1010
1011         if (lang) {
1012                 language = strdup(lang);
1013                 if (!language) {
1014                         ErrPrint("Heap: %s\n", strerror(errno));
1015                         return NULL;
1016                 }
1017         } else {
1018                 language = cur_locale();
1019                 if (!language)
1020                         return NULL;
1021         }
1022
1023         handle = open_db();
1024         if (!handle) {
1025                 free(language);
1026                 return NULL;
1027         }
1028
1029         ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1030         if (ret != SQLITE_OK) {
1031                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1032                 close_db(handle);
1033                 free(language);
1034                 return NULL;
1035         }
1036
1037         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1038         if (ret != SQLITE_OK) {
1039                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1040                 goto out;
1041         }
1042
1043         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1044         if (ret != SQLITE_OK) {
1045                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1046                 goto out;
1047         }
1048
1049         ret = sqlite3_step(stmt);
1050         if (ret == SQLITE_ROW) {
1051                 const char *tmp;
1052                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1053                 if (!tmp || !strlen(tmp)) {
1054                         icon = get_default_icon(pkgid);
1055                 } else {
1056                         icon = strdup(tmp);
1057                         if (!icon)
1058                                 ErrPrint("Heap: %s\n", strerror(errno));
1059                 }
1060         } else {
1061                 icon = get_default_icon(pkgid);
1062         }
1063
1064 out:
1065         sqlite3_reset(stmt);
1066         sqlite3_finalize(stmt);
1067         close_db(handle);
1068         free(language);
1069         return icon;
1070 }
1071
1072 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1073 {
1074         sqlite3_stmt *stmt;
1075         sqlite3 *handle;
1076         char *language;
1077         char *name = NULL;
1078         int ret;
1079
1080         if (lang) {
1081                 language = strdup(lang);
1082                 if (!language) {
1083                         ErrPrint("Error: %s\n", strerror(errno));
1084                         return NULL;
1085                 }
1086         } else {
1087                 language = cur_locale();
1088                 if (!language)
1089                         return NULL;
1090         }
1091
1092         handle = open_db();
1093         if (!handle) {
1094                 free(language);
1095                 return NULL;
1096         }
1097
1098         ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1099         if (ret != SQLITE_OK) {
1100                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1101                 close_db(handle);
1102                 free(language);
1103                 return NULL;
1104         }
1105
1106         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1107         if (ret != SQLITE_OK) {
1108                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1109                 goto out;
1110         }
1111
1112         ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1113         if (ret != SQLITE_OK) {
1114                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1115                 goto out;
1116         }
1117
1118         ret = sqlite3_step(stmt);
1119         if (ret == SQLITE_ROW) {
1120                 const char *tmp;
1121                 tmp = (const char *)sqlite3_column_text(stmt, 0);
1122                 if (!tmp || !strlen(tmp)) {
1123                         name = get_default_name(pkgid);
1124                 } else {
1125                         name = strdup(tmp);
1126                         if (!name)
1127                                 ErrPrint("Heap: %s\n", strerror(errno));
1128                 }
1129         } else {
1130                 name = get_default_name(pkgid);
1131         }
1132
1133 out:
1134         sqlite3_reset(stmt);
1135         sqlite3_finalize(stmt);
1136         close_db(handle);
1137         free(language);
1138         return name;
1139 }
1140
1141 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1142 {
1143         sqlite3_stmt *stmt;
1144         sqlite3 *handle;
1145         int size;
1146         int ret;
1147
1148         if (!w || !h || !cnt || !pkgid)
1149                 return -EINVAL;
1150
1151         handle = open_db();
1152         if (!handle)
1153                 return -EIO;
1154
1155         ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1156         if (ret != SQLITE_OK) {
1157                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1158                 ret = -EIO;
1159                 goto out;
1160         }
1161
1162         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1163         if (ret != SQLITE_OK) {
1164                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1165                 sqlite3_reset(stmt);
1166                 sqlite3_finalize(stmt);
1167                 ret = -EIO;
1168                 goto out;
1169         }
1170
1171         if (*cnt > NR_OF_SIZE_LIST)
1172                 *cnt = NR_OF_SIZE_LIST;
1173
1174         ret = 0;
1175         while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1176                 size = sqlite3_column_int(stmt, 0);
1177                 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1178         }
1179
1180         *cnt = ret;
1181         sqlite3_reset(stmt);
1182         sqlite3_finalize(stmt);
1183         ret = 0;
1184 out:
1185         close_db(handle);
1186         return ret;
1187 }
1188
1189 EAPI char *livebox_service_libexec(const char *pkgid)
1190 {
1191         sqlite3_stmt *stmt;
1192         sqlite3 *handle;
1193         int ret;
1194         char *libexec;
1195         char *appid;
1196         char *path;
1197
1198         if (!pkgid)
1199                 return NULL;
1200
1201         libexec = NULL;
1202         handle = open_db();
1203         if (!handle)
1204                 return NULL;
1205
1206         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1207         if (ret != SQLITE_OK) {
1208                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1209                 goto out;
1210         }
1211
1212         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1213         if (ret != SQLITE_OK) {
1214                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1215                 sqlite3_finalize(stmt);
1216                 goto out;
1217         }
1218
1219         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1220         if (ret != SQLITE_OK) {
1221                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1222                 sqlite3_finalize(stmt);
1223                 goto out;
1224         }
1225
1226         if (sqlite3_step(stmt) != SQLITE_ROW) {
1227                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1228                 sqlite3_reset(stmt);
1229                 sqlite3_finalize(stmt);
1230
1231                 libexec = util_conf_get_libexec(pkgid);
1232                 DbgPrint("Fallback to conf checker: %s\n", libexec);
1233                 goto out;
1234         }
1235
1236         appid = (char *)sqlite3_column_text(stmt, 0);
1237         if (!appid || !strlen(appid)) {
1238                 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1239                 sqlite3_reset(stmt);
1240                 sqlite3_finalize(stmt);
1241                 goto out;
1242         }
1243
1244         path = (char *)sqlite3_column_text(stmt, 1);
1245         if (!path || !strlen(path)) {
1246                 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1247                 sqlite3_reset(stmt);
1248                 sqlite3_finalize(stmt);
1249                 goto out;
1250         }
1251
1252         libexec = strdup(path);
1253         if (!libexec) {
1254                 ErrPrint("Heap: %s\n", strerror(errno));
1255                 sqlite3_reset(stmt);
1256                 sqlite3_finalize(stmt);
1257                 goto out;
1258         }
1259
1260         DbgPrint("libexec: %s\n", libexec);
1261
1262         sqlite3_reset(stmt);
1263         sqlite3_finalize(stmt);
1264 out:
1265         close_db(handle);
1266         return libexec;
1267 }
1268
1269 EAPI char *livebox_service_pkgname(const char *appid)
1270 {
1271         char *lb_pkgname;
1272         pkgmgr_appinfo_h handle;
1273         int ret;
1274         char *new_appid;
1275
1276         if (!appid)
1277                 return NULL;
1278
1279         lb_pkgname = get_lb_pkgname_by_appid(appid);
1280         if (lb_pkgname)
1281                 return lb_pkgname;
1282
1283         /*!
1284          * \note
1285          * Try to get the package id using given appid
1286          */
1287         ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1288         if (ret != PKGMGR_R_OK) {
1289                 ErrPrint("Failed to get appinfo\n");
1290                 return NULL;
1291         }
1292
1293         ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1294         if (ret != PKGMGR_R_OK) {
1295                 pkgmgr_appinfo_destroy_appinfo(handle);
1296                 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1297                 return NULL;
1298         }
1299
1300         lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1301         pkgmgr_appinfo_destroy_appinfo(handle);
1302
1303         if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1304                 return strdup(appid);
1305
1306         return lb_pkgname;
1307 }
1308
1309 EAPI char *livebox_service_provider_name(const char *lbid)
1310 {
1311         char *ret;
1312         int stage = 0;
1313         int seq = 0;
1314         int idx = 0;
1315         char *str = SAMSUNG_PREFIX;
1316
1317         if (!lbid)
1318                 return NULL;
1319
1320         while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1321                 idx++;
1322                 if (seq < 2 && lbid[idx] == '.') {
1323                         stage = idx;
1324                         seq++;
1325                 }
1326         }
1327
1328         if (!str[idx] && lbid[idx]) {
1329                 /* Inhouse */
1330                 return strdup(lbid);
1331         } else if (seq < 2) {
1332                 while (seq < 2) {
1333                         if (lbid[idx] == '.') {
1334                                 seq++;
1335                         } else if (!lbid[idx]) {
1336                                 ErrPrint("Invalid lbid: %s\n", lbid);
1337                                 return NULL;
1338                         }
1339
1340                         idx++;
1341                 }
1342
1343                 stage = idx;
1344         } else {
1345                 stage++;
1346         }
1347
1348         ret = strdup(lbid + stage);
1349         if (!ret) {
1350                 ErrPrint("Error: %s\n", strerror(errno));
1351                 return NULL;
1352         }
1353
1354         return ret;
1355 }
1356
1357 EAPI int livebox_service_is_enabled(const char *lbid)
1358 {
1359         return 1;
1360         /*
1361         ail_appinfo_h ai;
1362         char *pkgname;
1363         bool enabled;
1364         int ret;
1365
1366         pkgname = livebox_service_appid(lbid);
1367         if (!pkgname)
1368                 return 0;
1369
1370         ret = ail_get_appinfo(pkgname, &ai);
1371         if (ret != AIL_ERROR_OK) {
1372                 free(pkgname);
1373                 return 0;
1374         }
1375
1376         if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
1377                 enabled = false;
1378
1379         ail_destroy_appinfo(ai);
1380         free(pkgname);
1381         return enabled == true;
1382         */
1383 }
1384
1385 EAPI int livebox_service_is_primary(const char *lbid)
1386 {
1387         sqlite3_stmt *stmt;
1388         sqlite3 *handle;
1389         int ret = 0;
1390
1391         if (!lbid)
1392                 return 0;
1393
1394         handle = open_db();
1395         if (!handle)
1396                 return 0;
1397
1398         ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap 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         ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1406         if (ret != SQLITE_OK) {
1407                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1408                 goto out;
1409         }
1410
1411         ret = sqlite3_step(stmt);
1412         if (ret != SQLITE_ROW) {
1413                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1414                 goto out;
1415         }
1416
1417         ret = sqlite3_column_int(stmt, 1);
1418
1419 out:
1420         sqlite3_reset(stmt);
1421         sqlite3_finalize(stmt);
1422         close_db(handle);
1423         return ret;
1424 }
1425
1426 /*!
1427  * appid == Package ID
1428  * pkgid == Livebox ID
1429  */
1430 EAPI char *livebox_service_appid(const char *pkgname)
1431 {
1432         sqlite3_stmt *stmt;
1433         char *appid;
1434         char *tmp;
1435         sqlite3 *handle;
1436         int is_prime __attribute__((__unused__));
1437         int ret;
1438
1439         if (!pkgname)
1440                 return NULL;
1441
1442         appid = NULL;
1443         handle = open_db();
1444         if (!handle)
1445                 return NULL;
1446
1447         ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1448         if (ret != SQLITE_OK) {
1449                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1450                 goto out;
1451         }
1452
1453         ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
1454         if (ret != SQLITE_OK) {
1455                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1456                 sqlite3_reset(stmt);
1457                 sqlite3_finalize(stmt);
1458                 goto out;
1459         }
1460
1461         ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
1462         if (ret != SQLITE_OK) {
1463                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1464                 sqlite3_reset(stmt);
1465                 sqlite3_finalize(stmt);
1466                 goto out;
1467         }
1468
1469         ret = sqlite3_step(stmt);
1470         if (ret != SQLITE_ROW) {
1471                 pkgmgr_appinfo_h pkg_handle;
1472                 char *new_appid;
1473
1474                 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1475                 sqlite3_reset(stmt);
1476                 sqlite3_finalize(stmt);
1477
1478                 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1479                 if (ret != PKGMGR_R_OK) {
1480                         ErrPrint("Failed to get appinfo: %s\n", pkgname);
1481                         goto out;
1482                 }
1483
1484                 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1485                 if (ret != PKGMGR_R_OK) {
1486                         ErrPrint("Failed to get pkgname for (%s)\n", appid);
1487                         pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1488                         goto out;
1489                 }
1490
1491                 appid = strdup(new_appid);
1492                 if (!appid)
1493                         ErrPrint("Heap: %s\n", strerror(errno));
1494
1495                 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1496                 goto out;
1497         }
1498
1499         tmp = (char *)sqlite3_column_text(stmt, 0);
1500         if (!tmp || !strlen(tmp)) {
1501                 ErrPrint("APPID is NIL\n");
1502                 sqlite3_reset(stmt);
1503                 sqlite3_finalize(stmt);
1504                 goto out;
1505         }
1506
1507         appid = strdup(tmp);
1508         if (!appid) {
1509                 ErrPrint("Heap: %s\n", strerror(errno));
1510                 sqlite3_reset(stmt);
1511                 sqlite3_finalize(stmt);
1512                 goto out;
1513         }
1514
1515         is_prime = sqlite3_column_int(stmt, 1);
1516
1517         sqlite3_reset(stmt);
1518         sqlite3_finalize(stmt);
1519 out:
1520         close_db(handle);
1521         return appid;
1522 }
1523
1524 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1525 {
1526         sqlite3_stmt *stmt;
1527         sqlite3 *handle;
1528         int ret;
1529         char *path;
1530         char *appid;
1531         char *lb_src;
1532
1533         if (!pkgid)
1534                 return NULL;
1535
1536         path = NULL;
1537         handle = open_db();
1538         if (!handle)
1539                 return NULL;
1540
1541         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1542         if (ret != SQLITE_OK) {
1543                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1544                 goto out;
1545         }
1546
1547         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1548         if (ret != SQLITE_OK) {
1549                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1550                 sqlite3_finalize(stmt);
1551                 goto out;
1552         }
1553
1554         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1555         if (ret != SQLITE_OK) {
1556                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1557                 sqlite3_finalize(stmt);
1558                 goto out;
1559         }
1560
1561         ret = sqlite3_step(stmt);
1562         if (ret != SQLITE_ROW) {
1563                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1564                 sqlite3_reset(stmt);
1565                 sqlite3_finalize(stmt);
1566                 goto out;
1567         }
1568
1569         appid = (char *)sqlite3_column_text(stmt, 0);
1570         if (!appid || !strlen(appid)) {
1571                 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1572                 sqlite3_reset(stmt);
1573                 sqlite3_finalize(stmt);
1574                 goto out;
1575         }
1576
1577         lb_src = (char *)sqlite3_column_text(stmt, 1);
1578         if (!lb_src || !strlen(lb_src)) {
1579                 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1580                 sqlite3_reset(stmt);
1581                 sqlite3_finalize(stmt);
1582                 goto out;
1583         }
1584
1585         path = strdup(lb_src);
1586         if (!path) {
1587                 ErrPrint("Heap: %s\n", strerror(errno));
1588                 sqlite3_reset(stmt);
1589                 sqlite3_finalize(stmt);
1590                 goto out;
1591         }
1592
1593         DbgPrint("LB Src: %s\n", path);
1594
1595         sqlite3_reset(stmt);
1596         sqlite3_finalize(stmt);
1597 out:
1598         close_db(handle);
1599         return path;
1600 }
1601
1602 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1603 {
1604         sqlite3_stmt *stmt;
1605         sqlite3 *handle;
1606         int ret;
1607         char *group;
1608         char *tmp;
1609
1610         if (!pkgid)
1611                 return NULL;
1612
1613         group = NULL;
1614         handle = open_db();
1615         if (!handle)
1616                 return NULL;
1617
1618         ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1619         if (ret != SQLITE_OK) {
1620                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1621                 goto out;
1622         }
1623
1624         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1625         if (ret != SQLITE_OK) {
1626                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1627                 sqlite3_finalize(stmt);
1628                 goto out;
1629         }
1630
1631         ret = sqlite3_step(stmt);
1632         if (ret != SQLITE_ROW) {
1633                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1634                 sqlite3_reset(stmt);
1635                 sqlite3_finalize(stmt);
1636                 goto out;
1637         }
1638
1639         tmp = (char *)sqlite3_column_text(stmt, 0);
1640         if (tmp && strlen(tmp)) {
1641                 group = strdup(tmp);
1642                 if (!group)
1643                         ErrPrint("Heap: %s\n", strerror(errno));
1644         }
1645
1646         sqlite3_reset(stmt);
1647         sqlite3_finalize(stmt);
1648 out:
1649         close_db(handle);
1650         return group;
1651 }
1652
1653 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1654 {
1655         sqlite3_stmt *stmt;
1656         sqlite3 *handle;
1657         int ret;
1658         char *path;
1659         char *pd_src;
1660         const char *appid;
1661
1662         if (!pkgid)
1663                 return NULL;
1664
1665         path = NULL;
1666         handle = open_db();
1667         if (!handle)
1668                 return NULL;
1669
1670         ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1671         if (ret != SQLITE_OK) {
1672                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1673                 goto out;
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                 sqlite3_finalize(stmt);
1680                 goto out;
1681         }
1682
1683         ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1684         if (ret != SQLITE_OK) {
1685                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1686                 sqlite3_finalize(stmt);
1687                 goto out;
1688         }
1689
1690         ret = sqlite3_step(stmt);
1691         if (ret != SQLITE_ROW) {
1692                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1693                 sqlite3_reset(stmt);
1694                 sqlite3_finalize(stmt);
1695                 goto out;
1696         }
1697
1698         appid = (char *)sqlite3_column_text(stmt, 0);
1699         if (!appid || !strlen(appid)) {
1700                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1701                 sqlite3_reset(stmt);
1702                 sqlite3_finalize(stmt);
1703                 goto out;
1704         }
1705
1706         pd_src = (char *)sqlite3_column_text(stmt, 1);
1707         if (!pd_src || !strlen(pd_src)) {
1708                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1709                 sqlite3_reset(stmt);
1710                 sqlite3_finalize(stmt);
1711                 goto out;
1712         }
1713
1714         path = strdup(pd_src);
1715         if (!path) {
1716                 ErrPrint("Heap: %s\n", strerror(errno));
1717                 sqlite3_reset(stmt);
1718                 sqlite3_finalize(stmt);
1719                 goto out;
1720         }
1721
1722         DbgPrint("PD Src: %s\n", path);
1723         sqlite3_reset(stmt);
1724         sqlite3_finalize(stmt);
1725 out:
1726         close_db(handle);
1727         return path;
1728 }
1729
1730 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1731 {
1732         sqlite3_stmt *stmt;
1733         sqlite3 *handle;
1734         int ret;
1735         char *group;
1736         char *tmp;
1737
1738         if (!pkgid)
1739                 return NULL;
1740
1741         group = NULL;
1742         handle = open_db();
1743         if (!handle)
1744                 return NULL;
1745
1746         ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1747         if (ret != SQLITE_OK) {
1748                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1749                 goto out;
1750         }
1751
1752         ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1753         if (ret != SQLITE_OK) {
1754                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1755                 sqlite3_finalize(stmt);
1756                 goto out;
1757         }
1758
1759         ret = sqlite3_step(stmt);
1760         if (ret != SQLITE_ROW) {
1761                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1762                 sqlite3_reset(stmt);
1763                 sqlite3_finalize(stmt);
1764                 goto out;
1765         }
1766
1767         tmp = (char *)sqlite3_column_text(stmt, 0);
1768         if (tmp && strlen(tmp)) {
1769                 group = strdup(tmp);
1770                 if (!group)
1771                         ErrPrint("Heap: %s\n", strerror(errno));
1772         }
1773         sqlite3_reset(stmt);
1774         sqlite3_finalize(stmt);
1775 out:
1776         close_db(handle);
1777         return group;
1778 }
1779
1780 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1781 {
1782         sqlite3_stmt *stmt;
1783         sqlite3 *handle;
1784         const char *cluster;
1785         int cnt;
1786         int ret;
1787
1788         if (!cb)
1789                 return -EINVAL;
1790
1791         handle = open_db();
1792         if (!handle)
1793                 return -EIO;
1794
1795         cnt = 0;
1796         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1797         if (ret != SQLITE_OK) {
1798                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1799                 cnt = -EIO;
1800                 goto out;
1801         }
1802
1803         while (sqlite3_step(stmt) == SQLITE_ROW) {
1804                 cluster = (const char *)sqlite3_column_text(stmt, 0);
1805                 if (!cluster || !strlen(cluster))
1806                         continue;
1807
1808                 if (cb(cluster, data) < 0)
1809                         break;
1810
1811                 cnt++;
1812         }
1813
1814         sqlite3_reset(stmt);
1815         sqlite3_finalize(stmt);
1816 out:
1817         close_db(handle);
1818         return cnt;
1819 }
1820
1821 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1822 {
1823         sqlite3_stmt *stmt;
1824         sqlite3 *handle;
1825         const char *category;
1826         int cnt;
1827         int ret;
1828
1829         if (!cluster || !cb)
1830                 return -EINVAL;
1831
1832         handle = open_db();
1833         if (!handle)
1834                 return -EIO;
1835
1836         ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1837         if (ret != SQLITE_OK) {
1838                 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1839                 cnt = -EIO;
1840                 goto out;
1841         }
1842
1843         cnt = 0;
1844         while (sqlite3_step(stmt) == SQLITE_ROW) {
1845                 category = (const char *)sqlite3_column_text(stmt, 0);
1846                 if (!category || !strlen(category))
1847                         continue;
1848
1849                 if (cb(cluster, category, data) < 0)
1850                         break;
1851
1852                 cnt++;
1853         }
1854
1855         sqlite3_reset(stmt);
1856         sqlite3_finalize(stmt);
1857 out:
1858         close_db(handle);
1859         return cnt;
1860 }
1861
1862 EAPI int livebox_service_init(void)
1863 {
1864         if (s_info.handle) {
1865                 DbgPrint("Already initialized\n");
1866                 s_info.init_count++;
1867                 return 0;
1868         }
1869
1870         s_info.handle = open_db();
1871         if (s_info.handle) {
1872                 s_info.init_count++;
1873                 return 0;
1874         }
1875
1876         return -EIO;
1877 }
1878
1879 EAPI int livebox_service_fini(void)
1880 {
1881         if (!s_info.handle || s_info.init_count <= 0) {
1882                 ErrPrint("Service is not initialized\n");
1883                 return -EIO;
1884         }
1885
1886         s_info.init_count--;
1887         if (s_info.init_count > 0) {
1888                 DbgPrint("Init count %d\n", s_info.init_count);
1889                 return 0;
1890         }
1891
1892         db_util_close(s_info.handle);
1893         s_info.handle = NULL;
1894         return 0;
1895 }
1896
1897 EAPI int livebox_service_get_size(int type, int *width, int *height)
1898 {
1899         int _width;
1900         int _height;
1901
1902         if (!width)
1903                 width = &_width;
1904
1905         if (!height)
1906                 height = &_height;
1907
1908         return convert_size_from_type(type, width, height);
1909 }
1910
1911 EAPI int livebox_service_size_type(int width, int height)
1912 {
1913         int idx;
1914
1915         if (update_resolution() < 0)
1916                 ErrPrint("Failed to update the size list\n");
1917
1918         for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
1919                 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
1920                         break;
1921         }
1922
1923         switch (idx) {
1924         case 0:
1925                 return LB_SIZE_TYPE_1x1;
1926         case 1:
1927                 return LB_SIZE_TYPE_2x1;
1928         case 2:
1929                 return LB_SIZE_TYPE_2x2;
1930         case 3:
1931                 return LB_SIZE_TYPE_4x1;
1932         case 4:
1933                 return LB_SIZE_TYPE_4x2;
1934         case 5:
1935                 return LB_SIZE_TYPE_4x3;
1936         case 6:
1937                 return LB_SIZE_TYPE_4x4;
1938         case 7:
1939                 return LB_SIZE_TYPE_EASY_1x1;
1940         case 8:
1941                 return LB_SIZE_TYPE_EASY_3x1;
1942         case 9:
1943                 return LB_SIZE_TYPE_EASY_3x3;
1944         default:
1945                 break;
1946         }
1947
1948         return LB_SIZE_TYPE_UNKNOWN;
1949 }
1950
1951 /* End of a file */