2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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 of the License, or
8 * (at your option) any later version.
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.
25 #include <gdk/gdkkeysyms.h>
26 #include <gtk/gtkwidget.h>
27 #include <gtk/gtkfilesel.h>
28 #include <gtk/gtkentry.h>
29 #include <gtk/gtkmain.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkeditable.h>
32 #include <gtk/gtkstock.h>
33 #include <gtk/gtkdialog.h>
34 #include <gtk/gtkfilechooser.h>
35 #include <gtk/gtkfilechooserdialog.h>
39 #include "manage_window.h"
43 #include "prefs_common.h"
46 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
51 gboolean have_preview;
53 preview = GTK_WIDGET (data);
54 filename = gtk_file_chooser_get_preview_filename (file_chooser);
59 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
60 have_preview = (pixbuf != NULL);
63 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
65 gdk_pixbuf_unref (pixbuf);
67 gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
70 static GList *filesel_create(const gchar *title, const gchar *path,
71 gboolean multiple_files,
72 gboolean open, gboolean folder_mode,
75 GSList *slist = NULL, *slist_orig = NULL;
78 gint action = (open == TRUE) ?
79 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
80 GTK_FILE_CHOOSER_ACTION_OPEN):
81 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
82 GTK_FILE_CHOOSER_ACTION_SAVE);
84 gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
85 GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action,
86 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
87 action_btn, GTK_RESPONSE_ACCEPT,
90 GtkFileFilter *file_filter = gtk_file_filter_new();
91 gtk_file_filter_add_pattern(file_filter, filter);
92 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
96 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
98 preview = gtk_image_new ();
99 gtk_file_chooser_set_preview_widget (chooser, preview);
100 g_signal_connect (chooser, "update-preview",
101 G_CALLBACK (update_preview_cb), preview);
105 manage_window_set_transient (GTK_WINDOW(chooser));
106 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
108 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
110 if (path && strlen(path) > 0) {
111 char *filename = NULL;
112 char *realpath = strdup(path);
113 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
115 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
117 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
119 filename = (char *) path;
121 realpath = strdup(get_home_dir());
123 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
124 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
125 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
128 if (!prefs_common.attach_load_dir)
129 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
131 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), prefs_common.attach_load_dir);
134 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
135 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
137 manage_window_focus_out(chooser, NULL, NULL);
138 gtk_widget_destroy (chooser);
143 gchar *tmp = strdup(slist->data);
145 if (!path && prefs_common.attach_load_dir)
146 g_free(prefs_common.attach_load_dir);
148 if (strrchr(tmp, G_DIR_SEPARATOR))
149 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
152 prefs_common.attach_load_dir = g_strdup(tmp);
158 list = g_list_append(list, slist->data);
163 g_slist_free(slist_orig);
169 * This function lets the user select multiple files.
170 * This opens an Open type dialog.
171 * @param title the title of the dialog
173 GList *filesel_select_multiple_files_open(const gchar *title)
175 return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
178 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
182 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
186 * This function lets the user select one file.
187 * This opens an Open type dialog if "file" is NULL,
188 * Save dialog if "file" contains a path.
189 * @param title the title of the dialog
190 * @param path the optional path to save to
192 static gchar *filesel_select_file(const gchar *title, const gchar *path,
193 gboolean open, gboolean folder_mode,
196 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
197 gchar * result = NULL;
199 result = strdup(list->data);
204 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
206 return filesel_select_file (title, path, TRUE, FALSE, NULL);
209 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
212 return filesel_select_file (title, path, TRUE, FALSE, filter);
215 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
217 return filesel_select_file (title, path, FALSE, FALSE, NULL);
220 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
222 return filesel_select_file (title, path, TRUE, TRUE, NULL);
225 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
227 return filesel_select_file (title, path, FALSE, TRUE, NULL);