]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-event-manager.c
e6ddb1808d0d29c226f50a08415264e2bc271deb
[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         GString *str;
322
323         if (!is_pending) {
324                 /* FIXME: remove event if any */
325                 return;
326         }
327
328         empathy_contact_run_until_ready (contact,
329                                          EMPATHY_CONTACT_READY_NAME,
330                                          NULL);
331
332         str = g_string_new (NULL);
333         g_string_printf (str, _("Subscription requested by %s"),
334                          empathy_contact_get_name (contact));   
335         if (!G_STR_EMPTY (message)) {
336                 g_string_append_printf (str, _("\nMessage: %s"), message);
337         }
338
339         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
340                            NULL, event_pending_subscribe_func, NULL);
341
342         g_string_free (str, TRUE);
343 }
344
345 static void
346 event_manager_finalize (GObject *object)
347 {
348         EmpathyEventManagerPriv *priv = GET_PRIV (object);
349
350         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
351         g_slist_free (priv->events);
352         g_object_unref (priv->contact_manager);
353         g_object_unref (priv->dispatcher);
354 }
355
356 static void
357 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
358 {
359         GObjectClass *object_class = G_OBJECT_CLASS (klass);
360
361         object_class->finalize = event_manager_finalize;
362
363         signals[EVENT_ADDED] =
364                 g_signal_new ("event-added",
365                               G_TYPE_FROM_CLASS (klass),
366                               G_SIGNAL_RUN_LAST,
367                               0,
368                               NULL, NULL,
369                               g_cclosure_marshal_VOID__POINTER,
370                               G_TYPE_NONE,
371                               1, G_TYPE_POINTER);
372
373         signals[EVENT_REMOVED] =
374                 g_signal_new ("event-removed",
375                               G_TYPE_FROM_CLASS (klass),
376                               G_SIGNAL_RUN_LAST,
377                               0,
378                               NULL, NULL,
379                               g_cclosure_marshal_VOID__POINTER,
380                               G_TYPE_NONE,
381                               1, G_TYPE_POINTER);
382
383         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
384 }
385
386 static void
387 empathy_event_manager_init (EmpathyEventManager *manager)
388 {
389         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
390                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
391
392         manager->priv = priv;
393
394         priv->dispatcher = empathy_dispatcher_new ();
395         priv->contact_manager = empathy_contact_manager_new ();
396         g_signal_connect (priv->dispatcher, "filter-channel",
397                           G_CALLBACK (event_manager_filter_channel_cb),
398                           manager);
399         g_signal_connect (priv->dispatcher, "dispatch-channel",
400                           G_CALLBACK (event_manager_dispatch_channel_cb),
401                           manager);
402         g_signal_connect (priv->dispatcher, "filter-tube",
403                           G_CALLBACK (event_manager_filter_tube_cb),
404                           manager);
405         g_signal_connect (priv->contact_manager, "pendings-changed",
406                           G_CALLBACK (event_manager_pendings_changed_cb),
407                           manager);
408 }
409
410 EmpathyEventManager *
411 empathy_event_manager_new (void)
412 {
413         static EmpathyEventManager *manager = NULL;
414
415         if (!manager) {
416                 manager = g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
417                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
418         } else {
419                 g_object_ref (manager);
420         }
421
422         return manager;
423 }
424
425 GSList *
426 empathy_event_manager_get_events (EmpathyEventManager *manager)
427 {
428         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
429
430         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
431
432         return priv->events;
433 }
434
435 EmpathyEvent *
436 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
437 {
438         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
439
440         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
441
442         return priv->events ? priv->events->data : NULL;
443 }
444
445 void
446 empathy_event_activate (EmpathyEvent *event_public)
447 {
448         EventPriv *event = (EventPriv*) event_public;
449
450         g_return_if_fail (event_public != NULL);
451
452         if (event->func) {
453                 event->func (event);
454         } else {
455                 event_remove (event);
456         }
457 }
458