don't leak uid.
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-group.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* e-source-group.c
3  *
4  * Copyright (C) 2003  Ximian, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
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  * Author: Ettore Perazzoli <ettore@ximian.com>
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include "e-data-server-marshal.h"
29 #include "e-uid.h"
30 #include "e-source-group.h"
31
32 static GObjectClass *parent_class = NULL;
33
34 /* Private members.  */
35
36 struct _ESourceGroupPrivate {
37         char *uid;
38         char *name;
39         char *base_uri;
40
41         GSList *sources;
42
43         gboolean ignore_source_changed;
44         gboolean readonly;
45 };
46
47
48 /* Signals.  */
49
50 enum {
51         CHANGED,
52         SOURCE_REMOVED,
53         SOURCE_ADDED,
54         LAST_SIGNAL
55 };
56 static unsigned int signals[LAST_SIGNAL] = { 0 };
57
58
59 /* Callbacks.  */
60
61 static void
62 source_changed_callback (ESource *source,
63                          ESourceGroup *group)
64 {
65         if (! group->priv->ignore_source_changed)
66                 g_signal_emit (group, signals[CHANGED], 0);
67 }
68
69
70 /* GObject methods.  */
71
72 static void
73 impl_dispose (GObject *object)
74 {
75         ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
76
77         if (priv->sources != NULL) {
78                 GSList *p;
79
80                 for (p = priv->sources; p != NULL; p = p->next) {
81                         ESource *source = E_SOURCE (p->data);
82
83                         g_signal_handlers_disconnect_by_func (source,
84                                                               G_CALLBACK (source_changed_callback),
85                                                               object);
86                         g_object_unref (source);
87                 }
88
89                 g_slist_free (priv->sources);
90                 priv->sources = NULL;
91         }
92
93         (* G_OBJECT_CLASS (parent_class)->dispose) (object);
94 }
95
96 static void
97 impl_finalize (GObject *object)
98 {
99         ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
100
101         g_free (priv->uid);
102         g_free (priv->name);
103         g_free (priv->base_uri);
104         g_free (priv);
105
106         (* G_OBJECT_CLASS (parent_class)->finalize) (object);
107 }
108
109
110 /* Initialization.  */
111
112 static void
113 class_init (ESourceGroupClass *class)
114 {
115         GObjectClass *object_class = G_OBJECT_CLASS (class);
116
117         object_class->dispose  = impl_dispose;
118         object_class->finalize = impl_finalize;
119
120         parent_class = g_type_class_peek_parent (class);
121
122         signals[CHANGED] = 
123                 g_signal_new ("changed",
124                               G_OBJECT_CLASS_TYPE (object_class),
125                               G_SIGNAL_RUN_LAST,
126                               G_STRUCT_OFFSET (ESourceGroupClass, changed),
127                               NULL, NULL,
128                               e_data_server_marshal_VOID__VOID,
129                               G_TYPE_NONE, 0);
130
131         signals[SOURCE_ADDED] = 
132                 g_signal_new ("source_added",
133                               G_OBJECT_CLASS_TYPE (object_class),
134                               G_SIGNAL_RUN_LAST,
135                               G_STRUCT_OFFSET (ESourceGroupClass, source_added),
136                               NULL, NULL,
137                               e_data_server_marshal_VOID__OBJECT,
138                               G_TYPE_NONE, 1,
139                               G_TYPE_OBJECT);
140         signals[SOURCE_REMOVED] = 
141                 g_signal_new ("source_removed",
142                               G_OBJECT_CLASS_TYPE (object_class),
143                               G_SIGNAL_RUN_LAST,
144                               G_STRUCT_OFFSET (ESourceGroupClass, source_removed),
145                               NULL, NULL,
146                               e_data_server_marshal_VOID__OBJECT,
147                               G_TYPE_NONE, 1,
148                               G_TYPE_OBJECT);
149 }
150
151 static void
152 init (ESourceGroup *source_group)
153 {
154         ESourceGroupPrivate *priv;
155
156         priv = g_new0 (ESourceGroupPrivate, 1);
157         source_group->priv = priv;
158 }
159
160 GType
161 e_source_group_get_type (void)
162 {
163         static GType e_source_group_type = 0;
164
165         if (!e_source_group_type) {
166                 static GTypeInfo info = {
167                         sizeof (ESourceGroupClass),
168                         (GBaseInitFunc) NULL,
169                         (GBaseFinalizeFunc) NULL,
170                         (GClassInitFunc) class_init,
171                         NULL, NULL,
172                         sizeof (ESourceGroup),
173                         0,
174                         (GInstanceInitFunc) init
175                 };
176                 e_source_group_type = g_type_register_static (G_TYPE_OBJECT, "ESourceGroup", &info, 0);
177         }
178
179         return e_source_group_type;
180 }
181
182 /* Public methods.  */
183
184 ESourceGroup *
185 e_source_group_new (const char *name,
186                     const char *base_uri)
187 {
188         ESourceGroup *new;
189
190         g_return_val_if_fail (name != NULL, NULL);
191         g_return_val_if_fail (base_uri != NULL, NULL);
192
193         new = g_object_new (e_source_group_get_type (), NULL);
194         new->priv->uid = e_uid_new ();
195
196         e_source_group_set_name (new, name);
197         e_source_group_set_base_uri (new, base_uri);
198
199         return new;
200 }
201
202 ESourceGroup *
203 e_source_group_new_from_xml (const char *xml)
204 {
205         xmlDocPtr doc;
206         ESourceGroup *group;
207
208         doc = xmlParseDoc ((char *) xml);
209         if (doc == NULL)
210                 return NULL;
211
212         group = e_source_group_new_from_xmldoc (doc);
213         xmlFreeDoc (doc);
214
215         return group;
216 }
217
218 ESourceGroup *
219 e_source_group_new_from_xmldoc (xmlDocPtr doc)
220 {
221         xmlNodePtr root, p;
222         xmlChar *uid;
223         xmlChar *name;
224         xmlChar *base_uri;
225         xmlChar *readonly_str;
226         ESourceGroup *new = NULL;
227
228         g_return_val_if_fail (doc != NULL, NULL);
229
230         root = doc->children;
231         if (strcmp (root->name, "group") != 0)
232                 return NULL;
233
234         uid = xmlGetProp (root, "uid");
235         name = xmlGetProp (root, "name");
236         base_uri = xmlGetProp (root, "base_uri");
237         readonly_str = xmlGetProp (root, "readonly");
238
239         if (uid == NULL || name == NULL || base_uri == NULL)
240                 goto done;
241
242         new = g_object_new (e_source_group_get_type (), NULL);
243         new->priv->uid = g_strdup (uid);
244
245         e_source_group_set_name (new, name);
246         e_source_group_set_base_uri (new, base_uri);
247         
248         for (p = root->children; p != NULL; p = p->next) {
249                 ESource *new_source = e_source_new_from_xml_node (p);
250                 e_source_group_add_source (new, new_source, -1);
251         }
252
253         e_source_group_set_readonly (new, readonly_str && !strcmp (readonly_str, "yes"));
254
255  done:
256         if (uid != NULL)
257                 xmlFree (uid);
258
259         if (name != NULL)
260                 xmlFree (name);
261         if (base_uri != NULL)
262                 xmlFree (base_uri);
263         if (readonly_str != NULL)
264                 xmlFree (readonly_str);
265         return new;
266 }
267
268 gboolean
269 e_source_group_update_from_xml (ESourceGroup *group,
270                                 const char *xml,
271                                 gboolean *changed_return)
272 {
273         xmlDocPtr xmldoc;
274         gboolean success;
275
276         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
277         g_return_val_if_fail (xml != NULL, FALSE);
278
279         xmldoc = xmlParseDoc ((char *) xml);
280
281         success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return);
282
283         xmlFreeDoc (xmldoc);
284
285         return success;
286 }
287
288 gboolean
289 e_source_group_update_from_xmldoc (ESourceGroup *group,
290                                    xmlDocPtr doc,
291                                    gboolean *changed_return)
292 {
293         GHashTable *new_sources_hash;
294         GSList *new_sources_list = NULL;
295         xmlNodePtr root, nodep;
296         xmlChar *name, *base_uri, *readonly_str;
297         gboolean readonly;
298         gboolean changed = FALSE;
299         GSList *p, *q;
300
301         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
302         g_return_val_if_fail (doc != NULL, FALSE);
303
304         *changed_return = FALSE;
305
306         root = doc->children;
307         if (strcmp (root->name, "group") != 0)
308                 return FALSE;
309
310         name = xmlGetProp (root, "name");
311         if (name == NULL)
312                 return FALSE;
313
314         base_uri = xmlGetProp (root, "base_uri");
315         if (base_uri == NULL) {
316                 xmlFree (name);
317                 return FALSE;
318         }
319
320         if (strcmp (group->priv->name, name) != 0) {
321                 g_free (group->priv->name);
322                 group->priv->name = g_strdup (name);
323                 changed = TRUE;
324         }
325         xmlFree (name);
326
327         if (strcmp (group->priv->base_uri, base_uri) != 0) {
328                 g_free (group->priv->base_uri);
329                 group->priv->base_uri = g_strdup (base_uri);
330                 changed = TRUE;
331         }
332         xmlFree (base_uri);
333
334         readonly_str = xmlGetProp (root, "readonly");
335         readonly = readonly_str && !strcmp (readonly_str, "yes");
336         if (readonly != group->priv->readonly) {
337                 group->priv->readonly = readonly;
338                 changed = TRUE;
339         }
340         xmlFree (readonly_str);
341         
342         new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
343
344         for (nodep = root->children; nodep != NULL; nodep = nodep->next) {
345                 ESource *existing_source;
346                 char *uid = e_source_uid_from_xml_node (nodep);
347
348                 if (uid == NULL)
349                         continue;
350
351                 existing_source = e_source_group_peek_source_by_uid (group, uid);
352                 if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL)
353                         continue;
354
355                 if (existing_source == NULL) {
356                         ESource *new_source = e_source_new_from_xml_node (nodep);
357
358                         if (new_source != NULL) {
359                                 e_source_set_group (new_source, group);
360                                 g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group);
361                                 new_sources_list = g_slist_prepend (new_sources_list, new_source);
362
363                                 g_hash_table_insert (new_sources_hash, new_source, new_source);
364
365                                 g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source);
366                                 changed = TRUE;
367                         }
368                 } else {
369                         gboolean source_changed;
370
371                         group->priv->ignore_source_changed ++;
372
373                         if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) {
374                                 new_sources_list = g_slist_prepend (new_sources_list, existing_source);
375                                 g_object_ref (existing_source);
376                                 g_hash_table_insert (new_sources_hash, existing_source, existing_source);
377
378                                 if (source_changed)
379                                         changed = TRUE;
380                         }
381
382                         group->priv->ignore_source_changed --;
383                 }
384
385                 g_free (uid);
386         }
387
388         new_sources_list = g_slist_reverse (new_sources_list);
389
390         /* Emit "group_removed" and disconnect the "changed" signal for all the
391            groups that we haven't found in the new list.  */
392         q = new_sources_list;
393         for (p = group->priv->sources; p != NULL; p = p->next) {
394                 ESource *source = E_SOURCE (p->data);
395
396                 if (g_hash_table_lookup (new_sources_hash, source) == NULL) {
397                         changed = TRUE;
398
399                         g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
400                         g_signal_handlers_disconnect_by_func (source, source_changed_callback, group);
401                 }
402
403                 if (! changed && q != NULL) {
404                         if (q->data != p->data)
405                                 changed = TRUE;
406                         q = q->next;
407                 }
408         }
409
410         g_hash_table_destroy (new_sources_hash);
411
412         /* Replace the original group list with the new one.  */
413         g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL);
414         g_slist_free (group->priv->sources);
415
416         group->priv->sources = new_sources_list;
417
418         /* FIXME if the order changes, the function doesn't notice.  */
419
420         if (changed) {
421                 g_signal_emit (group, signals[CHANGED], 0);
422                 *changed_return = TRUE;
423         }
424
425         return TRUE;            /* Success. */
426 }
427
428 char *
429 e_source_group_uid_from_xmldoc (xmlDocPtr doc)
430 {
431         xmlNodePtr root = doc->children;
432         xmlChar *name;
433         char *retval;
434
435         if (strcmp (root->name, "group") != 0)
436                 return NULL;
437
438         name = xmlGetProp (root, "uid");
439         if (name == NULL)
440                 return NULL;
441
442         retval = g_strdup (name);
443         xmlFree (name);
444         return retval;
445 }
446
447 void
448 e_source_group_set_name (ESourceGroup *group,
449                          const char *name)
450 {
451         g_return_if_fail (E_IS_SOURCE_GROUP (group));
452         g_return_if_fail (name != NULL);
453
454         if (group->priv->readonly)
455                 return;
456         
457         if (group->priv->name == name)
458                 return;
459
460         g_free (group->priv->name);
461         group->priv->name = g_strdup (name);
462
463         g_signal_emit (group, signals[CHANGED], 0);
464 }
465
466 void e_source_group_set_base_uri (ESourceGroup *group,
467                                   const char *base_uri)
468 {
469         g_return_if_fail (E_IS_SOURCE_GROUP (group));
470         g_return_if_fail (base_uri != NULL);
471         
472         if (group->priv->readonly)
473                 return;
474         
475         if (group->priv->base_uri == base_uri)
476                 return;
477
478         g_free (group->priv->base_uri);
479         group->priv->base_uri = g_strdup (base_uri);
480
481         g_signal_emit (group, signals[CHANGED], 0);
482 }
483
484 void e_source_group_set_readonly (ESourceGroup *group,
485                                   gboolean      readonly)
486 {
487         GSList *i;
488         
489         g_return_if_fail (E_IS_SOURCE_GROUP (group));
490         
491         if (group->priv->readonly)
492                 return;
493         
494         if (group->priv->readonly == readonly)
495                 return;
496
497         group->priv->readonly = readonly;
498         for (i = group->priv->sources; i != NULL; i = i->next)
499                 e_source_set_readonly (E_SOURCE (i->data), readonly);   
500
501         g_signal_emit (group, signals[CHANGED], 0);
502 }
503
504 const char *
505 e_source_group_peek_uid (ESourceGroup *group)
506 {
507         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
508
509         return group->priv->uid;
510 }
511
512 const char *
513 e_source_group_peek_name (ESourceGroup *group)
514 {
515         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
516
517         return group->priv->name;
518 }
519
520 const char *
521 e_source_group_peek_base_uri (ESourceGroup *group)
522 {
523         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
524
525         return group->priv->base_uri;
526 }
527
528 gboolean
529 e_source_group_get_readonly (ESourceGroup *group)
530 {
531         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
532
533         return group->priv->readonly;
534 }
535
536 GSList *
537 e_source_group_peek_sources (ESourceGroup *group)
538 {
539         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
540
541         return group->priv->sources;
542 }
543
544 ESource *
545 e_source_group_peek_source_by_uid (ESourceGroup *group,
546                                    const char *uid)
547 {
548         GSList *p;
549
550         for (p = group->priv->sources; p != NULL; p = p->next) {
551                 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
552                         return E_SOURCE (p->data);
553         }
554
555         return NULL;
556 }
557
558 ESource *
559 e_source_group_peek_source_by_name (ESourceGroup *group,
560                                     const char *name)
561 {
562         GSList *p;
563
564         for (p = group->priv->sources; p != NULL; p = p->next) {
565                 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
566                         return E_SOURCE (p->data);
567         }
568
569         return NULL;
570 }
571
572 gboolean
573 e_source_group_add_source (ESourceGroup *group,
574                            ESource *source,
575                            int position)
576 {
577         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
578
579         if (group->priv->readonly)
580                 return FALSE;
581         
582         if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
583                 return FALSE;
584
585         e_source_set_group (source, group);
586         e_source_set_readonly (source, group->priv->readonly);
587         g_object_ref (source);
588
589         g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
590
591         group->priv->sources = g_slist_insert (group->priv->sources, source, position);
592         g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
593         g_signal_emit (group, signals[CHANGED], 0);
594
595         return TRUE;
596 }
597
598 gboolean
599 e_source_group_remove_source (ESourceGroup *group,
600                               ESource *source)
601 {
602         GSList *p;
603
604         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
605         g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
606
607         if (group->priv->readonly)
608                 return FALSE;
609
610         for (p = group->priv->sources; p != NULL; p = p->next) {
611                 if (E_SOURCE (p->data) == source) {
612                         group->priv->sources = g_slist_remove_link (group->priv->sources, p);
613                         g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
614                         g_signal_emit (group, signals[CHANGED], 0);
615                         return TRUE;
616                 }
617         }
618
619         return FALSE;
620 }
621
622 gboolean
623 e_source_group_remove_source_by_uid (ESourceGroup *group,
624                                      const char *uid)
625 {
626         GSList *p;
627
628         g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
629         g_return_val_if_fail (uid != NULL, FALSE);
630
631         if (group->priv->readonly)
632                 return FALSE;
633         
634         for (p = group->priv->sources; p != NULL; p = p->next) {
635                 ESource *source = E_SOURCE (p->data);
636
637                 if (strcmp (e_source_peek_uid (source), uid) == 0) {
638                         group->priv->sources = g_slist_remove_link (group->priv->sources, p);
639                         g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
640                         g_signal_emit (group, signals[CHANGED], 0);
641                         return TRUE;
642                 }
643         }
644
645         return FALSE;
646 }
647
648
649 char *
650 e_source_group_to_xml (ESourceGroup *group)
651 {
652         xmlDocPtr doc;
653         xmlNodePtr root;
654         xmlChar *xml_buffer;
655         char *returned_buffer;
656         int xml_buffer_size;
657         GSList *p;
658
659         doc = xmlNewDoc ("1.0");
660
661         root = xmlNewDocNode (doc, NULL, "group", NULL);
662         xmlSetProp (root, "uid", e_source_group_peek_uid (group));
663         xmlSetProp (root, "name", e_source_group_peek_name (group));
664         xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group));
665         xmlSetProp (root, "readonly", group->priv->readonly ? "yes" : "no");
666         
667         xmlDocSetRootElement (doc, root);
668
669         for (p = group->priv->sources; p != NULL; p = p->next)
670                 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
671
672         xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
673         xmlFreeDoc (doc);
674
675         returned_buffer = g_malloc (xml_buffer_size + 1);
676         memcpy (returned_buffer, xml_buffer, xml_buffer_size);
677         returned_buffer [xml_buffer_size] = '\0';
678         xmlFree (xml_buffer);
679
680         return returned_buffer;
681 }