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;
54 static guint signals[LAST_SIGNAL];
56 G_DEFINE_TYPE (EmpathyCameraMonitor, empathy_camera_monitor, G_TYPE_OBJECT);
58 static EmpathyCameraMonitor *manager_singleton = NULL;
60 static EmpathyCamera *
61 empathy_camera_new (const gchar *id,
65 EmpathyCamera *camera = g_slice_new (EmpathyCamera);
67 camera->id = g_strdup (id);
68 camera->device = g_strdup (device);
69 camera->name = g_strdup (name);
74 static EmpathyCamera *
75 empathy_camera_copy (EmpathyCamera *camera)
77 return empathy_camera_new (camera->id, camera->device, camera->name);
81 empathy_camera_free (EmpathyCamera *camera)
84 g_free (camera->device);
85 g_free (camera->name);
87 g_slice_free (EmpathyCamera, camera);
90 G_DEFINE_BOXED_TYPE (EmpathyCamera, empathy_camera,
91 empathy_camera_copy, empathy_camera_free)
94 empathy_camera_find (gconstpointer a,
97 const EmpathyCamera *camera = a;
100 return g_strcmp0 (camera->id, id);
104 empathy_camera_monitor_free_camera_foreach (gpointer data,
107 empathy_camera_free (data);
111 on_camera_added (CheeseCameraDeviceMonitor *device,
116 EmpathyCameraMonitor *self)
118 EmpathyCamera *camera = empathy_camera_new (id, filename, product_name);
120 if (self->priv->cameras == NULL)
123 g_queue_push_tail (self->priv->cameras, camera);
125 self->priv->num_cameras++;
127 if (self->priv->num_cameras == 1)
128 g_object_notify (G_OBJECT (self), "available");
130 g_signal_emit (self, signals[CAMERA_ADDED], 0, camera);
134 on_camera_removed (CheeseCameraDeviceMonitor *device,
136 EmpathyCameraMonitor *self)
138 EmpathyCamera *camera;
141 if (self->priv->cameras == NULL)
144 l = g_queue_find_custom (self->priv->cameras, id, empathy_camera_find);
146 g_return_if_fail (l != NULL);
150 g_queue_delete_link (self->priv->cameras, l);
152 self->priv->num_cameras--;
154 if (self->priv->num_cameras == 0)
155 g_object_notify (G_OBJECT (self), "available");
157 g_signal_emit (self, signals[CAMERA_REMOVED], 0, camera);
159 empathy_camera_free (camera);
163 empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
165 if (self->priv->cameras != NULL)
166 return self->priv->cameras->head;
172 empathy_camera_monitor_get_property (GObject *object,
177 EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
182 g_value_set_boolean (value, self->priv->num_cameras > 0);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 empathy_camera_monitor_dispose (GObject *object)
193 EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
195 tp_clear_object (&self->priv->cheese_monitor);
197 g_queue_foreach (self->priv->cameras,
198 empathy_camera_monitor_free_camera_foreach, NULL);
199 tp_clear_pointer (&self->priv->cameras, g_queue_free);
201 G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
205 empathy_camera_monitor_constructed (GObject *object)
207 EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
209 G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
211 cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
215 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
217 GObjectClass *object_class = G_OBJECT_CLASS (klass);
219 object_class->dispose = empathy_camera_monitor_dispose;
220 object_class->constructed = empathy_camera_monitor_constructed;
221 object_class->get_property = empathy_camera_monitor_get_property;
223 g_object_class_install_property (object_class, PROP_AVAILABLE,
224 g_param_spec_boolean ("available", "Available",
225 "Camera available", TRUE, G_PARAM_READABLE));
227 signals[CAMERA_ADDED] =
228 g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
229 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
231 g_cclosure_marshal_generic,
232 G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
234 signals[CAMERA_REMOVED] =
235 g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
236 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
238 g_cclosure_marshal_generic,
239 G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
241 g_type_class_add_private (object_class,
242 sizeof (EmpathyCameraMonitorPrivate));
246 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
248 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
249 EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
251 self->priv->cameras = g_queue_new ();
253 self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
255 g_signal_connect (self->priv->cheese_monitor, "added",
256 G_CALLBACK (on_camera_added), self);
257 g_signal_connect (self->priv->cheese_monitor, "removed",
258 G_CALLBACK (on_camera_removed), self);
261 /* No udev, assume there are cameras present */
262 self->priv->num_cameras = 1;
266 EmpathyCameraMonitor *
267 empathy_camera_monitor_dup_singleton (void)
271 if (manager_singleton)
273 retval = g_object_ref (manager_singleton);
277 retval = g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
279 manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
280 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
283 return EMPATHY_CAMERA_MONITOR (retval);
286 EmpathyCameraMonitor *
287 empathy_camera_monitor_new (void)
289 return EMPATHY_CAMERA_MONITOR (
290 g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL));
293 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
295 g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
297 return self->priv->num_cameras > 0;