Fix the wireless settings button for AirPCap devices in the
[obnox/wireshark/wip.git] / gtk / file_dlg.c
1 /* file_dlg.c
2  * Utilities to use when constructing file selection dialogs
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28 #include <string.h>
29 #include <errno.h>
30
31 #include <gtk/gtk.h>
32
33 #include <wsutil/file_util.h>
34
35 #include "gtk/gtkglobals.h"
36 #include "gtk/gui_utils.h"
37 #include "gtk/file_dlg.h"
38 #include "gtk/keys.h"
39
40
41 static gchar *last_open_dir = NULL;
42 static gboolean updated_last_open_dir = FALSE;
43
44 static void file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* file_te);
45
46 /* Keys ... */
47 #define E_FS_CALLER_PTR_KEY       "fs_caller_ptr"
48
49 /* Create a file selection dialog box window that belongs to Wireshark's
50    main window. */
51 GtkWidget *
52 file_selection_new(const gchar *title, file_selection_action_t action)
53 {
54     GtkWidget *win;
55     GtkFileChooserAction gtk_action;
56 #ifdef _WIN32
57     char *u3devicedocumentpath;
58 #endif
59     const gchar *ok_button_text;
60
61     switch (action) {
62
63     case FILE_SELECTION_OPEN:
64         gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
65         ok_button_text = GTK_STOCK_OPEN;
66         break;
67
68     case FILE_SELECTION_READ_BROWSE:
69         gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
70         ok_button_text = GTK_STOCK_OK;
71         break;
72
73     case FILE_SELECTION_SAVE:
74         gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
75         ok_button_text = GTK_STOCK_SAVE;
76         break;
77
78     case FILE_SELECTION_WRITE_BROWSE:
79         gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
80         ok_button_text = GTK_STOCK_OK;
81         break;
82
83     case FILE_SELECTION_CREATE_FOLDER:
84         gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
85         ok_button_text = GTK_STOCK_OK;
86         break;
87
88     default:
89         g_assert_not_reached();
90         gtk_action = -1;
91         ok_button_text = NULL;
92         break;
93     }
94     win = gtk_file_chooser_dialog_new(title, GTK_WINDOW(top_level), gtk_action,
95 #ifndef _WIN32
96                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
97                                       ok_button_text, GTK_RESPONSE_ACCEPT,
98 #else
99                                       ok_button_text, GTK_RESPONSE_ACCEPT,
100                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
101 #endif
102                                       NULL);
103
104     /* If we've opened a file before, start out by showing the files in the directory
105        in which that file resided. */
106     if (last_open_dir)
107         file_selection_set_current_folder(win, last_open_dir);
108 #ifdef _WIN32
109     else {
110         u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
111         if(u3devicedocumentpath != NULL)
112             file_selection_set_current_folder(win, u3devicedocumentpath);
113
114     }
115 #endif
116     return win;
117 }
118
119 /* Set the current folder for a file selection dialog. */
120 gboolean
121 file_selection_set_current_folder(GtkWidget *fs, const gchar *filename)
122 {
123     gboolean ret;
124     size_t filename_len = strlen(filename);
125     gchar *new_filename;
126
127     /* trim filename, so gtk_file_chooser_set_current_folder() likes it, see below */
128     if (filename[filename_len -1] == G_DIR_SEPARATOR
129 #ifdef _WIN32
130         && filename_len > 3)    /* e.g. "D:\" */
131 #else
132         && filename_len > 1)    /* e.g. "/" */
133 #endif
134     {
135         new_filename = g_strdup(filename);
136         new_filename[filename_len-1] = '\0';
137     } else {
138         new_filename = g_strdup(filename);
139     }
140
141     /* this function is very pedantic about it's filename parameter */
142     /* no trailing '\' allowed, unless a win32 root dir "D:\" */
143     ret = gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fs), new_filename);
144     g_free(new_filename);
145     return ret;
146 }
147
148 /* Set the "extra" widget for a file selection dialog, with user-supplied
149    options. */
150 void
151 file_selection_set_extra_widget(GtkWidget *fs, GtkWidget *extra)
152 {
153   gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(fs), extra);
154 }
155
156
157 /*
158  * A generic select_file routine that is intended to be connected to
159  * a Browse button on other dialog boxes. This allows the user to browse
160  * for a file and select it. We fill in the text_entry that is given to us.
161  *
162  * We display the window label specified in our args.
163  */
164 void
165 file_selection_browse(GtkWidget *file_bt, GtkWidget *file_te, const char *label, file_selection_action_t action)
166 {
167     GtkWidget *caller = gtk_widget_get_toplevel(file_bt);
168     GtkWidget *fs;
169     gchar     *f_name;
170
171     /* Has a file selection dialog box already been opened for that top-level
172        widget? */
173     fs = g_object_get_data(G_OBJECT(caller), E_FILE_SEL_DIALOG_PTR_KEY);
174     if (fs != NULL) {
175         /* Yes.  Just re-activate that dialog box. */
176         reactivate_window(fs);
177         return;
178     }
179
180     fs = file_selection_new(label, action);
181
182     g_object_set_data(G_OBJECT(fs), PRINT_FILE_TE_KEY, file_te);
183
184     /* Set the E_FS_CALLER_PTR_KEY for the new dialog to point to our caller. */
185     g_object_set_data(G_OBJECT(fs), E_FS_CALLER_PTR_KEY, caller);
186
187     /* Set the E_FILE_SEL_DIALOG_PTR_KEY for the caller to point to us */
188     g_object_set_data(G_OBJECT(caller), E_FILE_SEL_DIALOG_PTR_KEY, fs);
189
190     /* Call a handler when the file selection box is destroyed, so we can inform
191        our caller, if any, that it's been destroyed. */
192     g_signal_connect(fs, "destroy", G_CALLBACK(file_selection_browse_destroy_cb),
193                      file_te);
194
195     if (gtk_dialog_run(GTK_DIALOG(fs)) == GTK_RESPONSE_ACCEPT)
196     {
197         f_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs));
198         gtk_entry_set_text(GTK_ENTRY(file_te), f_name);
199         g_free(f_name);
200     }
201     window_destroy(fs);
202 }
203
204
205 static void
206 file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* parent_te)
207 {
208     GtkWidget *caller;
209
210     /* Get the widget that requested that we be popped up.
211        (It should arrange to destroy us if it's destroyed, so
212        that we don't get a pointer to a non-existent window here.) */
213     caller = g_object_get_data(G_OBJECT(win), E_FS_CALLER_PTR_KEY);
214
215     /* Tell it we no longer exist. */
216     g_object_set_data(G_OBJECT(caller), E_FILE_SEL_DIALOG_PTR_KEY, NULL);
217
218     /* Give the focus to the file text entry widget so the user can just press
219        Return to print to the file. */
220     gtk_widget_grab_focus(parent_te);
221 }
222
223
224 void
225 set_last_open_dir(const char *dirname)
226 {
227     size_t len;
228     gchar *new_last_open_dir;
229
230     if (dirname) {
231         len = strlen(dirname);
232         if (dirname[len-1] == G_DIR_SEPARATOR) {
233             new_last_open_dir = g_strconcat(dirname, NULL);
234         }
235         else {
236             new_last_open_dir = g_strconcat(dirname,
237                                             G_DIR_SEPARATOR_S, NULL);
238         }
239
240         if (last_open_dir == NULL ||
241             strcmp(last_open_dir, new_last_open_dir) != 0)
242             updated_last_open_dir = TRUE;
243     }
244     else {
245         new_last_open_dir = NULL;
246         if (last_open_dir != NULL)
247             updated_last_open_dir = TRUE;
248     }
249
250     g_free(last_open_dir);
251     last_open_dir = new_last_open_dir;
252 }
253
254 char *
255 get_last_open_dir(void)
256 {
257     return last_open_dir;
258 }