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