]> git.0d.be Git - empathy.git/blob - src/empathy-call-factory.c
Updated Polish 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/telepathy-glib.h>
27
28 #include <libempathy/empathy-client-factory.h>
29 #include <libempathy/empathy-request-util.h>
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-call-factory.h"
33 #include "empathy-call-handler.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
37
38 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
39
40 static void handle_channels (TpBaseClient *client,
41     TpAccount *account,
42     TpConnection *connection,
43     GList *channels,
44     GList *requests_satisfied,
45     gint64 user_action_time,
46     TpHandleChannelsContext *context);
47
48 static void approve_channels (TpBaseClient *client,
49     TpAccount *account,
50     TpConnection *connection,
51     GList *channels,
52     TpChannelDispatchOperation *dispatch_operation,
53     TpAddDispatchOperationContext *context);
54
55 /* signal enum */
56 enum
57 {
58     NEW_CALL_HANDLER,
59     INCOMING_CALL,
60     LAST_SIGNAL
61 };
62
63 static guint signals[LAST_SIGNAL] = {0};
64
65 static GObject *call_factory = NULL;
66
67 static void
68 empathy_call_factory_init (EmpathyCallFactory *obj)
69 {
70   TpBaseClient *client = (TpBaseClient *) obj;
71
72   tp_base_client_take_approver_filter (client, tp_asv_new (
73         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
74           TP_IFACE_CHANNEL_TYPE_CALL,
75         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
76           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
77         NULL));
78
79   tp_base_client_take_handler_filter (client, 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,
83           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
84         NULL));
85
86   tp_base_client_take_handler_filter (client, tp_asv_new (
87         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
88           TP_IFACE_CHANNEL_TYPE_CALL,
89         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
90           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
91         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
92         NULL));
93
94   tp_base_client_take_handler_filter (client, tp_asv_new (
95         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
96           TP_IFACE_CHANNEL_TYPE_CALL,
97         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
98           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
99         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
100         NULL));
101
102   tp_base_client_add_handler_capabilities_varargs (client,
103       "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
104       "org.freedesktop.Telepathy.Channel.Type.Call1/video",
105       "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
106       "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
107       "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
108       NULL);
109 }
110
111 static GObject *
112 empathy_call_factory_constructor (GType type, guint n_construct_params,
113   GObjectConstructParam *construct_params)
114 {
115   g_return_val_if_fail (call_factory == NULL, NULL);
116
117   call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
118           (type, n_construct_params, construct_params);
119   g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
120
121   return call_factory;
122 }
123
124 static void
125 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
126 {
127   GObjectClass *object_class = G_OBJECT_CLASS (klass);
128   TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
129
130   object_class->constructor = empathy_call_factory_constructor;
131
132   base_clt_cls->handle_channels = handle_channels;
133   base_clt_cls->add_dispatch_operation = approve_channels;
134
135   signals[NEW_CALL_HANDLER] =
136     g_signal_new ("new-call-handler",
137       G_TYPE_FROM_CLASS (klass),
138       G_SIGNAL_RUN_LAST, 0,
139       NULL, NULL,
140       g_cclosure_marshal_generic,
141       G_TYPE_NONE,
142       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
143
144   signals[INCOMING_CALL] =
145     g_signal_new ("incoming-call",
146       G_TYPE_FROM_CLASS (klass),
147       G_SIGNAL_RUN_LAST, 0,
148       NULL, NULL,
149       g_cclosure_marshal_generic,
150       G_TYPE_BOOLEAN,
151       4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
152       TP_TYPE_CHANNEL_DISPATCH_OPERATION,
153       TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
154 }
155
156 EmpathyCallFactory *
157 empathy_call_factory_initialise (void)
158 {
159   EmpathyCallFactory *self;
160   EmpathyClientFactory *factory;
161   TpAccountManager *am;
162
163   g_return_val_if_fail (call_factory == NULL, NULL);
164
165   am = tp_account_manager_dup ();
166   factory = empathy_client_factory_dup ();
167
168   self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
169       "account-manager", am,
170       "factory", factory,
171       "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
172       NULL));
173
174   g_object_unref (am);
175   g_object_unref (factory);
176
177   return self;
178 }
179
180 EmpathyCallFactory *
181 empathy_call_factory_get (void)
182 {
183   g_return_val_if_fail (call_factory != NULL, NULL);
184
185   return EMPATHY_CALL_FACTORY (call_factory);
186 }
187
188 static void
189 handle_channels (TpBaseClient *client,
190     TpAccount *account,
191     TpConnection *connection,
192     GList *channels,
193     GList *requests_satisfied,
194     gint64 user_action_time,
195     TpHandleChannelsContext *context)
196 {
197   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
198   GList *l;
199
200   for (l = channels; l != NULL; l = g_list_next (l))
201     {
202       TpChannel *channel = l->data;
203       TpCallChannel *call;
204       TpContact *tp_contact;
205       EmpathyContact *contact;
206       EmpathyCallHandler *handler;
207
208       if (tp_proxy_get_invalidated (channel) != NULL)
209         continue;
210
211       if (tp_channel_get_channel_type_id (channel) !=
212           TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
213         continue;
214
215       if (!TP_IS_CALL_CHANNEL (channel))
216         continue;
217
218       call = TP_CALL_CHANNEL (channel);
219
220       tp_contact = tp_channel_get_target_contact (channel);
221       contact = empathy_contact_dup_from_tp_contact (tp_contact);
222       handler = empathy_call_handler_new_for_channel (call, contact);
223
224       g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
225           handler, FALSE);
226
227       g_object_unref (handler);
228       g_object_unref (contact);
229     }
230
231   tp_handle_channels_context_accept (context);
232 }
233
234 static TpCallChannel *
235 find_call_channel (GList *channels)
236 {
237   GList *l;
238
239   for (l = channels; l != NULL; l = g_list_next (l))
240     {
241       TpChannel *channel = l->data;
242       GQuark channel_type;
243
244       if (tp_proxy_get_invalidated (channel) != NULL)
245         continue;
246
247       channel_type = tp_channel_get_channel_type_id (channel);
248
249       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
250         return TP_CALL_CHANNEL (channel);
251     }
252
253   return NULL;
254 }
255
256 static void
257 approve_channels (TpBaseClient *client,
258     TpAccount *account,
259     TpConnection *connection,
260     GList *channels,
261     TpChannelDispatchOperation *dispatch_operation,
262     TpAddDispatchOperationContext *context)
263 {
264   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
265   TpCallChannel *channel;
266   guint handle;
267   GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "" };
268   gboolean handled = FALSE;
269
270   channel = find_call_channel (channels);
271
272   if (channel == NULL)
273     {
274       DEBUG ("Failed to find the main channel; ignoring");
275       error.message = "Unknown channel";
276       goto out;
277     }
278
279   handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
280
281   if (handle == 0)
282     {
283       DEBUG ("Unknown handle, ignoring");
284       error.code = TP_ERROR_INVALID_HANDLE;
285       error.message = "Unknown handle";
286       goto out;
287     }
288
289   g_signal_emit (self, signals[INCOMING_CALL], 0,
290       handle, channel, dispatch_operation, context,
291       &handled);
292
293   if (handled)
294     return;
295
296   /* There was no call window so the context wasn't handled. */
297   DEBUG ("Call with a contact for which there's no existing "
298     "call window, ignoring");
299   error.message = "No call window with this contact";
300
301  out:
302   tp_add_dispatch_operation_context_fail (context, &error);
303 }
304
305 gboolean
306 empathy_call_factory_register (EmpathyCallFactory *self,
307     GError **error)
308 {
309   return tp_base_client_register (TP_BASE_CLIENT (self), error);
310 }