]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
Return meaningful errors when starting a call fails
[empathy.git] / libempathy-gtk / empathy-call-utils.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
4  * The code contained in this file is free software; you can redistribute
5  * it and/or modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either version
7  * 2.1 of the License, or (at your option) any later version.
8  *
9  * This file 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 code; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include <gtk/gtk.h>
26
27 #include <telepathy-glib/telepathy-glib.h>
28
29 #include <telepathy-yell/telepathy-yell.h>
30
31 #include "empathy-call-utils.h"
32
33 #include <libempathy/empathy-request-util.h>
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
37
38 static const gchar *
39 get_error_display_message (GError *error)
40 {
41   if (error->domain != TP_ERROR)
42     return _("There was an error starting the call");
43
44   switch (error->code)
45     {
46       case TP_ERROR_NETWORK_ERROR:
47         return _("Network error");
48       case TP_ERROR_NOT_CAPABLE:
49         return _("The specified contact doesn't support calls");
50       case TP_ERROR_OFFLINE:
51         return _("The specified contact is offline");
52       case TP_ERROR_INVALID_HANDLE:
53         return _("The specified contact is not valid");
54     }
55
56   return _("There was an error starting the call");
57 }
58
59 static void
60 show_call_error (GError *error)
61 {
62   GtkWidget *dialog;
63
64   dialog = gtk_message_dialog_new (NULL, 0,
65       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
66       get_error_display_message (error));
67
68   g_signal_connect_swapped (dialog, "response",
69       G_CALLBACK (gtk_widget_destroy),
70       dialog);
71
72   gtk_widget_show (dialog);
73 }
74
75 GHashTable *
76 empathy_call_create_call_request (const gchar *contact,
77     gboolean initial_audio,
78     gboolean initial_video)
79 {
80   return tp_asv_new (
81     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
82       TPY_IFACE_CHANNEL_TYPE_CALL,
83     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
84       TP_HANDLE_TYPE_CONTACT,
85     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
86       contact,
87     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
88       initial_audio,
89     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
90       initial_video,
91     NULL);
92 }
93
94 GHashTable *
95 empathy_call_create_streamed_media_request (const gchar *contact,
96     gboolean initial_audio,
97     gboolean initial_video)
98 {
99   return 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,
103       TP_HANDLE_TYPE_CONTACT,
104     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
105       contact,
106     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
107       initial_audio,
108     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
109       initial_video,
110     NULL);
111 }
112
113 static void
114 create_call_channel_cb (GObject *source,
115     GAsyncResult *result,
116     gpointer user_data)
117 {
118   GError *error = NULL;
119
120   if (!tp_account_channel_request_create_channel_finish (
121            TP_ACCOUNT_CHANNEL_REQUEST (source),
122            result,
123            &error))
124     {
125       DEBUG ("Failed to create Call channel: %s", error->message);
126       show_call_error (error);
127       g_error_free (error);
128     }
129 }
130
131 static void
132 create_streamed_media_channel_cb (GObject *source,
133     GAsyncResult *result,
134     gpointer user_data)
135 {
136   TpAccountChannelRequest *call_req = user_data;
137   GError *error = NULL;
138
139   if (tp_account_channel_request_create_channel_finish (
140       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
141     {
142       g_object_unref (call_req);
143       return;
144     }
145
146   DEBUG ("Failed to create StreamedMedia channel: %s", error->message);
147
148   if (error->code != TP_ERROR_NOT_IMPLEMENTED)
149     {
150       show_call_error (error);
151       return;
152     }
153
154   DEBUG ("Let's try with a Call channel");
155   g_error_free (error);
156   tp_account_channel_request_create_channel_async (call_req,
157       EMPATHY_CALL_BUS_NAME, NULL,
158       create_call_channel_cb,
159       NULL);
160 }
161
162 void
163 empathy_call_new_with_streams (const gchar *contact,
164     TpAccount *account,
165     gboolean initial_audio,
166     gboolean initial_video,
167     gint64 timestamp)
168 {
169   GHashTable *call_request, *streamed_media_request;
170   TpAccountChannelRequest *call_req, *streamed_media_req;
171
172   call_request = empathy_call_create_call_request (contact,
173       initial_audio,
174       initial_video);
175
176   streamed_media_request = empathy_call_create_streamed_media_request (
177       contact, initial_audio, initial_video);
178
179   call_req = tp_account_channel_request_new (account, call_request, timestamp);
180   streamed_media_req = tp_account_channel_request_new (account,
181       streamed_media_request,
182       timestamp);
183
184   tp_account_channel_request_create_channel_async (streamed_media_req,
185       EMPATHY_AV_BUS_NAME, NULL,
186       create_streamed_media_channel_cb,
187       call_req);
188
189   g_hash_table_unref (call_request);
190   g_hash_table_unref (streamed_media_request);
191   g_object_unref (streamed_media_req);
192 }