Commit c8f9da4f authored by Slava Monich's avatar Slava Monich

[libmce-glib] Asynchronously initialize D-Bus proxies. Contributes to JB#37115

parent 1d9b7b23
......@@ -131,14 +131,36 @@ mce_display_status_query_done(
mce_display_unref(self);
}
static
void
mce_display_status_ind(
ComNokiaMceSignal* proxy,
const char* status,
gpointer arg)
{
GDEBUG("Display is %s", status);
mce_display_status_update(MCE_DISPLAY(arg), status);
}
static
void
mce_display_status_query(
MceDisplay* self)
{
MceProxy* proxy = self->priv->proxy;
if (proxy->valid) {
MceDisplayPriv* priv = self->priv;
MceProxy* proxy = priv->proxy;
/*
* proxy->signal and proxy->request may not be available at the
* time when MceDisplay is created. In that case we have to wait
* for the valid signal before we can connect the display state
* signal and submit the initial query.
*/
if (proxy->signal && !priv->display_status_ind_id) {
priv->display_status_ind_id = g_signal_connect(proxy->signal,
MCE_DISPLAY_SIG, G_CALLBACK(mce_display_status_ind), self);
}
if (proxy->request && proxy->valid) {
com_nokia_mce_request_call_get_display_status(proxy->request, NULL,
mce_display_status_query_done, mce_display_ref(self));
}
......@@ -162,17 +184,6 @@ mce_display_valid_changed(
}
}
static
void
mce_display_status_ind(
ComNokiaMceSignal* proxy,
const char* status,
gpointer arg)
{
GDEBUG("Display is %s", status);
mce_display_status_update(MCE_DISPLAY(arg), status);
}
/*==========================================================================*
* API
*==========================================================================*/
......@@ -268,8 +279,6 @@ mce_display_init(
priv->proxy = mce_proxy_new();
priv->proxy_valid_id = mce_proxy_add_valid_changed_handler(priv->proxy,
mce_display_valid_changed, self);
priv->display_status_ind_id = g_signal_connect(priv->proxy->signal,
MCE_DISPLAY_SIG, G_CALLBACK(mce_display_status_ind), self);
}
static
......@@ -280,8 +289,11 @@ mce_display_finalize(
MceDisplay* self = MCE_DISPLAY(object);
MceDisplayPriv* priv = self->priv;
g_signal_handler_disconnect(priv->proxy->signal,
priv->display_status_ind_id);
if (priv->display_status_ind_id) {
g_signal_handler_disconnect(priv->proxy->signal,
priv->display_status_ind_id);
}
mce_proxy_remove_handler(priv->proxy, priv->proxy_valid_id);
mce_proxy_unref(priv->proxy);
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
......
......@@ -75,6 +75,7 @@ mce_name_appeared(
gpointer arg)
{
MceProxy* self = MCE_PROXY(arg);
GDEBUG("Name '%s' is owned by %s", name, owner);
GASSERT(!self->valid);
self->valid = TRUE;
......@@ -89,6 +90,7 @@ mce_name_vanished(
gpointer arg)
{
MceProxy* self = MCE_PROXY(arg);
GDEBUG("Name '%s' has disappeared", name);
if (self->valid) {
self->valid = FALSE;
......@@ -96,6 +98,92 @@ mce_name_vanished(
}
}
static
void
mce_proxy_init_check(
MceProxy* self)
{
MceProxyPriv* priv = self->priv;
if (self->signal && self->request && !priv->mce_watch_id) {
priv->mce_watch_id = g_bus_watch_name_on_connection(priv->bus,
MCE_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE,
mce_name_appeared, mce_name_vanished, self, NULL);
}
}
static
void
mce_proxy_request_proxy_new_finished(
GObject* object,
GAsyncResult* result,
gpointer arg)
{
MceProxy* self = MCE_PROXY(arg);
GError* error = NULL;
GASSERT(!self->request);
self->request = com_nokia_mce_request_proxy_new_finish(result, &error);
if (self->request) {
mce_proxy_init_check(self);
} else {
GERR("Failed to initialize MCE request proxy: %s", GERRMSG(error));
g_error_free(error);
}
mce_proxy_unref(self);
}
static
void
mce_proxy_signal_proxy_new_finished(
GObject* object,
GAsyncResult* result,
gpointer arg)
{
MceProxy* self = MCE_PROXY(arg);
GError* error = NULL;
GASSERT(!self->signal);
self->signal = com_nokia_mce_signal_proxy_new_finish(result, &error);
if (self->signal) {
mce_proxy_init_check(self);
} else {
GERR("Failed to initialize MCE signal proxy: %s", GERRMSG(error));
g_error_free(error);
}
mce_proxy_unref(self);
}
static
void
mce_proxy_bus_get_finished(
GObject* object,
GAsyncResult* result,
gpointer arg)
{
MceProxy* self = MCE_PROXY(arg);
MceProxyPriv* priv = self->priv;
GError* error = NULL;
priv->bus = g_bus_get_finish(result, &error);
if (priv->bus) {
com_nokia_mce_request_proxy_new(priv->bus,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
MCE_SERVICE, MCE_REQUEST_PATH, NULL,
mce_proxy_request_proxy_new_finished,
mce_proxy_ref(self));
com_nokia_mce_signal_proxy_new(priv->bus,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
MCE_SERVICE, MCE_SIGNAL_PATH, NULL,
mce_proxy_signal_proxy_new_finished,
mce_proxy_ref(self));
} else {
GERR("Failed to attach to system bus: %s", GERRMSG(error));
g_error_free(error);
}
mce_proxy_unref(self);
}
MceProxy*
mce_proxy_new()
{
......@@ -108,6 +196,8 @@ mce_proxy_new()
mce_proxy_ref(mce_proxy_instance);
} else {
mce_proxy_instance = g_object_new(MCE_PROXY_TYPE, NULL);
g_bus_get(G_BUS_TYPE_SYSTEM, NULL, mce_proxy_bus_get_finished,
mce_proxy_ref(mce_proxy_instance));
g_object_add_weak_pointer(G_OBJECT(mce_proxy_instance),
(gpointer*)(&mce_proxy_instance));
}
......@@ -158,17 +248,8 @@ void
mce_proxy_init(
MceProxy* self)
{
MceProxyPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self, MCE_PROXY_TYPE,
MceProxyPriv);
self->priv = priv;
priv->bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
self->signal = com_nokia_mce_signal_proxy_new_sync(priv->bus,
G_DBUS_PROXY_FLAGS_NONE, MCE_SERVICE, MCE_SIGNAL_PATH, NULL, NULL);
self->request = com_nokia_mce_request_proxy_new_sync(priv->bus,
G_DBUS_PROXY_FLAGS_NONE, MCE_SERVICE, MCE_REQUEST_PATH, NULL, NULL);
priv->mce_watch_id = g_bus_watch_name_on_connection(priv->bus,
MCE_SERVICE, G_BUS_NAME_WATCHER_FLAGS_NONE,
mce_name_appeared, mce_name_vanished, self, NULL);
self->priv = G_TYPE_INSTANCE_GET_PRIVATE(self,
MCE_PROXY_TYPE, MceProxyPriv);
}
static
......@@ -178,10 +259,19 @@ mce_proxy_finalize(
{
MceProxy* self = MCE_PROXY(object);
MceProxyPriv* priv = self->priv;
g_bus_unwatch_name(priv->mce_watch_id);
g_object_unref(self->signal);
g_object_unref(self->request);
g_object_unref(priv->bus);
if (priv->mce_watch_id) {
g_bus_unwatch_name(priv->mce_watch_id);
}
if (self->signal) {
g_object_unref(self->signal);
}
if (self->request) {
g_object_unref(self->request);
}
if (priv->bus) {
g_object_unref(priv->bus);
}
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
......
......@@ -153,14 +153,36 @@ mce_tklock_mode_query_done(
mce_tklock_unref(self);
}
static
void
mce_tklock_mode_ind(
ComNokiaMceSignal* proxy,
const char* mode,
gpointer arg)
{
GDEBUG("Mode is %s", mode);
mce_tklock_mode_update(MCE_TKLOCK(arg), mode);
}
static
void
mce_tklock_mode_query(
MceTklock* self)
{
MceProxy* proxy = self->priv->proxy;
if (proxy->valid) {
MceTklockPriv* priv = self->priv;
MceProxy* proxy = priv->proxy;
/*
* proxy->signal and proxy->request may not be available at the
* time when MceTklock is created. In that case we have to wait
* for the valid signal before we can connect the tklock mode
* signal and submit the initial query.
*/
if (proxy->signal && priv->proxy->signal) {
priv->tklock_mode_ind_id = g_signal_connect(proxy->signal,
MCE_TKLOCK_MODE_SIG, G_CALLBACK(mce_tklock_mode_ind), self);
}
if (proxy->request && proxy->valid) {
com_nokia_mce_request_call_get_tklock_mode(proxy->request, NULL,
mce_tklock_mode_query_done, mce_tklock_ref(self));
}
......@@ -184,17 +206,6 @@ mce_tklock_valid_changed(
}
}
static
void
mce_tklock_mode_ind(
ComNokiaMceSignal* proxy,
const char* mode,
gpointer arg)
{
GDEBUG("Mode is %s", mode);
mce_tklock_mode_update(MCE_TKLOCK(arg), mode);
}
/*==========================================================================*
* API
*==========================================================================*/
......@@ -302,8 +313,6 @@ mce_tklock_init(
priv->proxy = mce_proxy_new();
priv->proxy_valid_id = mce_proxy_add_valid_changed_handler(priv->proxy,
mce_tklock_valid_changed, self);
priv->tklock_mode_ind_id = g_signal_connect(priv->proxy->signal,
MCE_TKLOCK_MODE_SIG, G_CALLBACK(mce_tklock_mode_ind), self);
}
static
......@@ -314,8 +323,11 @@ mce_tklock_finalize(
MceTklock* self = MCE_TKLOCK(object);
MceTklockPriv* priv = self->priv;
g_signal_handler_disconnect(priv->proxy->signal,
priv->tklock_mode_ind_id);
if (priv->tklock_mode_ind_id) {
g_signal_handler_disconnect(priv->proxy->signal,
priv->tklock_mode_ind_id);
}
mce_proxy_remove_handler(priv->proxy, priv->proxy_valid_id);
mce_proxy_unref(priv->proxy);
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment