Improve some sieve-related translations
[claws.git] / src / gtk / filesel.c
blobed71b5312448b0d96737d6c96ae27d6898f4936c
1 /*
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/>.
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 GtkWidget *chooser = gtk_file_chooser_dialog_new
87 (title, NULL, action,
88 _("_Cancel"), GTK_RESPONSE_CANCEL,
89 action_btn, GTK_RESPONSE_ACCEPT,
90 NULL);
92 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(chooser), FALSE);
94 if (filter != NULL) {
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),
98 file_filter);
101 if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
102 GtkImage *preview;
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);
122 char *tmp = NULL;
123 if (path[strlen(path)-1] == G_DIR_SEPARATOR) {
124 filename = "";
125 } else if ((filename = strrchr(path, G_DIR_SEPARATOR)) != NULL) {
126 filename++;
127 *(strrchr(realpath, G_DIR_SEPARATOR)+1) = '\0';
128 } else {
129 filename = (char *) path;
130 g_free(realpath);
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);
135 if (tmp == NULL)
136 tmp = g_strdup(realpath);
137 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
138 g_free(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),
142 filename);
144 g_free(realpath);
145 } else {
146 gchar *tmp = NULL;
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);
151 if (tmp == NULL)
152 tmp = g_strdup(prefs_common.attach_load_dir);
153 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), tmp);
154 g_free(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);
163 slist_orig = slist;
165 if (slist) {
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';
174 if (!path)
175 prefs_common.attach_load_dir = g_filename_to_utf8(tmp, -1, NULL, NULL, NULL);
177 g_free(tmp);
180 while (slist) {
181 list = g_list_append(list, slist->data);
182 slist = slist->next;
185 if (slist_orig)
186 g_slist_free(slist_orig);
188 return list;
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,
202 const gchar *path,
203 const gchar *filter)
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,
217 const gchar *filter)
219 GList * list = filesel_create(title, path, FALSE, open, folder_mode, filter);
220 gchar * result = NULL;
221 if (list) {
222 result = g_strdup(list->data);
224 g_list_free(list);
225 return result;
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,
233 const gchar *filter)
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);