2 * ga-service-browser.c - Source for GaServiceBrowser
3 * Copyright (C) 2005 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <avahi-client/client.h>
28 #include <avahi-client/lookup.h>
29 #include <avahi-common/error.h>
31 #include "ga-service-browser.h"
32 #include "signals-marshal.h"
33 #include "ga-errors.h"
34 #include "ga-enums-enumtypes.h"
36 G_DEFINE_TYPE(GaServiceBrowser, ga_service_browser, G_TYPE_OBJECT)
48 static guint signals[LAST_SIGNAL] = { 0 };
59 /* private structure */
60 typedef struct _GaServiceBrowserPrivate GaServiceBrowserPrivate;
62 struct _GaServiceBrowserPrivate {
64 AvahiServiceBrowser *browser;
65 AvahiIfIndex interface;
66 AvahiProtocol protocol;
69 AvahiLookupFlags flags;
70 gboolean dispose_has_run;
73 #define GA_SERVICE_BROWSER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GA_TYPE_SERVICE_BROWSER, GaServiceBrowserPrivate))
75 static void ga_service_browser_init(GaServiceBrowser * obj) {
76 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(obj);
78 /* allocate any data required by the object here */
86 static void ga_service_browser_dispose(GObject * object);
87 static void ga_service_browser_finalize(GObject * object);
89 static void ga_service_browser_set_property(GObject * object,
91 const GValue * value, GParamSpec * pspec) {
92 GaServiceBrowser *browser = GA_SERVICE_BROWSER(object);
93 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(browser);
95 g_assert(priv->browser == NULL);
96 switch (property_id) {
98 priv->protocol = g_value_get_enum(value);
101 priv->interface = g_value_get_int(value);
104 priv->type = g_strdup(g_value_get_string(value));
107 priv->domain = g_strdup(g_value_get_string(value));
110 priv->flags = g_value_get_enum(value);
113 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
118 static void ga_service_browser_get_property(GObject * object,
120 GValue * value, GParamSpec * pspec) {
121 GaServiceBrowser *browser = GA_SERVICE_BROWSER(object);
122 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(browser);
124 switch (property_id) {
126 g_value_set_int(value, priv->protocol);
129 g_value_set_int(value, priv->interface);
132 g_value_set_string(value, priv->type);
135 g_value_set_string(value, priv->domain);
138 g_value_set_enum(value, priv->flags);
141 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
147 static void ga_service_browser_class_init(GaServiceBrowserClass *
148 ga_service_browser_class) {
149 GObjectClass *object_class = G_OBJECT_CLASS(ga_service_browser_class);
150 GParamSpec *param_spec;
152 g_type_class_add_private(ga_service_browser_class,
153 sizeof (GaServiceBrowserPrivate));
155 object_class->dispose = ga_service_browser_dispose;
156 object_class->finalize = ga_service_browser_finalize;
158 object_class->set_property = ga_service_browser_set_property;
159 object_class->get_property = ga_service_browser_get_property;
162 g_signal_new("new-service",
163 G_OBJECT_CLASS_TYPE(ga_service_browser_class),
167 ga_signals_marshal_VOID__INT_ENUM_STRING_STRING_STRING_UINT,
173 G_TYPE_STRING, GA_TYPE_LOOKUP_RESULT_FLAGS);
176 g_signal_new("removed-service",
177 G_OBJECT_CLASS_TYPE(ga_service_browser_class),
181 ga_signals_marshal_VOID__INT_ENUM_STRING_STRING_STRING_UINT,
187 G_TYPE_STRING, GA_TYPE_LOOKUP_RESULT_FLAGS);
189 signals[ALL_FOR_NOW] =
190 g_signal_new("all-for-now",
191 G_OBJECT_CLASS_TYPE(ga_service_browser_class),
195 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
197 signals[CACHE_EXHAUSTED] =
198 g_signal_new("cache-exhausted",
199 G_OBJECT_CLASS_TYPE(ga_service_browser_class),
203 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
206 g_signal_new("failure",
207 G_OBJECT_CLASS_TYPE(ga_service_browser_class),
211 g_cclosure_marshal_VOID__POINTER,
212 G_TYPE_NONE, 1, G_TYPE_POINTER);
214 param_spec = g_param_spec_enum("protocol", "Avahi protocol to browse",
215 "Avahi protocol to browse",
219 G_PARAM_STATIC_NAME |
220 G_PARAM_STATIC_BLURB);
221 g_object_class_install_property(object_class, PROP_PROTOCOL, param_spec);
223 param_spec = g_param_spec_int("interface", "interface index",
224 "Interface use for browser",
229 G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB);
230 g_object_class_install_property(object_class, PROP_IFINDEX, param_spec);
232 param_spec = g_param_spec_string("type", "service type",
233 "Service type to browse for",
236 G_PARAM_STATIC_NAME |
237 G_PARAM_STATIC_BLURB);
238 g_object_class_install_property(object_class, PROP_TYPE, param_spec);
240 param_spec = g_param_spec_string("domain", "service domain",
241 "Domain to browse in",
244 G_PARAM_STATIC_NAME |
245 G_PARAM_STATIC_BLURB);
246 g_object_class_install_property(object_class, PROP_DOMAIN, param_spec);
248 param_spec = g_param_spec_enum("flags", "Lookup flags for the browser",
249 "Browser lookup flags",
253 G_PARAM_STATIC_NAME |
254 G_PARAM_STATIC_BLURB);
255 g_object_class_install_property(object_class, PROP_FLAGS, param_spec);
258 void ga_service_browser_dispose(GObject * object) {
259 GaServiceBrowser *self = GA_SERVICE_BROWSER(object);
260 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(self);
262 if (priv->dispose_has_run)
265 priv->dispose_has_run = TRUE;
268 avahi_service_browser_free(priv->browser);
269 priv->browser = NULL;
271 g_object_unref(priv->client);
274 /* release any references held by the object here */
276 if (G_OBJECT_CLASS(ga_service_browser_parent_class)->dispose)
277 G_OBJECT_CLASS(ga_service_browser_parent_class)->dispose(object);
280 void ga_service_browser_finalize(GObject * object) {
281 GaServiceBrowser *self = GA_SERVICE_BROWSER(object);
282 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(self);
284 /* free any data held directly by the object here */
287 g_free(priv->domain);
290 G_OBJECT_CLASS(ga_service_browser_parent_class)->finalize(object);
293 static void _avahi_service_browser_cb(AvahiServiceBrowser * b, AvahiIfIndex interface,
294 AvahiProtocol protocol, AvahiBrowserEvent event,
295 const char *name, const char *type,
296 const char *domain, AvahiLookupResultFlags flags,
298 GaServiceBrowser *self = GA_SERVICE_BROWSER(userdata);
299 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(self);
300 if (priv->browser == NULL) {
303 g_assert(priv->browser == b);
306 case AVAHI_BROWSER_NEW:
307 case AVAHI_BROWSER_REMOVE:{
309 signalid = (event == AVAHI_BROWSER_NEW ? NEW : REMOVED);
310 g_signal_emit(self, signals[signalid], 0,
311 interface, protocol, name, type, domain, flags);
314 case AVAHI_BROWSER_CACHE_EXHAUSTED:
315 g_signal_emit(self, signals[CACHE_EXHAUSTED], 0);
317 case AVAHI_BROWSER_ALL_FOR_NOW:
318 g_signal_emit(self, signals[ALL_FOR_NOW], 0);
320 case AVAHI_BROWSER_FAILURE:{
322 int aerrno = avahi_client_errno(priv->client->avahi_client);
323 error = g_error_new(GA_ERRORS, aerrno,
324 "Browsing failed: %s",
325 avahi_strerror(aerrno));
326 g_signal_emit(self, signals[FAILURE], 0, error);
333 GaServiceBrowser *ga_service_browser_new(gchar * type) {
334 return ga_service_browser_new_full(AVAHI_IF_UNSPEC,
335 AVAHI_PROTO_UNSPEC, type, NULL, 0);
338 GaServiceBrowser *ga_service_browser_new_full(AvahiIfIndex interface,
339 AvahiProtocol protocol,
340 gchar * type, gchar * domain,
341 GaLookupFlags flags) {
342 return g_object_new(GA_TYPE_SERVICE_BROWSER,
343 "interface", interface,
344 "protocol", protocol,
345 "type", type, "domain", domain, "flags", flags, NULL);
348 gboolean ga_service_browser_attach(GaServiceBrowser * browser,
349 GaClient * client, GError ** error) {
350 GaServiceBrowserPrivate *priv = GA_SERVICE_BROWSER_GET_PRIVATE(browser);
352 g_object_ref(client);
353 priv->client = client;
355 priv->browser = avahi_service_browser_new(client->avahi_client,
358 priv->type, priv->domain,
360 _avahi_service_browser_cb,
362 if (priv->browser == NULL) {
364 int aerrno = avahi_client_errno(client->avahi_client);
365 *error = g_error_new(GA_ERRORS, aerrno,
366 "Attaching group failed: %s",
367 avahi_strerror(aerrno));