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