]> git.0d.be Git - empathy.git/blob - libempathy/empathy-chatroom-manager.c
839e5da8eb6be9dd61de921845c1192a22421e05
[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
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
44 typedef struct {
45         GList      *chatrooms;
46   gchar *file;
47 } EmpathyChatroomManagerPriv;
48
49 static void     chatroom_manager_finalize          (GObject                    *object);
50 static gboolean chatroom_manager_get_all           (EmpathyChatroomManager      *manager);
51 static gboolean chatroom_manager_file_parse        (EmpathyChatroomManager      *manager,
52                                                     const gchar                *filename);
53 static void     chatroom_manager_parse_chatroom    (EmpathyChatroomManager      *manager,
54                                                     xmlNodePtr                  node);
55 static gboolean chatroom_manager_file_save         (EmpathyChatroomManager      *manager);
56
57 enum {
58         CHATROOM_ADDED,
59         CHATROOM_REMOVED,
60         LAST_SIGNAL
61 };
62
63 static guint signals[LAST_SIGNAL];
64
65 /* properties */
66 enum
67 {
68   PROP_FILE = 1,
69   LAST_PROPERTY
70 };
71
72 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
73
74 static void
75 empathy_chatroom_manager_get_property (GObject *object,
76                                        guint property_id,
77                                        GValue *value,
78                                        GParamSpec *pspec)
79 {
80   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
81   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
82
83   switch (property_id)
84     {
85       case PROP_FILE:
86         g_value_set_string (value, priv->file);
87         break;
88       default:
89         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90         break;
91     }
92 }
93
94 static void
95 empathy_chatroom_manager_set_property (GObject *object,
96                                        guint property_id,
97                                        const GValue *value,
98                                        GParamSpec *pspec)
99 {
100   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
101   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
102
103   switch (property_id)
104     {
105       case PROP_FILE:
106         g_free (priv->file);
107         priv->file = g_value_dup_string (value);
108         break;
109       default:
110         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111         break;
112     }
113 }
114
115 static GObject *
116 empathy_chatroom_manager_constructor (GType type,
117                                       guint n_props,
118                                       GObjectConstructParam *props)
119 {
120   GObject *obj;
121   EmpathyChatroomManager *self;
122   EmpathyChatroomManagerPriv *priv;
123
124   /* Parent constructor chain */
125   obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
126         constructor (type, n_props, props);
127
128   self = EMPATHY_CHATROOM_MANAGER (obj);
129   priv = GET_PRIV (self);
130
131   if (priv->file == NULL)
132     {
133       /* Set the default file path */
134       gchar *dir;
135
136       dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
137       if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
138         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
139
140       priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
141       g_free (dir);
142     }
143
144   chatroom_manager_get_all (self);
145   return obj;
146 }
147
148 static void
149 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
150 {
151         GObjectClass *object_class = G_OBJECT_CLASS (klass);
152   GParamSpec *param_spec;
153
154   object_class->constructor = empathy_chatroom_manager_constructor;
155   object_class->get_property = empathy_chatroom_manager_get_property;
156   object_class->set_property = empathy_chatroom_manager_set_property;
157         object_class->finalize = chatroom_manager_finalize;
158
159   param_spec = g_param_spec_string (
160       "file",
161       "path of the favorite file",
162       "The path of the XML file containing user's favorites",
163       NULL,
164       G_PARAM_CONSTRUCT_ONLY |
165       G_PARAM_READWRITE |
166       G_PARAM_STATIC_NAME |
167       G_PARAM_STATIC_NICK |
168       G_PARAM_STATIC_BLURB);
169   g_object_class_install_property (object_class, PROP_FILE, param_spec);
170
171         signals[CHATROOM_ADDED] =
172                 g_signal_new ("chatroom-added",
173                               G_TYPE_FROM_CLASS (klass),
174                               G_SIGNAL_RUN_LAST,
175                               0,
176                               NULL, NULL,
177                               g_cclosure_marshal_VOID__OBJECT,
178                               G_TYPE_NONE,
179                               1, EMPATHY_TYPE_CHATROOM);
180         signals[CHATROOM_REMOVED] =
181                 g_signal_new ("chatroom-removed",
182                               G_TYPE_FROM_CLASS (klass),
183                               G_SIGNAL_RUN_LAST,
184                               0,
185                               NULL, NULL,
186                               g_cclosure_marshal_VOID__OBJECT,
187                               G_TYPE_NONE,
188                               1, EMPATHY_TYPE_CHATROOM);
189
190         g_type_class_add_private (object_class,
191                                   sizeof (EmpathyChatroomManagerPriv));
192 }
193
194 static void
195 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
196 {
197         EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
198                 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
199
200         manager->priv = priv;
201 }
202
203 static void
204 chatroom_manager_finalize (GObject *object)
205 {
206         EmpathyChatroomManagerPriv *priv;
207
208         priv = GET_PRIV (object);
209
210         g_list_foreach (priv->chatrooms, (GFunc) g_object_unref, NULL);
211         g_list_free (priv->chatrooms);
212   g_free (priv->file);
213
214         (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
215 }
216
217 EmpathyChatroomManager *
218 empathy_chatroom_manager_new (const gchar *file)
219 {
220         static EmpathyChatroomManager *manager = NULL;
221
222         if (!manager) {
223                 manager = g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
224         "file", file,
225         NULL);
226         
227                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
228         } else {
229                 g_object_ref (manager);
230         }
231
232         return manager;
233 }
234
235 gboolean
236 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
237                              EmpathyChatroom        *chatroom)
238 {
239         EmpathyChatroomManagerPriv *priv;
240
241         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
242         g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
243
244         priv = GET_PRIV (manager);
245
246         /* don't add more than once */
247         if (!empathy_chatroom_manager_find (manager,
248                                            empathy_chatroom_get_account (chatroom),
249                                            empathy_chatroom_get_room (chatroom))) {
250                 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
251                 chatroom_manager_file_save (manager);
252
253                 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
254
255                 return TRUE;
256         }
257
258         return FALSE;
259 }
260
261 void
262 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
263                                 EmpathyChatroom        *chatroom)
264 {
265         EmpathyChatroomManagerPriv *priv;
266         GList                     *l;
267
268         g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
269         g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
270
271         priv = GET_PRIV (manager);
272
273         for (l = priv->chatrooms; l; l = l->next) {
274                 EmpathyChatroom *this_chatroom;
275
276                 this_chatroom = l->data;
277
278                 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
279                         priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
280
281                         chatroom_manager_file_save (manager);
282
283                         g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, this_chatroom);
284                         g_object_unref (this_chatroom);
285                         break;
286                 }
287         }
288 }
289
290 EmpathyChatroom *
291 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
292                               McAccount             *account,
293                               const gchar           *room)
294 {
295         EmpathyChatroomManagerPriv *priv;
296         GList                     *l;
297
298         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
299         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
300         g_return_val_if_fail (room != NULL, NULL);
301
302         priv = GET_PRIV (manager);
303
304         for (l = priv->chatrooms; l; l = l->next) {
305                 EmpathyChatroom *chatroom;
306                 McAccount      *this_account;
307                 const gchar    *this_room;
308
309                 chatroom = l->data;
310                 this_account = empathy_chatroom_get_account (chatroom);
311                 this_room = empathy_chatroom_get_room (chatroom);
312
313                 if (this_account && this_room &&
314                     empathy_account_equal (account, this_account) &&
315                     strcmp (this_room, room) == 0) {
316                         return chatroom;
317                 }
318         }
319
320         return NULL;
321 }
322
323 GList *
324 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
325                                        McAccount             *account)
326 {
327         EmpathyChatroomManagerPriv *priv;
328         GList                     *chatrooms, *l;
329
330         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
331
332         priv = GET_PRIV (manager);
333
334         if (!account) {
335                 return g_list_copy (priv->chatrooms);
336         }
337
338         chatrooms = NULL;
339         for (l = priv->chatrooms; l; l = l->next) {
340                 EmpathyChatroom *chatroom;
341
342                 chatroom = l->data;
343
344                 if (empathy_account_equal (account,
345                                           empathy_chatroom_get_account (chatroom))) {
346                         chatrooms = g_list_append (chatrooms, chatroom);
347                 }
348         }
349
350         return chatrooms;
351 }
352
353 guint
354 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
355                                    McAccount             *account)
356 {
357         EmpathyChatroomManagerPriv *priv;
358         GList                     *l;
359         guint                      count = 0;
360
361         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
362
363         priv = GET_PRIV (manager);
364
365         if (!account) {
366                 return g_list_length (priv->chatrooms);
367         }
368
369         for (l = priv->chatrooms; l; l = l->next) {
370                 EmpathyChatroom *chatroom;
371
372                 chatroom = l->data;
373
374                 if (empathy_account_equal (account,
375                                           empathy_chatroom_get_account (chatroom))) {
376                         count++;
377                 }
378         }
379
380         return count;
381 }
382
383 void
384 empathy_chatroom_manager_store (EmpathyChatroomManager *manager)
385 {
386         g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
387
388         chatroom_manager_file_save (manager);
389 }
390
391 /*
392  * API to save/load and parse the chatrooms file.
393  */
394
395 static gboolean
396 chatroom_manager_get_all (EmpathyChatroomManager *manager)
397 {
398         EmpathyChatroomManagerPriv *priv;
399
400         priv = GET_PRIV (manager);
401
402         /* read file in */
403         if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
404             !chatroom_manager_file_parse (manager, priv->file))
405     return FALSE;
406
407         return TRUE;
408 }
409
410 static gboolean
411 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
412                              const gchar           *filename)
413 {
414         EmpathyChatroomManagerPriv *priv;
415         xmlParserCtxtPtr           ctxt;
416         xmlDocPtr                  doc;
417         xmlNodePtr                 chatrooms;
418         xmlNodePtr                 node;
419
420         priv = GET_PRIV (manager);
421
422         DEBUG ("Attempting to parse file:'%s'...", filename);
423
424         ctxt = xmlNewParserCtxt ();
425
426         /* Parse and validate the file. */
427         doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
428         if (!doc) {
429                 g_warning ("Failed to parse file:'%s'", filename);
430                 xmlFreeParserCtxt (ctxt);
431                 return FALSE;
432         }
433
434         if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
435                 g_warning ("Failed to validate file:'%s'", filename);
436                 xmlFreeDoc(doc);
437                 xmlFreeParserCtxt (ctxt);
438                 return FALSE;
439         }
440
441         /* The root node, chatrooms. */
442         chatrooms = xmlDocGetRootElement (doc);
443
444         for (node = chatrooms->children; node; node = node->next) {
445                 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
446                         chatroom_manager_parse_chatroom (manager, node);
447                 }
448         }
449
450         DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
451
452         xmlFreeDoc(doc);
453         xmlFreeParserCtxt (ctxt);
454
455         return TRUE;
456 }
457
458 static void
459 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
460                                  xmlNodePtr             node)
461 {
462         EmpathyChatroomManagerPriv *priv;
463         EmpathyChatroom            *chatroom;
464         McAccount                 *account;
465         xmlNodePtr                 child;
466         gchar                     *str;
467         gchar                     *name;
468         gchar                     *room;
469         gchar                     *account_id;
470         gboolean                   auto_connect;
471
472         priv = GET_PRIV (manager);
473
474         /* default values. */
475         name = NULL;
476         room = NULL;
477         auto_connect = TRUE;
478         account_id = NULL;
479
480         for (child = node->children; child; child = child->next) {
481                 gchar *tag;
482
483                 if (xmlNodeIsText (child)) {
484                         continue;
485                 }
486
487                 tag = (gchar *) child->name;
488                 str = (gchar *) xmlNodeGetContent (child);
489
490                 if (strcmp (tag, "name") == 0) {
491                         name = g_strdup (str);
492                 }
493                 else if (strcmp (tag, "room") == 0) {
494                         room = g_strdup (str);
495                 }
496                 else if (strcmp (tag, "auto_connect") == 0) {
497                         if (strcmp (str, "yes") == 0) {
498                                 auto_connect = TRUE;
499                         } else {
500                                 auto_connect = FALSE;
501                         }
502                 }
503                 else if (strcmp (tag, "account") == 0) {
504                         account_id = g_strdup (str);
505                 }
506
507                 xmlFree (str);
508         }
509
510         account = mc_account_lookup (account_id);
511         if (!account) {
512                 g_free (name);
513                 g_free (room);
514                 g_free (account_id);
515                 return;
516         }
517
518         chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
519   g_object_set (chatroom, "favorite", TRUE, NULL);
520         priv->chatrooms = g_list_prepend (priv->chatrooms, chatroom);
521         g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
522
523         g_object_unref (account);
524         g_free (name);
525         g_free (room);
526         g_free (account_id);
527 }
528
529 static gboolean
530 chatroom_manager_file_save (EmpathyChatroomManager *manager)
531 {
532         EmpathyChatroomManagerPriv *priv;
533         xmlDocPtr                  doc;
534         xmlNodePtr                 root;
535         GList                     *l;
536
537         priv = GET_PRIV (manager);
538
539         doc = xmlNewDoc ("1.0");
540         root = xmlNewNode (NULL, "chatrooms");
541         xmlDocSetRootElement (doc, root);
542
543         for (l = priv->chatrooms; l; l = l->next) {
544                 EmpathyChatroom *chatroom;
545                 xmlNodePtr      node;
546                 const gchar    *account_id;
547     gboolean favorite;
548
549                 chatroom = l->data;
550
551     g_object_get (chatroom, "favorite", &favorite, NULL);
552     if (!favorite)
553       continue;
554
555                 account_id = mc_account_get_unique_name (empathy_chatroom_get_account (chatroom));
556
557                 node = xmlNewChild (root, NULL, "chatroom", NULL);
558                 xmlNewTextChild (node, NULL, "name", empathy_chatroom_get_name (chatroom));
559                 xmlNewTextChild (node, NULL, "room", empathy_chatroom_get_room (chatroom));
560                 xmlNewTextChild (node, NULL, "account", account_id);
561                 xmlNewTextChild (node, NULL, "auto_connect", empathy_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
562         }
563
564         /* Make sure the XML is indented properly */
565         xmlIndentTreeOutput = 1;
566
567         DEBUG ("Saving file:'%s'", priv->file);
568         xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
569         xmlFreeDoc (doc);
570
571         xmlCleanupParser ();
572         xmlMemoryDump ();
573
574         return TRUE;
575 }