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/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
27 #include <gdk/gdkkeysyms.h>
32 #include "manage_window.h"
37 #include "prefs_common.h"
40 update_preview_cb (GtkFileChooser
*file_chooser
, gpointer data
)
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
);
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
);
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
,
76 GSList
*slist
= NULL
, *slist_orig
= 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
88 action_btn
, _("_Cancel"));
90 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser
), FALSE
);
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
),
99 if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
) {
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
);
121 if (path
[strlen(path
)-1] == G_DIR_SEPARATOR
) {
123 } else if ((filename
= strrchr(path
, G_DIR_SEPARATOR
)) != NULL
) {
125 *(strrchr(realpath
, G_DIR_SEPARATOR
)+1) = '\0';
127 filename
= (char *) path
;
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
);
134 tmp
= g_strdup(realpath
);
135 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser
), 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
),
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
);
150 tmp
= g_strdup(prefs_common
.attach_load_dir
);
151 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser
), 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
);
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';
172 prefs_common
.attach_load_dir
= g_filename_to_utf8(tmp
, -1, NULL
, NULL
, NULL
);
178 list
= g_list_append(list
, slist
->data
);
183 g_slist_free(slist_orig
);
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
,
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
,
216 GList
* list
= filesel_create(title
, path
, FALSE
, open
, folder_mode
, filter
);
217 gchar
* result
= NULL
;
219 result
= g_strdup(list
->data
);
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
,
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
);