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