]> git.0d.be Git - empathy.git/blob - libempathy/empathy-camera-monitor.c
Merge branch 'log-window-webview'
[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 void
205 empathy_camera_monitor_constructed (GObject *object)
206 {
207   EmpathyCameraMonitor *self = (EmpathyCameraMonitor *) object;
208
209   G_OBJECT_CLASS (empathy_camera_monitor_parent_class)->constructed (object);
210
211   cheese_camera_device_monitor_coldplug (self->priv->cheese_monitor);
212 }
213
214 static void
215 empathy_camera_monitor_class_init (EmpathyCameraMonitorClass *klass)
216 {
217   GObjectClass *object_class = G_OBJECT_CLASS (klass);
218
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;
222
223   g_object_class_install_property (object_class, PROP_AVAILABLE,
224       g_param_spec_boolean ("available", "Available",
225       "Camera available", TRUE, G_PARAM_READABLE));
226
227   signals[CAMERA_ADDED] =
228       g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
229           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
230           0, NULL, NULL,
231           g_cclosure_marshal_VOID__BOXED,
232           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
233
234   signals[CAMERA_REMOVED] =
235       g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
236           G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
237           0, NULL, NULL,
238           g_cclosure_marshal_VOID__BOXED,
239           G_TYPE_NONE, 1, EMPATHY_TYPE_CAMERA);
240
241   g_type_class_add_private (object_class,
242       sizeof (EmpathyCameraMonitorPrivate));
243 }
244
245 static void
246 empathy_camera_monitor_init (EmpathyCameraMonitor *self)
247 {
248   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
249       EMPATHY_TYPE_CAMERA_MONITOR, EmpathyCameraMonitorPrivate);
250
251   self->priv->cameras = g_queue_new ();
252
253   self->priv->cheese_monitor = cheese_camera_device_monitor_new ();
254
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);
259
260 #ifndef HAVE_UDEV
261   /* No udev, assume there are cameras present */
262   self->priv->num_cameras = 1;
263 #endif
264 }
265
266 EmpathyCameraMonitor *
267 empathy_camera_monitor_dup_singleton (void)
268 {
269   GObject *retval;
270
271   if (manager_singleton)
272     {
273       retval = g_object_ref (manager_singleton);
274     }
275   else
276     {
277       retval = g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL);
278
279       manager_singleton = EMPATHY_CAMERA_MONITOR (retval);
280       g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
281     }
282
283   return EMPATHY_CAMERA_MONITOR (retval);
284 }
285
286 EmpathyCameraMonitor *
287 empathy_camera_monitor_new (void)
288 {
289   return EMPATHY_CAMERA_MONITOR (
290       g_object_new (EMPATHY_TYPE_CAMERA_MONITOR, NULL));
291 }
292
293 gboolean empathy_camera_monitor_get_available (EmpathyCameraMonitor *self)
294 {
295   g_return_val_if_fail (EMPATHY_IS_CAMERA_MONITOR (self), FALSE);
296
297   return self->priv->num_cameras > 0;
298 }