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