]> git.0d.be Git - empathy.git/blob - libempathy/empathy-camera-monitor.c
CameraMonitor: add API to get all cameras
[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 G_DEFINE_TYPE (EmpathyCameraMonitor, empathy_camera_monitor, G_TYPE_OBJECT);
48
49 static EmpathyCameraMonitor *manager_singleton = NULL;
50
51 static EmpathyCamera *
52 empathy_camera_new (const gchar *id,
53     const gchar *device,
54     const gchar *name)
55 {
56   EmpathyCamera *camera = g_slice_new (EmpathyCamera);
57
58   camera->id = g_strdup (id);
59   camera->device = g_strdup (device);
60   camera->name = g_strdup (name);
61
62   return camera;
63 }
64
65 static void
66 empathy_camera_free (EmpathyCamera *camera)
67 {
68   g_free (camera->id);
69   g_free (camera->device);
70   g_free (camera->name);
71
72   g_slice_free (EmpathyCamera, camera);
73 }
74
75 static gint
76 empathy_camera_find (gconstpointer a,
77     gconstpointer b)
78 {
79   const EmpathyCamera *camera = a;
80   const gchar *id = b;
81
82   return g_strcmp0 (camera->id, id);
83 }
84
85 static void
86 empathy_camera_monitor_free_camera_foreach (gpointer data,
87     gpointer user_data)
88 {
89   empathy_camera_free (data);
90 }
91
92 static void
93 on_camera_added (CheeseCameraDeviceMonitor *device,
94     gchar *id,
95     gchar *filename,
96     gchar *product_name,
97     gint api_version,
98     EmpathyCameraMonitor *self)
99 {
100   EmpathyCamera *camera = empathy_camera_new (id, filename, product_name);
101
102   g_queue_push_tail (&self->priv->cameras, camera);
103
104   self->priv->num_cameras++;
105
106   if (self->priv->num_cameras == 1)
107     g_object_notify (G_OBJECT (self), "available");
108 }
109
110 static void
111 on_camera_removed (CheeseCameraDeviceMonitor *device,
112     gchar *id,
113     EmpathyCameraMonitor *self)
114 {
115   EmpathyCamera *camera;
116   GList *l;
117
118   l = g_queue_find_custom (&self->priv->cameras, id, empathy_camera_find);
119
120   g_return_if_fail (l != NULL);
121
122   camera = l->data;
123
124   g_queue_delete_link (&self->priv->cameras, l);
125
126   self->priv->num_cameras--;
127
128   if (self->priv->num_cameras == 0)
129     g_object_notify (G_OBJECT (self), "available");
130
131   empathy_camera_free (camera);
132 }
133
134 const GList *
135 empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
136 {
137   return self->priv->cameras.head;
138 }
139
140 static void
141 empathy_camera_monitor_get_property (GObject *object,
142     guint prop_id,
143     GValue *value,
144     GParamSpec *pspec)
145 {
146   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
147
148   switch (prop_id)
149     {
150     case PROP_AVAILABLE:
151       g_value_set_boolean (value, self->priv->num_cameras > 0);
152       break;
153     default:
154       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
155       break;
156   }
157 }
158
159 static void
160 empathy_camera_monitor_dispose (GObject *object)
161 {
162   EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
163
164   tp_clear_object (&self->priv->cheese_monitor);
165
166   g_queue_foreach (&self->priv->cameras,
167       empathy_camera_monitor_free_camera_foreach, NULL);
168   g_queue_clear (&self->priv->cameras);
169
170   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
171 }
172
173 static GObject *
174 empathy_camera_monitor_constructor (GType type,
175     guint n_props,
176     GObjectConstructParam *props)
177 {
178   GObject *retval;
179
180   if (manager_singleton)
181     {
182       retval = g_object_ref (manager_singleton);
183     }
184   else
185     {
186       retval =
187           G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->
188           constructor (type, n_props, props);
189
190       manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
191       g_object_add_weak_pointer (retval, (gpointer) & manager_singleton);
192     }
193
194   return retval;
195 }
196
197 static void
198 empathy_camera_monitor_constructed (GObject *object)
199 {
200   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
201
202   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
203
204   cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
205 }
206
207 static void
208 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
209 {
210   GObjectClass *object_class = G_OBJECT_CLASS (klass);
211
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;
216
217   g_object_class_install_property (object_class, PROP_AVAILABLE,
218       g_param_spec_boolean ("available", "Available",
219       "Camera available", TRUE, G_PARAM_READABLE));
220
221   g_type_class_add_private (object_class,
222       sizeof (EmpathyCameraMonitorPrivate));
223 }
224
225 static void
226 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
227 {
228   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
229       EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
230
231   g_queue_init (&self->priv->cameras);
232
233   self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
234
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);
239
240 #ifndef HAVE_UDEV
241   /* No udev, assume there are cameras present */
242   self->priv->num_cameras = 1;
243 #endif
244 }
245
246 EmpathyCameraMonitor *
247 empathy_camera_monitor_dup_singleton (void)
248 {
249   return g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
250 }
251
252 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
253 {
254   g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
255
256   return self->priv->num_cameras > 0;
257 }