2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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>
30 #include <hildon/hildon-file-chooser-dialog.h>
32 #include <hildon-widgets/hildon-file-chooser-dialog.h>
38 #include "manage_window.h"
43 #include "prefs_common.h"
46 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
49 char *filename = NULL, *type = NULL;
50 GdkPixbuf *pixbuf = NULL;
51 gboolean have_preview = FALSE;
53 preview = GTK_WIDGET (data);
54 filename = gtk_file_chooser_get_preview_filename (file_chooser);
56 if (filename == NULL) {
57 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
60 type = procmime_get_mime_type(filename);
62 if (type && !strncmp(type, "image/", 6))
63 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
70 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
71 g_object_unref(G_OBJECT(pixbuf));
74 gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
77 static GList *filesel_create(const gchar *title, const gchar *path,
78 gboolean multiple_files,
79 gboolean open, gboolean folder_mode,
82 GSList *slist = NULL, *slist_orig = NULL;
85 gint action = (open == TRUE) ?
86 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
87 GTK_FILE_CHOOSER_ACTION_OPEN):
88 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
89 GTK_FILE_CHOOSER_ACTION_SAVE);
91 gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
94 if( path && strcmp(path, get_plugin_dir()) == 0 ) {
95 #if !GTK_CHECK_VERSION(2,14,0)
96 chooser = gtk_file_chooser_dialog_new_with_backend
97 (title, NULL, action, "gtk+",
98 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
99 action_btn, GTK_RESPONSE_ACCEPT,
102 chooser = gtk_file_chooser_dialog_new
103 (title, NULL, action,
104 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
105 action_btn, GTK_RESPONSE_ACCEPT,
110 chooser = hildon_file_chooser_dialog_new (NULL, action);
113 #if !GTK_CHECK_VERSION(2,14,0)
114 GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
115 (title, NULL, action, "gtk+",
116 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
117 action_btn, GTK_RESPONSE_ACCEPT,
120 GtkWidget *chooser = gtk_file_chooser_dialog_new
121 (title, NULL, action,
122 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
123 action_btn, GTK_RESPONSE_ACCEPT,
128 #if GLIB_CHECK_VERSION(2,16,0)
129 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE);
132 if (filter != NULL) {
133 GtkFileFilter *file_filter = gtk_file_filter_new();
134 gtk_file_filter_add_pattern(file_filter, filter);
135 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
139 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
141 preview = GTK_IMAGE(gtk_image_new ());
142 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
143 g_signal_connect (chooser, "update-preview",
144 G_CALLBACK (update_preview_cb), preview);
148 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
149 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
152 manage_window_set_transient (GTK_WINDOW(chooser));
153 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
155 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
157 if (path && strlen(path) > 0) {
158 char *filename = NULL;
159 char *realpath = g_strdup(path);
161 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
163 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
165 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
167 filename = (char *) path;
169 realpath = g_strdup(get_home_dir());
171 if (g_utf8_validate(realpath, -1, NULL))
172 tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
174 tmp = g_strdup(realpath);
175 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
177 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
178 if (g_utf8_validate(filename, -1, NULL))
179 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser),
185 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
186 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
187 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
188 tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
190 tmp = g_strdup(prefs_common.attach_load_dir);
191 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
196 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK
197 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
199 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
201 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
203 manage_window_focus_out(chooser, NULL, NULL);
204 gtk_widget_destroy (chooser);
209 gchar *tmp = g_strdup(slist->data);
211 if (!path && prefs_common.attach_load_dir)
212 g_free(prefs_common.attach_load_dir);
214 if (strrchr(tmp, G_DIR_SEPARATOR))
215 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
218 prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
224 list = g_list_append(list, slist->data);
229 g_slist_free(slist_orig);
235 * This function lets the user select multiple files.
236 * This opens an Open type dialog.
237 * @param title the title of the dialog
239 GList *filesel_select_multiple_files_open(const gchar *title)
241 return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
244 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
248 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
252 * This function lets the user select one file.
253 * This opens an Open type dialog if "file" is NULL,
254 * Save dialog if "file" contains a path.
255 * @param title the title of the dialog
256 * @param path the optional path to save to
258 static gchar *filesel_select_file(const gchar *title, const gchar *path,
259 gboolean open, gboolean folder_mode,
262 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
263 gchar * result = NULL;
265 result = g_strdup(list->data);
270 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
272 return filesel_select_file (title, path, TRUE, FALSE, NULL);
275 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
278 return filesel_select_file (title, path, TRUE, FALSE, filter);
281 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
283 return filesel_select_file (title, path, FALSE, FALSE, NULL);
286 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
288 return filesel_select_file (title, path, TRUE, TRUE, NULL);
291 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
293 return filesel_select_file (title, path, FALSE, TRUE, NULL);