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
41 static GSList *drivers = NULL;
43 static GSList *watches = NULL;
47 obex_object_io_func func;
51 void obex_object_set_io_flags(void *object, int flags, int err)
55 for (l = watches; l;) {
56 struct io_watch *watch = l->data;
60 if (watch->object != object)
63 if (watch->func(object, flags, err, watch->user_data) == TRUE)
66 if (g_slist_find(watches, watch) == NULL)
69 watches = g_slist_remove(watches, watch);
74 static struct io_watch *find_io_watch(void *object)
78 for (l = watches; l; l = l->next) {
79 struct io_watch *watch = l->data;
81 if (watch->object == object)
88 static void reset_io_watch(void *object)
90 struct io_watch *watch;
92 watch = find_io_watch(object);
96 watches = g_slist_remove(watches, watch);
100 static int set_io_watch(void *object, obex_object_io_func func,
103 struct io_watch *watch;
106 reset_io_watch(object);
110 watch = find_io_watch(object);
114 watch = g_new0(struct io_watch, 1);
115 watch->object = object;
117 watch->user_data = user_data;
119 watches = g_slist_append(watches, watch);
124 static struct obex_mime_type_driver *find_driver(const uint8_t *target,
125 unsigned int target_size,
126 const char *mimetype, const uint8_t *who,
127 unsigned int who_size)
131 for (l = drivers; l; l = l->next) {
132 struct obex_mime_type_driver *driver = l->data;
134 if (memncmp0(target, target_size, driver->target, driver->target_size))
137 if (memncmp0(who, who_size, driver->who, driver->who_size))
140 if (mimetype == NULL || driver->mimetype == NULL) {
141 if (mimetype == driver->mimetype)
147 if (g_ascii_strcasecmp(mimetype, driver->mimetype) == 0)
154 struct obex_mime_type_driver *obex_mime_type_driver_find(const uint8_t *target,
155 unsigned int target_size,
156 const char *mimetype, const uint8_t *who,
157 unsigned int who_size)
159 struct obex_mime_type_driver *driver;
161 driver = find_driver(target, target_size, mimetype, who, who_size);
162 if (driver == NULL) {
164 /* Fallback to non-who specific */
165 driver = find_driver(target, target_size, mimetype, NULL, 0);
170 if (mimetype != NULL)
171 /* Fallback to target default */
172 driver = find_driver(target, target_size, NULL, NULL, 0);
175 /* Fallback to general default */
176 driver = find_driver(NULL, 0, NULL, NULL, 0);
182 int obex_mime_type_driver_register(struct obex_mime_type_driver *driver)
185 error("Invalid driver");
189 if (find_driver(driver->target, driver->target_size, driver->mimetype,
190 driver->who, driver->who_size)) {
191 error("Permission denied: %s could not be registered",
196 if (driver->set_io_watch == NULL)
197 driver->set_io_watch = set_io_watch;
199 DBG("driver %p mimetype %s registered", driver, driver->mimetype);
201 drivers = g_slist_append(drivers, driver);
206 void obex_mime_type_driver_unregister(struct obex_mime_type_driver *driver)
208 if (!g_slist_find(drivers, driver)) {
209 error("Unable to unregister: No such driver %p", driver);
213 DBG("driver %p mimetype %s unregistered", driver, driver->mimetype);
215 drivers = g_slist_remove(drivers, driver);