2 * Copyright 2012 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://www.tizenopensource.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"
43 #define SAMSUNG_PREFIX "com.samsung."
44 #define EAPI __attribute__((visibility("default")))
45 #define DEFAULT_TIMEOUT 2.0
48 static struct supported_size_list {
51 } SIZE_LIST[NR_OF_SIZE_LIST] = {
52 { 172, 172 }, /*!< 1x1 */
53 { 348, 172 }, /*!< 2x1 */
54 { 348, 348 }, /*!< 2x2 */
55 { 700, 172 }, /*!< 4x1 */
56 { 700, 348 }, /*!< 4x2 */
57 { 700, 520 }, /*!< 4x3 */
58 { 700, 700 }, /*!< 4x4 */
59 { 207, 207 }, /*!< 21x21 */
60 { 645, 207 }, /*!< 23x21 */
61 { 645, 645 }, /*!< 23x23 */
67 const char *conf_file;
72 .dbfile = "/opt/dbspace/.livebox.db",
73 .conf_file = "/usr/share/data-provider-master/resolution.ini",
78 static inline int update_info(int width_type, int height_type, int width, int height)
82 if (width_type == 1 && height_type == 1) {
83 DbgPrint("1x1 Updated to %dx%d\n", width, height);
85 } else if (width_type == 2 && height_type == 1) {
86 DbgPrint("2x1 Updated to %dx%d\n", width, height);
88 } else if (width_type == 2 && height_type == 2) {
89 DbgPrint("2x2 Updated to %dx%d\n", width, height);
91 } else if (width_type == 4 && height_type == 1) {
92 DbgPrint("4x1 Updated to %dx%d\n", width, height);
94 } else if (width_type == 4 && height_type == 2) {
95 DbgPrint("4x2 Updated to %dx%d\n", width, height);
97 } else if (width_type == 4 && height_type == 3) {
98 DbgPrint("4x3 Updated to %dx%d\n", width, height);
100 } else if (width_type == 4 && height_type == 4) {
101 DbgPrint("4x4 Updated to %dx%d\n", width, height);
103 } else if (width_type == 21 && height_type == 21) {
104 DbgPrint("Easy 1x1 Updated to %dx%d\n", width, height);
106 } else if (width_type == 23 && height_type == 21) {
107 DbgPrint("Easy 3x1 Updated to %dx%d\n", width, height);
109 } else if (width_type == 23 && height_type == 23) {
110 DbgPrint("Easy 3x3 Updated to %dx%d\n", width, height);
113 ErrPrint("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
117 SIZE_LIST[idx].w = width;
118 SIZE_LIST[idx].h = height;
122 static inline int update_from_file(void)
130 char buffer[MAX_COLUMN];
144 fp = fopen(s_info.conf_file, "r");
146 ErrPrint("Open failed: %s\n", strerror(errno));
156 if (idx == MAX_COLUMN) {
157 ErrPrint("Buffer overflow. Too long line. LINE MUST BE SHOT THAN %d\n", MAX_COLUMN);
163 if (isspace(ch) || ch == EOF)
178 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
179 ErrPrint("Invalid syntax: [%s]\n", buffer);
183 } else if (ch == '=') {
186 if (sscanf(buffer, "%dx%d", &width_type, &height_type) != 2) {
187 ErrPrint("Invalid syntax: [%s]\n", buffer);
191 } else if (ch == EOF) {
192 ErrPrint("Invalid Syntax\n");
203 if (isspace(ch) || ch == EOF)
211 if (isspace(ch) || ch == EOF) {
215 if (sscanf(buffer, "%dx%d", &width, &height) != 2) {
216 ErrPrint("Invalid syntax: [%s]\n", buffer);
218 } else if (ch == EOF) {
219 updated += update_info(width_type, height_type, width, height);
226 updated += update_info(width_type, height_type, width, height);
231 if (ch == '\n' || ch == '\r' || ch == '\f')
235 if (ch == '\n' || ch == '\r' || ch == '\f')
239 ErrPrint("Unknown status. couldn't be reach to here\n");
245 return NR_OF_SIZE_LIST - updated;
248 static int update_resolution(void)
260 if (s_info.res_resolved)
263 disp = XOpenDisplay(NULL);
265 ErrPrint("Failed to open a display\n");
269 root = XDefaultRootWindow(disp);
270 if (!XGetGeometry(disp, root, &dummy, &x, &y, &width, &height, &border, &depth)) {
275 if (update_from_file() == 0)
276 DbgPrint("Resolution info is all updated by file\n");
278 DbgPrint("Screen resolution: %dx%d\n", width, height);
279 for (i = 0; i < NR_OF_SIZE_LIST; i++) {
280 SIZE_LIST[i].w = (unsigned int)((double)SIZE_LIST[i].w * (double)width / 720.0f);
281 SIZE_LIST[i].h = (unsigned int)((double)SIZE_LIST[i].h * (double)width / 720.0f);
282 DbgPrint("(Ratio)Size is updated [%d] %dx%d\n", i, SIZE_LIST[i].w, SIZE_LIST[i].h);
286 s_info.res_resolved = 1;
290 static inline sqlite3 *open_db(void)
294 if (!s_info.handle) {
297 ret = db_util_open(s_info.dbfile, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
298 if (ret != SQLITE_OK) {
299 ErrPrint("Failed to open a DB\n");
303 handle = s_info.handle;
309 static inline void close_db(sqlite3 *handle)
312 db_util_close(handle);
315 static inline int convert_size_from_type(enum livebox_size_type type, int *width, int *height)
320 case LB_SIZE_TYPE_1x1: /*!< 172x172 */
323 case LB_SIZE_TYPE_2x1: /*!< 348x172 */
326 case LB_SIZE_TYPE_2x2: /*!< 348x348 */
329 case LB_SIZE_TYPE_4x1: /*!< 700x172 */
332 case LB_SIZE_TYPE_4x2: /*!< 700x348 */
335 case LB_SIZE_TYPE_4x3: /*!< 700x520 */
338 case LB_SIZE_TYPE_4x4: /*!< 700x700 */
341 case LB_SIZE_TYPE_EASY_1x1: /*< 207x207 */
344 case LB_SIZE_TYPE_EASY_3x1: /*!< 645x207 */
347 case LB_SIZE_TYPE_EASY_3x3: /*!< 645x645 */
354 if (update_resolution() < 0)
355 ErrPrint("Failed to update resolution\n");
357 *width = SIZE_LIST[idx].w;
358 *height = SIZE_LIST[idx].h;
362 EAPI int livebox_service_change_period(const char *pkgname, const char *id, double period)
364 struct packet *packet;
365 struct packet *result;
369 if (!pkgname || !id || period < 0.0f) {
370 ErrPrint("Invalid argument\n");
374 uri = util_id_to_uri(id);
378 packet = packet_create("service_change_period", "ssd", pkgname, uri, period);
381 ErrPrint("Failed to create a packet for period changing\n");
385 result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
386 packet_unref(packet);
389 if (packet_get(result, "i", &ret) != 1) {
390 ErrPrint("Failed to parse a result packet\n");
393 packet_unref(result);
395 ErrPrint("Failed to get result packet\n");
402 EAPI int livebox_service_trigger_update(const char *pkgname, const char *id, const char *cluster, const char *category, int force)
404 struct packet *packet;
405 struct packet *result;
410 ErrPrint("Invalid argument\n");
414 if (!force && access("/tmp/.live.paused", R_OK) == 0) {
415 DbgPrint("Provider is paused\n");
419 uri = util_id_to_uri(id);
424 cluster = "user,created";
427 category = "default";
429 packet = packet_create("service_update", "ssss", pkgname, uri, cluster, category);
432 ErrPrint("Failed to create a packet for service_update\n");
436 result = com_core_packet_oneshot_send(SERVICE_SOCKET, packet, DEFAULT_TIMEOUT);
437 packet_unref(packet);
440 if (packet_get(result, "i", &ret) != 1) {
441 ErrPrint("Failed to parse a result packet\n");
445 packet_unref(result);
447 ErrPrint("Failed to get result packet\n");
454 EAPI int livebox_service_get_pkglist(int (*cb)(const char *appid, const char *pkgname, int is_prime, void *data), void *data)
470 ret = sqlite3_prepare_v2(handle, "SELECT appid, pkgid, prime FROM pkgmap", -1, &stmt, NULL);
471 if (ret != SQLITE_OK) {
472 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
478 while (sqlite3_step(stmt) == SQLITE_ROW) {
479 appid = (char *)sqlite3_column_text(stmt, 0);
480 if (!appid || !strlen(appid)) {
481 ErrPrint("APPID is not valid\n");
485 pkgid = (char *)sqlite3_column_text(stmt, 1);
486 if (!pkgid || !strlen(pkgid)) {
487 ErrPrint("pkgid is not valid\n");
491 is_prime = sqlite3_column_int(stmt, 2);
495 if (cb(appid, pkgid, is_prime, data) < 0) {
496 DbgPrint("Callback stopped package crawling\n");
502 sqlite3_finalize(stmt);
509 EAPI int livebox_service_get_supported_size_types(const char *pkgid, int *cnt, int *types)
516 if (!types || !cnt || !pkgid)
523 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
524 if (ret != SQLITE_OK) {
525 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
530 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
531 if (ret != SQLITE_OK) {
532 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
534 sqlite3_finalize(stmt);
539 if (*cnt > NR_OF_SIZE_LIST)
540 *cnt = NR_OF_SIZE_LIST;
543 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
544 size = sqlite3_column_int(stmt, 0);
551 sqlite3_finalize(stmt);
558 static inline char *cur_locale(void)
561 language = vconf_get_str(VCONFKEY_LANGSET);
578 language = strdup("en-us");
580 ErrPrint("Heap: %s\n", strerror(errno));
586 static inline char *get_default_name(const char *pkgid)
597 ret = sqlite3_prepare_v2(handle, "SELECT name FROM client WHERE pkgid = ?", -1, &stmt, NULL);
598 if (ret != SQLITE_OK) {
599 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
604 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
605 if (ret != SQLITE_OK) {
606 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
610 ret = sqlite3_step(stmt);
611 if (ret == SQLITE_ROW) {
614 tmp = (const char *)sqlite3_column_text(stmt, 0);
615 if (tmp && strlen(tmp)) {
618 ErrPrint("Heap: %s\n", strerror(errno));
624 sqlite3_finalize(stmt);
629 static inline char *get_default_icon(const char *pkgid)
640 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM client WHERE pkgid = ?", -1, &stmt, NULL);
641 if (ret != SQLITE_OK) {
642 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
647 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
648 if (ret != SQLITE_OK) {
649 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
653 ret = sqlite3_step(stmt);
654 if (ret == SQLITE_ROW) {
657 tmp = (const char *)sqlite3_column_text(stmt, 0);
658 if (tmp && strlen(tmp)) {
661 ErrPrint("Heap: %s\n", strerror(errno));
667 sqlite3_finalize(stmt);
672 EAPI char *livebox_service_content(const char *pkgid)
676 char *content = NULL;
683 ret = sqlite3_prepare_v2(handle, "SELECT content FROM client WHERE pkgid = ?", -1, &stmt, NULL);
684 if (ret != SQLITE_OK) {
685 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
690 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
691 if (ret != SQLITE_OK) {
692 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
696 ret = sqlite3_step(stmt);
697 if (ret == SQLITE_ROW) {
700 tmp = (const char *)sqlite3_column_text(stmt, 0);
701 if (tmp && strlen(tmp)) {
702 content = strdup(tmp);
704 ErrPrint("Heap: %s\n", strerror(errno));
710 sqlite3_finalize(stmt);
715 EAPI char *livebox_service_setup_appid(const char *lbid)
726 ret = sqlite3_prepare_v2(handle, "SELECT setup FROM client WHERE pkgid = ?", -1, &stmt, NULL);
727 if (ret != SQLITE_OK) {
728 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
734 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
735 if (ret != SQLITE_OK) {
736 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
740 ret = sqlite3_step(stmt);
741 if (ret == SQLITE_ROW) {
744 tmp = (const char *)sqlite3_column_text(stmt, 0);
745 if (!tmp || !strlen(tmp))
750 ErrPrint("Error: %s\n", strerror(errno));
755 sqlite3_finalize(stmt);
760 EAPI int livebox_service_nodisplay(const char *pkgid)
770 ret = sqlite3_prepare_v2(handle, "SELECT nodisplay FROM client WHERE pkgid = ?", -1, &stmt, NULL);
771 if (ret != SQLITE_OK) {
772 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
777 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
778 if (ret != SQLITE_OK) {
779 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
784 ret = sqlite3_step(stmt);
785 if (ret == SQLITE_ROW)
786 ret = !!sqlite3_column_int(stmt, 0);
792 sqlite3_finalize(stmt);
797 static inline char *get_lb_pkgname_by_appid(const char *appid)
813 ret = sqlite3_prepare_v2(handle, "SELECT pkgid FROM pkgmap WHERE (appid = ? AND prime = 1) OR pkgid = ?", -1, &stmt, NULL);
814 if (ret != SQLITE_OK) {
815 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
820 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
821 if (ret != SQLITE_OK) {
822 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
826 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
827 if (ret != SQLITE_OK) {
828 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
832 if (sqlite3_step(stmt) != SQLITE_ROW) {
833 ErrPrint("Error: %s (has no record? - %s)\n", sqlite3_errmsg(handle), appid);
837 tmp = (char *)sqlite3_column_text(stmt, 0);
838 if (tmp && strlen(tmp)) {
841 ErrPrint("Heap: %s\n", strerror(errno));
846 sqlite3_finalize(stmt);
851 EAPI int livebox_service_touch_effect(const char *pkgid)
860 ErrPrint("Unable to open a DB\n");
864 ret = sqlite3_prepare_v2(handle, "SELECT touch_effect FROM client WHERE pkgid = ?", -1, &stmt, NULL);
865 if (ret != SQLITE_OK) {
866 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
873 * This function will validate the "pkgid"
874 * call the exported API in the exported API is not recomended
877 lbid = livebox_service_pkgname(pkgid);
879 ErrPrint("Invalid appid (%s)\n", pkgid);
884 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
886 if (ret != SQLITE_OK) {
887 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
892 ret = sqlite3_step(stmt);
893 if (ret == SQLITE_ROW) {
894 ret = !!sqlite3_column_int(stmt, 0);
896 ret = 1; /*!< Default true: In this case the DB is corrupted. */
897 ErrPrint("There is no result\n");
902 sqlite3_finalize(stmt);
907 EAPI int livebox_service_mouse_event(const char *pkgid)
918 ret = sqlite3_prepare_v2(handle, "SELECT mouse_event FROM client WHERE pkgid = ?", -1, &stmt, NULL);
919 if (ret != SQLITE_OK) {
920 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
925 lbid = livebox_service_pkgname(pkgid);
927 ErrPrint("Failed to get lbid: %s\n", pkgid);
932 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
934 if (ret != SQLITE_OK) {
935 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
940 ret = sqlite3_step(stmt);
941 if (ret == SQLITE_ROW) {
942 ret = !!sqlite3_column_int(stmt, 0);
944 ret = 0; /*!< Default is false, In this case the DB is corrupted */
945 ErrPrint("There is no result.\n");
950 sqlite3_finalize(stmt);
955 EAPI char *livebox_service_preview(const char *pkgid, int size_type)
960 char *preview = NULL;
966 ret = sqlite3_prepare_v2(handle, "SELECT preview FROM box_size WHERE pkgid = ? AND size_type = ?", -1, &stmt, NULL);
967 if (ret != SQLITE_OK) {
968 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
973 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
974 if (ret != SQLITE_OK) {
975 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
979 ret = sqlite3_bind_int(stmt, 2, size_type);
980 if (ret != SQLITE_OK) {
981 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
985 ret = sqlite3_step(stmt);
986 if (ret == SQLITE_ROW) {
988 tmp = (const char *)sqlite3_column_text(stmt, 0);
989 if (tmp && strlen(tmp)) {
990 preview = strdup(tmp);
992 ErrPrint("Heap: %s\n", strerror(errno));
998 sqlite3_finalize(stmt);
1003 EAPI char *livebox_service_i18n_icon(const char *pkgid, const char *lang)
1012 language = strdup(lang);
1014 ErrPrint("Heap: %s\n", strerror(errno));
1018 language = cur_locale();
1029 ret = sqlite3_prepare_v2(handle, "SELECT icon FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1030 if (ret != SQLITE_OK) {
1031 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1037 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1038 if (ret != SQLITE_OK) {
1039 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1043 ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1044 if (ret != SQLITE_OK) {
1045 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1049 ret = sqlite3_step(stmt);
1050 if (ret == SQLITE_ROW) {
1052 tmp = (const char *)sqlite3_column_text(stmt, 0);
1053 if (!tmp || !strlen(tmp)) {
1054 icon = get_default_icon(pkgid);
1058 ErrPrint("Heap: %s\n", strerror(errno));
1061 icon = get_default_icon(pkgid);
1065 sqlite3_reset(stmt);
1066 sqlite3_finalize(stmt);
1072 EAPI char *livebox_service_i18n_name(const char *pkgid, const char *lang)
1081 language = strdup(lang);
1083 ErrPrint("Error: %s\n", strerror(errno));
1087 language = cur_locale();
1098 ret = sqlite3_prepare_v2(handle, "SELECT name FROM i18n WHERE pkgid = ? AND lang = ?", -1, &stmt, NULL);
1099 if (ret != SQLITE_OK) {
1100 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1106 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1107 if (ret != SQLITE_OK) {
1108 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1112 ret = sqlite3_bind_text(stmt, 2, language, -1, SQLITE_TRANSIENT);
1113 if (ret != SQLITE_OK) {
1114 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1118 ret = sqlite3_step(stmt);
1119 if (ret == SQLITE_ROW) {
1121 tmp = (const char *)sqlite3_column_text(stmt, 0);
1122 if (!tmp || !strlen(tmp)) {
1123 name = get_default_name(pkgid);
1127 ErrPrint("Heap: %s\n", strerror(errno));
1130 name = get_default_name(pkgid);
1134 sqlite3_reset(stmt);
1135 sqlite3_finalize(stmt);
1141 EAPI int livebox_service_get_supported_sizes(const char *pkgid, int *cnt, int *w, int *h)
1148 if (!w || !h || !cnt || !pkgid)
1155 ret = sqlite3_prepare_v2(handle, "SELECT size_type FROM box_size WHERE pkgid = ? ORDER BY size_type ASC", -1, &stmt, NULL);
1156 if (ret != SQLITE_OK) {
1157 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1162 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1163 if (ret != SQLITE_OK) {
1164 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1165 sqlite3_reset(stmt);
1166 sqlite3_finalize(stmt);
1171 if (*cnt > NR_OF_SIZE_LIST)
1172 *cnt = NR_OF_SIZE_LIST;
1175 while (sqlite3_step(stmt) == SQLITE_ROW && ret < *cnt) {
1176 size = sqlite3_column_int(stmt, 0);
1177 ret += (convert_size_from_type(size, w + ret, h + ret) == 0);
1181 sqlite3_reset(stmt);
1182 sqlite3_finalize(stmt);
1189 EAPI char *livebox_service_libexec(const char *pkgid)
1206 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.libexec FROM pkgmap, provider WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1207 if (ret != SQLITE_OK) {
1208 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1212 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1213 if (ret != SQLITE_OK) {
1214 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1215 sqlite3_finalize(stmt);
1219 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1220 if (ret != SQLITE_OK) {
1221 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1222 sqlite3_finalize(stmt);
1226 if (sqlite3_step(stmt) != SQLITE_ROW) {
1227 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1228 sqlite3_reset(stmt);
1229 sqlite3_finalize(stmt);
1231 libexec = util_conf_get_libexec(pkgid);
1232 DbgPrint("Fallback to conf checker: %s\n", libexec);
1236 appid = (char *)sqlite3_column_text(stmt, 0);
1237 if (!appid || !strlen(appid)) {
1238 ErrPrint("Invalid appid: %s\n", sqlite3_errmsg(handle));
1239 sqlite3_reset(stmt);
1240 sqlite3_finalize(stmt);
1244 path = (char *)sqlite3_column_text(stmt, 1);
1245 if (!path || !strlen(path)) {
1246 ErrPrint("Invalid libexec: %s\n", sqlite3_errmsg(handle));
1247 sqlite3_reset(stmt);
1248 sqlite3_finalize(stmt);
1252 libexec = strdup(path);
1254 ErrPrint("Heap: %s\n", strerror(errno));
1255 sqlite3_reset(stmt);
1256 sqlite3_finalize(stmt);
1260 DbgPrint("libexec: %s\n", libexec);
1262 sqlite3_reset(stmt);
1263 sqlite3_finalize(stmt);
1269 EAPI char *livebox_service_pkgname(const char *appid)
1272 pkgmgr_appinfo_h handle;
1279 lb_pkgname = get_lb_pkgname_by_appid(appid);
1285 * Try to get the package id using given appid
1287 ret = pkgmgr_appinfo_get_appinfo(appid, &handle);
1288 if (ret != PKGMGR_R_OK) {
1289 ErrPrint("Failed to get appinfo\n");
1293 ret = pkgmgr_appinfo_get_pkgname(handle, &new_appid);
1294 if (ret != PKGMGR_R_OK) {
1295 pkgmgr_appinfo_destroy_appinfo(handle);
1296 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1300 lb_pkgname = get_lb_pkgname_by_appid(new_appid);
1301 pkgmgr_appinfo_destroy_appinfo(handle);
1303 if (!lb_pkgname && util_validate_livebox_package(appid) == 0)
1304 return strdup(appid);
1309 EAPI char *livebox_service_provider_name(const char *lbid)
1315 char *str = SAMSUNG_PREFIX;
1320 while (str[idx] && lbid[idx] && lbid[idx] == str[idx]) {
1322 if (seq < 2 && lbid[idx] == '.') {
1328 if (!str[idx] && lbid[idx]) {
1330 return strdup(lbid);
1331 } else if (seq < 2) {
1333 if (lbid[idx] == '.') {
1335 } else if (!lbid[idx]) {
1336 ErrPrint("Invalid lbid: %s\n", lbid);
1348 ret = strdup(lbid + stage);
1350 ErrPrint("Error: %s\n", strerror(errno));
1357 EAPI int livebox_service_is_enabled(const char *lbid)
1366 pkgname = livebox_service_appid(lbid);
1370 ret = ail_get_appinfo(pkgname, &ai);
1371 if (ret != AIL_ERROR_OK) {
1376 if (ail_appinfo_get_bool(ai, AIL_PROP_X_SLP_ENABLED_BOOL, &enabled) != AIL_ERROR_OK)
1379 ail_destroy_appinfo(ai);
1381 return enabled == true;
1385 EAPI int livebox_service_is_primary(const char *lbid)
1398 ret = sqlite3_prepare_v2(handle, "SELECT prime FROM pkgmap WHERE pkgid = ?", -1, &stmt, NULL);
1399 if (ret != SQLITE_OK) {
1400 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1405 ret = sqlite3_bind_text(stmt, 1, lbid, -1, SQLITE_TRANSIENT);
1406 if (ret != SQLITE_OK) {
1407 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1411 ret = sqlite3_step(stmt);
1412 if (ret != SQLITE_ROW) {
1413 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1417 ret = sqlite3_column_int(stmt, 1);
1420 sqlite3_reset(stmt);
1421 sqlite3_finalize(stmt);
1427 * appid == Package ID
1428 * pkgid == Livebox ID
1430 EAPI char *livebox_service_appid(const char *pkgname)
1436 int is_prime __attribute__((__unused__));
1447 ret = sqlite3_prepare_v2(handle, "SELECT appid, prime FROM pkgmap WHERE pkgid = ? OR appid = ?", -1, &stmt, NULL);
1448 if (ret != SQLITE_OK) {
1449 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1453 ret = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_TRANSIENT);
1454 if (ret != SQLITE_OK) {
1455 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1456 sqlite3_reset(stmt);
1457 sqlite3_finalize(stmt);
1461 ret = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_TRANSIENT);
1462 if (ret != SQLITE_OK) {
1463 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1464 sqlite3_reset(stmt);
1465 sqlite3_finalize(stmt);
1469 ret = sqlite3_step(stmt);
1470 if (ret != SQLITE_ROW) {
1471 pkgmgr_appinfo_h pkg_handle;
1474 ErrPrint("Has no record?: %s\n", sqlite3_errmsg(handle));
1475 sqlite3_reset(stmt);
1476 sqlite3_finalize(stmt);
1478 ret = pkgmgr_appinfo_get_appinfo(pkgname, &pkg_handle);
1479 if (ret != PKGMGR_R_OK) {
1480 ErrPrint("Failed to get appinfo: %s\n", pkgname);
1484 ret = pkgmgr_appinfo_get_pkgname(pkg_handle, &new_appid);
1485 if (ret != PKGMGR_R_OK) {
1486 ErrPrint("Failed to get pkgname for (%s)\n", appid);
1487 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1491 appid = strdup(new_appid);
1493 ErrPrint("Heap: %s\n", strerror(errno));
1495 pkgmgr_appinfo_destroy_appinfo(pkg_handle);
1499 tmp = (char *)sqlite3_column_text(stmt, 0);
1500 if (!tmp || !strlen(tmp)) {
1501 ErrPrint("APPID is NIL\n");
1502 sqlite3_reset(stmt);
1503 sqlite3_finalize(stmt);
1507 appid = strdup(tmp);
1509 ErrPrint("Heap: %s\n", strerror(errno));
1510 sqlite3_reset(stmt);
1511 sqlite3_finalize(stmt);
1515 is_prime = sqlite3_column_int(stmt, 1);
1517 sqlite3_reset(stmt);
1518 sqlite3_finalize(stmt);
1524 EAPI char *livebox_service_lb_script_path(const char *pkgid)
1541 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.box_src FROM provider, pkgmap WHERE pkgmap.pkgid = ? AND provider.pkgid = ?", -1, &stmt, NULL);
1542 if (ret != SQLITE_OK) {
1543 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1547 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1548 if (ret != SQLITE_OK) {
1549 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1550 sqlite3_finalize(stmt);
1554 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1555 if (ret != SQLITE_OK) {
1556 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1557 sqlite3_finalize(stmt);
1561 ret = sqlite3_step(stmt);
1562 if (ret != SQLITE_ROW) {
1563 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1564 sqlite3_reset(stmt);
1565 sqlite3_finalize(stmt);
1569 appid = (char *)sqlite3_column_text(stmt, 0);
1570 if (!appid || !strlen(appid)) {
1571 ErrPrint("Invalid appid : %s\n", sqlite3_errmsg(handle));
1572 sqlite3_reset(stmt);
1573 sqlite3_finalize(stmt);
1577 lb_src = (char *)sqlite3_column_text(stmt, 1);
1578 if (!lb_src || !strlen(lb_src)) {
1579 ErrPrint("No records for lb src : %s\n", sqlite3_errmsg(handle));
1580 sqlite3_reset(stmt);
1581 sqlite3_finalize(stmt);
1585 path = strdup(lb_src);
1587 ErrPrint("Heap: %s\n", strerror(errno));
1588 sqlite3_reset(stmt);
1589 sqlite3_finalize(stmt);
1593 DbgPrint("LB Src: %s\n", path);
1595 sqlite3_reset(stmt);
1596 sqlite3_finalize(stmt);
1602 EAPI char *livebox_service_lb_script_group(const char *pkgid)
1618 ret = sqlite3_prepare_v2(handle, "SELECT box_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1619 if (ret != SQLITE_OK) {
1620 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1624 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1625 if (ret != SQLITE_OK) {
1626 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1627 sqlite3_finalize(stmt);
1631 ret = sqlite3_step(stmt);
1632 if (ret != SQLITE_ROW) {
1633 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1634 sqlite3_reset(stmt);
1635 sqlite3_finalize(stmt);
1639 tmp = (char *)sqlite3_column_text(stmt, 0);
1640 if (tmp && strlen(tmp)) {
1641 group = strdup(tmp);
1643 ErrPrint("Heap: %s\n", strerror(errno));
1646 sqlite3_reset(stmt);
1647 sqlite3_finalize(stmt);
1653 EAPI char *livebox_service_pd_script_path(const char *pkgid)
1670 ret = sqlite3_prepare_v2(handle, "SELECT pkgmap.appid, provider.pd_src FROM provider, pkgmap WHERE provider.pkgid = ? AND pkgmap.pkgid = ?", -1, &stmt, NULL);
1671 if (ret != SQLITE_OK) {
1672 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1676 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1677 if (ret != SQLITE_OK) {
1678 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1679 sqlite3_finalize(stmt);
1683 ret = sqlite3_bind_text(stmt, 2, pkgid, -1, SQLITE_TRANSIENT);
1684 if (ret != SQLITE_OK) {
1685 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1686 sqlite3_finalize(stmt);
1690 ret = sqlite3_step(stmt);
1691 if (ret != SQLITE_ROW) {
1692 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1693 sqlite3_reset(stmt);
1694 sqlite3_finalize(stmt);
1698 appid = (char *)sqlite3_column_text(stmt, 0);
1699 if (!appid || !strlen(appid)) {
1700 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1701 sqlite3_reset(stmt);
1702 sqlite3_finalize(stmt);
1706 pd_src = (char *)sqlite3_column_text(stmt, 1);
1707 if (!pd_src || !strlen(pd_src)) {
1708 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1709 sqlite3_reset(stmt);
1710 sqlite3_finalize(stmt);
1714 path = strdup(pd_src);
1716 ErrPrint("Heap: %s\n", strerror(errno));
1717 sqlite3_reset(stmt);
1718 sqlite3_finalize(stmt);
1722 DbgPrint("PD Src: %s\n", path);
1723 sqlite3_reset(stmt);
1724 sqlite3_finalize(stmt);
1730 EAPI char *livebox_service_pd_script_group(const char *pkgid)
1746 ret = sqlite3_prepare_v2(handle, "SELECT pd_group FROM provider WHERE pkgid = ?", -1, &stmt, NULL);
1747 if (ret != SQLITE_OK) {
1748 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1752 ret = sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_TRANSIENT);
1753 if (ret != SQLITE_OK) {
1754 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1755 sqlite3_finalize(stmt);
1759 ret = sqlite3_step(stmt);
1760 if (ret != SQLITE_ROW) {
1761 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1762 sqlite3_reset(stmt);
1763 sqlite3_finalize(stmt);
1767 tmp = (char *)sqlite3_column_text(stmt, 0);
1768 if (tmp && strlen(tmp)) {
1769 group = strdup(tmp);
1771 ErrPrint("Heap: %s\n", strerror(errno));
1773 sqlite3_reset(stmt);
1774 sqlite3_finalize(stmt);
1780 EAPI int livebox_service_enumerate_cluster_list(int (*cb)(const char *cluster, void *data), void *data)
1784 const char *cluster;
1796 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT cluster FROM groupinfo", -1, &stmt, NULL);
1797 if (ret != SQLITE_OK) {
1798 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1803 while (sqlite3_step(stmt) == SQLITE_ROW) {
1804 cluster = (const char *)sqlite3_column_text(stmt, 0);
1805 if (!cluster || !strlen(cluster))
1808 if (cb(cluster, data) < 0)
1814 sqlite3_reset(stmt);
1815 sqlite3_finalize(stmt);
1821 EAPI int livebox_service_enumerate_category_list(const char *cluster, int (*cb)(const char *cluster, const char *category, void *data), void *data)
1825 const char *category;
1829 if (!cluster || !cb)
1836 ret = sqlite3_prepare_v2(handle, "SELECT DISTINCT category FROM groupinfo WHERE cluster = ?", -1, &stmt, NULL);
1837 if (ret != SQLITE_OK) {
1838 ErrPrint("Error: %s\n", sqlite3_errmsg(handle));
1844 while (sqlite3_step(stmt) == SQLITE_ROW) {
1845 category = (const char *)sqlite3_column_text(stmt, 0);
1846 if (!category || !strlen(category))
1849 if (cb(cluster, category, data) < 0)
1855 sqlite3_reset(stmt);
1856 sqlite3_finalize(stmt);
1862 EAPI int livebox_service_init(void)
1864 if (s_info.handle) {
1865 DbgPrint("Already initialized\n");
1866 s_info.init_count++;
1870 s_info.handle = open_db();
1871 if (s_info.handle) {
1872 s_info.init_count++;
1879 EAPI int livebox_service_fini(void)
1881 if (!s_info.handle || s_info.init_count <= 0) {
1882 ErrPrint("Service is not initialized\n");
1886 s_info.init_count--;
1887 if (s_info.init_count > 0) {
1888 DbgPrint("Init count %d\n", s_info.init_count);
1892 db_util_close(s_info.handle);
1893 s_info.handle = NULL;
1897 EAPI int livebox_service_get_size(int type, int *width, int *height)
1908 return convert_size_from_type(type, width, height);
1911 EAPI int livebox_service_size_type(int width, int height)
1915 if (update_resolution() < 0)
1916 ErrPrint("Failed to update the size list\n");
1918 for (idx = 0; idx < NR_OF_SIZE_LIST; idx++) {
1919 if (SIZE_LIST[idx].w == width && SIZE_LIST[idx].h == height)
1925 return LB_SIZE_TYPE_1x1;
1927 return LB_SIZE_TYPE_2x1;
1929 return LB_SIZE_TYPE_2x2;
1931 return LB_SIZE_TYPE_4x1;
1933 return LB_SIZE_TYPE_4x2;
1935 return LB_SIZE_TYPE_4x3;
1937 return LB_SIZE_TYPE_4x4;
1939 return LB_SIZE_TYPE_EASY_1x1;
1941 return LB_SIZE_TYPE_EASY_3x1;
1943 return LB_SIZE_TYPE_EASY_3x3;
1948 return LB_SIZE_TYPE_UNKNOWN;