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