]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Removed account property of EmpathyTpFile as it's only actually used once and easy...
[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 file_channel_state_cb (TpProxy      *proxy,
193                        const GValue *state,
194                        const GError *error,
195                        gpointer      user_data,
196                        GObject      *weak_object)
197 {
198         TpChannel           *channel = (TpChannel *) weak_object;
199         EmpathyEventManager *manager = (EmpathyEventManager *) user_data;
200
201         if (error) {
202                 DEBUG ("Error: %s", error->message);
203                 return;
204         }
205
206         /* Only deal with incoming channels */
207         if (g_value_get_uint (state) ==
208             EMP_FILE_TRANSFER_STATE_LOCAL_PENDING) {
209                 EmpathyContact *contact;
210                 gchar          *msg;
211                 EmpathyTpFile  *tp_file;
212
213                 tp_file = empathy_tp_file_new (channel);
214
215                 contact = empathy_tp_file_get_contact (tp_file);
216                 empathy_contact_run_until_ready (contact,
217                         EMPATHY_CONTACT_READY_NAME, NULL);
218
219                 msg = g_strdup_printf (_("Incoming file transfer from %s"),
220                                        empathy_contact_get_name (contact));
221
222                 event_manager_add (manager, contact,
223                                    EMPATHY_IMAGE_DOCUMENT_SEND,
224                                    msg,
225                                    channel,
226                                    event_channel_file_process_func,
227                                    tp_file);
228
229                 g_free (msg);
230         }
231 }
232
233 static void
234 event_manager_filter_channel_cb (EmpathyDispatcher   *dispatcher,
235                                  TpChannel           *channel,
236                                  EmpathyEventManager *manager)
237 {
238         gchar *channel_type;
239
240         g_object_get (channel, "channel-type", &channel_type, NULL);
241         if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)) {
242                 EmpathyTpChat *tp_chat;
243
244                 tp_chat = empathy_tp_chat_new (channel);
245                 g_signal_connect (tp_chat, "message-received",
246                                   G_CALLBACK (event_manager_chat_message_received_cb),
247                                   manager);
248         }
249         else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
250                 EmpathyTpGroup *tp_group;
251                 EmpathyContact *contact;
252                 gchar          *msg;
253
254                 tp_group = empathy_tp_group_new (channel);
255                 empathy_run_until_ready (tp_group);
256                 empathy_tp_group_get_invitation (tp_group, &contact);
257                 empathy_contact_run_until_ready (contact,
258                                                  EMPATHY_CONTACT_READY_NAME,
259                                                  NULL);
260
261                 msg = g_strdup_printf (_("Incoming call from %s"),
262                                        empathy_contact_get_name (contact));
263
264                 event_manager_add (manager, contact, EMPATHY_IMAGE_VOIP, msg,
265                                    channel, event_channel_process_func, NULL);
266
267                 g_free (msg);
268                 g_object_unref (contact);
269                 g_object_unref (tp_group);
270         }
271         else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE)) {
272                 tp_cli_dbus_properties_call_get (channel, -1,
273                                                  EMP_IFACE_CHANNEL_TYPE_FILE,
274                                                  "State",
275                                                  file_channel_state_cb,
276                                                  manager, NULL,
277                                                  G_OBJECT (channel));
278         }
279
280         g_free (channel_type);
281 }
282
283 static void
284 event_manager_dispatch_channel_cb (EmpathyDispatcher   *dispatcher,
285                                    TpChannel           *channel,
286                                    EmpathyEventManager *manager)
287 {
288         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
289         GSList                  *l;
290
291         for (l = priv->events; l; l = l->next) {
292                 EventPriv *event = l->data;
293
294                 if (event->channel &&
295                     empathy_proxy_equal (channel, event->channel)) {
296                         event_remove (event);
297                         break;
298                 }
299         }
300 }
301
302 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
303                               "you don't have the needed external " \
304                               "application to handle it.")
305
306 static void
307 event_tube_process_func (EventPriv *event)
308 {
309         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
310         EmpathyDispatcherTube   *tube = (EmpathyDispatcherTube*) event->user_data;
311
312         if (tube->activatable) {
313                 empathy_dispatcher_tube_process (priv->dispatcher, tube);
314         } else {
315                 GtkWidget *dialog;
316                 gchar     *str;
317
318                 /* Tell the user that the tube can't be handled */
319                 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
320                                        empathy_contact_get_name (tube->initiator));
321
322                 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
323                                                  GTK_MESSAGE_ERROR,
324                                                  GTK_BUTTONS_OK,
325                                                  "%s", str);
326                 gtk_window_set_title (GTK_WINDOW (dialog),
327                                       _("Invitation Error"));
328                 g_free (str);
329
330                 gtk_widget_show (dialog);
331                 g_signal_connect (dialog, "response",
332                                   G_CALLBACK (gtk_widget_destroy),
333                                   NULL);
334         }
335
336         empathy_dispatcher_tube_unref (tube);
337         event_remove (event);
338 }
339
340 static void
341 event_manager_filter_tube_cb (EmpathyDispatcher     *dispatcher,
342                               EmpathyDispatcherTube *tube,
343                               EmpathyEventManager   *manager)
344 {
345         const gchar *icon_name;
346         gchar       *msg;
347
348         empathy_contact_run_until_ready (tube->initiator,
349                                          EMPATHY_CONTACT_READY_NAME, NULL);
350
351         if (tube->activatable) {
352                 icon_name = GTK_STOCK_EXECUTE;
353                 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
354                                          "application will be started to handle it."),
355                                        empathy_contact_get_name (tube->initiator));
356         } else {
357                 icon_name = GTK_STOCK_DIALOG_ERROR;
358                 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
359                                        empathy_contact_get_name (tube->initiator));
360         }
361
362         event_manager_add (manager, tube->initiator, icon_name, msg,
363                            tube->channel, event_tube_process_func,
364                            empathy_dispatcher_tube_ref (tube));
365
366         g_free (msg);
367 }
368
369 static void
370 event_pending_subscribe_func (EventPriv *event)
371 {
372         empathy_subscription_dialog_show (event->public.contact, NULL);
373         event_remove (event);
374 }
375
376 static void
377 event_manager_pendings_changed_cb (EmpathyContactList  *list,
378                                    EmpathyContact      *contact,
379                                    EmpathyContact      *actor,
380                                    guint                reason,
381                                    gchar               *message,
382                                    gboolean             is_pending,
383                                    EmpathyEventManager *manager)
384 {
385         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
386         GString                 *str;
387
388         if (!is_pending) {
389                 GSList *l;
390
391                 for (l = priv->events; l; l = l->next) {
392                         EventPriv *event = l->data;
393
394                         if (event->public.contact == contact &&
395                             event->func == event_pending_subscribe_func) {
396                                 event_remove (event);
397                                 break;
398                         }
399                 }
400
401                 return;
402         }
403
404         empathy_contact_run_until_ready (contact,
405                                          EMPATHY_CONTACT_READY_NAME,
406                                          NULL);
407
408         str = g_string_new (NULL);
409         g_string_printf (str, _("Subscription requested by %s"),
410                          empathy_contact_get_name (contact));   
411         if (!G_STR_EMPTY (message)) {
412                 g_string_append_printf (str, _("\nMessage: %s"), message);
413         }
414
415         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
416                            NULL, event_pending_subscribe_func, NULL);
417
418         g_string_free (str, TRUE);
419 }
420
421 static void
422 event_manager_finalize (GObject *object)
423 {
424         EmpathyEventManagerPriv *priv = GET_PRIV (object);
425
426         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
427         g_slist_free (priv->events);
428         g_object_unref (priv->contact_manager);
429         g_object_unref (priv->dispatcher);
430 }
431
432 static void
433 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
434 {
435         GObjectClass *object_class = G_OBJECT_CLASS (klass);
436
437         object_class->finalize = event_manager_finalize;
438
439         signals[EVENT_ADDED] =
440                 g_signal_new ("event-added",
441                               G_TYPE_FROM_CLASS (klass),
442                               G_SIGNAL_RUN_LAST,
443                               0,
444                               NULL, NULL,
445                               g_cclosure_marshal_VOID__POINTER,
446                               G_TYPE_NONE,
447                               1, G_TYPE_POINTER);
448
449         signals[EVENT_REMOVED] =
450                 g_signal_new ("event-removed",
451                               G_TYPE_FROM_CLASS (klass),
452                               G_SIGNAL_RUN_LAST,
453                               0,
454                               NULL, NULL,
455                               g_cclosure_marshal_VOID__POINTER,
456                               G_TYPE_NONE,
457                               1, G_TYPE_POINTER);
458
459         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
460 }
461
462 static void
463 empathy_event_manager_init (EmpathyEventManager *manager)
464 {
465         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
466                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
467
468         manager->priv = priv;
469
470         priv->dispatcher = empathy_dispatcher_new ();
471         priv->contact_manager = empathy_contact_manager_new ();
472         g_signal_connect (priv->dispatcher, "filter-channel",
473                           G_CALLBACK (event_manager_filter_channel_cb),
474                           manager);
475         g_signal_connect (priv->dispatcher, "dispatch-channel",
476                           G_CALLBACK (event_manager_dispatch_channel_cb),
477                           manager);
478         g_signal_connect (priv->dispatcher, "filter-tube",
479                           G_CALLBACK (event_manager_filter_tube_cb),
480                           manager);
481         g_signal_connect (priv->contact_manager, "pendings-changed",
482                           G_CALLBACK (event_manager_pendings_changed_cb),
483                           manager);
484 }
485
486 EmpathyEventManager *
487 empathy_event_manager_new (void)
488 {
489         static EmpathyEventManager *manager = NULL;
490
491         if (!manager) {
492                 manager = g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
493                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
494         } else {
495                 g_object_ref (manager);
496         }
497
498         return manager;
499 }
500
501 GSList *
502 empathy_event_manager_get_events (EmpathyEventManager *manager)
503 {
504         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
505
506         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
507
508         return priv->events;
509 }
510
511 EmpathyEvent *
512 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
513 {
514         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
515
516         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
517
518         return priv->events ? priv->events->data : NULL;
519 }
520
521 void
522 empathy_event_activate (EmpathyEvent *event_public)
523 {
524         EventPriv *event = (EventPriv*) event_public;
525
526         g_return_if_fail (event_public != NULL);
527
528         if (event->func) {
529                 event->func (event);
530         } else {
531                 event_remove (event);
532         }
533 }
534