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