gedit-file-chooser-dialog.c 10.7 KB
Newer Older
Paolo Borelli's avatar
Paolo Borelli committed
1 2 3 4
/*
 * gedit-file-chooser-dialog.c
 * This file is part of gedit
 *
5
 * Copyright (C) 2005-2007 - Paolo Maggi
Paolo Borelli's avatar
Paolo Borelli committed
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 59 Temple Place, Suite 330,
Paolo Borelli's avatar
Paolo Borelli committed
20 21
 * Boston, MA 02111-1307, USA.
 */
22

Paolo Borelli's avatar
Paolo Borelli committed
23
/*
24 25 26
 * Modified by the gedit Team, 2005-2007. See the AUTHORS file for a
 * list of people on the gedit Team.
 * See the ChangeLog files for a list of changes.
Paolo Borelli's avatar
Paolo Borelli committed
27 28 29
 *
 * $Id$
 */
30

Paolo Borelli's avatar
Paolo Borelli committed
31 32 33 34 35 36 37 38 39 40 41
/* TODO: Override set_extra_widget */
/* TODO: add encoding property */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <glib/gi18n.h>
#include <gtk/gtk.h>
42
#include <libgnomevfs/gnome-vfs-mime-utils.h>
Paolo Borelli's avatar
Paolo Borelli committed
43 44

#include "gedit-file-chooser-dialog.h"
45
#include "gedit-encodings-option-menu.h"
46
#include "gedit-language-manager.h"
47 48
#include "gedit-prefs-manager-app.h"
#include "gedit-debug.h"
Paolo Borelli's avatar
Paolo Borelli committed
49 50 51

#define GEDIT_FILE_CHOOSER_DIALOG_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_FILE_CHOOSER_DIALOG, GeditFileChooserDialogPrivate))

52 53 54
#define ALL_FILES		_("All Files")
#define ALL_TEXT_FILES		_("All Text Files")

Paolo Borelli's avatar
Paolo Borelli committed
55 56 57 58 59 60 61
struct _GeditFileChooserDialogPrivate
{
	GtkWidget *option_menu;
};

G_DEFINE_TYPE(GeditFileChooserDialog, gedit_file_chooser_dialog, GTK_TYPE_FILE_CHOOSER_DIALOG)

62
static void
Paolo Borelli's avatar
Paolo Borelli committed
63 64 65
gedit_file_chooser_dialog_class_init (GeditFileChooserDialogClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
66

Paolo Borelli's avatar
Paolo Borelli committed
67 68 69 70 71 72 73 74 75
	g_type_class_add_private (object_class, sizeof(GeditFileChooserDialogPrivate));
}

static void
create_option_menu (GeditFileChooserDialog *dialog)
{
	GtkWidget *hbox;
	GtkWidget *label;
	GtkWidget *menu;
76

Paolo Borelli's avatar
Paolo Borelli committed
77 78 79 80 81
	hbox = gtk_hbox_new (FALSE, 6);

	label = gtk_label_new_with_mnemonic (_("C_haracter Coding:"));
	menu = gedit_encodings_option_menu_new (
		gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
82 83 84

	gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);
	gtk_box_pack_start (GTK_BOX (hbox),
Paolo Borelli's avatar
Paolo Borelli committed
85 86 87 88 89
			    label,
			    FALSE,
			    FALSE,
			    0);

90
	gtk_box_pack_end (GTK_BOX (hbox),
Paolo Borelli's avatar
Paolo Borelli committed
91 92 93 94 95
			  menu,
			  TRUE,
			  TRUE,
			  0);

96
	gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog),
Paolo Borelli's avatar
Paolo Borelli committed
97 98
					   hbox);
	gtk_widget_show_all (hbox);
99

Paolo Borelli's avatar
Paolo Borelli committed
100 101 102 103 104 105 106 107 108
	dialog->priv->option_menu = menu;
}

static void
action_changed (GeditFileChooserDialog *dialog,
		GParamSpec	       *pspec,
		gpointer		data)
{
	GtkFileChooserAction action;
109

Paolo Borelli's avatar
Paolo Borelli committed
110
	action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
111 112

	switch (action)
Paolo Borelli's avatar
Paolo Borelli committed
113 114 115 116 117 118 119 120 121 122 123
	{
		case GTK_FILE_CHOOSER_ACTION_OPEN:
			g_object_set (dialog->priv->option_menu,
				      "save_mode", FALSE,
				      NULL);
			gtk_widget_show (dialog->priv->option_menu);
			break;
		case GTK_FILE_CHOOSER_ACTION_SAVE:
			g_object_set (dialog->priv->option_menu,
				      "save_mode", TRUE,
				      NULL);
124
			gtk_widget_show (dialog->priv->option_menu);
Paolo Borelli's avatar
Paolo Borelli committed
125 126 127
			break;
		default:
			gtk_widget_hide (dialog->priv->option_menu);
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
	}
}

static void
filter_changed (GeditFileChooserDialog *dialog,
		GParamSpec	       *pspec,
		gpointer		data)
{
	GtkFileFilter *filter;
	const gchar *name;
	gint id = 0;

	if (!gedit_prefs_manager_active_file_filter_can_set ())
		return;

	filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
	name = gtk_file_filter_get_name (filter);
	g_return_if_fail (name != NULL);

	if (strcmp (name, ALL_TEXT_FILES) == 0)
		id = 1;

	gedit_debug_message (DEBUG_COMMANDS, "Active filter: %s (%d)", name, id);

	gedit_prefs_manager_set_active_file_filter (id);
Paolo Borelli's avatar
Paolo Borelli committed
153 154 155 156 157 158
}

static gboolean
all_text_files_filter (const GtkFileFilterInfo *filter_info,
		       gpointer                 data)
{
159 160 161 162 163
	static GSList *known_mime_types = NULL;
	GSList *mime_types;

	if (known_mime_types == NULL)
	{
164
		GtkSourceLanguageManager *lm;
165 166
		GSList *languages;
		GSList *l;
167

168
		lm = gedit_get_language_manager ();
169
		languages = gtk_source_language_manager_list_languages (lm);
170

171
		for (l = languages; l != NULL; l = l->next)
172
		{
173 174
			gchar **mime_types;
			gint i;
175 176
			GtkSourceLanguage *lang;

177
			lang = l->data;
178

179 180 181
			mime_types = gtk_source_language_get_mime_types (lang);
			if (mime_types == NULL)
				continue;
182

183
			for (i = 0; mime_types[i] != NULL; i++)
184 185
			{
				GnomeVFSMimeEquivalence res;
186 187

				res = gnome_vfs_mime_type_get_equivalence (mime_types[i],
188 189 190 191 192
									   "text/plain");

				if (res == GNOME_VFS_MIME_UNRELATED)
				{
					gedit_debug_message (DEBUG_COMMANDS,
193 194
							     "Mime-type %s is not related to text/plain",
							     mime_types[i]);
195

196 197
					known_mime_types = g_slist_prepend (known_mime_types,
									    g_strdup (mime_types[i]));
198 199 200
				}
			}

201
			g_strfreev (mime_types);
202 203
		}

204 205
		g_slist_free (languages);

206 207 208 209 210 211 212
		/* known_mime_types always has "text/plain" as first item" */
		known_mime_types = g_slist_prepend (known_mime_types, g_strdup ("text/plain"));
	}

	/* known mime_types contains "text/plain" and then the list of mime-types unrelated to "text/plain"
	 * that gedit recognizes */

Paolo Borelli's avatar
Paolo Borelli committed
213
	if (filter_info->mime_type == NULL)
214 215 216 217 218 219 220 221 222 223
		return FALSE;

	/*
	 * The filter is matching:
	 * - the mime-types beginning with "text/"
	 * - the mime-types inheriting from a known mime-type (note the text/plain is
	 *   the first known mime-type)
	 */

	if (strncmp (filter_info->mime_type, "text/", 5) == 0)
Paolo Borelli's avatar
Paolo Borelli committed
224
		return TRUE;
225

226 227
	mime_types = known_mime_types;
	while (mime_types != NULL)
Paolo Borelli's avatar
Paolo Borelli committed
228
	{
229 230 231 232 233 234 235 236
		GnomeVFSMimeEquivalence res;
		res = gnome_vfs_mime_type_get_equivalence (filter_info->mime_type,
							   (const gchar*)mime_types->data);

		if (res != GNOME_VFS_MIME_UNRELATED)
			return TRUE;

		mime_types = g_slist_next (mime_types);
Paolo Borelli's avatar
Paolo Borelli committed
237 238 239 240 241 242 243
	}

	return FALSE;
}

static void
gedit_file_chooser_dialog_init (GeditFileChooserDialog *dialog)
244 245
{
	dialog->priv = GEDIT_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
Paolo Borelli's avatar
Paolo Borelli committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259
}

static GtkWidget *
gedit_file_chooser_dialog_new_valist (const gchar          *title,
				      GtkWindow            *parent,
				      GtkFileChooserAction  action,
				      const GeditEncoding  *encoding,
				      const gchar          *first_button_text,
				      va_list               varargs)
{
	GtkWidget *result;
	const char *button_text = first_button_text;
	gint response_id;
	GtkFileFilter *filter;
260
	gint active_filter;
Paolo Borelli's avatar
Paolo Borelli committed
261 262

	g_return_val_if_fail (parent != NULL, NULL);
263

Paolo Borelli's avatar
Paolo Borelli committed
264 265 266
	result = g_object_new (GEDIT_TYPE_FILE_CHOOSER_DIALOG,
			       "title", title,
			       "file-system-backend", NULL,
267
			       "local-only", FALSE,
Paolo Borelli's avatar
Paolo Borelli committed
268 269 270 271 272
			       "action", action,
			       "select-multiple", action == GTK_FILE_CHOOSER_ACTION_OPEN,
			       NULL);

	create_option_menu (GEDIT_FILE_CHOOSER_DIALOG (result));
273

Paolo Borelli's avatar
Paolo Borelli committed
274 275 276 277
	g_signal_connect (result,
			  "notify::action",
			  G_CALLBACK (action_changed),
			  NULL);
278

Paolo Borelli's avatar
Paolo Borelli committed
279 280 281 282
	if (encoding != NULL)
		gedit_encodings_option_menu_set_selected_encoding (
				GEDIT_ENCODINGS_OPTION_MENU (GEDIT_FILE_CHOOSER_DIALOG (result)->priv->option_menu),
				encoding);
283 284 285 286 287

	active_filter = gedit_prefs_manager_get_active_file_filter ();
	gedit_debug_message (DEBUG_COMMANDS, "Active filter: %d", active_filter);

	/* Filters */
Paolo Borelli's avatar
Paolo Borelli committed
288
	filter = gtk_file_filter_new ();
289 290

	gtk_file_filter_set_name (filter, ALL_FILES);
Paolo Borelli's avatar
Paolo Borelli committed
291 292 293
	gtk_file_filter_add_pattern (filter, "*");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);

294 295 296 297 298
	if (active_filter != 1)
	{
		/* Make this filter the default */
		gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
	}
Paolo Borelli's avatar
Paolo Borelli committed
299 300

	filter = gtk_file_filter_new ();
301 302
	gtk_file_filter_set_name (filter, ALL_TEXT_FILES);
	gtk_file_filter_add_custom (filter,
Paolo Borelli's avatar
Paolo Borelli committed
303
				    GTK_FILE_FILTER_MIME_TYPE,
304 305
				    all_text_files_filter,
				    NULL,
Paolo Borelli's avatar
Paolo Borelli committed
306
				    NULL);
307 308 309 310 311 312 313 314 315 316 317 318 319
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);

	if (active_filter == 1)
	{
		/* Make this filter the default */
		gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
	}

	g_signal_connect (result,
			  "notify::filter",
			  G_CALLBACK (filter_changed),
			  NULL);

Paolo Borelli's avatar
Paolo Borelli committed
320 321 322 323 324 325
	gtk_window_set_transient_for (GTK_WINDOW (result), parent);
	gtk_window_set_destroy_with_parent (GTK_WINDOW (result), TRUE);

	while (button_text)
	{
		response_id = va_arg (varargs, gint);
326

Paolo Borelli's avatar
Paolo Borelli committed
327 328 329 330
		gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
		if ((response_id == GTK_RESPONSE_OK) ||
		    (response_id == GTK_RESPONSE_ACCEPT) ||
		    (response_id == GTK_RESPONSE_YES) ||
331
		    (response_id == GTK_RESPONSE_APPLY))
Paolo Borelli's avatar
Paolo Borelli committed
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
			gtk_dialog_set_default_response (GTK_DIALOG (result), response_id);

		button_text = va_arg (varargs, const gchar *);
	}

	return result;
}

/**
 * gedit_file_chooser_dialog_new:
 * @title: Title of the dialog, or %NULL
 * @parent: Transient parent of the dialog, or %NULL
 * @action: Open or save mode for the dialog
 * @first_button_text: stock ID or text to go in the first button, or %NULL
 * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
 *
 * Creates a new #GeditFileChooserDialog.  This function is analogous to
 * gtk_dialog_new_with_buttons().
 *
 * Return value: a new #GeditFileChooserDialog
 *
 **/
GtkWidget *
gedit_file_chooser_dialog_new (const gchar          *title,
			       GtkWindow            *parent,
			       GtkFileChooserAction  action,
			       const GeditEncoding  *encoding,
			       const gchar          *first_button_text,
			       ...)
{
	GtkWidget *result;
	va_list varargs;
364

Paolo Borelli's avatar
Paolo Borelli committed
365 366 367 368 369 370
	va_start (varargs, first_button_text);
	result = gedit_file_chooser_dialog_new_valist (title, parent, action,
						       encoding, first_button_text,
						       varargs);
	va_end (varargs);

371 372 373
	return result;
}

Paolo Borelli's avatar
Paolo Borelli committed
374 375 376 377 378 379
void
gedit_file_chooser_dialog_set_encoding (GeditFileChooserDialog *dialog,
					const GeditEncoding    *encoding)
{
	g_return_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog));
	g_return_if_fail (GEDIT_IS_ENCODINGS_OPTION_MENU (dialog->priv->option_menu));
380

Paolo Borelli's avatar
Paolo Borelli committed
381 382
	gedit_encodings_option_menu_set_selected_encoding (
				GEDIT_ENCODINGS_OPTION_MENU (dialog->priv->option_menu),
383
				encoding);
Paolo Borelli's avatar
Paolo Borelli committed
384
}
385

Paolo Borelli's avatar
Paolo Borelli committed
386 387 388 389
const GeditEncoding *
gedit_file_chooser_dialog_get_encoding (GeditFileChooserDialog *dialog)
{
	g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
390
	g_return_val_if_fail (GEDIT_IS_ENCODINGS_OPTION_MENU (dialog->priv->option_menu), NULL);
Paolo Borelli's avatar
Paolo Borelli committed
391 392 393 394 395
	g_return_val_if_fail ((gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_OPEN ||
			       gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE), NULL);

	return gedit_encodings_option_menu_get_selected_encoding (
				GEDIT_ENCODINGS_OPTION_MENU (dialog->priv->option_menu));
396
}