1
/* * This file is part of Maliit framework *
3
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
* Contact: maliit-discuss@lists.maliit.org
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License version 2.1 as published by the Free Software Foundation
11
* and appearing in the file LICENSE.LGPL included in the packaging
15
#include "meego-im-connector.h"
21
#include <dbus/dbus.h>
22
#include <dbus/dbus-glib.h>
23
#include <dbus/dbus-glib-lowlevel.h>
25
#define MALIIT_SERVER_NAME "org.maliit.server"
26
#define MALIIT_SERVER_OBJECT_PATH "/org/maliit/server/address"
27
#define MALIIT_SERVER_INTERFACE "org.maliit.Server.Address"
28
#define MALIIT_SERVER_ADDRESS_PROPERTY "address"
30
#ifndef MALIIT_USE_GIO_API
31
#define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
32
#define DBUS_PROPERTIES_GET_METHOD "Get"
35
#define MALIIT_SERVER_ADDRESS_ENV "MALIIT_SERVER_ADDRESS"
39
#define G_VALUE_INIT { 0, { { 0 } } }
42
struct _MeegoImConnectorPrivate {
43
DBusGConnection *connection;
46
MeegoImConnector *meego_im_connector_new();
49
try_reconnect(MeegoImConnector *connector)
51
if (connector->try_reconnect) {
52
meego_im_connector_run(connector);
54
return FALSE; // _run is responsible for setting up a new timeout if needed
59
connection_dropped(gpointer instance, MeegoImConnector *connector)
61
if (connector->priv->connection) {
62
dbus_g_connection_unref(connector->priv->connection);
64
try_reconnect(connector);
67
#ifdef MALIIT_USE_GIO_API
71
const char *overridden_address = g_getenv(MALIIT_SERVER_ADDRESS_ENV);
73
if (overridden_address && *overridden_address) {
74
return g_strdup(overridden_address);
77
GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_NONE;
79
#if defined(NO_DBUS_ACTIVATION)
80
flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
83
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
87
MALIIT_SERVER_OBJECT_PATH,
88
MALIIT_SERVER_INTERFACE,
95
GVariant *variant = g_dbus_proxy_get_cached_property(proxy, MALIIT_SERVER_ADDRESS_PROPERTY);
98
g_object_unref(proxy);
102
char* address = g_strdup(g_variant_get_string(variant, 0));
104
g_variant_unref(variant);
105
g_object_unref(proxy);
114
const char *overridden_address = g_getenv(MALIIT_SERVER_ADDRESS_ENV);
116
if (overridden_address && *overridden_address) {
117
return g_strdup(overridden_address);
120
GValue value = G_VALUE_INIT;
121
GError *error = NULL;
122
DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
125
DBG("%s: %s", __PRETTY_FUNCTION__, error->message);
130
DBusGProxy *proxy = dbus_g_proxy_new_for_name(connection,
132
MALIIT_SERVER_OBJECT_PATH,
133
DBUS_PROPERTIES_INTERFACE);
135
if (!dbus_g_proxy_call(proxy,
136
DBUS_PROPERTIES_GET_METHOD,
138
G_TYPE_STRING, MALIIT_SERVER_INTERFACE,
139
G_TYPE_STRING, MALIIT_SERVER_ADDRESS_PROPERTY,
141
G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
143
DBG("%s: %s", __PRETTY_FUNCTION__, error->message);
148
gchar *result = g_value_dup_string(&value);
149
g_value_unset(&value);
150
g_object_unref(proxy);
159
* Connects and maintains the DBusConnection, and the objects
160
* that depend on it. Makes sure that MeegoIMProxy and MeegoIMDbusObj
161
* has the correct DBusConnection by calling _connect on them when
162
* the connection has changed.
164
* MeegoIMProxy is responsible for letting the connector know that the
165
* connection was dropped by emitting the "connection-dropped signal".
168
meego_im_connector_new()
170
MeegoImConnector *self = g_new(MeegoImConnector, 1);
171
self->priv = g_new(MeegoImConnectorPrivate, 1);
173
self->priv->connection = NULL;
174
self->dbusobj = meego_imcontext_dbusobj_new();
175
self->proxy = meego_im_proxy_new();
176
self->try_reconnect = TRUE;
178
g_signal_connect(self->proxy, "connection-dropped",
179
G_CALLBACK(connection_dropped), (gpointer)self);
185
meego_im_connector_free(MeegoImConnector *self)
187
if (self->priv->connection) {
188
dbus_g_connection_unref(self->priv->connection);
190
g_object_unref(self->dbusobj);
191
g_object_unref(self->proxy);
198
static MeegoImConnector *meego_im_connector_singleton = NULL;
201
meego_im_connector_set_singleton(MeegoImConnector *self)
203
meego_im_connector_singleton = self;
207
meego_im_connector_get_singleton()
209
MeegoImConnector *connector = meego_im_connector_singleton;
211
connector = meego_im_connector_new();
212
meego_im_connector_set_singleton(connector);
213
meego_im_connector_run(connector);
219
meego_im_connector_run(MeegoImConnector *self)
221
DBusGConnection *connection = NULL;
222
DBusConnection *dbus_connection;
224
char *address = NULL;
226
g_return_if_fail(self != NULL);
228
address = get_dbus_address();
230
DBG("Couldn't connect to Maliit server. Retrying...");
232
g_timeout_add_seconds(2, (GSourceFunc)try_reconnect, self);
236
dbus_error_init(&error);
238
// Input contexts should not share the connection to the maliit server with
239
// each other (even not when they are in the same application). Therefore,
240
// use private connection for IC to server connection.
241
dbus_connection = dbus_connection_open_private(address, &error);
244
if (!dbus_connection) {
245
DBG("Couldn't connect to Maliit server: %s. Retrying...", error.message);
247
dbus_error_free(&error);
249
g_timeout_add_seconds(2, (GSourceFunc)try_reconnect, self);
253
dbus_connection_setup_with_g_main(dbus_connection, NULL);
254
connection = dbus_connection_get_g_connection(dbus_connection);
256
self->priv->connection = connection;
258
meego_im_proxy_connect(self->proxy, (gpointer)self->priv->connection);
259
meego_imcontext_dbusobj_connect(self->dbusobj, (gpointer)self->priv->connection);