]> git.0d.be Git - empathy.git/blob - src/empathy-event-manager.c
Clean some coding style
[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  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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-factory.h>
31 #include <libempathy/empathy-contact-manager.h>
32 #include <libempathy/empathy-tp-chat.h>
33 #include <libempathy/empathy-tp-call.h>
34 #include <libempathy/empathy-tp-group.h>
35 #include <libempathy/empathy-utils.h>
36
37 #include <extensions/extensions.h>
38
39 #include <libempathy-gtk/empathy-images.h>
40 #include <libempathy-gtk/empathy-contact-dialogs.h>
41
42 #include "empathy-event-manager.h"
43 #include "empathy-tube-dispatch.h"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyEventManager)
49
50 typedef struct {
51   EmpathyEventManager *manager;
52   EmpathyDispatchOperation *operation;
53   gulong approved_handler;
54   gulong claimed_handler;
55   gulong invalidated_handler;
56   /* Remove contact if applicable */
57   EmpathyContact *contact;
58   /* Tube dispatcher if applicable */
59   EmpathyTubeDispatch *tube_dispatch;
60   /* option signal handler */
61   gulong handler;
62 } EventManagerApproval;
63
64 typedef struct {
65   EmpathyDispatcher *dispatcher;
66   EmpathyContactManager *contact_manager;
67   GSList *events;
68   /* Ongoing approvals */
69   GSList *approvals;
70 } EmpathyEventManagerPriv;
71
72 typedef struct _EventPriv EventPriv;
73 typedef void (*EventFunc) (EventPriv *event);
74
75 struct _EventPriv {
76   EmpathyEvent public;
77   EmpathyEventManager *manager;
78   EventManagerApproval *approval;
79   EventFunc func;
80   gpointer user_data;
81 };
82
83 enum {
84   EVENT_ADDED,
85   EVENT_REMOVED,
86   LAST_SIGNAL
87 };
88
89 static guint signals[LAST_SIGNAL];
90
91 G_DEFINE_TYPE (EmpathyEventManager, empathy_event_manager, G_TYPE_OBJECT);
92
93 static EmpathyEventManager * manager_singleton = NULL;
94
95 static EventManagerApproval *
96 event_manager_approval_new (EmpathyEventManager *manager,
97   EmpathyDispatchOperation *operation)
98 {
99   EventManagerApproval *result = g_slice_new0 (EventManagerApproval);
100   result->operation = g_object_ref (operation);
101   result->manager = manager;
102
103   return result;
104 }
105
106 static void
107 event_manager_approval_free (EventManagerApproval *approval)
108 {
109   g_signal_handler_disconnect (approval->operation,
110     approval->approved_handler);
111   g_signal_handler_disconnect (approval->operation,
112     approval->claimed_handler);
113   g_signal_handler_disconnect (approval->operation,
114     approval->invalidated_handler);
115   g_object_unref (approval->operation);
116
117   if (approval->contact != NULL)
118     g_object_unref (approval->contact);
119
120   if (approval->tube_dispatch != NULL)
121     g_object_unref (approval->tube_dispatch);
122
123   g_slice_free (EventManagerApproval, approval);
124 }
125
126 static void event_remove (EventPriv *event);
127
128 static void
129 event_free (EventPriv *event)
130 {
131   g_free (event->public.icon_name);
132   g_free (event->public.message);
133
134   if (event->public.contact)
135     {
136       g_object_unref (event->public.contact);
137     }
138
139   g_slice_free (EventPriv, event);
140 }
141
142 static void
143 event_remove (EventPriv *event)
144 {
145   EmpathyEventManagerPriv *priv = GET_PRIV (event->manager);
146
147   DEBUG ("Removing event %p", event);
148   priv->events = g_slist_remove (priv->events, event);
149   g_signal_emit (event->manager, signals[EVENT_REMOVED], 0, event);
150   event_free (event);
151 }
152
153 static void
154 event_manager_add (EmpathyEventManager *manager, EmpathyContact *contact,
155   const gchar *icon_name, const gchar *message, EventManagerApproval *approval,
156   EventFunc func, gpointer user_data)
157 {
158   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
159   EventPriv               *event;
160
161   event = g_slice_new0 (EventPriv);
162   event->public.contact = contact ? g_object_ref (contact) : NULL;
163   event->public.icon_name = g_strdup (icon_name);
164   event->public.message = g_strdup (message);
165   event->func = func;
166   event->user_data = user_data;
167   event->manager = manager;
168   event->approval = approval;
169
170   DEBUG ("Adding event %p", event);
171   priv->events = g_slist_prepend (priv->events, event);
172   g_signal_emit (event->manager, signals[EVENT_ADDED], 0, event);
173 }
174
175 static void
176 event_channel_process_func (EventPriv *event)
177 {
178   empathy_dispatch_operation_approve (event->approval->operation);
179 }
180
181 static void
182 event_manager_chat_message_received_cb (EmpathyTpChat *tp_chat,
183   EmpathyMessage *message, EventManagerApproval *approval)
184 {
185   EmpathyContact  *sender;
186   gchar           *msg;
187   TpChannel       *channel;
188
189   g_signal_handlers_disconnect_by_func (tp_chat,
190     event_manager_chat_message_received_cb, approval);
191
192   sender = empathy_message_get_sender (message);
193   msg = g_strdup_printf (_("New message from %s:\n%s"),
194     empathy_contact_get_name (sender), empathy_message_get_body (message));
195
196   channel = empathy_tp_chat_get_channel (tp_chat);
197   event_manager_add (approval->manager, sender, EMPATHY_IMAGE_NEW_MESSAGE, msg,
198     approval, event_channel_process_func, NULL);
199
200   g_free (msg);
201 }
202
203 static void
204 event_manager_approval_done (EventManagerApproval *approval)
205 {
206   EmpathyEventManagerPriv *priv = GET_PRIV (approval->manager);
207   GSList                  *l;
208
209   priv->approvals = g_slist_remove (priv->approvals, approval);
210
211   for (l = priv->events; l; l = l->next)
212     {
213       EventPriv *event = l->data;
214
215       if (event->approval == approval)
216         {
217           event_remove (event);
218           break;
219         }
220     }
221
222   event_manager_approval_free (approval);
223 }
224
225 static void
226 event_manager_operation_approved_cb (EmpathyDispatchOperation *operation,
227   EventManagerApproval *approval)
228 {
229   event_manager_approval_done (approval);
230 }
231
232 static void
233 event_manager_operation_claimed_cb (EmpathyDispatchOperation *operation,
234   EventManagerApproval *approval)
235 {
236   event_manager_approval_done (approval);
237 }
238
239 static void
240 event_manager_operation_invalidated_cb (EmpathyDispatchOperation *operation,
241   guint domain, gint code, gchar *message,
242   EventManagerApproval *approval)
243 {
244   event_manager_approval_done (approval);
245 }
246
247 static void
248 event_manager_media_channel_got_name_cb (EmpathyContact *contact,
249   const GError *error, gpointer user_data, GObject *object)
250 {
251   EventManagerApproval *approval = user_data;
252   gchar *msg;
253
254   if (error != NULL)
255     {
256       /* FIXME just returning assuming the operation will be invalidated as
257        * well */
258       return;
259     }
260
261   msg = g_strdup_printf (_("Incoming call from %s"),
262     empathy_contact_get_name (contact));
263
264   event_manager_add (approval->manager,
265     approval->contact, EMPATHY_IMAGE_VOIP, msg,
266     approval, event_channel_process_func, NULL);
267
268   g_free (msg);
269 }
270
271 static void
272 event_manager_media_channel_got_contact (EventManagerApproval *approval)
273 {
274   empathy_contact_call_when_ready (approval->contact,
275      EMPATHY_CONTACT_READY_NAME, event_manager_media_channel_got_name_cb,
276         approval, NULL, G_OBJECT (approval->manager));
277 }
278
279 static void
280 event_manager_media_channel_contact_changed_cb (EmpathyTpCall *call,
281   GParamSpec *param, EventManagerApproval *approval)
282 {
283   EmpathyContact *contact;
284
285   g_object_get (G_OBJECT (call), "contact", &contact, NULL);
286
287   if (contact == NULL)
288     return;
289
290   approval->contact = contact;
291   event_manager_media_channel_got_contact (approval);
292 }
293
294 static void
295 event_manager_tube_approved_cb (EventPriv *event)
296 {
297   empathy_tube_dispatch_handle (event->approval->tube_dispatch);
298 }
299
300 static void
301 event_manager_add_tube_approval (EventManagerApproval *approval,
302   EmpathyTubeDispatchAbility ability)
303 {
304   const gchar *icon_name;
305   gchar       *msg;
306
307   if (ability == EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE)
308     {
309       icon_name = GTK_STOCK_EXECUTE;
310       msg = g_strdup_printf (_("%s is offering you an invitation."
311         " An external application will be started to handle it."),
312       empathy_contact_get_name (approval->contact));
313     }
314   else
315     {
316       icon_name = GTK_STOCK_DIALOG_ERROR;
317       msg = g_strdup_printf (_("%s is offering you an invitation, but "
318         "you don't have the needed external "
319         "application to handle it."),
320         empathy_contact_get_name (approval->contact));
321     }
322
323   event_manager_add (approval->manager, approval->contact, icon_name, msg,
324     approval, event_manager_tube_approved_cb, approval);
325
326   g_free (msg);
327 }
328
329 static void
330 event_manager_tube_dispatch_ability_cb (GObject *object,
331    GParamSpec *spec, gpointer user_data)
332 {
333   EventManagerApproval *approval = (EventManagerApproval *)user_data;
334   EmpathyTubeDispatchAbility dispatchability;
335
336   dispatchability =
337     empathy_tube_dispatch_is_dispatchable (approval->tube_dispatch);
338
339   if (dispatchability != EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN)
340     {
341       event_manager_add_tube_approval (approval, dispatchability);
342       g_signal_handler_disconnect (object, approval->handler);
343       approval->handler = 0;
344     }
345 }
346
347 static void
348 event_manager_tube_got_contact_name_cb (EmpathyContact *contact,
349   const GError *error, gpointer user_data, GObject *object)
350 {
351   EventManagerApproval *approval = (EventManagerApproval *)user_data;
352   EmpathyTubeDispatchAbility dispatchability;
353
354   if (error != NULL)
355     {
356       /* FIXME?, we assume that the operation gets invalidated as well (if it
357        * didn't already */
358        return;
359     }
360
361   dispatchability = empathy_tube_dispatch_is_dispatchable
362     (approval->tube_dispatch);
363
364
365   switch (dispatchability)
366     {
367       case EMPATHY_TUBE_DISPATCHABILITY_UNKNOWN:
368         approval->handler = g_signal_connect (approval->tube_dispatch,
369           "notify::dispatchability",
370           G_CALLBACK (event_manager_tube_dispatch_ability_cb), approval);
371         break;
372       case EMPATHY_TUBE_DISPATCHABILITY_POSSIBLE:
373         /* fallthrough */
374       case EMPATHY_TUBE_DISPATCHABILITY_IMPOSSIBLE:
375         event_manager_add_tube_approval (approval, dispatchability);
376         break;
377     }
378 }
379
380 static void
381 event_manager_approve_channel_cb (EmpathyDispatcher *dispatcher,
382   EmpathyDispatchOperation  *operation, EmpathyEventManager *manager)
383 {
384   const gchar *channel_type;
385   EventManagerApproval *approval;
386   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
387
388   channel_type = empathy_dispatch_operation_get_channel_type (operation);
389
390   approval = event_manager_approval_new (manager, operation);
391   priv->approvals = g_slist_prepend (priv->approvals, approval);
392
393   approval->approved_handler = g_signal_connect (operation, "approved",
394     G_CALLBACK (event_manager_operation_approved_cb), approval);
395
396   approval->claimed_handler = g_signal_connect (operation, "claimed",
397      G_CALLBACK (event_manager_operation_claimed_cb), approval);
398
399   approval->invalidated_handler = g_signal_connect (operation, "invalidated",
400      G_CALLBACK (event_manager_operation_invalidated_cb), approval);
401
402   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT))
403     {
404       EmpathyTpChat *tp_chat =
405         EMPATHY_TP_CHAT (
406           empathy_dispatch_operation_get_channel_wrapper (operation));
407
408       g_signal_connect (tp_chat, "message-received",
409         G_CALLBACK (event_manager_chat_message_received_cb), approval);
410
411     }
412   else if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA))
413     {
414       EmpathyContact *contact;
415       EmpathyTpCall *call = EMPATHY_TP_CALL (
416           empathy_dispatch_operation_get_channel_wrapper (operation));
417
418       g_object_get (G_OBJECT (call), "contact", &contact, NULL);
419
420       if (contact == NULL)
421         {
422           g_signal_connect (call, "notify::contact",
423             G_CALLBACK (event_manager_media_channel_contact_changed_cb),
424             approval);
425         }
426       else
427         {
428           approval->contact = contact;
429           event_manager_media_channel_got_contact (approval);
430         }
431
432     }
433   else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
434     {
435       EmpathyContact        *contact;
436       gchar                 *msg;
437       TpHandle               handle;
438       McAccount             *account;
439       EmpathyContactFactory *factory;
440       TpChannel *channel = empathy_dispatch_operation_get_channel (operation);
441
442       factory = empathy_contact_factory_dup_singleton ();
443       handle = tp_channel_get_handle (channel, NULL);
444       account = empathy_channel_get_account (channel);
445
446       contact = empathy_contact_factory_get_from_handle (factory, account,
447         handle);
448
449       empathy_contact_run_until_ready (contact,
450         EMPATHY_CONTACT_READY_NAME, NULL);
451
452       msg = g_strdup_printf (_("Incoming file transfer from %s"),
453         empathy_contact_get_name (contact));
454
455       event_manager_add (manager, contact, EMPATHY_IMAGE_DOCUMENT_SEND,
456         msg, approval, event_channel_process_func, NULL);
457
458       g_object_unref (factory);
459       g_object_unref (account);
460     }
461   else if (!tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_STREAM_TUBE) ||
462       !tp_strdiff (channel_type, EMP_IFACE_CHANNEL_TYPE_DBUS_TUBE))
463     {
464       EmpathyContact        *contact;
465       TpHandle               handle;
466       TpHandleType           handle_type;
467       McAccount             *account;
468       EmpathyContactFactory *factory;
469       EmpathyTubeDispatch *tube_dispatch;
470       TpChannel *channel;
471
472       channel = empathy_dispatch_operation_get_channel (operation);
473
474       handle = tp_channel_get_handle (channel, &handle_type);
475
476       /* Only understand p2p tubes */
477       if (handle_type != TP_HANDLE_TYPE_CONTACT)
478         return;
479
480       factory = empathy_contact_factory_dup_singleton ();
481       account = empathy_channel_get_account (channel);
482
483       contact = empathy_contact_factory_get_from_handle (factory, account,
484         handle);
485
486       tube_dispatch = empathy_tube_dispatch_new (operation);
487
488       approval->contact = contact;
489       approval->tube_dispatch = tube_dispatch;
490
491       empathy_contact_call_when_ready (contact,
492         EMPATHY_CONTACT_READY_NAME, event_manager_tube_got_contact_name_cb,
493         approval, NULL, G_OBJECT (manager));
494
495       g_object_unref (factory);
496       g_object_unref (account);
497     }
498   else
499     {
500       DEBUG ("Unknown channel type, ignoring..");
501     }
502 }
503
504 static void
505 event_pending_subscribe_func (EventPriv *event)
506 {
507   empathy_subscription_dialog_show (event->public.contact, NULL);
508   event_remove (event);
509 }
510
511 static void
512 event_manager_pendings_changed_cb (EmpathyContactList  *list,
513   EmpathyContact *contact, EmpathyContact *actor,
514   guint reason, gchar *message, gboolean is_pending,
515   EmpathyEventManager *manager)
516 {
517   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
518   GString                 *str;
519
520   if (!is_pending)
521     {
522       GSList *l;
523
524       for (l = priv->events; l; l = l->next)
525         {
526           EventPriv *event = l->data;
527
528       if (event->public.contact == contact &&
529           event->func == event_pending_subscribe_func)
530         {
531           event_remove (event);
532           break;
533         }
534       }
535
536       return;
537     }
538
539   empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_NAME, NULL);
540
541   str = g_string_new (NULL);
542   g_string_printf (str, _("Subscription requested by %s"),
543     empathy_contact_get_name (contact));
544
545   if (!G_STR_EMPTY (message))
546     g_string_append_printf (str, _("\nMessage: %s"), message);
547
548   event_manager_add (manager, contact, GTK_STOCK_DIALOG_QUESTION, str->str,
549     NULL, event_pending_subscribe_func, NULL);
550
551   g_string_free (str, TRUE);
552 }
553
554 static GObject *
555 event_manager_constructor (GType type,
556                            guint n_props,
557                            GObjectConstructParam *props)
558 {
559         GObject *retval;
560
561         if (manager_singleton) {
562                 retval = g_object_ref (manager_singleton);
563         } else {
564                 retval = G_OBJECT_CLASS (empathy_event_manager_parent_class)->constructor
565                         (type, n_props, props);
566
567                 manager_singleton = EMPATHY_EVENT_MANAGER (retval);
568                 g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
569         }
570
571         return retval;
572 }
573
574 static void
575 event_manager_finalize (GObject *object)
576 {
577   EmpathyEventManagerPriv *priv = GET_PRIV (object);
578
579   g_slist_foreach (priv->events, (GFunc) event_free, NULL);
580   g_slist_free (priv->events);
581   g_slist_foreach (priv->approvals, (GFunc) event_manager_approval_free, NULL);
582   g_slist_free (priv->approvals);
583   g_object_unref (priv->contact_manager);
584   g_object_unref (priv->dispatcher);
585 }
586
587 static void
588 empathy_event_manager_class_init (EmpathyEventManagerClass *klass)
589 {
590   GObjectClass *object_class = G_OBJECT_CLASS (klass);
591
592   object_class->finalize = event_manager_finalize;
593   object_class->constructor = event_manager_constructor;
594
595   signals[EVENT_ADDED] =
596     g_signal_new ("event-added",
597       G_TYPE_FROM_CLASS (klass),
598       G_SIGNAL_RUN_LAST,
599       0,
600       NULL, NULL,
601       g_cclosure_marshal_VOID__POINTER,
602       G_TYPE_NONE,
603       1, G_TYPE_POINTER);
604
605   signals[EVENT_REMOVED] =
606   g_signal_new ("event-removed",
607       G_TYPE_FROM_CLASS (klass),
608       G_SIGNAL_RUN_LAST,
609       0,
610       NULL, NULL,
611       g_cclosure_marshal_VOID__POINTER,
612       G_TYPE_NONE, 1, G_TYPE_POINTER);
613
614   g_type_class_add_private (object_class, sizeof (EmpathyEventManagerPriv));
615 }
616
617 static void
618 empathy_event_manager_init (EmpathyEventManager *manager)
619 {
620   EmpathyEventManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
621     EMPATHY_TYPE_EVENT_MANAGER, EmpathyEventManagerPriv);
622
623   manager->priv = priv;
624
625   priv->dispatcher = empathy_dispatcher_dup_singleton ();
626   priv->contact_manager = empathy_contact_manager_dup_singleton ();
627   g_signal_connect (priv->dispatcher, "approve",
628     G_CALLBACK (event_manager_approve_channel_cb), manager);
629   g_signal_connect (priv->contact_manager, "pendings-changed",
630     G_CALLBACK (event_manager_pendings_changed_cb), manager);
631 }
632
633 EmpathyEventManager *
634 empathy_event_manager_dup_singleton (void)
635 {
636   return g_object_new (EMPATHY_TYPE_EVENT_MANAGER, NULL);
637 }
638
639 GSList *
640 empathy_event_manager_get_events (EmpathyEventManager *manager)
641 {
642   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
643
644   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
645
646   return priv->events;
647 }
648
649 EmpathyEvent *
650 empathy_event_manager_get_top_event (EmpathyEventManager *manager)
651 {
652   EmpathyEventManagerPriv *priv = GET_PRIV (manager);
653
654   g_return_val_if_fail (EMPATHY_IS_EVENT_MANAGER (manager), NULL);
655
656   return priv->events ? priv->events->data : NULL;
657 }
658
659 void
660 empathy_event_activate (EmpathyEvent *event_public)
661 {
662   EventPriv *event = (EventPriv*) event_public;
663
664   g_return_if_fail (event_public != NULL);
665
666   if (event->func)
667     event->func (event);
668   else
669     event_remove (event);
670 }