aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/gal-view-instance.c
blob: 32f8a5fd26f7a9136ca9945aa3f9c73ca3054803 (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
/*
 *
 * 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 "gal-view-instance.h"

#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>

#include <gtk/gtk.h>
#include <libxml/parser.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>

#include <libedataserver/libedataserver.h>

#include "e-unicode.h"
#include "e-xml-utils.h"
#include "gal-view-instance-save-as-dialog.h"

G_DEFINE_TYPE (GalViewInstance, gal_view_instance, G_TYPE_OBJECT)

#define d(x)

enum {
    DISPLAY_VIEW,
    CHANGED,
    LOADED,
    LAST_SIGNAL
};

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

static void
gal_view_instance_changed (GalViewInstance *instance)
{
    g_return_if_fail (instance != NULL);
    g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));

    g_signal_emit (
        instance,
        gal_view_instance_signals[CHANGED], 0);
}

static void
gal_view_instance_display_view (GalViewInstance *instance,
                                GalView *view)
{
    g_return_if_fail (instance != NULL);
    g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));

    g_signal_emit (
        instance,
        gal_view_instance_signals[DISPLAY_VIEW], 0,
        view);
}

static void
save_current_view (GalViewInstance *instance)
{
    xmlDoc *doc;
    xmlNode *root;

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

    if (instance->current_id)
        e_xml_set_string_prop_by_name (root, (const guchar *)"current_view", instance->current_id);
    if (instance->current_type)
        e_xml_set_string_prop_by_name (root, (const guchar *)"current_view_type", instance->current_type);

    if (e_xml_save_file (instance->current_view_filename, doc) == -1)
        g_warning ("Unable to save view to %s - %s", instance->current_view_filename, g_strerror (errno));
    xmlFreeDoc (doc);
}

static void
view_changed (GalView *view,
              GalViewInstance *instance)
{
    if (instance->current_id != NULL) {
        g_free (instance->current_id);
        instance->current_id = NULL;
        save_current_view (instance);
        gal_view_instance_changed (instance);
    }

    gal_view_save (view, instance->custom_filename);
}

static void
disconnect_view (GalViewInstance *instance)
{
    if (instance->current_view) {
        if (instance->view_changed_id) {
            g_signal_handler_disconnect (
                instance->current_view,
                instance->view_changed_id);
        }

        g_object_unref (instance->current_view);
    }
    g_free (instance->current_type);
    g_free (instance->current_title);
    instance->current_title = NULL;
    instance->current_type = NULL;
    instance->view_changed_id = 0;
    instance->current_view = NULL;
}

static void
connect_view (GalViewInstance *instance,
              GalView *view)
{
    if (instance->current_view)
        disconnect_view (instance);
    instance->current_view = g_object_ref (view);

    instance->current_title = g_strdup (gal_view_get_title (view));
    instance->current_type = g_strdup (gal_view_get_type_code (view));
    instance->view_changed_id = g_signal_connect (
        instance->current_view, "changed",
        G_CALLBACK (view_changed), instance);

    gal_view_instance_display_view (instance, instance->current_view);
}

static void
gal_view_instance_dispose (GObject *object)
{
    GalViewInstance *instance = GAL_VIEW_INSTANCE (object);

    if (instance->collection) {
        if (instance->collection_changed_id) {
            g_signal_handler_disconnect (
                instance->collection,
                instance->collection_changed_id);
        }
        g_object_unref (instance->collection);
    }

    g_free (instance->instance_id);
    g_free (instance->custom_filename);
    g_free (instance->current_view_filename);

    g_free (instance->current_id);
    disconnect_view (instance);

    g_free (instance->default_view);

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

static void
gal_view_instance_class_init (GalViewInstanceClass *class)
{
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    object_class->dispose = gal_view_instance_dispose;

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

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

    gal_view_instance_signals[LOADED] = g_signal_new (
        "loaded",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (GalViewInstanceClass, loaded),
        NULL, NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

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

static void
gal_view_instance_init (GalViewInstance *instance)
{
    instance->collection            = NULL;

    instance->instance_id           = NULL;
    instance->custom_filename       = NULL;
    instance->current_view_filename = NULL;

    instance->current_title         = NULL;
    instance->current_type          = NULL;
    instance->current_id            = NULL;
    instance->current_view          = NULL;

    instance->view_changed_id       = 0;
    instance->collection_changed_id       = 0;

    instance->loaded = FALSE;
    instance->default_view = NULL;
}

static void
collection_changed (GalView *view,
                    GalViewInstance *instance)
{
    if (instance->current_id) {
        gchar *view_id = instance->current_id;
        instance->current_id = NULL;
        gal_view_instance_set_current_view_id (instance, view_id);
        g_free (view_id);
    }
}

static void
load_current_view (GalViewInstance *instance)
{
    xmlDoc *doc = NULL;
    xmlNode *root;
    GalView *view = NULL;

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

    if (doc == NULL) {
        instance->current_id = g_strdup (gal_view_instance_get_default_view (instance));

        if (instance->current_id) {
            gint index = gal_view_collection_get_view_index_by_id (
                instance->collection,
                instance->current_id);

            if (index != -1) {
                view = gal_view_collection_get_view (
                    instance->collection, index);
                view = gal_view_clone (view);
                connect_view (instance, view);
            }
        }
        return;
    }

    root = xmlDocGetRootElement (doc);
    instance->current_id = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view", NULL);

    if (instance->current_id != NULL) {
        gint index = gal_view_collection_get_view_index_by_id (
            instance->collection,
            instance->current_id);

        if (index != -1) {
            view = gal_view_collection_get_view (
                instance->collection, index);
            view = gal_view_clone (view);
        }
    }
    if (view == NULL) {
        gchar *type;
        type = e_xml_get_string_prop_by_name_with_default (root, (const guchar *)"current_view_type", NULL);
        view = gal_view_collection_load_view_from_file (
            instance->collection, type,
            instance->custom_filename);
        g_free (type);
    }

    connect_view (instance, view);

    xmlFreeDoc (doc);
}

/**
 * gal_view_instance_new:
 * @collection: This %GalViewCollection should be loaded before being passed to this function.
 * @instance_id: Which instance of this type of object is this (for most of evo, this is the folder id.)
 *
 * Create a new %GalViewInstance.
 *
 * Return value: The new %GalViewInstance.
 **/
GalViewInstance *
gal_view_instance_new (GalViewCollection *collection,
                       const gchar *instance_id)
{
    GalViewInstance *instance = g_object_new (GAL_TYPE_VIEW_INSTANCE, NULL);
    if (gal_view_instance_construct (instance, collection, instance_id))
        return instance;
    else {
        g_object_unref (instance);
        return NULL;
    }
}

GalViewInstance *
gal_view_instance_construct (GalViewInstance *instance,
                             GalViewCollection *collection,
                             const gchar *instance_id)
{
    gchar *filename;
    gchar *safe_id;

    g_return_val_if_fail (gal_view_collection_loaded (collection), NULL);

    instance->collection = collection;
    if (collection)
        g_object_ref (collection);
    instance->collection_changed_id = g_signal_connect (
        collection, "changed",
        G_CALLBACK (collection_changed), instance);

    if (instance_id)
        instance->instance_id = g_strdup (instance_id);
    else
        instance->instance_id = g_strdup ("");

    safe_id = g_strdup (instance->instance_id);
    e_filename_make_safe (safe_id);

    filename = g_strdup_printf ("custom_view-%s.xml", safe_id);
    instance->custom_filename = g_build_filename (instance->collection->local_dir, filename, NULL);
    g_free (filename);

    filename = g_strdup_printf ("current_view-%s.xml", safe_id);
    instance->current_view_filename = g_build_filename (instance->collection->local_dir, filename, NULL);
    g_free (filename);

    g_free (safe_id);

    return instance;
}

/* Manipulate the current view. */
gchar *
gal_view_instance_get_current_view_id (GalViewInstance *instance)
{
    if (instance->current_id && gal_view_collection_get_view_index_by_id (instance->collection, instance->current_id) != -1)
        return g_strdup (instance->current_id);
    else
        return NULL;
}

void
gal_view_instance_set_current_view_id (GalViewInstance *instance,
                                       const gchar *view_id)
{
    GalView *view;
    gint index;

    g_return_if_fail (instance != NULL);
    g_return_if_fail (GAL_IS_VIEW_INSTANCE (instance));

    d (g_print ("%s: view_id set to %s\n", G_STRFUNC, view_id));

    if (instance->current_id && !strcmp (instance->current_id, view_id))
        return;

    g_free (instance->current_id);
    instance->current_id = g_strdup (view_id);

    index = gal_view_collection_get_view_index_by_id (instance->collection, view_id);
    if (index != -1) {
        view = gal_view_collection_get_view (instance->collection, index);
        connect_view (instance, gal_view_clone (view));
    }

    if (instance->loaded)
        save_current_view (instance);
    gal_view_instance_changed (instance);
}

GalView *
gal_view_instance_get_current_view (GalViewInstance *instance)
{
    return instance->current_view;
}

void
gal_view_instance_set_custom_view (GalViewInstance *instance,
                                   GalView *view)
{
    g_free (instance->current_id);
    instance->current_id = NULL;

    view = gal_view_clone (view);
    connect_view (instance, view);
    gal_view_save (view, instance->custom_filename);
    save_current_view (instance);
    gal_view_instance_changed (instance);
}

static void
dialog_response (GtkWidget *dialog,
                 gint id,
                 GalViewInstance *instance)
{
    if (id == GTK_RESPONSE_OK) {
        gal_view_instance_save_as_dialog_save (GAL_VIEW_INSTANCE_SAVE_AS_DIALOG (dialog));
    }
    gtk_widget_destroy (dialog);
}

void
gal_view_instance_save_as (GalViewInstance *instance)
{
    GtkWidget *dialog;

    g_return_if_fail (instance != NULL);

    dialog = gal_view_instance_save_as_dialog_new (instance);
    g_signal_connect (
        dialog, "response",
        G_CALLBACK (dialog_response), instance);
    gtk_widget_show (dialog);
}

/* This is idempotent.  Once it's been called once, the rest of the calls are ignored. */
void
gal_view_instance_load (GalViewInstance *instance)
{
    if (!instance->loaded) {
        load_current_view (instance);
        instance->loaded = TRUE;
        g_signal_emit (instance, gal_view_instance_signals[LOADED], 0);
    }
}

/* These only mean anything before gal_view_instance_load is called the first time.  */
const gchar *
gal_view_instance_get_default_view (GalViewInstance *instance)
{
    if (instance->default_view)
        return instance->default_view;
    else
        return gal_view_collection_get_default_view (instance->collection);
}

void
gal_view_instance_set_default_view (GalViewInstance *instance,
                                    const gchar *id)
{
    g_free (instance->default_view);
    instance->default_view = g_strdup (id);
}

gboolean
gal_view_instance_exists (GalViewInstance *instance)
{
    struct stat st;

    if (instance->current_view_filename && g_stat (instance->current_view_filename, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode))
        return TRUE;
    else
        return FALSE;

}