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