add RFE 4686, 'use gtk native filechooser'
[claws.git] / src / gtk / filesel.c
blob41b9f89f795791be33ee31dbbeecb148c746c9d3
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2023 the Claws Mail team and 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 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/>.
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <gtk/gtk.h>
30 #include "claws.h"
31 #include "filesel.h"
32 #include "manage_window.h"
33 #include "gtkutils.h"
34 #include "utils.h"
35 #include "codeconv.h"
36 #include "procmime.h"
37 #include "prefs_common.h"
39 static void
40 update_preview_cb (GtkFileChooser *file_chooser, gpointer data)
42 GtkWidget *preview;
43 char *filename = NULL, *type = NULL;
44 GdkPixbuf *pixbuf = NULL;
45 gboolean have_preview = FALSE;
47 preview = GTK_WIDGET (data);
48 filename = gtk_file_chooser_get_preview_filename (file_chooser);
50 if (filename == NULL) {
51 gtk_file_chooser_set_preview_widget_active (file_chooser, FALSE);
52 return;
54 type = procmime_get_mime_type(filename);
56 if (type && !strncmp(type, "image/", 6))
57 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 128, 128, NULL);
59 g_free(type);
60 g_free (filename);
62 if (pixbuf) {
63 have_preview = TRUE;
64 gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);
65 g_object_unref(G_OBJECT(pixbuf));
68 gtk_file_chooser_set_preview_widget_active (file_chooser, have_preview);
71 static GList *filesel_create(const gchar *title, const gchar *path,
72 gboolean multiple_files,
73 gboolean open, gboolean folder_mode,
74 const gchar *filter)
76 GSList *slist = NULL, *slist_orig = NULL;
77 GList *list = NULL;
79 gint action = (open == TRUE) ?
80 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
81 GTK_FILE_CHOOSER_ACTION_OPEN):
82 (folder_mode == TRUE ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
83 GTK_FILE_CHOOSER_ACTION_SAVE);
85 gchar * action_btn = (open == TRUE) ? _("_Open"):_("_Save");
86 GtkFileChooserNative *chooser = gtk_file_chooser_native_new
87 (title, NULL, action,
88 action_btn, _("_Cancel"));
90 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE);
92 if (filter != NULL) {
93 GtkFileFilter *file_filter = gtk_file_filter_new();
94 gtk_file_filter_add_pattern(file_filter, filter);
95 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser),
96 file_filter);
99 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
100 GtkImage *preview;
101 preview = GTK_IMAGE(gtk_image_new ());
102 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(chooser), GTK_WIDGET(preview));
103 g_signal_connect(chooser, "update-preview",
104 G_CALLBACK (update_preview_cb), preview);
108 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
109 gtk_dialog_set_default_response(GTK_DIALOG(chooser), GTK_RESPONSE_ACCEPT);
112 manage_window_set_transient(GTK_WINDOW(chooser));
113 gtk_window_set_modal(GTK_WINDOW(chooser), TRUE);
115 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(chooser), multiple_files);
117 if (path && strlen(path) > 0) {
118 char *filename = NULL;
119 char *realpath = g_strdup(path);
120 char *tmp = NULL;
121 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
122 filename = "";
123 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
124 filename++;
125 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
126 } else {
127 filename = (char *) path;
128 g_free(realpath);
129 realpath = g_strdup(get_home_dir());
131 if (g_utf8_validate(realpath, -1, NULL))
132 tmp = g_filename_from_utf8(realpath, -1, NULL, NULL, NULL);
133 if (tmp == NULL)
134 tmp = g_strdup(realpath);
135 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
136 g_free(tmp);
137 if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
138 if (g_utf8_validate(filename, -1, NULL))
139 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser),
140 filename);
142 g_free(realpath);
143 } else {
144 gchar *tmp = NULL;
145 if (!prefs_common.attach_load_dir || !*prefs_common.attach_load_dir)
146 prefs_common.attach_load_dir = g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR);
147 if (g_utf8_validate(prefs_common.attach_load_dir, -1, NULL))
148 tmp = g_filename_from_utf8(prefs_common.attach_load_dir, -1, NULL, NULL, NULL);
149 if (tmp == NULL)
150 tmp = g_strdup(prefs_common.attach_load_dir);
151 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
152 g_free(tmp);
155 if (gtk_native_dialog_run(GTK_NATIVE_DIALOG(chooser)) == GTK_RESPONSE_ACCEPT)
156 slist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER (chooser));
158 g_object_unref(chooser);
160 slist_orig = slist;
162 if (slist) {
163 gchar *tmp = g_strdup(slist->data);
165 if (!path && prefs_common.attach_load_dir)
166 g_free(prefs_common.attach_load_dir);
168 if (strrchr(tmp, G_DIR_SEPARATOR))
169 *(strrchr(tmp, G_DIR_SEPARATOR)+1) = '\0';
171 if (!path)
172 prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
174 g_free(tmp);
177 while (slist) {
178 list = g_list_append(list, slist->data);
179 slist = slist->next;
182 if (slist_orig)
183 g_slist_free(slist_orig);
185 return list;
189 * This function lets the user select multiple files.
190 * This opens an Open type dialog.
191 * @param title the title of the dialog
193 GList *filesel_select_multiple_files_open(const gchar *title, const gchar *path)
195 return filesel_create(title, path, TRUE, TRUE, FALSE, NULL);
198 GList *filesel_select_multiple_files_open_with_filter( const gchar *title,
199 const gchar *path,
200 const gchar *filter)
202 return filesel_create (title, path, TRUE, TRUE, FALSE, filter);
206 * This function lets the user select one file.
207 * This opens an Open type dialog if "file" is NULL,
208 * Save dialog if "file" contains a path.
209 * @param title the title of the dialog
210 * @param path the optional path to save to
212 static gchar *filesel_select_file(const gchar *title, const gchar *path,
213 gboolean open, gboolean folder_mode,
214 const gchar *filter)
216 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
217 gchar * result = NULL;
218 if (list) {
219 result = g_strdup(list->data);
221 g_list_free(list);
222 return result;
224 gchar *filesel_select_file_open(const gchar *title, const gchar *path)
226 return filesel_select_file (title, path, TRUE, FALSE, NULL);
229 gchar *filesel_select_file_open_with_filter(const gchar *title, const gchar *path,
230 const gchar *filter)
232 return filesel_select_file (title, path, TRUE, FALSE, filter);
235 gchar *filesel_select_file_save(const gchar *title, const gchar *path)
237 return filesel_select_file (title, path, FALSE, FALSE, NULL);
240 gchar *filesel_select_file_open_folder(const gchar *title, const gchar *path)
242 return filesel_select_file (title, path, TRUE, TRUE, NULL);
245 gchar *filesel_select_file_save_folder(const gchar *title, const gchar *path)
247 return filesel_select_file (title, path, FALSE, TRUE, NULL);