~timo-jyrinki/ubuntu/trusty/maliit-framework/fix_qt52

« back to all changes in this revision

Viewing changes to connection-glib/meego-im-connector.c

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo, Sergio Schvezov, Ricardo Salveti de Araujo
  • Date: 2013-07-23 19:47:04 UTC
  • mfrom: (1.1.2) (1.2.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130723194704-1lsy1kmlda069cea
Tags: 0.99.0+git20130615+97e8335-0ubuntu1
[ Sergio Schvezov ]
* New build from HEAD 97e8335.
* Packaging import from lp:phablet-extras/maliit-framework.

[ Ricardo Salveti de Araujo ]
* debian/control: adding vcs and fixing dependencies
* General package cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* * This file is part of Maliit framework *
2
 
 *
3
 
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4
 
 * All rights reserved.
5
 
 *
6
 
 * Contact: maliit-discuss@lists.maliit.org
7
 
 *
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
12
 
 * of this file.
13
 
 */
14
 
 
15
 
#include "meego-im-connector.h"
16
 
#include "debug.h"
17
 
 
18
 
#include <glib.h>
19
 
#include <gio/gio.h>
20
 
 
21
 
#include <dbus/dbus.h>
22
 
#include <dbus/dbus-glib.h>
23
 
#include <dbus/dbus-glib-lowlevel.h>
24
 
 
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"
29
 
 
30
 
#ifndef MALIIT_USE_GIO_API
31
 
#define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
32
 
#define DBUS_PROPERTIES_GET_METHOD "Get"
33
 
#endif
34
 
 
35
 
#define MALIIT_SERVER_ADDRESS_ENV "MALIIT_SERVER_ADDRESS"
36
 
 
37
 
/* For glib < 2.30 */
38
 
#ifndef G_VALUE_INIT
39
 
#define G_VALUE_INIT { 0, { { 0 } } }
40
 
#endif
41
 
 
42
 
struct _MeegoImConnectorPrivate {
43
 
    DBusGConnection *connection;
44
 
};
45
 
 
46
 
MeegoImConnector *meego_im_connector_new();
47
 
 
48
 
static gboolean
49
 
try_reconnect(MeegoImConnector *connector)
50
 
{
51
 
    if (connector->try_reconnect) {
52
 
        meego_im_connector_run(connector);
53
 
    }
54
 
    return FALSE; // _run is responsible for setting up a new timeout if needed
55
 
}
56
 
 
57
 
 
58
 
static void
59
 
connection_dropped(gpointer instance, MeegoImConnector *connector)
60
 
{
61
 
    if (connector->priv->connection) {
62
 
        dbus_g_connection_unref(connector->priv->connection);
63
 
    }
64
 
    try_reconnect(connector);
65
 
}
66
 
 
67
 
#ifdef MALIIT_USE_GIO_API
68
 
static char *
69
 
get_dbus_address()
70
 
{
71
 
    const char *overridden_address = g_getenv(MALIIT_SERVER_ADDRESS_ENV);
72
 
 
73
 
    if (overridden_address && *overridden_address) {
74
 
        return g_strdup(overridden_address);
75
 
    }
76
 
 
77
 
    GDBusProxyFlags flags = G_DBUS_PROXY_FLAGS_NONE;
78
 
 
79
 
#if defined(NO_DBUS_ACTIVATION)
80
 
    flags = G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
81
 
#endif
82
 
 
83
 
    GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
84
 
                                                      flags,
85
 
                                                      0,
86
 
                                                      MALIIT_SERVER_NAME,
87
 
                                                      MALIIT_SERVER_OBJECT_PATH,
88
 
                                                      MALIIT_SERVER_INTERFACE,
89
 
                                                      0, 0);
90
 
 
91
 
 
92
 
    if (!proxy)
93
 
        return 0;
94
 
 
95
 
    GVariant *variant = g_dbus_proxy_get_cached_property(proxy, MALIIT_SERVER_ADDRESS_PROPERTY);
96
 
 
97
 
    if (!variant) {
98
 
        g_object_unref(proxy);
99
 
        return 0;
100
 
    }
101
 
 
102
 
    char* address = g_strdup(g_variant_get_string(variant, 0));
103
 
 
104
 
    g_variant_unref(variant);
105
 
    g_object_unref(proxy);
106
 
 
107
 
    return address;
108
 
}
109
 
 
110
 
#else
111
 
static char *
112
 
get_dbus_address()
113
 
{
114
 
    const char *overridden_address = g_getenv(MALIIT_SERVER_ADDRESS_ENV);
115
 
 
116
 
    if (overridden_address && *overridden_address) {
117
 
        return g_strdup(overridden_address);
118
 
    }
119
 
 
120
 
    GValue value = G_VALUE_INIT;
121
 
    GError *error = NULL;
122
 
    DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
123
 
 
124
 
    if (!connection) {
125
 
        DBG("%s: %s", __PRETTY_FUNCTION__, error->message);
126
 
        g_error_free(error);
127
 
        return NULL;
128
 
    }
129
 
 
130
 
    DBusGProxy *proxy = dbus_g_proxy_new_for_name(connection,
131
 
                                            MALIIT_SERVER_NAME,
132
 
                                            MALIIT_SERVER_OBJECT_PATH,
133
 
                                            DBUS_PROPERTIES_INTERFACE);
134
 
 
135
 
    if (!dbus_g_proxy_call(proxy,
136
 
                           DBUS_PROPERTIES_GET_METHOD,
137
 
                           &error,
138
 
                           G_TYPE_STRING, MALIIT_SERVER_INTERFACE,
139
 
                           G_TYPE_STRING, MALIIT_SERVER_ADDRESS_PROPERTY,
140
 
                           G_TYPE_INVALID,
141
 
                           G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
142
 
 
143
 
        DBG("%s: %s", __PRETTY_FUNCTION__, error->message);
144
 
        g_error_free(error);
145
 
        return NULL;
146
 
    }
147
 
 
148
 
    gchar *result = g_value_dup_string(&value);
149
 
    g_value_unset(&value);
150
 
    g_object_unref(proxy);
151
 
 
152
 
    return result;
153
 
}
154
 
#endif
155
 
 
156
 
/**
157
 
 * MeegoImConnector:
158
 
 *
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.
163
 
 *
164
 
 * MeegoIMProxy is responsible for letting the connector know that the
165
 
 * connection was dropped by emitting the "connection-dropped signal".
166
 
 */
167
 
MeegoImConnector *
168
 
meego_im_connector_new()
169
 
{
170
 
    MeegoImConnector *self = g_new(MeegoImConnector, 1);
171
 
    self->priv = g_new(MeegoImConnectorPrivate, 1);
172
 
 
173
 
    self->priv->connection = NULL;
174
 
    self->dbusobj = meego_imcontext_dbusobj_new();
175
 
    self->proxy = meego_im_proxy_new();
176
 
    self->try_reconnect = TRUE;
177
 
 
178
 
    g_signal_connect(self->proxy, "connection-dropped",
179
 
                     G_CALLBACK(connection_dropped), (gpointer)self);
180
 
 
181
 
    return self;
182
 
}
183
 
 
184
 
void
185
 
meego_im_connector_free(MeegoImConnector *self)
186
 
{
187
 
    if (self->priv->connection) {
188
 
        dbus_g_connection_unref(self->priv->connection);
189
 
    }
190
 
    g_object_unref(self->dbusobj);
191
 
    g_object_unref(self->proxy);
192
 
 
193
 
    g_free(self->priv);
194
 
    g_free(self);
195
 
}
196
 
 
197
 
 
198
 
static MeegoImConnector *meego_im_connector_singleton = NULL;
199
 
 
200
 
void
201
 
meego_im_connector_set_singleton(MeegoImConnector *self)
202
 
{
203
 
    meego_im_connector_singleton = self;
204
 
}
205
 
 
206
 
MeegoImConnector *
207
 
meego_im_connector_get_singleton()
208
 
{
209
 
    MeegoImConnector *connector = meego_im_connector_singleton;
210
 
    if (!connector) {
211
 
        connector = meego_im_connector_new();
212
 
        meego_im_connector_set_singleton(connector);
213
 
        meego_im_connector_run(connector);
214
 
    }
215
 
    return connector;
216
 
}
217
 
 
218
 
void
219
 
meego_im_connector_run(MeegoImConnector *self)
220
 
{
221
 
    DBusGConnection *connection = NULL;
222
 
    DBusConnection *dbus_connection;
223
 
    DBusError error;
224
 
    char *address = NULL;
225
 
 
226
 
    g_return_if_fail(self != NULL);
227
 
 
228
 
    address = get_dbus_address();
229
 
    if (!address) {
230
 
        DBG("Couldn't connect to Maliit server. Retrying...");
231
 
 
232
 
        g_timeout_add_seconds(2, (GSourceFunc)try_reconnect, self);
233
 
        return;
234
 
    }
235
 
 
236
 
    dbus_error_init(&error);
237
 
 
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);
242
 
    g_free(address);
243
 
 
244
 
    if (!dbus_connection) {
245
 
        DBG("Couldn't connect to Maliit server: %s. Retrying...", error.message);
246
 
 
247
 
        dbus_error_free(&error);
248
 
 
249
 
        g_timeout_add_seconds(2, (GSourceFunc)try_reconnect, self);
250
 
        return;
251
 
    }
252
 
 
253
 
    dbus_connection_setup_with_g_main(dbus_connection, NULL);
254
 
    connection = dbus_connection_get_g_connection(dbus_connection);
255
 
 
256
 
    self->priv->connection = connection;
257
 
 
258
 
    meego_im_proxy_connect(self->proxy, (gpointer)self->priv->connection);
259
 
    meego_imcontext_dbusobj_connect(self->dbusobj, (gpointer)self->priv->connection);
260
 
}