~ubuntu-branches/ubuntu/precise/gnome-bluetooth/precise-updates

« back to all changes in this revision

Viewing changes to common/obex-agent.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack
  • Date: 2009-08-05 13:34:38 UTC
  • mto: (2.2.1 experimental) (1.4.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090805133438-791u4ywsppj71d9y
Tags: upstream-2.27.8
ImportĀ upstreamĀ versionĀ 2.27.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  BlueZ - Bluetooth protocol stack for Linux
4
 
 *
5
 
 *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
6
 
 *
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 as published by the Free Software Foundation; either
11
 
 *  version 2.1 of the License, or (at your option) any later version.
12
 
 *
13
 
 *  This library is distributed in the hope that it will be useful,
14
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 *  Lesser General Public License for more details.
17
 
 *
18
 
 *  You should have received a copy of the GNU Lesser General Public
19
 
 *  License along with this library; if not, write to the Free Software
20
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
 
 *
22
 
 */
23
 
 
24
 
#ifdef HAVE_CONFIG_H
25
 
#include <config.h>
26
 
#endif
27
 
 
28
 
#include <stdio.h>
29
 
#include <dbus/dbus-glib.h>
30
 
#include <dbus/dbus-glib-lowlevel.h>
31
 
 
32
 
#include "obex-agent.h"
33
 
 
34
 
#ifdef DEBUG
35
 
#define DBG(fmt, arg...) printf("%s:%s() " fmt "\n", __FILE__, __FUNCTION__ , ## arg)
36
 
#else
37
 
#define DBG(fmt...)
38
 
#endif
39
 
 
40
 
#define OBEX_SERVICE    "org.openobex.client"
41
 
 
42
 
#define OBEX_CLIENT_PATH        "/"
43
 
#define OBEX_CLIENT_INTERFACE   "org.openobex.Client"
44
 
#define OBEX_TRANSFER_INTERFACE "org.openobex.Transfer"
45
 
 
46
 
static DBusGConnection *connection = NULL;
47
 
 
48
 
#define OBEX_AGENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
49
 
                                        OBEX_TYPE_AGENT, ObexAgentPrivate))
50
 
 
51
 
typedef struct _ObexAgentPrivate ObexAgentPrivate;
52
 
 
53
 
struct _ObexAgentPrivate {
54
 
        gchar *busname;
55
 
        gchar *path;
56
 
 
57
 
        ObexAgentReleaseFunc release_func;
58
 
        gpointer release_data;
59
 
 
60
 
        ObexAgentRequestFunc request_func;
61
 
        gpointer request_data;
62
 
 
63
 
        ObexAgentProgressFunc progress_func;
64
 
        gpointer progress_data;
65
 
 
66
 
        ObexAgentCompleteFunc complete_func;
67
 
        gpointer complete_data;
68
 
};
69
 
 
70
 
G_DEFINE_TYPE(ObexAgent, obex_agent, G_TYPE_OBJECT)
71
 
 
72
 
static gboolean obex_agent_request(ObexAgent *agent, const char *path,
73
 
                                                DBusGMethodInvocation *context)
74
 
{
75
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
76
 
        const char *sender = dbus_g_method_get_sender(context);
77
 
        gboolean result = FALSE;
78
 
 
79
 
        DBG("agent %p sender %s", agent, sender);
80
 
 
81
 
        if (g_str_equal(sender, priv->busname) == FALSE)
82
 
                return FALSE;
83
 
 
84
 
        if (priv->request_func) {
85
 
                DBusGProxy *proxy;
86
 
 
87
 
                proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
88
 
                                                path, OBEX_TRANSFER_INTERFACE);
89
 
 
90
 
                result = priv->request_func(context, proxy,
91
 
                                                priv->request_data);
92
 
 
93
 
                g_object_unref(proxy);
94
 
        } else
95
 
                dbus_g_method_return(context, "");
96
 
 
97
 
        return TRUE;
98
 
}
99
 
 
100
 
static gboolean obex_agent_progress(ObexAgent *agent, const char *path,
101
 
                        guint64 transferred, DBusGMethodInvocation *context)
102
 
{
103
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
104
 
        const char *sender = dbus_g_method_get_sender(context);
105
 
        gboolean result = FALSE;
106
 
 
107
 
        DBG("agent %p sender %s", agent, sender);
108
 
 
109
 
        if (g_str_equal(sender, priv->busname) == FALSE)
110
 
                return FALSE;
111
 
 
112
 
        if (priv->progress_func) {
113
 
                DBusGProxy *proxy;
114
 
 
115
 
                proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
116
 
                                                path, OBEX_TRANSFER_INTERFACE);
117
 
 
118
 
                result = priv->progress_func(context, proxy, transferred,
119
 
                                                        priv->progress_data);
120
 
 
121
 
                g_object_unref(proxy);
122
 
        } else
123
 
                dbus_g_method_return(context);
124
 
 
125
 
        return result;
126
 
}
127
 
 
128
 
static gboolean obex_agent_complete(ObexAgent *agent, const char *path,
129
 
                                                DBusGMethodInvocation *context)
130
 
{
131
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
132
 
        const char *sender = dbus_g_method_get_sender(context);
133
 
        gboolean result = FALSE;
134
 
 
135
 
        DBG("agent %p sender %s", agent, sender);
136
 
 
137
 
        if (g_str_equal(sender, priv->busname) == FALSE)
138
 
                return FALSE;
139
 
 
140
 
        if (priv->complete_func) {
141
 
                DBusGProxy *proxy;
142
 
 
143
 
                proxy = dbus_g_proxy_new_for_name(connection, OBEX_SERVICE,
144
 
                                                path, OBEX_TRANSFER_INTERFACE);
145
 
 
146
 
                result = priv->complete_func(context, proxy,
147
 
                                                priv->complete_data);
148
 
 
149
 
                g_object_unref(proxy);
150
 
        } else
151
 
                dbus_g_method_return(context);
152
 
 
153
 
        return result;
154
 
}
155
 
 
156
 
static gboolean obex_agent_release(ObexAgent *agent,
157
 
                                                DBusGMethodInvocation *context)
158
 
{
159
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
160
 
        const char *sender = dbus_g_method_get_sender(context);
161
 
        gboolean result = FALSE;
162
 
 
163
 
        DBG("agent %p sender %s", agent, sender);
164
 
 
165
 
        if (g_str_equal(sender, priv->busname) == FALSE)
166
 
                return FALSE;
167
 
 
168
 
        if (priv->release_func)
169
 
                result = priv->release_func(context, priv->release_data);
170
 
        else
171
 
                dbus_g_method_return(context);
172
 
 
173
 
        g_object_unref(agent);
174
 
 
175
 
        return result;
176
 
}
177
 
 
178
 
#include "obex-agent-glue.h"
179
 
 
180
 
static void obex_agent_init(ObexAgent *agent)
181
 
{
182
 
        DBG("agent %p", agent);
183
 
}
184
 
 
185
 
static void obex_agent_finalize(GObject *agent)
186
 
{
187
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
188
 
 
189
 
        DBG("agent %p", agent);
190
 
 
191
 
        g_free(priv->path);
192
 
        g_free(priv->busname);
193
 
 
194
 
        G_OBJECT_CLASS(obex_agent_parent_class)->finalize(agent);
195
 
}
196
 
 
197
 
static void obex_agent_class_init(ObexAgentClass *klass)
198
 
{
199
 
        GObjectClass *object_class = (GObjectClass *) klass;
200
 
        GError *error = NULL;
201
 
 
202
 
        DBG("class %p", klass);
203
 
 
204
 
        g_type_class_add_private(klass, sizeof(ObexAgentPrivate));
205
 
 
206
 
        object_class->finalize = obex_agent_finalize;
207
 
 
208
 
        connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
209
 
 
210
 
        if (error != NULL) {
211
 
                g_printerr("Connecting to session bus failed: %s\n",
212
 
                                                        error->message);
213
 
                g_error_free(error);
214
 
        }
215
 
 
216
 
        dbus_g_object_type_install_info(OBEX_TYPE_AGENT,
217
 
                                        &dbus_glib_obex_agent_object_info);
218
 
 
219
 
        //dbus_g_error_domain_register(AGENT_ERROR, "org.openobex.Error",
220
 
        //                                              AGENT_ERROR_TYPE);
221
 
}
222
 
 
223
 
ObexAgent *obex_agent_new(void)
224
 
{
225
 
        ObexAgent *agent;
226
 
 
227
 
        agent = OBEX_AGENT(g_object_new(OBEX_TYPE_AGENT, NULL));
228
 
 
229
 
        DBG("agent %p", agent);
230
 
 
231
 
        return agent;
232
 
}
233
 
 
234
 
gboolean obex_agent_setup(ObexAgent *agent, const char *path)
235
 
{
236
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
237
 
        DBusGProxy *proxy;
238
 
        GObject *object;
239
 
 
240
 
        DBG("agent %p path %s", agent, path);
241
 
 
242
 
        if (priv->path != NULL)
243
 
                return FALSE;
244
 
 
245
 
        priv->path = g_strdup(path);
246
 
 
247
 
        proxy = dbus_g_proxy_new_for_name_owner(connection, OBEX_SERVICE,
248
 
                        OBEX_CLIENT_PATH, OBEX_CLIENT_INTERFACE, NULL);
249
 
 
250
 
        g_free(priv->busname);
251
 
 
252
 
        if (proxy != NULL) {
253
 
                priv->busname = g_strdup(dbus_g_proxy_get_bus_name(proxy));
254
 
                g_object_unref(proxy);
255
 
        } else
256
 
                priv->busname = NULL;
257
 
 
258
 
        object = dbus_g_connection_lookup_g_object(connection, priv->path);
259
 
        if (object != NULL)
260
 
                g_object_unref(object);
261
 
 
262
 
        dbus_g_connection_register_g_object(connection,
263
 
                                                priv->path, G_OBJECT(agent));
264
 
 
265
 
        return TRUE;
266
 
}
267
 
 
268
 
void obex_agent_set_release_func(ObexAgent *agent,
269
 
                                ObexAgentReleaseFunc func, gpointer data)
270
 
{
271
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
272
 
 
273
 
        DBG("agent %p", agent);
274
 
 
275
 
        priv->release_func = func;
276
 
        priv->release_data = data;
277
 
}
278
 
 
279
 
void obex_agent_set_request_func(ObexAgent *agent,
280
 
                                ObexAgentRequestFunc func, gpointer data)
281
 
{
282
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
283
 
 
284
 
        DBG("agent %p", agent);
285
 
 
286
 
        priv->request_func = func;
287
 
        priv->request_data = data;
288
 
}
289
 
 
290
 
void obex_agent_set_progress_func(ObexAgent *agent,
291
 
                                ObexAgentProgressFunc func, gpointer data)
292
 
{
293
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
294
 
 
295
 
        DBG("agent %p", agent);
296
 
 
297
 
        priv->progress_func = func;
298
 
        priv->progress_data = data;
299
 
}
300
 
 
301
 
void obex_agent_set_complete_func(ObexAgent *agent,
302
 
                                ObexAgentCompleteFunc func, gpointer data)
303
 
{
304
 
        ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
305
 
 
306
 
        DBG("agent %p", agent);
307
 
 
308
 
        priv->complete_func = func;
309
 
        priv->complete_data = data;
310
 
}