add parameter dcerpc_info to PIDL_dissect_ipv?address()
[wireshark-wip.git] / ui / gtk / file_dlg.c
blob88b3f84660f928da72254f60851c8fbab563ada6
1 /* file_dlg.c
2 * Utilities to use when constructing file selection dialogs
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * Code to handle Windows shortcuts courtesy of:
28 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
29 * Copyright (C) 1999-2012 Hiroyuki Yamamoto
31 * licensed under the GPL2 or later.
34 #include "config.h"
35 #include <string.h>
36 #include <errno.h>
38 #include <gtk/gtk.h>
40 #ifdef _WIN32
41 # define COBJMACROS
42 # include <windows.h>
43 # include <objbase.h>
44 # include <objidl.h>
45 # include <shlobj.h>
46 #endif
48 #include <wsutil/file_util.h>
50 #include <epan/filesystem.h>
52 #include "ui/last_open_dir.h"
53 #include "ui/util.h"
55 #include "ui/gtk/gtkglobals.h"
56 #include "ui/gtk/gui_utils.h"
57 #include "ui/gtk/file_dlg.h"
58 #include "ui/gtk/keys.h"
59 #include "ui/gtk/stock_icons.h"
62 static gchar *last_open_dir = NULL;
63 static gboolean updated_last_open_dir = FALSE;
65 static void file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* file_te);
67 /* Keys ... */
68 #define E_FS_CALLER_PTR_KEY "fs_caller_ptr"
70 /* Create a file selection dialog box window that belongs to a top-level
71 window. */
72 GtkWidget *
73 file_selection_new(const gchar *title, GtkWindow *parent,
74 file_selection_action_t action)
76 GtkWidget *win;
77 GtkFileChooserAction gtk_action;
78 #ifdef _WIN32
79 char *u3devicedocumentpath;
80 #endif
81 const gchar *ok_button_text;
83 switch (action) {
85 case FILE_SELECTION_OPEN:
86 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
87 ok_button_text = GTK_STOCK_OPEN;
88 break;
90 case FILE_SELECTION_READ_BROWSE:
91 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
92 ok_button_text = GTK_STOCK_OK;
93 break;
95 case FILE_SELECTION_SAVE:
96 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
97 ok_button_text = WIRESHARK_STOCK_SAVE;
98 break;
100 case FILE_SELECTION_WRITE_BROWSE:
101 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
102 ok_button_text = GTK_STOCK_OK;
103 break;
105 case FILE_SELECTION_CREATE_FOLDER:
106 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
107 ok_button_text = GTK_STOCK_OK;
108 break;
110 default:
111 g_assert_not_reached();
112 gtk_action = (GtkFileChooserAction)-1;
113 ok_button_text = NULL;
114 break;
116 win = gtk_file_chooser_dialog_new(title, parent, gtk_action,
117 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
118 ok_button_text, GTK_RESPONSE_ACCEPT,
119 NULL);
120 gtk_dialog_set_alternative_button_order(GTK_DIALOG(win),
121 GTK_RESPONSE_ACCEPT,
122 GTK_RESPONSE_CANCEL,
123 -1);
124 if (action == FILE_SELECTION_SAVE)
125 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(win), TRUE);
127 /* If we've opened a file before, start out by showing the files in the directory
128 in which that file resided. */
129 if (last_open_dir)
130 file_selection_set_current_folder(win, last_open_dir);
131 #ifdef _WIN32
132 else {
133 u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
134 if(u3devicedocumentpath != NULL)
135 file_selection_set_current_folder(win, u3devicedocumentpath);
138 #endif
139 return win;
142 /* Set the current folder for a file selection dialog. */
143 gboolean
144 file_selection_set_current_folder(GtkWidget *fs, const gchar *filename)
146 gboolean ret;
147 size_t filename_len = strlen(filename);
148 gchar *new_filename;
150 /* trim filename, so gtk_file_chooser_set_current_folder() likes it, see below */
151 if (filename[filename_len -1] == G_DIR_SEPARATOR
152 #ifdef _WIN32
153 && filename_len > 3) /* e.g. "D:\" */
154 #else
155 && filename_len > 1) /* e.g. "/" */
156 #endif
158 new_filename = g_strdup(filename);
159 new_filename[filename_len-1] = '\0';
160 } else {
161 new_filename = g_strdup(filename);
164 /* this function is very pedantic about its filename parameter */
165 /* no trailing '\' allowed, unless a win32 root dir "D:\" */
166 ret = gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fs), new_filename);
167 g_free(new_filename);
168 return ret;
171 /* Set the "extra" widget for a file selection dialog, with user-supplied
172 options. */
173 void
174 file_selection_set_extra_widget(GtkWidget *fs, GtkWidget *extra)
176 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(fs), extra);
179 #ifdef _WIN32
180 static gchar *filesel_get_link(const gchar *link_file)
182 WIN32_FIND_DATAW wfd;
183 IShellLinkW *psl;
184 IPersistFile *ppf;
185 wchar_t *wlink_file;
186 wchar_t wtarget[MAX_PATH];
187 gchar *target = NULL;
189 wtarget[0] = 0L;
191 CoInitialize(NULL);
192 if (S_OK == CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
193 &IID_IShellLinkW, (void **)&psl)) {
194 if (S_OK == IShellLinkW_QueryInterface(psl, &IID_IPersistFile,
195 (void **)&ppf)) {
196 wlink_file = g_utf8_to_utf16(link_file, -1, NULL, NULL, NULL);
197 if (S_OK == IPersistFile_Load(ppf, wlink_file, STGM_READ)) {
198 if (S_OK == IShellLinkW_GetPath(psl, wtarget, MAX_PATH, &wfd,
199 SLGP_UNCPRIORITY)) {
200 target = g_utf16_to_utf8(wtarget, -1, NULL, NULL, NULL);
203 IPersistFile_Release(ppf);
204 g_free(wlink_file);
206 IShellLinkW_Release(psl);
208 CoUninitialize();
210 return target;
212 #endif /* _WIN32 */
214 /* Run the dialog, and handle some common operations, such as, if the
215 user selects a directory, browsing that directory, and handling
216 shortcuts on Windows.
218 Returns NULL if the user decided not to open/write to a file,
219 returns the pathname of the selected file if they selected a
220 file. */
221 gchar *
222 file_selection_run(GtkWidget *fs)
224 gchar *cf_name;
225 #ifdef _WIN32
226 gchar *target;
227 const gchar *ext;
228 #endif
230 for (;;) {
231 if (gtk_dialog_run(GTK_DIALOG(fs)) != GTK_RESPONSE_ACCEPT) {
232 /* They clicked "Cancel" or closed the dialog or...;
233 destroy the dialog and tell our caller the user decided
234 not to do anything with the file. */
235 window_destroy(fs);
236 return NULL;
239 cf_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs));
241 /* Perhaps the user specified a directory instead of a file.
242 Check whether they did. */
243 if (test_for_directory(cf_name) == EISDIR) {
244 /* It's a directory - set the file selection box to display that
245 directory, and go back and re-run it; don't try to open the
246 directory as a file (you'll get crap if you get anything) or
247 write to it (which won't work anyway). */
248 set_last_open_dir(cf_name);
249 g_free(cf_name);
250 file_selection_set_current_folder(fs, get_last_open_dir());
251 continue;
254 #ifdef _WIN32
255 /* Perhaps the user specified a "shortcut" instead of a file.
256 Check whether they did. */
257 if ((ext = strrchr(cf_name, '.')) && g_ascii_strcasecmp(ext, ".lnk") == 0) {
258 /* It ends with ".lnk", so it might be a shortcut. */
259 target = filesel_get_link(cf_name);
260 if (target != NULL) {
261 /* We resolved it, so it must've been a shortcut. */
262 g_free(cf_name);
263 if (test_for_directory(target)) {
264 /* It's a shortcut that points to a directory; treat it the same
265 way we treat a directory. */
266 set_last_open_dir(target);
267 g_free(target);
268 file_selection_set_current_folder(fs, get_last_open_dir());
269 continue;
271 /* It's a shortcut that points to a file; act as if the target
272 is what's selected. */
273 cf_name = target;
276 #endif
277 break;
280 return cf_name;
283 #ifndef _WIN32
284 /* If the specified file doesn't exist, return TRUE.
285 If it exists and is neither user-immutable nor not writable, return
286 TRUE.
287 Otherwise, as the user whether they want to overwrite it anyway, and
288 return TRUE if the file should be overwritten and FALSE otherwise. */
289 gboolean
290 file_target_unwritable_ui(GtkWidget *chooser_w, char *cf_name)
292 GtkWidget *msg_dialog;
293 gchar *display_basename;
294 gint response;
295 ws_statb64 statbuf;
297 /* Check whether the file has all the write permission bits clear
298 and, on systems that have the 4.4-Lite file flags, whether it
299 has the "user immutable" flag set. Treat both of those as an
300 indication that the user wants to protect the file from
301 casual overwriting, and ask the user if they want to override
302 that.
304 (Linux's "immutable" flag, as fetched and set by the appropriate
305 ioctls (FS_IOC_GETFLAGS/FS_IOC_SETFLAGS in newer kernels,
306 EXT2_IOC_GETFLAGS/EXT2_IOC_SETFLAGS in older kernels - non-ext2
307 file systems that support those ioctls use the same values as ext2
308 does), appears to be more like the *BSD/OS X "system immutable"
309 flag, as it can be set only by the superuser or by processes with
310 CAP_LINUX_IMMUTABLE, so it sounds as if it's not intended for
311 arbitrary users to set or clear. */
312 if (ws_stat64(cf_name, &statbuf) == -1) {
313 /* Either the file doesn't exist or we can't get its attributes.
314 In the former case, we have no reason to bother the user.
315 In the latter case, we don't have enough information to
316 know whether to bother the user, so we don't. */
317 return TRUE;
320 /* OK, we have the permission bits and, if HAVE_ST_FLAGS is defined,
321 the flags. (If we don't, we don't worry about it.) */
322 #ifdef HAVE_ST_FLAGS
323 if (statbuf.st_flags & UF_IMMUTABLE) {
324 display_basename = g_filename_display_basename(cf_name);
325 msg_dialog = gtk_message_dialog_new(GTK_WINDOW(chooser_w),
326 (GtkDialogFlags)(GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT),
327 GTK_MESSAGE_QUESTION,
328 GTK_BUTTONS_NONE,
329 #ifdef __APPLE__
330 /* Stuff in the OS X UI calls files with the "user immutable" bit
331 "locked"; pre-OS X Mac software might have had that notion and
332 called it "locked". */
333 "The file \"%s\" is locked.",
334 #else /* __APPLE__ */
335 /* Just call it "immutable" in *BSD. */
336 "The file \"%s\" is immutable.",
337 #endif /* __APPLE__ */
338 display_basename);
339 g_free(display_basename);
340 } else
341 #endif /* HAVE_ST_FLAGS */
342 if ((statbuf.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0) {
343 display_basename = g_filename_display_basename(cf_name);
344 msg_dialog = gtk_message_dialog_new(GTK_WINDOW(chooser_w),
345 (GtkDialogFlags)(GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT),
346 GTK_MESSAGE_QUESTION,
347 GTK_BUTTONS_NONE,
348 "The file \"%s\" is read-only.",
349 display_basename);
350 g_free(display_basename);
351 } else {
352 /* No problem, just drive on. */
353 msg_dialog = NULL;
355 if (msg_dialog != NULL) {
356 /* OK, ask the user if they want to overwrite the file. */
357 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msg_dialog),
358 "Do you want to overwrite it anyway?");
360 gtk_dialog_add_buttons(GTK_DIALOG(msg_dialog),
361 "Overwrite", GTK_RESPONSE_ACCEPT,
362 "Don't overwrite", GTK_RESPONSE_REJECT,
363 NULL);
364 gtk_dialog_set_default_response(GTK_DIALOG(msg_dialog), GTK_RESPONSE_REJECT);
366 response = gtk_dialog_run(GTK_DIALOG(msg_dialog));
367 gtk_widget_destroy(msg_dialog);
369 if (response != GTK_RESPONSE_ACCEPT) {
370 /* The user doesn't want to overwrite this file. */
371 return FALSE;
374 #ifdef HAVE_ST_FLAGS
375 /* OK, they want to overwrite the file. If it has the "user
376 immutable" flag, we have to turn that off first, so we
377 can move on top of, or overwrite, the file. */
378 if (statbuf.st_flags & UF_IMMUTABLE) {
379 /* If this fails, the attempt to save will fail, so just
380 let that happen and pop up a "you lose" dialog. */
381 chflags(cf_name, statbuf.st_flags & ~UF_IMMUTABLE);
383 #endif
385 return TRUE;
387 #endif
390 * A generic select_file routine that is intended to be connected to
391 * a Browse button on other dialog boxes. This allows the user to browse
392 * for a file and select it. We fill in the text_entry that is given to us.
394 * We display the window label specified in our args.
396 void
397 file_selection_browse(GtkWidget *file_bt, GtkWidget *file_te, const char *label, file_selection_action_t action)
399 GtkWidget *caller = gtk_widget_get_toplevel(file_bt);
400 GtkWidget *fs;
401 gchar *f_name;
403 fs = file_selection_new(label, GTK_WINDOW(caller), action);
405 g_object_set_data(G_OBJECT(fs), PRINT_FILE_TE_KEY, file_te);
407 /* Set the E_FS_CALLER_PTR_KEY for the new dialog to point to our caller. */
408 g_object_set_data(G_OBJECT(fs), E_FS_CALLER_PTR_KEY, caller);
410 /* Set the E_FILE_SEL_DIALOG_PTR_KEY for the caller to point to us */
411 g_object_set_data(G_OBJECT(caller), E_FILE_SEL_DIALOG_PTR_KEY, fs);
413 /* Call a handler when the file selection box is destroyed, so we can inform
414 our caller, if any, that it's been destroyed. */
415 g_signal_connect(fs, "destroy", G_CALLBACK(file_selection_browse_destroy_cb),
416 file_te);
418 if (gtk_dialog_run(GTK_DIALOG(fs)) == GTK_RESPONSE_ACCEPT)
420 f_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs));
421 gtk_entry_set_text(GTK_ENTRY(file_te), f_name);
422 g_free(f_name);
424 window_destroy(fs);
428 static void
429 file_selection_browse_destroy_cb(GtkWidget *win, GtkWidget* parent_te)
431 GtkWidget *caller;
433 /* Get the widget that requested that we be popped up.
434 (It should arrange to destroy us if it's destroyed, so
435 that we don't get a pointer to a non-existent window here.) */
436 caller = (GtkWidget *)g_object_get_data(G_OBJECT(win), E_FS_CALLER_PTR_KEY);
438 /* Tell it we no longer exist. */
439 g_object_set_data(G_OBJECT(caller), E_FILE_SEL_DIALOG_PTR_KEY, NULL);
441 /* Give the focus to the file text entry widget so the user can just press
442 Return to print to the file. */
443 gtk_widget_grab_focus(parent_te);
447 void
448 set_last_open_dir(const char *dirname)
450 size_t len;
451 gchar *new_last_open_dir;
453 if (dirname) {
454 len = strlen(dirname);
455 if (dirname[len-1] == G_DIR_SEPARATOR) {
456 new_last_open_dir = g_strconcat(dirname, NULL);
458 else {
459 new_last_open_dir = g_strconcat(dirname,
460 G_DIR_SEPARATOR_S, NULL);
463 if (last_open_dir == NULL ||
464 strcmp(last_open_dir, new_last_open_dir) != 0)
465 updated_last_open_dir = TRUE;
467 else {
468 new_last_open_dir = NULL;
469 if (last_open_dir != NULL)
470 updated_last_open_dir = TRUE;
473 g_free(last_open_dir);
474 last_open_dir = new_last_open_dir;
477 char *
478 get_last_open_dir(void)
480 return last_open_dir;