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