]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
Merge branch 'gnome-3-8'
[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 #include "empathy-call-utils.h"
23
24 #include <glib/gi18n-lib.h>
25 #include <gtk/gtk.h>
26
27 #include "empathy-request-util.h"
28
29 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
30 #include "empathy-debug.h"
31
32 static const gchar *
33 get_error_display_message (GError *error)
34 {
35   if (error->domain != TP_ERROR)
36     return _("There was an error starting the call");
37
38   switch (error->code)
39     {
40       case TP_ERROR_NETWORK_ERROR:
41         return _("Network error");
42       case TP_ERROR_NOT_CAPABLE:
43         return _("The specified contact doesn't support calls");
44       case TP_ERROR_OFFLINE:
45         return _("The specified contact is offline");
46       case TP_ERROR_INVALID_HANDLE:
47         return _("The specified contact is not valid");
48       case TP_ERROR_EMERGENCY_CALLS_NOT_SUPPORTED:
49         return _("Emergency calls are not supported on this protocol");
50       case TP_ERROR_INSUFFICIENT_BALANCE:
51         return _("You don't have enough credit in order to place this call");
52     }
53
54   return _("There was an error starting the call");
55 }
56
57 static void
58 show_call_error (GError *error)
59 {
60   GtkWidget *dialog;
61
62   dialog = gtk_message_dialog_new (NULL, 0,
63       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
64       "%s", get_error_display_message (error));
65
66   g_signal_connect_swapped (dialog, "response",
67       G_CALLBACK (gtk_widget_destroy),
68       dialog);
69
70   gtk_widget_show (dialog);
71 }
72
73 GHashTable *
74 empathy_call_create_call_request (const gchar *contact,
75     gboolean initial_audio,
76     gboolean initial_video)
77 {
78   GHashTable *asv = tp_asv_new (
79     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
80       TP_IFACE_CHANNEL_TYPE_CALL,
81     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
82       TP_HANDLE_TYPE_CONTACT,
83     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
84       contact,
85     NULL);
86
87   /* Only add InitialAudio or InitialVideo if they are true: it should work
88    * with genuinely voice-only CMs. */
89   if (initial_audio)
90     tp_asv_set_boolean (asv, TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO,
91                         initial_audio);
92   if (initial_video)
93     tp_asv_set_boolean (asv, TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO,
94                         initial_video);
95
96   return asv;
97 }
98
99 static void
100 create_call_channel_cb (GObject *source,
101     GAsyncResult *result,
102     gpointer user_data)
103 {
104   GError *error = NULL;
105
106   if (tp_account_channel_request_create_channel_finish (
107       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
108     return;
109
110   DEBUG ("Failed to create Call channel: %s", error->message);
111
112   show_call_error (error);
113 }
114
115 /* Try to request a Call channel and fallback to StreamedMedia if that fails */
116 static void
117 call_new_with_streams (const gchar *contact,
118     TpAccount *account,
119     gboolean initial_audio,
120     gboolean initial_video,
121     gint64 timestamp)
122 {
123   GHashTable *call_request;
124   TpAccountChannelRequest *call_req;
125
126   /* Call */
127   call_request = empathy_call_create_call_request (contact,
128       initial_audio,
129       initial_video);
130
131   call_req = tp_account_channel_request_new (account, call_request, timestamp);
132
133   g_hash_table_unref (call_request);
134
135   tp_account_channel_request_create_channel_async (call_req,
136       EMPATHY_CALL_BUS_NAME, NULL, create_call_channel_cb, NULL);
137
138   g_object_unref (call_req);
139 }
140
141 void
142 empathy_call_new_with_streams (const gchar *contact,
143     TpAccount *account,
144     gboolean initial_audio,
145     gboolean initial_video,
146     gint64 timestamp)
147 {
148   call_new_with_streams (contact, account, initial_audio, initial_video,
149       timestamp);
150 }
151
152 /* Copied from telepathy-yell call-channel.c */
153 void
154 empathy_call_channel_send_video (TpCallChannel *self,
155     gboolean send)
156 {
157   GPtrArray *contents;
158   gboolean found = FALSE;
159   guint i;
160
161   g_return_if_fail (TP_IS_CALL_CHANNEL (self));
162
163   /* Loop over all the contents, if some of them a video set all their
164    * streams to sending, otherwise request a video channel in case we want to
165    * sent */
166   contents = tp_call_channel_get_contents (self);
167   for (i = 0 ; i < contents->len ; i++)
168     {
169       TpCallContent *content = g_ptr_array_index (contents, i);
170
171       if (tp_call_content_get_media_type (content) ==
172               TP_MEDIA_STREAM_TYPE_VIDEO)
173         {
174           GPtrArray *streams;
175           guint j;
176
177           found = TRUE;
178           streams = tp_call_content_get_streams (content);
179           for (j = 0; j < streams->len; j++)
180             {
181               TpCallStream *stream = g_ptr_array_index (streams, j);
182
183               tp_call_stream_set_sending_async (stream, send, NULL, NULL);
184             }
185         }
186     }
187
188   if (send && !found)
189     {
190       tp_call_channel_add_content_async (self, "video",
191           TP_MEDIA_STREAM_TYPE_VIDEO, TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL,
192           NULL, NULL);
193     }
194 }
195
196 /* Copied from telepathy-yell call-channel.c */
197 TpSendingState
198 empathy_call_channel_get_video_state (TpCallChannel *self)
199 {
200   TpSendingState result = TP_SENDING_STATE_NONE;
201   GPtrArray *contents;
202   guint i;
203
204   g_return_val_if_fail (TP_IS_CALL_CHANNEL (self), TP_SENDING_STATE_NONE);
205
206   contents = tp_call_channel_get_contents (self);
207   for (i = 0 ; i < contents->len ; i++)
208     {
209       TpCallContent *content = g_ptr_array_index (contents, i);
210
211       if (tp_call_content_get_media_type (content) ==
212               TP_MEDIA_STREAM_TYPE_VIDEO)
213         {
214           GPtrArray *streams;
215           guint j;
216
217           streams = tp_call_content_get_streams (content);
218           for (j = 0; j < streams->len; j++)
219             {
220               TpCallStream *stream = g_ptr_array_index (streams, j);
221               TpSendingState state;
222
223               state = tp_call_stream_get_local_sending_state (stream);
224               if (state != TP_SENDING_STATE_PENDING_STOP_SENDING &&
225                   state > result)
226                 result = state;
227             }
228         }
229     }
230
231   return result;
232 }