]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-call-dialog.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[empathy.git] / libempathy-gtk / empathy-new-call-dialog.c
1 /*
2  * Copyright (C) 2009 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  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n-lib.h>
24
25 #include <libempathy/empathy-camera-monitor.h>
26 #include <libempathy/empathy-utils.h>
27
28 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
29 #include <libempathy/empathy-debug.h>
30
31 #include <libempathy-gtk/empathy-contact-chooser.h>
32 #include <libempathy-gtk/empathy-ui-utils.h>
33 #include <libempathy-gtk/empathy-images.h>
34
35 #include "empathy-new-call-dialog.h"
36 #include "empathy-call-utils.h"
37
38 static EmpathyNewCallDialog *dialog_singleton = NULL;
39
40 G_DEFINE_TYPE(EmpathyNewCallDialog, empathy_new_call_dialog,
41                GTK_TYPE_DIALOG)
42
43 struct _EmpathyNewCallDialogPriv {
44   GtkWidget *chooser;
45   GtkWidget *button_audio;
46   GtkWidget *button_video;
47
48   EmpathyCameraMonitor *monitor;
49 };
50
51 /* Re-use the accept and ok Gtk response so we are sure they won't be used
52  * when the dialog window is closed for example */
53 enum
54 {
55   RESPONSE_AUDIO = GTK_RESPONSE_ACCEPT,
56   RESPONSE_VIDEO = GTK_RESPONSE_OK,
57 };
58
59 /**
60  * SECTION:empathy-new-call-dialog
61  * @title: EmpathyNewCallDialog
62  * @short_description: A dialog to show a new call
63  * @include: libempathy-gtk/empathy-new-call-dialog.h
64  *
65  * #EmpathyNewCallDialog is a dialog which allows a call
66  * to be started with any contact on any enabled account.
67  */
68
69 static void
70 empathy_new_call_dialog_response (GtkDialog *dialog,
71     int response_id)
72 {
73   EmpathyNewCallDialog *self = (EmpathyNewCallDialog *) dialog;
74   FolksIndividual *individual;
75   EmpathyContact *contact;
76
77   if (response_id != RESPONSE_AUDIO &&
78       response_id != RESPONSE_VIDEO)
79     goto out;
80
81   individual = empathy_contact_chooser_dup_selected (
82       EMPATHY_CONTACT_CHOOSER (self->priv->chooser));
83   if (individual == NULL) goto out;
84
85   empathy_individual_can_audio_video_call (individual, NULL, NULL, &contact);
86   g_assert (contact != NULL);
87
88   empathy_call_new_with_streams (empathy_contact_get_id (contact),
89       empathy_contact_get_account (contact), TRUE,
90       response_id == RESPONSE_VIDEO, empathy_get_current_action_time ());
91
92   g_object_unref (individual);
93   g_object_unref (contact);
94
95 out:
96   gtk_widget_destroy (GTK_WIDGET (dialog));
97 }
98
99 static void
100 empathy_new_call_dialog_dispose (GObject *object)
101 {
102   EmpathyNewCallDialog *self = (EmpathyNewCallDialog *) object;
103
104   tp_clear_object (&self->priv->monitor);
105
106   G_OBJECT_CLASS (empathy_new_call_dialog_parent_class)->dispose (object);
107 }
108
109 static GObject *
110 empathy_new_call_dialog_constructor (GType type,
111     guint n_props,
112     GObjectConstructParam *props)
113 {
114   GObject *retval;
115
116   if (dialog_singleton)
117     {
118       retval = G_OBJECT (dialog_singleton);
119       g_object_ref (retval);
120     }
121   else
122     {
123       retval = G_OBJECT_CLASS (
124       empathy_new_call_dialog_parent_class)->constructor (type,
125         n_props, props);
126
127       dialog_singleton = EMPATHY_NEW_CALL_DIALOG (retval);
128       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
129     }
130
131   return retval;
132 }
133
134 static gboolean
135 filter_individual (EmpathyContactChooser *chooser,
136     FolksIndividual *individual,
137     gboolean is_online,
138     gboolean searching,
139     gpointer user_data)
140 {
141   gboolean can_audio_call, can_video_call;
142
143   empathy_individual_can_audio_video_call (individual, &can_audio_call,
144       &can_video_call, NULL);
145
146   return can_audio_call || can_video_call;
147 }
148
149 static void
150 selection_changed_cb (GtkWidget *chooser,
151     FolksIndividual *selected,
152     EmpathyNewCallDialog *self)
153 {
154   gboolean can_audio_call, can_video_call;
155
156   if (selected == NULL)
157     {
158       can_audio_call = can_video_call = FALSE;
159     }
160   else
161     {
162       empathy_individual_can_audio_video_call (selected, &can_audio_call,
163           &can_video_call, NULL);
164     }
165
166   gtk_widget_set_sensitive (self->priv->button_audio, can_audio_call);
167   gtk_widget_set_sensitive (self->priv->button_video, can_video_call);
168 }
169
170 static void
171 selection_activate_cb (GtkWidget *chooser,
172     EmpathyNewCallDialog *self)
173 {
174   gtk_dialog_response (GTK_DIALOG (self), RESPONSE_AUDIO);
175 }
176
177 static void
178 empathy_new_call_dialog_init (EmpathyNewCallDialog *self)
179 {
180   GtkWidget *label;
181   GtkWidget *image;
182   GtkWidget *content;
183
184   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
185       EMPATHY_TYPE_NEW_CALL_DIALOG, EmpathyNewCallDialogPriv);
186
187   self->priv->monitor = empathy_camera_monitor_dup_singleton ();
188
189   content = gtk_dialog_get_content_area (GTK_DIALOG (self));
190
191   label = gtk_label_new (_("Enter a contact identifier or phone number:"));
192   gtk_box_pack_start (GTK_BOX (content), label, FALSE, FALSE, 0);
193   gtk_widget_show (label);
194
195   /* contact chooser */
196   self->priv->chooser = empathy_contact_chooser_new ();
197
198   empathy_contact_chooser_set_filter_func (
199       EMPATHY_CONTACT_CHOOSER (self->priv->chooser), filter_individual, self);
200
201   gtk_box_pack_start (GTK_BOX (content), self->priv->chooser, TRUE, TRUE, 6);
202   gtk_widget_show (self->priv->chooser);
203
204   g_signal_connect (self->priv->chooser, "selection-changed",
205       G_CALLBACK (selection_changed_cb), self);
206   g_signal_connect (self->priv->chooser, "activate",
207       G_CALLBACK (selection_activate_cb), self);
208
209   /* close button */
210   gtk_dialog_add_button (GTK_DIALOG (self),
211       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
212
213   /* add video button */
214   self->priv->button_video = gtk_button_new_with_mnemonic (_("_Video Call"));
215   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
216       GTK_ICON_SIZE_BUTTON);
217   gtk_button_set_image (GTK_BUTTON (self->priv->button_video), image);
218
219   gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_video,
220       RESPONSE_VIDEO);
221   gtk_widget_show (self->priv->button_video);
222
223   /* add audio button */
224   self->priv->button_audio = gtk_button_new_with_mnemonic (_("_Audio Call"));
225   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
226       GTK_ICON_SIZE_BUTTON);
227   gtk_button_set_image (GTK_BUTTON (self->priv->button_audio), image);
228
229   gtk_dialog_add_action_widget (GTK_DIALOG (self), self->priv->button_audio,
230       RESPONSE_AUDIO);
231   gtk_widget_show (self->priv->button_audio);
232
233   /* Tweak the dialog */
234   gtk_window_set_title (GTK_WINDOW (self), _("New Call"));
235   gtk_window_set_role (GTK_WINDOW (self), "new_call");
236
237   /* Set a default height so a few contacts are displayed */
238   gtk_window_set_default_size (GTK_WINDOW (self), -1, 400);
239
240   gtk_widget_set_sensitive (self->priv->button_audio, FALSE);
241   gtk_widget_set_sensitive (self->priv->button_video, FALSE);
242 }
243
244 static void
245 empathy_new_call_dialog_class_init (
246   EmpathyNewCallDialogClass *class)
247 {
248   GObjectClass *object_class = G_OBJECT_CLASS (class);
249   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
250
251   g_type_class_add_private (class, sizeof (EmpathyNewCallDialogPriv));
252
253   object_class->constructor = empathy_new_call_dialog_constructor;
254   object_class->dispose = empathy_new_call_dialog_dispose;
255
256   dialog_class->response = empathy_new_call_dialog_response;
257 }
258
259 /**
260  * empathy_new_call_dialog_new:
261  * @parent: parent #GtkWindow of the dialog
262  *
263  * Create a new #EmpathyNewCallDialog it.
264  *
265  * Return value: the new #EmpathyNewCallDialog
266  */
267 GtkWidget *
268 empathy_new_call_dialog_show (GtkWindow *parent)
269 {
270   GtkWidget *dialog;
271
272   dialog = g_object_new (EMPATHY_TYPE_NEW_CALL_DIALOG, NULL);
273
274   if (parent)
275     {
276       gtk_window_set_transient_for (GTK_WINDOW (dialog),
277                   GTK_WINDOW (parent));
278     }
279
280   gtk_widget_show (dialog);
281   return dialog;
282 }