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