2 * Copyright © 2003 Marco Pesenti Gritti
3 * Copyright © 2003, 2004 Christian Persch
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "ephy-file-chooser.h"
25 #include "ephy-file-helpers.h"
26 #include "eel-gconf-extensions.h"
27 #include "ephy-state.h"
29 #include "ephy-debug.h"
30 #include "ephy-stock-icons.h"
32 #include <gtk/gtkstock.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34 #include <glib/gi18n.h>
36 #define EPHY_FILE_CHOOSER_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_FILE_CHOOSER, EphyFileChooserPrivate))
38 struct _EphyFileChooserPrivate
43 static void ephy_file_chooser_class_init (EphyFileChooserClass *klass);
44 static void ephy_file_chooser_init (EphyFileChooser *dialog);
52 static GObjectClass *parent_class = NULL;
55 ephy_file_chooser_get_type (void)
57 static GType type = 0;
59 if (G_UNLIKELY (type == 0))
61 const GTypeInfo our_info =
63 sizeof (EphyFileChooserClass),
66 (GClassInitFunc) ephy_file_chooser_class_init,
69 sizeof (EphyFileChooser),
71 (GInstanceInitFunc) ephy_file_chooser_init
74 type = g_type_register_static (GTK_TYPE_FILE_CHOOSER_DIALOG,
83 current_folder_changed_cb (GtkFileChooser *chooser, EphyFileChooser *dialog)
85 if (dialog->priv->persist_key != NULL)
89 dir = gtk_file_chooser_get_current_folder (chooser);
91 eel_gconf_set_path (dialog->priv->persist_key, dir);
98 ephy_file_chooser_init (EphyFileChooser *dialog)
100 dialog->priv = EPHY_FILE_CHOOSER_GET_PRIVATE (dialog);
104 ephy_file_chooser_constructor (GType type,
105 guint n_construct_properties,
106 GObjectConstructParam *construct_params)
112 object = parent_class->constructor (type, n_construct_properties,
115 downloads_dir = ephy_file_get_downloads_dir ();
116 gtk_file_chooser_add_shortcut_folder
117 (GTK_FILE_CHOOSER (object), downloads_dir, NULL);
118 g_free (downloads_dir);
120 gtk_window_set_icon_name (GTK_WINDOW (object), EPHY_STOCK_EPHY);
126 ephy_file_chooser_finalize (GObject *object)
128 EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
130 g_free (dialog->priv->persist_key);
132 LOG ("EphyFileChooser finalised");
134 G_OBJECT_CLASS (parent_class)->finalize (object);
138 ephy_file_chooser_set_persist_key (EphyFileChooser *dialog, const char *key)
140 char *dir, *expanded, *converted;
142 g_return_if_fail (key != NULL && key[0] != '\0');
144 dialog->priv->persist_key = g_strdup (key);
146 dir = eel_gconf_get_string (key);
149 converted = g_filename_from_utf8
150 (dir, -1, NULL, NULL, NULL);
152 if (converted != NULL)
154 expanded = gnome_vfs_expand_initial_tilde (converted);
156 gtk_file_chooser_set_current_folder
157 (GTK_FILE_CHOOSER (dialog), expanded);
166 g_signal_connect (dialog, "current-folder-changed",
167 G_CALLBACK (current_folder_changed_cb), dialog);
171 ephy_file_chooser_get_persist_key (EphyFileChooser *dialog)
173 g_return_val_if_fail (EPHY_IS_FILE_CHOOSER (dialog), NULL);
175 return dialog->priv->persist_key;
179 ephy_file_chooser_add_pattern_filter (EphyFileChooser *dialog,
181 const char *first_pattern,
184 GtkFileFilter *filth;
188 filth = gtk_file_filter_new ();
190 va_start (args, first_pattern);
192 pattern = first_pattern;
193 while (pattern != NULL)
195 gtk_file_filter_add_pattern (filth, pattern);
196 pattern = va_arg (args, const char *);
200 gtk_file_filter_set_name (filth, title);
202 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filth);
208 ephy_file_chooser_add_mime_filter (EphyFileChooser *dialog,
210 const char *first_mimetype,
213 GtkFileFilter *filth;
215 const char *mimetype;
217 filth = gtk_file_filter_new ();
219 va_start (args, first_mimetype);
221 mimetype = first_mimetype;
222 while (mimetype != NULL)
224 gtk_file_filter_add_mime_type (filth, mimetype);
225 mimetype = va_arg (args, const char *);
229 gtk_file_filter_set_name (filth, title);
231 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filth);
237 ephy_file_chooser_set_property (GObject *object,
242 EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
246 case PROP_PERSIST_KEY:
247 ephy_file_chooser_set_persist_key (dialog, g_value_get_string (value));
253 ephy_file_chooser_get_property (GObject *object,
258 EphyFileChooser *dialog = EPHY_FILE_CHOOSER (object);
262 case PROP_PERSIST_KEY:
263 g_value_set_string (value, ephy_file_chooser_get_persist_key (dialog));
269 ephy_file_chooser_class_init (EphyFileChooserClass *klass)
271 GObjectClass *object_class = G_OBJECT_CLASS (klass);
273 parent_class = g_type_class_peek_parent (klass);
275 object_class->constructor = ephy_file_chooser_constructor;
276 object_class->finalize = ephy_file_chooser_finalize;
277 object_class->get_property = ephy_file_chooser_get_property;
278 object_class->set_property = ephy_file_chooser_set_property;
280 g_object_class_install_property (object_class,
282 g_param_spec_string ("persist-key",
284 "The gconf key to which to persist the selected directory",
286 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
288 g_type_class_add_private (object_class, sizeof (EphyFileChooserPrivate));
292 ephy_file_chooser_new (const char *title,
294 GtkFileChooserAction action,
295 const char *persist_key,
296 EphyFileFilterDefault default_filter)
298 EphyFileChooser *dialog;
299 GtkFileFilter *filter[EPHY_FILE_FILTER_LAST];
301 g_return_val_if_fail (default_filter >= 0 && default_filter <= EPHY_FILE_FILTER_LAST, NULL);
303 dialog = EPHY_FILE_CHOOSER (g_object_new (EPHY_TYPE_FILE_CHOOSER,
308 /* NOTE: We cannot set this property on object construction time.
309 * This is because GtkFileChooserDialog overrides the gobject
310 * constructor; the GtkFileChooser delegate will only be set
311 * _after_ our instance_init and construct-param setters will have
314 if (persist_key != NULL)
316 ephy_file_chooser_set_persist_key (dialog, persist_key);
319 if (action == GTK_FILE_CHOOSER_ACTION_OPEN ||
320 action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ||
321 action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
323 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
324 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
325 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
327 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
328 GTK_RESPONSE_ACCEPT);
330 else if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
332 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
333 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
334 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
336 gtk_dialog_set_default_response (GTK_DIALOG (dialog),
337 GTK_RESPONSE_ACCEPT);
340 if (default_filter != EPHY_FILE_FILTER_NONE)
342 filter[EPHY_FILE_FILTER_ALL_SUPPORTED] =
343 ephy_file_chooser_add_mime_filter
345 _("All supported types"),
347 "application/xhtml+xml",
354 filter[EPHY_FILE_FILTER_WEBPAGES] =
355 ephy_file_chooser_add_mime_filter
356 (dialog, _("Web pages"),
358 "application/xhtml+xml",
362 filter[EPHY_FILE_FILTER_IMAGES] =
363 ephy_file_chooser_add_mime_filter
364 (dialog, _("Images"),
370 filter[EPHY_FILE_FILTER_ALL] =
371 ephy_file_chooser_add_pattern_filter
372 (dialog, _("All files"), "*", NULL);
374 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog),
375 filter[default_filter]);
380 gtk_window_set_transient_for (GTK_WINDOW (dialog),
381 GTK_WINDOW (parent));
382 gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parent)),
383 GTK_WINDOW (dialog));
384 gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);