]> git.0d.be Git - empathy.git/blob - libempathy/empathy-chatroom-manager.c
Add empathy_tp_chat_get_connection() to direct access the channel's connection.
[empathy.git] / libempathy / empathy-chatroom-manager.c
1 /*
2  * Copyright (C) 2004-2007 Imendio AB
3  * Copyright (C) 2007-2009 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  *          Martyn Russell <martyn@imendio.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #include "empathy-tp-chat.h"
34 #include "empathy-chatroom-manager.h"
35 #include "empathy-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
39
40 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
41 #define CHATROOMS_DTD_FILENAME "empathy-chatroom-manager.dtd"
42 #define SAVE_TIMER 4
43
44 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
45
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
47 typedef struct
48 {
49   GList *chatrooms;
50   gchar *file;
51   /* source id of the autosave timer */
52   gint save_timer_id;
53 } EmpathyChatroomManagerPriv;
54
55 enum {
56   CHATROOM_ADDED,
57   CHATROOM_REMOVED,
58   LAST_SIGNAL
59 };
60
61 static guint signals[LAST_SIGNAL];
62
63 /* properties */
64 enum
65 {
66   PROP_FILE = 1,
67   LAST_PROPERTY
68 };
69
70 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
71
72 /*
73  * API to save/load and parse the chatrooms file.
74  */
75
76 static gboolean
77 chatroom_manager_file_save (EmpathyChatroomManager *manager)
78 {
79         EmpathyChatroomManagerPriv *priv;
80         xmlDocPtr                  doc;
81         xmlNodePtr                 root;
82         GList                     *l;
83
84         priv = GET_PRIV (manager);
85
86         doc = xmlNewDoc ("1.0");
87         root = xmlNewNode (NULL, "chatrooms");
88         xmlDocSetRootElement (doc, root);
89
90         for (l = priv->chatrooms; l; l = l->next) {
91                 EmpathyChatroom *chatroom;
92                 xmlNodePtr       node;
93                 const gchar     *account_id;
94                 gboolean         favorite;
95
96                 chatroom = l->data;
97
98                 g_object_get (chatroom, "favorite", &favorite, NULL);
99                 if (!favorite) {
100                         continue;
101                 }
102
103                 account_id = mc_account_get_unique_name (empathy_chatroom_get_account (chatroom));
104
105                 node = xmlNewChild (root, NULL, "chatroom", NULL);
106                 xmlNewTextChild (node, NULL, "name", empathy_chatroom_get_name (chatroom));
107                 xmlNewTextChild (node, NULL, "room", empathy_chatroom_get_room (chatroom));
108                 xmlNewTextChild (node, NULL, "account", account_id);
109                 xmlNewTextChild (node, NULL, "auto_connect",
110                         empathy_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
111         }
112
113         /* Make sure the XML is indented properly */
114         xmlIndentTreeOutput = 1;
115
116         DEBUG ("Saving file:'%s'", priv->file);
117         xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
118         xmlFreeDoc (doc);
119
120         xmlCleanupParser ();
121         xmlMemoryDump ();
122
123         return TRUE;
124 }
125
126 static gboolean
127 save_timeout (EmpathyChatroomManager *self)
128 {
129   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
130
131   priv->save_timer_id = 0;
132   chatroom_manager_file_save (self);
133
134   return FALSE;
135 }
136
137 static void
138 reset_save_timeout (EmpathyChatroomManager *self)
139 {
140   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
141
142   if (priv->save_timer_id > 0)
143     {
144       g_source_remove (priv->save_timer_id);
145     }
146
147   priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
148       (GSourceFunc) save_timeout, self);
149 }
150
151 static void
152 chatroom_changed_cb (EmpathyChatroom *chatroom,
153                      GParamSpec *spec,
154                      EmpathyChatroomManager *self)
155 {
156   reset_save_timeout (self);
157 }
158
159 static void
160 add_chatroom (EmpathyChatroomManager *self,
161               EmpathyChatroom *chatroom)
162 {
163   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
164
165   priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
166
167   g_signal_connect (chatroom, "notify",
168       G_CALLBACK (chatroom_changed_cb), self);
169 }
170
171 static void
172 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
173                                  xmlNodePtr             node)
174 {
175         EmpathyChatroomManagerPriv *priv;
176         EmpathyChatroom            *chatroom;
177         McAccount                 *account;
178         xmlNodePtr                 child;
179         gchar                     *str;
180         gchar                     *name;
181         gchar                     *room;
182         gchar                     *account_id;
183         gboolean                   auto_connect;
184
185         priv = GET_PRIV (manager);
186
187         /* default values. */
188         name = NULL;
189         room = NULL;
190         auto_connect = TRUE;
191         account_id = NULL;
192
193         for (child = node->children; child; child = child->next) {
194                 gchar *tag;
195
196                 if (xmlNodeIsText (child)) {
197                         continue;
198                 }
199
200                 tag = (gchar *) child->name;
201                 str = (gchar *) xmlNodeGetContent (child);
202
203                 if (strcmp (tag, "name") == 0) {
204                         name = g_strdup (str);
205                 }
206                 else if (strcmp (tag, "room") == 0) {
207                         room = g_strdup (str);
208                 }
209                 else if (strcmp (tag, "auto_connect") == 0) {
210                         if (strcmp (str, "yes") == 0) {
211                                 auto_connect = TRUE;
212                         } else {
213                                 auto_connect = FALSE;
214                         }
215                 }
216                 else if (strcmp (tag, "account") == 0) {
217                         account_id = g_strdup (str);
218                 }
219
220                 xmlFree (str);
221         }
222
223         account = mc_account_lookup (account_id);
224         if (!account) {
225                 g_free (name);
226                 g_free (room);
227                 g_free (account_id);
228                 return;
229         }
230
231         chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
232         g_object_set (chatroom, "favorite", TRUE, NULL);
233         add_chatroom (manager, chatroom);
234         g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
235
236         g_object_unref (account);
237         g_free (name);
238         g_free (room);
239         g_free (account_id);
240 }
241
242 static gboolean
243 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
244                              const gchar           *filename)
245 {
246         EmpathyChatroomManagerPriv *priv;
247         xmlParserCtxtPtr           ctxt;
248         xmlDocPtr                  doc;
249         xmlNodePtr                 chatrooms;
250         xmlNodePtr                 node;
251
252         priv = GET_PRIV (manager);
253
254         DEBUG ("Attempting to parse file:'%s'...", filename);
255
256         ctxt = xmlNewParserCtxt ();
257
258         /* Parse and validate the file. */
259         doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
260         if (!doc) {
261                 g_warning ("Failed to parse file:'%s'", filename);
262                 xmlFreeParserCtxt (ctxt);
263                 return FALSE;
264         }
265
266         if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
267                 g_warning ("Failed to validate file:'%s'", filename);
268                 xmlFreeDoc(doc);
269                 xmlFreeParserCtxt (ctxt);
270                 return FALSE;
271         }
272
273         /* The root node, chatrooms. */
274         chatrooms = xmlDocGetRootElement (doc);
275
276         for (node = chatrooms->children; node; node = node->next) {
277                 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
278                         chatroom_manager_parse_chatroom (manager, node);
279                 }
280         }
281
282         DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
283
284         xmlFreeDoc(doc);
285         xmlFreeParserCtxt (ctxt);
286
287         return TRUE;
288 }
289
290 static gboolean
291 chatroom_manager_get_all (EmpathyChatroomManager *manager)
292 {
293         EmpathyChatroomManagerPriv *priv;
294
295         priv = GET_PRIV (manager);
296
297         /* read file in */
298         if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
299             !chatroom_manager_file_parse (manager, priv->file)) {
300                 return FALSE;
301         }
302
303         return TRUE;
304 }
305
306 static void
307 empathy_chatroom_manager_get_property (GObject *object,
308                                        guint property_id,
309                                        GValue *value,
310                                        GParamSpec *pspec)
311 {
312   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
313   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
314
315   switch (property_id)
316     {
317       case PROP_FILE:
318         g_value_set_string (value, priv->file);
319         break;
320       default:
321         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
322         break;
323     }
324 }
325
326 static void
327 empathy_chatroom_manager_set_property (GObject *object,
328                                        guint property_id,
329                                        const GValue *value,
330                                        GParamSpec *pspec)
331 {
332   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
333   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
334
335   switch (property_id)
336     {
337       case PROP_FILE:
338         g_free (priv->file);
339         priv->file = g_value_dup_string (value);
340         break;
341       default:
342         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343         break;
344     }
345 }
346
347 static void
348 chatroom_manager_finalize (GObject *object)
349 {
350   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
351   EmpathyChatroomManagerPriv *priv;
352   GList *l;
353
354   priv = GET_PRIV (object);
355
356   if (priv->save_timer_id > 0)
357     {
358       /* have to save before destroy the object */
359       g_source_remove (priv->save_timer_id);
360       priv->save_timer_id = 0;
361       chatroom_manager_file_save (self);
362     }
363
364   for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
365     {
366       EmpathyChatroom *chatroom = l->data;
367
368       g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
369           self);
370
371       g_object_unref (chatroom);
372     }
373
374   g_list_free (priv->chatrooms);
375   g_free (priv->file);
376
377   (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
378 }
379
380 static GObject *
381 empathy_chatroom_manager_constructor (GType type,
382                                       guint n_props,
383                                       GObjectConstructParam *props)
384 {
385   GObject *obj;
386   EmpathyChatroomManager *self;
387   EmpathyChatroomManagerPriv *priv;
388
389   if (chatroom_manager_singleton != NULL)
390     return g_object_ref (chatroom_manager_singleton);
391
392   /* Parent constructor chain */
393   obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
394         constructor (type, n_props, props);
395
396   self = EMPATHY_CHATROOM_MANAGER (obj);
397   priv = GET_PRIV (self);
398
399   chatroom_manager_singleton = self;
400   g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
401
402   if (priv->file == NULL)
403     {
404       /* Set the default file path */
405       gchar *dir;
406
407       dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
408       if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
409         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
410
411       priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
412       g_free (dir);
413     }
414
415   chatroom_manager_get_all (self);
416   return obj;
417 }
418
419 static void
420 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
421 {
422   GObjectClass *object_class = G_OBJECT_CLASS (klass);
423   GParamSpec *param_spec;
424
425   object_class->constructor = empathy_chatroom_manager_constructor;
426   object_class->get_property = empathy_chatroom_manager_get_property;
427   object_class->set_property = empathy_chatroom_manager_set_property;
428         object_class->finalize = chatroom_manager_finalize;
429
430   param_spec = g_param_spec_string (
431       "file",
432       "path of the favorite file",
433       "The path of the XML file containing user's favorites",
434       NULL,
435       G_PARAM_CONSTRUCT_ONLY |
436       G_PARAM_READWRITE |
437       G_PARAM_STATIC_NAME |
438       G_PARAM_STATIC_NICK |
439       G_PARAM_STATIC_BLURB);
440   g_object_class_install_property (object_class, PROP_FILE, param_spec);
441
442   signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
443       G_TYPE_FROM_CLASS (klass),
444       G_SIGNAL_RUN_LAST,
445       0, NULL, NULL,
446       g_cclosure_marshal_VOID__OBJECT,
447       G_TYPE_NONE,
448       1, EMPATHY_TYPE_CHATROOM);
449
450   signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
451       G_TYPE_FROM_CLASS (klass),
452       G_SIGNAL_RUN_LAST,
453       0, NULL, NULL,
454       g_cclosure_marshal_VOID__OBJECT,
455       G_TYPE_NONE,
456       1, EMPATHY_TYPE_CHATROOM);
457
458   g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
459 }
460
461 static void
462 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
463 {
464   EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
465       EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
466
467   manager->priv = priv;
468 }
469
470 EmpathyChatroomManager *
471 empathy_chatroom_manager_dup_singleton (const gchar *file)
472 {
473   return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
474       "file", file, NULL));
475 }
476
477 gboolean
478 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
479                              EmpathyChatroom        *chatroom)
480 {
481   EmpathyChatroomManagerPriv *priv;
482
483   g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
484   g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
485
486   priv = GET_PRIV (manager);
487
488   /* don't add more than once */
489   if (!empathy_chatroom_manager_find (manager,
490       empathy_chatroom_get_account (chatroom),
491       empathy_chatroom_get_room (chatroom)))
492     {
493       gboolean favorite;
494
495       g_object_get (chatroom, "favorite", &favorite, NULL);
496       add_chatroom (manager, chatroom);
497
498       if (favorite)
499         reset_save_timeout (manager);
500
501       g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
502       return TRUE;
503     }
504
505   return FALSE;
506 }
507
508 void
509 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
510                                  EmpathyChatroom        *chatroom)
511 {
512   EmpathyChatroomManagerPriv *priv;
513   GList *l;
514
515   g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
516   g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
517
518   priv = GET_PRIV (manager);
519
520   for (l = priv->chatrooms; l; l = l->next)
521     {
522       EmpathyChatroom *this_chatroom;
523
524       this_chatroom = l->data;
525
526       if (this_chatroom == chatroom ||
527           empathy_chatroom_equal (chatroom, this_chatroom))
528         {
529           gboolean favorite;
530
531           priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
532           g_object_get (chatroom, "favorite", &favorite, NULL);
533           if (favorite)
534             reset_save_timeout (manager);
535
536           g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, this_chatroom);
537           g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
538               manager);
539
540           g_object_unref (this_chatroom);
541           break;
542         }
543     }
544 }
545
546 EmpathyChatroom *
547 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
548                                McAccount *account,
549                                const gchar *room)
550 {
551         EmpathyChatroomManagerPriv *priv;
552         GList                     *l;
553
554         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
555         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
556         g_return_val_if_fail (room != NULL, NULL);
557
558         priv = GET_PRIV (manager);
559
560         for (l = priv->chatrooms; l; l = l->next) {
561                 EmpathyChatroom *chatroom;
562                 McAccount      *this_account;
563                 const gchar    *this_room;
564
565                 chatroom = l->data;
566                 this_account = empathy_chatroom_get_account (chatroom);
567                 this_room = empathy_chatroom_get_room (chatroom);
568
569                 if (this_account && this_room &&
570                     empathy_account_equal (account, this_account) &&
571                     strcmp (this_room, room) == 0) {
572                         return chatroom;
573                 }
574         }
575
576         return NULL;
577 }
578
579 GList *
580 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
581                                        McAccount             *account)
582 {
583         EmpathyChatroomManagerPriv *priv;
584         GList                     *chatrooms, *l;
585
586         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
587
588         priv = GET_PRIV (manager);
589
590         if (!account) {
591                 return g_list_copy (priv->chatrooms);
592         }
593
594         chatrooms = NULL;
595         for (l = priv->chatrooms; l; l = l->next) {
596                 EmpathyChatroom *chatroom;
597
598                 chatroom = l->data;
599
600                 if (empathy_account_equal (account,
601                                           empathy_chatroom_get_account (chatroom))) {
602                         chatrooms = g_list_append (chatrooms, chatroom);
603                 }
604         }
605
606         return chatrooms;
607 }
608
609 guint
610 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
611                                    McAccount             *account)
612 {
613         EmpathyChatroomManagerPriv *priv;
614         GList                     *l;
615         guint                      count = 0;
616
617         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
618
619         priv = GET_PRIV (manager);
620
621         if (!account) {
622                 return g_list_length (priv->chatrooms);
623         }
624
625         for (l = priv->chatrooms; l; l = l->next) {
626                 EmpathyChatroom *chatroom;
627
628                 chatroom = l->data;
629
630                 if (empathy_account_equal (account,
631                                            empathy_chatroom_get_account (chatroom))) {
632                         count++;
633                 }
634         }
635
636         return count;
637 }
638
639 static void
640 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
641   gpointer user_data)
642 {
643   EmpathyChatroomManager *manager = EMPATHY_CHATROOM_MANAGER (user_data);
644   McAccount *account = empathy_tp_chat_get_account (chat);
645   EmpathyChatroom *chatroom;
646   const gchar *roomname;
647   gboolean favorite;
648
649   roomname = empathy_tp_chat_get_id (chat);
650   chatroom = empathy_chatroom_manager_find (manager, account, roomname);
651
652   if (chatroom == NULL)
653     return;
654
655   g_object_set (chatroom, "tp-chat", NULL, NULL);
656   g_object_get (chatroom, "favorite", &favorite, NULL);
657
658   if (!favorite)
659     {
660       /* Remove the chatroom from the list, unless it's in the list of
661        * favourites..
662        * FIXME this policy should probably not be in libempathy */
663       empathy_chatroom_manager_remove (manager, chatroom);
664     }
665 }
666
667 static void
668 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
669   EmpathyDispatchOperation *operation, gpointer user_data)
670 {
671   EmpathyChatroomManager *manager = EMPATHY_CHATROOM_MANAGER (user_data);
672   EmpathyChatroom *chatroom;
673   TpChannel *channel;
674   EmpathyTpChat *chat;
675   const gchar *roomname;
676   GQuark channel_type;
677   TpHandleType handle_type;
678   McAccount *account;
679
680   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
681
682   /* Observe Text channels to rooms only */
683   if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
684     return;
685
686   channel = empathy_dispatch_operation_get_channel (operation);
687   tp_channel_get_handle (channel, &handle_type);
688
689   if (handle_type != TP_HANDLE_TYPE_ROOM)
690     return;
691
692   chat = EMPATHY_TP_CHAT (
693     empathy_dispatch_operation_get_channel_wrapper (operation));
694   account = empathy_tp_chat_get_account (chat);
695
696   roomname = empathy_tp_chat_get_id (chat);
697
698   chatroom = empathy_chatroom_manager_find (manager, account, roomname);
699
700   if (chatroom == NULL)
701     {
702       chatroom = empathy_chatroom_new_full (account, roomname, roomname,
703         FALSE);
704       g_object_set (G_OBJECT (chatroom), "tp-chat", chat, NULL);
705       empathy_chatroom_manager_add (manager, chatroom);
706       g_object_unref (chatroom);
707     }
708   else
709     {
710       g_object_set (G_OBJECT (chatroom), "tp-chat", chat, NULL);
711     }
712
713   /* A TpChat is always destroyed as it only gets unreffed after the channel
714    * has been invalidated in the dispatcher..  */
715   g_signal_connect (chat, "destroy",
716     G_CALLBACK (chatroom_manager_chat_destroyed_cb),
717     manager);
718 }
719
720 void
721 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
722   EmpathyDispatcher *dispatcher)
723 {
724   g_signal_connect (dispatcher, "observe",
725     G_CALLBACK (chatroom_manager_observe_channel_cb), manager);
726 }