]> git.meshlink.io Git - catta/blob - avahi-client/entrygroup.c
* Remove AvahiEntryGroups from the client's list when we free it
[catta] / avahi-client / entrygroup.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 void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
44 {
45     if (group == NULL || group->callback == NULL)
46         return;
47
48     group->callback (group, state, group->user_data);
49 }
50
51 AvahiEntryGroup*
52 avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
53 {
54     AvahiEntryGroup *tmp = NULL;
55     DBusMessage *message = NULL, *reply;
56     DBusError error;
57     char *path;
58
59     if (client == NULL)
60         return NULL;
61     
62     dbus_error_init (&error);
63
64     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
65             AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
66
67     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
68
69     if (dbus_error_is_set (&error))
70     {
71         dbus_error_free (&error);
72
73         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
74         goto fail;
75     }
76
77     if (reply == NULL)
78     {
79
80         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
81         goto fail;
82     }
83
84     dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
85
86     if (dbus_error_is_set (&error))
87     {
88         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
89         goto fail;
90     }
91
92     tmp = malloc (sizeof (AvahiEntryGroup));
93
94     tmp->client = client;
95
96     tmp->path = strdup (path);
97     tmp->callback = callback;
98     tmp->user_data = user_data;
99
100     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
101
102     dbus_message_unref (message);
103
104     avahi_client_set_errno (client, AVAHI_OK);
105     return tmp;
106
107 fail:
108     if (tmp) free (tmp);
109     if (message) dbus_message_unref (message);
110     return NULL;
111 }
112
113 int
114 avahi_entry_group_free (AvahiEntryGroup *group)
115 {
116     AvahiClient *client = group->client;
117     DBusMessage *message;
118
119     if (group == NULL || group->path == NULL)
120         return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
121
122     message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
123             group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free");
124
125     if (message == NULL)
126         return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
127
128     dbus_connection_send (client->bus, message, NULL);
129     
130     AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, client->groups, group);
131
132     free (group);
133
134     return avahi_client_set_errno (client, AVAHI_OK);
135 }
136
137 int
138 avahi_entry_group_commit (AvahiEntryGroup *group)
139 {
140     DBusMessage *message;
141     DBusError error;
142
143     dbus_error_init (&error);
144
145     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
146             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
147
148     dbus_connection_send (group->client->bus, message, NULL);
149
150     return avahi_client_set_errno (group->client, AVAHI_OK);
151 }
152
153 int
154 avahi_entry_group_reset (AvahiEntryGroup *group)
155 {
156     DBusMessage *message;
157
158     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
159             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
160
161     dbus_connection_send (group->client->bus, message, NULL);
162
163     return avahi_client_set_errno (group->client, AVAHI_OK);
164 }
165
166 int
167 avahi_entry_group_get_state (AvahiEntryGroup *group)
168 {
169     DBusMessage *message, *reply;
170     DBusError error;
171     int state;
172
173     dbus_error_init (&error);
174
175     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
176             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
177
178     reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
179
180     if (dbus_error_is_set (&error))
181     {
182         dbus_error_free (&error);
183
184         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
185     }
186
187     dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
188
189     if (dbus_error_is_set (&error))
190     {
191         dbus_error_free (&error);
192
193         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
194     }
195
196     avahi_client_set_errno (group->client, AVAHI_OK);
197     return state;
198 }
199
200 int
201 avahi_client_errno (AvahiClient *client)
202 {
203     return client->error;
204 }
205
206 AvahiClient*
207 avahi_entry_group_get_client (AvahiEntryGroup *group)
208 {
209     return group->client;
210 }
211
212 int
213 avahi_entry_group_is_empty (AvahiEntryGroup *group)
214 {
215     return AVAHI_OK;
216 }
217
218 int
219 avahi_entry_group_add_service (AvahiEntryGroup *group,
220                                AvahiIfIndex interface,
221                                AvahiProtocol protocol,
222                                const char *name,
223                                const char *type,
224                                const char *domain,
225                                const char *host,
226                                uint16_t port,
227                                AvahiStringList *txt)
228 {
229     DBusMessage *message;
230     DBusMessageIter iter, sub;
231     AvahiStringList *p;
232
233     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
234             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
235
236     if (!message)
237     {
238         dbus_message_unref (message);
239         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
240     }
241
242     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
243                 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
244                 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
245     {
246         dbus_message_unref (message);
247         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
248     }
249     
250     dbus_message_iter_init_append(message, &iter);
251     dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
252
253     /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
254     for (p = txt; p != NULL; p = p->next) {
255         DBusMessageIter sub2;
256         const guint8 *data = p->text;
257
258         dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
259         dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
260         dbus_message_iter_close_container(&sub, &sub2);
261     }
262
263     dbus_message_iter_close_container(&iter, &sub);
264
265     dbus_connection_send (group->client->bus, message, NULL);
266
267     return avahi_client_set_errno (group->client, AVAHI_OK);
268 }
269
270 /* XXX: debug function */
271 char* avahi_entry_group_path (AvahiEntryGroup *group)
272 {
273     if (group != NULL) return group->path;
274     else return NULL;
275 }