e-shell-utils.c 7.36 KB
Newer Older
1 2 3
/*
 * e-shell-utils.c
 *
4 5 6
 * 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.
7
 *
8 9 10 11
 * 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.
12
 *
13 14
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 16 17 18 19 20
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

21 22 23 24 25 26
/**
 * SECTION: e-shell-utils
 * @short_description: high-level utilities with shell integration
 * @include: shell/e-shell-utils.h
 **/

27 28 29 30
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

31 32 33 34
#include "e-shell-utils.h"

#include <glib/gi18n-lib.h>

35
#include <libedataserver/libedataserver.h>
36

37 38 39 40 41 42 43 44
/**
 * 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
45 46 47 48
 * returns the selected #GFile.  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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 *
 * Returns: the #GFile to open, or %NULL
 **/
GFile *
e_shell_run_open_dialog (EShell *shell,
                         const gchar *title,
                         GtkCallback customize_func,
                         gpointer customize_data)
{
	GtkFileChooser *file_chooser;
	GFile *chosen_file = NULL;
	GtkWidget *dialog;
	GtkWindow *parent;

	g_return_val_if_fail (E_IS_SHELL (shell), NULL);

	parent = e_shell_get_active_window (shell);

	dialog = gtk_file_chooser_dialog_new (
		title, parent,
		GTK_FILE_CHOOSER_ACTION_OPEN,
70 71
		_("_Cancel"), GTK_RESPONSE_CANCEL,
		_("_Open"), GTK_RESPONSE_ACCEPT, NULL);
72 73 74 75 76 77 78 79 80 81 82 83

	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);

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

84 85
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
		chosen_file = gtk_file_chooser_get_file (file_chooser);
86 87 88 89 90 91

	gtk_widget_destroy (dialog);

	return chosen_file;
}

92 93 94 95
/**
 * e_shell_run_save_dialog:
 * @shell: an #EShell
 * @title: file chooser dialog title
96
 * @suggestion: file name suggestion, or %NULL
97
 * @filters: Possible filters for dialog, or %NULL
98 99 100 101
 * @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
102 103 104 105
 * returns the selected #GFile.  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.
106
 *
107 108
 * 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
109 110
 * 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.
111
 *
112 113 114 115 116
 * Returns: the #GFile to save to, or %NULL
 **/
GFile *
e_shell_run_save_dialog (EShell *shell,
                         const gchar *title,
117
                         const gchar *suggestion,
118
                         const gchar *filters,
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                         GtkCallback customize_func,
                         gpointer customize_data)
{
	GtkFileChooser *file_chooser;
	GFile *chosen_file = NULL;
	GtkWidget *dialog;
	GtkWindow *parent;

	g_return_val_if_fail (E_IS_SHELL (shell), NULL);

	parent = e_shell_get_active_window (shell);

	dialog = gtk_file_chooser_dialog_new (
		title, parent,
		GTK_FILE_CHOOSER_ACTION_SAVE,
134 135
		_("_Cancel"), GTK_RESPONSE_CANCEL,
		_("_Save"), GTK_RESPONSE_ACCEPT, NULL);
136 137 138

	file_chooser = GTK_FILE_CHOOSER (dialog);

139 140
	gtk_dialog_set_default_response (
		GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
141 142 143 144

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

145 146 147 148 149 150 151 152
	if (suggestion != NULL) {
		gchar *current_name;

		current_name = g_strdup (suggestion);
		e_filename_make_safe (current_name);
		gtk_file_chooser_set_current_name (file_chooser, current_name);
		g_free (current_name);
	}
153

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

158
		for (i = 0; flts && flts[i]; i++) {
159
			GtkFileFilter *filter = gtk_file_filter_new ();
160
			gchar *flt = flts[i];
161 162 163 164 165 166 167 168 169
			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)
Matthew Barnes's avatar
Matthew Barnes committed
170 171
				gtk_file_filter_set_name (
					filter, _("Berkeley Mailbox (mbox)"));
172
			else if (g_ascii_strcasecmp (flt, "*.vcf") == 0)
Matthew Barnes's avatar
Matthew Barnes committed
173 174
				gtk_file_filter_set_name (
					filter, _("vCard (.vcf)"));
175
			else if (g_ascii_strcasecmp (flt, "*.ics") == 0)
Matthew Barnes's avatar
Matthew Barnes committed
176 177
				gtk_file_filter_set_name (
					filter, _("iCalendar (.ics)"));
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

			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);
		}

194
		if (flts && flts[0]) {
195 196 197 198 199 200 201 202 203 204
			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);
	}

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

209 210
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
		chosen_file = gtk_file_chooser_get_file (file_chooser);
211 212 213 214 215

	gtk_widget_destroy (dialog);

	return chosen_file;
}
216 217 218 219

/**
 * e_shell_utils_import_uris:
 * @shell: The #EShell instance
220
 * @uris: %NULL-terminated list of URIs to import
221 222 223
 *
 * 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
224
 * the importer itself is configurable.
225 226 227 228 229 230 231
 *
 * URIs should be either a filename or URI of form file://.
 * All others are skipped.
 *
 * Returns: the number of URIs successfully handled
 **/
guint
232
e_shell_utils_import_uris (EShell *shell,
233
                           const gchar * const *uris)
234 235 236 237 238 239 240 241
{
	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);
242
	assistant = e_import_assistant_new_simple (parent, uris);
243 244 245 246 247 248 249 250 251 252

	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);

253 254 255
		gtk_application_add_window (
			GTK_APPLICATION (shell),
			GTK_WINDOW (assistant));
256 257

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

261
	return g_strv_length ((gchar **) uris);
262
}
263