aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/menus/gal-view-collection.c
blob: 39ce216ce1ebaa74e2cbd181214340e6de31afa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Chris Lahey <clahey@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ctype.h>
#include <string.h>
#include <errno.h>

#include <libxml/parser.h>
#include <libedataserver/e-xml-utils.h>

#include <glib/gi18n.h>
#include "e-util/e-util.h"
#include "e-util/e-xml-utils.h"
#include "e-util/e-unicode.h"

#include "gal-view-collection.h"

G_DEFINE_TYPE (GalViewCollection, gal_view_collection, G_TYPE_OBJECT)

#define d(x)

enum {
    DISPLAY_VIEW,
    CHANGED,
    LAST_SIGNAL
};

static guint gal_view_collection_signals[LAST_SIGNAL] = { 0, };

/**
 * gal_view_collection_display_view:
 * @collection: The GalViewCollection to send the signal on.
 * @view: The view to display.
 *
 */
void
gal_view_collection_display_view (GalViewCollection *collection,
                  GalView *view)
{
    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (view != NULL);
    g_return_if_fail (GAL_IS_VIEW (view));

    g_signal_emit (collection,
               gal_view_collection_signals[DISPLAY_VIEW], 0,
               view);
}

static void
gal_view_collection_changed (GalViewCollection *collection)
{
    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));

    g_signal_emit (collection,
               gal_view_collection_signals[CHANGED], 0);
}

static void
gal_view_collection_item_free (GalViewCollectionItem *item)
{
    g_free (item->id);
    if (item->view) {
        if (item->view_changed_id)
            g_signal_handler_disconnect (item->view,
                             item->view_changed_id);
        g_object_unref (item->view);
    }
    g_free (item);
}

static gchar *
gal_view_generate_string (GalViewCollection *collection,
              GalView           *view,
              gint which)
{
    gchar *ret_val;
    gchar *pointer;

    if (which == 1)
        ret_val = g_strdup (gal_view_get_title (view));
    else
        ret_val = g_strdup_printf("%s_%d", gal_view_get_title(view), which);
    for (pointer = ret_val; *pointer; pointer = g_utf8_next_char (pointer)) {
        if (!g_unichar_isalnum (g_utf8_get_char (pointer))) {
            gchar *ptr = pointer;
            for (; ptr < g_utf8_next_char (pointer); *ptr = '_', ptr++)
                ;
        }
    }
    return ret_val;
}

static gint
gal_view_check_string (GalViewCollection *collection,
               gchar *string)
{
    gint i;

    if (!strcmp (string, "current_view"))
        return FALSE;

    for (i = 0; i < collection->view_count; i++) {
        if (!strcmp (string, collection->view_data[i]->id))
            return FALSE;
    }
    for (i = 0; i < collection->removed_view_count; i++) {
        if (!strcmp (string, collection->removed_view_data[i]->id))
            return FALSE;
    }
    return TRUE;
}

static gchar *
gal_view_generate_id (GalViewCollection *collection,
              GalView           *view)
{
    gint i;
    for (i = 1; TRUE; i++) {
        gchar *try;

        try = gal_view_generate_string (collection, view, i);
        if (gal_view_check_string (collection, try))
            return try;
        g_free (try);
    }
}

static void
gal_view_collection_dispose (GObject *object)
{
    GalViewCollection *collection = GAL_VIEW_COLLECTION (object);
    gint i;

    for (i = 0; i < collection->view_count; i++) {
        gal_view_collection_item_free (collection->view_data[i]);
    }
    g_free (collection->view_data);
    collection->view_data = NULL;
    collection->view_count = 0;

    g_list_foreach (
        collection->factory_list,
        (GFunc) g_object_unref, NULL);
    g_list_free (collection->factory_list);
    collection->factory_list = NULL;

    for (i = 0; i < collection->removed_view_count; i++) {
        gal_view_collection_item_free (collection->removed_view_data[i]);
    }
    g_free (collection->removed_view_data);
    collection->removed_view_data  = NULL;
    collection->removed_view_count = 0;

    g_free (collection->system_dir);
    collection->system_dir = NULL;

    g_free (collection->local_dir);
    collection->local_dir = NULL;

    g_free (collection->default_view);
    collection->default_view = NULL;

    g_free (collection->title);
    collection->title = NULL;

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (gal_view_collection_parent_class)->dispose (object);
}

static void
gal_view_collection_class_init (GalViewCollectionClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->dispose = gal_view_collection_dispose;

    gal_view_collection_signals[DISPLAY_VIEW] =
        g_signal_new ("display_view",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GalViewCollectionClass, display_view),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1, GAL_TYPE_VIEW);

    gal_view_collection_signals[CHANGED] =
        g_signal_new ("changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GalViewCollectionClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

    klass->display_view = NULL;
    klass->changed      = NULL;
}

static void
gal_view_collection_init (GalViewCollection *collection)
{
    collection->view_data             = NULL;
    collection->view_count            = 0;
    collection->factory_list          = NULL;

    collection->removed_view_data     = NULL;
    collection->removed_view_count    = 0;

    collection->system_dir            = NULL;
    collection->local_dir             = NULL;

    collection->loaded                = FALSE;
    collection->default_view          = NULL;
    collection->default_view_built_in = TRUE;

    collection->title                 = NULL;
}

/**
 * gal_view_collection_new:
 *
 * A collection of views and view factories.
 */
GalViewCollection *
gal_view_collection_new                      (void)
{
    return g_object_new (GAL_VIEW_COLLECTION_TYPE, NULL);
}

void
gal_view_collection_set_title (GalViewCollection *collection,
                   const gchar *title)
{
    g_free (collection->title);
    collection->title = g_strdup (title);
}

/**
 * gal_view_collection_set_storage_directories
 * @collection: The view collection to initialize
 * @system_dir: The location of the system built in views
 * @local_dir: The location to store the users set up views
 *
 * Sets up the GalViewCollection.
 */
void
gal_view_collection_set_storage_directories  (GalViewCollection *collection,
                          const gchar        *system_dir,
                          const gchar        *local_dir)
{
    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (system_dir != NULL);
    g_return_if_fail (local_dir != NULL);

    g_free (collection->system_dir);
    g_free (collection->local_dir);

    collection->system_dir = g_strdup (system_dir);
    collection->local_dir = g_strdup (local_dir);
}

/**
 * gal_view_collection_add_factory
 * @collection: The view collection to add a factory to
 * @factory: The factory to add.  The @collection will add a reference
 * to the factory object, so you should unref it after calling this
 * function if you no longer need it.
 *
 * Adds the given factory to this collection.  This list is used both
 * when loading views from their xml description as well as when the
 * user tries to create a new view.
 */
void
gal_view_collection_add_factory              (GalViewCollection *collection,
                          GalViewFactory    *factory)
{
    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (factory != NULL);
    g_return_if_fail (GAL_IS_VIEW_FACTORY (factory));

    g_object_ref (factory);
    collection->factory_list = g_list_prepend (collection->factory_list, factory);
}

static void
view_changed (GalView *view,
          GalViewCollectionItem *item)
{
    item->changed = TRUE;
    item->ever_changed = TRUE;

    g_signal_handler_block (G_OBJECT (item->view), item->view_changed_id);
    gal_view_collection_changed (item->collection);
    g_signal_handler_unblock (G_OBJECT (item->view), item->view_changed_id);
}

/* Use factory list to load a GalView file. */
static GalView *
gal_view_collection_real_load_view_from_file (GalViewCollection *collection, const gchar *type, const gchar *title, const gchar *dir, const gchar *filename)
{
    GalViewFactory *factory;
    GList *factories;

    factory = NULL;
    for (factories = collection->factory_list; factories; factories = factories->next) {
        if (type && !strcmp (gal_view_factory_get_type_code (factories->data), type)) {
            factory = factories->data;
            break;
        }
    }
    if (factory) {
        GalView *view;

        view = gal_view_factory_new_view (factory, title);
        gal_view_set_title (view, title);
        gal_view_load (view, filename);
        return view;
    }
    return NULL;
}

GalView *
gal_view_collection_load_view_from_file (GalViewCollection *collection, const gchar *type, const gchar *filename)
{
    return gal_view_collection_real_load_view_from_file (collection, type, "", collection->local_dir, filename);
}

static GalViewCollectionItem *
load_single_file (GalViewCollection *collection,
          gchar *dir,
          gboolean local,
          xmlNode *node)
{
    GalViewCollectionItem *item;
    item = g_new (GalViewCollectionItem, 1);
    item->ever_changed = local;
    item->changed = FALSE;
    item->built_in = !local;
    item->id = e_xml_get_string_prop_by_name(node, (const guchar *)"id");
    item->filename = e_xml_get_string_prop_by_name(node, (const guchar *)"filename");
    item->title = e_xml_get_translated_utf8_string_prop_by_name(node, (const guchar *)"title");
    item->type = e_xml_get_string_prop_by_name(node, (const guchar *)"type");
    item->collection = collection;
    item->view_changed_id = 0;

    if (item->filename) {
        gchar *fullpath;
        fullpath = g_build_filename (dir, item->filename, NULL);
        item->view = gal_view_collection_real_load_view_from_file (collection, item->type, item->title, dir, fullpath);
        g_free (fullpath);
        if (item->view) {
            item->view_changed_id =
                g_signal_connect(item->view, "changed",
                         G_CALLBACK (view_changed), item);
        }
    }
    return item;
}

static void
load_single_dir (GalViewCollection *collection,
         gchar *dir,
         gboolean local)
{
    xmlDoc *doc = NULL;
    xmlNode *root;
    xmlNode *child;
    gchar *filename = g_build_filename(dir, "galview.xml", NULL);
    gchar *default_view;

    if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
#ifdef G_OS_WIN32
        gchar *locale_filename = g_win32_locale_filename_from_utf8 (filename);
        if (locale_filename != NULL)
            doc = xmlParseFile (locale_filename);
        g_free (locale_filename);
#else
        doc = xmlParseFile (filename);
#endif
    }

    if (!doc) {
        g_free (filename);
        return;
    }
    root = xmlDocGetRootElement (doc);
    for (child = root->xmlChildrenNode; child; child = child->next) {
        gchar *id;
        gboolean found = FALSE;
        gint i;

        if (!strcmp ((gchar *)child->name, "text"))
            continue;

        id = e_xml_get_string_prop_by_name(child, (const guchar *)"id");
        for (i = 0; i < collection->view_count; i++) {
            if (!strcmp (id, collection->view_data[i]->id)) {
                if (!local)
                    collection->view_data[i]->built_in = TRUE;
                found = TRUE;
                break;
            }
        }
        if (!found) {
            for (i = 0; i < collection->removed_view_count; i++) {
                if (!strcmp (id, collection->removed_view_data[i]->id)) {
                    if (!local)
                        collection->removed_view_data[i]->built_in = TRUE;
                    found = TRUE;
                    break;
                }
            }
        }

        if (!found) {
            GalViewCollectionItem *item = load_single_file (collection, dir, local, child);
            if (item->filename && *item->filename) {
                collection->view_data = g_renew (GalViewCollectionItem *, collection->view_data, collection->view_count + 1);
                collection->view_data[collection->view_count] = item;
                collection->view_count++;
            } else {
                collection->removed_view_data = g_renew (GalViewCollectionItem *, collection->removed_view_data, collection->removed_view_count + 1);
                collection->removed_view_data[collection->removed_view_count] = item;
                collection->removed_view_count++;
            }
        }
        g_free (id);
    }

    default_view = e_xml_get_string_prop_by_name (root, (const guchar *)"default-view");
    if (default_view) {
        if (local)
            collection->default_view_built_in = FALSE;
        else
            collection->default_view_built_in = TRUE;
        g_free (collection->default_view);
        collection->default_view = default_view;
    }

    g_free (filename);
    xmlFreeDoc (doc);
}

/**
 * gal_view_collection_load
 * @collection: The view collection to load information for
 *
 * Loads the data from the system and user directories specified in
 * set storage directories.  This is primarily for internal use by
 * other parts of gal_view.
 */
void
gal_view_collection_load              (GalViewCollection *collection)
{
    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (collection->local_dir != NULL);
    g_return_if_fail (collection->system_dir != NULL);
    g_return_if_fail (!collection->loaded);

    if ((g_mkdir_with_parents (collection->local_dir, 0777) == -1) && (errno != EEXIST))
        g_warning ("Unable to create dir %s: %s", collection->local_dir, g_strerror(errno));

    load_single_dir (collection, collection->local_dir, TRUE);
    load_single_dir (collection, collection->system_dir, FALSE);
    gal_view_collection_changed (collection);

    collection->loaded = TRUE;
}

/**
 * gal_view_collection_save
 * @collection: The view collection to save information for
 *
 * Saves the data to the user directory specified in set storage
 * directories.  This is primarily for internal use by other parts of
 * gal_view.
 */
void
gal_view_collection_save              (GalViewCollection *collection)
{
    gint i;
    xmlDoc *doc;
    xmlNode *root;
    gchar *filename;

    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (collection->local_dir != NULL);

    doc = xmlNewDoc((const guchar *)"1.0");
    root = xmlNewNode(NULL, (const guchar *)"GalViewCollection");
    xmlDocSetRootElement (doc, root);

    if (collection->default_view && !collection->default_view_built_in) {
        e_xml_set_string_prop_by_name(root, (const guchar *)"default-view", collection->default_view);
    }

    for (i = 0; i < collection->view_count; i++) {
        xmlNode *child;
        GalViewCollectionItem *item;

        item = collection->view_data[i];
        if (item->ever_changed) {
            child = xmlNewChild(root, NULL, (const guchar *)"GalView", NULL);
            e_xml_set_string_prop_by_name(child, (const guchar *)"id", item->id);
            e_xml_set_string_prop_by_name(child, (const guchar *)"title", item->title);
            e_xml_set_string_prop_by_name(child, (const guchar *)"filename", item->filename);
            e_xml_set_string_prop_by_name(child, (const guchar *)"type", item->type);

            if (item->changed) {
                filename = g_build_filename (collection->local_dir, item->filename, NULL);
                gal_view_save (item->view, filename);
                g_free (filename);
            }
        }
    }
    for (i = 0; i < collection->removed_view_count; i++) {
        xmlNode *child;
        GalViewCollectionItem *item;

        item = collection->removed_view_data[i];

        child = xmlNewChild(root, NULL, (const guchar *)"GalView", NULL);
        e_xml_set_string_prop_by_name(child, (const guchar *)"id", item->id);
        e_xml_set_string_prop_by_name(child, (const guchar *)"title", item->title);
        e_xml_set_string_prop_by_name(child, (const guchar *)"type", item->type);
    }
    filename = g_build_filename(collection->local_dir, "galview.xml", NULL);
    if (e_xml_save_file (filename, doc) == -1)
        g_warning ("Unable to save view to %s - %s", filename, g_strerror(errno));
    xmlFreeDoc (doc);
    g_free (filename);
}

/**
 * gal_view_collection_get_count
 * @collection: The view collection to count
 *
 * Calculates the number of views in the given collection.
 *
 * Returns: The number of views in the collection.
 */
gint
gal_view_collection_get_count (GalViewCollection *collection)
{
    g_return_val_if_fail (collection != NULL, -1);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), -1);

    return collection->view_count;
}

/**
 * gal_view_collection_get_view
 * @collection: The view collection to query
 * @n: The view to get.
 *
 * Returns: The nth view in the collection
 */
GalView *
gal_view_collection_get_view (GalViewCollection *collection,
                  gint n)
{
    g_return_val_if_fail (collection != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), NULL);
    g_return_val_if_fail (n < collection->view_count, NULL);
    g_return_val_if_fail (n >= 0, NULL);

    return collection->view_data[n]->view;
}

/**
 * gal_view_collection_get_view_item
 * @collection: The view collection to query
 * @n: The view item to get.
 *
 * Returns: The nth view item in the collection
 */
GalViewCollectionItem *
gal_view_collection_get_view_item (GalViewCollection *collection,
                   gint n)
{
    g_return_val_if_fail (collection != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), NULL);
    g_return_val_if_fail (n < collection->view_count, NULL);
    g_return_val_if_fail (n >= 0, NULL);

    return collection->view_data[n];
}

gint
gal_view_collection_get_view_index_by_id     (GalViewCollection *collection, const gchar *view_id)
{
    gint i;
    for (i = 0; i < collection->view_count; i++) {
        if (!strcmp (collection->view_data[i]->id, view_id))
            return i;
    }
    return -1;
}

gchar *
gal_view_collection_get_view_id_by_index (GalViewCollection *collection, gint n)
{
    g_return_val_if_fail (collection != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), NULL);
    g_return_val_if_fail (n < collection->view_count, NULL);
    g_return_val_if_fail (n >= 0, NULL);

    return g_strdup (collection->view_data[n]->id);
}

void
gal_view_collection_append                   (GalViewCollection *collection,
                          GalView           *view)
{
    GalViewCollectionItem *item;

    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (view != NULL);
    g_return_if_fail (GAL_IS_VIEW (view));

    item = g_new (GalViewCollectionItem, 1);
    item->ever_changed = TRUE;
    item->changed = TRUE;
    item->built_in = FALSE;
    item->title = g_strdup (gal_view_get_title (view));
    item->type = g_strdup (gal_view_get_type_code (view));
    item->id = gal_view_generate_id (collection, view);
    item->filename = g_strdup_printf("%s.galview", item->id);
    item->view = view;
    item->collection = collection;
    g_object_ref (view);

    item->view_changed_id =
        g_signal_connect(item->view, "changed",
                 G_CALLBACK (view_changed), item);

    collection->view_data = g_renew (GalViewCollectionItem *, collection->view_data, collection->view_count + 1);
    collection->view_data[collection->view_count] = item;
    collection->view_count++;

    gal_view_collection_changed (collection);
}

void
gal_view_collection_delete_view              (GalViewCollection *collection,
                          gint                i)
{
    GalViewCollectionItem *item;

    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (i >= 0 && i < collection->view_count);

    item = collection->view_data[i];
    memmove (collection->view_data + i, collection->view_data + i + 1, (collection->view_count - i - 1) * sizeof (GalViewCollectionItem *));
    collection->view_count--;
    if (item->built_in) {
        g_free (item->filename);
        item->filename = NULL;

        collection->removed_view_data = g_renew (GalViewCollectionItem *, collection->removed_view_data, collection->removed_view_count + 1);
        collection->removed_view_data[collection->removed_view_count] = item;
        collection->removed_view_count++;
    } else {
        gal_view_collection_item_free (item);
    }

    gal_view_collection_changed (collection);
}

void
gal_view_collection_copy_view                (GalViewCollection *collection,
                          gint                i)
{
    GalViewCollectionItem *item;
    GalView *view;

    g_return_if_fail (collection != NULL);
    g_return_if_fail (GAL_IS_VIEW_COLLECTION (collection));
    g_return_if_fail (i >= 0 && i < collection->view_count);

    view = collection->view_data[i]->view;

    item = g_new (GalViewCollectionItem, 1);
    item->ever_changed = TRUE;
    item->changed = FALSE;
    item->built_in = FALSE;
    item->title = g_strdup (gal_view_get_title (view));
    item->type = g_strdup (gal_view_get_type_code (view));
    item->id = gal_view_generate_id (collection, view);
    item->filename = g_strdup_printf("%s.galview", item->id);
    item->view = gal_view_clone (view);
    item->collection = collection;

    item->view_changed_id =
        g_signal_connect(item->view, "changed",
                 G_CALLBACK (view_changed), item);

    collection->view_data = g_renew (GalViewCollectionItem *, collection->view_data, collection->view_count + 1);
    collection->view_data[collection->view_count] = item;
    collection->view_count++;

    gal_view_collection_changed (collection);
}

gboolean
gal_view_collection_loaded (GalViewCollection *collection)
{
    return collection->loaded;
}

const gchar *
gal_view_collection_append_with_title (GalViewCollection *collection, const gchar *title, GalView *view)
{
    GalViewCollectionItem *item;

    g_return_val_if_fail (collection != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), NULL);
    g_return_val_if_fail (view != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW (view), NULL);

    gal_view_set_title (view, title);

    d(g_print("%s: %p\n", G_STRFUNC, view));

    item = g_new (GalViewCollectionItem, 1);
    item->ever_changed = TRUE;
    item->changed = TRUE;
    item->built_in = FALSE;
    item->title = g_strdup (gal_view_get_title (view));
    item->type = g_strdup (gal_view_get_type_code (view));
    item->id = gal_view_generate_id (collection, view);
    item->filename = g_strdup_printf("%s.galview", item->id);
    item->view = view;
    item->collection = collection;
    g_object_ref (view);

    item->view_changed_id =
        g_signal_connect(item->view, "changed",
                 G_CALLBACK (view_changed), item);

    collection->view_data = g_renew (GalViewCollectionItem *, collection->view_data, collection->view_count + 1);
    collection->view_data[collection->view_count] = item;
    collection->view_count++;

    gal_view_collection_changed (collection);
    return item->id;
}

const gchar *
gal_view_collection_set_nth_view (GalViewCollection *collection, gint i, GalView *view)
{
    GalViewCollectionItem *item;

    g_return_val_if_fail (collection != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW_COLLECTION (collection), NULL);
    g_return_val_if_fail (view != NULL, NULL);
    g_return_val_if_fail (GAL_IS_VIEW (view), NULL);
    g_return_val_if_fail (i >= 0, NULL);
    g_return_val_if_fail (i < collection->view_count, NULL);

    d(g_print("%s: %p\n", G_STRFUNC, view));

    item = collection->view_data[i];

    gal_view_set_title (view, item->title);
    g_object_ref (view);
    if (item->view) {
        g_signal_handler_disconnect (item->view,
                         item->view_changed_id);
        g_object_unref (item->view);
    }
    item->view = view;

    item->ever_changed = TRUE;
    item->changed = TRUE;
    item->type = g_strdup (gal_view_get_type_code (view));

    item->view_changed_id =
        g_signal_connect(item->view, "changed",
                 G_CALLBACK (view_changed), item);

    gal_view_collection_changed (collection);
    return item->id;
}

const gchar *
gal_view_collection_get_default_view (GalViewCollection *collection)
{
    return collection->default_view;
}

void
gal_view_collection_set_default_view (GalViewCollection *collection, const gchar *id)
{
    g_free (collection->default_view);
    collection->default_view = g_strdup (id);
    gal_view_collection_changed (collection);
    collection->default_view_built_in = FALSE;
}