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 <tp-account-widgets/tpaw-camera-monitor.h>
27 #include "empathy-gsettings.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
30 #include "empathy-debug.h"
32 struct _EmpathyCameraMenuPrivate
34 /* Borrowed ref; the call window actually owns us. */
35 EmpathyCallWindow
*window
;
37 /* Given away ref; the call window's UI manager now owns this. */
38 GtkActionGroup
*action_group
;
40 /* An invisible radio action so new cameras are always in the
41 * same radio group. */
42 GtkAction
*anchor_action
;
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. */
49 /* TRUE if we're in _update and so calling _set_active. */
52 /* Queue of GtkRadioActions. */
55 TpawCameraMonitor
*camera_monitor
;
60 G_DEFINE_TYPE (EmpathyCameraMenu
, empathy_camera_menu
, G_TYPE_OBJECT
);
67 static void empathy_camera_menu_update (EmpathyCameraMenu
*self
);
70 empathy_camera_menu_init (EmpathyCameraMenu
*self
)
72 self
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
73 EMPATHY_TYPE_CAMERA_MENU
, EmpathyCameraMenuPrivate
);
77 empathy_camera_menu_set_property (GObject
*object
,
82 EmpathyCameraMenu
*self
= EMPATHY_CAMERA_MENU (object
);
87 self
->priv
->window
= g_value_get_object (value
);
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
95 empathy_camera_menu_get_property (GObject
*object
,
100 EmpathyCameraMenu
*self
= EMPATHY_CAMERA_MENU (object
);
105 g_value_set_object (value
, self
->priv
->window
);
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
113 empathy_camera_menu_clean (EmpathyCameraMenu
*self
)
115 GtkUIManager
*ui_manager
;
117 if (self
->priv
->ui_id
== 0)
120 ui_manager
= empathy_call_window_get_ui_manager (self
->priv
->window
);
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;
128 empathy_camera_menu_activate_cb (GtkAction
*action
,
129 EmpathyCameraMenu
*self
)
131 EmpathyGstVideoSrc
*video
;
133 gchar
*current_device
= NULL
;
135 if (self
->priv
->in_update
)
138 video
= empathy_call_window_get_video_src (self
->priv
->window
);
140 current_device
= empathy_video_src_dup_device (video
);
142 device
= gtk_action_get_name (action
);
144 /* Don't change the device if it's the currently used one */
145 if (!tp_strdiff (device
, current_device
))
148 empathy_call_window_change_webcam (self
->priv
->window
, device
);
151 g_free (current_device
);
155 empathy_camera_menu_update (EmpathyCameraMenu
*self
)
159 GtkUIManager
*ui_manager
;
160 EmpathyGstVideoSrc
*video
;
162 gchar
*current_camera
= NULL
;
165 ui_manager
= empathy_call_window_get_ui_manager (self
->priv
->window
);
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
);
172 video
= empathy_call_window_get_video_src (self
->priv
->window
);
174 current_camera
= empathy_video_src_dup_device (video
);
176 empathy_camera_menu_clean (self
);
177 self
->priv
->ui_id
= gtk_ui_manager_new_merge_id (ui_manager
);
179 for (l
= self
->priv
->cameras
->head
; l
!= NULL
; l
= g_list_next (l
))
181 GtkRadioAction
*action
= l
->data
;
182 const gchar
*name
= gtk_action_get_name (GTK_ACTION (action
));
184 if (!tp_strdiff (current_camera
, name
))
186 self
->priv
->in_update
= TRUE
;
187 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action
), TRUE
);
188 self
->priv
->in_update
= FALSE
;
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
);
199 g_free (current_camera
);
203 empathy_camera_menu_add_camera (EmpathyCameraMenu
*self
,
206 GtkRadioAction
*action
;
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
));
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
);
216 g_queue_push_tail (self
->priv
->cameras
, action
);
218 g_signal_connect (action
, "activate",
219 G_CALLBACK (empathy_camera_menu_activate_cb
), self
);
223 empathy_camera_menu_camera_added_cb (TpawCameraMonitor
*monitor
,
225 EmpathyCameraMenu
*self
)
227 empathy_camera_menu_add_camera (self
, camera
);
228 empathy_camera_menu_update (self
);
232 empathy_camera_menu_camera_removed_cb (TpawCameraMonitor
*monitor
,
234 EmpathyCameraMenu
*self
)
238 for (l
= self
->priv
->cameras
->head
; l
!= NULL
; l
= g_list_next (l
))
240 GtkAction
*action
= l
->data
;
243 device
= gtk_action_get_name (action
);
245 if (tp_strdiff (device
, camera
->device
))
248 g_signal_handlers_disconnect_by_func (action
,
249 G_CALLBACK (empathy_camera_menu_activate_cb
), self
);
251 gtk_action_group_remove_action (self
->priv
->action_group
,
253 g_queue_remove (self
->priv
->cameras
, action
);
257 empathy_camera_menu_update (self
);
261 empathy_camera_menu_prefs_camera_changed_cb (GSettings
*settings
,
263 EmpathyCameraMenu
*self
)
265 gchar
*device
= g_settings_get_string (settings
, key
);
266 GtkRadioAction
*action
= NULL
;
267 gboolean found
= FALSE
;
270 for (l
= self
->priv
->cameras
->head
; l
!= NULL
; l
= g_list_next (l
))
275 name
= gtk_action_get_name (GTK_ACTION (action
));
277 if (!tp_strdiff (device
, name
))
284 /* If the selected camera isn't found, we connect the first
286 if (!found
&& self
->priv
->cameras
->head
!= NULL
)
287 action
= self
->priv
->cameras
->head
->data
;
289 if (action
!= NULL
&&
290 !gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action
)))
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
);
303 empathy_camera_menu_get_cameras (EmpathyCameraMenu
*self
)
305 const GList
*cameras
;
307 cameras
= tpaw_camera_monitor_get_cameras (self
->priv
->camera_monitor
);
309 for (; cameras
!= NULL
; cameras
= g_list_next (cameras
))
311 TpawCamera
*camera
= cameras
->data
;
313 empathy_camera_menu_add_camera (self
, camera
);
316 empathy_camera_menu_update (self
);
318 /* Do as if the gsettings key had changed, so we select the key that
320 empathy_camera_menu_prefs_camera_changed_cb (self
->priv
->settings
,
321 EMPATHY_PREFS_CALL_CAMERA_DEVICE
, self
);
325 empathy_camera_menu_constructed (GObject
*obj
)
327 EmpathyCameraMenu
*self
= EMPATHY_CAMERA_MENU (obj
);
328 GtkUIManager
*ui_manager
;
330 g_assert (EMPATHY_IS_CALL_WINDOW (self
->priv
->window
));
332 ui_manager
= empathy_call_window_get_ui_manager (self
->priv
->window
);
334 g_assert (GTK_IS_UI_MANAGER (ui_manager
));
336 /* Okay let's go go go. */
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
);
343 self
->priv
->anchor_action
= g_object_new (GTK_TYPE_RADIO_ACTION
,
344 "name", "EmpathyCameraMenuAnchorAction",
346 gtk_action_group_add_action (self
->priv
->action_group
,
347 self
->priv
->anchor_action
);
348 g_object_unref (self
->priv
->anchor_action
);
350 self
->priv
->camera_monitor
= tpaw_camera_monitor_new ();
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);
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
);
362 self
->priv
->cameras
= g_queue_new ();
364 empathy_camera_menu_get_cameras (self
);
368 empathy_camera_menu_dispose (GObject
*obj
)
370 EmpathyCameraMenu
*self
= EMPATHY_CAMERA_MENU (obj
);
372 tp_clear_pointer (&self
->priv
->cameras
, g_queue_free
);
374 tp_clear_object (&self
->priv
->camera_monitor
);
375 tp_clear_object (&self
->priv
->settings
);
377 G_OBJECT_CLASS (empathy_camera_menu_parent_class
)->dispose (obj
);
381 empathy_camera_menu_class_init (EmpathyCameraMenuClass
*klass
)
383 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
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
;
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
));
395 g_type_class_add_private (object_class
, sizeof (EmpathyCameraMenuPrivate
));
399 empathy_camera_menu_new (EmpathyCallWindow
*window
)
401 return g_object_new (EMPATHY_TYPE_CAMERA_MENU
,
407 empathy_camera_menu_set_sensitive (EmpathyCameraMenu
*self
,
410 GtkUIManager
*ui_manager
;
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
);
416 ui_manager
= empathy_call_window_get_ui_manager (self
->priv
->window
);
417 gtk_ui_manager_ensure_update (ui_manager
);