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.
26 #include <libempathy/empathy-camera-monitor.h>
27 #include <libempathy/empathy-gsettings.h>
29 #include "empathy-camera-menu.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
32 #include <libempathy/empathy-debug.h>
34 struct _EmpathyCameraMenuPrivate
36 /* Borrowed ref; the call window actually owns us. */
37 EmpathyCallWindow *window;
39 /* Given away ref; the call window's UI manager now owns this. */
40 GtkActionGroup *action_group;
42 /* An invisible radio action so new cameras are always in the
43 * same radio group. */
44 GtkAction *anchor_action;
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. */
51 /* TRUE if we're in _update and so calling _set_active. */
54 /* Queue of GtkRadioActions. */
57 EmpathyCameraMonitor *camera_monitor;
62 G_DEFINE_TYPE (EmpathyCameraMenu, empathy_camera_menu, G_TYPE_OBJECT);
69 static void empathy_camera_menu_update (EmpathyCameraMenu *self);
72 empathy_camera_menu_init (EmpathyCameraMenu *self)
74 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
75 EMPATHY_TYPE_CAMERA_MENU, EmpathyCameraMenuPrivate);
79 empathy_camera_menu_set_property (GObject *object,
84 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
89 self->priv->window = g_value_get_object (value);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 empathy_camera_menu_get_property (GObject *object,
102 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
107 g_value_set_object (value, self->priv->window);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 empathy_camera_menu_clean (EmpathyCameraMenu *self)
117 GtkUIManager *ui_manager;
119 if (self->priv->ui_id == 0)
122 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
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;
130 empathy_camera_menu_activate_cb (GtkAction *action,
131 EmpathyCameraMenu *self)
133 EmpathyGstVideoSrc *video;
135 gchar *current_device;
137 if (self->priv->in_update)
140 video = empathy_call_window_get_video_src (self->priv->window);
142 device = gtk_action_get_name (action);
143 current_device = empathy_video_src_dup_device (video);
145 /* Don't change the device if it's the currently used one */
146 if (!tp_strdiff (device, current_device))
149 empathy_call_window_play_camera (self->priv->window, FALSE);
150 empathy_video_src_change_device (video, device);
151 empathy_call_window_play_camera (self->priv->window, TRUE);
154 g_free (current_device);
158 empathy_camera_menu_update (EmpathyCameraMenu *self)
161 GtkUIManager *ui_manager;
162 EmpathyGstVideoSrc *video;
163 gchar *current_camera;
165 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
167 video = empathy_call_window_get_video_src (self->priv->window);
168 current_camera = empathy_video_src_dup_device (video);
170 empathy_camera_menu_clean (self);
171 self->priv->ui_id = gtk_ui_manager_new_merge_id (ui_manager);
173 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
175 GtkRadioAction *action = l->data;
176 const gchar *name = gtk_action_get_name (GTK_ACTION (action));
178 if (!tp_strdiff (current_camera, name))
180 self->priv->in_update = TRUE;
181 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
182 self->priv->in_update = FALSE;
185 gtk_ui_manager_add_ui (ui_manager, self->priv->ui_id,
186 /* TODO: this should probably be passed from the call
187 * window, seeing that it's a reference to
188 * empathy-call-window.ui. */
189 "/menubar1/edit/menucamera",
190 name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
193 g_free (current_camera);
197 empathy_camera_menu_add_camera (EmpathyCameraMenu *self,
198 EmpathyCamera *camera)
200 GtkRadioAction *action;
203 action = gtk_radio_action_new (camera->device, camera->name, NULL, NULL, 0);
204 gtk_action_group_add_action (self->priv->action_group, GTK_ACTION (action));
206 group = gtk_radio_action_get_group (
207 GTK_RADIO_ACTION (self->priv->anchor_action));
208 gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
210 g_queue_push_tail (self->priv->cameras, action);
212 g_signal_connect (action, "activate",
213 G_CALLBACK (empathy_camera_menu_activate_cb), self);
217 empathy_camera_menu_camera_added_cb (EmpathyCameraMonitor *monitor,
218 EmpathyCamera *camera,
219 EmpathyCameraMenu *self)
221 empathy_camera_menu_add_camera (self, camera);
222 empathy_camera_menu_update (self);
226 empathy_camera_menu_camera_removed_cb (EmpathyCameraMonitor *monitor,
227 EmpathyCamera *camera,
228 EmpathyCameraMenu *self)
232 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
234 GtkAction *action = l->data;
237 device = gtk_action_get_name (action);
239 if (tp_strdiff (device, camera->device))
242 g_signal_handlers_disconnect_by_func (action,
243 G_CALLBACK (empathy_camera_menu_activate_cb), self);
245 gtk_action_group_remove_action (self->priv->action_group,
247 g_queue_remove (self->priv->cameras, action);
251 empathy_camera_menu_update (self);
255 empathy_camera_menu_prefs_camera_changed_cb (GSettings *settings,
257 EmpathyCameraMenu *self)
259 gchar *device = g_settings_get_string (settings, key);
260 GtkRadioAction *action = NULL;
261 gboolean found = FALSE;
264 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
269 name = gtk_action_get_name (GTK_ACTION (action));
271 if (!tp_strdiff (device, name))
278 /* If the selected camera isn't found, we connect the first
280 if (!found && self->priv->cameras->head != NULL)
281 action = self->priv->cameras->head->data;
283 if (action != NULL &&
284 !gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
286 g_signal_handlers_block_by_func (settings,
287 empathy_camera_menu_prefs_camera_changed_cb, self);
288 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
289 g_signal_handlers_unblock_by_func (settings,
290 empathy_camera_menu_prefs_camera_changed_cb, self);
297 empathy_camera_menu_get_cameras (EmpathyCameraMenu *self)
299 const GList *cameras;
301 cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
303 for (; cameras != NULL; cameras = g_list_next (cameras))
305 EmpathyCamera *camera = cameras->data;
307 empathy_camera_menu_add_camera (self, camera);
310 empathy_camera_menu_update (self);
312 /* Do as if the gsettings key had changed, so we select the key that
314 empathy_camera_menu_prefs_camera_changed_cb (self->priv->settings,
315 EMPATHY_PREFS_CALL_CAMERA_DEVICE, self);
319 empathy_camera_menu_constructed (GObject *obj)
321 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
322 GtkUIManager *ui_manager;
324 g_assert (EMPATHY_IS_CALL_WINDOW (self->priv->window));
326 ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
328 g_assert (GTK_IS_UI_MANAGER (ui_manager));
330 /* Okay let's go go go. */
332 self->priv->action_group = gtk_action_group_new ("EmpathyCameraMenu");
333 gtk_ui_manager_insert_action_group (ui_manager, self->priv->action_group, -1);
334 /* the UI manager now owns this */
335 g_object_unref (self->priv->action_group);
337 self->priv->anchor_action = g_object_new (GTK_TYPE_RADIO_ACTION,
338 "name", "EmpathyCameraMenuAnchorAction",
340 gtk_action_group_add_action (self->priv->action_group,
341 self->priv->anchor_action);
342 g_object_unref (self->priv->anchor_action);
344 self->priv->camera_monitor = empathy_camera_monitor_new ();
346 tp_g_signal_connect_object (self->priv->camera_monitor, "added",
347 G_CALLBACK (empathy_camera_menu_camera_added_cb), self, 0);
348 tp_g_signal_connect_object (self->priv->camera_monitor, "removed",
349 G_CALLBACK (empathy_camera_menu_camera_removed_cb), self, 0);
351 self->priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
352 g_signal_connect (self->priv->settings,
353 "changed::"EMPATHY_PREFS_CALL_CAMERA_DEVICE,
354 G_CALLBACK (empathy_camera_menu_prefs_camera_changed_cb), self);
356 self->priv->cameras = g_queue_new ();
358 empathy_camera_menu_get_cameras (self);
362 empathy_camera_menu_dispose (GObject *obj)
364 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
366 tp_clear_pointer (&self->priv->cameras, g_queue_free);
368 tp_clear_object (&self->priv->camera_monitor);
369 tp_clear_object (&self->priv->settings);
371 G_OBJECT_CLASS (empathy_camera_menu_parent_class)->dispose (obj);
375 empathy_camera_menu_class_init (EmpathyCameraMenuClass *klass)
377 GObjectClass *object_class = G_OBJECT_CLASS (klass);
379 object_class->set_property = empathy_camera_menu_set_property;
380 object_class->get_property = empathy_camera_menu_get_property;
381 object_class->constructed = empathy_camera_menu_constructed;
382 object_class->dispose = empathy_camera_menu_dispose;
384 g_object_class_install_property (object_class, PROP_WINDOW,
385 g_param_spec_object ("window", "window", "window",
386 EMPATHY_TYPE_CALL_WINDOW,
387 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
389 g_type_class_add_private (object_class, sizeof (EmpathyCameraMenuPrivate));
393 empathy_camera_menu_new (EmpathyCallWindow *window)
395 return g_object_new (EMPATHY_TYPE_CAMERA_MENU,