6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <io.h> /* open/close on win32 */
38 #include "gtkglobals.h"
42 #include "compat_macros.h"
44 #include "simple_dialog.h"
45 #include <epan/prefs.h>
51 enum { DND_TARGET_STRING, DND_TARGET_ROOTWIN, DND_TARGET_URL };
53 /* convert drag and drop URI to a local filename */
55 dnd_uri2filename(gchar *cf_name)
65 * On win32 (at least WinXP), this string looks like (UNC or local filename):
66 * file:////servername/sharename/dir1/dir2/capture-file.cap
68 * file:///d:/dir1/dir2/capture-file.cap
69 * we have to remove the prefix to get a valid filename.
71 * On UNIX (at least KDE 3.0 Konqueror), this string looks like:
72 * file:/dir1/dir2/capture-file.cap
73 * we have to remove the file: to get a valid filename.
75 if (strncmp("file:////", cf_name, 9) == 0) {
76 /* win32 UNC: now becoming: //servername/sharename/dir1/dir2/capture-file.cap */
78 } else if (strncmp("file:///", cf_name, 8) == 0) {
79 /* win32 local: now becoming: d:/dir1/dir2/capture-file.cap */
81 } else if (strncmp("file:", cf_name, 5) == 0) {
82 /* unix local: now becoming: /dir1/dir2/capture-file.cap */
87 * unescape the escaped URI characters (spaces, ...)
89 * we have to replace escaped chars to their equivalents,
90 * e.g. %20 (always a two digit hexstring) -> ' '
91 * the percent character '%' is escaped be a double one "%%"
93 * we do this conversation "in place" as the result is always
94 * equal or smaller in size.
102 /* this is an escaped '%' char (was: "%%") */
107 /* convert escaped hexnumber to unscaped character */
111 ret = sscanf(esc, "%x", &i);
117 /* somethings wrong, just jump over that char
118 * this will result in a wrong string, but we might get
119 * user feedback and can fix it later ;-) */
135 dnd_merge_files(int in_file_count, char **in_filenames)
141 /* merge the files in chonological order */
143 merge_ok = cf_merge_files(&tmpname, in_file_count, in_filenames,
144 WTAP_FILE_PCAP, FALSE);
154 /* Try to open the merged capture file. */
155 if (cf_open(&cfile, tmpname, TRUE /* temporary file */, &err) != CF_OK) {
156 /* We couldn't open it; don't dismiss the open dialog box,
157 just leave it around so that the user can, after they
158 dismiss the alert box popped up for the open error,
165 switch (cf_read(&cfile)) {
169 /* Just because we got an error, that doesn't mean we were unable
170 to read any of the file; we handle what we could get from the
174 case CF_READ_ABORTED:
175 /* The user bailed out of re-reading the capture file; the
176 capture file has been closed - just free the capture file name
177 string and return (without changing the last containing
182 gtk_widget_grab_focus(packet_list);
185 /* open/merge the dnd file */
187 dnd_open_file_cmd(GtkSelectionData *selection_data)
190 gchar *cf_name, *cf_name_freeme;
193 GString *dialog_text;
198 /* DND_TARGET_URL on Win32:
199 * The selection_data->data is a single string, containing one or more URI's,
200 * seperated by CR/NL chars. The length of the whole field can be found
201 * in the selection_data->length field. If it contains one file, simply open it,
202 * If it contains more than one file, ask to merge these files. */
204 /* the data string is not zero terminated -> make a zero terminated "copy" of it */
205 cf_name_freeme = g_malloc(selection_data->length + 1);
206 memcpy(cf_name_freeme, selection_data->data, selection_data->length);
207 cf_name_freeme[selection_data->length] = '\0';
209 /* count the number of input files */
210 cf_name = cf_name_freeme;
211 for(in_files = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
216 in_filenames = g_malloc(sizeof(char*) * in_files);
218 /* store the starts of the file entries in a gchar array */
219 cf_name = cf_name_freeme;
220 in_filenames[0] = cf_name;
221 for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_files; ) {
223 in_filenames[files_work] = cf_name;
227 /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
228 cf_name = cf_name_freeme;
229 g_strdelimit(cf_name, "\r\n", '\0');
231 /* convert all filenames from URI to local filename (in place) */
232 for(files_work = 0; files_work < in_files; files_work++) {
233 in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
238 /* shouldn't happen */
241 /* open and read the capture file (this will close an existing file) */
242 if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
243 /* XXX - add this to the menu if the read fails? */
245 add_menu_recent_capture_file(in_filenames[0]);
247 /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
251 /* build and show the info dialog */
252 dialog_text = g_string_sized_new(200);
253 g_string_append(dialog_text, PRIMARY_TEXT_START
254 "Merging the following files:" PRIMARY_TEXT_END "\n\n");
255 for(files_work = 0; files_work < in_files; files_work++) {
256 g_string_append(dialog_text, in_filenames[files_work]);
257 g_string_append(dialog_text, "\n");
259 g_string_append(dialog_text, "\nThe packets in these files will be merged chronologically into a new temporary file.");
260 dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
263 g_string_free(dialog_text, TRUE);
265 /* actually merge the files now */
266 dnd_merge_files(in_files, in_filenames);
269 g_free(in_filenames);
270 g_free(cf_name_freeme);
273 /* ask the user to save current unsaved file, before opening the dnd file */
275 dnd_save_file_answered_cb(gpointer dialog _U_, gint btn, gpointer data _U_)
279 /* save file first */
280 file_save_as_cmd(after_save_open_dnd_file, data);
282 case(ESD_BTN_DONT_SAVE):
284 dnd_open_file_cmd(data);
286 case(ESD_BTN_CANCEL):
289 g_assert_not_reached();
294 /* we have received some drag and drop data */
295 /* (as we only registered to "text/uri-list", we will only get a file list here) */
297 dnd_data_received(GtkWidget *widget _U_, GdkDragContext *dc _U_, gint x _U_, gint y _U_,
298 GtkSelectionData *selection_data, guint info, guint t _U_, gpointer data _U_)
302 if (info == DND_TARGET_URL) {
303 /* ask the user to save it's current capture file first */
304 if((cfile.state != FILE_CLOSED) && !cfile.user_saved && prefs.gui_ask_unsaved) {
305 /* user didn't saved his current file, ask him */
306 dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
307 ESD_BTNS_SAVE_DONTSAVE_CANCEL,
308 PRIMARY_TEXT_START "Save capture file before opening a new one?" PRIMARY_TEXT_END "\n\n"
309 "If you open a new capture file without saving, your current capture data will be discarded.");
310 simple_dialog_set_cb(dialog, dnd_save_file_answered_cb, selection_data);
313 dnd_open_file_cmd(selection_data);
318 /* init the drag and drop functionality */
320 dnd_init(GtkWidget *w)
322 /* we are only interested in the URI list containing filenames */
323 static GtkTargetEntry target_entry[] = {
324 /*{"STRING", 0, DND_TARGET_STRING},*/
325 /*{"text/plain", 0, DND_TARGET_STRING},*/
326 {"text/uri-list", 0, DND_TARGET_URL}
329 /* set this window as a dnd destination */
331 w, GTK_DEST_DEFAULT_ALL, target_entry,
332 sizeof(target_entry) / sizeof(GtkTargetEntry),
333 (GdkDragAction)(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
335 /* get notified, if some dnd coming in */
336 gtk_signal_connect(GTK_OBJECT(w), "drag_data_received",
337 GTK_SIGNAL_FUNC(dnd_data_received), NULL);