Updated Portuguese translation
[empathy-mirror.git] / src / empathy-camera-menu.c
blobcf18340c4b7ead3d30cc31e2651e133fb9c30213
1 /*
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.
19 * Thanks guys!
22 #include "config.h"
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. */
47 guint ui_id;
49 /* TRUE if we're in _update and so calling _set_active. */
50 gboolean in_update;
52 /* Queue of GtkRadioActions. */
53 GQueue *cameras;
55 TpawCameraMonitor *camera_monitor;
57 GSettings *settings;
60 G_DEFINE_TYPE (EmpathyCameraMenu, empathy_camera_menu, G_TYPE_OBJECT);
62 enum
64 PROP_WINDOW = 1,
67 static void empathy_camera_menu_update (EmpathyCameraMenu *self);
69 static void
70 empathy_camera_menu_init (EmpathyCameraMenu *self)
72 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
73 EMPATHY_TYPE_CAMERA_MENU, EmpathyCameraMenuPrivate);
76 static void
77 empathy_camera_menu_set_property (GObject *object,
78 guint property_id,
79 const GValue *value,
80 GParamSpec *pspec)
82 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
84 switch (property_id)
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);
94 static void
95 empathy_camera_menu_get_property (GObject *object,
96 guint property_id,
97 GValue *value,
98 GParamSpec *pspec)
100 EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
102 switch (property_id)
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);
112 static void
113 empathy_camera_menu_clean (EmpathyCameraMenu *self)
115 GtkUIManager *ui_manager;
117 if (self->priv->ui_id == 0)
118 return;
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;
127 static void
128 empathy_camera_menu_activate_cb (GtkAction *action,
129 EmpathyCameraMenu *self)
131 EmpathyGstVideoSrc *video;
132 const gchar *device;
133 gchar *current_device = NULL;
135 if (self->priv->in_update)
136 return;
138 video = empathy_call_window_get_video_src (self->priv->window);
139 if (video != NULL)
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))
146 goto out;
148 empathy_call_window_change_webcam (self->priv->window, device);
150 out:
151 g_free (current_device);
154 static void
155 empathy_camera_menu_update (EmpathyCameraMenu *self)
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;
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);
173 if (video != NULL)
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);
202 static void
203 empathy_camera_menu_add_camera (EmpathyCameraMenu *self,
204 TpawCamera *camera)
206 GtkRadioAction *action;
207 GSList *group;
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);
222 static void
223 empathy_camera_menu_camera_added_cb (TpawCameraMonitor *monitor,
224 TpawCamera *camera,
225 EmpathyCameraMenu *self)
227 empathy_camera_menu_add_camera (self, camera);
228 empathy_camera_menu_update (self);
231 static void
232 empathy_camera_menu_camera_removed_cb (TpawCameraMonitor *monitor,
233 TpawCamera *camera,
234 EmpathyCameraMenu *self)
236 GList *l;
238 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
240 GtkAction *action = l->data;
241 const gchar *device;
243 device = gtk_action_get_name (action);
245 if (tp_strdiff (device, camera->device))
246 continue;
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,
252 action);
253 g_queue_remove (self->priv->cameras, action);
254 break;
257 empathy_camera_menu_update (self);
260 static void
261 empathy_camera_menu_prefs_camera_changed_cb (GSettings *settings,
262 gchar *key,
263 EmpathyCameraMenu *self)
265 gchar *device = g_settings_get_string (settings, key);
266 GtkRadioAction *action = NULL;
267 gboolean found = FALSE;
268 GList *l;
270 for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
272 const gchar *name;
274 action = l->data;
275 name = gtk_action_get_name (GTK_ACTION (action));
277 if (!tp_strdiff (device, name))
279 found = TRUE;
280 break;
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;
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);
299 g_free (device);
302 static void
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
319 * was last set. */
320 empathy_camera_menu_prefs_camera_changed_cb (self->priv->settings,
321 EMPATHY_PREFS_CALL_CAMERA_DEVICE, self);
324 static void
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",
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);
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);
367 static void
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);
380 static void
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));
398 EmpathyCameraMenu *
399 empathy_camera_menu_new (EmpathyCallWindow *window)
401 return g_object_new (EMPATHY_TYPE_CAMERA_MENU,
402 "window", window,
403 NULL);
406 void
407 empathy_camera_menu_set_sensitive (EmpathyCameraMenu *self,
408 gboolean sensitive)
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);