]> git.0d.be Git - empathy.git/blob - src/empathy-call-factory.c
Updated Belarusian translation.
[empathy.git] / src / empathy-call-factory.c
1 /*
2  * empathy-call-factory.c - Source for EmpathyCallFactory
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <telepathy-glib/account-channel-request.h>
27 #include <telepathy-glib/simple-handler.h>
28 #include <telepathy-glib/interfaces.h>
29 #include <telepathy-glib/util.h>
30
31 #include <libempathy/empathy-client-factory.h>
32 #include <libempathy/empathy-request-util.h>
33 #include <libempathy/empathy-tp-contact-factory.h>
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-call-factory.h"
37 #include "empathy-call-handler.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
40 #include <libempathy/empathy-debug.h>
41
42 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
43
44 static void handle_channels (TpBaseClient *client,
45     TpAccount *account,
46     TpConnection *connection,
47     GList *channels,
48     GList *requests_satisfied,
49     gint64 user_action_time,
50     TpHandleChannelsContext *context);
51
52 static void approve_channels (TpBaseClient *client,
53     TpAccount *account,
54     TpConnection *connection,
55     GList *channels,
56     TpChannelDispatchOperation *dispatch_operation,
57     TpAddDispatchOperationContext *context);
58
59 /* signal enum */
60 enum
61 {
62     NEW_CALL_HANDLER,
63     INCOMING_CALL,
64     LAST_SIGNAL
65 };
66
67 static guint signals[LAST_SIGNAL] = {0};
68
69 static GObject *call_factory = NULL;
70
71 static void
72 empathy_call_factory_init (EmpathyCallFactory *obj)
73 {
74   TpBaseClient *client = (TpBaseClient *) obj;
75
76   tp_base_client_take_approver_filter (client, tp_asv_new (
77         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
78           TP_IFACE_CHANNEL_TYPE_CALL,
79         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
80           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
81         NULL));
82
83   tp_base_client_take_handler_filter (client, tp_asv_new (
84         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
85           TP_IFACE_CHANNEL_TYPE_CALL,
86         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
87           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
88         NULL));
89
90   tp_base_client_take_handler_filter (client, tp_asv_new (
91         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
92           TP_IFACE_CHANNEL_TYPE_CALL,
93         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
94           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
95         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
96         NULL));
97
98   tp_base_client_take_handler_filter (client, tp_asv_new (
99         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
100           TP_IFACE_CHANNEL_TYPE_CALL,
101         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
102           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
103         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
104         NULL));
105
106   tp_base_client_add_handler_capabilities_varargs (client,
107       "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
108       "org.freedesktop.Telepathy.Channel.Type.Call1/video",
109       "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
110       "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
111       "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
112       NULL);
113 }
114
115 static GObject *
116 empathy_call_factory_constructor (GType type, guint n_construct_params,
117   GObjectConstructParam *construct_params)
118 {
119   g_return_val_if_fail (call_factory == NULL, NULL);
120
121   call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
122           (type, n_construct_params, construct_params);
123   g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
124
125   return call_factory;
126 }
127
128 static void
129 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
130 {
131   GObjectClass *object_class = G_OBJECT_CLASS (klass);
132   TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
133
134   object_class->constructor = empathy_call_factory_constructor;
135
136   base_clt_cls->handle_channels = handle_channels;
137   base_clt_cls->add_dispatch_operation = approve_channels;
138
139   signals[NEW_CALL_HANDLER] =
140     g_signal_new ("new-call-handler",
141       G_TYPE_FROM_CLASS (klass),
142       G_SIGNAL_RUN_LAST, 0,
143       NULL, NULL,
144       g_cclosure_marshal_generic,
145       G_TYPE_NONE,
146       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
147
148   signals[INCOMING_CALL] =
149     g_signal_new ("incoming-call",
150       G_TYPE_FROM_CLASS (klass),
151       G_SIGNAL_RUN_LAST, 0,
152       NULL, NULL,
153       g_cclosure_marshal_generic,
154       G_TYPE_BOOLEAN,
155       4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
156       TP_TYPE_CHANNEL_DISPATCH_OPERATION,
157       TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
158 }
159
160 EmpathyCallFactory *
161 empathy_call_factory_initialise (void)
162 {
163   EmpathyCallFactory *self;
164   EmpathyClientFactory *factory;
165   TpAccountManager *am;
166
167   g_return_val_if_fail (call_factory == NULL, NULL);
168
169   am = tp_account_manager_dup ();
170   factory = empathy_client_factory_dup ();
171
172   self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
173       "account-manager", am,
174       "factory", factory,
175       "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
176       NULL));
177
178   g_object_unref (am);
179   g_object_unref (factory);
180
181   return self;
182 }
183
184 EmpathyCallFactory *
185 empathy_call_factory_get (void)
186 {
187   g_return_val_if_fail (call_factory != NULL, NULL);
188
189   return EMPATHY_CALL_FACTORY (call_factory);
190 }
191
192 static void
193 handle_channels (TpBaseClient *client,
194     TpAccount *account,
195     TpConnection *connection,
196     GList *channels,
197     GList *requests_satisfied,
198     gint64 user_action_time,
199     TpHandleChannelsContext *context)
200 {
201   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
202   GList *l;
203
204   for (l = channels; l != NULL; l = g_list_next (l))
205     {
206       TpChannel *channel = l->data;
207       TpCallChannel *call;
208       TpContact *tp_contact;
209       EmpathyContact *contact;
210       EmpathyCallHandler *handler;
211
212       if (tp_proxy_get_invalidated (channel) != NULL)
213         continue;
214
215       if (tp_channel_get_channel_type_id (channel) !=
216           TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
217         continue;
218
219       if (!TP_IS_CALL_CHANNEL (channel))
220         continue;
221
222       call = TP_CALL_CHANNEL (channel);
223
224       tp_contact = tp_channel_get_target_contact (channel);
225       contact = empathy_contact_dup_from_tp_contact (tp_contact);
226       handler = empathy_call_handler_new_for_channel (call, contact);
227
228       g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
229           handler, FALSE);
230
231       g_object_unref (handler);
232       g_object_unref (contact);
233     }
234
235   tp_handle_channels_context_accept (context);
236 }
237
238 static TpCallChannel *
239 find_call_channel (GList *channels)
240 {
241   GList *l;
242
243   for (l = channels; l != NULL; l = g_list_next (l))
244     {
245       TpChannel *channel = l->data;
246       GQuark channel_type;
247
248       if (tp_proxy_get_invalidated (channel) != NULL)
249         continue;
250
251       channel_type = tp_channel_get_channel_type_id (channel);
252
253       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
254         return TP_CALL_CHANNEL (channel);
255     }
256
257   return NULL;
258 }
259
260 static void
261 approve_channels (TpBaseClient *client,
262     TpAccount *account,
263     TpConnection *connection,
264     GList *channels,
265     TpChannelDispatchOperation *dispatch_operation,
266     TpAddDispatchOperationContext *context)
267 {
268   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
269   TpCallChannel *channel;
270   guint handle;
271   GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "" };
272   gboolean handled = FALSE;
273
274   channel = find_call_channel (channels);
275
276   if (channel == NULL)
277     {
278       DEBUG ("Failed to find the main channel; ignoring");
279       error.message = "Unknown channel";
280       goto out;
281     }
282
283   handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
284
285   if (handle == 0)
286     {
287       DEBUG ("Unknown handle, ignoring");
288       error.code = TP_ERROR_INVALID_HANDLE;
289       error.message = "Unknown handle";
290       goto out;
291     }
292
293   g_signal_emit (self, signals[INCOMING_CALL], 0,
294       handle, channel, dispatch_operation, context,
295       &handled);
296
297   if (handled)
298     return;
299
300   /* There was no call window so the context wasn't handled. */
301   DEBUG ("Call with a contact for which there's no existing "
302     "call window, ignoring");
303   error.message = "No call window with this contact";
304
305  out:
306   tp_add_dispatch_operation_context_fail (context, &error);
307 }
308
309 gboolean
310 empathy_call_factory_register (EmpathyCallFactory *self,
311     GError **error)
312 {
313   return tp_base_client_register (TP_BASE_CLIENT (self), error);
314 }