2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>
40 #include "manage_window.h"
44 static gchar *last_selected_dir = NULL;
45 static GList *filesel_create(const gchar *title, const gchar *path,
46 gboolean multiple_files,
49 GSList *slist = NULL, *slist_orig = NULL;
52 gint action = (open == TRUE) ? GTK_FILE_CHOOSER_ACTION_OPEN:GTK_FILE_CHOOSER_ACTION_SAVE;
53 gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
54 GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action,
55 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
56 action_btn, GTK_RESPONSE_OK,
59 manage_window_set_transient (GTK_WINDOW(chooser));
60 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
61 gtk_window_set_wmclass
62 (GTK_WINDOW(chooser), "file_selection", "Sylpheed");
64 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
66 if (path && strlen(path) > 0) {
67 char *filename = NULL;
68 char *realpath = strdup(path);
69 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
71 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
73 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
75 filename = (char *) path;
77 realpath = strdup(get_home_dir());
79 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
80 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
83 if (!last_selected_dir)
84 last_selected_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
86 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), last_selected_dir);
89 if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK)
90 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
92 manage_window_focus_out(chooser, NULL, NULL);
93 gtk_widget_destroy (chooser);
98 gchar *tmp = strdup(slist->data);
99 if (last_selected_dir)
100 g_free(last_selected_dir);
102 if (strrchr(tmp, G_DIR_SEPARATOR))
103 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
104 last_selected_dir = g_strdup(tmp);
109 list = g_list_append(list, slist->data);
114 g_slist_free(slist_orig);
120 * This function lets the user select multiple files.
121 * This opens an Open type dialog.
122 * @param title the title of the dialog
124 GList *filesel_select_multiple_files_open(const gchar *title)
126 return filesel_create(title, NULL, TRUE, TRUE);
130 * This function lets the user select one file.
131 * This opens an Open type dialog if "file" is NULL,
132 * Save dialog if "file" contains a path.
133 * @param title the title of the dialog
134 * @param path the optional path to save to
136 static gchar *filesel_select_file(const gchar *title, const gchar *path,
139 GList * list = filesel_create(title, path, FALSE, open);
140 gchar * result = NULL;
142 result = strdup(list->data);
147 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
149 return filesel_select_file (title, path, TRUE);
152 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
154 return filesel_select_file (title, path, FALSE);