2 * Copyright (C) 2011 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
25 #include <telepathy-glib/util.h>
27 #include "empathy-camera-monitor.h"
28 #include "cheese-camera-device-monitor.h"
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
34 struct _EmpathyCameraMonitorPrivate
36 CheeseCameraDeviceMonitor *cheese_monitor;
47 G_DEFINE_TYPE (EmpathyCameraMonitor, empathy_camera_monitor, G_TYPE_OBJECT);
49 static EmpathyCameraMonitor *manager_singleton = NULL;
51 static EmpathyCamera *
52 empathy_camera_new (const gchar *id,
56 EmpathyCamera *camera = g_slice_new (EmpathyCamera);
58 camera->id = g_strdup (id);
59 camera->device = g_strdup (device);
60 camera->name = g_strdup (name);
66 empathy_camera_free (EmpathyCamera *camera)
69 g_free (camera->device);
70 g_free (camera->name);
72 g_slice_free (EmpathyCamera, camera);
76 empathy_camera_find (gconstpointer a,
79 const EmpathyCamera *camera = a;
82 return g_strcmp0 (camera->id, id);
86 empathy_camera_monitor_free_camera_foreach (gpointer data,
89 empathy_camera_free (data);
93 on_camera_added (CheeseCameraDeviceMonitor *device,
98 EmpathyCameraMonitor *self)
100 EmpathyCamera *camera = empathy_camera_new (id, filename, product_name);
102 g_queue_push_tail (&self->priv->cameras, camera);
104 self->priv->num_cameras++;
106 if (self->priv->num_cameras == 1)
107 g_object_notify (G_OBJECT (self), "available");
111 on_camera_removed (CheeseCameraDeviceMonitor *device,
113 EmpathyCameraMonitor *self)
115 EmpathyCamera *camera;
118 l = g_queue_find_custom (&self->priv->cameras, id, empathy_camera_find);
120 g_return_if_fail (l != NULL);
124 g_queue_delete_link (&self->priv->cameras, l);
126 self->priv->num_cameras--;
128 if (self->priv->num_cameras == 0)
129 g_object_notify (G_OBJECT (self), "available");
131 empathy_camera_free (camera);
135 empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
137 return self->priv->cameras.head;
141 empathy_camera_monitor_get_property (GObject *object,
146 EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
151 g_value_set_boolean (value, self->priv->num_cameras > 0);
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 empathy_camera_monitor_dispose (GObject *object)
162 EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
164 tp_clear_object (&self->priv->cheese_monitor);
166 g_queue_foreach (&self->priv->cameras,
167 empathy_camera_monitor_free_camera_foreach, NULL);
168 g_queue_clear (&self->priv->cameras);
170 G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
174 empathy_camera_monitor_constructor (GType type,
176 GObjectConstructParam *props)
180 if (manager_singleton)
182 retval = g_object_ref (manager_singleton);
187 G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->
188 constructor (type, n_props, props);
190 manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
191 g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
198 empathy_camera_monitor_constructed (GObject *object)
200 EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
202 G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
204 cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
208 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
210 GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 object_class->dispose = empathy_camera_monitor_dispose;
213 object_class->constructor = empathy_camera_monitor_constructor;
214 object_class->constructed = empathy_camera_monitor_constructed;
215 object_class->get_property = empathy_camera_monitor_get_property;
217 g_object_class_install_property (object_class, PROP_AVAILABLE,
218 g_param_spec_boolean ("available", "Available",
219 "Camera available", TRUE, G_PARAM_READABLE));
221 g_type_class_add_private (object_class,
222 sizeof (EmpathyCameraMonitorPrivate));
226 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
228 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
229 EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
231 g_queue_init (&self->priv->cameras);
233 self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
235 g_signal_connect (self->priv->cheese_monitor, "added",
236 G_CALLBACK (on_camera_added), self);
237 g_signal_connect (self->priv->cheese_monitor, "removed",
238 G_CALLBACK (on_camera_removed), self);
241 /* No udev, assume there are cameras present */
242 self->priv->num_cameras = 1;
246 EmpathyCameraMonitor *
247 empathy_camera_monitor_dup_singleton (void)
249 return g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
252 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
254 g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
256 return self->priv->num_cameras > 0;