diff options
author | Hyunho Kang <hhstark.kang@samsung.com> | 2016-01-21 21:28:03 +0900 |
---|---|---|
committer | Hyunho Kang <hhstark.kang@samsung.com> | 2016-02-29 20:55:34 +0900 |
commit | 9aa2bf03acb3ef7bb53cc06ad5eb508bb4d5ad1f (patch) | |
tree | 61b55ea7ddadb6bc0a6a82b7eea9790f6bfe2dfa | |
parent | 1dd68ec6880c42239299dd17bfdb1e9af083aa86 (diff) | |
download | badge-9aa2bf03acb3ef7bb53cc06ad5eb508bb4d5ad1f.tar.gz badge-9aa2bf03acb3ef7bb53cc06ad5eb508bb4d5ad1f.tar.bz2 badge-9aa2bf03acb3ef7bb53cc06ad5eb508bb4d5ad1f.zip |
Using gdbus for IPC instead of com-core packagetizen_3.0_dbus
Change-Id: I6034279fa6730d6ceed6414aa04cdcbbc7a3d1c6
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
Signed-off-by: jusung son <jusung07.son@samsung.com>
-rwxr-xr-x | CMakeLists.txt | 13 | ||||
-rwxr-xr-x | badge-service.pc.in | 11 | ||||
-rwxr-xr-x | include/badge_db.h | 6 | ||||
-rwxr-xr-x | include/badge_internal.h | 37 | ||||
-rwxr-xr-x | include/badge_ipc.h | 4 | ||||
-rwxr-xr-x | packaging/badge.spec | 12 | ||||
-rwxr-xr-x | src/badge.c | 19 | ||||
-rwxr-xr-x | src/badge_db.c | 24 | ||||
-rw-r--r-- | src/badge_error.c | 46 | ||||
-rwxr-xr-x | src/badge_internal.c | 18 | ||||
-rwxr-xr-x | src/badge_ipc.c | 849 |
11 files changed, 523 insertions, 516 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 1005d2b..3bcb933 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(badge C) SET(EXEC_PREFIX "\${prefix}") +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) SET(PROJECT_NAME "${PROJECT_NAME}") SET(LIBDIR ${LIB_INSTALL_DIR}) SET(INCLUDEDIR "\${prefix}/include/${PROJECT_NAME}") @@ -14,15 +15,13 @@ SET(INSTALL_HEADERS_DEVEL badge.h badge_error.h badge_internal.h -) - -SET(INSTALL_HEADERS_SERVICE_DEVEL badge_setting_service.h badge_db.h ) SET(SRCS src/badge.c + src/badge_error.c src/badge_setting.c src/badge_db.c src/badge_ipc.c @@ -42,7 +41,6 @@ pkg_check_modules(pkgs REQUIRED dbus-1 dbus-glib-1 vconf - com-core capi-appfw-package-manager db-util libtzplatform-config @@ -71,14 +69,7 @@ CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY) SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.pc") INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) -CONFIGURE_FILE("${PROJECT_NAME}-service.pc.in" "${PROJECT_NAME}-service.pc" @ONLY) -SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}-service.pc") -INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-service.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) - FOREACH(hfile ${INSTALL_HEADERS_DEVEL}) INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/${hfile} DESTINATION include/${PROJECT_NAME}) ENDFOREACH(hfile) -FOREACH(hfile ${INSTALL_HEADERS_SERVICE_DEVEL}) - INSTALL(FILES ${CMAKE_SOURCE_DIR}/include/${hfile} DESTINATION include/${PROJECT_NAME}/service) -ENDFOREACH(hfile) diff --git a/badge-service.pc.in b/badge-service.pc.in deleted file mode 100755 index 7864b50..0000000 --- a/badge-service.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@PREFIX@ -exec_prefix=@EXEC_PREFIX@ -libdir=@LIBDIR@ -includedir=@INCLUDEDIR@/service - -Name: badge -Description: Badge supporting library -Version: @VERSION@ -Requires: -Libs: -L${libdir} -lbadge -Cflags: -I${includedir} diff --git a/include/badge_db.h b/include/badge_db.h index 28d30ed..e58a2d8 100755 --- a/include/badge_db.h +++ b/include/badge_db.h @@ -34,8 +34,10 @@ extern "C" { int badge_db_insert(const char *pkgname, const char *writable_pkg, const char *caller); int badge_db_delete(const char *pkgname, const char *caller_pkg); -int badge_db_set_count(const char *pkgname, const char *caller_pkg, int count); -int badge_db_set_display_option(const char *pkgname, const char *caller_pkg, int is_display); +int badge_db_set_count(const char *pkgname, const char *caller_pkg, unsigned int count); +int badge_db_get_count(const char *pkgname, unsigned int *count); +int badge_db_set_display_option(const char *pkgname, const char *caller_pkg, unsigned int is_display); +int badge_db_get_display_option(const char *pkgname, unsigned int *is_display); int badge_db_exec(sqlite3 * db, const char *query, int *num_changes); #ifdef __cplusplus diff --git a/include/badge_internal.h b/include/badge_internal.h index 8b9509a..06e110b 100755 --- a/include/badge_internal.h +++ b/include/badge_internal.h @@ -25,6 +25,7 @@ #include <stdbool.h> #include <stdarg.h> +#include <glib.h> #include "badge_error.h" #include "badge.h" @@ -37,6 +38,8 @@ #define DEPRECATED __attribute__((deprecated)) #endif +#define BADGE_ERROR (badge_error_quark ()) +GQuark badge_error_quark(void); #ifdef __cplusplus extern "C" { @@ -214,40 +217,6 @@ int badge_setting_property_set(const char *pkgname, const char *property, const */ int badge_setting_property_get(const char *pkgname, const char *property, char **value); -/** - * @internal - * @brief Called to retrieve the badge existence. - * @since_tizen 2.4 - * @param[in] app_id The name of the application - * @param[in] count The count of the badge - * @param[in] user_data The user data passed from the foreach function - * @pre badge_foreach_existed() will invoke this callback. - * @see badge_foreach_existed() - */ -typedef void (*badge_cb)(const char *app_id, unsigned int count, void *user_data) DEPRECATED; - - -/** - * @internal - * @brief Retrieves all existing badges. - * @since_tizen 2.4 - * @privlevel public - * @privilege %http://tizen.org/privilege/notification - * @param[in] callback The callback function - * @param[in] user_data The user data to be passed to the callback function - * @return #BADGE_ERROR_NONE if success, other value if failure - * @retval BADGE_ERROR_NONE Success - * @retval BADGE_ERROR_INVALID_PARAMETER Invalid parameter - * @retval BADGE_ERROR_PERMISSION_DENIED The application does not have the privilege to call this method - * @retval BADGE_ERROR_FROM_DB Error form DB - * @retval BADGE_ERROR_OUT_OF_MEMORY Out of memory - * @retval BADGE_ERROR_NOT_EXIST Not exist - * @see #badge_error_e - * @see badge_get_count() - * @see badge_is_existing() - */ -int badge_foreach_existed(badge_cb callback, void *user_data) DEPRECATED; - #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/include/badge_ipc.h b/include/badge_ipc.h index cca3fcf..cb23ad5 100755 --- a/include/badge_ipc.h +++ b/include/badge_ipc.h @@ -39,7 +39,9 @@ int badge_ipc_monitor_fini(void); int badge_ipc_request_insert(const char *pkgname, const char *writable_pkg, const char *caller); int badge_ipc_request_delete(const char *pkgname, const char *caller); int badge_ipc_request_set_count(const char *pkgname, const char *caller, int count); -int badge_ipc_request_set_display(const char *pkgname, const char *caller, int display_option); +int badge_ipc_request_get_count(const char *pkgname, unsigned int *count); +int badge_ipc_request_set_display(const char *pkgname, const char *caller, unsigned int display_option); +int badge_ipc_request_get_display(const char *pkgname, unsigned int *is_display); int badge_ipc_is_master_ready(void); int badge_ipc_add_deferred_task(void (*badge_add_deferred_task)(void *data), void *user_data); diff --git a/packaging/badge.spec b/packaging/badge.spec index 76ad5b0..1260287 100755 --- a/packaging/badge.spec +++ b/packaging/badge.spec @@ -10,7 +10,6 @@ BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(dbus-glib-1) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(vconf) -BuildRequires: pkgconfig(com-core) BuildRequires: pkgconfig(capi-appfw-package-manager) BuildRequires: pkgconfig(db-util) BuildRequires: pkgconfig(libtzplatform-config) @@ -99,16 +98,15 @@ chmod 644 %{TZ_SYS_DB}/.%{name}.db-journal %defattr(-,root,root,-) %{_libdir}/libbadge.so* %{_datarootdir}/license/%{name} +%{_includedir}/badge/badge.h +%{_includedir}/badge/badge_error.h +%{_includedir}/badge/badge_internal.h %files devel %defattr(-,root,root,-) %{_includedir}/badge/badge.h %{_includedir}/badge/badge_error.h %{_includedir}/badge/badge_internal.h +%{_includedir}/badge/badge_db.h +%{_includedir}/badge/badge_setting_service.h %{_libdir}/pkgconfig/%{name}.pc - -%files service-devel -%defattr(-,root,root,-) -%{_includedir}/badge/service/badge_db.h -%{_includedir}/badge/service/badge_setting_service.h -%{_libdir}/pkgconfig/badge-service.pc diff --git a/src/badge.c b/src/badge.c index 3ee7ce5..eb47465 100755 --- a/src/badge.c +++ b/src/badge.c @@ -148,13 +148,6 @@ int badge_is_existing(const char *app_id, bool *existing) return _badge_is_existing(app_id, existing); } - -EXPORT_API -int badge_foreach_existed(badge_cb callback, void *data) -{ - return _badge_foreach_existed((badge_foreach_cb)callback, data); -} - EXPORT_API int badge_foreach(badge_foreach_cb callback, void *user_data) { @@ -171,6 +164,8 @@ int badge_set_count(const char *app_id, unsigned int count) if (app_id == NULL) return BADGE_ERROR_INVALID_PARAMETER; + DBG("app_id %s, count %d", app_id, count); + result = _badge_is_existing(app_id, &existing); if (result != BADGE_ERROR_NONE) { @@ -201,7 +196,10 @@ out: EXPORT_API int badge_get_count(const char *app_id, unsigned int *count) { - return _badget_get_count(app_id, count); + if (app_id == NULL || count == NULL) + return BADGE_ERROR_INVALID_PARAMETER; + + return badge_ipc_request_get_count(app_id, count); } EXPORT_API @@ -245,7 +243,10 @@ out: EXPORT_API int badge_get_display(const char *app_id, unsigned int *is_display) { - return _badget_get_display(app_id, is_display); + if (app_id == NULL || is_display == NULL) + return BADGE_ERROR_INVALID_PARAMETER; + + return badge_ipc_request_get_display(app_id, is_display); } EXPORT_API diff --git a/src/badge_db.c b/src/badge_db.c index 84d9bc6..be21135 100755 --- a/src/badge_db.c +++ b/src/badge_db.c @@ -80,7 +80,7 @@ int badge_db_delete(const char *pkgname, const char *caller) } EXPORT_API -int badge_db_set_count(const char *pkgname, const char *caller, int count) +int badge_db_set_count(const char *pkgname, const char *caller, unsigned int count) { int result = BADGE_ERROR_NONE; @@ -90,7 +90,17 @@ int badge_db_set_count(const char *pkgname, const char *caller, int count) } EXPORT_API -int badge_db_set_display_option(const char *pkgname, const char *caller, int is_display) +int badge_db_get_count(const char *pkgname, unsigned int *count) +{ + int result = BADGE_ERROR_NONE; + + result = _badget_get_count(pkgname, count); + + return result; +} + +EXPORT_API +int badge_db_set_display_option(const char *pkgname, const char *caller, unsigned int is_display) { int result = BADGE_ERROR_NONE; @@ -100,6 +110,16 @@ int badge_db_set_display_option(const char *pkgname, const char *caller, int is_ } EXPORT_API +int badge_db_get_display_option(const char *pkgname, unsigned int *is_display) +{ + int result = BADGE_ERROR_NONE; + + result = _badget_get_display(pkgname, is_display); + + return result; +} + +EXPORT_API int badge_db_exec(sqlite3 * db, const char *query, int *num_changes) { int ret = 0; diff --git a/src/badge_error.c b/src/badge_error.c new file mode 100644 index 0000000..1f3008a --- /dev/null +++ b/src/badge_error.c @@ -0,0 +1,46 @@ +/* + * libbadge + * + * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include <gio/gio.h> +#include "badge_error.h" + +static const GDBusErrorEntry dbus_error_entries[] = +{ + {BADGE_ERROR_INVALID_PARAMETER, "org.freedesktop.Badge.Error.INVALID_PARAMETER"}, + {BADGE_ERROR_OUT_OF_MEMORY, "org.freedesktop.Badge.Error.OUT_OF_MEMORY"}, + {BADGE_ERROR_IO_ERROR, "org.freedesktop.Badge.Error.IO_ERROR"}, + {BADGE_ERROR_PERMISSION_DENIED, "org.freedesktop.Badge.Error.PERMISSION_DENIED"}, + {BADGE_ERROR_FROM_DB, "org.freedesktop.Badge.Error.FROM_DB"}, + {BADGE_ERROR_ALREADY_EXIST, "org.freedesktop.Badge.Error.ALREADY_EXIST"}, + {BADGE_ERROR_FROM_DBUS, "org.freedesktop.Badge.Error.FROM_DBUS"}, + {BADGE_ERROR_NOT_EXIST, "org.freedesktop.Badge.Error.NOT_EXIST"}, + {BADGE_ERROR_SERVICE_NOT_READY, "org.freedesktop.Badge.Error.SERVICE_NOT_READY"}, + {BADGE_ERROR_INVALID_PACKAGE, "org.freedesktop.Badge.Error.INVALID_PACKAGE"}, +}; + +EXPORT_API GQuark badge_error_quark (void) +{ + static volatile gsize quark_volatile = 0; + g_dbus_error_register_error_domain ("badge-error-quark", + &quark_volatile, + dbus_error_entries, + G_N_ELEMENTS(dbus_error_entries)); + return (GQuark) quark_volatile; +} + diff --git a/src/badge_internal.c b/src/badge_internal.c index 4aad0f2..bc16153 100755 --- a/src/badge_internal.c +++ b/src/badge_internal.c @@ -753,6 +753,7 @@ int _badget_set_display(const char *pkgname, result = ret; goto return_close_db; } + } else if (ret == BADGE_ERROR_NOT_EXIST) { sqlbuf = sqlite3_mprintf("INSERT INTO %q " \ "(pkgname, " \ @@ -863,6 +864,7 @@ return_close_db: void badge_changed_cb_call(unsigned int action, const char *pkgname, unsigned int count) { + DBG("call badge_change_cb"); GList *list = g_badge_cb_list; struct _badge_cb_data *bd = NULL; @@ -871,16 +873,19 @@ void badge_changed_cb_call(unsigned int action, const char *pkgname, if (!bd) continue; - if (bd->callback) + if (bd->callback) { + DBG("call badge_change_cb : action %d, pkgname %s, count %d", + action, pkgname, count); bd->callback(action, pkgname, count, bd->data); + } list = g_list_next(list); } } -static void _badge_changed_monitor_init() +static int _badge_changed_monitor_init() { - badge_ipc_monitor_init(); + return badge_ipc_monitor_init(); } static void _badge_chanaged_monitor_fini() @@ -906,11 +911,16 @@ int _badge_register_changed_cb(badge_change_cb callback, void *data) { struct _badge_cb_data *bd = NULL; GList *found = NULL; + int ret; if (!callback) return BADGE_ERROR_INVALID_PARAMETER; - _badge_changed_monitor_init(); + ret = _badge_changed_monitor_init(); + if (ret != BADGE_ERROR_NONE) { + ERR("badge_ipc_monitor_init err : %d", ret); + return ret; + } found = g_list_find_custom(g_badge_cb_list, (gconstpointer)callback, _badge_data_compare); diff --git a/src/badge_ipc.c b/src/badge_ipc.c index 5e44de5..7e2a4e9 100755 --- a/src/badge_ipc.c +++ b/src/badge_ipc.c @@ -22,44 +22,26 @@ #include <stdlib.h> #include <stdarg.h> +#include <gio/gio.h> #include <vconf.h> -#include <packet.h> -#include <com-core.h> -#include <com-core_packet.h> - #include "badge.h" #include "badge_log.h" #include "badge_error.h" #include "badge_internal.h" #include "badge_ipc.h" -#define BADGE_IPC_TIMEOUT 1.0 - -#if !defined(VCONFKEY_MASTER_STARTED) -#define VCONFKEY_MASTER_STARTED "memory/data-provider-master/started" -#endif - -static struct info { - int server_fd; - int client_fd; - const char *socket_file; - struct { - int (*request_cb)(const char *appid, const char *name, int type, const char *content, const char *icon, pid_t pid, double period, int allow_duplicate, void *data); - void *data; - } server_cb; - int initialized; - int is_started_cb_set_svc; - int is_started_cb_set_task; -} s_info = { - .server_fd = -1, - .client_fd = -1, - .socket_file = BADGE_ADDR, - .initialized = 0, - .is_started_cb_set_svc = 0, - .is_started_cb_set_task = 0, -}; + +#define PROVIDER_BUS_NAME "org.tizen.data_provider_service" +#define PROVIDER_OBJECT_PATH "/org/tizen/data_provider_service" +#define PROVIDER_BADGE_INTERFACE_NAME "org.tizen.data_provider_badge_service" + + +#define DBUS_SERVICE_DBUS "org.freedesktop.DBus" +#define DBUS_PATH_DBUS "/org/freedesktop/DBus" +#define DBUS_INTERFACE_DBUS "org.freedesktop.DBus" + typedef struct _task_list task_list; struct _task_list { @@ -69,47 +51,56 @@ struct _task_list { void (*task_cb) (void *data); void *data; }; - static task_list *g_task_list; -static int badge_ipc_monitor_register(void); -static int badge_ipc_monitor_deregister(void); -static void _do_deferred_task(void); -static void _master_started_cb_task(keynode_t *node, void *data); - -/*! - * functions to check state of master - */ -static inline void _set_master_started_cb(vconf_callback_fn cb) -{ - int ret = -1; - - ret = vconf_notify_key_changed(VCONFKEY_MASTER_STARTED, cb, NULL); - if (ret != 0) - ERR("failed to notify key(%s) : %d", VCONFKEY_MASTER_STARTED, ret); -} - -static inline void _unset_master_started_cb(vconf_callback_fn cb) -{ - int ret = -1; +static GDBusConnection *_gdbus_conn = NULL; +static int monitor_id = 0; +static int provider_monitor_id = 0; +static int is_master_started = 0; - ret = vconf_ignore_key_changed(VCONFKEY_MASTER_STARTED, cb); - if (ret != 0) - ERR("failed to notify key(%s) : %d", VCONFKEY_MASTER_STARTED, ret); -} +static void _do_deferred_task(void); int badge_ipc_is_master_ready(void) { - int ret = -1, is_master_started = 0; - - ret = vconf_get_bool(VCONFKEY_MASTER_STARTED, &is_master_started); - if (ret == 0 && is_master_started == 1) { - ERR("the master has been started"); - } else { + GVariant *result = NULL; + GError *err = NULL; + gboolean name_exist; + + result = g_dbus_connection_call_sync( + _gdbus_conn, + DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, + DBUS_INTERFACE_DBUS, + "NameHasOwner", + g_variant_new("(s)", PROVIDER_BUS_NAME), + G_VARIANT_TYPE("(b)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &err); + + if (err || (result == NULL)) { + if (err) { + ERR("No reply. error = %s", err->message); + g_error_free(err); + } is_master_started = 0; - ERR("the master has been stopped"); + } else { + g_variant_get(result, "(b)", &name_exist); + + if (!name_exist) { + ERR("Name not exist %s", PROVIDER_BUS_NAME); + ERR("the master has been stopped"); + is_master_started = 0; + } else { + DBG("the master has been started"); + is_master_started = 1; + } } + if(result) + g_variant_unref(result); + return is_master_started; } @@ -117,19 +108,14 @@ int badge_ipc_add_deferred_task( void (*badge_add_deferred_task)(void *data), void *user_data) { - task_list *list = NULL; - task_list *list_new = NULL; + task_list *list; + task_list *list_new; list_new = (task_list *) malloc(sizeof(task_list)); if (list_new == NULL) return BADGE_ERROR_OUT_OF_MEMORY; - if (s_info.is_started_cb_set_task == 0) { - _set_master_started_cb(_master_started_cb_task); - s_info.is_started_cb_set_task = 1; - } - list_new->next = NULL; list_new->prev = NULL; @@ -153,9 +139,9 @@ int badge_ipc_add_deferred_task( int badge_ipc_del_deferred_task( void (*badge_add_deferred_task)(void *data)) { - task_list *list_del = NULL; - task_list *list_prev = NULL; - task_list *list_next = NULL; + task_list *list_del; + task_list *list_prev; + task_list *list_next; list_del = g_task_list; @@ -184,14 +170,6 @@ int badge_ipc_del_deferred_task( } free(list_del); - - if (g_task_list == NULL) { - if (s_info.is_started_cb_set_task == 1) { - _unset_master_started_cb(_master_started_cb_task); - s_info.is_started_cb_set_task = 0; - } - } - return BADGE_ERROR_NONE; } list_del = list_del->next; @@ -202,18 +180,14 @@ int badge_ipc_del_deferred_task( static void _do_deferred_task(void) { - task_list *list_do = NULL; - task_list *list_temp = NULL; + task_list *list_do; + task_list *list_temp; if (g_task_list == NULL) return; list_do = g_task_list; g_task_list = NULL; - if (s_info.is_started_cb_set_task == 1) { - _unset_master_started_cb(_master_started_cb_task); - s_info.is_started_cb_set_task = 0; - } while (list_do->prev != NULL) list_do = list_do->prev; @@ -229,460 +203,465 @@ static void _do_deferred_task(void) } } -static void _master_started_cb_service(keynode_t *node, void *data) +/* + * dbus handler implementation + */ +static void _insert_badge_notify(GVariant *parameters) { - int ret = BADGE_ERROR_NONE; - - if (badge_ipc_is_master_ready()) { - ERR("try to register a badge service"); - ret = badge_ipc_monitor_deregister(); - if (ret != BADGE_ERROR_NONE) - ERR("failed to deregister a monitor"); - - ret = badge_ipc_monitor_register(); - if (ret != BADGE_ERROR_NONE) - ERR("failed to register a monitor"); - - } else { - ERR("try to unregister a badge service"); - ret = badge_ipc_monitor_deregister(); - if (ret != BADGE_ERROR_NONE) - ERR("failed to deregister a monitor"); + char *pkgname = NULL; - } + g_variant_get(parameters, "(&s)", &pkgname); + badge_changed_cb_call(BADGE_ACTION_CREATE, pkgname, 0); } -static void _master_started_cb_task(keynode_t *node, void *data) +static void _delete_badge_notify(GVariant *parameters) { - if (badge_ipc_is_master_ready()) - _do_deferred_task(); + char *pkgname = NULL; + + g_variant_get(parameters, "(&s)", &pkgname); + badge_changed_cb_call(BADGE_ACTION_REMOVE, pkgname, 0); } -/*! - * functions to handler services - */ -static struct packet *_handler_insert_badge(pid_t pid, int handle, const struct packet *packet) +static void _set_badge_count_notify(GVariant *parameters) { - int ret = 0; char *pkgname = NULL; + int count = 0; - if (!packet) { - ERR("a packet is null"); - return NULL; - } - - DBG(""); - - /* return code, pkgname */ - if (packet_get(packet, "is", &ret, &pkgname) == 2) { - if (ret == BADGE_ERROR_NONE) - badge_changed_cb_call(BADGE_ACTION_CREATE, pkgname, 0); - else - ERR("failed to insert a new badge:%d", ret); + g_variant_get(parameters, "(&si)", &pkgname, &count); + badge_changed_cb_call(BADGE_ACTION_UPDATE, pkgname, count); +} - } else { - ERR("failed to get data from a packet"); - } +static void _set_disp_option_notify(GVariant *parameters) +{ + char *pkgname = NULL; + int is_display = 0; - return NULL; + g_variant_get(parameters, "(&si)", &pkgname, &is_display); + badge_changed_cb_call(BADGE_ACTION_CHANGED_DISPLAY, pkgname, is_display); } -static struct packet *_handler_delete_badge(pid_t pid, int handle, const struct packet *packet) +static void _handle_badge_notify(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) { - int ret = 0; - char *pkgname = NULL; + DBG("signal_name: %s", signal_name); + if (g_strcmp0(signal_name, "insert_badge_notify") == 0) + _insert_badge_notify(parameters); + else if (g_strcmp0(signal_name, "delete_badge_notify") == 0) + _delete_badge_notify(parameters); + else if (g_strcmp0(signal_name, "set_badge_count_notify") == 0) + _set_badge_count_notify(parameters); + else if (g_strcmp0(signal_name, "set_disp_option_notify") == 0) + _set_disp_option_notify(parameters); +} - if (!packet) { - ERR("a packet is null"); - return NULL; +static int _dbus_init(void) +{ + GError *error = NULL; + + if (_gdbus_conn == NULL) { + _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (_gdbus_conn == NULL) { + if (error != NULL) { + ERR("Failed to get dbus [%s]", error->message); + g_error_free(error); + } + return BADGE_ERROR_IO_ERROR; + } + badge_error_quark(); } - DBG(""); - - if (packet_get(packet, "is", &ret, &pkgname) == 2) { - if (ret == BADGE_ERROR_NONE) - badge_changed_cb_call(BADGE_ACTION_REMOVE, pkgname, 0); - else - ERR("failed to remove a badge:%d", ret); + return BADGE_ERROR_NONE; +} - } else { - ERR("failed to get data from a packet"); +static int _dbus_signal_init() +{ + int ret = BADGE_ERROR_NONE; + int id; + + if (monitor_id == 0) { + DBG("get dbus connection success"); + id = g_dbus_connection_signal_subscribe(_gdbus_conn, + PROVIDER_BUS_NAME, + PROVIDER_BADGE_INTERFACE_NAME, /* interface */ + NULL, /* member */ + PROVIDER_OBJECT_PATH, /* path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + _handle_badge_notify, + NULL, + NULL); + + DBG("subscribe id : %d", id); + if (id == 0) { + ret = BADGE_ERROR_IO_ERROR; + ERR("Failed to _register_noti_dbus_interface"); + } else { + monitor_id = id; + } } - return NULL; + return ret; } -static struct packet *_handler_set_badge_count(pid_t pid, int handle, const struct packet *packet) +/* + * implement user request + */ +static int _send_sync_badge(GVariant *body, GDBusMessage **reply, char *cmd) { - int ret = 0; - char *pkgname = NULL; - int count = 0; + GError *err = NULL; + GDBusMessage *msg; + int ret = BADGE_ERROR_NONE; - if (!packet) { - ERR("a packet is null"); - return NULL; + msg = g_dbus_message_new_method_call( + PROVIDER_BUS_NAME, + PROVIDER_OBJECT_PATH, + PROVIDER_BADGE_INTERFACE_NAME, + cmd); + if (!msg) { + ERR("Can't allocate new method call"); + if (body) + g_variant_unref(body); + return BADGE_ERROR_OUT_OF_MEMORY; } - DBG(""); + if (body != NULL) + g_dbus_message_set_body(msg, body); - if (packet_get(packet, "isi", &ret, &pkgname, &count) == 3) { - if (ret == BADGE_ERROR_NONE) - badge_changed_cb_call(BADGE_ACTION_UPDATE, pkgname, count); - else - ERR("failed to update count of badge:%d", ret); + *reply = g_dbus_connection_send_message_with_reply_sync( + _gdbus_conn, + msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, + -1, + NULL, + NULL, + &err); - } else { - ERR("failed to get data from a packet"); + g_object_unref(msg); + + if (!*reply) { + if (err != NULL) { + ERR("No reply. cmd = %s, error = %s", cmd, err->message); + g_error_free(err); + } + return BADGE_ERROR_SERVICE_NOT_READY; + } + + if (g_dbus_message_to_gerror(*reply, &err)) { + ret = err->code; + ERR("_send_sync_badge cmd = %s, error %s", cmd, err->message); + g_error_free(err); + return ret; } + ERR("_send_sync_badge done !!"); + return BADGE_ERROR_NONE; - return NULL; } -static struct packet *_handler_set_display_option(pid_t pid, int handle, const struct packet *packet) +static int _send_service_register() { - int ret = 0; - char *pkgname = NULL; - int is_display = 0; + GDBusMessage *reply = NULL; + int result; - if (!packet) { - ERR("a packet is null"); - return NULL; - } + result = _send_sync_badge(NULL, &reply, "badge_service_register"); - DBG(""); + if(reply) + g_object_unref(reply); - if (packet_get(packet, "isi", &ret, &pkgname, &is_display) == 3) { - if (ret == BADGE_ERROR_NONE) - badge_changed_cb_call(BADGE_ACTION_CHANGED_DISPLAY, pkgname, is_display); - else - ERR("failed to update the display option of badge:%d, %d", ret, is_display); + DBG("_send_service_register dones"); + return result; +} - } else { - ERR("failed to get data from a packet"); - } +static int _ipc_monitor_register(void) +{ + DBG("register a service\n"); - return NULL; + return _send_service_register(); } -static int _handler_service_register(pid_t pid, int handle, const struct packet *packet, void *data) +static void _on_name_appeared(GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) { - int ret; - - DBG(""); + DBG("name appeared : %s", name); + is_master_started = 1; + _ipc_monitor_register(); - if (!packet) { - ERR("Packet is not valid\n"); - ret = BADGE_ERROR_INVALID_PARAMETER; - } else if (packet_get(packet, "i", &ret) != 1) { - ERR("Packet is not valid\n"); - ret = BADGE_ERROR_INVALID_PARAMETER; - } else { - if (ret == BADGE_ERROR_NONE) - badge_changed_cb_call(BADGE_ACTION_SERVICE_READY, NULL, 0); + _do_deferred_task(); +} - } - return ret; +static void _on_name_vanished(GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + DBG("name vanished : %s", name); + is_master_started = 0; } -/*! - * functions to initialize and register a monitor - */ -static int badge_ipc_monitor_register(void) +int badge_ipc_monitor_init(void) { - int ret; - struct packet *packet; - static struct method service_table[] = { - { - .cmd = "insert_badge", - .handler = _handler_insert_badge, - }, - { - .cmd = "delete_badge", - .handler = _handler_delete_badge, - }, - { - .cmd = "set_badge_count", - .handler = _handler_set_badge_count, - }, - { - .cmd = "set_disp_option", - .handler = _handler_set_display_option, - }, - { - .cmd = NULL, - .handler = NULL, - }, - }; - - if (s_info.initialized == 1) - return BADGE_ERROR_NONE; - else - s_info.initialized = 1; - - ERR("register a service\n"); - com_core_packet_use_thread(1); - - s_info.server_fd = com_core_packet_client_init(s_info.socket_file, 0, service_table); - if (s_info.server_fd < 0) { - ERR("Failed to make a connection to the master\n"); - return BADGE_ERROR_IO_ERROR; + DBG("register a service\n"); + int ret = BADGE_ERROR_NONE; + + ret = _dbus_init(); + if (ret != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", ret); + return ret; } - packet = packet_create("service_register", ""); - if (!packet) { - ERR("Failed to build a packet\n"); - return BADGE_ERROR_IO_ERROR; + ret = _dbus_signal_init(); + if (ret != BADGE_ERROR_NONE) { + ERR("Can't init dbus signal %d", ret); + return ret; } - ret = com_core_packet_async_send(s_info.server_fd, packet, 1.0, _handler_service_register, NULL); - DBG("Service register sent: %d\n", ret); - packet_destroy(packet); - if (ret != 0) { - com_core_packet_client_fini(s_info.server_fd); - s_info.server_fd = BADGE_ERROR_INVALID_PARAMETER; - ret = BADGE_ERROR_IO_ERROR; - } else { - ret = BADGE_ERROR_NONE; + ret = _ipc_monitor_register(); + if (ret != BADGE_ERROR_NONE) { + ERR("Can't init ipc_monitor_register %d", ret); + return ret; + } + + if (provider_monitor_id == 0) { + provider_monitor_id = g_bus_watch_name_on_connection( + _gdbus_conn, + PROVIDER_BUS_NAME, + G_BUS_NAME_WATCHER_FLAGS_NONE, + _on_name_appeared, + _on_name_vanished, + NULL, + NULL); + + if (provider_monitor_id == 0) { + ERR("watch on name fail"); + g_dbus_connection_signal_unsubscribe(_gdbus_conn, monitor_id); + monitor_id = 0; + return BADGE_ERROR_IO_ERROR; + } } - DBG("Server FD: %d\n", s_info.server_fd); return ret; } -int badge_ipc_monitor_deregister(void) +int badge_ipc_monitor_fini(void) { - if (s_info.initialized == 0) - return BADGE_ERROR_NONE; - - com_core_packet_client_fini(s_info.server_fd); - s_info.server_fd = BADGE_ERROR_INVALID_PARAMETER; - - s_info.initialized = 0; + if (provider_monitor_id) { + g_bus_unwatch_name(provider_monitor_id); + provider_monitor_id = 0; + } + if (monitor_id) { + g_dbus_connection_signal_unsubscribe(_gdbus_conn, monitor_id); + monitor_id = 0; + } return BADGE_ERROR_NONE; } -int badge_ipc_monitor_init(void) +int badge_ipc_request_insert(const char *pkgname, const char *writable_pkg, const char *caller) { - int ret = BADGE_ERROR_NONE; - - if (badge_ipc_is_master_ready()) - ret = badge_ipc_monitor_register(); + int result; + GDBusMessage *reply = NULL; + GVariant *body; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; + } - if (s_info.is_started_cb_set_svc == 0) { - _set_master_started_cb(_master_started_cb_service); - s_info.is_started_cb_set_svc = 1; + body = g_variant_new("(sss)", pkgname, writable_pkg, caller); + if (!body) { + ERR("Cannot create gvariant. Out of memory."); + return BADGE_ERROR_OUT_OF_MEMORY; } - return ret; + result = _send_sync_badge(body, &reply, "insert_badge"); + + if(reply) + g_object_unref(reply); + + DBG("badge_ipc_request_insert done [result: %d]", result); + return result; } -int badge_ipc_monitor_fini(void) +int badge_ipc_request_delete(const char *pkgname, const char *caller) { - int ret = BADGE_ERROR_NONE; - - if (s_info.is_started_cb_set_svc == 1) { - _unset_master_started_cb(_master_started_cb_service); - s_info.is_started_cb_set_svc = 0; + int result; + GDBusMessage *reply = NULL; + GVariant *body; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; } + body = g_variant_new("(ss)", pkgname, caller); - ret = badge_ipc_monitor_deregister(); + result = _send_sync_badge(body, &reply, "delete_badge"); - return ret; -} + if(reply) + g_object_unref(reply); + DBG("badge_ipc_request_delete done [result: %d]", result); + return result; +} -int badge_ipc_request_insert(const char *pkgname, const char *writable_pkg, const char *caller) +int badge_ipc_request_set_count(const char *pkgname, const char *caller, int count) { - int ret = 0; - struct packet *packet; - struct packet *result; - - packet = packet_create("insert_badge", "sss", pkgname, writable_pkg, caller); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "i", &ret) != 1) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } - - if (ret != BADGE_ERROR_NONE) { - packet_unref(result); - return ret; - } - packet_unref(result); - } else { - if (badge_ipc_is_master_ready() == 1) - return BADGE_ERROR_PERMISSION_DENIED; - else - return BADGE_ERROR_SERVICE_NOT_READY; + int result; + GDBusMessage *reply = NULL; + GVariant *body; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; } + body = g_variant_new("(ssi)", pkgname, caller, count); - return BADGE_ERROR_NONE; + result = _send_sync_badge(body, &reply, "set_badge_count"); + + if(reply) + g_object_unref(reply); + + DBG("badge_ipc_request_set_count done [result: %d]", result); + return result; } -int badge_ipc_request_delete(const char *pkgname, const char *caller) +int badge_ipc_request_get_count(const char *pkgname, unsigned int *count) { - int ret = 0; - struct packet *packet; - struct packet *result; - - packet = packet_create("delete_badge", "ss", pkgname, caller); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "i", &ret) != 1) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } + int result; + GDBusMessage *reply = NULL; + GVariant *body; + GVariant *reply_body; + int ret_count; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; + } + body = g_variant_new("(s)", pkgname); - if (ret != BADGE_ERROR_NONE) { - packet_unref(result); - return ret; - } - packet_unref(result); - } else { - if (badge_ipc_is_master_ready() == 1) - return BADGE_ERROR_PERMISSION_DENIED; - else - return BADGE_ERROR_SERVICE_NOT_READY; + result = _send_sync_badge(body, &reply, "get_badge_count"); + if (result == BADGE_ERROR_NONE) { + reply_body = g_dbus_message_get_body(reply); + g_variant_get(reply_body, "(i)", &ret_count); + *count = ret_count; } - return BADGE_ERROR_NONE; + if(reply) + g_object_unref(reply); + + DBG("badge_ipc_request_get_count done [result: %d]", result); + return result; } -int badge_ipc_request_set_count(const char *pkgname, const char *caller, int count) +int badge_ipc_request_set_display(const char *pkgname, const char *caller, unsigned int display_option) { - int ret = 0; - struct packet *packet; - struct packet *result; - - packet = packet_create("set_badge_count", "ssi", pkgname, caller, count); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "i", &ret) != 1) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } - - if (ret != BADGE_ERROR_NONE) { - packet_unref(result); - return ret; - } - packet_unref(result); - } else { - if (badge_ipc_is_master_ready() == 1) - return BADGE_ERROR_PERMISSION_DENIED; - else - return BADGE_ERROR_SERVICE_NOT_READY; + int result; + GDBusMessage *reply = NULL; + GVariant *body; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; } + body = g_variant_new("(ssi)", pkgname, caller, display_option); - return BADGE_ERROR_NONE; + result = _send_sync_badge(body, &reply, "set_disp_option"); + if(reply) + g_object_unref(reply); + + DBG("badge_ipc_request_set_display done [result: %d]", result); + return result; } -int badge_ipc_request_set_display(const char *pkgname, const char *caller, int display_option) +int badge_ipc_request_get_display(const char *pkgname, unsigned int *is_display) { - int ret = 0; - struct packet *packet; - struct packet *result; - - packet = packet_create("set_disp_option", "ssi", pkgname, caller, display_option); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "i", &ret) != 1) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } + int result ; + GDBusMessage *reply = NULL; + GVariant *body ; + GVariant *reply_body; + unsigned int ret_is_display; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; + } + body = g_variant_new("(s)", pkgname); - if (ret != BADGE_ERROR_NONE) { - packet_unref(result); - return ret; - } - packet_unref(result); - } else { - if (badge_ipc_is_master_ready() == 1) - return BADGE_ERROR_PERMISSION_DENIED; - else - return BADGE_ERROR_SERVICE_NOT_READY; + result = _send_sync_badge(body, &reply, "get_disp_option"); + if (result == BADGE_ERROR_NONE) { + reply_body = g_dbus_message_get_body(reply); + g_variant_get(reply_body, "(i)", &ret_is_display); + *is_display = ret_is_display; } - return BADGE_ERROR_NONE; + if(reply) + g_object_unref(reply); + + DBG("badge_ipc_request_get_display done [result: %d]", result); + return result; } int badge_ipc_setting_property_set(const char *pkgname, const char *property, const char *value) { - int status = 0; - int ret = 0; - struct packet *packet; - struct packet *result; - - packet = packet_create("set_noti_property", "sss", pkgname, property, value); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "ii", &status, &ret) != 2) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } - packet_unref(result); - } else { - ERR("failed to receive answer(delete)"); - return BADGE_ERROR_SERVICE_NOT_READY; + int result; + GDBusMessage *reply = NULL; + GVariant *body; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; } + body = g_variant_new("(sss)", pkgname, property, value); + + result = _send_sync_badge(body, &reply, "set_noti_property"); + if(reply) + g_object_unref(reply); - return status; + DBG("badge_ipc_setting_property_set done [result: %d]", result); + return result; } int badge_ipc_setting_property_get(const char *pkgname, const char *property, char **value) { - int status = 0; - char *ret = NULL; - struct packet *packet; - struct packet *result; - - packet = packet_create("get_noti_property", "ss", pkgname, property); - result = com_core_packet_oneshot_send(BADGE_ADDR, - packet, - BADGE_IPC_TIMEOUT); - packet_destroy(packet); - - if (result != NULL) { - if (packet_get(result, "is", &status, &ret) != 2) { - ERR("Failed to get a result packet"); - packet_unref(result); - return BADGE_ERROR_IO_ERROR; - } - if (status == BADGE_ERROR_NONE && ret != NULL) - *value = strdup(ret); + int result; + GDBusMessage *reply = NULL; + GVariant *body; + GVariant *reply_body; + char *ret_val = NULL; + + result = _dbus_init(); + if (result != BADGE_ERROR_NONE) { + ERR("Can't init dbus %d", result); + return result; + } + body = g_variant_new("(ss)", pkgname, property); - packet_unref(result); - } else { - ERR("failed to receive answer(delete)"); - return BADGE_ERROR_SERVICE_NOT_READY; + result = _send_sync_badge(body, &reply, "get_noti_property"); + if (result == BADGE_ERROR_NONE) { + reply_body = g_dbus_message_get_body(reply); + g_variant_get(reply_body, "(&s)", ret_val); + + if (ret_val != NULL) + *value = strdup(ret_val); + else + result = BADGE_ERROR_IO_ERROR; } + if(reply) + g_object_unref(reply); - return status; + DBG("badge_ipc_setting_property_get done [result: %d]", result); + return result; } + + |