summaryrefslogtreecommitdiffstats
path: root/src/ibusserver.c
blob: 22998568ce419b18b3bfde89b6e1d550d222415d (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* vim:set et sts=4: */
/* ibus - The Input Bus
 * Copyright (C) 2008-2009 Huang Peng <shawn.p.huang@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "ibusserver.h"
#include "ibusinternal.h"

#define IBUS_SERVER_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_SERVER, IBusServerPrivate))
#define DECLARE_PRIV IBusServerPrivate *priv = IBUS_SERVER_GET_PRIVATE(server)

enum {
    NEW_CONNECTION,
    LAST_SIGNAL,
};

enum {
    PROP_0,
    PROP_CONNECTION_TYPE,
};

/* IBusServerPriv */
struct _IBusServerPrivate {
    DBusServer *server;
    GType       connection_type;
};
typedef struct _IBusServerPrivate IBusServerPrivate;

static guint            server_signals[LAST_SIGNAL] = { 0 };

/* functions prototype */
static void     ibus_server_class_init  (IBusServerClass    *klass);
static void     ibus_server_init        (IBusServer         *server);
static void     ibus_server_destroy     (IBusServer         *server);
static void     ibus_server_set_property(IBusServer         *server,
                                         guint               prop_id,
                                         const GValue       *value,
                                         GParamSpec         *pspec);
static void     ibus_server_get_property(IBusServer         *server,
                                         guint               prop_id,
                                         GValue             *value,
                                         GParamSpec         *pspec);
static gboolean ibus_server_listen_internal
                                        (IBusServer         *server,
                                         const gchar        *address);
static void     ibus_server_new_connection
                                        (IBusServer         *server,
                                         IBusConnection     *connection);

static IBusObjectClass  *parent_class = NULL;

GType
ibus_server_get_type (void)
{
    static GType type = 0;

    static const GTypeInfo type_info = {
        sizeof (IBusServerClass),
        (GBaseInitFunc)     NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc)    ibus_server_class_init,
        NULL,               /* class finalize */
        NULL,               /* class data */
        sizeof (IBusServer),
        0,
        (GInstanceInitFunc) ibus_server_init,
    };

    if (type == 0) {
        type = g_type_register_static (IBUS_TYPE_OBJECT,
                    "IBusServer",
                    &type_info,
                    (GTypeFlags)0);
    }

    return type;
}

IBusServer *
ibus_server_new (void)
{
    IBusServer *server;

    server = IBUS_SERVER (g_object_new (IBUS_TYPE_SERVER, NULL));
    return server;
}

gboolean
ibus_server_listen  (IBusServer  *server,
                     const gchar *address)
{
    g_assert (IBUS_IS_SERVER (server));
    g_assert (address != NULL);

    return ibus_server_listen_internal (server, address);
}

static void
ibus_server_class_init (IBusServerClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);

    parent_class = (IBusObjectClass *) g_type_class_peek_parent (klass);

    g_type_class_add_private (klass, sizeof (IBusServerPrivate));

    gobject_class->set_property = (GObjectSetPropertyFunc) ibus_server_set_property;
    gobject_class->get_property = (GObjectGetPropertyFunc) ibus_server_get_property;

    ibus_object_class->destroy = (IBusObjectDestroyFunc) ibus_server_destroy;

    klass->new_connection = ibus_server_new_connection;

    /* install properties */
    g_object_class_install_property (gobject_class,
                    PROP_CONNECTION_TYPE,
                    g_param_spec_gtype ("connection-type",
                        "connection type",
                        "The connection type of server object",
                        IBUS_TYPE_CONNECTION,
                        G_PARAM_READWRITE));

    /* install signals */
    server_signals[NEW_CONNECTION] =
        g_signal_new (I_("new-connection"),
            G_TYPE_FROM_CLASS (klass),
            G_SIGNAL_RUN_LAST,
            G_STRUCT_OFFSET (IBusServerClass, new_connection),
            NULL, NULL,
            ibus_marshal_VOID__OBJECT,
            G_TYPE_NONE, 1,
            G_TYPE_OBJECT);
}

static void
ibus_server_init (IBusServer *server)
{
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);
    priv->server = NULL;
    priv->connection_type = IBUS_TYPE_CONNECTION;
}

static void
ibus_server_destroy (IBusServer *server)
{
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    if (priv->server) {
        dbus_server_unref (priv->server);
        priv->server = NULL;
    }

    IBUS_OBJECT_CLASS(parent_class)->destroy (IBUS_OBJECT (server));
}

static void
ibus_server_set_property    (IBusServer     *server,
                             guint           prop_id,
                             const GValue   *value,
                             GParamSpec     *pspec)
{
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    switch (prop_id) {
    case PROP_CONNECTION_TYPE:
    {
        GType type;
        type = g_value_get_gtype (value);
        g_assert (g_type_is_a (type, IBUS_TYPE_CONNECTION));
        priv->connection_type = type;
        break;
    }
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (server, prop_id, pspec);
    }
}

static void
ibus_server_get_property    (IBusServer     *server,
                             guint           prop_id,
                             GValue         *value,
                             GParamSpec     *pspec)
{
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    switch (prop_id) {
    case PROP_CONNECTION_TYPE:
        g_value_set_gtype (value, priv->connection_type);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (server, prop_id, pspec);
    }
}

static void
ibus_server_new_connection (IBusServer      *server,
                            IBusConnection  *connection)
{
}

static void
_new_connection_cb (DBusServer      *dbus_server,
                    DBusConnection  *new_connection,
                    IBusServer      *server)
{
    IBusServerPrivate *priv;
    IBusConnection *connection;

    priv = IBUS_SERVER_GET_PRIVATE (server);
    connection = IBUS_CONNECTION (g_object_new (priv->connection_type, NULL));
    ibus_connection_set_connection (connection, new_connection, FALSE);

    g_signal_emit (server, server_signals[NEW_CONNECTION], 0, connection);
    g_object_unref (connection);
}

static gboolean
ibus_server_listen_internal (IBusServer  *server,
                             const gchar *address)
{
    g_assert (IBUS_IS_SERVER (server));
    g_assert (address != NULL);

    IBusServerPrivate *priv;
    DBusError error;

    priv = IBUS_SERVER_GET_PRIVATE (server);

    g_assert (priv->server == NULL);

    dbus_error_init (&error);
    priv->server = dbus_server_listen (address, &error);

    if (priv->server == NULL) {
        g_warning ("Can not listen on '%s':\n"
                   "  %s:%s",
                   address, error.name, error.message);
        return FALSE;
    }

    dbus_server_set_new_connection_function (priv->server,
                (DBusNewConnectionFunction) _new_connection_cb,
                server, NULL);

    dbus_server_set_auth_mechanisms (priv->server, NULL);

    dbus_server_setup (priv->server, NULL);
    return TRUE;
}

void
ibus_server_disconnect (IBusServer *server)
{
    g_assert (IBUS_IS_SERVER (server));

    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    g_assert (priv->server != NULL);
    dbus_server_disconnect (priv->server);
}

const gchar *
ibus_server_get_address (IBusServer *server)
{
    g_assert (IBUS_IS_SERVER (server));

    gchar *address, *tmp;
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    g_assert (priv->server != NULL);

    tmp = dbus_server_get_address (priv->server);
    address = g_strdup (tmp);
    dbus_free (tmp);
    return address;
}

const gchar *
ibus_server_get_id (IBusServer *server)
{
    g_assert (IBUS_IS_SERVER (server));

    gchar *id, *tmp;
    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    g_assert (priv->server != NULL);

    tmp = dbus_server_get_id (priv->server);
    id = g_strdup (tmp);
    dbus_free (tmp);
    return id;
}

gboolean
ibus_server_is_connected (IBusServer *server)
{
    g_assert (IBUS_IS_SERVER (server));

    IBusServerPrivate *priv;
    priv = IBUS_SERVER_GET_PRIVATE (server);

    g_assert (priv->server != NULL);

    return dbus_server_get_is_connected (priv->server);
}