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