summaryrefslogtreecommitdiffstats
path: root/bus/connection.c
blob: fd05ab1f976d9fc7da7e74fe162cf0655566cabe (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
/* vim:set et sts=4: */
/* bus - 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 "connection.h"
#include "matchrule.h"

#define BUS_CONNECTION_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), BUS_TYPE_CONNECTION, BusConnectionPrivate))

/* BusConnectionPriv */
struct _BusConnectionPrivate {
    gchar *unique_name;
    /* list for well known names */
    GList  *names;
    GList  *rules;
};
typedef struct _BusConnectionPrivate BusConnectionPrivate;

// static guint            _signals[LAST_SIGNAL] = { 0 };

/* functions prototype */
static void     bus_connection_class_init   (BusConnectionClass     *klass);
static void     bus_connection_init         (BusConnection          *connection);
static void     bus_connection_destroy      (BusConnection          *connection);
static gboolean bus_connection_ibus_message (BusConnection          *connection,
                                             IBusMessage            *message);
#if 0
static gboolean bus_connection_dbus_signal  (BusConnection          *connection,
                                             DBusMessage            *message);
#endif

static IBusObjectClass  *parent_class = NULL;

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

    static const GTypeInfo type_info = {
        sizeof (BusConnectionClass),
        (GBaseInitFunc)     NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc)    bus_connection_class_init,
        NULL,               /* class finalize */
        NULL,               /* class data */
        sizeof (IBusConnection),
        0,
        (GInstanceInitFunc) bus_connection_init,
    };

    if (type == 0) {
        type = g_type_register_static (IBUS_TYPE_CONNECTION,
                    "BusConnection",
                    &type_info,
                    (GTypeFlags)0);
    }

    return type;
}

BusConnection *
bus_connection_new (void)
{
    BusConnection *connection = BUS_CONNECTION (g_object_new (BUS_TYPE_CONNECTION, NULL));
    return connection;
}

static void
bus_connection_class_init (BusConnectionClass *klass)
{
    IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (klass);
    IBusConnectionClass *ibus_connection_class = IBUS_CONNECTION_CLASS (klass);

    parent_class = (IBusObjectClass *) g_type_class_peek_parent (klass);

    g_type_class_add_private (klass, sizeof (BusConnectionPrivate));

    ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_connection_destroy;

    ibus_connection_class->ibus_message =
            (IBusIBusMessageFunc) bus_connection_ibus_message;

}

static void
bus_connection_init (BusConnection *connection)
{
    BusConnectionPrivate *priv;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);

    priv->unique_name = NULL;
    priv->names = NULL;
}

static void
bus_connection_destroy (BusConnection *connection)
{
    GList *name;
    BusConnectionPrivate *priv;

    IBUS_OBJECT_CLASS(parent_class)->destroy (IBUS_OBJECT (connection));

    priv = BUS_CONNECTION_GET_PRIVATE (connection);

    if (priv->unique_name) {
        g_free (priv->unique_name);
        priv->unique_name = NULL;
    }

    for (name = priv->names; name != NULL; name = name->next) {
        g_free (name->data);
    }
    g_list_free (priv->names);
    priv->names = NULL;
}

static gboolean
bus_connection_ibus_message (BusConnection  *connection,
                             IBusMessage    *message)
{
    gboolean retval;

#if 0
    gchar *str = ibus_message_to_string (message);
    g_debug ("%s", str);
    g_free(str);
#endif

    retval = IBUS_CONNECTION_CLASS (parent_class)->ibus_message (
                    (IBusConnection *)connection,
                    message);
    return retval;
}

#if 0
static gboolean
bus_connection_dbus_signal  (BusConnection  *connection,
                             DBusMessage    *message)
{
    gboolean retval;
    retval = IBUS_CONNECTION_CLASS (parent_class)->dbus_signal (
            IBUS_CONNECTION (connection), message);
    return retval;
}
#endif

const gchar *
bus_connection_get_unique_name (BusConnection   *connection)
{
    BusConnectionPrivate *priv;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);
    return priv->unique_name;
}

void
bus_connection_set_unique_name (BusConnection   *connection,
                                const gchar     *name)
{
    BusConnectionPrivate *priv;
    priv = BUS_CONNECTION_GET_PRIVATE (connection);
    g_assert (priv->unique_name == NULL);
    priv->unique_name = g_strdup (name);
}

const GList *
bus_connection_get_names (BusConnection   *connection)
{
    BusConnectionPrivate *priv;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);
    return priv->names;
}

const gchar *
bus_connection_add_name (BusConnection     *connection,
                          const gchar       *name)
{
    gchar *new_name;
    BusConnectionPrivate *priv;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);
    new_name = g_strdup (name);
    priv->names = g_list_append (priv->names, new_name);

    return new_name;
}

gboolean
bus_connection_remove_name (BusConnection     *connection,
                             const gchar       *name)
{
    BusConnectionPrivate *priv;
    GList *link;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);

    link = g_list_find_custom (priv->names, name, (GCompareFunc) g_strcmp0);

    if (link) {
        g_free (link->data);
        priv->names = g_list_delete_link (priv->names, link);
        return TRUE;
    }
    return FALSE;
}

gboolean
bus_connection_add_match (BusConnection  *connection,
                          const gchar    *rule)
{
    g_assert (BUS_IS_CONNECTION (connection));
    g_assert (rule != NULL);

    BusMatchRule *p;
    GList *link;
    BusConnectionPrivate *priv;

    priv = BUS_CONNECTION_GET_PRIVATE (connection);

    p = bus_match_rule_new (rule);
    if (p == NULL)
        return FALSE;

    for (link = priv->rules; link != NULL; link = link->next) {
        if (bus_match_rule_is_equal (p, (BusMatchRule *)link->data)) {
            g_object_unref (p);
            return TRUE;
        }
    }

    priv->rules = g_list_append (priv->rules, p);
    return TRUE;

}

gboolean
bus_connection_remove_match (BusConnection  *connection,
                             const gchar    *rule)
{
    return FALSE;
}