]> git.0d.be Git - empathy.git/blob - libempathy/empathy-chatroom-manager.c
chatroom-manager: add_chatroom: connect the 'notify' signal instead of each signal...
[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 (empathy_chatroom_equal (chatroom, this_chatroom)) {
355         gboolean favorite;
356                         priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
357
358       g_object_get (chatroom, "favorite", &favorite, NULL);
359
360       if (favorite)
361         {
362           reset_save_timeout (manager);
363         }
364
365                         g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, this_chatroom);
366
367       g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
368           manager);
369
370                         g_object_unref (this_chatroom);
371                         break;
372                 }
373         }
374 }
375
376 EmpathyChatroom *
377 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
378                               McAccount             *account,
379                               const gchar           *room)
380 {
381         EmpathyChatroomManagerPriv *priv;
382         GList                     *l;
383
384         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
385         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
386         g_return_val_if_fail (room != NULL, NULL);
387
388         priv = GET_PRIV (manager);
389
390         for (l = priv->chatrooms; l; l = l->next) {
391                 EmpathyChatroom *chatroom;
392                 McAccount      *this_account;
393                 const gchar    *this_room;
394
395                 chatroom = l->data;
396                 this_account = empathy_chatroom_get_account (chatroom);
397                 this_room = empathy_chatroom_get_room (chatroom);
398
399                 if (this_account && this_room &&
400                     empathy_account_equal (account, this_account) &&
401                     strcmp (this_room, room) == 0) {
402                         return chatroom;
403                 }
404         }
405
406         return NULL;
407 }
408
409 GList *
410 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
411                                        McAccount             *account)
412 {
413         EmpathyChatroomManagerPriv *priv;
414         GList                     *chatrooms, *l;
415
416         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
417
418         priv = GET_PRIV (manager);
419
420         if (!account) {
421                 return g_list_copy (priv->chatrooms);
422         }
423
424         chatrooms = NULL;
425         for (l = priv->chatrooms; l; l = l->next) {
426                 EmpathyChatroom *chatroom;
427
428                 chatroom = l->data;
429
430                 if (empathy_account_equal (account,
431                                           empathy_chatroom_get_account (chatroom))) {
432                         chatrooms = g_list_append (chatrooms, chatroom);
433                 }
434         }
435
436         return chatrooms;
437 }
438
439 guint
440 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
441                                    McAccount             *account)
442 {
443         EmpathyChatroomManagerPriv *priv;
444         GList                     *l;
445         guint                      count = 0;
446
447         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
448
449         priv = GET_PRIV (manager);
450
451         if (!account) {
452                 return g_list_length (priv->chatrooms);
453         }
454
455         for (l = priv->chatrooms; l; l = l->next) {
456                 EmpathyChatroom *chatroom;
457
458                 chatroom = l->data;
459
460                 if (empathy_account_equal (account,
461                                           empathy_chatroom_get_account (chatroom))) {
462                         count++;
463                 }
464         }
465
466         return count;
467 }
468
469 /*
470  * API to save/load and parse the chatrooms file.
471  */
472
473 static gboolean
474 chatroom_manager_get_all (EmpathyChatroomManager *manager)
475 {
476         EmpathyChatroomManagerPriv *priv;
477
478         priv = GET_PRIV (manager);
479
480         /* read file in */
481         if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
482             !chatroom_manager_file_parse (manager, priv->file))
483     return FALSE;
484
485         return TRUE;
486 }
487
488 static gboolean
489 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
490                              const gchar           *filename)
491 {
492         EmpathyChatroomManagerPriv *priv;
493         xmlParserCtxtPtr           ctxt;
494         xmlDocPtr                  doc;
495         xmlNodePtr                 chatrooms;
496         xmlNodePtr                 node;
497
498         priv = GET_PRIV (manager);
499
500         DEBUG ("Attempting to parse file:'%s'...", filename);
501
502         ctxt = xmlNewParserCtxt ();
503
504         /* Parse and validate the file. */
505         doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
506         if (!doc) {
507                 g_warning ("Failed to parse file:'%s'", filename);
508                 xmlFreeParserCtxt (ctxt);
509                 return FALSE;
510         }
511
512         if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
513                 g_warning ("Failed to validate file:'%s'", filename);
514                 xmlFreeDoc(doc);
515                 xmlFreeParserCtxt (ctxt);
516                 return FALSE;
517         }
518
519         /* The root node, chatrooms. */
520         chatrooms = xmlDocGetRootElement (doc);
521
522         for (node = chatrooms->children; node; node = node->next) {
523                 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
524                         chatroom_manager_parse_chatroom (manager, node);
525                 }
526         }
527
528         DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
529
530         xmlFreeDoc(doc);
531         xmlFreeParserCtxt (ctxt);
532
533         return TRUE;
534 }
535
536 static void
537 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
538                                  xmlNodePtr             node)
539 {
540         EmpathyChatroomManagerPriv *priv;
541         EmpathyChatroom            *chatroom;
542         McAccount                 *account;
543         xmlNodePtr                 child;
544         gchar                     *str;
545         gchar                     *name;
546         gchar                     *room;
547         gchar                     *account_id;
548         gboolean                   auto_connect;
549
550         priv = GET_PRIV (manager);
551
552         /* default values. */
553         name = NULL;
554         room = NULL;
555         auto_connect = TRUE;
556         account_id = NULL;
557
558         for (child = node->children; child; child = child->next) {
559                 gchar *tag;
560
561                 if (xmlNodeIsText (child)) {
562                         continue;
563                 }
564
565                 tag = (gchar *) child->name;
566                 str = (gchar *) xmlNodeGetContent (child);
567
568                 if (strcmp (tag, "name") == 0) {
569                         name = g_strdup (str);
570                 }
571                 else if (strcmp (tag, "room") == 0) {
572                         room = g_strdup (str);
573                 }
574                 else if (strcmp (tag, "auto_connect") == 0) {
575                         if (strcmp (str, "yes") == 0) {
576                                 auto_connect = TRUE;
577                         } else {
578                                 auto_connect = FALSE;
579                         }
580                 }
581                 else if (strcmp (tag, "account") == 0) {
582                         account_id = g_strdup (str);
583                 }
584
585                 xmlFree (str);
586         }
587
588         account = mc_account_lookup (account_id);
589         if (!account) {
590                 g_free (name);
591                 g_free (room);
592                 g_free (account_id);
593                 return;
594         }
595
596         chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
597   g_object_set (chatroom, "favorite", TRUE, NULL);
598   add_chatroom (manager, chatroom);
599         g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
600
601         g_object_unref (account);
602         g_free (name);
603         g_free (room);
604         g_free (account_id);
605 }
606
607 static gboolean
608 chatroom_manager_file_save (EmpathyChatroomManager *manager)
609 {
610         EmpathyChatroomManagerPriv *priv;
611         xmlDocPtr                  doc;
612         xmlNodePtr                 root;
613         GList                     *l;
614
615         priv = GET_PRIV (manager);
616
617         doc = xmlNewDoc ("1.0");
618         root = xmlNewNode (NULL, "chatrooms");
619         xmlDocSetRootElement (doc, root);
620
621         for (l = priv->chatrooms; l; l = l->next) {
622                 EmpathyChatroom *chatroom;
623                 xmlNodePtr      node;
624                 const gchar    *account_id;
625     gboolean favorite;
626
627                 chatroom = l->data;
628
629     g_object_get (chatroom, "favorite", &favorite, NULL);
630     if (!favorite)
631       continue;
632
633                 account_id = mc_account_get_unique_name (empathy_chatroom_get_account (chatroom));
634
635                 node = xmlNewChild (root, NULL, "chatroom", NULL);
636                 xmlNewTextChild (node, NULL, "name", empathy_chatroom_get_name (chatroom));
637                 xmlNewTextChild (node, NULL, "room", empathy_chatroom_get_room (chatroom));
638                 xmlNewTextChild (node, NULL, "account", account_id);
639                 xmlNewTextChild (node, NULL, "auto_connect", empathy_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
640         }
641
642         /* Make sure the XML is indented properly */
643         xmlIndentTreeOutput = 1;
644
645         DEBUG ("Saving file:'%s'", priv->file);
646         xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
647         xmlFreeDoc (doc);
648
649         xmlCleanupParser ();
650         xmlMemoryDump ();
651
652         return TRUE;
653 }