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