2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
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.
22 #include <sys/types.h>
24 #include <iniparser.h>
29 #include <sys/smack.h>
31 #include <vconf-keys.h>
32 #include <unicode/uloc.h>
34 #include <tzplatform_config.h>
35 #include <pkgmgr-info.h>
36 #include <system_info.h>
38 #include <cynara-client.h>
43 #include <aul_widget.h>
44 #include <aul_app_com.h>
45 #include <system_info.h>
47 #include "widget_errno.h"
49 #include "widget_instance.h"
50 #include "widget_service.h"
51 #include "widget_service_internal.h"
54 #define MAX_BUF_SIZE 4096
55 #define SMACK_LABEL_LEN 255
57 #define RESOLUTION_PATH_FORMAT "/usr/share/widget_service/%dx%d/resolution.ini"
58 #define RESOLUTION_PATH_DEFAULT "/usr/share/widget_service/resolution.ini"
59 #define RESOLUTION_SECTION_NAME "resolution"
60 #define RESOLUTION_FORMAT "%dx%d"
62 #define CREATE_WIDGET_TABLE " \
63 PRAGMA user_version = 50; \
64 PRAGMA journal_mode = PERSIST; \
65 PRAGMA foreign_keys = ON; \
66 BEGIN EXCLUSIVE TRANSACTION; \
67 CREATE TABLE widget_class ( \
68 classid TEXT NOT NULL, \
69 update_period INTEGER DEFAULT 0, \
71 appid TEXT NOT NULL, \
72 pkgid TEXT NOT NULL, \
73 nodisplay INTEGER DEFAULT 0, \
74 max_instance INTEGER DEFAULT 0, \
75 prime INTEGER DEFAULT 0, \
76 is_disable INTEGER DEFAULT 0, \
77 PRIMARY KEY(classid) \
79 CREATE TABLE support_size ( \
80 classid TEXT NOT NULL, \
81 preview TEXT NOT NULL, \
82 frame INTEGER DEFAULT 0, \
83 width INTEGER NOT NULL, \
84 height INTEGER NOT NULL, \
85 FOREIGN KEY(classid) REFERENCES widget_class (classid) ON DELETE CASCADE \
87 CREATE TABLE label ( \
88 classid TEXT NOT NULL, \
89 locale TEXT DEFAULT 'No Locale', \
90 label TEXT NOT NULL, \
91 FOREIGN KEY(classid) REFERENCES widget_class (classid) ON DELETE CASCADE \
94 classid TEXT NOT NULL, \
95 locale TEXT DEFAULT 'No Locale', \
97 FOREIGN KEY(classid) REFERENCES widget_class (classid) ON DELETE CASCADE \
101 #define DB_LABEL "User::Home"
102 #define SET_SMACK_LABEL(x) \
104 if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
105 _E("failed chsmack -a %s %s", DB_LABEL, x); \
107 _D("chsmack -a %s %s", DB_LABEL, x); \
110 #define WIDGET_TBL_COUNT 4
111 #define DISABLE_ENDPOINT "widget.disable"
113 static char *_widget_table_list[WIDGET_TBL_COUNT] = {
120 struct widget_instance_info_s {
123 const char *instance_id;
126 static GList *lifecycle_cbs;
127 static bool _is_resolution_loaded = false;
128 static const char *_iso3lang;
129 static char _country[ULOC_COUNTRY_CAPACITY];
130 static char *_syslang;
131 static bool _is_corrupted = false;
132 static aul_app_com_connection_h _conn;
133 static widget_disable_event_cb _disable_event_cb;
134 static void *_disable_user_data;
136 static inline bool _is_widget_feature_enabled(void)
138 static bool feature = false;
139 static bool retrieved = false;
142 if (retrieved == true)
145 ret = system_info_get_platform_bool(
146 "http://tizen.org/feature/shell.appwidget", &feature);
147 if (ret != SYSTEM_INFO_ERROR_NONE) {
148 _E("failed to get system info");
157 static int check_privilege(const char *privilege)
159 cynara *p_cynara = NULL;
164 char subject_label[SMACK_LABEL_LEN + 1] = "";
166 char *client_session = "";
168 ret = cynara_initialize(&p_cynara, NULL);
169 if (ret != CYNARA_API_SUCCESS) {
170 LOGE("cannot init cynara [%d] failed!", ret);
175 fd = open("/proc/self/attr/current", O_RDONLY);
177 LOGE("open [%d] failed!", errno);
182 ret = read(fd, subject_label, SMACK_LABEL_LEN);
184 LOGE("read [%d] failed!", errno);
191 snprintf(uid, 10, "%d", getuid());
193 ret = cynara_check(p_cynara, subject_label, client_session, uid,
195 if (ret != CYNARA_API_ACCESS_ALLOWED) {
196 LOGE("cynara access check [%d] failed!", ret);
204 cynara_finish(p_cynara);
210 #define GLOBALAPP_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
211 static int _is_global(uid_t uid)
213 if (uid == ROOT_USER || uid == GLOBALAPP_USER)
219 static const char *_get_db_path(uid_t uid, bool is_init)
223 if (!_is_global(uid))
224 tzplatform_set_user(uid);
226 if (is_init && !_is_global(uid)) {
227 path = tzplatform_mkpath(TZ_SYS_ETC,
228 "skel/.applications/dbspace/.widget.db");
230 path = tzplatform_mkpath(_is_global(uid) ?
231 TZ_SYS_DB : TZ_USER_DB, ".widget.db");
234 tzplatform_reset_user();
239 static int _check_integrity_cb(void *pid, int argc, char **argv,
242 if (strcmp(argv[0], "ok") == 0)
245 _E("db integrity result fail : %s" , argv[0]);
246 _is_corrupted = true;
250 static void __change_smack(const char *path)
252 char journal_path[PATH_MAX];
254 snprintf(journal_path, sizeof(journal_path), "%s-journal", path);
255 SET_SMACK_LABEL(path);
256 SET_SMACK_LABEL(journal_path);
259 static int __change_own(uid_t uid, const char *path)
262 struct passwd *result;
265 char buf[MAX_BUF_SIZE];
266 char journal_path[PATH_MAX];
269 ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
270 if (result == NULL) {
272 printf("no such user: %d", uid);
274 printf("getpwuid_r failed: %d", errno);
275 return WIDGET_ERROR_FAULT;
278 if (pwd.pw_uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))
281 new_uid = pwd.pw_uid;
283 ret = chown(path, new_uid, pwd.pw_gid);
285 printf("chown fail %s", path);
286 return WIDGET_ERROR_FAULT;
289 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
290 ret = chmod(path, mode);
292 printf("chmod failed : %s", path);
293 return WIDGET_ERROR_FAULT;;
296 snprintf(journal_path, sizeof(journal_path), "%s-journal", path);
297 ret = chown(journal_path, new_uid, pwd.pw_gid);
299 printf("chown fail %s", path);
300 return WIDGET_ERROR_FAULT;
303 ret = chmod(journal_path, mode);
305 printf("chmod failed : %s", path);
306 return WIDGET_ERROR_FAULT;;
309 return WIDGET_ERROR_NONE;
312 static int _recover_db(const char *path, uid_t uid, bool is_init)
318 _I("DB recovery process start");
319 if (access(path, F_OK) == 0)
322 ret = sqlite3_open_v2(path, &db,
323 SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
325 if (ret != SQLITE_OK) {
326 _E("Failed to open db[%d]", ret);
327 sqlite3_close_v2(db);
329 return WIDGET_ERROR_FAULT;
332 ret = sqlite3_exec(db, CREATE_WIDGET_TABLE, NULL, NULL, &errmsg);
333 sqlite3_close_v2(db);
334 if (ret != SQLITE_OK) {
335 _E("Failed to exec query[%d][%s]", ret, errmsg);
336 sqlite3_free(errmsg);
337 return WIDGET_ERROR_FAULT;
340 __change_smack(path);
342 ret = __change_own(uid, path);
343 if (ret != WIDGET_ERROR_NONE)
346 _I("DB recovery process done");
348 return WIDGET_ERROR_NONE;
351 static int _integrity_check(sqlite3 *db)
356 ret = sqlite3_exec(db, "PRAGMA integrity_check",
357 _check_integrity_cb, NULL, &errmsg);
358 if (ret != SQLITE_OK || _is_corrupted) {
359 _E("Failed to exec query[%d][%s]", ret, errmsg);
360 sqlite3_free(errmsg);
361 return WIDGET_ERROR_FAULT;
364 return WIDGET_ERROR_NONE;
367 static int _check_table_exist(sqlite3 *db)
374 "SELECT name FROM sqlite_master WHERE type='table'" \
375 " ORDER BY name ASC";
377 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
378 if (ret != SQLITE_OK) {
379 /* LCOV_EXCL_START */
380 _E("prepare error: %s", sqlite3_errmsg(db));
381 return WIDGET_ERROR_FAULT;
385 while (sqlite3_step(stmt) == SQLITE_ROW && idx < WIDGET_TBL_COUNT) {
386 val = (char *)sqlite3_column_text(stmt, 0);
387 _I("%s : %s", _widget_table_list[idx], val);
388 if (strcmp(_widget_table_list[idx], val) != 0)
393 sqlite3_finalize(stmt);
394 if (idx != WIDGET_TBL_COUNT) {
395 _E("wrong table count");
396 return WIDGET_ERROR_FAULT;
399 return WIDGET_ERROR_NONE;
402 static int _check_db_integrity(uid_t uid, bool is_init)
408 path = _get_db_path(uid, is_init);
410 return WIDGET_ERROR_FAULT;
412 _I("_check_db_integrity %d %d", uid, is_init);
414 /* check table exist */
415 ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
416 if (ret != SQLITE_OK) {
417 /* LCOV_EXCL_START */
418 sqlite3_close_v2(db);
419 return _recover_db(path, uid, is_init);
423 /* check integrity */
424 ret = _integrity_check(db);
425 if (ret != WIDGET_ERROR_NONE) {
426 sqlite3_close_v2(db);
427 return _recover_db(path, uid, is_init);
430 /* check table exist */
431 ret = _check_table_exist(db);
432 sqlite3_close_v2(db);
433 if (ret != WIDGET_ERROR_NONE)
434 return _recover_db(path, uid, is_init);
436 return WIDGET_ERROR_NONE;
439 EAPI int widget_service_check_db_integrity(bool is_init)
443 ret = _check_db_integrity(ROOT_USER, is_init);
444 _I("check global app db result : %d", ret);
446 ret = _check_db_integrity(tzplatform_getuid(TZ_SYS_DEFAULT_USER),
448 _I("check default user db result : %d", ret);
450 return WIDGET_ERROR_NONE;
453 static sqlite3 *_open_db(uid_t uid)
459 path = _get_db_path(uid, false);
463 ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
464 if (ret != SQLITE_OK) {
465 /* LCOV_EXCL_START */
466 _E("failed to open widget db. path(%s), result(%d)",
475 static void _get_column_str(sqlite3_stmt *stmt, int idx, char **str)
479 val = (char *)sqlite3_column_text(stmt, idx);
486 static void _get_column_int(sqlite3_stmt *stmt, int idx, int *i)
488 *i = sqlite3_column_int(stmt, idx);
491 static void _get_column_double(sqlite3_stmt *stmt, int idx, double *i)
493 *i = sqlite3_column_double(stmt, idx);
496 #define WIDGET_SIZE_TYPE_MAX 21
497 static int size_list[WIDGET_SIZE_TYPE_MAX][5] = {
498 { 1, 1, 175, 175, WIDGET_SIZE_TYPE_1x1 }, /*!< 1x1 */
499 { 2, 1, 354, 175, WIDGET_SIZE_TYPE_2x1 }, /*!< 2x1 */
500 { 2, 2, 354, 354, WIDGET_SIZE_TYPE_2x2 }, /*!< 2x2 */
501 { 4, 1, 712, 175, WIDGET_SIZE_TYPE_4x1 }, /*!< 4x1 */
502 { 4, 2, 712, 354, WIDGET_SIZE_TYPE_4x2 }, /*!< 4x2 */
503 { 4, 3, 712, 533, WIDGET_SIZE_TYPE_4x3 }, /*!< 4x3 */
504 { 4, 4, 712, 712, WIDGET_SIZE_TYPE_4x4 }, /*!< 4x4 */
505 { 4, 5, 712, 891, WIDGET_SIZE_TYPE_4x5 }, /*!< 4x5 */
506 { 4, 6, 712, 1070, WIDGET_SIZE_TYPE_4x6 }, /*!< 4x6 */
507 { 21, 21, 224, 215, WIDGET_SIZE_TYPE_EASY_1x1 }, /*!< 21x21 */
508 { 23, 21, 680, 215, WIDGET_SIZE_TYPE_EASY_3x1 }, /*!< 23x21 */
509 { 23, 23, 680, 653, WIDGET_SIZE_TYPE_EASY_3x3 }, /*!< 23x23 */
510 { 0, 0, 720, 1280, WIDGET_SIZE_TYPE_FULL }, /*!< 0x0 */
511 { 3, 1, 480, 126, WIDGET_SIZE_TYPE_3x1 }, /*!< 3x1 */
512 { 3, 3, 480, 426, WIDGET_SIZE_TYPE_3x3 }, /*!< 3x3 */
513 { 3, 6, 480, 876, WIDGET_SIZE_TYPE_3x6 }, /*!< 3x6 */
514 { 6, 3, 984, 426, WIDGET_SIZE_TYPE_6x3 }, /*!< 6x3 */
515 { 6, 6, 984, 876, WIDGET_SIZE_TYPE_6x6 }, /*!< 6x6 */
516 { 6, 12, 720, 1280, WIDGET_SIZE_TYPE_6x12 }, /*!< 6x12 */
517 { 2, 6, 360, 876, WIDGET_SIZE_TYPE_2x6 }, /*!< 2x6 */
518 { 6, 4, 876, 600, WIDGET_SIZE_TYPE_6x4 }, /*!< 6x4 */
521 static char *_resolution_file_path(void)
528 length = strlen(RESOLUTION_PATH_FORMAT) + 12;
529 path = calloc(1, length);
531 _E("calloc: %d\n", errno);
535 ret = system_info_get_platform_int(
536 "http://tizen.org/feature/screen.width", &w);
537 if (ret != SYSTEM_INFO_ERROR_NONE) {
538 _E("fail to get width: %d", ret);
543 ret = system_info_get_platform_int(
544 "http://tizen.org/feature/screen.height", &h);
545 if (ret != SYSTEM_INFO_ERROR_NONE) {
546 _E("fail to get height: %d", ret);
550 _D("screen size : %d %d", w, h);
552 snprintf(path, length, RESOLUTION_PATH_FORMAT, w, h);
553 _D("Selected conf file: %s", path);
554 if (access(path, F_OK) != 0) {
555 _E("Fallback to default, access: %d", errno);
556 strncpy(path, RESOLUTION_PATH_DEFAULT, length);
557 if (access(path, F_OK) != 0) {
558 _E("Serious error - there is no conf file, use default setting: %d\n", errno);
567 static void _resolution_update_size_list(int width_type, int height_type, int width, int height)
571 if (width_type == 1 && height_type == 1) {
573 } else if (width_type == 2 && height_type == 1) {
575 } else if (width_type == 2 && height_type == 2) {
577 } else if (width_type == 4 && height_type == 1) {
579 } else if (width_type == 4 && height_type == 2) {
581 } else if (width_type == 4 && height_type == 3) {
583 } else if (width_type == 4 && height_type == 4) {
585 } else if (width_type == 4 && height_type == 5) {
587 } else if (width_type == 4 && height_type == 6) {
589 } else if (width_type == 21 && height_type == 21) {
591 } else if (width_type == 23 && height_type == 21) {
593 } else if (width_type == 23 && height_type == 23) {
595 } else if (width_type == 0 && height_type == 0) {
597 } else if (width_type == 3 && height_type == 1) {
599 } else if (width_type == 3 && height_type == 3) {
601 } else if (width_type == 3 && height_type == 6) {
603 } else if (width_type == 6 && height_type == 3) {
605 } else if (width_type == 6 && height_type == 6) {
607 } else if (width_type == 6 && height_type == 12) {
609 } else if (width_type == 2 && height_type == 6) {
611 } else if (width_type == 6 && height_type == 4) {
614 _E("Unknown size type: %dx%d (%dx%d)\n", width_type, height_type, width, height);
618 size_list[idx][2] = width;
619 size_list[idx][3] = height;
621 _I("size type(%d): %dx%d (%dx%d)\n", idx, width_type, height_type, width, height);
624 static int _resolution_update_from_file(void)
631 const char *size_str;
633 char *file_path = NULL;
636 char key_format[MAX_COLUMN];
639 file_path = _resolution_file_path();
640 if (file_path == NULL)
641 return WIDGET_ERROR_OUT_OF_MEMORY;
643 dic = iniparser_load(file_path);
645 _E("fail to load [%s] ", file_path);
647 return WIDGET_ERROR_IO_ERROR;
650 snprintf(key_format, sizeof(key_format), "%s:%s",
651 RESOLUTION_SECTION_NAME, RESOLUTION_FORMAT);
652 key_num = iniparser_getsecnkeys(dic, RESOLUTION_SECTION_NAME);
654 _E("key_num is zero");
655 iniparser_freedict(dic);
656 return WIDGET_ERROR_NONE;
660 key_list = calloc(key_num, sizeof(char *));
661 if (key_list == NULL) {
663 iniparser_freedict(dic);
664 return WIDGET_ERROR_OUT_OF_MEMORY;
667 if (!iniparser_getseckeys(dic, RESOLUTION_SECTION_NAME,
668 (const char **)key_list)) {
669 _E("Failed to get section keys");
671 iniparser_freedict(dic);
672 return WIDGET_ERROR_IO_ERROR;
675 for (i = 0; i < key_num; i++) {
676 type_str = key_list[i];
677 size_str = iniparser_getstring(dic, type_str, NULL);
678 if (sscanf(type_str, key_format, &width_type, &height_type) != 2) {
679 _E("Invalid syntax: [%s]", type_str);
683 if (size_str == NULL) {
684 _E("sizen str is NULL");
688 if (sscanf(size_str, "%dx%d", &width, &height) != 2) {
689 _E("Invalid syntax: [%s]", size_str);
692 _resolution_update_size_list(width_type, height_type, width, height);
696 iniparser_freedict(dic);
698 return WIDGET_ERROR_NONE;
701 static int _get_supported_size_type(int width, int height, int *type)
705 if (!_is_resolution_loaded) {
706 _resolution_update_from_file();
707 _is_resolution_loaded = true;
710 for (i = 0; i < WIDGET_SIZE_TYPE_MAX; i++) {
711 if (width == size_list[i][2] && height == size_list[i][3])
715 if (i == WIDGET_SIZE_TYPE_MAX) {
716 _E("failed to get supported size");
720 *type = size_list[i][4];
725 static int _get_supported_size(int type, int *width, int *height)
729 if (!_is_resolution_loaded) {
730 _resolution_update_from_file();
731 _is_resolution_loaded = true;
734 for (i = 0; i < WIDGET_SIZE_TYPE_MAX; i++) {
735 if (type == size_list[i][4])
739 if (i == WIDGET_SIZE_TYPE_MAX) {
740 _E("failed to get supported size type");
744 *width = size_list[i][2];
745 *height = size_list[i][3];
750 static int _convert_to_support_info(int *width, int *height, int *type, int count)
755 if (!_is_resolution_loaded) {
756 _resolution_update_from_file();
757 _is_resolution_loaded = true;
760 for (j = 0; j < count; j++) {
761 for (i = 0; i < WIDGET_SIZE_TYPE_MAX; i++) {
762 if (size_list[i][0] == width[j] &&
763 size_list[i][1] == height[j])
766 if (i == WIDGET_SIZE_TYPE_MAX) {
767 _E("failed to get supported size type");
770 width[j] = size_list[i][2];
771 height[j] = size_list[i][3];
772 type[j] = size_list[i][4];
778 static int _convert_to_support_size(int *width, int *height, int count)
783 if (!_is_resolution_loaded) {
784 _resolution_update_from_file();
785 _is_resolution_loaded = true;
788 for (j = 0; j < count; j++) {
789 for (i = 0; i < WIDGET_SIZE_TYPE_MAX; i++) {
790 if (size_list[i][0] == width[j] &&
791 size_list[i][1] == height[j])
794 if (i == WIDGET_SIZE_TYPE_MAX) {
795 _E("failed to get supported size type");
798 width[j] = size_list[i][2];
799 height[j] = size_list[i][3];
805 static int _convert_type_to_support_size_ratio(int type, int *w, int *h)
809 if (!_is_resolution_loaded) {
810 _resolution_update_from_file();
811 _is_resolution_loaded = true;
814 for (i = 0; i < WIDGET_SIZE_TYPE_MAX; i++) {
815 if (size_list[i][4] == type)
819 if (i == WIDGET_SIZE_TYPE_MAX) {
820 _E("failed to get support size ratio");
824 *w = size_list[i][0];
825 *h = size_list[i][1];
830 static int _get_widget_supported_sizes(const char *widget_id, uid_t uid,
831 int *cnt, int **w, int **h)
833 static const char query[] =
834 "SELECT width, height FROM support_size WHERE classid=?";
837 sqlite3_stmt *stmt = NULL;
845 return WIDGET_ERROR_IO_ERROR;
847 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
848 if (ret != SQLITE_OK) {
849 /* LCOV_EXCL_START */
850 _E("prepare error: %s", sqlite3_errmsg(db));
851 ret = WIDGET_ERROR_FAULT;
856 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
858 while (sqlite3_step(stmt) == SQLITE_ROW)
865 ret = WIDGET_ERROR_NOT_EXIST;
870 width = malloc(sizeof(int) * count);
872 _E("Failed to allocate width");
873 ret = WIDGET_ERROR_FAULT; /* It shoud be WIDGET_ERROR_OUT_OF_MEMORY */
877 height = malloc(sizeof(int) * count);
879 _E("Failed to allocate height");
880 ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
884 for (i = 0; i < count; i++) {
885 ret = sqlite3_step(stmt);
886 if (ret != SQLITE_ROW) {
887 _E("sqlite3_step() is failed. err(%s)",
889 ret = WIDGET_ERROR_FAULT;
892 _get_column_int(stmt, 0, &width[i]);
893 _get_column_int(stmt, 1, &height[i]);
896 if (_convert_to_support_size(width, height, count)) {
897 _E("failed to convert size");
898 ret = WIDGET_ERROR_FAULT;
905 sqlite3_finalize(stmt);
906 sqlite3_close_v2(db);
908 return WIDGET_ERROR_NONE;
916 sqlite3_finalize(stmt);
918 sqlite3_close_v2(db);
924 static int _get_widget_supported_info(const char *widget_id, uid_t uid,
925 int *cnt, int **w, int **h, int **t)
927 static const char query[] =
928 "SELECT width, height FROM support_size WHERE classid=?";
931 sqlite3_stmt *stmt = NULL;
940 return WIDGET_ERROR_IO_ERROR;
942 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
943 if (ret != SQLITE_OK) {
944 /* LCOV_EXCL_START */
945 _E("prepare error: %s", sqlite3_errmsg(db));
946 ret = WIDGET_ERROR_FAULT;
951 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
953 while (sqlite3_step(stmt) == SQLITE_ROW)
960 ret = WIDGET_ERROR_NOT_EXIST;
965 width = malloc(sizeof(int) * count);
967 _E("Failed to allocate width");
968 ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
972 height = malloc(sizeof(int) * count);
974 _E("Failed to allocate height");
975 ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
979 type = malloc(sizeof(int) * count);
981 _E("Failed to allocate type");
982 ret = WIDGET_ERROR_FAULT; /* It should be WIDGET_ERROR_OUT_OF_MEMORY */
986 for (i = 0; i < count; i++) {
987 ret = sqlite3_step(stmt);
988 if (ret != SQLITE_ROW) {
989 _E("sqlite3_step() is failed. err(%s)",
991 ret = WIDGET_ERROR_FAULT;
994 _get_column_int(stmt, 0, &width[i]);
995 _get_column_int(stmt, 1, &height[i]);
998 if (_convert_to_support_info(width, height, type, count)) {
999 _E("failed to convert size");
1000 ret = WIDGET_ERROR_FAULT;
1008 sqlite3_finalize(stmt);
1009 sqlite3_close_v2(db);
1011 return WIDGET_ERROR_NONE;
1021 sqlite3_finalize(stmt);
1023 sqlite3_close_v2(db);
1028 /* LCOV_EXCL_START */
1029 static int _instance_cb(const char *widget_id, const char *instance_id, void *data)
1032 struct widget_instance_info_s *instance_info = (struct widget_instance_info_s *)data;
1034 _D("widget_id %s, instance_id %s", widget_id, instance_id);
1035 if (strcmp(instance_info->instance_id, instance_id) == 0) {
1036 ret = widget_instance_change_period(widget_id, instance_id, instance_info->period);
1039 instance_info->exists = true;
1041 return WIDGET_ERROR_NONE;
1043 /* LCOV_EXCL_STOP */
1045 EAPI int widget_service_change_period(const char *pkgname, const char *id, double period)
1047 widget_instance_h instance;
1048 struct widget_instance_info_s instance_info;
1051 if (!_is_widget_feature_enabled()) {
1052 _E("not supported");
1053 return WIDGET_ERROR_NOT_SUPPORTED;
1056 if (!pkgname || !id || period < 0.0f) {
1057 _E("invalid parameter");
1058 return WIDGET_ERROR_INVALID_PARAMETER;
1061 instance = widget_instance_get_instance(pkgname, id);
1063 instance_info.instance_id = id;
1064 instance_info.exists = false;
1065 instance_info.period = period;
1066 ret = widget_service_get_widget_instance_list(pkgname, _instance_cb, &instance_info);
1067 _D("get list ret %d, %d", ret, instance_info.exists);
1068 if (ret == 0 || !instance_info.exists) {
1069 _E("instance not exists or out of bound(package)");
1070 return WIDGET_ERROR_PERMISSION_DENIED;
1072 ret = WIDGET_ERROR_NONE;
1074 ret = widget_instance_change_period(pkgname, id, period);
1075 widget_instance_unref(instance);
1077 ret = WIDGET_ERROR_NONE;
1083 EAPI int widget_service_trigger_update(const char *widget_id, const char *id, bundle *b, int force)
1086 bundle_raw *raw = NULL;
1089 if (!_is_widget_feature_enabled()) {
1090 _E("not supported");
1091 return WIDGET_ERROR_NOT_SUPPORTED;
1095 _E("invalid parameter");
1096 return WIDGET_ERROR_INVALID_PARAMETER;
1100 bundle_encode(b, &raw, &len);
1102 ret = widget_instance_trigger_update_v2(widget_id, id, (const char *)raw, force);
1110 struct widget_list_item {
1116 static void __free_widget_list(gpointer data)
1118 struct widget_list_item *item = (struct widget_list_item *)data;
1120 free(item->classid);
1125 static int _get_widget_list(const char *pkgid, uid_t uid, GList **list)
1127 static const char query[] =
1128 "SELECT classid, pkgid, prime FROM widget_class WHERE is_disable = 0";
1129 static const char query_pkgid[] =
1131 char query_buf[MAX_BUF_SIZE];
1136 struct widget_list_item *item;
1138 len = snprintf(query_buf, sizeof(query_buf), "%s", query);
1140 strncat(query_buf, query_pkgid, MAX_BUF_SIZE - len - 1);
1144 return WIDGET_ERROR_IO_ERROR;
1146 ret = sqlite3_prepare_v2(db, query_buf, strlen(query_buf), &stmt, NULL);
1147 if (ret != SQLITE_OK) {
1148 /* LCOV_EXCL_START */
1149 _E("prepare error: %s", sqlite3_errmsg(db));
1150 sqlite3_close_v2(db);
1151 return WIDGET_ERROR_FAULT;
1152 /* LCOV_EXCL_STOP */
1156 sqlite3_bind_text(stmt, 1, pkgid, -1, SQLITE_STATIC);
1158 while (sqlite3_step(stmt) == SQLITE_ROW) {
1159 item = calloc(1, sizeof(struct widget_list_item));
1161 /* LCOV_EXCL_START */
1162 _E("out of memory");
1163 sqlite3_finalize(stmt);
1164 sqlite3_close_v2(db);
1165 return WIDGET_ERROR_OUT_OF_MEMORY;
1166 /* LCOV_EXCL_STOP */
1169 _get_column_str(stmt, 0, &item->classid);
1170 _get_column_str(stmt, 1, &item->pkgid);
1171 _get_column_int(stmt, 2, &item->is_prime);
1173 *list = g_list_append(*list, item);
1176 sqlite3_finalize(stmt);
1177 sqlite3_close_v2(db);
1179 return WIDGET_ERROR_NONE;
1182 EAPI int widget_service_get_widget_list(widget_list_cb cb, void *data)
1187 struct widget_list_item *item;
1189 if (!_is_widget_feature_enabled()) {
1190 _E("not supported");
1191 return WIDGET_ERROR_NOT_SUPPORTED;
1195 _E("invalid parameter");
1196 return WIDGET_ERROR_INVALID_PARAMETER;
1199 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1200 return WIDGET_ERROR_PERMISSION_DENIED;
1202 ret = _get_widget_list(NULL, getuid(), &list);
1203 if (ret == WIDGET_ERROR_NONE && !_is_global(getuid()))
1204 _get_widget_list(NULL, GLOBALAPP_USER, &list);
1206 for (tmp = list; tmp; tmp = tmp->next) {
1207 item = (struct widget_list_item *)tmp->data;
1208 /* TODO: get 'is_prime' */
1209 if (cb(item->pkgid, item->classid, item->is_prime, data)
1210 != WIDGET_ERROR_NONE)
1214 ret = g_list_length(list);
1215 g_list_free_full(list, __free_widget_list);
1218 return WIDGET_ERROR_NOT_EXIST;
1223 EAPI int widget_service_get_widget_list_by_pkgid(const char *pkgid,
1224 widget_list_by_pkgid_cb cb, void *data)
1229 struct widget_list_item *item;
1231 if (!_is_widget_feature_enabled()) {
1232 _E("not supported");
1233 return WIDGET_ERROR_NOT_SUPPORTED;
1236 if (pkgid == NULL || cb == NULL) {
1237 _E("invalid parameter");
1238 return WIDGET_ERROR_INVALID_PARAMETER;
1241 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1242 return WIDGET_ERROR_PERMISSION_DENIED;
1244 ret = _get_widget_list(pkgid, getuid(), &list);
1245 if (ret == WIDGET_ERROR_NONE && !_is_global(getuid()))
1246 _get_widget_list(pkgid, GLOBALAPP_USER, &list);
1248 for (tmp = list; tmp; tmp = tmp->next) {
1249 item = (struct widget_list_item *)tmp->data;
1250 /* TODO: get 'is_prime' */
1251 if (cb(item->classid, item->is_prime, data)
1252 != WIDGET_ERROR_NONE)
1256 ret = g_list_length(list);
1257 g_list_free_full(list, __free_widget_list);
1262 static char *_get_main_app_id(const char *widget_id, uid_t uid)
1264 static const char query[] =
1265 "SELECT pkgid FROM widget_class WHERE classid=?";
1270 pkgmgrinfo_pkginfo_h pkginfo;
1276 set_last_result(WIDGET_ERROR_IO_ERROR);
1280 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1281 if (ret != SQLITE_OK) {
1282 /* LCOV_EXCL_START */
1283 _E("prepare error: %s", sqlite3_errmsg(db));
1284 sqlite3_close_v2(db);
1285 set_last_result(WIDGET_ERROR_FAULT);
1287 /* LCOV_EXCL_STOP */
1290 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1292 ret = sqlite3_step(stmt);
1293 if (ret != SQLITE_ROW) {
1294 sqlite3_finalize(stmt);
1295 sqlite3_close_v2(db);
1296 /* TODO: which error should be set? */
1297 if (ret == SQLITE_DONE) {
1298 set_last_result(WIDGET_ERROR_NOT_EXIST);
1300 set_last_result(WIDGET_ERROR_FAULT);
1301 _E("step error: %s", sqlite3_errmsg(db));
1306 _get_column_str(stmt, 0, &pkgid);
1308 sqlite3_finalize(stmt);
1309 sqlite3_close_v2(db);
1311 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, getuid(), &pkginfo);
1313 if (ret != PMINFO_R_OK) {
1314 set_last_result(WIDGET_ERROR_FAULT);
1318 ret = pkgmgrinfo_pkginfo_get_mainappid(pkginfo, &mainappid);
1319 if (ret != PMINFO_R_OK) {
1320 _E("failed to get mainappid of widget %s", widget_id);
1321 set_last_result(WIDGET_ERROR_FAULT);
1322 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
1326 appid = strdup(mainappid);
1327 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
1329 set_last_result(WIDGET_ERROR_NONE);
1334 EAPI char *widget_service_get_main_app_id(const char *widget_id)
1338 if (!_is_widget_feature_enabled()) {
1339 _E("not supported");
1340 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1344 if (widget_id == NULL) {
1345 _E("invalid parameter");
1346 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1350 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
1351 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
1355 appid = _get_main_app_id(widget_id, getuid());
1356 if (appid == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
1357 && !_is_global(getuid()))
1358 appid = _get_main_app_id(widget_id, GLOBALAPP_USER);
1361 _E("Failed to get main appid. widget(%s), result(%d)",
1362 widget_id, get_last_result());
1368 EAPI int widget_service_get_supported_size_types(const char *widget_id,
1369 int *cnt, int **types)
1375 if (!_is_widget_feature_enabled()) {
1376 _E("not supported");
1377 return WIDGET_ERROR_NOT_SUPPORTED;
1380 if (widget_id == NULL || cnt == NULL || types == NULL) {
1381 _E("invalid parameter");
1382 return WIDGET_ERROR_INVALID_PARAMETER;
1385 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1386 return WIDGET_ERROR_PERMISSION_DENIED;
1388 _D("id : %s", widget_id);
1390 ret = _get_widget_supported_info(widget_id, getuid(), cnt,
1391 &width, &height, types);
1392 if (ret == WIDGET_ERROR_NOT_EXIST && !_is_global(getuid()))
1393 ret = _get_widget_supported_info(widget_id, GLOBALAPP_USER,
1394 cnt, &width, &height, types);
1396 if (ret == WIDGET_ERROR_NOT_EXIST) {
1397 _E("cannot find supported sizes for widget %s", widget_id);
1399 return WIDGET_ERROR_NONE;
1400 } else if (ret != WIDGET_ERROR_NONE) {
1407 return WIDGET_ERROR_NONE;
1410 static char *_get_app_id_of_setup_app(const char *widget_id, uid_t uid)
1412 static const char query[] =
1413 "SELECT setup_appid FROM widget_class WHERE classid=?";
1421 set_last_result(WIDGET_ERROR_IO_ERROR);
1425 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1426 if (ret != SQLITE_OK) {
1427 /* LCOV_EXCL_START */
1428 _E("prepare error: %s", sqlite3_errmsg(db));
1429 sqlite3_close_v2(db);
1430 set_last_result(WIDGET_ERROR_FAULT);
1432 /* LCOV_EXCL_STOP */
1435 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1437 ret = sqlite3_step(stmt);
1438 if (ret != SQLITE_ROW) {
1439 sqlite3_finalize(stmt);
1440 sqlite3_close_v2(db);
1441 /* TODO: which error should be set? */
1442 if (ret == SQLITE_DONE) {
1443 set_last_result(WIDGET_ERROR_NOT_EXIST);
1445 set_last_result(WIDGET_ERROR_FAULT);
1446 _E("step error: %s", sqlite3_errmsg(db));
1451 _get_column_str(stmt, 0, &appid);
1453 sqlite3_finalize(stmt);
1454 sqlite3_close_v2(db);
1456 set_last_result(WIDGET_ERROR_NONE);
1461 EAPI char *widget_service_get_app_id_of_setup_app(const char *widget_id)
1465 if (!_is_widget_feature_enabled()) {
1466 _E("not supported");
1467 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1471 if (widget_id == NULL) {
1472 _E("invalid parameter");
1473 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1477 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
1478 _E("permission denied");
1479 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
1483 appid = _get_app_id_of_setup_app(widget_id, getuid());
1484 if (appid == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
1485 && !_is_global(getuid()))
1486 appid = _get_app_id_of_setup_app(widget_id, GLOBALAPP_USER);
1489 _E("Failed to get setdup app. widget_id(%s), result(%d)",
1490 widget_id, get_last_result());
1496 static int _get_nodisplay(const char *widget_id, uid_t uid)
1498 static const char query[] =
1499 "SELECT nodisplay FROM widget_class WHERE classid=?";
1507 set_last_result(WIDGET_ERROR_IO_ERROR);
1511 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1512 if (ret != SQLITE_OK) {
1513 _E("prepare error: %s", sqlite3_errmsg(db));
1514 sqlite3_close_v2(db);
1515 set_last_result(WIDGET_ERROR_FAULT);
1519 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1521 ret = sqlite3_step(stmt);
1522 if (ret != SQLITE_ROW) {
1523 sqlite3_finalize(stmt);
1524 sqlite3_close_v2(db);
1525 /* TODO: which error should be set? */
1526 if (ret == SQLITE_DONE) {
1527 set_last_result(WIDGET_ERROR_NOT_EXIST);
1529 set_last_result(WIDGET_ERROR_FAULT);
1530 _E("step error: %s", sqlite3_errmsg(db));
1535 _get_column_int(stmt, 0, &nodisplay);
1537 sqlite3_finalize(stmt);
1538 sqlite3_close_v2(db);
1540 set_last_result(WIDGET_ERROR_NONE);
1545 static int _get_update_period(const char *widget_id, uid_t uid, double *period)
1547 static const char query[] =
1548 "SELECT update_period FROM widget_class WHERE classid=?";
1555 return WIDGET_ERROR_IO_ERROR;
1558 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1559 if (ret != SQLITE_OK) {
1560 _E("prepare error: %s", sqlite3_errmsg(db));
1561 sqlite3_close_v2(db);
1562 return WIDGET_ERROR_FAULT;
1565 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1567 ret = sqlite3_step(stmt);
1568 if (ret != SQLITE_ROW) {
1569 sqlite3_finalize(stmt);
1570 sqlite3_close_v2(db);
1571 if (ret == SQLITE_DONE) {
1572 return WIDGET_ERROR_NOT_EXIST;
1574 _E("step error: %s", sqlite3_errmsg(db));
1575 return WIDGET_ERROR_FAULT;
1579 _get_column_double(stmt, 0, period);
1580 sqlite3_finalize(stmt);
1581 sqlite3_close_v2(db);
1583 return WIDGET_ERROR_NONE;
1586 int widget_service_get_update_period(const char *widget_id, double *period)
1590 ret = _get_update_period(widget_id, getuid(), period);
1591 if (ret == WIDGET_ERROR_NOT_EXIST)
1592 ret = _get_update_period(widget_id, GLOBALAPP_USER, period);
1594 if (ret != WIDGET_ERROR_NONE) {
1595 _E("Failed to get update period. widget(%s), result(%d)",
1602 EAPI int widget_service_get_nodisplay(const char *widget_id)
1606 if (!_is_widget_feature_enabled()) {
1607 _E("not supported");
1608 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1612 if (widget_id == NULL) {
1613 _E("invalid parameter");
1614 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1618 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
1619 _E("permission denied");
1620 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
1624 nodisplay = _get_nodisplay(widget_id, getuid());
1625 if (get_last_result() == WIDGET_ERROR_NOT_EXIST
1626 && !_is_global(getuid()))
1627 nodisplay = _get_nodisplay(widget_id, GLOBALAPP_USER);
1629 if (get_last_result() != WIDGET_ERROR_NONE) {
1630 _E("Failed to get nodisplay. widget(%s), result(%d)",
1631 widget_id, get_last_result());
1637 /* deprecated, always return need_of_frame as false */
1638 EAPI int widget_service_get_need_of_frame(const char *pkgid, widget_size_type_e size_type, bool *need_of_frame)
1640 if (!_is_widget_feature_enabled()) {
1641 _E("not supported");
1642 return WIDGET_ERROR_NOT_SUPPORTED;
1646 _E("invalid parameter");
1647 return WIDGET_ERROR_INVALID_PARAMETER;
1650 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1651 return WIDGET_ERROR_PERMISSION_DENIED;
1653 *need_of_frame = false;
1655 return WIDGET_ERROR_NONE;
1658 /* deprecated, always return need_of_touch_event as false */
1659 EAPI int widget_service_get_need_of_touch_effect(const char *pkgid, widget_size_type_e size_type, bool *need_of_touch_event)
1661 if (!_is_widget_feature_enabled()) {
1662 _E("not supported");
1663 return WIDGET_ERROR_NOT_SUPPORTED;
1667 _E("invalid parameter");
1668 return WIDGET_ERROR_INVALID_PARAMETER;
1671 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1672 return WIDGET_ERROR_PERMISSION_DENIED;
1674 *need_of_touch_event = false;
1676 return WIDGET_ERROR_NONE;
1679 /* deprecated, always return need_of_mouse_event as false */
1680 EAPI int widget_service_get_need_of_mouse_event(const char *pkgid, widget_size_type_e size_type, bool *need_of_mouse_event)
1682 if (!_is_widget_feature_enabled()) {
1683 _E("not supported");
1684 return WIDGET_ERROR_NOT_SUPPORTED;
1688 _E("invalid parameter");
1689 return WIDGET_ERROR_INVALID_PARAMETER;
1692 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
1693 return WIDGET_ERROR_PERMISSION_DENIED;
1695 *need_of_mouse_event = false;
1696 return WIDGET_ERROR_NONE;
1699 static char *_get_preview_image_path(const char *widget_id, int width,
1700 int height, uid_t uid)
1702 static const char query[] =
1703 "SELECT preview FROM support_size WHERE "
1704 "classid=? AND width=? AND height=?";
1712 set_last_result(WIDGET_ERROR_IO_ERROR);
1716 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1717 if (ret != SQLITE_OK) {
1718 /* LCOV_EXCL_START */
1719 _E("prepare error: %s", sqlite3_errmsg(db));
1720 sqlite3_close_v2(db);
1721 set_last_result(WIDGET_ERROR_FAULT);
1723 /* LCOV_EXCL_STOP */
1726 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1727 sqlite3_bind_int(stmt, 2, width);
1728 sqlite3_bind_int(stmt, 3, height);
1730 ret = sqlite3_step(stmt);
1731 if (ret != SQLITE_ROW) {
1732 sqlite3_finalize(stmt);
1733 sqlite3_close_v2(db);
1734 /* TODO: which error should be set? */
1735 if (ret == SQLITE_DONE) {
1736 set_last_result(WIDGET_ERROR_NOT_EXIST);
1738 set_last_result(WIDGET_ERROR_FAULT);
1739 _E("step error: %s", sqlite3_errmsg(db));
1744 _get_column_str(stmt, 0, &path);
1746 sqlite3_finalize(stmt);
1747 sqlite3_close_v2(db);
1749 set_last_result(WIDGET_ERROR_NONE);
1754 static int update_lang_info(void)
1760 syslang = vconf_get_str(VCONFKEY_LANGSET);
1762 ErrPrint("Failed to get vconf-lang");
1766 if (_syslang && !strcmp(_syslang, syslang)) {
1767 _D("Syslang is not changed: %s", syslang);
1776 uloc_setDefault((const char *)_syslang, &err);
1777 if (!U_SUCCESS(err)) {
1778 _E("Failed to set default lang: %s", u_errorName(err));
1784 _iso3lang = uloc_getISO3Language(uloc_getDefault());
1785 if (!_iso3lang || !strlen(_iso3lang)) {
1786 _E("Failed to get iso3lang");
1793 country_len = uloc_getCountry(uloc_getDefault(), _country, ULOC_COUNTRY_CAPACITY, &err);
1794 if (!U_SUCCESS(err) || country_len <= 0) {
1795 _E("Failed to get locale: %s, %s, %d (%s)", u_errorName(err), _iso3lang, country_len, _country);
1804 EAPI char *widget_service_get_preview_image_path(const char *widget_id,
1805 widget_size_type_e size_type)
1815 if (!_is_widget_feature_enabled()) {
1816 _E("not supported");
1817 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1821 /* TODO: validate size_type */
1822 if (widget_id == NULL) {
1823 _E("invalid parameter");
1824 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1828 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
1829 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
1833 _convert_type_to_support_size_ratio(size_type, &w, &h);
1834 path = _get_preview_image_path(widget_id, w, h, getuid());
1835 if (path == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
1836 && !_is_global(getuid()))
1837 path = _get_preview_image_path(widget_id, w, h, GLOBALAPP_USER);
1840 _E("Can not find preview path");
1841 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
1845 if (update_lang_info() != 0)
1848 buf_len = strlen(path) + strlen(_iso3lang) + strlen(_country) + 3; /* '/' '-' '/' */
1849 lang_path = malloc(buf_len + 1);
1851 /* LCOV_EXCL_START */
1852 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
1853 _E("Heap: %d", errno);
1856 /* LCOV_EXCL_STOP */
1859 for (i = strlen(path); i >= 0 && path[i] != '/'; i--);
1862 strncpy(lang_path, path, i);
1863 printed = snprintf(lang_path + i, buf_len - i, "%s-%s/%s", _iso3lang, _country, path + i);
1864 if (lang_path[i + printed] != '\0') {
1865 _E("Path is truncated");
1866 lang_path[i + printed] = '\0';
1869 if (access(lang_path, R_OK) != 0) {
1870 _D("Access failed: %s, %d", lang_path, errno);
1881 static char *_get_main_widget_id(const char *pkg_id, uid_t uid)
1883 static const char query[] =
1884 "SELECT classid FROM widget_class "
1885 "WHERE pkgid=? and prime=1 ";
1893 set_last_result(WIDGET_ERROR_IO_ERROR);
1897 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1898 if (ret != SQLITE_OK) {
1899 /* LCOV_EXCL_START */
1900 _E("prepare error: %s", sqlite3_errmsg(db));
1901 sqlite3_close_v2(db);
1902 set_last_result(WIDGET_ERROR_FAULT);
1904 /* LCOV_EXCL_STOP */
1907 sqlite3_bind_text(stmt, 1, pkg_id, -1, SQLITE_STATIC);
1909 ret = sqlite3_step(stmt);
1910 if (ret != SQLITE_ROW) {
1911 if (ret != SQLITE_DONE)
1912 _E("step error: %s", sqlite3_errmsg(db));
1913 sqlite3_finalize(stmt);
1914 sqlite3_close_v2(db);
1915 /* TODO: which error should be set? */
1916 set_last_result(ret == SQLITE_DONE ? WIDGET_ERROR_NOT_EXIST :
1917 WIDGET_ERROR_FAULT);
1921 _get_column_str(stmt, 0, &widget_id);
1923 sqlite3_finalize(stmt);
1924 sqlite3_close_v2(db);
1926 set_last_result(WIDGET_ERROR_NONE);
1931 static char *_get_icon(const char *widget_id, const char *lang, uid_t uid)
1933 static const char query[] =
1934 "SELECT icon FROM icon "
1935 "WHERE classid=? AND (locale=? OR locale IS NULL) "
1936 "ORDER BY locale DESC";
1944 set_last_result(WIDGET_ERROR_IO_ERROR);
1948 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
1949 if (ret != SQLITE_OK) {
1950 /* LCOV_EXCL_START */
1951 _E("prepare error: %s", sqlite3_errmsg(db));
1952 sqlite3_close_v2(db);
1953 set_last_result(WIDGET_ERROR_FAULT);
1955 /* LCOV_EXCL_STOP */
1958 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
1960 sqlite3_bind_text(stmt, 2, lang, -1, SQLITE_STATIC);
1962 sqlite3_bind_null(stmt, 2);
1964 ret = sqlite3_step(stmt);
1965 if (ret != SQLITE_ROW) {
1966 if (ret != SQLITE_DONE)
1967 _E("step error: %s", sqlite3_errmsg(db));
1968 sqlite3_finalize(stmt);
1969 sqlite3_close_v2(db);
1970 /* TODO: which error should be set? */
1971 set_last_result(ret == SQLITE_DONE ? WIDGET_ERROR_NOT_EXIST :
1972 WIDGET_ERROR_FAULT);
1976 _get_column_str(stmt, 0, &icon);
1978 sqlite3_finalize(stmt);
1979 sqlite3_close_v2(db);
1981 set_last_result(WIDGET_ERROR_NONE);
1986 EAPI char *widget_service_get_icon(const char *pkgid, const char *lang)
1991 if (!_is_widget_feature_enabled()) {
1992 _E("not supported");
1993 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
1997 if (pkgid == NULL) {
1998 _E("invalid parameter");
1999 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
2003 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
2004 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
2008 widget_id = _get_main_widget_id(pkgid, getuid());
2009 if (widget_id == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2010 && !_is_global(getuid()))
2011 widget_id = _get_main_widget_id(pkgid, GLOBALAPP_USER);
2013 icon = _get_icon(widget_id, lang, getuid());
2014 if (icon == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2015 && !_is_global(getuid()))
2016 icon = _get_icon(widget_id, lang, GLOBALAPP_USER);
2019 _E("Failed to get icon. pkgid(%s), lang(%s), result(%d)",
2020 pkgid, lang, get_last_result());
2026 static char *_get_name(const char *widget_id, const char *lang, uid_t uid)
2028 static const char query[] =
2029 "SELECT label FROM label "
2030 "WHERE classid=? AND (LOWER(locale)=LOWER(?) OR locale IS NULL) "
2031 "ORDER BY locale DESC";
2039 set_last_result(WIDGET_ERROR_IO_ERROR);
2043 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2044 if (ret != SQLITE_OK) {
2045 /* LCOV_EXCL_START */
2046 _E("step error: %s", sqlite3_errmsg(db));
2047 sqlite3_close_v2(db);
2048 set_last_result(WIDGET_ERROR_FAULT);
2050 /* LCOV_EXCL_STOP */
2053 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
2055 sqlite3_bind_text(stmt, 2, lang, -1, SQLITE_STATIC);
2057 sqlite3_bind_null(stmt, 2);
2059 ret = sqlite3_step(stmt);
2060 if (ret != SQLITE_ROW) {
2061 if (ret != SQLITE_DONE)
2062 _E("step error: %s", sqlite3_errmsg(db));
2063 sqlite3_finalize(stmt);
2064 sqlite3_close_v2(db);
2065 /* TODO: which error should be set? */
2066 set_last_result(ret == SQLITE_DONE ? WIDGET_ERROR_NOT_EXIST :
2067 WIDGET_ERROR_FAULT);
2071 _get_column_str(stmt, 0, &label);
2073 sqlite3_finalize(stmt);
2074 sqlite3_close_v2(db);
2076 set_last_result(WIDGET_ERROR_NONE);
2082 static char *__get_cur_locale(void)
2087 language = vconf_get_str(VCONFKEY_LANGSET);
2102 language = strdup("en-us");
2108 EAPI char *widget_service_get_name(const char *widget_id, const char *lang)
2113 if (!_is_widget_feature_enabled()) {
2114 _E("not supported");
2115 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
2119 if (widget_id == NULL) {
2120 _E("invalid parameter");
2121 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
2125 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
2126 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
2131 language = __get_cur_locale();
2133 language = strdup(lang);
2136 /* LCOV_EXCL_START */
2137 _E("Out-of-memory");
2138 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
2140 /* LCOV_EXCL_STOP */
2143 name = _get_name(widget_id, language, getuid());
2144 if (name == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2145 && !_is_global(getuid()))
2146 name = _get_name(widget_id, language, GLOBALAPP_USER);
2149 _E("Failed to get name. widget(%s), lang(%s), result(%d)",
2150 widget_id, lang, get_last_result());
2158 EAPI int widget_service_get_supported_sizes(const char *widget_id, int *cnt,
2163 if (!_is_widget_feature_enabled()) {
2164 _E("not supported");
2165 return WIDGET_ERROR_NOT_SUPPORTED;
2168 if (widget_id == NULL || cnt == NULL || w == NULL || h == NULL) {
2169 _E("invalid parameter");
2170 return WIDGET_ERROR_INVALID_PARAMETER;
2173 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2174 return WIDGET_ERROR_PERMISSION_DENIED;
2176 ret = _get_widget_supported_sizes(widget_id, getuid(), cnt, w, h);
2177 if (ret == WIDGET_ERROR_NOT_EXIST
2178 && !_is_global(getuid()))
2179 ret = _get_widget_supported_sizes(widget_id, GLOBALAPP_USER,
2182 if (ret != WIDGET_ERROR_NONE) {
2183 if (ret == WIDGET_ERROR_NOT_EXIST) {
2184 _E("cannot find supported sizes for widget %s",
2187 _E("Failed to get widget(%s) supported size",
2195 static char *_get_widget_id(const char *appid, uid_t uid)
2197 static const char query[] =
2198 "SELECT classid FROM widget_class WHERE appid=?";
2206 set_last_result(WIDGET_ERROR_IO_ERROR);
2210 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2211 if (ret != SQLITE_OK) {
2212 /* LCOV_EXCL_START */
2213 _E("prepare error: %s", sqlite3_errmsg(db));
2214 sqlite3_close_v2(db);
2215 set_last_result(WIDGET_ERROR_FAULT);
2217 /* LCOV_EXCL_STOP */
2220 sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_STATIC);
2222 ret = sqlite3_step(stmt);
2223 if (ret != SQLITE_ROW) {
2224 _E("step error: %s", sqlite3_errmsg(db));
2225 sqlite3_finalize(stmt);
2226 sqlite3_close_v2(db);
2227 /* TODO: which error should be set? */
2228 set_last_result(ret == SQLITE_DONE ? WIDGET_ERROR_NOT_EXIST :
2229 WIDGET_ERROR_FAULT);
2233 _get_column_str(stmt, 0, &classid);
2235 sqlite3_finalize(stmt);
2236 sqlite3_close_v2(db);
2238 set_last_result(WIDGET_ERROR_NONE);
2243 static char *__get_widget_main_appid_from_pkgid_or_appid(const char *id)
2245 pkgmgrinfo_appinfo_h appinfo = NULL;
2246 pkgmgrinfo_pkginfo_h pkginfo = NULL;
2247 char *new_pkgid = NULL;
2249 GList *widget_list = NULL;
2251 char *widget_id = NULL;
2253 struct widget_list_item *item;
2255 ret = pkgmgrinfo_pkginfo_get_pkginfo(id, &pkginfo);
2256 if (ret != PMINFO_R_OK) {
2257 ret = pkgmgrinfo_appinfo_get_appinfo(id, &appinfo);
2258 if (ret != PMINFO_R_OK) {
2259 _E("Failed to get app/pkg info - %s", id);
2263 ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &pkgid);
2264 if (ret != PMINFO_R_OK) {
2265 _E("Failed to get pkgid - %s", id);
2269 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo);
2270 if (ret != PMINFO_R_OK) {
2271 _E("Failed to get pkginfo - %s", id);
2275 new_pkgid = strdup(pkgid);
2276 if (new_pkgid == NULL) {
2277 _E("out of memory"); /* LCOV_EXCL_LINE */
2278 goto end; /* LCOV_EXCL_LINE */
2283 ret = _get_widget_list(id, getuid(), &widget_list);
2284 if ((ret != WIDGET_ERROR_NONE || widget_list == NULL)
2285 && !_is_global(getuid()))
2286 ret = _get_widget_list(id, GLOBALAPP_USER, &widget_list);
2288 if (widget_list == NULL)
2291 iter = g_list_first(widget_list);
2293 item = (struct widget_list_item *)iter->data;
2294 iter = g_list_next(iter);
2296 widget_list = g_list_remove(widget_list, item);
2297 if (item->is_prime) {
2300 widget_id = strdup(item->classid);
2303 if (widget_id == NULL)
2304 widget_id = strdup(item->classid);
2306 free(item->classid);
2314 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
2316 pkgmgrinfo_appinfo_destroy_appinfo(appinfo);
2323 EAPI char *widget_service_get_widget_id(const char *appid)
2328 if (!_is_widget_feature_enabled()) {
2329 _E("not supported");
2330 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
2334 if (appid == NULL) {
2335 _E("invalid parameter");
2336 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
2340 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
2341 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
2345 classid = _get_widget_id(appid, getuid());
2346 if (classid == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2347 && !_is_global(getuid()))
2348 classid = _get_widget_id(appid, GLOBALAPP_USER);
2352 else if (get_last_result() != WIDGET_ERROR_NOT_EXIST)
2355 widget_id = __get_widget_main_appid_from_pkgid_or_appid(appid);
2356 if (widget_id == NULL) {
2357 _E("Failed to get widget id");
2361 classid = _get_widget_id(widget_id, getuid());
2362 if (classid == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2363 && !_is_global(getuid()))
2364 classid = _get_widget_id(widget_id, GLOBALAPP_USER);
2367 _E("Failed to get widget id. appid(%s), result(%d)",
2368 appid, get_last_result());
2376 static char *_get_package_id(const char *widget_id, uid_t uid)
2378 static const char query[] =
2379 "SELECT pkgid FROM widget_class WHERE classid=?";
2387 set_last_result(WIDGET_ERROR_IO_ERROR);
2391 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2392 if (ret != SQLITE_OK) {
2393 /* LCOV_EXCL_START */
2394 _E("prepare error: %s", sqlite3_errmsg(db));
2395 sqlite3_close_v2(db);
2396 set_last_result(WIDGET_ERROR_FAULT);
2398 /* LCOV_EXCL_STOP */
2401 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
2403 ret = sqlite3_step(stmt);
2404 if (ret != SQLITE_ROW) {
2405 sqlite3_finalize(stmt);
2406 sqlite3_close_v2(db);
2407 /* TODO: which error should be set? */
2408 if (ret == SQLITE_DONE) {
2409 set_last_result(WIDGET_ERROR_NOT_EXIST);
2411 set_last_result(WIDGET_ERROR_FAULT);
2412 _E("step error: %s", sqlite3_errmsg(db));
2417 _get_column_str(stmt, 0, &pkgid);
2419 sqlite3_finalize(stmt);
2420 sqlite3_close_v2(db);
2422 set_last_result(WIDGET_ERROR_NONE);
2427 EAPI char *widget_service_get_package_id(const char *widget_id)
2431 if (!_is_widget_feature_enabled()) {
2432 _E("not supported");
2433 set_last_result(WIDGET_ERROR_NOT_SUPPORTED);
2437 if (widget_id == NULL) {
2438 _E("invalid parameter");
2439 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
2443 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0) {
2444 set_last_result(WIDGET_ERROR_PERMISSION_DENIED);
2448 pkgid = _get_package_id(widget_id, getuid());
2449 if (pkgid == NULL && get_last_result() == WIDGET_ERROR_NOT_EXIST
2450 && !_is_global(getuid()))
2451 pkgid = _get_package_id(widget_id, GLOBALAPP_USER);
2454 _E("Failed to get package id. widget(%s), result(%d)",
2455 widget_id, get_last_result());
2461 EAPI int widget_service_get_size(widget_size_type_e type, int *width,
2464 if (!_is_widget_feature_enabled()) {
2465 _E("not supported");
2466 return WIDGET_ERROR_NOT_SUPPORTED;
2469 if (width == NULL || height == NULL) {
2470 _E("invalid parameter");
2471 return WIDGET_ERROR_INVALID_PARAMETER;
2474 if (_get_supported_size(type, width, height))
2475 return WIDGET_ERROR_INVALID_PARAMETER;
2477 return WIDGET_ERROR_NONE;
2480 EAPI int widget_service_get_size_type(int width, int height,
2481 widget_size_type_e *size_type)
2483 if (!_is_widget_feature_enabled()) {
2484 _E("not supported");
2485 return WIDGET_ERROR_NOT_SUPPORTED;
2488 if (width <= 0 || height <= 0 || size_type == NULL) {
2489 _E("invalid parameter");
2490 return WIDGET_ERROR_INVALID_PARAMETER;
2493 if (_get_supported_size_type(width, height, (int *)size_type))
2494 return WIDGET_ERROR_INVALID_PARAMETER;
2496 return WIDGET_ERROR_NONE;
2499 EAPI int widget_service_get_content_of_widget_instance(const char *widget_id, const char *widget_instance_id, bundle **b)
2504 if (!_is_widget_feature_enabled()) {
2505 _E("not supported");
2506 return WIDGET_ERROR_NOT_SUPPORTED;
2509 if (widget_id == NULL || widget_instance_id == NULL || b == NULL) {
2510 _E("invalid parameter");
2511 return WIDGET_ERROR_INVALID_PARAMETER;
2514 ret = aul_widget_instance_get_content(widget_id, widget_instance_id, &raw);
2516 *b = bundle_decode((const bundle_raw *)raw, strlen(raw));
2518 return WIDGET_ERROR_NONE;
2523 ret = WIDGET_ERROR_INVALID_PARAMETER;
2526 ret = WIDGET_ERROR_IO_ERROR;
2529 ret = WIDGET_ERROR_NOT_EXIST;
2532 ret = WIDGET_ERROR_PERMISSION_DENIED;
2535 ret = WIDGET_ERROR_FAULT;
2541 struct instance_cb {
2542 const char *widget_id;
2543 widget_instance_list_cb cb;
2548 /* LCOV_EXCL_START */
2549 static void __instance_list_cb(const char *instance_id, void *user_data)
2551 struct instance_cb *cb_data = (struct instance_cb *)user_data;
2558 _D("instance list: %s %s", cb_data->widget_id, instance_id);
2560 cb_data->cb(cb_data->widget_id, instance_id, cb_data->data);
2562 /* LCOV_EXCL_STOP */
2564 EAPI int widget_service_get_widget_instance_list(const char *widget_id, widget_instance_list_cb cb, void *data)
2566 struct instance_cb cb_data;
2569 cb_data.widget_id = widget_id;
2571 cb_data.data = data;
2574 if (!_is_widget_feature_enabled()) {
2575 _E("not supported");
2576 return WIDGET_ERROR_NOT_SUPPORTED;
2579 if (widget_id == NULL || cb == NULL) {
2580 _E("inavlid parameter");
2581 return WIDGET_ERROR_INVALID_PARAMETER;
2584 ret = aul_widget_instance_foreach(widget_id, __instance_list_cb, &cb_data);
2586 if (ret == AUL_R_EILLACC)
2587 return WIDGET_ERROR_PERMISSION_DENIED;
2589 if (ret == AUL_R_ENOAPP || cb_data.cnt == 0)
2590 return WIDGET_ERROR_NOT_EXIST;
2595 struct lifecycle_s {
2597 widget_lifecycle_event_cb cb;
2601 static int __listen_status(const char *widget_id, const char *instance_id, int status, void *data)
2603 struct lifecycle_s *cb_info = (struct lifecycle_s *)data;
2608 cb_info->cb(widget_id, status, instance_id, cb_info->data);
2613 EAPI int widget_service_set_lifecycle_event_cb(const char *widget_id, widget_lifecycle_event_cb cb, void *data)
2615 int ret = WIDGET_ERROR_NONE;
2616 struct lifecycle_s *cb_info;
2617 GList *head = lifecycle_cbs;
2619 if (!_is_widget_feature_enabled()) {
2620 _E("not supported");
2621 return WIDGET_ERROR_NOT_SUPPORTED;
2625 _E("invalid parameter");
2626 return WIDGET_ERROR_INVALID_PARAMETER;
2630 cb_info = (struct lifecycle_s *)head->data;
2631 if (cb_info && widget_id && cb_info->widget_id) {
2632 if (strlen(widget_id) != strlen(cb_info->widget_id)) {
2637 if (strncmp(cb_info->widget_id, widget_id, strlen(widget_id)) == 0)
2638 return WIDGET_ERROR_INVALID_PARAMETER;
2640 } else if (cb_info && widget_id == NULL && cb_info->widget_id == NULL) {
2641 return WIDGET_ERROR_INVALID_PARAMETER;
2647 cb_info = (struct lifecycle_s *)malloc(sizeof(struct lifecycle_s));
2648 if (cb_info == NULL)
2649 return WIDGET_ERROR_OUT_OF_MEMORY;
2652 cb_info->widget_id = strdup(widget_id);
2654 cb_info->widget_id = NULL;
2657 cb_info->data = data;
2659 lifecycle_cbs = g_list_append(lifecycle_cbs, cb_info);
2661 widget_instance_listen_status(widget_id, __listen_status, cb_info);
2666 EAPI int widget_service_unset_lifecycle_event_cb(const char *widget_id, void **user_data)
2668 struct lifecycle_s *cb_info;
2669 struct lifecycle_s *found = NULL;
2670 GList *head = lifecycle_cbs;
2672 if (!_is_widget_feature_enabled()) {
2673 _E("not supported");
2674 return WIDGET_ERROR_NOT_SUPPORTED;
2678 cb_info = (struct lifecycle_s *)head->data;
2679 if (cb_info && widget_id && cb_info->widget_id) {
2680 if (strncmp(cb_info->widget_id, widget_id, strlen(widget_id)) == 0) {
2684 } else if (cb_info && widget_id == NULL && cb_info->widget_id == NULL) {
2693 lifecycle_cbs = g_list_remove(lifecycle_cbs, found);
2695 *user_data = found->data;
2697 if (found->widget_id)
2698 free(found->widget_id);
2702 widget_instance_unlisten_status(widget_id);
2704 return WIDGET_ERROR_NONE;
2707 return WIDGET_ERROR_NOT_EXIST;
2710 static int __get_max_instance(const char *widget_id, uid_t uid)
2712 static const char query[] =
2713 "SELECT max_instance FROM widget_class WHERE classid=?";
2717 int max_instance = 0;
2721 return WIDGET_ERROR_IO_ERROR;
2723 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2724 if (ret != SQLITE_OK) {
2725 _E("prepare error: %s", sqlite3_errmsg(db));
2726 sqlite3_close_v2(db);
2727 return WIDGET_ERROR_FAULT;
2730 sqlite3_bind_text(stmt, 1, widget_id, -1, SQLITE_STATIC);
2732 ret = sqlite3_step(stmt);
2733 if (ret != SQLITE_ROW) {
2734 sqlite3_finalize(stmt);
2735 sqlite3_close_v2(db);
2736 if (ret == SQLITE_DONE) {
2737 return WIDGET_ERROR_NOT_EXIST;
2739 _E("step error: %s", sqlite3_errmsg(db));
2740 return WIDGET_ERROR_FAULT;
2744 _get_column_int(stmt, 0, &max_instance);
2745 _D("widget_id: %s, max-instance: %d", widget_id, max_instance);
2747 sqlite3_finalize(stmt);
2748 sqlite3_close_v2(db);
2750 return max_instance;
2753 EAPI int widget_service_get_widget_max_count(const char *widget_id)
2757 if (!_is_widget_feature_enabled()) {
2758 _E("Not supported");
2759 return WIDGET_ERROR_NOT_SUPPORTED;
2762 if (widget_id == NULL) {
2763 _E("Invalid parameter");
2764 return WIDGET_ERROR_INVALID_PARAMETER;
2767 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2768 return WIDGET_ERROR_PERMISSION_DENIED;
2770 ret = __get_max_instance(widget_id, getuid());
2771 if (ret == WIDGET_ERROR_NOT_EXIST && !_is_global(getuid()))
2772 ret = __get_max_instance(widget_id, GLOBALAPP_USER);
2773 _I("Widget(%s) max count. result(%d)", widget_id, ret);
2778 EAPI int widget_service_get_instance_count(const char *widget_id, const char *cluster, const char *category)
2782 if (!_is_widget_feature_enabled()) {
2783 _E("Not supported");
2784 return WIDGET_ERROR_NOT_SUPPORTED;
2787 if (widget_id == NULL) {
2788 _E("Invalid parameter");
2789 return WIDGET_ERROR_INVALID_PARAMETER;
2792 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2793 return WIDGET_ERROR_PERMISSION_DENIED;
2795 ret = aul_widget_instance_count(widget_id);
2796 if (ret == AUL_R_EILLACC)
2797 return WIDGET_ERROR_PERMISSION_DENIED;
2802 EAPI int widget_service_set_widget_disabled(const char *widget_id,
2807 if (!_is_widget_feature_enabled()) {
2808 _E("Not supported");
2809 return WIDGET_ERROR_NOT_SUPPORTED;
2812 if (widget_id == NULL) {
2813 _E("Invalid parameter");
2814 return WIDGET_ERROR_INVALID_PARAMETER;
2817 ret = aul_widget_service_set_disable(widget_id, disabled);
2819 _E("set disable error : %d", ret);
2822 ret = WIDGET_ERROR_IO_ERROR;
2825 ret = WIDGET_ERROR_PERMISSION_DENIED;
2828 ret = WIDGET_ERROR_FAULT;
2835 static int _get_disable(const char *widget_id, bool *is_disabled, uid_t uid)
2843 query = sqlite3_mprintf("SELECT is_disable FROM widget_class"
2844 " WHERE classid = %Q", widget_id);
2848 _E("_get_disable db null");
2849 sqlite3_free(query);
2850 return WIDGET_ERROR_IO_ERROR;
2853 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
2854 if (ret != SQLITE_OK) {
2855 _E("prepare error: %s", sqlite3_errmsg(db));
2856 sqlite3_free(query);
2857 sqlite3_close_v2(db);
2858 return WIDGET_ERROR_FAULT;
2861 ret = sqlite3_step(stmt);
2862 if (ret != SQLITE_ROW) {
2863 sqlite3_free(query);
2864 sqlite3_finalize(stmt);
2865 sqlite3_close_v2(db);
2866 if (ret == SQLITE_DONE) {
2867 return WIDGET_ERROR_NOT_EXIST;
2869 _E("step error: %s, %d", sqlite3_errmsg(db), ret);
2870 return WIDGET_ERROR_FAULT;
2873 _get_column_int(stmt, 0, &disable);
2875 *is_disabled = (bool)disable;
2877 sqlite3_free(query);
2878 sqlite3_finalize(stmt);
2879 sqlite3_close_v2(db);
2881 return WIDGET_ERROR_NONE;
2884 EAPI int widget_service_get_widget_disabled(const char *widget_id,
2889 if (!_is_widget_feature_enabled()) {
2890 _E("Not supported");
2891 return WIDGET_ERROR_NOT_SUPPORTED;
2894 if (widget_id == NULL || is_disabled == NULL) {
2895 _E("Invalid parameter");
2896 return WIDGET_ERROR_INVALID_PARAMETER;
2899 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2900 return WIDGET_ERROR_PERMISSION_DENIED;
2902 ret = _get_disable(widget_id, is_disabled, getuid());
2903 if (ret != WIDGET_ERROR_NONE && !_is_global(getuid())) {
2904 ret = _get_disable(widget_id, is_disabled, GLOBALAPP_USER);
2906 _E("failed to get disable");
2912 static void __notify_disable(const char *widget_id, bool is_disabled)
2914 _disable_event_cb(widget_id, is_disabled, _disable_user_data);
2917 static int __disable_cb(const char *endpoint, aul_app_com_result_e e,
2918 bundle *widget_info, void *user_data)
2920 char *disable = NULL;
2921 char *widget_id = NULL;
2923 bundle_get_str(widget_info, AUL_K_WIDGET_DISABLE, &disable);
2924 bundle_get_str(widget_info, AUL_K_WIDGET_ID, &widget_id);
2926 __notify_disable(widget_id, (bool)atoi(disable));
2928 return WIDGET_ERROR_NONE;
2931 EAPI int widget_service_set_disable_event_cb(widget_disable_event_cb callback,
2934 if (callback == NULL) {
2935 _E("invalid parameter");
2936 return WIDGET_ERROR_INVALID_PARAMETER;
2939 if (!_is_widget_feature_enabled()) {
2940 _E("Not supported");
2941 return WIDGET_ERROR_NOT_SUPPORTED;
2944 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2945 return WIDGET_ERROR_PERMISSION_DENIED;
2947 if (aul_app_com_create(DISABLE_ENDPOINT, NULL,
2948 __disable_cb, NULL, &_conn) < 0) {
2949 _E("failed to create app com endpoint");
2950 return WIDGET_ERROR_IO_ERROR;
2953 _disable_event_cb = callback;
2954 _disable_user_data = user_data;
2956 return WIDGET_ERROR_NONE;
2959 EAPI int widget_service_unset_disable_event_cb(void)
2961 if (!_is_widget_feature_enabled()) {
2962 _E("not supported");
2963 return WIDGET_ERROR_NOT_SUPPORTED;
2966 if (check_privilege("http://tizen.org/privilege/widget.viewer") < 0)
2967 return WIDGET_ERROR_PERMISSION_DENIED;
2970 if (aul_app_com_leave(_conn) < 0) {
2971 _E("failed to leave app com disable");
2972 return WIDGET_ERROR_IO_ERROR;
2976 return WIDGET_ERROR_IO_ERROR;
2979 if (_disable_event_cb)
2980 _disable_event_cb = NULL;
2982 if (_disable_user_data)
2983 _disable_user_data = NULL;
2985 return WIDGET_ERROR_NONE;