summaryrefslogtreecommitdiff
path: root/meego/meego-im-connector.c
blob: c1ca0de4249e64f8be27b77453b3dce191f6fa56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* * This file is part of meego-im-framework *
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * Contact: Nokia Corporation (directui@nokia.com)
 *
 * If you have questions regarding the use of this file, please contact
 * Nokia at directui@nokia.com.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * and appearing in the file LICENSE.LGPL included in the packaging
 * of this file.
 */

#include "meego-im-connector.h"

#include <glib.h>

#define DBUS_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
#define DBUS_PROPERTIES_GET_METHOD "Get"

#define MEEGO_IM_SOCKET_PATH "unix:path=/tmp/meego-im-uiserver/imserver_dbus"

#define MALIIT_SERVER_ADDRESS_ENV "MALIIT_SERVER_ADDRESS"

#define MALIIT_SERVER_NAME "org.maliit.server"
#define MALIIT_SERVER_OBJECT_PATH "/org/maliit/server/address"
#define MALIIT_SERVER_INTERFACE "org.maliit.Server.Address"
#define MALIIT_SERVER_ADDRESS_PROPERTY "address"

MeegoImConnector *meego_im_connector_new();

static gboolean
try_reconnect(MeegoImConnector *connector)
{
    meego_im_connector_run(connector);
    return FALSE; // _run is responsible for setting up a new timeout if needed
}


static void
connection_dropped(gpointer instance, MeegoImConnector *connector)
{
    if (connector->connection) {
        dbus_g_connection_unref(connector->connection);
    }
    try_reconnect(connector);
}

static char *
get_dbus_address()
{
    const char *overridden_address = g_getenv(MALIIT_SERVER_ADDRESS_ENV);

    if (overridden_address && *overridden_address) {
        return g_strdup(overridden_address);
    }

	GValue value = { 0 };
    GError *error = NULL;
    DBusGConnection *connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);

    if (!connection) {
        g_warning("%s: %s", __PRETTY_FUNCTION__, error->message);
        g_error_free(error);
        return NULL;
    }

    DBusGProxy *proxy = dbus_g_proxy_new_for_name(connection,
                                            MALIIT_SERVER_NAME,
                                            MALIIT_SERVER_OBJECT_PATH,
                                            DBUS_PROPERTIES_INTERFACE);

    if (!dbus_g_proxy_call(proxy,
                           DBUS_PROPERTIES_GET_METHOD,
                           &error,
                           G_TYPE_STRING, MALIIT_SERVER_INTERFACE,
                           G_TYPE_STRING, MALIIT_SERVER_ADDRESS_PROPERTY,
                           G_TYPE_INVALID,
                           G_TYPE_VALUE, &value, G_TYPE_INVALID)) {

        g_warning("%s: %s", __PRETTY_FUNCTION__, error->message);
        g_error_free(error);
		// Let's try PR1.2 address if PR1.3/Maliit was not found.
		return g_strdup(MEEGO_IM_SOCKET_PATH);
    }

    gchar *result = g_value_dup_string(&value);
    g_value_unset(&value);
    g_object_unref(proxy);

    return result;
}


/**
 * MeegoImConnector:
 *
 * Connects and maintains the DBusConnection, and the objects
 * that depend on it. Makes sure that MeegoIMProxy and MeegoIMDbusObj
 * has the correct DBusConnection by calling _connect on them when
 * the connection has changed.
 *
 * MeegoIMProxy is responsible for letting the connector know that the
 * connection was dropped by emitting the "connection-dropped signal".
 */
MeegoImConnector *
meego_im_connector_new()
{
    MeegoImConnector *self = g_new(MeegoImConnector, 1);

    self->connection = NULL;
    self->dbusobj = meego_imcontext_dbusobj_get_singleton();
    self->proxy = meego_im_proxy_get_singleton();

    g_signal_connect(self->proxy, "connection-dropped",
                     G_CALLBACK(connection_dropped), (gpointer)self);

    return self;
}

void
meego_im_connector_free(MeegoImConnector *self)
{
    if (self->connection) {
        dbus_g_connection_unref(self->connection);
    }
    g_free(self);
}

MeegoImConnector *
meego_im_connector_get_singleton()
{
    static MeegoImConnector *connector = NULL;
    if (!connector) {
        connector = meego_im_connector_new();
        meego_im_connector_run(connector);
    }
    return connector;
}

void
meego_im_connector_run(MeegoImConnector *self)
{
    GError *error = NULL;
    DBusGConnection *connection = NULL;
    gchar *address = get_dbus_address();

    g_return_if_fail(self != NULL);

    connection = dbus_g_connection_open(address, &error);

    g_free(address);

    if (!connection) {
        g_warning("Couldn't connect to Maliit server. Retrying...");
        g_error_free(error);

        g_timeout_add_seconds(2, (GSourceFunc)try_reconnect, self);
        return;
    }

    self->connection = connection;

    meego_im_proxy_connect(self->proxy, self->connection);
    meego_imcontext_dbusobj_connect(self->dbusobj, self->connection);
}