]> git.meshlink.io Git - catta/blob - avahi-client/browser.c
bcd991496c26b27c292735754f1a171741d73a7d
[catta] / avahi-client / browser.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
37
38 #include <stdlib.h>
39
40 #include "client.h"
41 #include "internal.h"
42
43 /* AvahiDomainBrowser */
44
45 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
46 {
47     AvahiDomainBrowser *tmp = NULL;
48     DBusMessage *message = NULL, *reply;
49     DBusError error;
50     char *path;
51
52     if (client == NULL)
53         return NULL;
54
55     dbus_error_init (&error);
56
57     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
58             AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
59
60     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &btype, DBUS_TYPE_INVALID))
61         goto dbus_error;
62
63     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
64
65     if (dbus_error_is_set (&error) || reply == NULL)
66         goto dbus_error;
67
68     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
69         goto dbus_error;
70
71     if (dbus_error_is_set (&error) || path == NULL)
72         goto dbus_error;
73
74     tmp = malloc (sizeof (AvahiDomainBrowser));
75     tmp->client = client;
76     tmp->callback = callback;
77     tmp->user_data = user_data;
78     tmp->path = strdup (path);
79
80     AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
81
82     return tmp;
83
84 dbus_error:
85     dbus_error_free (&error);
86     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
87
88     return NULL;
89 }
90
91 char*
92 avahi_domain_browser_path (AvahiDomainBrowser *b)
93 {
94     return b->path;
95 }
96
97 DBusHandlerResult
98 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
99 {
100     AvahiDomainBrowser *n, *db = NULL;
101     DBusError error;
102     const char *path;
103     char *domain;
104     int interface, protocol;
105
106     dbus_error_init (&error);
107
108     path = dbus_message_get_path (message);
109
110     if (path == NULL)
111         goto out;
112
113     for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
114     {
115         printf ("cmp: %s, %s\n", n->path, path);
116         if (strcmp (n->path, path) == 0) {
117             db = n;
118             break;
119         }
120     }
121
122     if (db == NULL)
123         goto out;
124
125     dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
126             DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
127
128     if (dbus_error_is_set (&error))
129         goto out;
130
131     db->callback (db, interface, protocol, event, domain, db->user_data);
132
133     return DBUS_HANDLER_RESULT_HANDLED;
134
135 out:
136     dbus_error_free (&error);
137     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
138 }
139
140 /* AvahiServiceTypeBrowser */
141 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
142 {
143     AvahiServiceTypeBrowser *tmp = NULL;
144     DBusMessage *message = NULL, *reply;
145     DBusError error;
146     char *path;
147
148     if (client == NULL)
149         return NULL;
150
151     dbus_error_init (&error);
152
153     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
154             AVAHI_DBUS_PATH_SERVER,
155             AVAHI_DBUS_INTERFACE_SERVER,
156             "ServiceTypeBrowserNew");
157
158     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
159         goto dbus_error;
160
161     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
162
163     if (dbus_error_is_set (&error) || reply == NULL)
164         goto dbus_error;
165
166     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
167         goto dbus_error;
168
169     if (dbus_error_is_set (&error) || path == NULL)
170         goto dbus_error;
171
172     tmp = malloc (sizeof (AvahiServiceTypeBrowser));
173     tmp->client = client;
174     tmp->callback = callback;
175     tmp->user_data = user_data;
176     tmp->path = strdup (path);
177
178     AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
179
180     return tmp;
181
182 dbus_error:
183     dbus_error_free (&error);
184     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
185
186     return NULL;
187 }
188
189 char*
190 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
191 {
192     return b->path;
193 }
194
195 DBusHandlerResult
196 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
197 {
198     AvahiServiceTypeBrowser *n, *db = NULL;
199     DBusError error;
200     const char *path;
201     char *domain, *type;
202     int interface, protocol;
203
204     dbus_error_init (&error);
205
206     path = dbus_message_get_path (message);
207
208     if (path == NULL)
209         goto out;
210
211     for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
212     {
213         printf ("cmp: %s, %s\n", n->path, path);
214         if (strcmp (n->path, path) == 0) {
215             db = n;
216             break;
217         }
218     }
219
220     if (db == NULL)
221         goto out;
222
223     dbus_message_get_args (message, &error,
224             DBUS_TYPE_INT32, &interface,
225             DBUS_TYPE_INT32, &protocol,
226             DBUS_TYPE_STRING, &type,
227             DBUS_TYPE_STRING, &domain,
228             DBUS_TYPE_INVALID);
229     
230     if (dbus_error_is_set (&error))
231         goto out;
232
233     db->callback (db, interface, protocol, event, type, domain, db->user_data);
234
235     return DBUS_HANDLER_RESULT_HANDLED;
236
237 out:
238     dbus_error_free (&error);
239     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
240 }
241
242 /* AvahiServiceBrowser */
243
244 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data)
245 {
246     AvahiServiceBrowser *tmp = NULL;
247     DBusMessage *message = NULL, *reply;
248     DBusError error;
249     char *path;
250
251     if (client == NULL)
252         return NULL;
253
254     dbus_error_init (&error);
255
256     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
257             AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
258
259     if (!dbus_message_append_args (message,
260                 DBUS_TYPE_INT32, &interface,
261                 DBUS_TYPE_INT32, &protocol,
262                 DBUS_TYPE_STRING, &type,
263                 DBUS_TYPE_STRING, &domain,
264                 DBUS_TYPE_INVALID))
265         goto dbus_error;
266
267     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
268
269     if (dbus_error_is_set (&error) || reply == NULL)
270         goto dbus_error;
271
272     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
273         goto dbus_error;
274
275     if (dbus_error_is_set (&error) || path == NULL)
276         goto dbus_error;
277
278     tmp = malloc (sizeof (AvahiServiceBrowser));
279     tmp->client = client;
280     tmp->callback = callback;
281     tmp->user_data = user_data;
282     tmp->path = strdup (path);
283
284     AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
285
286     return tmp;
287
288 dbus_error:
289     dbus_error_free (&error);
290     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
291
292     return NULL;
293 }
294
295 char*
296 avahi_service_browser_path (AvahiServiceBrowser *b)
297 {
298     return b->path;
299 }
300
301 DBusHandlerResult
302 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
303 {
304     AvahiServiceBrowser *n, *db = NULL;
305     DBusError error;
306     const char *path;
307     char *name, *type, *domain;
308     int interface, protocol;
309
310     dbus_error_init (&error);
311
312     path = dbus_message_get_path (message);
313
314     if (path == NULL)
315         goto out;
316
317     for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
318     {
319         printf ("cmp: %s, %s\n", n->path, path);
320         if (strcmp (n->path, path) == 0) {
321             db = n;
322             break;
323         }
324     }
325
326     if (db == NULL)
327         goto out;
328
329     dbus_message_get_args (message, &error,
330             DBUS_TYPE_INT32, &interface,
331             DBUS_TYPE_INT32, &protocol,
332             DBUS_TYPE_STRING, &name,
333             DBUS_TYPE_STRING, &type,
334             DBUS_TYPE_STRING, &domain,
335             DBUS_TYPE_INVALID);
336
337     if (dbus_error_is_set (&error))
338         goto out;
339
340     db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
341
342     return DBUS_HANDLER_RESULT_HANDLED;
343
344 out:
345     dbus_error_free (&error);
346     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
347 }
348
349