]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Enable approving of file transfers
[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   approval = event_manager_approval_new (manager, operation);
243   priv->approvals = g_slist_prepend (priv->approvals, approval);
244
245   approval->approved_handler = g_signal_connect (operation, "approved",
246     G_CALLBACK (event_manager_operation_approved_cb), approval);
247
248   approval->claimed_handler = g_signal_connect (operation, "claimed",
249      G_CALLBACK (event_manager_operation_claimed_cb), approval);
250
251   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
252     {
253       EmpathyTpChat *tp_chat =
254         EMPATHY_TP_CHAT (
255           empathy_dispatch_operation_get_channel_wrapper (operation));
256
257       g_signal_connect (tp_chat, "message-received",
258         G_CALLBACK (event_manager_chat_message_received_cb), approval);
259       g_object_unref (G_OBJECT (tp_chat));
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 #endif
286   else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER)) 
287     {
288       EmpathyContact        *contact;
289       gchar                 *msg;
290       TpHandle               handle;
291       McAccount             *account;
292       EmpathyContactFactory *factory;
293       TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
294
295       factory = empathy_contact_factory_dup_singleton ();
296       handle = tp_channel_get_handle (channel, NULL);
297       account = empathy_channel_get_account (channel);
298
299       contact = empathy_contact_factory_get_from_handle (factory, account,
300         handle);
301
302       empathy_contact_run_until_ready (contact,
303         EMPATHY_CONTACT_READY_NAME, NULL);
304
305       msg = g_strdup_printf (_("Incoming file transfer from %s"),
306         empathy_contact_get_name (contact));
307
308       event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
309         msg, approval, event_channel_process_func, NULL);
310
311       g_object_unref (channel);
312       g_object_unref (factory);
313       g_object_unref (account);
314     }
315   else
316     {
317       DEBUG ("Unknown channel type, ignoring..");
318     }
319 }
320
321 #if 0 /* FIXME dispatcher */
322
323 #define TUBE_NO_APP_MESSAGE _("%s is offering you an invitation, but " \
324                               "you don't have the needed external " \
325                               "application to handle it.")
326
327
328 static void
329 event_tube_process_func (EventPriv *event)
330 {
331         EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
332         EmpathyDispatcherTube   *tube = (EmpathyDispatcherTube*) event->user_data;
333
334         if (tube->activatable) {
335                 empathy_dispatcher_tube_process (priv->dispatcher, tube);
336         } else {
337                 GtkWidget *dialog;
338                 gchar     *str;
339
340                 /* Tell the user that the tube can't be handled */
341                 str = g_strdup_printf (TUBE_NO_APP_MESSAGE,
342                                        empathy_contact_get_name (tube->initiator));
343
344                 dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
345                                                  GTK_MESSAGE_ERROR,
346                                                  GTK_BUTTONS_OK,
347                                                  "%s", str);
348                 gtk_window_set_title (GTK_WINDOW (dialog),
349                                       _("Invitation Error"));
350                 g_free (str);
351
352                 gtk_widget_show (dialog);
353                 g_signal_connect (dialog, "response",
354                                   G_CALLBACK (gtk_widget_destroy),
355                                   NULL);
356         }
357
358         empathy_dispatcher_tube_unref (tube);
359         event_remove (event);
360 }
361
362 static void
363 event_manager_filter_tube_cb (EmpathyDispatcher     *dispatcher,
364                               EmpathyDispatcherTube *tube,
365                               EmpathyEventManager   *manager)
366 {
367         const gchar *icon_name;
368         gchar       *msg;
369
370         empathy_contact_run_until_ready (tube->initiator,
371                                          EMPATHY_CONTACT_READY_NAME, NULL);
372
373         if (tube->activatable) {
374                 icon_name = GTK_STOCK_EXECUTE;
375                 msg = g_strdup_printf (_("%s is offering you an invitation. An external "
376                                          "application will be started to handle it."),
377                                        empathy_contact_get_name (tube->initiator));
378         } else {
379                 icon_name = GTK_STOCK_DIALOG_ERROR;
380                 msg = g_strdup_printf (TUBE_NO_APP_MESSAGE,
381                                        empathy_contact_get_name (tube->initiator));
382         }
383
384         event_manager_add (manager, tube->initiator, icon_name, msg,
385                            tube->channel, event_tube_process_func,
386                            empathy_dispatcher_tube_ref (tube));
387
388         g_free (msg);
389 }
390 #endif
391
392 static void
393 event_pending_subscribe_func (EventPriv *event)
394 {
395         empathy_subscription_dialog_show (event->public.contact, NULL);
396         event_remove (event);
397 }
398
399 static void
400 event_manager_pendings_changed_cb (EmpathyContactList  *list,
401                                    EmpathyContact      *contact,
402                                    EmpathyContact      *actor,
403                                    guint                reason,
404                                    gchar               *message,
405                                    gboolean             is_pending,
406                                    EmpathyEventManager *manager)
407 {
408         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
409         GString                 *str;
410
411         if (!is_pending) {
412                 GSList *l;
413
414                 for (l = priv->events; l; l = l->next) {
415                         EventPriv *event = l->data;
416
417                         if (event->public.contact == contact &&
418                             event->func == event_pending_subscribe_func) {
419                                 event_remove (event);
420                                 break;
421                         }
422                 }
423
424                 return;
425         }
426
427         empathy_contact_run_until_ready (contact,
428                                          EMPATHY_CONTACT_READY_NAME,
429                                          NULL);
430
431         str = g_string_new (NULL);
432         g_string_printf (str, _("Subscription requested by %s"),
433                          empathy_contact_get_name (contact));   
434         if (!G_STR_EMPTY (message)) {
435                 g_string_append_printf (str, _("\nMessage: %s"), message);
436         }
437
438         event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
439                            NULL, event_pending_subscribe_func, NULL);
440
441         g_string_free (str, TRUE);
442 }
443
444 static GObject *
445 event_manager_constructor (GType type,
446                            guint n_props,
447                            GObjectConstructParam *props)
448 {
449         GObject *retval;
450
451         if (manager_singleton) {
452                 retval = g_object_ref (manager_singleton);
453         } else {
454                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
455                         (type, n_props, props);
456
457                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
458                 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
459         }
460
461         return retval;
462 }
463
464 static void
465 event_manager_finalize (GObject *object)
466 {
467         EmpathyEventManagerPriv *priv = GET_PRIV (object);
468
469         g_slist_foreach (priv->events, (GFunc) event_free, NULL);
470         g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
471         g_slist_free (priv->events);
472         g_object_unref (priv->contact_manager);
473         g_object_unref (priv->dispatcher);
474 }
475
476 static void
477 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
478 {
479         GObjectClass *object_class = G_OBJECT_CLASS (klass);
480
481         object_class->finalize = event_manager_finalize;
482         object_class->constructor = event_manager_constructor;
483
484         signals[EVENT_ADDED] =
485                 g_signal_new ("event-added",
486                               G_TYPE_FROM_CLASS (klass),
487                               G_SIGNAL_RUN_LAST,
488                               0,
489                               NULL, NULL,
490                               g_cclosure_marshal_VOID__POINTER,
491                               G_TYPE_NONE,
492                               1, G_TYPE_POINTER);
493
494         signals[EVENT_REMOVED] =
495                 g_signal_new ("event-removed",
496                               G_TYPE_FROM_CLASS (klass),
497                               G_SIGNAL_RUN_LAST,
498                               0,
499                               NULL, NULL,
500                               g_cclosure_marshal_VOID__POINTER,
501                               G_TYPE_NONE,
502                               1, G_TYPE_POINTER);
503
504         g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
505 }
506
507 static void
508 empathy_event_manager_init (EmpathyEventManager *manager)
509 {
510         EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
511                 EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
512
513         manager->priv = priv;
514
515         priv->dispatcher = empathy_get_dispatcher ();
516         priv->contact_manager = empathy_contact_manager_dup_singleton ();
517         g_signal_connect (priv->dispatcher, "approve",
518                           G_CALLBACK (event_manager_approve_channel_cb),
519                           manager);
520         /*g_signal_connect (priv->dispatcher, "dispatch-channel",
521                           G_CALLBACK (event_manager_dispatch_channel_cb),
522                           manager);
523   */
524 #if 0 /* FIXME  dispatcher */
525         g_signal_connect (priv->dispatcher, "filter-tube",
526                           G_CALLBACK (event_manager_filter_tube_cb),
527                           manager);
528 #endif
529         g_signal_connect (priv->contact_manager, "pendings-changed",
530                           G_CALLBACK (event_manager_pendings_changed_cb),
531                           manager);
532 }
533
534 EmpathyEventManager *
535 empathy_event_manager_dup_singleton (void)
536 {
537         return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
538 }
539
540 GSList *
541 empathy_event_manager_get_events (EmpathyEventManager *manager)
542 {
543         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
544
545         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
546
547         return priv->events;
548 }
549
550 EmpathyEvent *
551 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
552 {
553         EmpathyEventManagerPriv *priv = GET_PRIV (manager);
554
555         g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
556
557         return priv->events ? priv->events->data : NULL;
558 }
559
560 void
561 empathy_event_activate (EmpathyEvent *event_public)
562 {
563         EventPriv *event = (EventPriv*) event_public;
564
565         g_return_if_fail (event_public != NULL);
566
567         if (event->func) {
568                 event->func (event);
569         } else {
570                 event_remove (event);
571         }
572 }
573