]> git.0d.be Git - empathy.git/blob - libempathy/empathy-camera-monitor.c
CameraMonitor: dynamically-allocate the queue
[empathy.git] / libempathy / empathy-camera-monitor.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24
25 #include <telepathy-glib/util.h>
26
27 #include "empathy-camera-monitor.h"
28 #include "cheese-camera-device-monitor.h"
29 #include "empathy-utils.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
33
34 struct _EmpathyCameraMonitorPrivate
35 {
36   CheeseCameraDeviceMonitor *cheese_monitor;
37   GQueue *cameras;
38   gint num_cameras;
39 };
40
41 enum
42 {
43   PROP_0,
44   PROP_AVAILABLE,
45 };
46
47 enum
48 {
49   CAMERA_ADDED,
50   CAMERA_REMOVED,
51   LAST_SIGNAL
52 };
53
54 static guint signals[LAST_SIGNAL];
55
56 G_DEFINE_TYPE (EmpathyCameraMonitor, empathy_camera_monitor, G_TYPE_OBJECT);
57
58 static EmpathyCameraMonitor *manager_singleton = NULL;
59
60 static EmpathyCamera *
61 empathy_camera_new (const gchar *id,
62     const gchar *device,
63     const gchar *name)
64 {
65   EmpathyCamera *camera = g_slice_new (EmpathyCamera);
66
67   camera->id = g_strdup (id);
68   camera->device = g_strdup (device);
69   camera->name = g_strdup (name);
70
71   return camera;
72 }
73
74 static EmpathyCamera *
75 empathy_camera_copy (EmpathyCamera *camera)
76 {
77   return empathy_camera_new (camera->id, camera->device, camera->name);
78 }
79
80 static void
81 empathy_camera_free (EmpathyCamera *camera)
82 {
83   g_free (camera->id);
84   g_free (camera->device);
85   g_free (camera->name);
86
87   g_slice_free (EmpathyCamera, camera);
88 }
89
90 G_DEFINE_BOXED_TYPE (EmpathyCamera, empathy_camera,
91     empathy_camera_copy, empathy_camera_free)
92
93 static gint
94 empathy_camera_find (gconstpointer a,
95     gconstpointer b)
96 {
97   const EmpathyCamera *camera = a;
98   const gchar *id = b;
99
100   return g_strcmp0 (camera->id, id);
101 }
102
103 static void
104 empathy_camera_monitor_free_camera_foreach (gpointer data,
105     gpointer user_data)
106 {
107   empathy_camera_free (data);
108 }
109
110 static void
111 on_camera_added (CheeseCameraDeviceMonitor *device,
112     gchar *id,
113     gchar *filename,
114     gchar *product_name,
115     gint api_version,
116     EmpathyCameraMonitor *self)
117 {
118   EmpathyCamera *camera = empathy_camera_new (id, filename, product_name);
119
120   if (self->priv->cameras == NULL)
121     return;
122
123   g_queue_push_tail (self->priv->cameras, camera);
124
125   self->priv->num_cameras++;
126
127   if (self->priv->num_cameras == 1)
128     g_object_notify (G_OBJECT (self), "available");
129
130   g_signal_emit (self, signals[CAMERA_ADDED], 0, camera);
131 }
132
133 static void
134 on_camera_removed (CheeseCameraDeviceMonitor *device,
135     gchar *id,
136     EmpathyCameraMonitor *self)
137 {
138   EmpathyCamera *camera;
139   GList *l;
140
141   if (self->priv->cameras == NULL)
142     return;
143
144   l = g_queue_find_custom (self->priv->cameras, id, empathy_camera_find);
145
146   g_return_if_fail (l != NULL);
147
148   camera = l->data;
149
150   g_queue_delete_link (self->priv->cameras, l);
151
152   self->priv->num_cameras--;
153
154   if (self->priv->num_cameras == 0)
155     g_object_notify (G_OBJECT (self), "available");
156
157   g_signal_emit (self, signals[CAMERA_REMOVED], 0, camera);
158
159   empathy_camera_free (camera);
160 }
161
162 const GList *
163 empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
164 {
165   if (self->priv->cameras != NULL)
166     return self->priv->cameras->head;
167   else
168     return NULL;
169 }
170
171 static void
172 empathy_camera_monitor_get_property (GObject *object,
173     guint prop_id,
174     GValue *value,
175     GParamSpec *pspec)
176 {
177   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
178
179   switch (prop_id)
180     {
181     case PROP_AVAILABLE:
182       g_value_set_boolean (value, self->priv->num_cameras > 0);
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187   }
188 }
189
190 static void
191 empathy_camera_monitor_dispose (GObject *object)
192 {
193   EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
194
195   tp_clear_object (&self->priv->cheese_monitor);
196
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);
200
201   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
202 }
203
204 static GObject *
205 empathy_camera_monitor_constructor (GType type,
206     guint n_props,
207     GObjectConstructParam *props)
208 {
209   GObject *retval;
210
211   if (manager_singleton)
212     {
213       retval = g_object_ref (manager_singleton);
214     }
215   else
216     {
217       retval =
218           G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->
219           constructor (type, n_props, props);
220
221       manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
222       g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
223     }
224
225   return retval;
226 }
227
228 static void
229 empathy_camera_monitor_constructed (GObject *object)
230 {
231   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
232
233   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
234
235   cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
236 }
237
238 static void
239 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
240 {
241   GObjectClass *object_class = G_OBJECT_CLASS (klass);
242
243   object_class->dispose = empathy_camera_monitor_dispose;
244   object_class->constructor = empathy_camera_monitor_constructor;
245   object_class->constructed = empathy_camera_monitor_constructed;
246   object_class->get_property = empathy_camera_monitor_get_property;
247
248   g_object_class_install_property (object_class, PROP_AVAILABLE,
249       g_param_spec_boolean ("available", "Available",
250       "Camera available", TRUE, G_PARAM_READABLE));
251
252   signals[CAMERA_ADDED] =
253       g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
254           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
255           0, NULL, NULL,
256           g_cclosure_marshal_VOID__BOXED,
257           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
258
259   signals[CAMERA_REMOVED] =
260       g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
261           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
262           0, NULL, NULL,
263           g_cclosure_marshal_VOID__BOXED,
264           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
265
266   g_type_class_add_private (object_class,
267       sizeof (EmpathyCameraMonitorPrivate));
268 }
269
270 static void
271 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
272 {
273   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
274       EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
275
276   self->priv->cameras = g_queue_new ();
277
278   self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
279
280   g_signal_connect (self->priv->cheese_monitor, "added",
281       G_CALLBACK (on_camera_added), self);
282   g_signal_connect (self->priv->cheese_monitor, "removed",
283       G_CALLBACK (on_camera_removed), self);
284
285 #ifndef HAVE_UDEV
286   /* No udev, assume there are cameras present */
287   self->priv->num_cameras = 1;
288 #endif
289 }
290
291 EmpathyCameraMonitor *
292 empathy_camera_monitor_dup_singleton (void)
293 {
294   return g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
295 }
296
297 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
298 {
299   g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
300
301   return self->priv->num_cameras > 0;
302 }