File: file.c

package info (click to toggle)
gwyddion 2.47-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,180 kB
  • ctags: 37,276
  • sloc: ansic: 317,531; sh: 5,122; xml: 3,583; python: 3,067; makefile: 2,694; cpp: 2,550; pascal: 418; perl: 154; ruby: 130; sed: 17
file content (735 lines) | stat: -rw-r--r-- 24,888 bytes parent folder | download
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
/*
 *  @(#) $Id: file.c 19229 2016-11-10 16:28:18Z yeti-dn $
 *  Copyright (C) 2003-2006 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <string.h>
#include <libgwyddion/gwymacros.h>
#include <libgwymodule/gwymodule-file.h>
#include <libgwydgets/gwydgetutils.h>
#include <gtk/gtk.h>
#include <app/gwyapp.h>
#include "gwyappfilechooser.h"
#include "gwyappinternal.h"

typedef struct {
    const gchar *funcname;
    const gchar *modname;
    const GwyModuleInfo *modinfo;
} FindFileFuncModuleData;

static void          warn_broken_load_func     (const gchar *name,
                                                GwyContainer *data);
static GwyContainer* gwy_app_file_load_real    (const gchar *filename_utf8,
                                                const gchar *filename_sys,
                                                const gchar *name,
                                                gboolean do_add_loaded);
static void          gwy_app_file_add_loaded   (GwyContainer *data,
                                                const gchar *filename_utf8,
                                                const gchar *filename_sys);
static void          gwy_app_file_open_or_merge(gboolean merge);

static gchar *current_dir = NULL;

/*** Current directory handling ********************************************/

/**
 * gwy_app_get_current_directory:
 *
 * Returns what the app uses as `current directory'.
 *
 * Warning: This function is probably temporary.
 *
 * Returns: A string in GLib file name encoding that should not be modified
 *          neither freed, valid only until next call to
 *          gwy_app_set_current_directory().  It ends with a
 *          %G_DIR_SEPARATOR_S.
 **/
const gchar*
gwy_app_get_current_directory(void)
{
    if (!current_dir) {
        gchar *s = g_get_current_dir();

        gwy_app_set_current_directory(s);
        g_free(s);
    }

    return current_dir;
}

/**
 * gwy_app_set_current_directory:
 * @directory: The directory to set, or a filename to take directory part
 *             from, it must be an absolute path.  In GLib file name encoding.
 *
 * Sets what the app should use as `current directory'.
 *
 * Warning: This function is probably temporary.
 **/
void
gwy_app_set_current_directory(const gchar *directory)
{
    g_return_if_fail(directory);

    if (g_file_test(directory, G_FILE_TEST_IS_DIR)) {
        g_free(current_dir);
        if (g_str_has_suffix(directory, G_DIR_SEPARATOR_S))
            current_dir = g_strdup(directory);
        else
            current_dir = g_strconcat(directory, G_DIR_SEPARATOR_S, NULL);
    }
    else if (g_file_test(directory, G_FILE_TEST_EXISTS)) {
        gchar *s;

        g_free(current_dir);
        s = g_path_get_dirname(directory);
        /* "/" */
        if (g_str_has_suffix(s, G_DIR_SEPARATOR_S))
            current_dir = s;
        else {
            current_dir = g_strconcat(s, G_DIR_SEPARATOR_S, NULL);
            g_free(s);
        }
    }
    else
        g_warning("Invalid or nonexistent directory `%s'", directory);
}

/*** File loading **********************************************************/

/**
 * gwy_app_file_load:
 * @filename_utf8: Name of file to load, in UTF-8.
 * @filename_sys: Name of file to load, in GLib encoding.
 * @name: File type to open file as, but normally %NULL to automatically
 *        detect from file contents.
 *
 * Loads a file into application (a high-level function).
 *
 * At least one of @filename_utf8, @filename_sys must be non-%NULL.
 *
 * The file is loaded in interactive mode, modules can ask for user input.
 * Upon a successful load all necessary setup tasks are performed.  If the
 * load fails, an error dialog is presented.
 *
 * Returns: Container of the just loaded file on success, %NULL on failure.
 **/
GwyContainer*
gwy_app_file_load(const gchar *filename_utf8,
                  const gchar *filename_sys,
                  const gchar *name)
{
    GwyContainer *data;

    data = gwy_app_file_load_real(filename_utf8, filename_sys, name, TRUE);
    /* gwy_app_file_add_loaded() takes a reference therefore we can release
     * the initial one */
    if (data) {
        g_object_unref(data);
        warn_broken_load_func(name, data);
    }

    return data;
}

static void
find_file_func_author(gpointer hkey, gpointer hvalue, gpointer user_data)
{
    const gchar *modname = (const gchar*)hkey;
    const GwyModuleInfo *modinfo = (const GwyModuleInfo*)hvalue;
    FindFileFuncModuleData *ffdata = (FindFileFuncModuleData*)user_data;
    GSList *l;

    l = gwy_module_get_functions(modname);
    while (l) {
        const gchar *funcname = (const gchar*)l->data;
        if (g_str_has_prefix(funcname, "file::")
            && gwy_strequal(funcname + sizeof("file::") - 1,
                            ffdata->funcname)) {
            ffdata->modname = modname;
            ffdata->modinfo = modinfo;
            return;
        }
        l = g_slist_next(l);
    }
}

static void
warn_broken_load_func(const gchar *name,
                      GwyContainer *data)
{
    static const gchar *broken_file_funcs[] = {
        "ambfile", "dimensionfile",
        /*"at present no modules are broken and this is not a file type name",*/
    };

    FindFileFuncModuleData ffdata;
    const gchar *description;
    GtkWidget *dialog;
    guint i;

    if (!name) {
        g_return_if_fail(GWY_IS_CONTAINER(data));
        gwy_file_get_data_info(data, &name, NULL);
        g_return_if_fail(name);
    }

    for (i = 0; i < G_N_ELEMENTS(broken_file_funcs); i++) {
        if (gwy_strequal(name, broken_file_funcs[i]))
            break;
    }
    if (i == G_N_ELEMENTS(broken_file_funcs))
        return;

    description = gwy_file_func_get_description(name);
    gwy_clear(&ffdata, 1);
    ffdata.funcname = name;
    gwy_module_foreach(find_file_func_author, &ffdata);
    g_return_if_fail(ffdata.modinfo && ffdata.modname);

    dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING,
                                    GTK_BUTTONS_OK,
                                    _("Imported data are likely incorrect."));

    gtk_message_dialog_format_secondary_text
        (GTK_MESSAGE_DIALOG(dialog),
         _("Import support for files of type\n\n"
           "%s\n\n"
           "is incomplete due to the lack of documentation, "
           "testing files and/or people willing to help with the testing.\n\n"
           "If you can help to improve the import please contact the "
           "author of module %s-%s:\n\n"
           "%s"),
       _(description), ffdata.modname, ffdata.modinfo->version,
       ffdata.modinfo->author);

    g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), NULL);
    gtk_widget_show_all(dialog);
    gtk_window_present(GTK_WINDOW(dialog));
}

static GwyContainer*
gwy_app_file_load_real(const gchar *filename_utf8,
                       const gchar *filename_sys,
                       const gchar *name,
                       gboolean do_add_loaded)
{
    GtkWidget *dialog;
    gboolean free_utf8 = FALSE, free_sys = FALSE;
    GwyContainer *data;
    GError *err = NULL;

    g_return_val_if_fail(filename_utf8 || filename_sys, NULL);
    if (!filename_sys) {
        filename_sys = g_filename_from_utf8(filename_utf8, -1,
                                            NULL, NULL, NULL);
        if (!filename_sys) {
            g_warning("FIXME: file name not convertible to system encoding");
            return NULL;
        }
        free_sys = TRUE;
    }

    if (!filename_utf8) {
        filename_utf8 = g_filename_to_utf8(filename_sys, -1,
                                           NULL, NULL, NULL);
        free_utf8 = TRUE;
    }

    _gwy_app_log_start_message_capture();
    if (name)
        data = gwy_file_func_run_load(name, filename_sys,
                                      GWY_RUN_INTERACTIVE, &err);
    else
        data = gwy_file_load_with_func(filename_sys, GWY_RUN_INTERACTIVE,
                                       &name, &err);

    if (data) {
        gwy_data_validate(data,
                          GWY_DATA_VALIDATE_CORRECT
                          | GWY_DATA_VALIDATE_NO_REPORT);
        if (do_add_loaded)
            gwy_app_file_add_loaded(data, filename_utf8, filename_sys);
    }
    else {
        if (do_add_loaded)
            _gwy_app_log_discard_captured_messages();

        if (err && !g_error_matches(err,
                                    GWY_MODULE_FILE_ERROR,
                                    GWY_MODULE_FILE_ERROR_CANCELLED)) {
            gchar *filename_disp = g_filename_display_basename(filename_utf8);
            GString *message = g_string_new(NULL);

            dialog = gtk_message_dialog_new(NULL, 0,
                                            GTK_MESSAGE_ERROR,
                                            GTK_BUTTONS_CLOSE,
                                            _("Opening of `%s' failed"),
                                            filename_disp);
            g_free(filename_disp);
            g_string_append(message, err->message);
            g_string_append(message, "\n\n");
            g_string_append_printf(message,
                                   _("Full file path: %s."), filename_utf8);
            if (name) {
                g_string_append(message, "\n\n");
                g_string_append_printf(message, _("Loaded using: %s."), name);
            }
            gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
                                                     "%s", message->str);
            g_string_free(message, TRUE);
            g_signal_connect(dialog, "response",
                             G_CALLBACK(gtk_widget_destroy), NULL);
            gtk_widget_show_all(dialog);
            gtk_window_present(GTK_WINDOW(dialog));
            g_clear_error(&err);
        }
        else if (!err)
            g_warning("A file module failed to report error properly.");
    }

    if (free_sys)
        g_free((gpointer)filename_sys);
    if (free_utf8)
        g_free((gpointer)filename_utf8);

    return data;
}

static void
set_filename(GwyContainer *data, const gchar *filename)
{
    GQuark quark = g_quark_from_string("/filename");

    if (gwy_container_contains(data, quark))
        return;

    gwy_container_set_const_string(data, quark, (const guchar*)filename);
}

static void
gwy_app_file_add_loaded(GwyContainer *data,
                        const gchar *filename_utf8,
                        const gchar *filename_sys)
{
    if (filename_utf8)
        set_filename(data, filename_utf8);

    gwy_app_data_browser_add(data);
    gwy_app_data_browser_reset_visibility(data,
                                          GWY_VISIBILITY_RESET_DEFAULT);
    _gwy_app_data_browser_add_messages(data);

    if (filename_utf8)
        gwy_app_recent_file_list_update(data, filename_utf8, filename_sys, 0);
    gwy_app_set_current_directory(filename_sys);
}

/**
 * gwy_app_file_open:
 *
 * Opens a user-selected file (very high-level app function).
 **/
void
gwy_app_file_open(void)
{
    gwy_app_file_open_or_merge(FALSE);
}

/**
 * gwy_app_file_merge:
 *
 * Merges a user-selected file (very high-level app function).
 *
 * Since: 2.7
 **/
void
gwy_app_file_merge(void)
{
    gwy_app_file_open_or_merge(TRUE);
}

static void
gwy_app_file_open_or_merge(gboolean merge)
{
    GtkWidget *dialog;
    GSList *filenames = NULL, *l;
    gchar *name;
    gint response;

    dialog = _gwy_app_file_chooser_get(GTK_FILE_CHOOSER_ACTION_OPEN);
    if (merge)
        gtk_window_set_title(GTK_WINDOW(dialog), _("Merge File"));
    else
        gtk_window_set_title(GTK_WINDOW(dialog), _("Open File"));

    /* XXX: UTF-8 conversion missing */
    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
                                        gwy_app_get_current_directory());

    response = gtk_dialog_run(GTK_DIALOG(dialog));
    name = _gwy_app_file_chooser_get_selected_type(GWY_APP_FILE_CHOOSER(dialog));
    if (response == GTK_RESPONSE_OK)
        filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
    gtk_widget_hide(dialog);

    for (l = filenames; l; l = g_slist_next(l)) {
        gchar *fname_sys = (gchar*)l->data;

        if (merge) {
            GwyContainer *data;

            data = gwy_app_file_load_real(NULL, fname_sys, name, FALSE);
            if (data) {
                GwyContainer *current_data;

                gwy_app_data_browser_merge(data);
                warn_broken_load_func(name, data);
                g_object_unref(data);

                gwy_app_data_browser_get_current(GWY_APP_CONTAINER,
                                                 &current_data,
                                                 NULL);
                _gwy_app_data_browser_add_messages(current_data);
            }
            else
                _gwy_app_log_discard_captured_messages();
        }
        else
            gwy_app_file_load(NULL, fname_sys, name);

        g_free(fname_sys);
    }
    g_slist_free(filenames);
    g_free(name);
}

/*** File saving ***********************************************************/

/**
 * gwy_app_file_write:
 * @data: Data to write.
 * @filename_utf8: Name of file to write data to, in UTF-8.
 * @filename_sys: Name of file to write data to, in GLib encoding.
 * @name: File type to save file as, but normally %NULL to automatically detect
 *        from file name.
 *
 * Writes container to a file (a high-level function).
 *
 * At least one of @filename_utf8, @filename_sys must be non-%NULL.
 *
 * The file is saved in interactive mode, modules can ask for user input.
 * If the write fails, an error dialog is presented.
 *
 * Returns: %TRUE on success.
 **/
gboolean
gwy_app_file_write(GwyContainer *data,
                   const gchar *filename_utf8,
                   const gchar *filename_sys,
                   const gchar *name)
{
    GwyContainer *container;
    GtkWidget *dialog;
    gboolean free_utf8 = FALSE, free_sys = FALSE;
    GwyFileOperationType saveok;
    GError *err = NULL;
    gint id;

    /* If the @data is the current container, make thumbnail from the current
     * channel.
     * FIXME: Needs new data browser functions to do this less hackishly. */
    gwy_app_data_browser_get_current(GWY_APP_CONTAINER, &container,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);
    if (data != container)
        id = 0;

    g_return_val_if_fail(filename_utf8 || filename_sys, FALSE);
    if (!filename_sys) {
        filename_sys = g_filename_from_utf8(filename_utf8, -1,
                                            NULL, NULL, NULL);
        if (!filename_sys) {
            g_warning("FIXME: file name not convertible to system encoding");
            return FALSE;
        }
        free_sys = TRUE;
    }

    if (!filename_utf8) {
        filename_utf8 = g_filename_to_utf8(filename_sys, -1,
                                           NULL, NULL, NULL);
        free_utf8 = TRUE;
    }

    if (name) {
        saveok = gwy_file_func_get_operations(name);
        if (saveok & GWY_FILE_OPERATION_SAVE
            && gwy_file_func_run_save(name, data, filename_sys,
                                      GWY_RUN_INTERACTIVE, &err))
            saveok = GWY_FILE_OPERATION_SAVE;
        else if (saveok & GWY_FILE_OPERATION_EXPORT
                 && gwy_file_func_run_export(name, data, filename_sys,
                                             GWY_RUN_INTERACTIVE, &err))
            saveok = GWY_FILE_OPERATION_EXPORT;
        else
            saveok = 0;
    }
    else
        saveok = gwy_file_save_with_func(data, filename_sys,
                                         GWY_RUN_INTERACTIVE, &name, &err);

    switch (saveok) {
        case GWY_FILE_OPERATION_SAVE:
        gwy_undo_container_set_unmodified(data);
        set_filename(data, filename_utf8);
        gwy_app_recent_file_list_update(data, filename_utf8, filename_sys, id);

        case GWY_FILE_OPERATION_EXPORT:
        gwy_app_set_current_directory(filename_sys);
        break;

        default:
        if (err && !g_error_matches(err,
                                    GWY_MODULE_FILE_ERROR,
                                    GWY_MODULE_FILE_ERROR_CANCELLED)) {
            gchar *filename_disp = g_filename_display_basename(filename_utf8);
            GString *message = g_string_new(NULL);

            dialog = gtk_message_dialog_new(NULL, 0,
                                            GTK_MESSAGE_ERROR,
                                            GTK_BUTTONS_CLOSE,
                                            _("Saving of `%s' failed"),
                                            filename_disp);
            g_free(filename_disp);
            g_string_append(message, err->message);
            g_string_append(message, "\n\n");
            g_string_append_printf(message,
                                   _("Full file path: %s."), filename_utf8);
            if (name) {
                g_string_append(message, "\n\n");
                g_string_append_printf(message, _("Saved using: %s."), name);
            }
            gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
                                                     "%s", message->str);
            g_string_free(message, TRUE);
            g_signal_connect(dialog, "response",
                             G_CALLBACK(gtk_widget_destroy), NULL);
            gtk_widget_show_all(dialog);
            gtk_window_present(GTK_WINDOW(dialog));
            g_clear_error(&err);
        }
        else if (!err)
            g_warning("A file module failed to report error properly.");
        break;
    }

    if (free_sys)
        g_free((gpointer)filename_sys);
    if (free_utf8)
        g_free((gpointer)filename_utf8);

    return saveok != 0;
}

/**
 * gwy_app_file_save:
 *
 * Saves current data to a file (very high-level app function).
 *
 * May fall back to gwy_app_file_save_as() when current data has no file name
 * associated with it, or the format it was loaded from is not saveable.
 **/
void
gwy_app_file_save(void)
{
    GwyFileOperationType saveops;
    const gchar *name, *filename_sys;
    GwyContainer *data;

    gwy_app_data_browser_get_current(GWY_APP_CONTAINER, &data, 0);
    g_return_if_fail(data);
    gwy_file_get_data_info(data, &name, &filename_sys);
    if (!name || !filename_sys) {
        gwy_app_file_save_as();
        return;
    }
    saveops = gwy_file_func_get_operations(name);
    if (!(saveops & (GWY_FILE_OPERATION_SAVE | GWY_FILE_OPERATION_EXPORT))) {
        gwy_app_file_save_as();
        return;
    }

    gwy_app_file_write(data, NULL, filename_sys, name);
}

/**
 * gwy_app_file_save_as:
 *
 * Saves current data to a user-selected file (very high-level app function).
 **/
void
gwy_app_file_save_as(void)
{
    GtkWidget *dialog;
    gchar *name = NULL, *filename_sys = NULL, *filename_utf8;
    gint response;
    GwyContainer *data;

    gwy_app_data_browser_get_current(GWY_APP_CONTAINER, &data, 0);
    g_return_if_fail(data);
    gwy_file_get_data_info(data, NULL, (const gchar**)&filename_sys);

    dialog = _gwy_app_file_chooser_get(GTK_FILE_CHOOSER_ACTION_SAVE);
    /* XXX: UTF-8 conversion missing */
    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
                                        gwy_app_get_current_directory());
    if (filename_sys) {
        filename_sys = g_path_get_basename(filename_sys);
        filename_utf8 = g_filename_to_utf8(filename_sys, -1, NULL, NULL, NULL);
        gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog),
                                          filename_utf8);
        g_free(filename_utf8);
        g_free(filename_sys);
    }

    filename_sys = NULL;
    while (TRUE) {
        response = gtk_dialog_run(GTK_DIALOG(dialog));
        name = _gwy_app_file_chooser_get_selected_type(GWY_APP_FILE_CHOOSER(dialog));
        if (response != GTK_RESPONSE_OK)
            break;

        filename_sys = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        if (filename_sys && gwy_app_file_confirm_overwrite(dialog)) {
            gwy_app_file_write(data, NULL, filename_sys, name);
            break;
        }
        g_free(filename_sys);
        filename_sys = NULL;
    }
    gtk_widget_hide(dialog);
    g_free(filename_sys);
    g_free(name);
}

/**
 * gwy_app_file_close:
 *
 * Closes the current file file (very high-level app function).
 *
 * Since: 2.45
 **/
void
gwy_app_file_close(void)
{
    GwyContainer *current_data;

    gwy_app_data_browser_get_current(GWY_APP_CONTAINER,
                                     &current_data,
                                     NULL);
    g_return_if_fail(current_data);
    gwy_app_data_browser_remove(current_data);
}

/**
 * gwy_app_file_confirm_overwrite:
 * @chooser: A file chooser for save action.
 *
 * Asks for file overwrite for a file save chooser.
 *
 * Returns: %TRUE if it is OK to overwrite the file, %FALSE when user cancelled
 *          it or there was other problem.
 **/
gboolean
gwy_app_file_confirm_overwrite(GtkWidget *chooser)
{
    GtkWidget *dialog, *toplevel;
    gchar *filename_sys, *filename_utf8, *dirname_sys, *dirname_utf8,
          *fullname_sys;
    gint response;

    g_return_val_if_fail(gtk_file_chooser_get_action(GTK_FILE_CHOOSER(chooser))
                         == GTK_FILE_CHOOSER_ACTION_SAVE,
                         FALSE);
    fullname_sys = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
    if (!fullname_sys)
        return FALSE;
    if (!g_file_test(fullname_sys, G_FILE_TEST_EXISTS))
        return TRUE;

    filename_sys = g_path_get_basename(fullname_sys);
    dirname_sys = g_path_get_dirname(fullname_sys);
    filename_utf8 = g_filename_to_utf8(filename_sys, -1, NULL, NULL, NULL);
    dirname_utf8 = g_filename_to_utf8(dirname_sys, -1, NULL, NULL, NULL);

    toplevel = gtk_widget_get_toplevel(chooser);
    dialog = gtk_message_dialog_new(GTK_WINDOW(toplevel),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_QUESTION,
                                    GTK_BUTTONS_YES_NO,
                                    _("File `%s' already exists.  Replace?"),
                                    filename_utf8);
    gtk_message_dialog_format_secondary_text
        (GTK_MESSAGE_DIALOG(dialog),
         _("The file already exists in `%s'.  "
           "Replacing it will overwrite its contents."),
         dirname_utf8);
    gtk_window_set_title(GTK_WINDOW(dialog), _("Replace File?"));

    g_free(fullname_sys);
    g_free(filename_sys);
    g_free(dirname_sys);
    g_free(filename_utf8);
    g_free(dirname_utf8);

    response = gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);

    return response == GTK_RESPONSE_YES;
}

/************************** Documentation ****************************/

/**
 * SECTION:file
 * @title: file
 * @short_description: High level file functions
 *
 * High-level functions gwy_app_file_load() and gwy_app_file_write()
 * wrap low-level file handling functions from
 * <link linkend="libgwymodule-gwymodule-file">gwymodule-file</link>
 * and handle registration of loaded containers in data browser, showing
 * windows, remembering file types, or the differenced between save and export.
 *
 * They are complemented by application-level functions gwy_app_file_open(),
 * gwy_app_file_save(), and gwy_app_file_save_as() that perform the
 * corresponding operations of File menu.  These are probably not of general
 * interest.
 *
 * Beside that, functions to maintain application-level idea of current
 * directory are provided: gwy_app_get_current_directory(),
 * gwy_app_set_current_directory().  They should be used in place of system
 * chdir() which has various unwanted side-effect, like change of the directory
 * where core is dumped on segfault.
 **/

/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */