]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-new-call-dialog.c
Merge branch 'sasl'
[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 <libempathy/empathy-tp-contact-factory.h>
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-call-factory.h>
34 #include <libempathy/empathy-utils.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
37 #include <libempathy/empathy-debug.h>
38
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
45 static EmpathyNewCallDialog *dialog_singleton = NULL;
46
47 G_DEFINE_TYPE(EmpathyNewCallDialog, empathy_new_call_dialog,
48                EMPATHY_TYPE_CONTACT_SELECTOR_DIALOG)
49
50 typedef struct _EmpathyNewCallDialogPriv EmpathyNewCallDialogPriv;
51
52 typedef struct {
53   EmpathyAccountChooserFilterResultCallback callback;
54   gpointer                                  user_data;
55 } FilterCallbackData;
56
57 struct _EmpathyNewCallDialogPriv {
58   GtkWidget *check_video;
59 };
60
61 #define GET_PRIV(o) \
62   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_NEW_CALL_DIALOG, \
63     EmpathyNewCallDialogPriv))
64
65 static void
66 create_media_channel_cb (GObject *source,
67     GAsyncResult *result,
68     gpointer user_data)
69 {
70   GError *error = NULL;
71
72   if (!tp_account_channel_request_create_channel_finish (
73         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
74     {
75       DEBUG ("Failed to create media channel: %s", error->message);
76       g_error_free (error);
77     }
78 }
79
80 /**
81  * SECTION:empathy-new-call-dialog
82  * @title: EmpathyNewCallDialog
83  * @short_description: A dialog to show a new call
84  * @include: libempathy-gtk/empathy-new-call-dialog.h
85  *
86  * #EmpathyNewCallDialog is a dialog which allows a call
87  * to be started with any contact on any enabled account.
88  */
89
90 static void
91 call_contact (TpAccount *account,
92     const gchar *contact_id,
93     gboolean video,
94     gint64 timestamp)
95 {
96   GHashTable *request;
97   TpAccountChannelRequest *req;
98
99   request = tp_asv_new (
100       TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
101         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
102       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
103       TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING, contact_id,
104       TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
105         TRUE,
106       TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
107         video,
108       NULL);
109
110   req = tp_account_channel_request_new (account, request, timestamp);
111
112   tp_account_channel_request_create_channel_async (req, NULL, NULL,
113       create_media_channel_cb, NULL);
114
115   g_object_unref (req);
116 }
117
118 static void
119 empathy_new_call_dialog_response (GtkDialog *dialog, int response_id)
120 {
121   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
122   gboolean video;
123   TpAccount *account;
124   const gchar *contact_id;
125
126   if (response_id != GTK_RESPONSE_ACCEPT) goto out;
127
128   contact_id = empathy_contact_selector_dialog_get_selected (
129       EMPATHY_CONTACT_SELECTOR_DIALOG (dialog), NULL, &account);
130
131   if (EMP_STR_EMPTY (contact_id) || account == NULL) goto out;
132
133   /* check if video is enabled now because the dialog will be destroyed once
134    * we return from this function. */
135   video = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->check_video));
136
137   call_contact (account, contact_id, video, gtk_get_current_event_time ());
138
139 out:
140   gtk_widget_destroy (GTK_WIDGET (dialog));
141 }
142
143 static void
144 conn_prepared_cb (GObject *conn,
145     GAsyncResult *result,
146     gpointer user_data)
147 {
148   FilterCallbackData *data = user_data;
149   GError *myerr = NULL;
150   TpCapabilities *caps;
151   GPtrArray *classes;
152   guint i;
153
154   if (!tp_proxy_prepare_finish (conn, result, &myerr))
155       goto out;
156
157   caps = tp_connection_get_capabilities (TP_CONNECTION (conn));
158   classes = tp_capabilities_get_channel_classes (caps);
159
160   for (i = 0; i < classes->len; i++)
161     {
162       GHashTable *fixed;
163       GStrv allowed;
164       const gchar *chan_type;
165
166       tp_value_array_unpack (g_ptr_array_index (classes, i), 2,
167           &fixed, &allowed);
168
169       chan_type = tp_asv_get_string (fixed, TP_PROP_CHANNEL_CHANNEL_TYPE);
170
171       if (tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
172         continue;
173
174       if (tp_asv_get_uint32 (fixed, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, NULL) !=
175           TP_HANDLE_TYPE_CONTACT)
176         continue;
177
178       data->callback (TRUE, data->user_data);
179       g_slice_free (FilterCallbackData, data);
180       return;
181     }
182
183 out:
184   data->callback (FALSE, data->user_data);
185   g_slice_free (FilterCallbackData, data);
186 }
187
188 static void
189 empathy_new_call_dialog_account_filter (EmpathyContactSelectorDialog *dialog,
190     EmpathyAccountChooserFilterResultCallback callback,
191     gpointer callback_data,
192     TpAccount *account)
193 {
194   TpConnection *connection;
195   FilterCallbackData *cb_data;
196   GQuark features[] = { TP_CONNECTION_FEATURE_CAPABILITIES, 0 };
197
198   if (tp_account_get_connection_status (account, NULL) !=
199       TP_CONNECTION_STATUS_CONNECTED)
200     {
201       callback (FALSE, callback_data);
202       return;
203     }
204
205   /* check if CM supports calls */
206   connection = tp_account_get_connection (account);
207   if (connection == NULL)
208     {
209       callback (FALSE, callback_data);
210       return;
211     }
212
213   cb_data = g_slice_new0 (FilterCallbackData);
214   cb_data->callback = callback;
215   cb_data->user_data = callback_data;
216   tp_proxy_prepare_async (connection, features, conn_prepared_cb, cb_data);
217 }
218
219 static GObject *
220 empathy_new_call_dialog_constructor (GType type,
221     guint n_props,
222     GObjectConstructParam *props)
223 {
224   GObject *retval;
225
226   if (dialog_singleton)
227     {
228       retval = G_OBJECT (dialog_singleton);
229       g_object_ref (retval);
230     }
231   else
232     {
233       retval = G_OBJECT_CLASS (
234       empathy_new_call_dialog_parent_class)->constructor (type,
235         n_props, props);
236
237       dialog_singleton = EMPATHY_NEW_CALL_DIALOG (retval);
238       g_object_add_weak_pointer (retval, (gpointer) &dialog_singleton);
239     }
240
241   return retval;
242 }
243
244 static void
245 empathy_new_call_dialog_init (EmpathyNewCallDialog *dialog)
246 {
247   EmpathyContactSelectorDialog *parent = EMPATHY_CONTACT_SELECTOR_DIALOG (
248         dialog);
249   EmpathyNewCallDialogPriv *priv = GET_PRIV (dialog);
250   GtkWidget *image;
251
252   /* add video toggle */
253   priv->check_video = gtk_check_button_new_with_mnemonic (_("Send _Video"));
254
255   gtk_box_pack_end (GTK_BOX (parent->vbox), priv->check_video,
256       FALSE, TRUE, 0);
257
258   gtk_widget_show (priv->check_video);
259
260   /* add chat button */
261   parent->button_action = gtk_button_new_with_mnemonic (_("C_all"));
262   image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP,
263       GTK_ICON_SIZE_BUTTON);
264   gtk_button_set_image (GTK_BUTTON (parent->button_action), image);
265
266   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), parent->button_action,
267       GTK_RESPONSE_ACCEPT);
268   gtk_widget_show (parent->button_action);
269
270   /* Tweak the dialog */
271   gtk_window_set_title (GTK_WINDOW (dialog), _("New Call"));
272   gtk_window_set_role (GTK_WINDOW (dialog), "new_call");
273
274   gtk_widget_set_sensitive (parent->button_action, FALSE);
275 }
276
277 static void
278 empathy_new_call_dialog_class_init (
279   EmpathyNewCallDialogClass *class)
280 {
281   GObjectClass *object_class = G_OBJECT_CLASS (class);
282   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (class);
283   EmpathyContactSelectorDialogClass *selector_dialog_class = \
284     EMPATHY_CONTACT_SELECTOR_DIALOG_CLASS (class);
285
286   g_type_class_add_private (class, sizeof (EmpathyNewCallDialogPriv));
287
288   object_class->constructor = empathy_new_call_dialog_constructor;
289
290   dialog_class->response = empathy_new_call_dialog_response;
291
292   selector_dialog_class->account_filter = empathy_new_call_dialog_account_filter;
293 }
294
295 /**
296  * empathy_new_call_dialog_new:
297  * @parent: parent #GtkWindow of the dialog
298  *
299  * Create a new #EmpathyNewCallDialog it.
300  *
301  * Return value: the new #EmpathyNewCallDialog
302  */
303 GtkWidget *
304 empathy_new_call_dialog_show (GtkWindow *parent)
305 {
306   GtkWidget *dialog;
307
308   dialog = g_object_new (EMPATHY_TYPE_NEW_CALL_DIALOG, NULL);
309
310   if (parent)
311     {
312       gtk_window_set_transient_for (GTK_WINDOW (dialog),
313                   GTK_WINDOW (parent));
314     }
315
316   gtk_widget_show (dialog);
317   return dialog;
318 }