]> git.0d.be Git - empathy.git/blob - libempathy/empathy-camera-monitor.c
include telepathy-glib.h
[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/telepathy-glib.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   EmpathyCameraDeviceMonitor *empathy_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 (EmpathyCameraDeviceMonitor *device,
112     gchar *id,
113     gchar *filename,
114     gchar *product_name,
115     gint api_version,
116     EmpathyCameraMonitor *self)
117 {
118   EmpathyCamera *camera;
119
120   if (self->priv->cameras == NULL)
121     return;
122
123   camera = empathy_camera_new (id, filename, product_name);
124
125   g_queue_push_tail (self->priv->cameras, camera);
126
127   self->priv->num_cameras++;
128
129   if (self->priv->num_cameras == 1)
130     g_object_notify (G_OBJECT (self), "available");
131
132   g_signal_emit (self, signals[CAMERA_ADDED], 0, camera);
133 }
134
135 static void
136 on_camera_removed (EmpathyCameraDeviceMonitor *device,
137     gchar *id,
138     EmpathyCameraMonitor *self)
139 {
140   EmpathyCamera *camera;
141   GList *l;
142
143   if (self->priv->cameras == NULL)
144     return;
145
146   l = g_queue_find_custom (self->priv->cameras, id, empathy_camera_find);
147
148   g_return_if_fail (l != NULL);
149
150   camera = l->data;
151
152   g_queue_delete_link (self->priv->cameras, l);
153
154   self->priv->num_cameras--;
155
156   if (self->priv->num_cameras == 0)
157     g_object_notify (G_OBJECT (self), "available");
158
159   g_signal_emit (self, signals[CAMERA_REMOVED], 0, camera);
160
161   empathy_camera_free (camera);
162 }
163
164 const GList *
165 empathy_camera_monitor_get_cameras (EmpathyCameraMonitor *self)
166 {
167   if (self->priv->cameras != NULL)
168     return self->priv->cameras->head;
169   else
170     return NULL;
171 }
172
173 static void
174 empathy_camera_monitor_get_property (GObject *object,
175     guint prop_id,
176     GValue *value,
177     GParamSpec *pspec)
178 {
179   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
180
181   switch (prop_id)
182     {
183     case PROP_AVAILABLE:
184       g_value_set_boolean (value, self->priv->num_cameras > 0);
185       break;
186     default:
187       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188       break;
189   }
190 }
191
192 static void
193 empathy_camera_monitor_dispose (GObject *object)
194 {
195   EmpathyCameraMonitor *self = EMPATHY_CAMERA_MONITOR (object);
196
197   tp_clear_object (&self->priv->empathy_monitor);
198
199   g_queue_foreach (self->priv->cameras,
200       empathy_camera_monitor_free_camera_foreach, NULL);
201   tp_clear_pointer (&self->priv->cameras, g_queue_free);
202
203   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->dispose (object);
204 }
205
206 static void
207 empathy_camera_monitor_constructed (GObject *object)
208 {
209   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
210
211   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
212
213   empathy_camera_device_monitor_coldplug (self->priv->empathy_monitor);
214 }
215
216 static void
217 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
218 {
219   GObjectClass *object_class = G_OBJECT_CLASS (klass);
220
221   object_class->dispose = empathy_camera_monitor_dispose;
222   object_class->constructed = empathy_camera_monitor_constructed;
223   object_class->get_property = empathy_camera_monitor_get_property;
224
225   g_object_class_install_property (object_class, PROP_AVAILABLE,
226       g_param_spec_boolean ("available", "Available",
227       "Camera available", TRUE, G_PARAM_READABLE));
228
229   signals[CAMERA_ADDED] =
230       g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
231           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
232           0, NULL, NULL,
233           g_cclosure_marshal_generic,
234           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
235
236   signals[CAMERA_REMOVED] =
237       g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
238           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
239           0, NULL, NULL,
240           g_cclosure_marshal_generic,
241           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
242
243   g_type_class_add_private (object_class,
244       sizeof (EmpathyCameraMonitorPrivate));
245 }
246
247 static void
248 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
249 {
250   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
251       EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
252
253   self->priv->cameras = g_queue_new ();
254
255   self->priv->empathy_monitor = empathy_camera_device_monitor_new ();
256
257   g_signal_connect (self->priv->empathy_monitor, "added",
258       G_CALLBACK (on_camera_added), self);
259   g_signal_connect (self->priv->empathy_monitor, "removed",
260       G_CALLBACK (on_camera_removed), self);
261
262 #ifndef HAVE_UDEV
263   /* No udev, assume there are cameras present */
264   self->priv->num_cameras = 1;
265 #endif
266 }
267
268 EmpathyCameraMonitor *
269 empathy_camera_monitor_dup_singleton (void)
270 {
271   GObject *retval;
272
273   if (manager_singleton)
274     {
275       retval = g_object_ref (manager_singleton);
276     }
277   else
278     {
279       retval = g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
280
281       manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
282       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
283     }
284
285   return EMPATHY_CAMERA_MONITOR (retval);
286 }
287
288 EmpathyCameraMonitor *
289 empathy_camera_monitor_new (void)
290 {
291   return EMPATHY_CAMERA_MONITOR (
292       g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL));
293 }
294
295 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
296 {
297   g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
298
299   return self->priv->num_cameras > 0;
300 }