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