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