2 * Copyright (C) 2009,2011 Nokia Corporation.
3 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
5 * Author: Jens Georg <mail@jensge.org>
6 * Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7 * Jorn Baayen <jorn@openedhand.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:gupnp-simple-context-manager
27 * @short_description: Abstract implementation of basic #GUPnPContextManager.
34 #include <libgssdp/gssdp-error.h>
36 #include "gupnp-simple-context-manager.h"
37 #include "gupnp-context.h"
39 G_DEFINE_ABSTRACT_TYPE (GUPnPSimpleContextManager,
40 gupnp_simple_context_manager,
41 GUPNP_TYPE_CONTEXT_MANAGER);
43 struct _GUPnPSimpleContextManagerPrivate {
44 GList *contexts; /* List of GUPnPContext instances */
46 GSource *idle_context_creation_src;
50 gupnp_simple_context_manager_get_interfaces (GUPnPSimpleContextManager *manager)
52 GUPnPSimpleContextManagerClass *object_class =
53 GUPNP_SIMPLE_CONTEXT_MANAGER_GET_CLASS (manager);
55 return object_class->get_interfaces (manager);
59 create_and_signal_context (const char *interface,
60 GUPnPSimpleContextManager *manager)
62 GUPnPContext *context;
67 g_object_get (manager,
72 context = g_initable_new (GUPNP_TYPE_CONTEXT,
75 "interface", interface,
79 if (!(error->domain == GSSDP_ERROR &&
80 error->code == GSSDP_ERROR_NO_IP_ADDRESS))
82 ("Failed to create context for interface '%s': %s",
91 g_signal_emit_by_name (manager,
95 manager->priv->contexts = g_list_append (manager->priv->contexts,
100 * Create a context for all network interfaces that are up.
103 create_contexts (gpointer data)
105 GUPnPSimpleContextManager *manager = (GUPnPSimpleContextManager *) data;
108 manager->priv->idle_context_creation_src = NULL;
110 if (manager->priv->contexts != NULL)
113 ifaces = gupnp_simple_context_manager_get_interfaces (manager);
115 create_and_signal_context ((char *) ifaces->data, manager);
116 g_free (ifaces->data);
117 ifaces = g_list_delete_link (ifaces, ifaces);
124 destroy_contexts (GUPnPSimpleContextManager *manager)
126 while (manager->priv->contexts) {
127 GUPnPContext *context;
129 context = GUPNP_CONTEXT (manager->priv->contexts->data);
131 g_signal_emit_by_name (manager,
132 "context-unavailable",
134 g_object_unref (context);
136 manager->priv->contexts = g_list_delete_link
137 (manager->priv->contexts,
138 manager->priv->contexts);
143 schedule_contexts_creation (GUPnPSimpleContextManager *manager)
145 manager->priv->idle_context_creation_src = NULL;
147 /* Create contexts in mainloop so that is happens after user has hooked
148 * to the "context-available" signal.
150 manager->priv->idle_context_creation_src = g_idle_source_new ();
151 g_source_attach (manager->priv->idle_context_creation_src,
152 g_main_context_get_thread_default ());
153 g_source_set_callback (manager->priv->idle_context_creation_src,
157 g_source_unref (manager->priv->idle_context_creation_src);
161 gupnp_simple_context_manager_init (GUPnPSimpleContextManager *manager)
164 G_TYPE_INSTANCE_GET_PRIVATE (manager,
165 GUPNP_TYPE_SIMPLE_CONTEXT_MANAGER,
166 GUPnPSimpleContextManagerPrivate);
170 gupnp_simple_context_manager_constructed (GObject *object)
172 GObjectClass *parent_class;
173 GUPnPSimpleContextManager *manager;
175 manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
176 schedule_contexts_creation (manager);
179 parent_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
180 if (parent_class->constructed != NULL)
181 parent_class->constructed (object);
185 gupnp_simple_context_manager_dispose (GObject *object)
187 GUPnPSimpleContextManager *manager;
188 GObjectClass *object_class;
190 manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
192 destroy_contexts (manager);
194 if (manager->priv->idle_context_creation_src) {
195 g_source_destroy (manager->priv->idle_context_creation_src);
196 manager->priv->idle_context_creation_src = NULL;
201 object_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
202 object_class->dispose (object);
206 gupnp_simple_context_manager_class_init (GUPnPSimpleContextManagerClass *klass)
208 GObjectClass *object_class;
210 object_class = G_OBJECT_CLASS (klass);
212 object_class->constructed = gupnp_simple_context_manager_constructed;
213 object_class->dispose = gupnp_simple_context_manager_dispose;
215 g_type_class_add_private (klass,
216 sizeof (GUPnPSimpleContextManagerPrivate));