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