[go: up one dir, main page]

File: connection.c

package info (click to toggle)
ibus 1.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 9,616 kB
  • ctags: 10,909
  • sloc: ansic: 33,920; sh: 10,485; python: 6,705; makefile: 1,043; cpp: 170
file content (217 lines) | stat: -rw-r--r-- 6,137 bytes parent folder | download
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
/* vim:set et sts=4: */
/* bus - The Input Bus
 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * 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 <unistd.h>
#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 {
};
typedef struct _BusConnectionPrivate BusConnectionPrivate;

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

/* functions prototype */
static void     bus_connection_destroy      (BusConnection          *connection);
static gboolean bus_connection_authenticate_unix_user
                                            (IBusConnection         *connection,
                                             gulong                  uid);
static gboolean bus_connection_ibus_message (BusConnection          *connection,
                                             IBusMessage            *message);
#if 0
static gboolean bus_connection_dbus_signal  (BusConnection          *connection,
                                             DBusMessage            *message);
#endif

G_DEFINE_TYPE (BusConnection, bus_connection, IBUS_TYPE_CONNECTION)

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);

    ibus_object_class->destroy = (IBusObjectDestroyFunc) bus_connection_destroy;

    ibus_connection_class->authenticate_unix_user = bus_connection_authenticate_unix_user;
    ibus_connection_class->ibus_message =
            (IBusIBusMessageFunc) bus_connection_ibus_message;

}

static void
bus_connection_init (BusConnection *connection)
{
    connection->unique_name = NULL;
    connection->names = NULL;
}

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

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

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

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

static gboolean
bus_connection_authenticate_unix_user (IBusConnection *connection,
                                       gulong          uid)
{
    /* just allow root or same user connect to ibus */
    if (uid == 0 || uid == getuid ())
        return TRUE;
    return FALSE;
}

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 (bus_connection_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 (bus_connection_parent_class)->dbus_signal (
            IBUS_CONNECTION (connection), message);
    return retval;
}
#endif

const gchar *
bus_connection_get_unique_name (BusConnection   *connection)
{
    return connection->unique_name;
}

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

const GList *
bus_connection_get_names (BusConnection   *connection)
{
    return connection->names;
}

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

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

    return new_name;
}

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

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

    if (link) {
        g_free (link->data);
        connection->names = g_list_delete_link (connection->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;

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

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

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

}

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