aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-utils.c
blob: b1de7119a89e0eec5d0cc77d6e4638be1f869055 (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
/*
 * 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)
 *
 */

/**
 * SECTION: e-shell-utils
 * @short_description: high-level utilities with shell integration
 * @include: shell/e-shell-utils.h
 **/

#include "e-shell-utils.h"

#include <glib/gi18n-lib.h>

#include "widgets/misc/e-import-assistant.h"

/**
 * e_shell_configure_ui_manager:
 * @shell: an #EShell
 * @ui_manager: an #EUIManager
 *
 * Adds shell integration to @ui_manager.  In particular, it keeps
 * @ui_manager's EUIManager:express-mode property synchronized with
 * @shell's EShell:express-mode property.
 **/
void
e_shell_configure_ui_manager (EShell *shell,
                              EUIManager *ui_manager)
{
    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (E_IS_UI_MANAGER (ui_manager));

    g_object_bind_property (
        shell, "express-mode",
        ui_manager, "express-mode",
        G_BINDING_SYNC_CREATE);
}

/**
 * e_shell_configure_web_view:
 * @shell: an #EShell
 * @web_view: an #EWebView
 *
 * Adds shell integration to @web_view.  In particular, it configures
 * @web_view to honor the printing and save-to-disk lockdown options.
 **/
void
e_shell_configure_web_view (EShell *shell,
                            EWebView *web_view)
{
#if 0  /* XXX Lockdown keys have moved to gsettings-desktop-scheams,
    *     so disable lockdown integration until we're ready for
    *     GSettings. */
    EShellSettings *shell_settings;

    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (E_IS_WEB_VIEW (web_view));

    shell_settings = e_shell_get_shell_settings (shell);

#ifndef G_OS_WIN32
    g_object_bind_property (
        shell_settings, "disable-printing",
        web_view, "disable-printing",
        G_BINDING_SYNC_CREATE);

    g_object_bind_property (
        shell_settings, "disable-save-to-disk",
        web_view, "disable-save-to-disk",
        G_BINDING_SYNC_CREATE);
#endif
#endif
}

/**
 * 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
 * @filters: Possible filters for dialog, 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.
 *
 * With non-%NULL @filters will be added also file filters to the dialog.
 * The string format is "pat1:mt1;pat2:mt2:...", where 'pat' is a pattern
 * and 'mt' is a MIME type for the pattern to be used.
 * There can be more than one MIME type, those are separated by comma.
 *
 * Returns: the #GFile to save to, or %NULL
 **/
GFile *
e_shell_run_save_dialog (EShell *shell,
                         const gchar *title,
                         const gchar *suggestion,
             const gchar *filters,
                         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);

    if (filters != NULL) {
        gchar **flts = g_strsplit (filters, ";", -1);
        gint i;

        for (i = 0; flts[i]; i++) {
            GtkFileFilter *filter = gtk_file_filter_new ();
            gchar *flt = flts[i];
            gchar *delim = strchr (flt, ':'), *next = NULL;

            if (delim) {
                *delim = 0;
                next = strchr (delim + 1, ',');
            }

            gtk_file_filter_add_pattern (filter, flt);
            if (g_ascii_strcasecmp (flt, "*.mbox") == 0)
                gtk_file_filter_set_name (filter, _("Berkeley Mailbox (mbox)"));
            else if (g_ascii_strcasecmp (flt, "*.vcf") == 0)
                gtk_file_filter_set_name (filter, _("vCard (.vcf)"));
            else if (g_ascii_strcasecmp (flt, "*.ics") == 0)
                gtk_file_filter_set_name (filter, _("iCalendar (.ics)"));

            while (delim) {
                delim++;
                if (next)
                    *next = 0;

                gtk_file_filter_add_mime_type (filter, delim);

                delim = next;
                if (next)
                    next = strchr (next + 1, ',');
            }

            gtk_file_chooser_add_filter (file_chooser, filter);
        }

        if (flts && flts[0]) {
            GtkFileFilter *filter = gtk_file_filter_new ();

            gtk_file_filter_add_pattern (filter, "*");
            gtk_file_filter_set_name (filter, _("All Files (*)"));
            gtk_file_chooser_add_filter (file_chooser, filter);
        }

        g_strfreev (flts);
    }

    /* 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_utils_import_uris:
 * @shell: The #EShell instance
 * @uris: %NULL-terminated list of URIs to import
 *
 * Imports given URIs to Evolution, giving user a choice what to import
 * if more than one importer can be applied, and where to import it, if
 * the importer itself is configurable.
 *
 * URIs should be either a filename or URI of form file://.
 * All others are skipped.
 *
 * Returns: the number of URIs successfully handled
 **/
guint
e_shell_utils_import_uris (EShell *shell,
                           gchar **uris)
{
    GtkWindow *parent;
    GtkWidget *assistant;

    g_return_val_if_fail (shell != NULL, 0);
    g_return_val_if_fail (uris != NULL, 0);

    parent = e_shell_get_active_window (shell);
    assistant = e_import_assistant_new_simple (parent, uris);

    if (assistant) {
        g_signal_connect_after (
            assistant, "cancel",
            G_CALLBACK (gtk_widget_destroy), NULL);

        g_signal_connect_after (
            assistant, "finished",
            G_CALLBACK (gtk_widget_destroy), NULL);

        e_shell_watch_window (shell, GTK_WINDOW (assistant));

        gtk_widget_show (assistant);
    } else
        g_warning ("Cannot import any of the given URIs");

    return g_strv_length (uris);
}

/**
 * e_shell_hide_widgets_for_express_mode:
 * @shell: an #EShell
 * @builder: a #GtkBuilder
 * @widget_name: NULL-terminated list of strings
 *
 * If Evolution is running in Express mode (i.e. if the specified @shell is
 * in Express mode), then this function will hide a list of widgets, based
 * on their specified names.  The list of names must be NULL-terminated,
 * and each element of that list must be the name of a widget present in
 * @builder.  Those widgets will then get hidden.
 *
 * This can be used to simplify preference dialogs and such in an easy
 * fashion, for use in Express mode.
 *
 * If Evolution is not running in Express mode, this function does nothing.
 */
void
e_shell_hide_widgets_for_express_mode (EShell *shell,
                                       GtkBuilder *builder,
                                       const gchar *widget_name,
                                       ...)
{
    va_list args;

    g_return_if_fail (E_IS_SHELL (shell));
    g_return_if_fail (GTK_IS_BUILDER (builder));
    g_return_if_fail (widget_name != NULL);

    if (!e_shell_get_express_mode (shell))
        return;

    va_start (args, widget_name);

    while (widget_name != NULL) {
        GObject *object;

        object = gtk_builder_get_object (builder, widget_name);
        if (!GTK_IS_WIDGET (object)) {
            g_error (
                "Object '%s' was not found in the builder "
                "file, or it is not a GtkWidget", widget_name);
            g_assert_not_reached ();
        }

        gtk_widget_hide (GTK_WIDGET (object));

        widget_name = va_arg (args, const gchar *);
    }

    va_end (args);
}