32bc4c648898c9b0ba0cf928b3a100b2658a9b3b
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "sylpheed.h"
38 #include "filesel.h"
39 #include "manage_window.h"
40 #include "gtkutils.h"
41 #include "utils.h"
42 #include "codeconv.h"
43 #include "prefs_common.h"
44
45 static GList *filesel_create(const gchar *title, const gchar *path,
46                              gboolean multiple_files,
47                              gboolean open, gboolean folder_mode,
48                              const gchar *filter)
49 {
50         GSList *slist = NULL, *slist_orig = NULL;
51         GList *list = NULL;
52         gchar *last_selected_dir = prefs_common.attach_load_dir;
53
54         gint action = (open == TRUE) ? 
55                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
56                                                GTK_FILE_CHOOSER_ACTION_OPEN):
57                         (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
58                                                GTK_FILE_CHOOSER_ACTION_SAVE);
59                         
60         gchar * action_btn = (open == TRUE) ? GTK_STOCK_OPEN:GTK_STOCK_SAVE;
61         GtkWidget *chooser = gtk_file_chooser_dialog_new (title, NULL, action, 
62                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
63                                 action_btn, GTK_RESPONSE_ACCEPT, 
64                                 NULL);
65         if (filter != NULL) {
66                 GtkFileFilter *file_filter = gtk_file_filter_new();
67                 gtk_file_filter_add_pattern(file_filter, filter);
68                 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
69                                             file_filter);
70         }
71
72         manage_window_set_transient (GTK_WINDOW(chooser));
73         gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
74
75         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser), multiple_files);
76
77         if (path && strlen(path) > 0) {
78                 char *filename = NULL;
79                 char *realpath = strdup(path);
80                 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
81                         filename = "";
82                 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
83                         filename++;
84                         *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
85                 } else {
86                         filename = (char *) path;
87                         free(realpath); 
88                         realpath = strdup(get_home_dir());
89                 }
90                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), realpath);
91                 if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
92                         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), filename);
93                 free(realpath);
94         } else {
95                 if (!last_selected_dir)
96                         last_selected_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
97
98                 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), last_selected_dir);
99         }
100
101         if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) 
102                 slist = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
103         
104         manage_window_focus_out(chooser, NULL, NULL);
105         gtk_widget_destroy (chooser);
106
107         slist_orig = slist;
108         
109         if (slist) {
110                 gchar *tmp = strdup(slist->data);
111                 if (last_selected_dir)
112                         g_free(last_selected_dir);
113                 
114                 if (strrchr(tmp, G_DIR_SEPARATOR))
115                         *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
116                 last_selected_dir = g_strdup(tmp);
117
118                 if (prefs_common.attach_load_dir)
119                         g_free(prefs_common.attach_load_dir);
120
121                 prefs_common.attach_load_dir = last_selected_dir;
122
123                 g_free(tmp);
124         }
125
126         while (slist) {
127                 list = g_list_append(list, slist->data);
128                 slist = slist->next;
129         }
130         
131         if (slist_orig)
132                 g_slist_free(slist_orig);
133         
134         return list;
135 }
136
137 /**
138  * This function lets the user select multiple files.
139  * This opens an Open type dialog.
140  * @param title the title of the dialog
141  */
142 GList *filesel_select_multiple_files_open(const gchar *title)
143 {
144         return filesel_create(title, NULL, TRUE, TRUE, FALSE, NULL);
145 }
146
147 /**
148  * This function lets the user select one file.
149  * This opens an Open type dialog if "file" is NULL, 
150  * Save dialog if "file" contains a path.
151  * @param title the title of the dialog
152  * @param path the optional path to save to
153  */
154 static gchar *filesel_select_file(const gchar *title, const gchar *path,
155                                   gboolean open, gboolean folder_mode,
156                                   const gchar *filter)
157 {
158         GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
159         gchar * result = NULL;
160         if (list) {
161                 result = strdup(list->data);
162         }
163         g_list_free(list);
164         return result;
165 }
166 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
167 {
168         return filesel_select_file (title, path, TRUE, FALSE, NULL);
169 }
170
171 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
172                                             const gchar *filter)
173 {
174         return filesel_select_file (title, path, TRUE, FALSE, filter);
175 }
176
177 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
178 {
179         return filesel_select_file (title, path, FALSE, FALSE, NULL);
180 }
181
182 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
183 {
184         return filesel_select_file (title, path, TRUE, TRUE, NULL);
185 }
186
187 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
188 {
189         return filesel_select_file (title, path, FALSE, TRUE, NULL);
190 }
191