2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
3 * Copyright (C) 2006, 2007 OpenedHand Ltd.
5 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6 * Jorn Baayen <jorn@openedhand.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gupnp-resource-factory
26 * @short_description: Class for resource and resource proxy object creation.
28 * #GUPnPResourceFactory objects are used by #GUPnPControlPoint,
29 * #GUPnPDeviceProxy and #GUPnPDevice to create resource proxy and resource
30 * objects. Register UPnP type - #GType pairs to have resource or resource proxy
31 * objects created with the specified #GType whenever an object for a resource
32 * of the specified UPnP type is requested. The #GType<!-- -->s need
33 * to be derived from the relevant resource or resource proxy type (e.g.
34 * a device proxy type needs to be derived from #GUPnPDeviceProxy).
39 #include "gupnp-resource-factory-private.h"
40 #include "gupnp-root-device.h"
42 G_DEFINE_TYPE (GUPnPResourceFactory,
43 gupnp_resource_factory,
46 struct _GUPnPResourceFactoryPrivate {
47 GHashTable *resource_type_hash;
48 GHashTable *proxy_type_hash;
52 gupnp_resource_factory_init (GUPnPResourceFactory *factory)
55 G_TYPE_INSTANCE_GET_PRIVATE (factory,
56 GUPNP_TYPE_RESOURCE_FACTORY,
57 GUPnPResourceFactoryPrivate);
59 factory->priv->resource_type_hash =
60 g_hash_table_new_full (g_str_hash,
64 factory->priv->proxy_type_hash =
65 g_hash_table_new_full (g_str_hash,
72 gupnp_resource_factory_finalize (GObject *object)
74 GUPnPResourceFactory *self;
75 GObjectClass *object_class;
77 self = GUPNP_RESOURCE_FACTORY (object);
79 if (self->priv->resource_type_hash) {
80 g_hash_table_destroy (self->priv->resource_type_hash);
81 self->priv->resource_type_hash = NULL;
84 if (self->priv->proxy_type_hash) {
85 g_hash_table_destroy (self->priv->proxy_type_hash);
86 self->priv->proxy_type_hash = NULL;
89 object_class = G_OBJECT_CLASS (gupnp_resource_factory_parent_class);
90 object_class->finalize (object);
94 gupnp_resource_factory_class_init (GUPnPResourceFactoryClass *klass)
96 GObjectClass *object_class;
98 object_class = G_OBJECT_CLASS (klass);
100 object_class->finalize = gupnp_resource_factory_finalize;
102 g_type_class_add_private (klass, sizeof (GUPnPResourceFactoryPrivate));
106 * gupnp_resource_factory_new:
108 * Create a new #GUPnPResourceFactory object.
110 * Return value: A #GUPnPResourceFactory object.
112 GUPnPResourceFactory *
113 gupnp_resource_factory_new (void)
115 return g_object_new (GUPNP_TYPE_RESOURCE_FACTORY, NULL);
119 * gupnp_resource_factory_get_default:
121 * Get the default singleton #GUPnPResourceFactory object.
123 * Returns: (transfer none): A @GUPnPResourceFactory object.
125 GUPnPResourceFactory *
126 gupnp_resource_factory_get_default (void)
128 static GUPnPResourceFactory *default_factory = NULL;
130 if (G_UNLIKELY (default_factory == NULL)) {
131 default_factory = g_object_new (GUPNP_TYPE_RESOURCE_FACTORY,
135 return default_factory;
139 * gupnp_resource_factory_create_device_proxy:
140 * @factory: A #GUPnPResourceFactory
141 * @context: A #GUPnPContext
142 * @doc: A #GUPnPXMLDoc
143 * @element: The #xmlNode ponting to the right device element
144 * @udn: The UDN of the device to create a proxy for
145 * @location: The location of the device description file
146 * @url_base: The URL base for this device, or %NULL if none
149 * Create a #GUPnPDeviceProxy for the device with element @element, as
150 * read from the device description file specified by @location.
152 * Return value: A new #GUPnPDeviceProxy.
155 gupnp_resource_factory_create_device_proxy
156 (GUPnPResourceFactory *factory,
157 GUPnPContext *context,
161 const char *location,
162 const SoupURI *url_base)
164 GUPnPDeviceProxy *proxy;
166 GType proxy_type = GUPNP_TYPE_DEVICE_PROXY;
168 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
169 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
170 g_return_val_if_fail (GUPNP_IS_XML_DOC (doc), NULL);
171 g_return_val_if_fail (element != NULL, NULL);
172 g_return_val_if_fail (location != NULL, NULL);
173 g_return_val_if_fail (url_base != NULL, NULL);
175 upnp_type = xml_util_get_child_element_content_glib (element,
180 value = g_hash_table_lookup (factory->priv->proxy_type_hash,
183 proxy_type = GPOINTER_TO_SIZE (value);
188 proxy = g_object_new (proxy_type,
189 "resource-factory", factory,
191 "location", location,
193 "url-base", url_base,
202 * gupnp_resource_factory_create_service_proxy:
203 * @factory: A #GUPnPResourceFactory
204 * @context: A #GUPnPContext
205 * @doc: A #GUPnPXMLDoc
206 * @element: The #xmlNode ponting to the right service element
207 * @location: The location of the service description file
208 * @udn: The UDN of the device the service is contained in
209 * @service_type: The service type
210 * @url_base: The URL base for this service, or %NULL if none
212 * Create a #GUPnPServiceProxy for the service with element @element, as
213 * read from the service description file specified by @location.
215 * Return value: A new #GUPnPServiceProxy.
218 gupnp_resource_factory_create_service_proxy
219 (GUPnPResourceFactory *factory,
220 GUPnPContext *context,
224 const char *service_type,
225 const char *location,
226 const SoupURI *url_base)
228 GUPnPServiceProxy *proxy;
229 GType proxy_type = GUPNP_TYPE_SERVICE_PROXY;
231 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
232 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
233 g_return_val_if_fail (GUPNP_IS_XML_DOC (doc), NULL);
234 g_return_val_if_fail (element != NULL, NULL);
235 g_return_val_if_fail (location != NULL, NULL);
236 g_return_val_if_fail (url_base != NULL, NULL);
241 value = g_hash_table_lookup (factory->priv->proxy_type_hash,
244 proxy_type = GPOINTER_TO_SIZE (value);
247 proxy = g_object_new (proxy_type,
249 "location", location,
251 "service-type", service_type,
252 "url-base", url_base,
261 * gupnp_resource_factory_create_device:
262 * @factory: A #GUPnPResourceFactory
263 * @context: A #GUPnPContext
264 * @root_device: The #GUPnPRootDevice
265 * @element: The #xmlNode ponting to the right device element
266 * @udn: The UDN of the device to create a device for
267 * @location: The location of the device description file
268 * @url_base: The URL base for this device
270 * Create a #GUPnPDevice for the device with element @element, as
271 * read from the device description file specified by @location.
273 * Return value: A new #GUPnPDevice.
276 gupnp_resource_factory_create_device
277 (GUPnPResourceFactory *factory,
278 GUPnPContext *context,
279 GUPnPDevice *root_device,
282 const char *location,
283 const SoupURI *url_base)
287 GType device_type = GUPNP_TYPE_DEVICE;
289 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
290 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
291 g_return_val_if_fail (GUPNP_IS_ROOT_DEVICE (root_device), NULL);
292 g_return_val_if_fail (element != NULL, NULL);
293 g_return_val_if_fail (url_base != NULL, NULL);
295 upnp_type = xml_util_get_child_element_content_glib (element,
300 value = g_hash_table_lookup (factory->priv->resource_type_hash,
303 device_type = GPOINTER_TO_SIZE (value);
308 device = g_object_new (device_type,
309 "resource-factory", factory,
311 "root-device", root_device,
312 "location", location,
314 "url-base", url_base,
322 * gupnp_resource_factory_create_service:
323 * @factory: A #GUPnPResourceFactory
324 * @context: A #GUPnPContext
325 * @root_device: The #GUPnPRootDevice
326 * @element: The #xmlNode ponting to the right service element
327 * @udn: The UDN of the device the service is contained in
328 * @location: The location of the service description file
329 * @url_base: The URL base for this service
331 * Create a #GUPnPService for the service with element @element, as
332 * read from the service description file specified by @location.
334 * Return value: A new #GUPnPService.
337 gupnp_resource_factory_create_service
338 (GUPnPResourceFactory *factory,
339 GUPnPContext *context,
340 GUPnPDevice *root_device,
343 const char *location,
344 const SoupURI *url_base)
346 GUPnPService *service;
348 GType service_type = GUPNP_TYPE_SERVICE;
350 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
351 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
352 g_return_val_if_fail (GUPNP_IS_ROOT_DEVICE (root_device), NULL);
353 g_return_val_if_fail (element != NULL, NULL);
354 g_return_val_if_fail (location != NULL, NULL);
355 g_return_val_if_fail (url_base != NULL, NULL);
357 upnp_type = xml_util_get_child_element_content_glib (element,
362 value = g_hash_table_lookup (factory->priv->resource_type_hash,
365 service_type = GPOINTER_TO_SIZE (value);
370 service = g_object_new (service_type,
372 "root-device", root_device,
373 "location", location,
375 "url-base", url_base,
383 * gupnp_resource_factory_register_resource_type:
384 * @factory: A #GUPnPResourceFactory.
385 * @upnp_type: The UPnP type name of the resource.
386 * @type: The requested GType assignment for the resource.
388 * Registers the GType @type for the resource of UPnP type @upnp_type. After
389 * this call, the factory @factory will create object of GType @type each time
390 * it is asked to create a resource object for UPnP type @upnp_type.
392 * Note: GType @type must be a derived type of #GUPNP_TYPE_DEVICE if resource is
393 * a device or #GUPNP_TYPE_SERVICE if its a service.
396 gupnp_resource_factory_register_resource_type (GUPnPResourceFactory *factory,
397 const char *upnp_type,
400 g_hash_table_insert (factory->priv->resource_type_hash,
401 g_strdup (upnp_type),
402 GSIZE_TO_POINTER (type));
406 * gupnp_resource_factory_unregister_resource_type:
407 * @factory: A #GUPnPResourceFactory.
408 * @upnp_type: The UPnP type name of the resource.
410 * Unregisters the GType assignment for the resource of UPnP type @upnp_type.
412 * Return value: %TRUE if GType assignment was removed successfully, %FALSE
416 gupnp_resource_factory_unregister_resource_type
417 (GUPnPResourceFactory *factory,
418 const char *upnp_type)
420 return g_hash_table_remove (factory->priv->resource_type_hash,
425 * gupnp_resource_factory_register_resource_proxy_type:
426 * @factory: A #GUPnPResourceFactory.
427 * @upnp_type: The UPnP type name of the resource.
428 * @type: The requested GType assignment for the resource proxy.
430 * Registers the GType @type for the proxy of resource of UPnP type @upnp_type.
431 * After this call, the factory @factory will create object of GType @type each
432 * time it is asked to create a resource proxy object for UPnP type @upnp_type.
434 * Note: GType @type must be a derived type of #GUPNP_TYPE_DEVICE_PROXY if
435 * resource is a device or #GUPNP_TYPE_SERVICE_PROXY if its a service.
438 gupnp_resource_factory_register_resource_proxy_type
439 (GUPnPResourceFactory *factory,
440 const char *upnp_type,
443 g_hash_table_insert (factory->priv->proxy_type_hash,
444 g_strdup (upnp_type),
445 GSIZE_TO_POINTER (type));
449 * gupnp_resource_factory_unregister_resource_proxy_type:
450 * @factory: A #GUPnPResourceFactory.
451 * @upnp_type: The UPnP type name of the resource.
453 * Unregisters the GType assignment for the proxy of resource of UPnP type
456 * Return value: %TRUE if GType assignment was removed successfully, %FALSE
460 gupnp_resource_factory_unregister_resource_proxy_type
461 (GUPnPResourceFactory *factory,
462 const char *upnp_type)
464 return g_hash_table_remove (factory->priv->proxy_type_hash, upnp_type);