2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2019 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 GtkWidget
*chooser
= gtk_file_chooser_dialog_new
88 _("_Cancel"), GTK_RESPONSE_CANCEL
,
89 action_btn
, GTK_RESPONSE_ACCEPT
,
92 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser
), FALSE
);
95 GtkFileFilter
*file_filter
= gtk_file_filter_new();
96 gtk_file_filter_add_pattern(file_filter
, filter
);
97 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(chooser
),
101 if (action
== GTK_FILE_CHOOSER_ACTION_OPEN
) {
103 preview
= GTK_IMAGE(gtk_image_new ());
104 gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER(chooser
), GTK_WIDGET(preview
));
105 g_signal_connect (chooser
, "update-preview",
106 G_CALLBACK (update_preview_cb
), preview
);
110 if (action
== GTK_FILE_CHOOSER_ACTION_SAVE
) {
111 gtk_dialog_set_default_response(GTK_DIALOG(chooser
), GTK_RESPONSE_ACCEPT
);
114 manage_window_set_transient (GTK_WINDOW(chooser
));
115 gtk_window_set_modal(GTK_WINDOW(chooser
), TRUE
);
117 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(chooser
), multiple_files
);
119 if (path
&& strlen(path
) > 0) {
120 char *filename
= NULL
;
121 char *realpath
= g_strdup(path
);
123 if (path
[strlen(path
)-1] == G_DIR_SEPARATOR
) {
125 } else if ((filename
= strrchr(path
, G_DIR_SEPARATOR
)) != NULL
) {
127 *(strrchr(realpath
, G_DIR_SEPARATOR
)+1) = '\0';
129 filename
= (char *) path
;
131 realpath
= g_strdup(get_home_dir());
133 if (g_utf8_validate(realpath
, -1, NULL
))
134 tmp
= g_filename_from_utf8(realpath
, -1, NULL
, NULL
, NULL
);
136 tmp
= g_strdup(realpath
);
137 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser
), tmp
);
139 if (action
== GTK_FILE_CHOOSER_ACTION_SAVE
) {
140 if (g_utf8_validate(filename
, -1, NULL
))
141 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser
),
147 if (!prefs_common
.attach_load_dir
|| !*prefs_common
.attach_load_dir
)
148 prefs_common
.attach_load_dir
= g_strdup_printf("%s%c", get_home_dir(), G_DIR_SEPARATOR
);
149 if (g_utf8_validate(prefs_common
.attach_load_dir
, -1, NULL
))
150 tmp
= g_filename_from_utf8(prefs_common
.attach_load_dir
, -1, NULL
, NULL
, NULL
);
152 tmp
= g_strdup(prefs_common
.attach_load_dir
);
153 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser
), tmp
);
157 if (gtk_dialog_run (GTK_DIALOG (chooser
)) == GTK_RESPONSE_ACCEPT
)
158 slist
= gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser
));
160 manage_window_focus_out(chooser
, NULL
, NULL
);
161 gtk_widget_destroy (chooser
);
166 gchar
*tmp
= g_strdup(slist
->data
);
168 if (!path
&& prefs_common
.attach_load_dir
)
169 g_free(prefs_common
.attach_load_dir
);
171 if (strrchr(tmp
, G_DIR_SEPARATOR
))
172 *(strrchr(tmp
, G_DIR_SEPARATOR
)+1) = '\0';
175 prefs_common
.attach_load_dir
= g_filename_to_utf8(tmp
, -1, NULL
, NULL
, NULL
);
181 list
= g_list_append(list
, slist
->data
);
186 g_slist_free(slist_orig
);
192 * This function lets the user select multiple files.
193 * This opens an Open type dialog.
194 * @param title the title of the dialog
196 GList
*filesel_select_multiple_files_open(const gchar
*title
, const gchar
*path
)
198 return filesel_create(title
, path
, TRUE
, TRUE
, FALSE
, NULL
);
201 GList
*filesel_select_multiple_files_open_with_filter( const gchar
*title
,
205 return filesel_create (title
, path
, TRUE
, TRUE
, FALSE
, filter
);
209 * This function lets the user select one file.
210 * This opens an Open type dialog if "file" is NULL,
211 * Save dialog if "file" contains a path.
212 * @param title the title of the dialog
213 * @param path the optional path to save to
215 static gchar
*filesel_select_file(const gchar
*title
, const gchar
*path
,
216 gboolean open
, gboolean folder_mode
,
219 GList
* list
= filesel_create(title
, path
, FALSE
, open
, folder_mode
, filter
);
220 gchar
* result
= NULL
;
222 result
= g_strdup(list
->data
);
227 gchar
*filesel_select_file_open(const gchar
*title
, const gchar
*path
)
229 return filesel_select_file (title
, path
, TRUE
, FALSE
, NULL
);
232 gchar
*filesel_select_file_open_with_filter(const gchar
*title
, const gchar
*path
,
235 return filesel_select_file (title
, path
, TRUE
, FALSE
, filter
);
238 gchar
*filesel_select_file_save(const gchar
*title
, const gchar
*path
)
240 return filesel_select_file (title
, path
, FALSE
, FALSE
, NULL
);
243 gchar
*filesel_select_file_open_folder(const gchar
*title
, const gchar
*path
)
245 return filesel_select_file (title
, path
, TRUE
, TRUE
, NULL
);
248 gchar
*filesel_select_file_save_folder(const gchar
*title
, const gchar
*path
)
250 return filesel_select_file (title
, path
, FALSE
, TRUE
, NULL
);