]> git.meshlink.io Git - catta/blob - avahi-client/browser.c
* Add DomainBrowser support to C client API
[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* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
44 {
45     AvahiDomainBrowser *tmp = NULL;
46     DBusMessage *message = NULL, *reply;
47     DBusError error;
48     char *path;
49
50     if (client == NULL)
51         return NULL;
52
53     dbus_error_init (&error);
54
55     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
56             AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
57
58     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))
59         goto dbus_error;
60
61     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
62
63     if (dbus_error_is_set (&error) || reply == NULL)
64         goto dbus_error;
65
66     if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
67         goto dbus_error;
68
69     if (dbus_error_is_set (&error) || path == NULL)
70         goto dbus_error;
71
72     tmp = malloc (sizeof (AvahiDomainBrowser));
73     tmp->client = client;
74     tmp->callback = callback;
75     tmp->user_data = user_data;
76     tmp->path = strdup (path);
77
78     AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
79
80     return tmp;
81
82 dbus_error:
83     dbus_error_free (&error);
84     avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
85
86     return NULL;
87 }
88
89 char*
90 avahi_domain_browser_path (AvahiDomainBrowser *b)
91 {
92     return b->path;
93 }
94
95 DBusHandlerResult
96 avahi_entry_group_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
97 {
98     AvahiDomainBrowser *n, *db = NULL;
99     DBusError error;
100     const char *path;
101     char *domain;
102     int interface, protocol;
103
104     dbus_error_init (&error);
105
106     path = dbus_message_get_path (message);
107
108     printf ("bailing out 1\n");
109     if (path == NULL)
110         goto out;
111
112     for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
113     {
114         printf ("cmp: %s, %s\n", n->path, path);
115         if (strcmp (n->path, path) == 0) {
116             db = n;
117             break;
118         }
119     }
120
121     printf ("bailing out 2\n");
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     printf ("bailing out 3\n");
129     if (dbus_error_is_set (&error))
130         goto out;
131
132     db->callback (db, interface, protocol, event, domain, db->user_data);
133
134     return DBUS_HANDLER_RESULT_HANDLED;
135
136 out:
137     dbus_error_free (&error);
138     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
139 }