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>
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 gdk_pixbuf_unref (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 GtkWidget *chooser = gtk_file_chooser_dialog_new_with_backend
114 (title, NULL, action, "gtk+",
115 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
116 action_btn, GTK_RESPONSE_ACCEPT,
119 if (filter != NULL) {
120 GtkFileFilter *file_filter = gtk_file_filter_new();
121 gtk_file_filter_add_pattern(file_filter, filter);
122 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
126 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
128 preview = GTK_IMAGE(gtk_image_new ());
129 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
130 g_signal_connect (chooser, "update-preview",
131 G_CALLBACK (update_preview_cb), preview);
135 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
136 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
139 manage_window_set_transient (GTK_WINDOW(chooser));
140 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
142 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
144 if (path && strlen(path) > 0) {
145 char *filename = NULL;
146 char *realpath = g_strdup(path);
148 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
150 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
152 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
154 filename = (char *) path;
156 realpath = g_strdup(get_home_dir());
159 if (g_utf8_validate(realpath, -1, NULL))
160 tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
162 tmp = g_strdup(realpath);
163 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
165 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
167 if (g_utf8_validate(filename, -1, NULL))
168 tmp = g_filename_from_utf8(filename, -1, NULL, NULL, NULL);
170 tmp = g_strdup(filename);
171 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), tmp);
177 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
178 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
179 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
180 tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
182 tmp = g_strdup(prefs_common.attach_load_dir);
183 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
188 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK
189 || gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
191 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
193 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
195 manage_window_focus_out(chooser, NULL, NULL);
196 gtk_widget_destroy (chooser);
201 gchar *tmp = g_strdup(slist->data);
203 if (!path && prefs_common.attach_load_dir)
204 g_free(prefs_common.attach_load_dir);
206 if (strrchr(tmp, G_DIR_SEPARATOR))
207 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
210 prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
216 list = g_list_append(list, slist->data);
221 g_slist_free(slist_orig);
227 * This function lets the user select multiple files.
228 * This opens an Open type dialog.
229 * @param title the title of the dialog
231 GList *filesel_select_multiple_files_open(const gchar *title)
233 return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
236 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
240 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
244 * This function lets the user select one file.
245 * This opens an Open type dialog if "file" is NULL,
246 * Save dialog if "file" contains a path.
247 * @param title the title of the dialog
248 * @param path the optional path to save to
250 static gchar *filesel_select_file(const gchar *title, const gchar *path,
251 gboolean open, gboolean folder_mode,
254 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
255 gchar * result = NULL;
257 result = g_strdup(list->data);
262 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
264 return filesel_select_file (title, path, TRUE, FALSE, NULL);
267 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
270 return filesel_select_file (title, path, TRUE, FALSE, filter);
273 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
275 return filesel_select_file (title, path, FALSE, FALSE, NULL);
278 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
280 return filesel_select_file (title, path, TRUE, TRUE, NULL);
283 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
285 return filesel_select_file (title, path, FALSE, TRUE, NULL);