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"
44 #include "prefs_common.h"
47 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
50 char *filename = NULL, *type = NULL;
51 GdkPixbuf *pixbuf = NULL;
52 gboolean have_preview = FALSE;
54 preview = GTK_WIDGET (data);
55 filename = gtk_file_chooser_get_preview_filename (file_chooser);
57 if (filename == NULL) {
58 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
61 type = procmime_get_mime_type(filename);
63 if (type && !strncmp(type, "image/", 6))
64 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
71 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
72 gdk_pixbuf_unref (pixbuf);
75 gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
78 static GList *filesel_create(const gchar *title, const gchar *path,
79 gboolean multiple_files,
80 gboolean open, gboolean folder_mode,
83 GSList *slist = NULL, *slist_orig = NULL;
86 gint action = (open == TRUE) ?
87 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
88 GTK_FILE_CHOOSER_ACTION_OPEN):
89 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
90 GTK_FILE_CHOOSER_ACTION_SAVE);
92 gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
93 GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action,
94 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
95 action_btn, GTK_RESPONSE_ACCEPT,
98 GtkFileFilter *file_filter = gtk_file_filter_new();
99 gtk_file_filter_add_pattern(file_filter, filter);
100 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
104 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
106 preview = GTK_IMAGE(gtk_image_new ());
107 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
108 g_signal_connect (chooser, "update-preview",
109 G_CALLBACK (update_preview_cb), preview);
113 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
114 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
117 manage_window_set_transient (GTK_WINDOW(chooser));
118 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
120 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
122 if (path && strlen(path) > 0) {
123 char *filename = NULL;
124 char *realpath = strdup(path);
125 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
127 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
129 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
131 filename = (char *) path;
133 realpath = strdup(get_home_dir());
135 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
136 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
137 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
140 if (!prefs_common.attach_load_dir)
141 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
143 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), prefs_common.attach_load_dir);
146 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
147 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
149 manage_window_focus_out(chooser, NULL, NULL);
150 gtk_widget_destroy (chooser);
155 gchar *tmp = strdup(slist->data);
157 if (!path && prefs_common.attach_load_dir)
158 g_free(prefs_common.attach_load_dir);
160 if (strrchr(tmp, G_DIR_SEPARATOR))
161 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
164 prefs_common.attach_load_dir = g_strdup(tmp);
170 list = g_list_append(list, slist->data);
175 g_slist_free(slist_orig);
181 * This function lets the user select multiple files.
182 * This opens an Open type dialog.
183 * @param title the title of the dialog
185 GList *filesel_select_multiple_files_open(const gchar *title)
187 return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
190 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
194 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
198 * This function lets the user select one file.
199 * This opens an Open type dialog if "file" is NULL,
200 * Save dialog if "file" contains a path.
201 * @param title the title of the dialog
202 * @param path the optional path to save to
204 static gchar *filesel_select_file(const gchar *title, const gchar *path,
205 gboolean open, gboolean folder_mode,
208 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
209 gchar * result = NULL;
211 result = strdup(list->data);
216 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
218 return filesel_select_file (title, path, TRUE, FALSE, NULL);
221 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
224 return filesel_select_file (title, path, TRUE, FALSE, filter);
227 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
229 return filesel_select_file (title, path, FALSE, FALSE, NULL);
232 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
234 return filesel_select_file (title, path, TRUE, TRUE, NULL);
237 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
239 return filesel_select_file (title, path, FALSE, TRUE, NULL);