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