root / src / filesel.c @ 601
History | View | Annotate | Download (6.2 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 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 | #include <glib.h> |
| 21 | #include <glib/gi18n.h> |
| 22 | #include <gdk/gdkkeysyms.h> |
| 23 | #include <gtk/gtkfilechooserdialog.h> |
| 24 | #include <gtk/gtkexpander.h> |
| 25 | #include <gtk/gtkstock.h> |
| 26 | |
| 27 | #include "main.h" |
| 28 | #include "filesel.h" |
| 29 | #include "manage_window.h" |
| 30 | #include "alertpanel.h" |
| 31 | #include "utils.h" |
| 32 | #include "prefs_common.h" |
| 33 | |
| 34 | static GSList *filesel_select_file_full (const gchar *title, |
| 35 | const gchar *file,
|
| 36 | GtkFileChooserAction action, |
| 37 | gboolean multiple); |
| 38 | |
| 39 | static GtkWidget *filesel_create (const gchar *title, |
| 40 | GtkFileChooserAction action); |
| 41 | |
| 42 | static void filesel_save_expander_set_expanded (GtkWidget *dialog, |
| 43 | gboolean expanded); |
| 44 | static gboolean filesel_save_expander_get_expanded (GtkWidget *dialog);
|
| 45 | |
| 46 | gchar *filesel_select_file(const gchar *title, const gchar *file, |
| 47 | GtkFileChooserAction action) |
| 48 | {
|
| 49 | GSList *list; |
| 50 | gchar *selected = NULL;
|
| 51 | |
| 52 | list = filesel_select_file_full(title, file, action, FALSE); |
| 53 | if (list) {
|
| 54 | selected = (gchar *)list->data; |
| 55 | slist_free_strings(list->next); |
| 56 | } |
| 57 | g_slist_free(list); |
| 58 | |
| 59 | return selected;
|
| 60 | } |
| 61 | |
| 62 | GSList *filesel_select_files(const gchar *title, const gchar *file, |
| 63 | GtkFileChooserAction action) |
| 64 | {
|
| 65 | return filesel_select_file_full(title, file, action, TRUE);
|
| 66 | } |
| 67 | |
| 68 | static GSList *filesel_select_file_full(const gchar *title, const gchar *file, |
| 69 | GtkFileChooserAction action, |
| 70 | gboolean multiple) |
| 71 | {
|
| 72 | static GHashTable *path_table = NULL; |
| 73 | gchar *cwd; |
| 74 | GtkWidget *dialog; |
| 75 | gchar *prev_dir; |
| 76 | static gboolean save_expander_expanded = FALSE;
|
| 77 | GSList *list = NULL;
|
| 78 | |
| 79 | if (!path_table)
|
| 80 | path_table = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 81 | g_free, g_free); |
| 82 | |
| 83 | prev_dir = g_get_current_dir(); |
| 84 | |
| 85 | if ((cwd = g_hash_table_lookup(path_table, title)) != NULL) |
| 86 | change_dir(cwd); |
| 87 | else
|
| 88 | change_dir(startup_dir); |
| 89 | |
| 90 | dialog = filesel_create(title, action); |
| 91 | |
| 92 | change_dir(prev_dir); |
| 93 | g_free(prev_dir); |
| 94 | |
| 95 | manage_window_set_transient(GTK_WINDOW(dialog)); |
| 96 | |
| 97 | if (file)
|
| 98 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), |
| 99 | file); |
| 100 | |
| 101 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), |
| 102 | multiple); |
| 103 | |
| 104 | if (action == GTK_FILE_CHOOSER_ACTION_SAVE && save_expander_expanded) {
|
| 105 | filesel_save_expander_set_expanded |
| 106 | (dialog, save_expander_expanded); |
| 107 | } |
| 108 | |
| 109 | gtk_widget_show(dialog); |
| 110 | |
| 111 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
|
| 112 | list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); |
| 113 | if (list) {
|
| 114 | cwd = gtk_file_chooser_get_current_folder |
| 115 | (GTK_FILE_CHOOSER(dialog)); |
| 116 | if (cwd)
|
| 117 | g_hash_table_replace |
| 118 | (path_table, g_strdup(title), cwd); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (action == GTK_FILE_CHOOSER_ACTION_SAVE)
|
| 123 | save_expander_expanded = |
| 124 | filesel_save_expander_get_expanded(dialog); |
| 125 | |
| 126 | manage_window_focus_out(dialog, NULL, NULL); |
| 127 | gtk_widget_destroy(dialog); |
| 128 | |
| 129 | return list;
|
| 130 | } |
| 131 | |
| 132 | gchar *filesel_save_as(const gchar *file)
|
| 133 | {
|
| 134 | gchar *filename; |
| 135 | |
| 136 | filename = filesel_select_file(_("Save as"), file,
|
| 137 | GTK_FILE_CHOOSER_ACTION_SAVE); |
| 138 | |
| 139 | if (filename && is_file_exist(filename)) {
|
| 140 | AlertValue aval; |
| 141 | |
| 142 | aval = alertpanel(_("Overwrite"),
|
| 143 | _("Overwrite existing file?"),
|
| 144 | GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL);
|
| 145 | if (G_ALERTDEFAULT != aval) {
|
| 146 | g_free(filename); |
| 147 | filename = NULL;
|
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return filename;
|
| 152 | } |
| 153 | |
| 154 | gchar *filesel_select_dir(const gchar *dir)
|
| 155 | {
|
| 156 | GSList *list; |
| 157 | gchar *selected = NULL;
|
| 158 | |
| 159 | list = filesel_select_file_full(_("Select directory"), dir,
|
| 160 | GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, |
| 161 | FALSE); |
| 162 | if (list) {
|
| 163 | selected = (gchar *)list->data; |
| 164 | slist_free_strings(list->next); |
| 165 | } |
| 166 | g_slist_free(list); |
| 167 | |
| 168 | return selected;
|
| 169 | } |
| 170 | |
| 171 | static GtkWidget *filesel_create(const gchar *title, |
| 172 | GtkFileChooserAction action) |
| 173 | {
|
| 174 | GtkWidget *dialog; |
| 175 | |
| 176 | if (prefs_common.comply_gnome_hig)
|
| 177 | dialog = gtk_file_chooser_dialog_new |
| 178 | (title, NULL, action,
|
| 179 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 180 | (action == GTK_FILE_CHOOSER_ACTION_SAVE || |
| 181 | action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) |
| 182 | ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
| 183 | NULL);
|
| 184 | else
|
| 185 | dialog = gtk_file_chooser_dialog_new |
| 186 | (title, NULL, action,
|
| 187 | (action == GTK_FILE_CHOOSER_ACTION_SAVE || |
| 188 | action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER) |
| 189 | ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
| 190 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 191 | NULL);
|
| 192 | gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); |
| 193 | gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
| 194 | gtk_window_set_wmclass |
| 195 | (GTK_WINDOW(dialog), "file_selection", "Sylpheed"); |
| 196 | gtk_dialog_set_default_response |
| 197 | (GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); |
| 198 | |
| 199 | MANAGE_WINDOW_SIGNALS_CONNECT(dialog); |
| 200 | |
| 201 | return dialog;
|
| 202 | } |
| 203 | |
| 204 | static void container_foreach_cb(GtkWidget *widget, gpointer data) |
| 205 | {
|
| 206 | GtkWidget **expander = (GtkWidget **)data; |
| 207 | |
| 208 | if (*expander == NULL) { |
| 209 | if (GTK_IS_EXPANDER(widget))
|
| 210 | *expander = widget; |
| 211 | else if (GTK_IS_CONTAINER(widget)) |
| 212 | gtk_container_foreach(GTK_CONTAINER(widget), |
| 213 | container_foreach_cb, data); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static void filesel_save_expander_set_expanded(GtkWidget *dialog, |
| 218 | gboolean expanded) |
| 219 | {
|
| 220 | GtkWidget *expander = NULL;
|
| 221 | |
| 222 | gtk_container_foreach(GTK_CONTAINER(dialog), container_foreach_cb, |
| 223 | &expander); |
| 224 | if (expander)
|
| 225 | gtk_expander_set_expanded(GTK_EXPANDER(expander), expanded); |
| 226 | } |
| 227 | |
| 228 | static gboolean filesel_save_expander_get_expanded(GtkWidget *dialog)
|
| 229 | {
|
| 230 | GtkWidget *expander = NULL;
|
| 231 | |
| 232 | gtk_container_foreach(GTK_CONTAINER(dialog), container_foreach_cb, |
| 233 | &expander); |
| 234 | if (expander)
|
| 235 | return gtk_expander_get_expanded(GTK_EXPANDER(expander));
|
| 236 | else
|
| 237 | return FALSE;
|
| 238 | } |