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-mic-menu.h"
25 #include "empathy-mic-monitor.h"
27 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
28 #include "empathy-debug.h"
30 struct _EmpathyMicMenuPrivate
32 /* Borrowed ref; the call window actually owns us. */
33 EmpathyCallWindow
*window
;
35 /* Given away ref; the call window's UI manager now owns this. */
36 GtkActionGroup
*action_group
;
38 /* An invisible radio action so new microphones are always in the
39 * same radio group. */
40 GtkAction
*anchor_action
;
42 /* The merge ID used with the UI manager. We need to keep this
43 * around so in _clean we can remove all the items we've added
44 * before and start again. */
47 /* TRUE if we're in _update and so calling _set_active. */
50 /* Queue of GtkRadioActions. */
53 EmpathyMicMonitor
*mic_monitor
;
56 G_DEFINE_TYPE (EmpathyMicMenu
, empathy_mic_menu
, G_TYPE_OBJECT
);
58 #define MONITOR_KEY "empathy-mic-menu-is-monitor"
65 static void empathy_mic_menu_update (EmpathyMicMenu
*self
);
68 empathy_mic_menu_init (EmpathyMicMenu
*self
)
70 EmpathyMicMenuPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
71 EMPATHY_TYPE_MIC_MENU
, EmpathyMicMenuPrivate
);
77 empathy_mic_menu_set_property (GObject
*object
,
82 EmpathyMicMenu
*self
= EMPATHY_MIC_MENU (object
);
83 EmpathyMicMenuPrivate
*priv
= self
->priv
;
88 priv
->window
= g_value_get_object (value
);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
96 empathy_mic_menu_get_property (GObject
*object
,
101 EmpathyMicMenu
*self
= EMPATHY_MIC_MENU (object
);
102 EmpathyMicMenuPrivate
*priv
= self
->priv
;
107 g_value_set_object (value
, priv
->window
);
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
115 empathy_mic_menu_clean (EmpathyMicMenu
*self
)
117 EmpathyMicMenuPrivate
*priv
= self
->priv
;
118 GtkUIManager
*ui_manager
;
120 if (priv
->ui_id
== 0)
123 ui_manager
= empathy_call_window_get_ui_manager (priv
->window
);
125 gtk_ui_manager_remove_ui (ui_manager
, priv
->ui_id
);
126 gtk_ui_manager_ensure_update (ui_manager
);
131 empathy_mic_menu_change_mic_cb (GObject
*source_object
,
132 GAsyncResult
*result
,
135 EmpathyGstAudioSrc
*audio
= EMPATHY_GST_AUDIO_SRC (source_object
);
136 EmpathyMicMenu
*self
= user_data
;
137 GError
*error
= NULL
;
139 if (!empathy_audio_src_change_microphone_finish (audio
, result
, &error
))
141 DEBUG ("Failed to change microphone: %s", error
->message
);
142 g_clear_error (&error
);
144 /* We call update here because if this change operation failed
145 * and we don't update the menu items, it'll point to the wrong
146 * device. We don't want to call it if the change was successful
147 * because we'll get the notify::microphone signal fired in a
148 * bit and the current value hasn't changed so it'd keep jumping
149 * between these values like there's no tomorrow, etc. */
150 empathy_mic_menu_update (self
);
155 empathy_mic_menu_activate_cb (GtkToggleAction
*action
,
156 EmpathyMicMenu
*self
)
158 EmpathyMicMenuPrivate
*priv
= self
->priv
;
159 EmpathyGstAudioSrc
*audio
;
165 audio
= empathy_call_window_get_audio_src (priv
->window
);
167 g_object_get (action
, "value", &value
, NULL
);
169 empathy_audio_src_change_microphone_async (audio
, value
,
170 empathy_mic_menu_change_mic_cb
, self
);
174 empathy_mic_menu_update (EmpathyMicMenu
*self
)
176 EmpathyMicMenuPrivate
*priv
= self
->priv
;
178 GtkUIManager
*ui_manager
;
179 EmpathyGstAudioSrc
*audio
;
182 ui_manager
= empathy_call_window_get_ui_manager (priv
->window
);
184 audio
= empathy_call_window_get_audio_src (priv
->window
);
185 current_mic
= empathy_audio_src_get_microphone (audio
);
187 empathy_mic_menu_clean (self
);
188 priv
->ui_id
= gtk_ui_manager_new_merge_id (ui_manager
);
190 for (l
= priv
->microphones
->head
; l
!= NULL
; l
= l
->next
)
192 GtkRadioAction
*action
= l
->data
;
193 const gchar
*name
= gtk_action_get_name (GTK_ACTION (action
));
197 g_object_get (action
, "value", &value
, NULL
);
199 active
= (value
== (gint
) current_mic
);
203 priv
->in_update
= TRUE
;
204 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action
), TRUE
);
205 priv
->in_update
= FALSE
;
208 /* If action is a monitor then don't show it in the UI, BUT do
209 * display it regardless if it is the current device. This is so
210 * we don't have a rubbish UI by showing monitor devices in
211 * Empathy, but still show the correct device when someone plays
212 * with pavucontrol. */
213 if (g_object_get_data (G_OBJECT (action
), MONITOR_KEY
) != NULL
217 gtk_ui_manager_add_ui (ui_manager
, priv
->ui_id
,
218 /* TODO: this should probably be passed from the call
219 * window, seeing that it's a reference to
220 * empathy-call-window.ui. */
221 "/menubar1/edit/menumicrophone",
222 name
, name
, GTK_UI_MANAGER_MENUITEM
, FALSE
);
227 empathy_mic_menu_add_microphone (EmpathyMicMenu
*self
,
229 const gchar
*description
,
233 EmpathyMicMenuPrivate
*priv
= self
->priv
;
234 GtkRadioAction
*action
;
237 action
= gtk_radio_action_new (name
, description
, NULL
, NULL
, source_idx
);
238 gtk_action_group_add_action_with_accel (priv
->action_group
,
239 GTK_ACTION (action
), NULL
);
241 /* Set MONITOR_KEY on the action to non-NULL if it's a monitor
242 * because we don't want to show monitors if we can help it. */
245 g_object_set_data (G_OBJECT (action
), MONITOR_KEY
,
246 GUINT_TO_POINTER (TRUE
));
249 group
= gtk_radio_action_get_group (GTK_RADIO_ACTION (priv
->anchor_action
));
250 gtk_radio_action_set_group (GTK_RADIO_ACTION (action
), group
);
252 g_queue_push_tail (priv
->microphones
, action
);
254 g_signal_connect (action
, "activate",
255 G_CALLBACK (empathy_mic_menu_activate_cb
), self
);
259 empathy_mic_menu_notify_microphone_cb (EmpathyGstAudioSrc
*audio
,
261 EmpathyMicMenu
*self
)
263 empathy_mic_menu_update (self
);
267 empathy_mic_menu_microphone_added_cb (EmpathyMicMonitor
*monitor
,
270 const gchar
*description
,
272 EmpathyMicMenu
*self
)
274 empathy_mic_menu_add_microphone (self
, name
, description
,
275 source_idx
, is_monitor
);
277 empathy_mic_menu_update (self
);
281 empathy_mic_menu_microphone_removed_cb (EmpathyMicMonitor
*monitor
,
283 EmpathyMicMenu
*self
)
285 EmpathyMicMenuPrivate
*priv
= self
->priv
;
288 for (l
= priv
->microphones
->head
; l
!= NULL
; l
= l
->next
)
290 GtkRadioAction
*action
= l
->data
;
293 g_object_get (action
, "value", &value
, NULL
);
295 if (value
!= (gint
) source_idx
)
301 g_signal_handlers_disconnect_by_func (action
,
302 G_CALLBACK (empathy_mic_menu_activate_cb
), self
);
304 gtk_action_group_remove_action (priv
->action_group
, GTK_ACTION (action
));
305 g_queue_remove (priv
->microphones
, action
);
309 empathy_mic_menu_update (self
);
313 empathy_mic_menu_list_microphones_cb (GObject
*source_object
,
314 GAsyncResult
*result
,
317 EmpathyMicMonitor
*monitor
= EMPATHY_MIC_MONITOR (source_object
);
318 EmpathyMicMenu
*self
= user_data
;
319 GError
*error
= NULL
;
320 const GList
*mics
= NULL
;
322 mics
= empathy_mic_monitor_list_microphones_finish (monitor
, result
, &error
);
326 DEBUG ("Failed to get microphone list: %s", error
->message
);
327 g_clear_error (&error
);
331 for (; mics
!= NULL
; mics
= mics
->next
)
333 EmpathyMicrophone
*mic
= mics
->data
;
335 empathy_mic_menu_add_microphone (self
, mic
->name
,
336 mic
->description
, mic
->index
, mic
->is_monitor
);
339 empathy_mic_menu_update (self
);
343 empathy_mic_menu_constructed (GObject
*obj
)
345 EmpathyMicMenu
*self
= EMPATHY_MIC_MENU (obj
);
346 EmpathyMicMenuPrivate
*priv
= self
->priv
;
347 GtkUIManager
*ui_manager
;
348 EmpathyGstAudioSrc
*audio
;
350 g_assert (EMPATHY_IS_CALL_WINDOW (priv
->window
));
352 ui_manager
= empathy_call_window_get_ui_manager (priv
->window
);
353 audio
= empathy_call_window_get_audio_src (priv
->window
);
355 g_assert (GTK_IS_UI_MANAGER (ui_manager
));
356 g_assert (EMPATHY_IS_GST_AUDIO_SRC (audio
));
358 /* Okay let's go go go. */
360 priv
->mic_monitor
= empathy_mic_monitor_new ();
362 priv
->action_group
= gtk_action_group_new ("EmpathyMicMenu");
363 gtk_ui_manager_insert_action_group (ui_manager
, priv
->action_group
, -1);
364 /* the UI manager now owns this */
365 g_object_unref (priv
->action_group
);
367 priv
->anchor_action
= g_object_new (GTK_TYPE_RADIO_ACTION
,
368 "name", "EmpathyMicMenuAnchorAction",
370 gtk_action_group_add_action (priv
->action_group
, priv
->anchor_action
);
371 g_object_unref (priv
->anchor_action
);
373 priv
->microphones
= g_queue_new ();
375 /* Don't bother with any of this if we don't support changing
376 * microphone, so don't listen for microphone changes or enumerate
377 * the available microphones. */
378 if (!empathy_audio_src_supports_changing_mic (audio
))
381 tp_g_signal_connect_object (audio
, "notify::microphone",
382 G_CALLBACK (empathy_mic_menu_notify_microphone_cb
), self
, 0);
383 tp_g_signal_connect_object (priv
->mic_monitor
, "microphone-added",
384 G_CALLBACK (empathy_mic_menu_microphone_added_cb
), self
, 0);
385 tp_g_signal_connect_object (priv
->mic_monitor
, "microphone-removed",
386 G_CALLBACK (empathy_mic_menu_microphone_removed_cb
), self
, 0);
388 empathy_mic_monitor_list_microphones_async (priv
->mic_monitor
,
389 empathy_mic_menu_list_microphones_cb
, self
);
393 empathy_mic_menu_dispose (GObject
*obj
)
395 EmpathyMicMenu
*self
= EMPATHY_MIC_MENU (obj
);
396 EmpathyMicMenuPrivate
*priv
= self
->priv
;
398 if (priv
->microphones
!= NULL
)
399 g_queue_free (priv
->microphones
);
400 priv
->microphones
= NULL
;
402 tp_clear_object (&priv
->mic_monitor
);
404 G_OBJECT_CLASS (empathy_mic_menu_parent_class
)->dispose (obj
);
408 empathy_mic_menu_class_init (EmpathyMicMenuClass
*klass
)
410 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
412 object_class
->set_property
= empathy_mic_menu_set_property
;
413 object_class
->get_property
= empathy_mic_menu_get_property
;
414 object_class
->constructed
= empathy_mic_menu_constructed
;
415 object_class
->dispose
= empathy_mic_menu_dispose
;
417 g_object_class_install_property (object_class
, PROP_WINDOW
,
418 g_param_spec_object ("window", "window", "window",
419 EMPATHY_TYPE_CALL_WINDOW
,
420 G_PARAM_WRITABLE
| G_PARAM_STATIC_STRINGS
| G_PARAM_CONSTRUCT_ONLY
));
422 g_type_class_add_private (object_class
, sizeof (EmpathyMicMenuPrivate
));
426 empathy_mic_menu_new (EmpathyCallWindow
*window
)
428 return g_object_new (EMPATHY_TYPE_MIC_MENU
,