]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
fix FT client side code as the generated code changed
[empathy.git] / src / empathy-event-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #include <telepathy-glib/util.h>
28
29 #include <libempathy/empathy-dispatcher.h>
30 #include <libempathy/empathy-contact-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-tp-chat.h>
33 #include <libempathy/empathy-tp-group.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-tp-file.h>
36
37 #include <libempathy-gtk/empathy-ft-manager.h>
38 #include <libempathy-gtk/empathy-images.h>
39 #include <libempathy-gtk/empathy-contact-dialogs.h>
40
41 #include "empathy-event-manager.h"
42
43 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
44 #include <libempathy/empathy-debug.h>
45
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
47 typedef struct {
48         EmpathyDispatcher     *dispatcher;
49         EmpathyContactManager *contact_manager;
50         GSList                *events;
51 } EmpathyEventManagerPriv;
52
53 typedef struct _EventPriv EventPriv;
54 typedef void (*EventFunc) (EventPriv *event);
55
56 struct _EventPriv {
57         EmpathyEvent         public;
58         TpChannel           *channel;
59         EmpathyEventManager *manager;
60         EventFunc            func;
61         gpointer             user_data;
62 };
63
64 enum {
65         EVENT_ADDED,
66         EVENT_REMOVED,
67         LAST_SIGNAL
68 };
69
70 static guint signals[LAST_SIGNAL];
71
72 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
73
74 static void event_remove (EventPriv *event);
75
76 static void
77 event_free (EventPriv *event)
78 {
79         g_free (event->public.icon_name);
80         g_free (event->public.message);
81
82         if (event->public.contact) {
83                 g_object_unref (event->public.contact);
84         }
85
86         if (event->channel) {
87                 g_signal_handlers_disconnect_by_func (event->channel,
88                                                       event_remove,
89                                                       event);
90                 g_object_unref (event->channel);
91         }
92         g_slice_free (EventPriv, event);
93 }
94
95 static void
96 event_remove (EventPriv *event)
97 {
98         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
99
100         DEBUG ("Removing event %p", event);
101         priv->events = g_slist_remove (priv->events, event);
102         g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
103         event_free (event);
104 }
105
106 static void
107 event_manager_add (EmpathyEventManager *manager,
108                    EmpathyContact      *contact,
109                    const gchar         *icon_name,
110                    const gchar         *message,
111                    TpChannel           *channel,
112                    EventFunc            func,
113                    gpointer             user_data)
114 {
115         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
116         EventPriv               *event;
117
118         event = g_slice_new0 (EventPriv);
119         event->public.contact = contact ? g_object_ref (contact) : NULL;
120         event->public.icon_name = g_strdup (icon_name);
121         event->public.message = g_strdup (message);
122         event->manager = manager;
123         event->func = func;
124         event->user_data = user_data;
125
126         if (channel) {
127                 event->channel = g_object_ref (channel);
128                 g_signal_connect_swapped (channel, "invalidated",
129                                           G_CALLBACK (event_remove),
130                                           event);
131         }
132
133         DEBUG ("Adding event %p", event);
134         priv->events = g_slist_prepend (priv->events, event);
135         g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
136 }
137
138 static void
139 event_channel_process_func (EventPriv *event)
140 {
141         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
142
143         /* This will emit "dispatch-channel" and the event will be removed
144          * in the callback of that signal, no need to remove the event here. */ 
145         empathy_dispatcher_channel_process (priv->dispatcher, event->channel);
146 }
147
148 static gboolean
149 event_manager_chat_unref_idle (gpointer user_data)
150 {
151         g_object_unref (user_data);
152         return FALSE;
153 }
154
155 static void
156 event_manager_chat_message_received_cb (EmpathyTpChat       *tp_chat,
157                                         EmpathyMessage      *message,
158                                         EmpathyEventManager *manager)
159 {
160         EmpathyContact  *sender;
161         gchar           *msg;
162         TpChannel       *channel;
163
164         g_idle_add (event_manager_chat_unref_idle, tp_chat);
165         g_signal_handlers_disconnect_by_func (tp_chat,
166                                               event_manager_chat_message_received_cb,
167                                               manager);
168
169         sender = empathy_message_get_sender (message);
170         msg = g_strdup_printf (_("New message from %s:\n%s"),
171                                empathy_contact_get_name (sender),
172                                empathy_message_get_body (message));
173
174         channel = empathy_tp_chat_get_channel (tp_chat);
175         event_manager_add (manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
176                            channel, event_channel_process_func, NULL);
177
178         g_free (msg);
179 }
180
181 static void
182 event_manager_filter_channel_cb (EmpathyDispatcher   *dispatcher,
183                                  TpChannel           *channel,
184                                  EmpathyEventManager *manager)
185 {
186         gchar *channel_type;
187
188         g_object_get (channel, "channel-type", &channel_type, NULL);
189         if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
190                 EmpathyTpChat *tp_chat;
191
192                 tp_chat = empathy_tp_chat_new (channel);
193                 g_signal_connect (tp_chat, "message-received",
194                                   G_CALLBACK (event_manager_chat_message_received_cb),
195                                   manager);
196         }
197         else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
198                 EmpathyTpGroup *tp_group;
199                 EmpathyContact *contact;
200                 gchar          *msg;
201
202                 tp_group = empathy_tp_group_new (channel);
203                 empathy_run_until_ready (tp_group);
204                 empathy_tp_group_get_invitation (tp_group, &contact);
205                 empathy_contact_run_until_ready (contact,
206                                                  EMPATHY_CONTACT_READY_NAME,
207                                                  NULL);
208
209                 msg = g_strdup_printf (_("Incoming call from %s"),
210                                        empathy_contact_get_name (contact));
211
212                 event_manager_add (manager, contact, EMPATHY_IMAGE_VOIP, msg,
213                                    channel, event_channel_process_func, NULL);
214
215                 g_free (msg);
216                 g_object_unref (contact);
217                 g_object_unref (tp_group);
218         }
219         else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
220                 EmpathyContact        *contact;
221                 gchar                 *msg;
222                 TpHandle               handle;
223                 McAccount             *account;
224                 EmpathyContactFactory *factory;
225
226                 factory = empathy_contact_factory_new ();
227                 handle = tp_channel_get_handle (channel, NULL);
228                 account = empathy_channel_get_account (channel);
229
230                 contact = empathy_contact_factory_get_from_handle (factory,
231                                                                    account,
232                                                                    handle);
233
234                 empathy_contact_run_until_ready (contact,
235                         EMPATHY_CONTACT_READY_NAME, NULL);
236
237                 msg = g_strdup_printf (_("Incoming file transfer from %s"),
238                                        empathy_contact_get_name (contact));
239
240                 event_manager_add (manager, contact,
241                                    EMPATHY_IMAGE_DOCUMENT_SEND,
242                                    msg, channel,
243                                    event_channel_process_func, NULL);
244
245                 g_object_unref (factory);
246                 g_object_unref (account);
247         }
248
249         g_free (channel_type);
250 }
251
252 static void
253 event_manager_dispatch_channel_cb (EmpathyDispatcher   *dispatcher,
254                                    TpChannel           *channel,
255                                    EmpathyEventManager *manager)
256 {
257         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
258         GSList                  *l;
259
260         for (l = priv->events; l; l = l->next) {
261                 EventPriv *event = l->data;
262
263                 if (event->channel &&
264                     empathy_proxy_equal (channel, event->channel)) {
265                         event_remove (event);
266                         break;
267                 }
268         }
269 }
270
271 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
272                               "you don't have the needed external " \
273                               "application to handle it.")
274
275 static void
276 event_tube_process_func (EventPriv *event)
277 {
278         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
279         EmpathyDispatcherTube   *tube = (EmpathyDispatcherTube*) event->user_data;
280
281         if (tube->activatable) {
282                 empathy_dispatcher_tube_process (priv->dispatcher, tube);
283         } else {
284                 GtkWidget *dialog;
285                 gchar     *str;
286
287                 /* Tell the user that the tube can't be handled */
288                 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
289                                        empathy_contact_get_name (tube->initiator));
290
291                 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
292                                                  GTK_MESSAGE_ERROR,
293                                                  GTK_BUTTONS_OK,
294                                                  "%s", str);
295                 gtk_window_set_title (GTK_WINDOW (dialog),
296                                       _("Invitation Error"));
297                 g_free (str);
298
299                 gtk_widget_show (dialog);
300                 g_signal_connect (dialog, "response",
301                                   G_CALLBACK (gtk_widget_destroy),
302                                   NULL);
303         }
304
305         empathy_dispatcher_tube_unref (tube);
306         event_remove (event);
307 }
308
309 static void
310 event_manager_filter_tube_cb (EmpathyDispatcher     *dispatcher,
311                               EmpathyDispatcherTube *tube,
312                               EmpathyEventManager   *manager)
313 {
314         const gchar *icon_name;
315         gchar       *msg;
316
317         empathy_contact_run_until_ready (tube->initiator,
318                                          EMPATHY_CONTACT_READY_NAME, NULL);
319
320         if (tube->activatable) {
321                 icon_name = GTK_STOCK_EXECUTE;
322                 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
323                                          "application will be started to handle it."),
324                                        empathy_contact_get_name (tube->initiator));
325         } else {
326                 icon_name = GTK_STOCK_DIALOG_ERROR;
327                 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
328                                        empathy_contact_get_name (tube->initiator));
329         }
330
331         event_manager_add (manager, tube->initiator, icon_name, msg,
332                            tube->channel, event_tube_process_func,
333                            empathy_dispatcher_tube_ref (tube));
334
335         g_free (msg);
336 }
337
338 static void
339 event_pending_subscribe_func (EventPriv *event)
340 {
341         empathy_subscription_dialog_show (event->public.contact, NULL);
342         event_remove (event);
343 }
344
345 static void
346 event_manager_pendings_changed_cb (EmpathyContactList  *list,
347                                    EmpathyContact      *contact,
348                                    EmpathyContact      *actor,
349                                    guint                reason,
350                                    gchar               *message,
351                                    gboolean             is_pending,
352                                    EmpathyEventManager *manager)
353 {
354         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
355         GString                 *str;
356
357         if (!is_pending) {
358                 GSList *l;
359
360                 for (l = priv->events; l; l = l->next) {
361                         EventPriv *event = l->data;
362
363                         if (event->public.contact == contact &&
364                             event->func == event_pending_subscribe_func) {
365                                 event_remove (event);
366                                 break;
367                         }
368                 }
369
370                 return;
371         }
372
373         empathy_contact_run_until_ready (contact,
374                                          EMPATHY_CONTACT_READY_NAME,
375                                          NULL);
376
377         str = g_string_new (NULL);
378         g_string_printf (str, _("Subscription requested by %s"),
379                          empathy_contact_get_name (contact));   
380         if (!G_STR_EMPTY (message)) {
381                 g_string_append_printf (str, _("\nMessage: %s"), message);
382         }
383
384         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
385                            NULL, event_pending_subscribe_func, NULL);
386
387         g_string_free (str, TRUE);
388 }
389
390 static void
391 event_manager_finalize (GObject *object)
392 {
393         EmpathyEventManagerPriv *priv = GET_PRIV (object);
394
395         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
396         g_slist_free (priv->events);
397         g_object_unref (priv->contact_manager);
398         g_object_unref (priv->dispatcher);
399 }
400
401 static void
402 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
403 {
404         GObjectClass *object_class = G_OBJECT_CLASS (klass);
405
406         object_class->finalize = event_manager_finalize;
407
408         signals[EVENT_ADDED] =
409                 g_signal_new ("event-added",
410                               G_TYPE_FROM_CLASS (klass),
411                               G_SIGNAL_RUN_LAST,
412                               0,
413                               NULL, NULL,
414                               g_cclosure_marshal_VOID__POINTER,
415                               G_TYPE_NONE,
416                               1, G_TYPE_POINTER);
417
418         signals[EVENT_REMOVED] =
419                 g_signal_new ("event-removed",
420                               G_TYPE_FROM_CLASS (klass),
421                               G_SIGNAL_RUN_LAST,
422                               0,
423                               NULL, NULL,
424                               g_cclosure_marshal_VOID__POINTER,
425                               G_TYPE_NONE,
426                               1, G_TYPE_POINTER);
427
428         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
429 }
430
431 static void
432 empathy_event_manager_init (EmpathyEventManager *manager)
433 {
434         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
435                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
436
437         manager->priv = priv;
438
439         priv->dispatcher = empathy_dispatcher_new ();
440         priv->contact_manager = empathy_contact_manager_new ();
441         g_signal_connect (priv->dispatcher, "filter-channel",
442                           G_CALLBACK (event_manager_filter_channel_cb),
443                           manager);
444         g_signal_connect (priv->dispatcher, "dispatch-channel",
445                           G_CALLBACK (event_manager_dispatch_channel_cb),
446                           manager);
447         g_signal_connect (priv->dispatcher, "filter-tube",
448                           G_CALLBACK (event_manager_filter_tube_cb),
449                           manager);
450         g_signal_connect (priv->contact_manager, "pendings-changed",
451                           G_CALLBACK (event_manager_pendings_changed_cb),
452                           manager);
453 }
454
455 EmpathyEventManager *
456 empathy_event_manager_new (void)
457 {
458         static EmpathyEventManager *manager = NULL;
459
460         if (!manager) {
461                 manager = g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
462                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
463         } else {
464                 g_object_ref (manager);
465         }
466
467         return manager;
468 }
469
470 GSList *
471 empathy_event_manager_get_events (EmpathyEventManager *manager)
472 {
473         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
474
475         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
476
477         return priv->events;
478 }
479
480 EmpathyEvent *
481 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
482 {
483         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
484
485         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
486
487         return priv->events ? priv->events->data : NULL;
488 }
489
490 void
491 empathy_event_activate (EmpathyEvent *event_public)
492 {
493         EventPriv *event = (EventPriv*) event_public;
494
495         g_return_if_fail (event_public != NULL);
496
497         if (event->func) {
498                 event->func (event);
499         } else {
500                 event_remove (event);
501         }
502 }
503