2 * Copyright (C) 2011 Collabora Ltd.
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.
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.
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
18 * GtkAction code based on gnome-terminal's TerminalTabsMenu object.
23 #include "empathy-camera-menu.h"
25 #include "empathy-camera-monitor.h"
26 #include "empathy-gsettings.h"
28 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
29 #include "empathy-debug.h"
31 struct _EmpathyCameraMenuPrivate
33 /* Borrowed ref; the call window actually owns us. */
34 EmpathyCallWindow *window;
36 /* Given away ref; the call window's UI manager now owns this. */
37 GtkActionGroup *action_group;
39 /* An invisible radio action so new cameras are always in the
40 * same radio group. */
41 GtkAction *anchor_action;
43 /* The merge ID used with the UI manager. We need to keep this
44 * around so in _clean we can remove all the items we've added
45 * before and start again. */
48 /* TRUE if we're in _update and so calling _set_active. */
51 /* Queue of GtkRadioActions. */
54 EmpathyCameraMonitor *camera_monitor;
59 G_DEFINE_TYPE (EmpathyCameraMenu, empathy_camera_menu, G_TYPE_OBJECT);
66 static void empathy_camera_menu_update (EmpathyCameraMenu *self);
69 empathy_camera_menu_init (EmpathyCameraMenu *self)
71 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
72 EMPATHY_TYPE_CAMERA_MENU, EmpathyCameraMenuPrivate);
76 empathy_camera_menu_set_property (GObject *object,
81 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
86 self->priv->window = g_value_get_object (value);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 empathy_camera_menu_get_property (GObject *object,
99 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
104 g_value_set_object (value, self->priv->window);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 empathy_camera_menu_clean (EmpathyCameraMenu *self)
114 GtkUIManager *ui_manager;
116 if (self->priv->ui_id == 0)
119 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
121 gtk_ui_manager_remove_ui (ui_manager, self->priv->ui_id);
122 gtk_ui_manager_ensure_update (ui_manager);
123 self->priv->ui_id = 0;
127 empathy_camera_menu_activate_cb (GtkAction *action,
128 EmpathyCameraMenu *self)
130 EmpathyGstVideoSrc *video;
132 gchar *current_device = NULL;
134 if (self->priv->in_update)
137 video = empathy_call_window_get_video_src (self->priv->window);
139 current_device = empathy_video_src_dup_device (video);
141 device = gtk_action_get_name (action);
143 /* Don't change the device if it's the currently used one */
144 if (!tp_strdiff (device, current_device))
147 empathy_call_window_change_webcam (self->priv->window, device);
150 g_free (current_device);
154 empathy_camera_menu_update (EmpathyCameraMenu *self)
158 GtkUIManager *ui_manager;
159 EmpathyGstVideoSrc *video;
161 gchar *current_camera = NULL;
164 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
166 menu = gtk_ui_manager_get_action (ui_manager, "/menubar1/edit/menucamera");
167 n_cameras = g_queue_get_length (self->priv->cameras);
168 show_menu = (n_cameras > 1);
169 gtk_action_set_visible (menu, show_menu);
171 video = empathy_call_window_get_video_src (self->priv->window);
173 current_camera = empathy_video_src_dup_device (video);
175 empathy_camera_menu_clean (self);
176 self->priv->ui_id = gtk_ui_manager_new_merge_id (ui_manager);
178 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
180 GtkRadioAction *action = l->data;
181 const gchar *name = gtk_action_get_name (GTK_ACTION (action));
183 if (!tp_strdiff (current_camera, name))
185 self->priv->in_update = TRUE;
186 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
187 self->priv->in_update = FALSE;
190 gtk_ui_manager_add_ui (ui_manager, self->priv->ui_id,
191 /* TODO: this should probably be passed from the call
192 * window, seeing that it's a reference to
193 * empathy-call-window.ui. */
194 "/menubar1/edit/menucamera",
195 name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
198 g_free (current_camera);
202 empathy_camera_menu_add_camera (EmpathyCameraMenu *self,
203 EmpathyCamera *camera)
205 GtkRadioAction *action;
208 action = gtk_radio_action_new (camera->device, camera->name, NULL, NULL, 0);
209 gtk_action_group_add_action (self->priv->action_group, GTK_ACTION (action));
211 group = gtk_radio_action_get_group (
212 GTK_RADIO_ACTION (self->priv->anchor_action));
213 gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
215 g_queue_push_tail (self->priv->cameras, action);
217 g_signal_connect (action, "activate",
218 G_CALLBACK (empathy_camera_menu_activate_cb), self);
222 empathy_camera_menu_camera_added_cb (EmpathyCameraMonitor *monitor,
223 EmpathyCamera *camera,
224 EmpathyCameraMenu *self)
226 empathy_camera_menu_add_camera (self, camera);
227 empathy_camera_menu_update (self);
231 empathy_camera_menu_camera_removed_cb (EmpathyCameraMonitor *monitor,
232 EmpathyCamera *camera,
233 EmpathyCameraMenu *self)
237 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
239 GtkAction *action = l->data;
242 device = gtk_action_get_name (action);
244 if (tp_strdiff (device, camera->device))
247 g_signal_handlers_disconnect_by_func (action,
248 G_CALLBACK (empathy_camera_menu_activate_cb), self);
250 gtk_action_group_remove_action (self->priv->action_group,
252 g_queue_remove (self->priv->cameras, action);
256 empathy_camera_menu_update (self);
260 empathy_camera_menu_prefs_camera_changed_cb (GSettings *settings,
262 EmpathyCameraMenu *self)
264 gchar *device = g_settings_get_string (settings, key);
265 GtkRadioAction *action = NULL;
266 gboolean found = FALSE;
269 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
274 name = gtk_action_get_name (GTK_ACTION (action));
276 if (!tp_strdiff (device, name))
283 /* If the selected camera isn't found, we connect the first
285 if (!found && self->priv->cameras->head != NULL)
286 action = self->priv->cameras->head->data;
288 if (action != NULL &&
289 !gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
291 g_signal_handlers_block_by_func (settings,
292 empathy_camera_menu_prefs_camera_changed_cb, self);
293 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
294 g_signal_handlers_unblock_by_func (settings,
295 empathy_camera_menu_prefs_camera_changed_cb, self);
302 empathy_camera_menu_get_cameras (EmpathyCameraMenu *self)
304 const GList *cameras;
306 cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
308 for (; cameras != NULL; cameras = g_list_next (cameras))
310 EmpathyCamera *camera = cameras->data;
312 empathy_camera_menu_add_camera (self, camera);
315 empathy_camera_menu_update (self);
317 /* Do as if the gsettings key had changed, so we select the key that
319 empathy_camera_menu_prefs_camera_changed_cb (self->priv->settings,
320 EMPATHY_PREFS_CALL_CAMERA_DEVICE, self);
324 empathy_camera_menu_constructed (GObject *obj)
326 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
327 GtkUIManager *ui_manager;
329 g_assert (EMPATHY_IS_CALL_WINDOW (self->priv->window));
331 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
333 g_assert (GTK_IS_UI_MANAGER (ui_manager));
335 /* Okay let's go go go. */
337 self->priv->action_group = gtk_action_group_new ("EmpathyCameraMenu");
338 gtk_ui_manager_insert_action_group (ui_manager, self->priv->action_group, -1);
339 /* the UI manager now owns this */
340 g_object_unref (self->priv->action_group);
342 self->priv->anchor_action = g_object_new (GTK_TYPE_RADIO_ACTION,
343 "name", "EmpathyCameraMenuAnchorAction",
345 gtk_action_group_add_action (self->priv->action_group,
346 self->priv->anchor_action);
347 g_object_unref (self->priv->anchor_action);
349 self->priv->camera_monitor = empathy_camera_monitor_new ();
351 tp_g_signal_connect_object (self->priv->camera_monitor, "added",
352 G_CALLBACK (empathy_camera_menu_camera_added_cb), self, 0);
353 tp_g_signal_connect_object (self->priv->camera_monitor, "removed",
354 G_CALLBACK (empathy_camera_menu_camera_removed_cb), self, 0);
356 self->priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
357 g_signal_connect (self->priv->settings,
358 "changed::"EMPATHY_PREFS_CALL_CAMERA_DEVICE,
359 G_CALLBACK (empathy_camera_menu_prefs_camera_changed_cb), self);
361 self->priv->cameras = g_queue_new ();
363 empathy_camera_menu_get_cameras (self);
367 empathy_camera_menu_dispose (GObject *obj)
369 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
371 tp_clear_pointer (&self->priv->cameras, g_queue_free);
373 tp_clear_object (&self->priv->camera_monitor);
374 tp_clear_object (&self->priv->settings);
376 G_OBJECT_CLASS (empathy_camera_menu_parent_class)->dispose (obj);
380 empathy_camera_menu_class_init (EmpathyCameraMenuClass *klass)
382 GObjectClass *object_class = G_OBJECT_CLASS (klass);
384 object_class->set_property = empathy_camera_menu_set_property;
385 object_class->get_property = empathy_camera_menu_get_property;
386 object_class->constructed = empathy_camera_menu_constructed;
387 object_class->dispose = empathy_camera_menu_dispose;
389 g_object_class_install_property (object_class, PROP_WINDOW,
390 g_param_spec_object ("window", "window", "window",
391 EMPATHY_TYPE_CALL_WINDOW,
392 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
394 g_type_class_add_private (object_class, sizeof (EmpathyCameraMenuPrivate));
398 empathy_camera_menu_new (EmpathyCallWindow *window)
400 return g_object_new (EMPATHY_TYPE_CAMERA_MENU,
406 empathy_camera_menu_set_sensitive (EmpathyCameraMenu *self,
409 GtkUIManager *ui_manager;
411 gtk_action_group_set_sensitive (self->priv->action_group, sensitive);
412 if (sensitive) /* Mark the active camera as such. */
413 empathy_camera_menu_update (self);
415 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
416 gtk_ui_manager_ensure_update (ui_manager);