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