]> git.0d.be Git - empathy.git/blob - src/empathy-camera-menu.c
camera-menu: Show the menu only if there is more than one camera
[empathy.git] / src / empathy-camera-menu.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  * GtkAction code based on gnome-terminal's TerminalTabsMenu object.
19  * Thanks guys!
20  */
21
22 #include <config.h>
23
24 #include <gtk/gtk.h>
25
26 #include <libempathy/empathy-camera-monitor.h>
27 #include <libempathy/empathy-gsettings.h>
28
29 #include "empathy-camera-menu.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
32 #include <libempathy/empathy-debug.h>
33
34 struct _EmpathyCameraMenuPrivate
35 {
36   /* Borrowed ref; the call window actually owns us. */
37   EmpathyCallWindow *window;
38
39   /* Given away ref; the call window's UI manager now owns this. */
40   GtkActionGroup *action_group;
41
42   /* An invisible radio action so new cameras are always in the
43    * same radio group. */
44   GtkAction *anchor_action;
45
46   /* The merge ID used with the UI manager. We need to keep this
47    * around so in _clean we can remove all the items we've added
48    * before and start again. */
49   guint ui_id;
50
51   /* TRUE if we're in _update and so calling _set_active. */
52   gboolean in_update;
53
54   /* Queue of GtkRadioActions. */
55   GQueue *cameras;
56
57   EmpathyCameraMonitor *camera_monitor;
58
59   GSettings *settings;
60 };
61
62 G_DEFINE_TYPE (EmpathyCameraMenu, empathy_camera_menu, G_TYPE_OBJECT);
63
64 enum
65 {
66   PROP_WINDOW = 1,
67 };
68
69 static void empathy_camera_menu_update (EmpathyCameraMenu *self);
70
71 static void
72 empathy_camera_menu_init (EmpathyCameraMenu *self)
73 {
74   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
75     EMPATHY_TYPE_CAMERA_MENU, EmpathyCameraMenuPrivate);
76 }
77
78 static void
79 empathy_camera_menu_set_property (GObject *object,
80     guint property_id,
81     const GValue *value,
82     GParamSpec *pspec)
83 {
84   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
85
86   switch (property_id)
87     {
88       case PROP_WINDOW:
89         self->priv->window = g_value_get_object (value);
90         break;
91       default:
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93     }
94 }
95
96 static void
97 empathy_camera_menu_get_property (GObject *object,
98     guint property_id,
99     GValue *value,
100     GParamSpec *pspec)
101 {
102   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
103
104   switch (property_id)
105     {
106       case PROP_WINDOW:
107         g_value_set_object (value, self->priv->window);
108         break;
109       default:
110         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111     }
112 }
113
114 static void
115 empathy_camera_menu_clean (EmpathyCameraMenu *self)
116 {
117   GtkUIManager *ui_manager;
118
119   if (self->priv->ui_id == 0)
120     return;
121
122   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
123
124   gtk_ui_manager_remove_ui (ui_manager, self->priv->ui_id);
125   gtk_ui_manager_ensure_update (ui_manager);
126   self->priv->ui_id = 0;
127 }
128
129 static void
130 empathy_camera_menu_activate_cb (GtkAction *action,
131     EmpathyCameraMenu *self)
132 {
133   EmpathyGstVideoSrc *video;
134   const gchar *device;
135   gchar *current_device = NULL;
136
137   if (self->priv->in_update)
138     return;
139
140   video = empathy_call_window_get_video_src (self->priv->window);
141   if (video != NULL)
142     current_device = empathy_video_src_dup_device (video);
143
144   device = gtk_action_get_name (action);
145
146   /* Don't change the device if it's the currently used one */
147   if (!tp_strdiff (device, current_device))
148     goto out;
149
150   empathy_call_window_change_webcam (self->priv->window, device);
151
152  out:
153   g_free (current_device);
154 }
155
156 static void
157 empathy_camera_menu_update (EmpathyCameraMenu *self)
158 {
159   GList *l;
160   GtkAction *menu;
161   GtkUIManager *ui_manager;
162   EmpathyGstVideoSrc *video;
163   gboolean show_menu;
164   gchar *current_camera = NULL;
165   guint n_cameras;
166
167   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
168
169   menu = gtk_ui_manager_get_action (ui_manager, "/menubar1/edit/menucamera");
170   n_cameras = g_queue_get_length (self->priv->cameras);
171   show_menu = (n_cameras > 1);
172   gtk_action_set_visible (menu, show_menu);
173
174   video = empathy_call_window_get_video_src (self->priv->window);
175   if (video != NULL)
176     current_camera = empathy_video_src_dup_device (video);
177
178   empathy_camera_menu_clean (self);
179   self->priv->ui_id = gtk_ui_manager_new_merge_id (ui_manager);
180
181   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
182     {
183       GtkRadioAction *action = l->data;
184       const gchar *name = gtk_action_get_name (GTK_ACTION (action));
185
186       if (!tp_strdiff (current_camera, name))
187         {
188           self->priv->in_update = TRUE;
189           gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
190           self->priv->in_update = FALSE;
191         }
192
193       gtk_ui_manager_add_ui (ui_manager, self->priv->ui_id,
194           /* TODO: this should probably be passed from the call
195            * window, seeing that it's a reference to
196            * empathy-call-window.ui. */
197           "/menubar1/edit/menucamera",
198           name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
199     }
200
201   g_free (current_camera);
202 }
203
204 static void
205 empathy_camera_menu_add_camera (EmpathyCameraMenu *self,
206     EmpathyCamera *camera)
207 {
208   GtkRadioAction *action;
209   GSList *group;
210
211   action = gtk_radio_action_new (camera->device, camera->name, NULL, NULL, 0);
212   gtk_action_group_add_action (self->priv->action_group, GTK_ACTION (action));
213
214   group = gtk_radio_action_get_group (
215       GTK_RADIO_ACTION (self->priv->anchor_action));
216   gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
217
218   g_queue_push_tail (self->priv->cameras, action);
219
220   g_signal_connect (action, "activate",
221       G_CALLBACK (empathy_camera_menu_activate_cb), self);
222 }
223
224 static void
225 empathy_camera_menu_camera_added_cb (EmpathyCameraMonitor *monitor,
226     EmpathyCamera *camera,
227     EmpathyCameraMenu *self)
228 {
229   empathy_camera_menu_add_camera (self, camera);
230   empathy_camera_menu_update (self);
231 }
232
233 static void
234 empathy_camera_menu_camera_removed_cb (EmpathyCameraMonitor *monitor,
235     EmpathyCamera *camera,
236     EmpathyCameraMenu *self)
237 {
238   GList *l;
239
240   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
241     {
242       GtkAction *action = l->data;
243       const gchar *device;
244
245       device = gtk_action_get_name (action);
246
247       if (tp_strdiff (device, camera->device))
248         continue;
249
250       g_signal_handlers_disconnect_by_func (action,
251           G_CALLBACK (empathy_camera_menu_activate_cb), self);
252
253       gtk_action_group_remove_action (self->priv->action_group,
254           action);
255       g_queue_remove (self->priv->cameras, action);
256       break;
257     }
258
259   empathy_camera_menu_update (self);
260 }
261
262 static void
263 empathy_camera_menu_prefs_camera_changed_cb (GSettings *settings,
264     gchar *key,
265     EmpathyCameraMenu *self)
266 {
267   gchar *device = g_settings_get_string (settings, key);
268   GtkRadioAction *action = NULL;
269   gboolean found = FALSE;
270   GList *l;
271
272   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
273     {
274       const gchar *name;
275
276       action = l->data;
277       name = gtk_action_get_name (GTK_ACTION (action));
278
279       if (!tp_strdiff (device, name))
280         {
281           found = TRUE;
282           break;
283         }
284     }
285
286   /* If the selected camera isn't found, we connect the first
287    * available one */
288   if (!found && self->priv->cameras->head != NULL)
289     action = self->priv->cameras->head->data;
290
291   if (action != NULL &&
292       !gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
293     {
294       g_signal_handlers_block_by_func (settings,
295           empathy_camera_menu_prefs_camera_changed_cb, self);
296       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
297       g_signal_handlers_unblock_by_func (settings,
298           empathy_camera_menu_prefs_camera_changed_cb, self);
299     }
300
301   g_free (device);
302 }
303
304 static void
305 empathy_camera_menu_get_cameras (EmpathyCameraMenu *self)
306 {
307   const GList *cameras;
308
309   cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
310
311   for (; cameras != NULL; cameras = g_list_next (cameras))
312     {
313       EmpathyCamera *camera = cameras->data;
314
315       empathy_camera_menu_add_camera (self, camera);
316     }
317
318   empathy_camera_menu_update (self);
319
320   /* Do as if the gsettings key had changed, so we select the key that
321    * was last set. */
322   empathy_camera_menu_prefs_camera_changed_cb (self->priv->settings,
323       EMPATHY_PREFS_CALL_CAMERA_DEVICE, self);
324 }
325
326 static void
327 empathy_camera_menu_constructed (GObject *obj)
328 {
329   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
330   GtkUIManager *ui_manager;
331
332   g_assert (EMPATHY_IS_CALL_WINDOW (self->priv->window));
333
334   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
335
336   g_assert (GTK_IS_UI_MANAGER (ui_manager));
337
338   /* Okay let's go go go. */
339
340   self->priv->action_group = gtk_action_group_new ("EmpathyCameraMenu");
341   gtk_ui_manager_insert_action_group (ui_manager, self->priv->action_group, -1);
342   /* the UI manager now owns this */
343   g_object_unref (self->priv->action_group);
344
345   self->priv->anchor_action = g_object_new (GTK_TYPE_RADIO_ACTION,
346       "name", "EmpathyCameraMenuAnchorAction",
347       NULL);
348   gtk_action_group_add_action (self->priv->action_group,
349       self->priv->anchor_action);
350   g_object_unref (self->priv->anchor_action);
351
352   self->priv->camera_monitor = empathy_camera_monitor_new ();
353
354   tp_g_signal_connect_object (self->priv->camera_monitor, "added",
355       G_CALLBACK (empathy_camera_menu_camera_added_cb), self, 0);
356   tp_g_signal_connect_object (self->priv->camera_monitor, "removed",
357       G_CALLBACK (empathy_camera_menu_camera_removed_cb), self, 0);
358
359   self->priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
360   g_signal_connect (self->priv->settings,
361       "changed::"EMPATHY_PREFS_CALL_CAMERA_DEVICE,
362       G_CALLBACK (empathy_camera_menu_prefs_camera_changed_cb), self);
363
364   self->priv->cameras = g_queue_new ();
365
366   empathy_camera_menu_get_cameras (self);
367 }
368
369 static void
370 empathy_camera_menu_dispose (GObject *obj)
371 {
372   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
373
374   tp_clear_pointer (&self->priv->cameras, g_queue_free);
375
376   tp_clear_object (&self->priv->camera_monitor);
377   tp_clear_object (&self->priv->settings);
378
379   G_OBJECT_CLASS (empathy_camera_menu_parent_class)->dispose (obj);
380 }
381
382 static void
383 empathy_camera_menu_class_init (EmpathyCameraMenuClass *klass)
384 {
385   GObjectClass *object_class = G_OBJECT_CLASS (klass);
386
387   object_class->set_property = empathy_camera_menu_set_property;
388   object_class->get_property = empathy_camera_menu_get_property;
389   object_class->constructed = empathy_camera_menu_constructed;
390   object_class->dispose = empathy_camera_menu_dispose;
391
392   g_object_class_install_property (object_class, PROP_WINDOW,
393       g_param_spec_object ("window", "window", "window",
394           EMPATHY_TYPE_CALL_WINDOW,
395           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
396
397   g_type_class_add_private (object_class, sizeof (EmpathyCameraMenuPrivate));
398 }
399
400 EmpathyCameraMenu *
401 empathy_camera_menu_new (EmpathyCallWindow *window)
402 {
403   return g_object_new (EMPATHY_TYPE_CAMERA_MENU,
404       "window", window,
405       NULL);
406 }
407
408 void
409 empathy_camera_menu_set_sensitive (EmpathyCameraMenu *self,
410     gboolean sensitive)
411 {
412   GtkUIManager *ui_manager;
413
414   gtk_action_group_set_sensitive (self->priv->action_group, sensitive);
415   if (sensitive) /* Mark the active camera as such. */
416     empathy_camera_menu_update (self);
417
418   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
419   gtk_ui_manager_ensure_update (ui_manager);
420 }