]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
72417328b10129047634cf9a2c4cdf3b5b6b3bda
[empathy.git] / src / empathy-event-manager.c
1 /*
2  * Copyright (C) 2007-2008 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <glib/gi18n.h>
25
26 #include <telepathy-glib/util.h>
27
28 #include <libempathy/empathy-dispatcher.h>
29 #include <libempathy/empathy-contact-factory.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 <extensions/extensions.h>
36
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
47 typedef struct {
48         EmpathyEventManager *manager;
49         EmpathyDispatchOperation *operation;
50         guint approved_handler;
51         guint claimed_handler;
52 } EventManagerApproval;
53
54 typedef struct {
55         EmpathyDispatcher     *dispatcher;
56         EmpathyContactManager *contact_manager;
57         GSList                *events;
58         /* Ongoing approvals */
59         GSList                *approvals;
60 } EmpathyEventManagerPriv;
61
62 typedef struct _EventPriv EventPriv;
63 typedef void (*EventFunc) (EventPriv *event);
64
65 struct _EventPriv {
66         EmpathyEvent         public;
67         EmpathyEventManager *manager;
68         EventManagerApproval *approval;
69         EventFunc            func;
70         gpointer             user_data;
71 };
72
73 enum {
74         EVENT_ADDED,
75         EVENT_REMOVED,
76         LAST_SIGNAL
77 };
78
79 static guint signals[LAST_SIGNAL];
80
81 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
82
83 static EmpathyEventManager * manager_singleton = NULL;
84
85 static EventManagerApproval *
86 event_manager_approval_new (EmpathyEventManager *manager,
87         EmpathyDispatchOperation *operation)
88 {
89         EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
90         result->operation = g_object_ref (operation);
91         result->manager = manager;
92
93         return result;
94 }
95
96 static void
97 event_manager_approval_free (EventManagerApproval *approval)
98 {
99   g_signal_handler_disconnect (approval->operation,
100     approval->approved_handler);
101   g_signal_handler_disconnect (approval->operation,
102     approval->claimed_handler);
103   g_object_unref (approval->operation);
104   g_slice_free (EventManagerApproval, approval);
105 }
106
107 static void event_remove (EventPriv *event);
108
109 static void
110 event_free (EventPriv *event)
111 {
112         g_free (event->public.icon_name);
113         g_free (event->public.message);
114
115         if (event->public.contact) {
116                 g_object_unref (event->public.contact);
117         }
118
119         g_slice_free (EventPriv, event);
120 }
121
122 static void
123 event_remove (EventPriv *event)
124 {
125         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
126
127         DEBUG ("Removing event %p", event);
128         priv->events = g_slist_remove (priv->events, event);
129         g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
130         event_free (event);
131 }
132
133 static void
134 event_manager_add (EmpathyEventManager *manager,
135                    EmpathyContact      *contact,
136                    const gchar         *icon_name,
137                    const gchar         *message,
138                    EventManagerApproval *approval,
139                    EventFunc            func,
140                    gpointer             user_data)
141 {
142         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
143         EventPriv               *event;
144
145         event = g_slice_new0 (EventPriv);
146         event->public.contact = contact ? g_object_ref (contact) : NULL;
147         event->public.icon_name = g_strdup (icon_name);
148         event->public.message = g_strdup (message);
149         event->func = func;
150         event->user_data = user_data;
151         event->manager = manager;
152
153         if (approval) {
154                 event->approval = approval;
155 #if 0 /* FIXME */
156                 g_signal_connect_swapped (channel, "invalidated",
157                                           G_CALLBACK (event_remove),
158                                           event);
159 #endif
160         }
161
162         DEBUG ("Adding event %p", event);
163         priv->events = g_slist_prepend (priv->events, event);
164         g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
165 }
166
167 static void
168 event_channel_process_func (EventPriv *event)
169 {
170         empathy_dispatch_operation_approve (event->approval->operation);
171 }
172
173 static void
174 event_manager_chat_message_received_cb (EmpathyTpChat       *tp_chat,
175                                         EmpathyMessage      *message,
176                                         EventManagerApproval *approval)
177 {
178         EmpathyContact  *sender;
179         gchar           *msg;
180         TpChannel       *channel;
181
182         g_signal_handlers_disconnect_by_func (tp_chat,
183                                               event_manager_chat_message_received_cb,
184                                               approval);
185
186         sender = empathy_message_get_sender (message);
187         msg = g_strdup_printf (_("New message from %s:\n%s"),
188                                empathy_contact_get_name (sender),
189                                empathy_message_get_body (message));
190
191         channel = empathy_tp_chat_get_channel (tp_chat);
192         event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
193                            approval, event_channel_process_func, NULL);
194
195         g_free (msg);
196 }
197
198 static void
199 event_manager_approval_done (EventManagerApproval *approval)
200 {
201   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
202   GSList                  *l;
203
204   priv->approvals = g_slist_remove (priv->approvals, approval);
205
206   for (l = priv->events; l; l = l->next) {
207     EventPriv *event = l->data;
208
209     if (event->approval == approval) {
210       event_remove (event);
211       break;
212     }
213   }
214
215   event_manager_approval_free (approval);
216 }
217
218 static void
219 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
220   EventManagerApproval *approval)
221 {
222   event_manager_approval_done (approval);
223 }
224
225 static void
226 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
227   EventManagerApproval *approval)
228 {
229   event_manager_approval_done (approval);
230 }
231
232 static void
233 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
234   EmpathyDispatchOperation  *operation, EmpathyEventManager *manager)
235 {
236   const gchar *channel_type;
237   EventManagerApproval *approval;
238   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
239
240   channel_type = empathy_dispatch_operation_get_channel_type (operation);
241
242   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
243     {
244       EmpathyTpChat *tp_chat =
245         EMPATHY_TP_CHAT (
246           empathy_dispatch_operation_get_channel_wrapper (operation));
247
248       approval = event_manager_approval_new (manager, operation);
249       priv->approvals = g_slist_prepend (priv->approvals, approval);
250
251       g_signal_connect (tp_chat, "message-received",
252         G_CALLBACK (event_manager_chat_message_received_cb), approval);
253       g_object_unref (G_OBJECT (tp_chat));
254
255       approval->approved_handler = g_signal_connect (operation, "approved",
256         G_CALLBACK (event_manager_operation_approved_cb), approval);
257
258       approval->claimed_handler = g_signal_connect (operation, "claimed",
259         G_CALLBACK (event_manager_operation_claimed_cb), approval);
260
261     }
262 #if 0
263         else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA)) {
264                 EmpathyTpGroup *tp_group;
265                 EmpathyContact *contact;
266                 gchar          *msg;
267
268                 tp_group = empathy_tp_group_new (channel);
269                 empathy_run_until_ready (tp_group);
270                 empathy_tp_group_get_invitation (tp_group, &contact);
271                 empathy_contact_run_until_ready (contact,
272                                                  EMPATHY_CONTACT_READY_NAME,
273                                                  NULL);
274
275                 msg = g_strdup_printf (_("Incoming call from %s"),
276                                        empathy_contact_get_name (contact));
277
278                 event_manager_add (manager, contact, EMPATHY_IMAGE_VOIP, msg,
279                                    channel, event_channel_process_func, NULL);
280
281                 g_free (msg);
282                 g_object_unref (contact);
283                 g_object_unref (tp_group);
284         }
285         else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) {
286                 EmpathyContact        *contact;
287                 gchar                 *msg;
288                 TpHandle               handle;
289                 McAccount             *account;
290                 EmpathyContactFactory *factory;
291
292                 factory = empathy_contact_factory_dup_singleton ();
293                 handle = tp_channel_get_handle (channel, NULL);
294                 account = empathy_channel_get_account (channel);
295
296                 contact = empathy_contact_factory_get_from_handle (factory,
297                                                                    account,
298                                                                    handle);
299
300                 empathy_contact_run_until_ready (contact,
301                         EMPATHY_CONTACT_READY_NAME, NULL);
302
303                 msg = g_strdup_printf (_("Incoming file transfer from %s"),
304                                        empathy_contact_get_name (contact));
305
306                 event_manager_add (manager, contact,
307                                    EMPATHY_IMAGE_DOCUMENT_SEND,
308                                    msg, channel,
309                                    event_channel_process_func, NULL);
310
311                 g_object_unref (factory);
312                 g_object_unref (account);
313         }
314
315         g_free (channel_type);
316 #endif
317 }
318
319 #if 0 /* FIXME dispatcher */
320
321 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
322                               "you don't have the needed external " \
323                               "application to handle it.")
324
325
326 static void
327 event_tube_process_func (EventPriv *event)
328 {
329         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
330         EmpathyDispatcherTube   *tube = (EmpathyDispatcherTube*) event->user_data;
331
332         if (tube->activatable) {
333                 empathy_dispatcher_tube_process (priv->dispatcher, tube);
334         } else {
335                 GtkWidget *dialog;
336                 gchar     *str;
337
338                 /* Tell the user that the tube can't be handled */
339                 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
340                                        empathy_contact_get_name (tube->initiator));
341
342                 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
343                                                  GTK_MESSAGE_ERROR,
344                                                  GTK_BUTTONS_OK,
345                                                  "%s", str);
346                 gtk_window_set_title (GTK_WINDOW (dialog),
347                                       _("Invitation Error"));
348                 g_free (str);
349
350                 gtk_widget_show (dialog);
351                 g_signal_connect (dialog, "response",
352                                   G_CALLBACK (gtk_widget_destroy),
353                                   NULL);
354         }
355
356         empathy_dispatcher_tube_unref (tube);
357         event_remove (event);
358 }
359
360 static void
361 event_manager_filter_tube_cb (EmpathyDispatcher     *dispatcher,
362                               EmpathyDispatcherTube *tube,
363                               EmpathyEventManager   *manager)
364 {
365         const gchar *icon_name;
366         gchar       *msg;
367
368         empathy_contact_run_until_ready (tube->initiator,
369                                          EMPATHY_CONTACT_READY_NAME, NULL);
370
371         if (tube->activatable) {
372                 icon_name = GTK_STOCK_EXECUTE;
373                 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
374                                          "application will be started to handle it."),
375                                        empathy_contact_get_name (tube->initiator));
376         } else {
377                 icon_name = GTK_STOCK_DIALOG_ERROR;
378                 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
379                                        empathy_contact_get_name (tube->initiator));
380         }
381
382         event_manager_add (manager, tube->initiator, icon_name, msg,
383                            tube->channel, event_tube_process_func,
384                            empathy_dispatcher_tube_ref (tube));
385
386         g_free (msg);
387 }
388 #endif
389
390 static void
391 event_pending_subscribe_func (EventPriv *event)
392 {
393         empathy_subscription_dialog_show (event->public.contact, NULL);
394         event_remove (event);
395 }
396
397 static void
398 event_manager_pendings_changed_cb (EmpathyContactList  *list,
399                                    EmpathyContact      *contact,
400                                    EmpathyContact      *actor,
401                                    guint                reason,
402                                    gchar               *message,
403                                    gboolean             is_pending,
404                                    EmpathyEventManager *manager)
405 {
406         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
407         GString                 *str;
408
409         if (!is_pending) {
410                 GSList *l;
411
412                 for (l = priv->events; l; l = l->next) {
413                         EventPriv *event = l->data;
414
415                         if (event->public.contact == contact &&
416                             event->func == event_pending_subscribe_func) {
417                                 event_remove (event);
418                                 break;
419                         }
420                 }
421
422                 return;
423         }
424
425         empathy_contact_run_until_ready (contact,
426                                          EMPATHY_CONTACT_READY_NAME,
427                                          NULL);
428
429         str = g_string_new (NULL);
430         g_string_printf (str, _("Subscription requested by %s"),
431                          empathy_contact_get_name (contact));   
432         if (!G_STR_EMPTY (message)) {
433                 g_string_append_printf (str, _("\nMessage: %s"), message);
434         }
435
436         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
437                            NULL, event_pending_subscribe_func, NULL);
438
439         g_string_free (str, TRUE);
440 }
441
442 static GObject *
443 event_manager_constructor (GType type,
444                            guint n_props,
445                            GObjectConstructParam *props)
446 {
447         GObject *retval;
448
449         if (manager_singleton) {
450                 retval = g_object_ref (manager_singleton);
451         } else {
452                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
453                         (type, n_props, props);
454
455                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
456                 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
457         }
458
459         return retval;
460 }
461
462 static void
463 event_manager_finalize (GObject *object)
464 {
465         EmpathyEventManagerPriv *priv = GET_PRIV (object);
466
467         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
468         g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
469         g_slist_free (priv->events);
470         g_object_unref (priv->contact_manager);
471         g_object_unref (priv->dispatcher);
472 }
473
474 static void
475 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
476 {
477         GObjectClass *object_class = G_OBJECT_CLASS (klass);
478
479         object_class->finalize = event_manager_finalize;
480         object_class->constructor = event_manager_constructor;
481
482         signals[EVENT_ADDED] =
483                 g_signal_new ("event-added",
484                               G_TYPE_FROM_CLASS (klass),
485                               G_SIGNAL_RUN_LAST,
486                               0,
487                               NULL, NULL,
488                               g_cclosure_marshal_VOID__POINTER,
489                               G_TYPE_NONE,
490                               1, G_TYPE_POINTER);
491
492         signals[EVENT_REMOVED] =
493                 g_signal_new ("event-removed",
494                               G_TYPE_FROM_CLASS (klass),
495                               G_SIGNAL_RUN_LAST,
496                               0,
497                               NULL, NULL,
498                               g_cclosure_marshal_VOID__POINTER,
499                               G_TYPE_NONE,
500                               1, G_TYPE_POINTER);
501
502         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
503 }
504
505 static void
506 empathy_event_manager_init (EmpathyEventManager *manager)
507 {
508         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
509                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
510
511         manager->priv = priv;
512
513         priv->dispatcher = empathy_get_dispatcher ();
514         priv->contact_manager = empathy_contact_manager_dup_singleton ();
515         g_signal_connect (priv->dispatcher, "approve",
516                           G_CALLBACK (event_manager_approve_channel_cb),
517                           manager);
518         /*g_signal_connect (priv->dispatcher, "dispatch-channel",
519                           G_CALLBACK (event_manager_dispatch_channel_cb),
520                           manager);
521   */
522 #if 0 /* FIXME  dispatcher */
523         g_signal_connect (priv->dispatcher, "filter-tube",
524                           G_CALLBACK (event_manager_filter_tube_cb),
525                           manager);
526 #endif
527         g_signal_connect (priv->contact_manager, "pendings-changed",
528                           G_CALLBACK (event_manager_pendings_changed_cb),
529                           manager);
530 }
531
532 EmpathyEventManager *
533 empathy_event_manager_dup_singleton (void)
534 {
535         return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
536 }
537
538 GSList *
539 empathy_event_manager_get_events (EmpathyEventManager *manager)
540 {
541         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
542
543         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
544
545         return priv->events;
546 }
547
548 EmpathyEvent *
549 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
550 {
551         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
552
553         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
554
555         return priv->events ? priv->events->data : NULL;
556 }
557
558 void
559 empathy_event_activate (EmpathyEvent *event_public)
560 {
561         EventPriv *event = (EventPriv*) event_public;
562
563         g_return_if_fail (event_public != NULL);
564
565         if (event->func) {
566                 event->func (event);
567         } else {
568                 event_remove (event);
569         }
570 }
571