2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
19 #include <stdlib.h> /* malloc */
20 #include <string.h> /* strdup, strerror */
21 #include <sys/types.h>
29 #include <com-core_packet.h>
33 #include <package-manager.h>
35 #include <vconf-keys.h>
41 #include "livebox-service.h"
42 #include "livebox-errno.h"
44 #define SAMSUNG_PREFIX "com.samsung."
45 #define EAPI __attribute__((visibility("default")))
46 #define DEFAULT_TIMEOUT 2.0
49 static struct supported_size_list {
52 } SIZE_LIST[NR_OF_SIZE_LIST] = {
53 { 175, 175 }, /*!< 1x1 */
54 { 354, 175 }, /*!< 2x1 */
55 { 354, 354 }, /*!< 2x2 */
56 { 712, 175 }, /*!< 4x1 */
57 { 712, 354 }, /*!< 4x2 */
58 { 712, 533 }, /*!< 4x3 */
59 { 712, 712 }, /*!< 4x4 */
60 { 712, 891 }, /*!< 4x5 */
61 { 712, 1070 }, /*!< 4x6 */
62 { 207, 207 }, /*!< 21x21 */
63 { 645, 207 }, /*!< 23x21 */
64 { 645, 645 }, /*!< 23x23 */
65 { 720, 1280 }, /*!< 0x0 */
71 const char *conf_file;
76 .dbfile = "/opt/dbspace/.livebox.db",
77 .conf_file = "/usr/share/data-provider-master/resolution.ini",
82 static inline int update_info(int width_type, int height_type, int width, int height)
86 if (width_type == 1 && height_type == 1) {
87 DbgPrint("1x1 Updated to %dx%d\n", width, height);
89 } else if (width_type == 2 && height_type == 1) {
90 DbgPrint("2x1 Updated to %dx%d\n", width, height);
92 } else if (width_type == 2 && height_type == 2) {
93 DbgPrint("2x2 Updated to %dx%d\n", width, height);
95 } else if (width_type == 4 && height_type == 1) {
96 DbgPrint("4x1 Updated to %dx%d\n", width, height);
98 } else if (width_type == 4 && height_type == 2) {
99 DbgPrint("4x2 Updated to %dx%d\n", width, height);
101 } else if (width_type == 4 && height_type == 3) {
102 DbgPrint("4x3 Updated to %dx%d\n", width, height);
104 } else if (width_type == 4 && height_type == 4) {
105 DbgPrint("4x4 Updated to %dx%d\n", width, height);
107 } else if (width_type == 4 && height_type == 5) {
108 DbgPrint("4x5 Updated to %dx%d\n", width, height);
110 } else if (width_type == 4 && height_type == 6) {
111 DbgPrint("4x6 Updated to %dx%d\n", width, height);
113 } else if (width_type == 21 && height_type == 21) {
114 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
116 } else if (width_type == 23 && height_type == 21) {
117 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
119 } else if (width_type == 23 && height_type == 23) {
120 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
122 } else if (width_type == 0 && height_type == 0) {
123 DbgPrint("Special 0x0 Updated to %dx%d\n", width, height);
126 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
130 SIZE_LIST[idx].w = width;
131 SIZE_LIST[idx].h = height;
135 static inline int update_from_file(void)
143 char buffer[MAX_COLUMN];
157 fp = fopen(s_info.conf_file, "r");
159 ErrPrint("Open failed: %s\n", strerror(errno));
160 return LB_STATUS_ERROR_IO;
169 if (idx == MAX_COLUMN) {
170 ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
176 if (isspace(ch) || ch == EOF)
191 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
192 ErrPrint("Invalid syntax: [%s]\n", buffer);
196 } else if (ch == '=') {
199 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
200 ErrPrint("Invalid syntax: [%s]\n", buffer);
204 } else if (ch == EOF) {
205 ErrPrint("Invalid Syntax\n");
216 if (isspace(ch) || ch == EOF)
224 if (isspace(ch) || ch == EOF) {
228 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
229 ErrPrint("Invalid syntax: [%s]\n", buffer);
231 } else if (ch == EOF) {
232 updated += update_info(width_type, height_type, width, height);
239 updated += update_info(width_type, height_type, width, height);
244 if (ch == '\n' || ch == '\r' || ch == '\f')
248 if (ch == '\n' || ch == '\r' || ch == '\f')
252 ErrPrint("Unknown status. couldn't be reach to here\n");
258 return NR_OF_SIZE_LIST - updated;
261 static int update_resolution(void)
273 if (s_info.res_resolved)
274 return LB_STATUS_SUCCESS;
276 disp = XOpenDisplay(NULL);
278 ErrPrint("Failed to open a display\n");
279 return LB_STATUS_ERROR_FAULT;
282 root = XDefaultRootWindow(disp);
283 if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
285 return LB_STATUS_ERROR_FAULT;
288 if (update_from_file() == 0)
289 DbgPrint("Resolution info is all updated by file\n");
291 DbgPrint("Screen resolution: %dx%d\n", width, height);
292 for (i = 0; i < NR_OF_SIZE_LIST; i++) {
293 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
294 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / 720.0f);
295 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
299 s_info.res_resolved = 1;
300 return LB_STATUS_SUCCESS;
303 static inline sqlite3 *open_db(void)
307 if (!s_info.handle) {
310 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
311 if (ret != SQLITE_OK) {
312 ErrPrint("Failed to open a DB\n");
316 handle = s_info.handle;
322 static inline void close_db(sqlite3 *handle)
325 db_util_close(handle);
328 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
333 case LB_SIZE_TYPE_1x1: /*!< 175x175 */
336 case LB_SIZE_TYPE_2x1: /*!< 354x175 */
339 case LB_SIZE_TYPE_2x2: /*!< 354x354 */
342 case LB_SIZE_TYPE_4x1: /*!< 712x175 */
345 case LB_SIZE_TYPE_4x2: /*!< 712x354 */
348 case LB_SIZE_TYPE_4x3: /*!< 712x533 */
351 case LB_SIZE_TYPE_4x4: /*!< 712x712 */
354 case LB_SIZE_TYPE_4x5: /*!< 712x891 */
357 case LB_SIZE_TYPE_4x6: /*!< 712x1070 */
360 case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
363 case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
366 case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
369 case LB_SIZE_TYPE_0x0: /*!< 720x1280 */
373 return LB_STATUS_ERROR_INVALID;
376 if (update_resolution() < 0)
377 ErrPrint("Failed to update resolution\n");
379 *width = SIZE_LIST[idx].w;
380 *height = SIZE_LIST[idx].h;
381 return LB_STATUS_SUCCESS;
384 EAPI int livebox_service_change_period(const char *pkgname, const char *id, double period)
386 struct packet *packet;
387 struct packet *result;
391 if (!pkgname || !id || period < 0.0f) {
392 ErrPrint("Invalid argument\n");
393 return LB_STATUS_ERROR_INVALID;
396 uri = util_id_to_uri(id);
398 return LB_STATUS_ERROR_MEMORY;
400 packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
403 ErrPrint("Failed to create a packet for period changing\n");
404 return LB_STATUS_ERROR_FAULT;
407 result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
408 packet_unref(packet);
411 if (packet_get(result, "i", &ret) != 1) {
412 ErrPrint("Failed to parse a result packet\n");
413 ret = LB_STATUS_ERROR_INVALID;
415 packet_unref(result);
417 ErrPrint("Failed to get result packet\n");
418 ret = LB_STATUS_ERROR_FAULT;
424 EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
426 struct packet *packet;
427 struct packet *result;
432 ErrPrint("Invalid argument\n");
433 return LB_STATUS_ERROR_INVALID;
436 if (!force && access("/tmp/.live.paused", R_OK) == 0) {
437 DbgPrint("Provider is paused\n");
438 return LB_STATUS_ERROR_CANCEL;
441 uri = util_id_to_uri(id);
443 return LB_STATUS_ERROR_MEMORY;
446 cluster = "user,created";
449 category = "default";
451 packet = packet_create("service_update", "ssss", pkgname, uri, cluster, category);
454 ErrPrint("Failed to create a packet for service_update\n");
455 return LB_STATUS_ERROR_FAULT;
458 result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
459 packet_unref(packet);
462 if (packet_get(result, "i", &ret) != 1) {
463 ErrPrint("Failed to parse a result packet\n");
464 ret = LB_STATUS_ERROR_INVALID;
467 packet_unref(result);
469 ErrPrint("Failed to get result packet\n");
470 ret = LB_STATUS_ERROR_FAULT;
476 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
486 return LB_STATUS_ERROR_INVALID;
490 return LB_STATUS_ERROR_IO;
492 ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
493 if (ret != SQLITE_OK) {
494 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
495 ret = LB_STATUS_ERROR_IO;
500 while (sqlite3_step(stmt) == SQLITE_ROW) {
501 appid = (char *)sqlite3_column_text(stmt, 0);
502 if (!appid || !strlen(appid)) {
503 ErrPrint("APPID is not valid\n");
507 pkgid = (char *)sqlite3_column_text(stmt, 1);
508 if (!pkgid || !strlen(pkgid)) {
509 ErrPrint("pkgid is not valid\n");
513 is_prime = sqlite3_column_int(stmt, 2);
517 if (cb(appid, pkgid, is_prime, data) < 0) {
518 DbgPrint("Callback stopped package crawling\n");
524 sqlite3_finalize(stmt);
531 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
538 if (!types || !cnt || !pkgid)
539 return LB_STATUS_ERROR_INVALID;
543 return LB_STATUS_ERROR_IO;
545 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
546 if (ret != SQLITE_OK) {
547 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
548 ret = LB_STATUS_ERROR_IO;
552 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
553 if (ret != SQLITE_OK) {
554 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
556 sqlite3_finalize(stmt);
557 ret = LB_STATUS_ERROR_IO;
561 if (*cnt > NR_OF_SIZE_LIST)
562 *cnt = NR_OF_SIZE_LIST;
565 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
566 size = sqlite3_column_int(stmt, 0);
573 sqlite3_finalize(stmt);
580 static inline char *cur_locale(void)
583 language = vconf_get_str(VCONFKEY_LANGSET);
600 language = strdup("en-us");
602 ErrPrint("Heap: %s\n", strerror(errno));
608 static inline char *get_default_name(const char *pkgid)
619 ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
620 if (ret != SQLITE_OK) {
621 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
626 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
627 if (ret != SQLITE_OK) {
628 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
632 ret = sqlite3_step(stmt);
633 if (ret == SQLITE_ROW) {
636 tmp = (const char *)sqlite3_column_text(stmt, 0);
637 if (tmp && strlen(tmp)) {
640 ErrPrint("Heap: %s\n", strerror(errno));
646 sqlite3_finalize(stmt);
651 static inline char *get_default_icon(const char *pkgid)
662 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
663 if (ret != SQLITE_OK) {
664 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
669 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
670 if (ret != SQLITE_OK) {
671 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
675 ret = sqlite3_step(stmt);
676 if (ret == SQLITE_ROW) {
679 tmp = (const char *)sqlite3_column_text(stmt, 0);
680 if (tmp && strlen(tmp)) {
683 ErrPrint("Heap: %s\n", strerror(errno));
689 sqlite3_finalize(stmt);
694 EAPI char *livebox_service_content(const char *pkgid)
698 char *content = NULL;
705 ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
706 if (ret != SQLITE_OK) {
707 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
712 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
713 if (ret != SQLITE_OK) {
714 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
718 ret = sqlite3_step(stmt);
719 if (ret == SQLITE_ROW) {
722 tmp = (const char *)sqlite3_column_text(stmt, 0);
723 if (tmp && strlen(tmp)) {
724 content = strdup(tmp);
726 ErrPrint("Heap: %s\n", strerror(errno));
732 sqlite3_finalize(stmt);
737 EAPI char *livebox_service_setup_appid(const char *lbid)
748 ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
749 if (ret != SQLITE_OK) {
750 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
756 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
757 if (ret != SQLITE_OK) {
758 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
762 ret = sqlite3_step(stmt);
763 if (ret == SQLITE_ROW) {
766 tmp = (const char *)sqlite3_column_text(stmt, 0);
767 if (!tmp || !strlen(tmp))
772 ErrPrint("Error: %s\n", strerror(errno));
777 sqlite3_finalize(stmt);
782 EAPI int livebox_service_nodisplay(const char *pkgid)
792 ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
793 if (ret != SQLITE_OK) {
794 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
799 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
800 if (ret != SQLITE_OK) {
801 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
806 ret = sqlite3_step(stmt);
807 if (ret == SQLITE_ROW)
808 ret = !!sqlite3_column_int(stmt, 0);
814 sqlite3_finalize(stmt);
819 static inline char *get_lb_pkgname_by_appid(const char *appid)
835 ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
836 if (ret != SQLITE_OK) {
837 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
842 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
843 if (ret != SQLITE_OK) {
844 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
848 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
849 if (ret != SQLITE_OK) {
850 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
854 if (sqlite3_step(stmt) != SQLITE_ROW) {
855 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
859 tmp = (char *)sqlite3_column_text(stmt, 0);
860 if (tmp && strlen(tmp)) {
863 ErrPrint("Heap: %s\n", strerror(errno));
868 sqlite3_finalize(stmt);
873 EAPI int livebox_service_need_frame(const char *pkgid, int size_type)
882 ErrPrint("Unable to open a DB\n");
886 ret = sqlite3_prepare_v2(handle, "SELECT need_frame FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
887 if (ret != SQLITE_OK) {
888 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
895 lbid = livebox_service_pkgname(pkgid);
897 ErrPrint("Invalid appid (%s)\n", pkgid);
902 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
904 if (ret != SQLITE_OK) {
905 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
910 ret = sqlite3_bind_int(stmt, 2, size_type);
911 if (ret != SQLITE_OK) {
912 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
917 ret = sqlite3_step(stmt);
918 if (ret == SQLITE_ROW) {
919 ret = !!sqlite3_column_int(stmt, 0);
922 ErrPrint("There is no such result\n");
926 sqlite3_finalize(stmt);
931 EAPI int livebox_service_touch_effect(const char *pkgid, int size_type)
940 ErrPrint("Unable to open a DB\n");
944 ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
945 if (ret != SQLITE_OK) {
946 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
953 * This function will validate the "pkgid"
954 * call the exported API in the exported API is not recomended
957 lbid = livebox_service_pkgname(pkgid);
959 ErrPrint("Invalid appid (%s)\n", pkgid);
964 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
966 if (ret != SQLITE_OK) {
967 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
972 ret = sqlite3_bind_int(stmt, 2, size_type);
973 if (ret != SQLITE_OK) {
974 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
979 ret = sqlite3_step(stmt);
980 if (ret == SQLITE_ROW) {
981 ret = !!sqlite3_column_int(stmt, 0);
983 ret = 1; /*!< Default true: In this case the DB is corrupted. */
984 ErrPrint("There is no result\n");
989 sqlite3_finalize(stmt);
994 EAPI int livebox_service_mouse_event(const char *pkgid)
1005 ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
1006 if (ret != SQLITE_OK) {
1007 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1012 lbid = livebox_service_pkgname(pkgid);
1014 ErrPrint("Failed to get lbid: %s\n", pkgid);
1019 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1021 if (ret != SQLITE_OK) {
1022 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1027 ret = sqlite3_step(stmt);
1028 if (ret == SQLITE_ROW) {
1029 ret = !!sqlite3_column_int(stmt, 0);
1031 ret = 0; /*!< Default is false, In this case the DB is corrupted */
1032 ErrPrint("There is no result.\n");
1036 sqlite3_reset(stmt);
1037 sqlite3_finalize(stmt);
1042 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
1047 char *preview = NULL;
1053 ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
1054 if (ret != SQLITE_OK) {
1055 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1060 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1061 if (ret != SQLITE_OK) {
1062 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1066 ret = sqlite3_bind_int(stmt, 2, size_type);
1067 if (ret != SQLITE_OK) {
1068 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1072 ret = sqlite3_step(stmt);
1073 if (ret == SQLITE_ROW) {
1075 tmp = (const char *)sqlite3_column_text(stmt, 0);
1076 if (tmp && strlen(tmp)) {
1077 preview = strdup(tmp);
1079 ErrPrint("Heap: %s\n", strerror(errno));
1084 sqlite3_reset(stmt);
1085 sqlite3_finalize(stmt);
1090 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1099 language = strdup(lang);
1101 ErrPrint("Heap: %s\n", strerror(errno));
1105 language = cur_locale();
1116 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1117 if (ret != SQLITE_OK) {
1118 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1124 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1125 if (ret != SQLITE_OK) {
1126 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1130 ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1131 if (ret != SQLITE_OK) {
1132 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1136 ret = sqlite3_step(stmt);
1137 if (ret == SQLITE_ROW) {
1139 tmp = (const char *)sqlite3_column_text(stmt, 0);
1140 if (!tmp || !strlen(tmp)) {
1141 icon = get_default_icon(pkgid);
1145 ErrPrint("Heap: %s\n", strerror(errno));
1148 icon = get_default_icon(pkgid);
1152 sqlite3_reset(stmt);
1153 sqlite3_finalize(stmt);
1159 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1168 language = strdup(lang);
1170 ErrPrint("Error: %s\n", strerror(errno));
1174 language = cur_locale();
1185 ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1186 if (ret != SQLITE_OK) {
1187 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1193 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1194 if (ret != SQLITE_OK) {
1195 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1199 ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1200 if (ret != SQLITE_OK) {
1201 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1205 ret = sqlite3_step(stmt);
1206 if (ret == SQLITE_ROW) {
1208 tmp = (const char *)sqlite3_column_text(stmt, 0);
1209 if (!tmp || !strlen(tmp)) {
1210 name = get_default_name(pkgid);
1214 ErrPrint("Heap: %s\n", strerror(errno));
1217 name = get_default_name(pkgid);
1221 sqlite3_reset(stmt);
1222 sqlite3_finalize(stmt);
1228 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1235 if (!w || !h || !cnt || !pkgid)
1236 return LB_STATUS_ERROR_INVALID;
1240 return LB_STATUS_ERROR_IO;
1242 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1243 if (ret != SQLITE_OK) {
1244 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1245 ret = LB_STATUS_ERROR_IO;
1249 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1250 if (ret != SQLITE_OK) {
1251 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1252 sqlite3_reset(stmt);
1253 sqlite3_finalize(stmt);
1254 ret = LB_STATUS_ERROR_IO;
1258 if (*cnt > NR_OF_SIZE_LIST)
1259 *cnt = NR_OF_SIZE_LIST;
1262 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1263 size = sqlite3_column_int(stmt, 0);
1264 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1268 sqlite3_reset(stmt);
1269 sqlite3_finalize(stmt);
1276 EAPI char *livebox_service_libexec(const char *pkgid)
1293 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1294 if (ret != SQLITE_OK) {
1295 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1299 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1300 if (ret != SQLITE_OK) {
1301 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1302 sqlite3_finalize(stmt);
1306 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1307 if (ret != SQLITE_OK) {
1308 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1309 sqlite3_finalize(stmt);
1313 if (sqlite3_step(stmt) != SQLITE_ROW) {
1314 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1315 sqlite3_reset(stmt);
1316 sqlite3_finalize(stmt);
1318 libexec = util_conf_get_libexec(pkgid);
1319 DbgPrint("Fallback to conf checker: %s\n", libexec);
1323 appid = (char *)sqlite3_column_text(stmt, 0);
1324 if (!appid || !strlen(appid)) {
1325 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1326 sqlite3_reset(stmt);
1327 sqlite3_finalize(stmt);
1331 path = (char *)sqlite3_column_text(stmt, 1);
1332 if (!path || !strlen(path)) {
1333 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1334 sqlite3_reset(stmt);
1335 sqlite3_finalize(stmt);
1339 libexec = strdup(path);
1341 ErrPrint("Heap: %s\n", strerror(errno));
1342 sqlite3_reset(stmt);
1343 sqlite3_finalize(stmt);
1347 DbgPrint("libexec: %s\n", libexec);
1349 sqlite3_reset(stmt);
1350 sqlite3_finalize(stmt);
1356 EAPI char *livebox_service_pkgname(const char *appid)
1359 pkgmgr_appinfo_h handle;
1366 lb_pkgname = get_lb_pkgname_by_appid(appid);
1372 * Try to get the package id using given appid
1374 ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1375 if (ret != PKGMGR_R_OK) {
1376 ErrPrint("Failed to get appinfo\n");
1380 ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1381 if (ret != PKGMGR_R_OK) {
1382 pkgmgr_appinfo_destroy_appinfo(handle);
1383 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1387 lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1388 pkgmgr_appinfo_destroy_appinfo(handle);
1390 if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1391 return strdup(appid);
1396 EAPI char *livebox_service_provider_name(const char *lbid)
1402 char *str = SAMSUNG_PREFIX;
1407 while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1409 if (seq < 2 && lbid[idx] == '.') {
1415 if (!str[idx] && lbid[idx]) {
1417 return strdup(lbid);
1418 } else if (seq < 2) {
1420 if (lbid[idx] == '.') {
1422 } else if (!lbid[idx]) {
1423 ErrPrint("Invalid lbid: %s\n", lbid);
1435 ret = strdup(lbid + stage);
1437 ErrPrint("Error: %s\n", strerror(errno));
1444 EAPI int livebox_service_is_enabled(const char *lbid)
1453 pkgname = livebox_service_appid(lbid);
1457 ret = ail_get_appinfo(pkgname, &ai);
1458 if (ret != AIL_ERROR_OK) {
1463 if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
1466 ail_destroy_appinfo(ai);
1468 return enabled == true;
1472 EAPI int livebox_service_is_primary(const char *lbid)
1485 ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
1486 if (ret != SQLITE_OK) {
1487 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1492 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1493 if (ret != SQLITE_OK) {
1494 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1498 ret = sqlite3_step(stmt);
1499 if (ret != SQLITE_ROW) {
1500 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1504 ret = sqlite3_column_int(stmt, 0);
1507 sqlite3_reset(stmt);
1508 sqlite3_finalize(stmt);
1514 * appid == Package ID
1515 * pkgid == Livebox ID
1517 EAPI char *livebox_service_appid(const char *pkgname)
1523 int is_prime __attribute__((__unused__));
1534 ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1535 if (ret != SQLITE_OK) {
1536 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1540 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
1541 if (ret != SQLITE_OK) {
1542 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1543 sqlite3_reset(stmt);
1544 sqlite3_finalize(stmt);
1548 ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
1549 if (ret != SQLITE_OK) {
1550 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1551 sqlite3_reset(stmt);
1552 sqlite3_finalize(stmt);
1556 ret = sqlite3_step(stmt);
1557 if (ret != SQLITE_ROW) {
1558 pkgmgr_appinfo_h pkg_handle;
1561 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1562 sqlite3_reset(stmt);
1563 sqlite3_finalize(stmt);
1565 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1566 if (ret != PKGMGR_R_OK) {
1567 ErrPrint("Failed to get appinfo: %s\n", pkgname);
1571 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1572 if (ret != PKGMGR_R_OK) {
1573 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1574 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1578 appid = strdup(new_appid);
1580 ErrPrint("Heap: %s\n", strerror(errno));
1582 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1586 tmp = (char *)sqlite3_column_text(stmt, 0);
1587 if (!tmp || !strlen(tmp)) {
1588 ErrPrint("APPID is NIL\n");
1589 sqlite3_reset(stmt);
1590 sqlite3_finalize(stmt);
1594 appid = strdup(tmp);
1596 ErrPrint("Heap: %s\n", strerror(errno));
1597 sqlite3_reset(stmt);
1598 sqlite3_finalize(stmt);
1602 is_prime = sqlite3_column_int(stmt, 1);
1604 sqlite3_reset(stmt);
1605 sqlite3_finalize(stmt);
1611 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1628 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1629 if (ret != SQLITE_OK) {
1630 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1634 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1635 if (ret != SQLITE_OK) {
1636 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1637 sqlite3_finalize(stmt);
1641 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1642 if (ret != SQLITE_OK) {
1643 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1644 sqlite3_finalize(stmt);
1648 ret = sqlite3_step(stmt);
1649 if (ret != SQLITE_ROW) {
1650 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1651 sqlite3_reset(stmt);
1652 sqlite3_finalize(stmt);
1656 appid = (char *)sqlite3_column_text(stmt, 0);
1657 if (!appid || !strlen(appid)) {
1658 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1659 sqlite3_reset(stmt);
1660 sqlite3_finalize(stmt);
1664 lb_src = (char *)sqlite3_column_text(stmt, 1);
1665 if (!lb_src || !strlen(lb_src)) {
1666 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1667 sqlite3_reset(stmt);
1668 sqlite3_finalize(stmt);
1672 path = strdup(lb_src);
1674 ErrPrint("Heap: %s\n", strerror(errno));
1675 sqlite3_reset(stmt);
1676 sqlite3_finalize(stmt);
1680 DbgPrint("LB Src: %s\n", path);
1682 sqlite3_reset(stmt);
1683 sqlite3_finalize(stmt);
1689 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1705 ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1706 if (ret != SQLITE_OK) {
1707 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1711 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1712 if (ret != SQLITE_OK) {
1713 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1714 sqlite3_finalize(stmt);
1718 ret = sqlite3_step(stmt);
1719 if (ret != SQLITE_ROW) {
1720 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1721 sqlite3_reset(stmt);
1722 sqlite3_finalize(stmt);
1726 tmp = (char *)sqlite3_column_text(stmt, 0);
1727 if (tmp && strlen(tmp)) {
1728 group = strdup(tmp);
1730 ErrPrint("Heap: %s\n", strerror(errno));
1733 sqlite3_reset(stmt);
1734 sqlite3_finalize(stmt);
1740 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1757 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1758 if (ret != SQLITE_OK) {
1759 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1763 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1764 if (ret != SQLITE_OK) {
1765 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1766 sqlite3_finalize(stmt);
1770 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1771 if (ret != SQLITE_OK) {
1772 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1773 sqlite3_finalize(stmt);
1777 ret = sqlite3_step(stmt);
1778 if (ret != SQLITE_ROW) {
1779 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1780 sqlite3_reset(stmt);
1781 sqlite3_finalize(stmt);
1785 appid = (char *)sqlite3_column_text(stmt, 0);
1786 if (!appid || !strlen(appid)) {
1787 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1788 sqlite3_reset(stmt);
1789 sqlite3_finalize(stmt);
1793 pd_src = (char *)sqlite3_column_text(stmt, 1);
1794 if (!pd_src || !strlen(pd_src)) {
1795 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1796 sqlite3_reset(stmt);
1797 sqlite3_finalize(stmt);
1801 path = strdup(pd_src);
1803 ErrPrint("Heap: %s\n", strerror(errno));
1804 sqlite3_reset(stmt);
1805 sqlite3_finalize(stmt);
1809 DbgPrint("PD Src: %s\n", path);
1810 sqlite3_reset(stmt);
1811 sqlite3_finalize(stmt);
1817 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1833 ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1834 if (ret != SQLITE_OK) {
1835 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1839 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1840 if (ret != SQLITE_OK) {
1841 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1842 sqlite3_finalize(stmt);
1846 ret = sqlite3_step(stmt);
1847 if (ret != SQLITE_ROW) {
1848 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1849 sqlite3_reset(stmt);
1850 sqlite3_finalize(stmt);
1854 tmp = (char *)sqlite3_column_text(stmt, 0);
1855 if (tmp && strlen(tmp)) {
1856 group = strdup(tmp);
1858 ErrPrint("Heap: %s\n", strerror(errno));
1860 sqlite3_reset(stmt);
1861 sqlite3_finalize(stmt);
1867 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1871 const char *cluster;
1876 return LB_STATUS_ERROR_INVALID;
1880 return LB_STATUS_ERROR_IO;
1883 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1884 if (ret != SQLITE_OK) {
1885 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1886 cnt = LB_STATUS_ERROR_IO;
1890 while (sqlite3_step(stmt) == SQLITE_ROW) {
1891 cluster = (const char *)sqlite3_column_text(stmt, 0);
1892 if (!cluster || !strlen(cluster))
1895 if (cb(cluster, data) < 0)
1901 sqlite3_reset(stmt);
1902 sqlite3_finalize(stmt);
1908 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1912 const char *category;
1916 if (!cluster || !cb)
1917 return LB_STATUS_ERROR_INVALID;
1921 return LB_STATUS_ERROR_IO;
1923 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1924 if (ret != SQLITE_OK) {
1925 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1926 cnt = LB_STATUS_ERROR_IO;
1931 while (sqlite3_step(stmt) == SQLITE_ROW) {
1932 category = (const char *)sqlite3_column_text(stmt, 0);
1933 if (!category || !strlen(category))
1936 if (cb(cluster, category, data) < 0)
1942 sqlite3_reset(stmt);
1943 sqlite3_finalize(stmt);
1949 EAPI int livebox_service_init(void)
1951 if (s_info.handle) {
1952 DbgPrint("Already initialized\n");
1953 s_info.init_count++;
1957 s_info.handle = open_db();
1958 if (s_info.handle) {
1959 s_info.init_count++;
1963 return LB_STATUS_ERROR_IO;
1966 EAPI int livebox_service_fini(void)
1968 if (!s_info.handle || s_info.init_count <= 0) {
1969 ErrPrint("Service is not initialized\n");
1970 return LB_STATUS_ERROR_IO;
1973 s_info.init_count--;
1974 if (s_info.init_count > 0) {
1975 DbgPrint("Init count %d\n", s_info.init_count);
1979 db_util_close(s_info.handle);
1980 s_info.handle = NULL;
1984 EAPI int livebox_service_get_size(int type, int *width, int *height)
1995 return convert_size_from_type(type, width, height);
1998 EAPI int livebox_service_size_type(int width, int height)
2002 if (update_resolution() < 0)
2003 ErrPrint("Failed to update the size list\n");
2005 for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
2006 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
2012 return LB_SIZE_TYPE_1x1;
2014 return LB_SIZE_TYPE_2x1;
2016 return LB_SIZE_TYPE_2x2;
2018 return LB_SIZE_TYPE_4x1;
2020 return LB_SIZE_TYPE_4x2;
2022 return LB_SIZE_TYPE_4x3;
2024 return LB_SIZE_TYPE_4x4;
2026 return LB_SIZE_TYPE_4x5;
2028 return LB_SIZE_TYPE_4x6;
2030 return LB_SIZE_TYPE_EASY_1x1;
2032 return LB_SIZE_TYPE_EASY_3x1;
2034 return LB_SIZE_TYPE_EASY_3x3;
2036 return LB_SIZE_TYPE_0x0;
2041 return LB_SIZE_TYPE_UNKNOWN;