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