aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-utils.c
blob: 4d4f0fd2eaea1951aac8c3fcf43f315add512c09 (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
/*
 * e-shell-utils.c
 *
 * 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/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-shell-utils.h"

#include <glib/gi18n-lib.h>

/**
 * e_shell_run_open_dialog:
 * @shell: an #EShell
 * @title: file chooser dialog title
 * @customize_func: optional dialog customization function
 * @customize_data: optional data to pass to @customize_func
 *
 * Runs a #GtkFileChooserDialog in open mode with the given title and
 * returns the selected #GFile.  It automatically remembers the selected
 * folder.  If @customize_func is provided, the function is called just
 * prior to running the dialog (the file chooser is the first argument,
 * @customize data is the second).  If the user cancels the dialog the
 * function will return %NULL.
 *
 * Returns: the #GFile to open, or %NULL
 **/
GFile *
e_shell_run_open_dialog (EShell *shell,
                         const gchar *title,
                         GtkCallback customize_func,
                         gpointer customize_data)
{
    EShellSettings *shell_settings;
    GtkFileChooser *file_chooser;
    GFile *chosen_file = NULL;
    GtkWidget *dialog;
    GtkWindow *parent;
    const gchar *property_name;
    gchar *uri;

    g_return_val_if_fail (E_IS_SHELL (shell), NULL);

    property_name = "file-chooser-folder";
    shell_settings = e_shell_get_shell_settings (shell);

    parent = e_shell_get_active_window (shell);

    dialog = gtk_file_chooser_dialog_new (
        title, parent,
        GTK_FILE_CHOOSER_ACTION_OPEN,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);

    file_chooser = GTK_FILE_CHOOSER (dialog);

    gtk_dialog_set_default_response (
        GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);

    gtk_file_chooser_set_local_only (file_chooser, FALSE);

    /* Restore the current folder from the previous file chooser. */
    uri = e_shell_settings_get_string (shell_settings, property_name);
    if (uri != NULL)
        gtk_file_chooser_set_current_folder_uri (file_chooser, uri);
    g_free (uri);

    /* Allow further customizations before running the dialog. */
    if (customize_func != NULL)
        customize_func (dialog, customize_data);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_ACCEPT)
        goto exit;

    chosen_file = gtk_file_chooser_get_file (file_chooser);

    /* Save the current folder for subsequent file choosers. */
    uri = gtk_file_chooser_get_current_folder_uri (file_chooser);
    e_shell_settings_set_string (shell_settings, property_name, uri);
    g_free (uri);

exit:
    gtk_widget_destroy (dialog);

    return chosen_file;
}

/**
 * e_shell_run_save_dialog:
 * @shell: an #EShell
 * @title: file chooser dialog title
 * @suggestion: file name suggestion, or %NULL
 * @customize_func: optional dialog customization function
 * @customize_data: optional data to pass to @customize_func
 *
 * Runs a #GtkFileChooserDialog in save mode with the given title and
 * returns the selected #GFile.  It automatically remembers the selected
 * folder.  If @customize_func is provided, the function is called just
 * prior to running the dialog (the file chooser is the first argument,
 * @customize_data is the second).  If the user cancels the dialog the
 * function will return %NULL.
 *
 * Returns: the #GFile to save to, or %NULL
 **/
GFile *
e_shell_run_save_dialog (EShell *shell,
                         const gchar *title,
                         const gchar *suggestion,
                         GtkCallback customize_func,
                         gpointer customize_data)
{
    EShellSettings *shell_settings;
    GtkFileChooser *file_chooser;
    GFile *chosen_file = NULL;
    GtkWidget *dialog;
    GtkWindow *parent;
    const gchar *property_name;
    gchar *uri;

    g_return_val_if_fail (E_IS_SHELL (shell), NULL);

    property_name = "file-chooser-folder";
    shell_settings = e_shell_get_shell_settings (shell);

    parent = e_shell_get_active_window (shell);

    dialog = gtk_file_chooser_dialog_new (
        title, parent,
        GTK_FILE_CHOOSER_ACTION_SAVE,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);

    file_chooser = GTK_FILE_CHOOSER (dialog);

    gtk_dialog_set_default_response (
        GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);

    gtk_file_chooser_set_local_only (file_chooser, FALSE);
    gtk_file_chooser_set_do_overwrite_confirmation (file_chooser, TRUE);

    /* Restore the current folder from the previous file chooser. */
    uri = e_shell_settings_get_string (shell_settings, property_name);
    if (uri != NULL)
        gtk_file_chooser_set_current_folder_uri (file_chooser, uri);
    g_free (uri);

    if (suggestion != NULL)
        gtk_file_chooser_set_current_name (file_chooser, suggestion);

    /* Allow further customizations before running the dialog. */
    if (customize_func != NULL)
        customize_func (dialog, customize_data);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_ACCEPT)
        goto exit;

    chosen_file = gtk_file_chooser_get_file (file_chooser);

    /* Save the current folder for subsequent file choosers. */
    uri = gtk_file_chooser_get_current_folder_uri (file_chooser);
    e_shell_settings_set_string (shell_settings, property_name, uri);
    g_free (uri);

exit:
    gtk_widget_destroy (dialog);

    return chosen_file;
}