2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
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>
38 #include <hildon-widgets/hildon-file-chooser-dialog.h>
43 #include "manage_window.h"
48 #include "prefs_common.h"
51 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
54 char *filename = NULL, *type = NULL;
55 GdkPixbuf *pixbuf = NULL;
56 gboolean have_preview = FALSE;
58 preview = GTK_WIDGET (data);
59 filename = gtk_file_chooser_get_preview_filename (file_chooser);
61 if (filename == NULL) {
62 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
65 type = procmime_get_mime_type(filename);
67 if (type && !strncmp(type, "image/", 6))
68 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
75 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
76 gdk_pixbuf_unref (pixbuf);
79 gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
82 static GList *filesel_create(const gchar *title, const gchar *path,
83 gboolean multiple_files,
84 gboolean open, gboolean folder_mode,
87 GSList *slist = NULL, *slist_orig = NULL;
90 gint action = (open == TRUE) ?
91 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
92 GTK_FILE_CHOOSER_ACTION_OPEN):
93 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
94 GTK_FILE_CHOOSER_ACTION_SAVE);
96 gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
99 if( path && strcmp(path, get_plugin_dir()) == 0 ) {
100 chooser = gtk_file_chooser_dialog_new_with_backend
101 (title, NULL, action, "gtk+",
102 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
103 action_btn, GTK_RESPONSE_ACCEPT,
107 chooser = hildon_file_chooser_dialog_new (NULL, action);
110 GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
111 (title, NULL, action, "gtk+",
112 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
113 action_btn, GTK_RESPONSE_ACCEPT,
116 if (filter != NULL) {
117 GtkFileFilter *file_filter = gtk_file_filter_new();
118 gtk_file_filter_add_pattern(file_filter, filter);
119 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
123 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
125 preview = GTK_IMAGE(gtk_image_new ());
126 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
127 g_signal_connect (chooser, "update-preview",
128 G_CALLBACK (update_preview_cb), preview);
132 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
133 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
136 manage_window_set_transient (GTK_WINDOW(chooser));
137 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
139 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
141 if (path && strlen(path) > 0) {
142 char *filename = NULL;
143 char *realpath = g_strdup(path);
145 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
147 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
149 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
151 filename = (char *) path;
153 realpath = g_strdup(get_home_dir());
156 if (g_utf8_validate(realpath, -1, NULL))
157 tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
159 tmp = g_strdup(realpath);
160 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
162 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
164 if (g_utf8_validate(filename, -1, NULL))
165 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
167 tmp = g_strdup(filename);
168 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
174 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
175 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
176 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
177 tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
179 tmp = g_strdup(prefs_common.attach_load_dir);
180 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
185 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK
186 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
188 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
190 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
192 manage_window_focus_out(chooser, NULL, NULL);
193 gtk_widget_destroy (chooser);
198 gchar *tmp = g_strdup(slist->data);
200 if (!path && prefs_common.attach_load_dir)
201 g_free(prefs_common.attach_load_dir);
203 if (strrchr(tmp, G_DIR_SEPARATOR))
204 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
207 prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
213 list = g_list_append(list, slist->data);
218 g_slist_free(slist_orig);
224 * This function lets the user select multiple files.
225 * This opens an Open type dialog.
226 * @param title the title of the dialog
228 GList *filesel_select_multiple_files_open(const gchar *title)
230 return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
233 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
237 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
241 * This function lets the user select one file.
242 * This opens an Open type dialog if "file" is NULL,
243 * Save dialog if "file" contains a path.
244 * @param title the title of the dialog
245 * @param path the optional path to save to
247 static gchar *filesel_select_file(const gchar *title, const gchar *path,
248 gboolean open, gboolean folder_mode,
251 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
252 gchar * result = NULL;
254 result = g_strdup(list->data);
259 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
261 return filesel_select_file (title, path, TRUE, FALSE, NULL);
264 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
267 return filesel_select_file (title, path, TRUE, FALSE, filter);
270 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
272 return filesel_select_file (title, path, FALSE, FALSE, NULL);
275 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
277 return filesel_select_file (title, path, TRUE, TRUE, NULL);
280 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
282 return filesel_select_file (title, path, FALSE, TRUE, NULL);