fix erratic folder selection in open mode
[claws.git] / src / gtk / filesel.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif 
23
24 #include <glib.h>
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>
36
37 #include "intl.h"
38 #include "sylpheed.h"
39 #include "filesel.h"
40 #include "manage_window.h"
41 #include "gtkutils.h"
42 #include "utils.h"
43
44 static gchar *last_selected_dir = NULL;
45 static GList *filesel_create(const gchar *title, const gchar *path, gboolean multiple_files)
46 {
47         GSList *slist = NULL, *slist_orig = NULL;
48         GList *list = NULL;
49
50         gint action = (path != NULL) ? GTK_FILE_CHOOSER_ACTION_SAVE:GTK_FILE_CHOOSER_ACTION_OPEN;
51         gchar * action_btn = (path != NULL) ? GTK_STOCK_SAVE:GTK_STOCK_OPEN;
52         GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action, 
53                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
54                                 action_btn, GTK_RESPONSE_OK, 
55                                 NULL);
56         
57         manage_window_set_transient (GTK_WINDOW(chooser));
58         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
59         gtk_window_set_wmclass
60                 (GTK_WINDOW(chooser), "file_selection", "Sylpheed");
61
62         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
63
64         if (path) {
65                 char *filename = NULL;
66                 char *realpath = strdup(path);
67                 if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
68                         filename++;
69                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
70                 } else {
71                         filename = (char *) path;
72                         free(realpath); 
73                         realpath = strdup(get_home_dir());
74                 }
75                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
76                 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
77                 free(realpath);
78         } else {
79                 if (!last_selected_dir)
80                         last_selected_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
81
82                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), last_selected_dir);
83         }
84
85         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK) 
86                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
87         
88         manage_window_focus_out(chooser, NULL, NULL);
89         gtk_widget_destroy (chooser);
90
91         slist_orig = slist;
92         
93         if (slist) {
94                 gchar *tmp = strdup(slist->data);
95                 if (last_selected_dir)
96                         g_free(last_selected_dir);
97                 
98                 if (strrchr(tmp, G_DIR_SEPARATOR))
99                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
100                 last_selected_dir = g_strdup(tmp);
101                 g_free(tmp);
102         }
103
104         while (slist) {
105                 list = g_list_append(list, slist->data);
106                 slist = slist->next;
107         }
108         
109         if (slist_orig)
110                 g_slist_free(slist_orig);
111         
112         return list;
113 }
114
115 /**
116  * This function lets the user select multiple files.
117  * This opens an Open type dialog.
118  * @param title the title of the dialog
119  */
120 GList *filesel_select_multiple_files(const gchar *title)
121 {
122         return filesel_create(title, NULL, TRUE);
123 }
124
125 /**
126  * This function lets the user select one file.
127  * This opens an Open type dialog if "file" is NULL, 
128  * Save dialog if "file" contains a path.
129  * @param title the title of the dialog
130  * @param path the optional path to save to
131  */
132 gchar *filesel_select_file(const gchar *title, const gchar *path)
133 {
134         GList * list = filesel_create(title, path, FALSE);
135         gchar * result = NULL;
136         if (list) {
137                 result = strdup(list->data);
138         }
139         g_list_free(list);
140         return result;
141 }