5 * Copyright (C) 2007-2010 Intel Corporation
6 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
42 #define OBEX_SYNC_UUID "IRMC-SYNC"
43 #define OBEX_SYNC_UUID_LEN 9
45 #define SYNC_INTERFACE "org.bluez.obex.Synchronization"
46 #define ERROR_INF SYNC_INTERFACE ".Error"
47 #define SYNC_UUID "00001104-0000-1000-8000-00805f9b34fb"
50 struct obc_session *session;
55 static DBusConnection *conn = NULL;
57 static DBusMessage *sync_setlocation(DBusConnection *connection,
58 DBusMessage *message, void *user_data)
60 struct sync_data *sync = user_data;
62 char *path = NULL, *tmp;
64 if (dbus_message_get_args(message, NULL,
65 DBUS_TYPE_STRING, &location,
66 DBUS_TYPE_INVALID) == FALSE)
67 return g_dbus_create_error(message,
68 ERROR_INF ".InvalidArguments", NULL);
70 if (!g_ascii_strcasecmp(location, "INT") ||
71 !g_ascii_strcasecmp(location, "INTERNAL"))
72 path = g_strdup("telecom/pb.vcf");
73 else if (!g_ascii_strncasecmp(location, "SIM", 3)) {
74 tmp = g_ascii_strup(location, 4);
75 path = g_build_filename(tmp, "telecom/pb.vcf", NULL);
78 return g_dbus_create_error(message,
79 ERROR_INF ".InvalidArguments", "InvalidPhonebook");
81 g_free(sync->phonebook_path);
82 sync->phonebook_path = path;
84 return dbus_message_new_method_return(message);
87 static DBusMessage *sync_getphonebook(DBusConnection *connection,
88 DBusMessage *message, void *user_data)
90 struct sync_data *sync = user_data;
91 struct obc_transfer *transfer;
92 const char *target_file;
96 if (dbus_message_get_args(message, NULL,
97 DBUS_TYPE_STRING, &target_file,
98 DBUS_TYPE_INVALID) == FALSE)
99 return g_dbus_create_error(message,
100 ERROR_INF ".InvalidArguments",
101 "Invalid arguments in method call");
104 return g_dbus_create_error(message,
105 ERROR_INF ".InProgress", "Transfer in progress");
107 /* set default phonebook_path to memory internal phonebook */
108 if (!sync->phonebook_path)
109 sync->phonebook_path = g_strdup("telecom/pb.vcf");
111 transfer = obc_transfer_get("phonebook", sync->phonebook_path,
113 if (transfer == NULL)
116 if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
119 return obc_transfer_create_dbus_reply(transfer, message);
122 reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
128 static DBusMessage *sync_putphonebook(DBusConnection *connection,
129 DBusMessage *message, void *user_data)
131 struct sync_data *sync = user_data;
132 struct obc_transfer *transfer;
133 const char *source_file;
137 if (dbus_message_get_args(message, NULL,
138 DBUS_TYPE_STRING, &source_file,
139 DBUS_TYPE_INVALID) == FALSE)
140 return g_dbus_create_error(message,
141 ERROR_INF ".InvalidArguments",
142 "Invalid arguments in method call");
144 /* set default phonebook_path to memory internal phonebook */
145 if (!sync->phonebook_path)
146 sync->phonebook_path = g_strdup("telecom/pb.vcf");
148 transfer = obc_transfer_put(NULL, sync->phonebook_path, source_file,
150 if (transfer == NULL)
153 if (!obc_session_queue(sync->session, transfer, NULL, NULL, &err))
156 return obc_transfer_create_dbus_reply(transfer, message);
159 reply = g_dbus_create_error(message, ERROR_INF ".Failed", "%s",
165 static const GDBusMethodTable sync_methods[] = {
166 { GDBUS_METHOD("SetLocation",
167 GDBUS_ARGS({ "location", "s" }), NULL,
169 { GDBUS_METHOD("GetPhonebook",
170 GDBUS_ARGS({ "targetfile", "s" }),
171 GDBUS_ARGS({ "transfer", "o" },
172 { "properties", "a{sv}" }),
173 sync_getphonebook) },
174 { GDBUS_METHOD("PutPhonebook",
175 GDBUS_ARGS({ "sourcefile", "s" }),
176 GDBUS_ARGS({ "transfer", "o" },
177 { "properties", "a{sv}" }),
178 sync_putphonebook) },
182 static void sync_free(void *data)
184 struct sync_data *sync = data;
186 obc_session_unref(sync->session);
187 g_free(sync->phonebook_path);
191 static int sync_probe(struct obc_session *session)
193 struct sync_data *sync;
196 path = obc_session_get_path(session);
200 sync = g_try_new0(struct sync_data, 1);
204 sync->session = obc_session_ref(session);
206 if (!g_dbus_register_interface(conn, path, SYNC_INTERFACE, sync_methods,
207 NULL, NULL, sync, sync_free)) {
215 static void sync_remove(struct obc_session *session)
217 const char *path = obc_session_get_path(session);
221 g_dbus_unregister_interface(conn, path, SYNC_INTERFACE);
224 static struct obc_driver sync = {
227 .target = OBEX_SYNC_UUID,
228 .target_len = OBEX_SYNC_UUID_LEN,
230 .remove = sync_remove
239 conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
243 err = obc_driver_register(&sync);
245 dbus_connection_unref(conn);
257 dbus_connection_unref(conn);
260 obc_driver_unregister(&sync);