5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #define AGENT_INTERFACE "org.openobex.Agent"
35 struct pending_request {
36 DBusPendingCall *call;
37 DBusPendingCallNotifyFunction function;
39 DBusFreeFunction destroy;
49 struct pending_request *pending;
52 static void pending_request_free(struct pending_request *req)
55 dbus_pending_call_unref(req->call);
58 req->destroy(req->data);
63 void obc_agent_free(struct obc_agent *agent)
66 g_dbus_remove_watch(agent->conn, agent->watch);
69 if (agent->pending->call)
70 dbus_pending_call_cancel(agent->pending->call);
71 pending_request_free(agent->pending);
74 dbus_connection_unref(agent->conn);
80 static void agent_disconnected(DBusConnection *connection, void *user_data)
82 struct obc_agent *agent = user_data;
87 agent->destroy(agent, agent->data);
89 obc_agent_free(agent);
92 struct obc_agent *obc_agent_create(DBusConnection *conn, const char *name,
93 const char *path, GFunc destroy,
96 struct obc_agent *agent;
98 agent = g_new0(struct obc_agent, 1);
99 agent->conn = dbus_connection_ref(conn);
100 agent->name = g_strdup(name);
101 agent->path = g_strdup(path);
102 agent->destroy = destroy;
103 agent->data = user_data;
105 agent->watch = g_dbus_add_disconnect_watch(conn, name,
112 static void agent_request_reply(DBusPendingCall *call, void *user_data)
114 struct obc_agent *agent = user_data;
115 struct pending_request *req = agent->pending;
117 agent->pending = NULL;
120 req->function(call, req->data);
122 pending_request_free(req);
125 int obc_agent_request(struct obc_agent *agent, const char *path,
126 DBusPendingCallNotifyFunction function,
127 void *user_data, DBusFreeFunction destroy)
129 struct pending_request *req;
130 DBusMessage *message;
137 message = dbus_message_new_method_call(agent->name,
138 agent->path, AGENT_INTERFACE, "Request");
140 dbus_message_append_args(message,
141 DBUS_TYPE_OBJECT_PATH, &path,
144 req = g_new0(struct pending_request, 1);
145 req->function = function;
146 req->destroy = destroy;
147 req->data = user_data;
149 if (!dbus_connection_send_with_reply(agent->conn, message,
152 dbus_message_unref(message);
156 agent->pending = req;
158 dbus_message_unref(message);
160 dbus_pending_call_set_notify(req->call, agent_request_reply,
166 void obc_agent_notify_progress(struct obc_agent *agent, const char *path,
169 DBusMessage *message;
173 message = dbus_message_new_method_call(agent->name,
174 agent->path, AGENT_INTERFACE, "Progress");
178 dbus_message_set_no_reply(message, TRUE);
180 dbus_message_append_args(message,
181 DBUS_TYPE_OBJECT_PATH, &path,
182 DBUS_TYPE_UINT64, &transferred,
185 g_dbus_send_message(agent->conn, message);
188 void obc_agent_notify_complete(struct obc_agent *agent, const char *path)
190 DBusMessage *message;
194 message = dbus_message_new_method_call(agent->name,
195 agent->path, AGENT_INTERFACE, "Complete");
199 dbus_message_set_no_reply(message, TRUE);
201 dbus_message_append_args(message,
202 DBUS_TYPE_OBJECT_PATH, &path,
205 g_dbus_send_message(agent->conn, message);
208 void obc_agent_notify_error(struct obc_agent *agent, const char *path,
211 DBusMessage *message;
215 message = dbus_message_new_method_call(agent->name,
216 agent->path, AGENT_INTERFACE, "Error");
220 dbus_message_set_no_reply(message, TRUE);
222 dbus_message_append_args(message,
223 DBUS_TYPE_OBJECT_PATH, &path,
224 DBUS_TYPE_STRING, &err,
227 g_dbus_send_message(agent->conn, message);
230 void obc_agent_release(struct obc_agent *agent)
232 DBusMessage *message;
236 message = dbus_message_new_method_call(agent->name,
237 agent->path, AGENT_INTERFACE, "Release");
239 dbus_message_set_no_reply(message, TRUE);
241 g_dbus_send_message(agent->conn, message);
244 const char *obc_agent_get_name(struct obc_agent *agent)
249 const char *obc_agent_get_path(struct obc_agent *agent)