]> git.0d.be Git - empathy.git/blob - src/empathy-camera-menu.c
CameraMenu: use empathy_camera_monitor_new()
[empathy.git] / src / empathy-camera-menu.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * GtkAction code based on gnome-terminal's TerminalTabsMenu object.
19  * Thanks guys!
20  */
21
22 #include <config.h>
23
24 #include <gtk/gtk.h>
25
26 #include <libempathy/empathy-camera-monitor.h>
27 #include <libempathy/empathy-gsettings.h>
28
29 #include "empathy-camera-menu.h"
30
31 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
32 #include <libempathy/empathy-debug.h>
33
34 struct _EmpathyCameraMenuPrivate
35 {
36   /* Borrowed ref; the call window actually owns us. */
37   EmpathyCallWindow *window;
38
39   /* Given away ref; the call window's UI manager now owns this. */
40   GtkActionGroup *action_group;
41
42   /* An invisible radio action so new cameras are always in the
43    * same radio group. */
44   GtkAction *anchor_action;
45
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. */
49   guint ui_id;
50
51   /* TRUE if we're in _update and so calling _set_active. */
52   gboolean in_update;
53
54   /* Queue of GtkRadioActions. */
55   GQueue *cameras;
56
57   EmpathyCameraMonitor *camera_monitor;
58
59   GSettings *settings;
60 };
61
62 G_DEFINE_TYPE (EmpathyCameraMenu, empathy_camera_menu, G_TYPE_OBJECT);
63
64 enum
65 {
66   PROP_WINDOW = 1,
67 };
68
69 static void empathy_camera_menu_update (EmpathyCameraMenu *self);
70
71 static void
72 empathy_camera_menu_init (EmpathyCameraMenu *self)
73 {
74   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
75     EMPATHY_TYPE_CAMERA_MENU, EmpathyCameraMenuPrivate);
76 }
77
78 static void
79 empathy_camera_menu_set_property (GObject *object,
80     guint property_id,
81     const GValue *value,
82     GParamSpec *pspec)
83 {
84   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
85
86   switch (property_id)
87     {
88       case PROP_WINDOW:
89         self->priv->window = g_value_get_object (value);
90         break;
91       default:
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93     }
94 }
95
96 static void
97 empathy_camera_menu_get_property (GObject *object,
98     guint property_id,
99     GValue *value,
100     GParamSpec *pspec)
101 {
102   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (object);
103
104   switch (property_id)
105     {
106       case PROP_WINDOW:
107         g_value_set_object (value, self->priv->window);
108         break;
109       default:
110         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111     }
112 }
113
114 static void
115 empathy_camera_menu_clean (EmpathyCameraMenu *self)
116 {
117   GtkUIManager *ui_manager;
118
119   if (self->priv->ui_id == 0)
120     return;
121
122   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
123
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;
127 }
128
129 static void
130 empathy_camera_menu_activate_cb (GtkAction *action,
131     EmpathyCameraMenu *self)
132 {
133   EmpathyGstVideoSrc *video;
134   const gchar *device;
135
136   if (self->priv->in_update)
137     return;
138
139   video = empathy_call_window_get_video_src (self->priv->window);
140
141   device = gtk_action_get_name (action);
142
143   empathy_video_src_change_device (video, device);
144 }
145
146 static void
147 empathy_camera_menu_update (EmpathyCameraMenu *self)
148 {
149   GList *l;
150   GtkUIManager *ui_manager;
151   EmpathyGstVideoSrc *video;
152   gchar *current_camera;
153
154   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
155
156   video = empathy_call_window_get_video_src (self->priv->window);
157   current_camera = empathy_video_src_dup_device (video);
158
159   empathy_camera_menu_clean (self);
160   self->priv->ui_id = gtk_ui_manager_new_merge_id (ui_manager);
161
162   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
163     {
164       GtkRadioAction *action = l->data;
165       const gchar *name = gtk_action_get_name (GTK_ACTION (action));
166
167       if (!tp_strdiff (current_camera, name))
168         {
169           self->priv->in_update = TRUE;
170           gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
171           self->priv->in_update = FALSE;
172         }
173
174       gtk_ui_manager_add_ui (ui_manager, self->priv->ui_id,
175           /* TODO: this should probably be passed from the call
176            * window, seeing that it's a reference to
177            * empathy-call-window.ui. */
178           "/menubar1/edit/menucamera",
179           name, name, GTK_UI_MANAGER_MENUITEM, FALSE);
180     }
181
182   g_free (current_camera);
183 }
184
185 static void
186 empathy_camera_menu_add_camera (EmpathyCameraMenu *self,
187     EmpathyCamera *camera)
188 {
189   GtkRadioAction *action;
190   GSList *group;
191
192   action = gtk_radio_action_new (camera->device, camera->name, NULL, NULL, 0);
193   gtk_action_group_add_action (self->priv->action_group, GTK_ACTION (action));
194
195   group = gtk_radio_action_get_group (
196       GTK_RADIO_ACTION (self->priv->anchor_action));
197   gtk_radio_action_set_group (GTK_RADIO_ACTION (action), group);
198
199   g_queue_push_tail (self->priv->cameras, action);
200
201   g_signal_connect (action, "activate",
202       G_CALLBACK (empathy_camera_menu_activate_cb), self);
203 }
204
205 static void
206 empathy_camera_menu_camera_added_cb (EmpathyCameraMonitor *monitor,
207     EmpathyCamera *camera,
208     EmpathyCameraMenu *self)
209 {
210   empathy_camera_menu_add_camera (self, camera);
211   empathy_camera_menu_update (self);
212 }
213
214 static void
215 empathy_camera_menu_camera_removed_cb (EmpathyCameraMonitor *monitor,
216     EmpathyCamera *camera,
217     EmpathyCameraMenu *self)
218 {
219   GList *l;
220
221   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
222     {
223       GtkAction *action = l->data;
224       const gchar *device;
225
226       device = gtk_action_get_name (action);
227
228       if (tp_strdiff (device, camera->device))
229         continue;
230
231       g_signal_handlers_disconnect_by_func (action,
232           G_CALLBACK (empathy_camera_menu_activate_cb), self);
233
234       gtk_action_group_remove_action (self->priv->action_group,
235           action);
236       g_queue_remove (self->priv->cameras, action);
237       break;
238     }
239
240   empathy_camera_menu_update (self);
241 }
242
243 static void
244 empathy_camera_menu_prefs_camera_changed_cb (GSettings *settings,
245     gchar *key,
246     EmpathyCameraMenu *self)
247 {
248   gchar *device = g_settings_get_string (settings, key);
249   GtkRadioAction *action;
250   gboolean found = FALSE;
251   GList *l;
252
253   for (l = self->priv->cameras->head; l != NULL; l = g_list_next (l))
254     {
255       const gchar *name;
256
257       action = l->data;
258       name = gtk_action_get_name (GTK_ACTION (action));
259
260       if (!tp_strdiff (device, name))
261         {
262           found = TRUE;
263           break;
264         }
265     }
266
267   /* If the selected camera isn't found, we connect the first
268    * available one */
269   if (!found)
270     action = self->priv->cameras->head->data;
271
272   if (!gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
273     {
274       g_signal_handlers_block_by_func (settings,
275           empathy_camera_menu_prefs_camera_changed_cb, self);
276       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
277       g_signal_handlers_unblock_by_func (settings,
278           empathy_camera_menu_prefs_camera_changed_cb, self);
279     }
280
281   g_free (device);
282 }
283
284 static void
285 empathy_camera_menu_get_cameras (EmpathyCameraMenu *self)
286 {
287   const GList *cameras;
288
289   cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
290
291   for (; cameras != NULL; cameras = g_list_next (cameras))
292     {
293       EmpathyCamera *camera = cameras->data;
294
295       empathy_camera_menu_add_camera (self, camera);
296     }
297
298   empathy_camera_menu_update (self);
299
300   /* Do as if the gsettings key had changed, so we select the key that
301    * was last set. */
302   empathy_camera_menu_prefs_camera_changed_cb (self->priv->settings,
303       EMPATHY_PREFS_CALL_CAMERA_DEVICE, self);
304 }
305
306 static void
307 empathy_camera_menu_constructed (GObject *obj)
308 {
309   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
310   GtkUIManager *ui_manager;
311
312   g_assert (EMPATHY_IS_CALL_WINDOW (self->priv->window));
313
314   ui_manager = empathy_call_window_get_ui_manager (self->priv->window);
315
316   g_assert (GTK_IS_UI_MANAGER (ui_manager));
317
318   /* Okay let's go go go. */
319
320   self->priv->action_group = gtk_action_group_new ("EmpathyCameraMenu");
321   gtk_ui_manager_insert_action_group (ui_manager, self->priv->action_group, -1);
322   /* the UI manager now owns this */
323   g_object_unref (self->priv->action_group);
324
325   self->priv->anchor_action = g_object_new (GTK_TYPE_RADIO_ACTION,
326       "name", "EmpathyCameraMenuAnchorAction",
327       NULL);
328   gtk_action_group_add_action (self->priv->action_group,
329       self->priv->anchor_action);
330   g_object_unref (self->priv->anchor_action);
331
332   self->priv->camera_monitor = empathy_camera_monitor_new ();
333
334   tp_g_signal_connect_object (self->priv->camera_monitor, "added",
335       G_CALLBACK (empathy_camera_menu_camera_added_cb), self, 0);
336   tp_g_signal_connect_object (self->priv->camera_monitor, "removed",
337       G_CALLBACK (empathy_camera_menu_camera_removed_cb), self, 0);
338
339   self->priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
340   g_signal_connect (self->priv->settings,
341       "changed::"EMPATHY_PREFS_CALL_CAMERA_DEVICE,
342       G_CALLBACK (empathy_camera_menu_prefs_camera_changed_cb), self);
343
344   self->priv->cameras = g_queue_new ();
345
346   empathy_camera_menu_get_cameras (self);
347 }
348
349 static void
350 empathy_camera_menu_dispose (GObject *obj)
351 {
352   EmpathyCameraMenu *self = EMPATHY_CAMERA_MENU (obj);
353
354   tp_clear_pointer (&self->priv->cameras, g_queue_free);
355
356   tp_clear_object (&self->priv->camera_monitor);
357   tp_clear_object (&self->priv->settings);
358
359   G_OBJECT_CLASS (empathy_camera_menu_parent_class)->dispose (obj);
360 }
361
362 static void
363 empathy_camera_menu_class_init (EmpathyCameraMenuClass *klass)
364 {
365   GObjectClass *object_class = G_OBJECT_CLASS (klass);
366
367   object_class->set_property = empathy_camera_menu_set_property;
368   object_class->get_property = empathy_camera_menu_get_property;
369   object_class->constructed = empathy_camera_menu_constructed;
370   object_class->dispose = empathy_camera_menu_dispose;
371
372   g_object_class_install_property (object_class, PROP_WINDOW,
373       g_param_spec_object ("window", "window", "window",
374           EMPATHY_TYPE_CALL_WINDOW,
375           G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
376
377   g_type_class_add_private (object_class, sizeof (EmpathyCameraMenuPrivate));
378 }
379
380 EmpathyCameraMenu *
381 empathy_camera_menu_new (EmpathyCallWindow *window)
382 {
383   return g_object_new (EMPATHY_TYPE_CAMERA_MENU,
384       "window", window,
385       NULL);
386 }