Most of the stuff in file_dlg.c is specific to capture files, so rename
[obnox/wireshark/wip.git] / gtk / drag_and_drop.c
1 /* dnd.c
2  * Drag and Drop
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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
29 #include <gtk/gtk.h>
30
31 #include "globals.h"
32
33 #include "gtkglobals.h"
34 #include "util.h"
35 #include "capture_file_dlg.h"
36 #include "../menu.h"
37 #include "compat_macros.h"
38 #include "file.h"
39 #include "simple_dialog.h"
40 #include "main.h"
41 #ifdef HAVE_LIBPCAP
42 #include "capture.h"
43 #endif
44 #include <epan/prefs.h>
45
46 #include <string.h>
47 #include <stdio.h>
48
49
50 enum { DND_TARGET_STRING, DND_TARGET_ROOTWIN, DND_TARGET_URL };
51
52 /* convert drag and drop URI to a local filename */
53 static gchar *
54 dnd_uri2filename(gchar *cf_name)
55 {
56     gchar     *src, *dest;
57     gint      ret;
58     guint     i;
59     gchar     esc[3];
60
61
62     /* Remove URI header.
63      * we have to remove the prefix to get a valid filename. */
64 #ifdef _WIN32
65     /*
66      * On win32 (at least WinXP), this prefix looks like (UNC):
67      * file:////servername/sharename/dir1/dir2/capture-file.cap
68      * or (local filename):
69      * file:///d:/dir1/dir2/capture-file.cap
70      */
71     if (strncmp("file:////", cf_name, 9) == 0) {
72         /* win32 UNC: now becoming: //servername/sharename/dir1/dir2/capture-file.cap */
73         cf_name += 7;
74     } else if (strncmp("file:///", cf_name, 8) == 0) {
75         /* win32 local: now becoming: d:/dir1/dir2/capture-file.cap */
76         cf_name += 8;
77     }
78 #else
79     /*
80      * On UNIX (at least KDE 3.0 Konqueror), this prefix looks like:
81      * file:/dir1/dir2/capture-file.cap
82      *
83      * On UNIX (at least GNOME Nautilus 2.8.2), this prefix looks like:
84      * file:///dir1/dir2/capture-file.cap
85      */ 
86     if (strncmp("file:", cf_name, 5) == 0) {
87         /* now becoming: /dir1/dir2/capture-file.cap or ///dir1/dir2/capture-file.cap */
88         cf_name += 5;
89         /* shorten //////thing to /thing */
90         for(; cf_name[1] == '/'; ++cf_name);
91     }
92 #endif
93
94     /* 
95      * unescape the escaped URI characters (spaces, ...)
96      *
97      * we have to replace escaped chars to their equivalents, 
98      * e.g. %20 (always a two digit hexstring) -> ' '
99      * the percent character '%' is escaped be a double one "%%"
100      *
101      * we do this conversation "in place" as the result is always 
102      * equal or smaller in size.
103      */
104     src = cf_name;
105     dest = cf_name;
106     while (*src) {
107         if (*src == '%') {
108             src++;
109             if (*src == '%') {
110                 /* this is an escaped '%' char (was: "%%") */
111                 *dest = *src;
112                 src++;
113                 dest++;
114             } else {
115                 /* convert escaped hexnumber to unscaped character */
116                 esc[0] = src[0];
117                 esc[1] = src[1];
118                 esc[2] = '\0';
119                 ret = sscanf(esc, "%x", &i);
120                 if (ret == 1) {
121                     src+=2;
122                     *dest = (gchar) i;
123                     dest++;
124                 } else {
125                     /* somethings wrong, just jump over that char
126                      * this will result in a wrong string, but we might get
127                      * user feedback and can fix it later ;-) */
128                     src++;
129                 }
130             }
131         } else {
132             *dest = *src;
133             src++;
134             dest++;
135         }
136     }
137     *dest = '\0';
138
139     return cf_name;
140 }
141
142 static void
143 dnd_merge_files(int in_file_count, char **in_filenames)
144 {
145     char *tmpname;
146     cf_status_t merge_status;
147     int err;
148
149     /* merge the files in chonological order */
150     tmpname = NULL;
151     merge_status = cf_merge_files(&tmpname, in_file_count, in_filenames,
152                               WTAP_FILE_PCAP, FALSE);
153
154     if (merge_status != CF_OK) {
155         /* merge failed */
156         g_free(tmpname);
157         return;
158     }
159
160     cf_close(&cfile);
161
162     /* Try to open the merged capture file. */
163     if (cf_open(&cfile, tmpname, TRUE /* temporary file */, &err) != CF_OK) {
164         /* We couldn't open it; don't dismiss the open dialog box,
165            just leave it around so that the user can, after they
166            dismiss the alert box popped up for the open error,
167            try again. */
168         g_free(tmpname);
169         return;
170     }
171     g_free(tmpname);
172
173     switch (cf_read(&cfile)) {
174
175     case CF_READ_OK:
176     case CF_READ_ERROR:
177         /* Just because we got an error, that doesn't mean we were unable
178            to read any of the file; we handle what we could get from the
179            file. */
180         break;
181
182     case CF_READ_ABORTED:
183         /* The user bailed out of re-reading the capture file; the
184            capture file has been closed - just free the capture file name
185            string and return (without changing the last containing
186            directory). */
187         return;
188     }
189
190     gtk_widget_grab_focus(packet_list);
191 }
192
193 /* open/merge the dnd file */
194 void
195 dnd_open_file_cmd(gchar *cf_names_freeme)
196 {
197     int       err;
198     gchar     *cf_name;
199     int       in_files;
200     gpointer  dialog;
201     GString   *dialog_text;
202     int       files_work;
203     char      **in_filenames;
204
205     
206     /* DND_TARGET_URL on Win32:
207      * The cf_name_freeme is a single string, containing one or more URI's,
208      * seperated by CR/NL chars. The length of the whole field can be found 
209      * in the selection_data->length field. If it contains one file, simply open it,
210      * If it contains more than one file, ask to merge these files. */
211
212     /* count the number of input files */
213     cf_name = cf_names_freeme;
214     for(in_files = 0; (cf_name = strstr(cf_name, "\r\n")) != NULL; ) {
215         cf_name += 2;
216         in_files++;
217     }
218
219     in_filenames = g_malloc(sizeof(char*) * in_files);
220
221     /* store the starts of the file entries in a gchar array */
222     cf_name = cf_names_freeme;
223     in_filenames[0] = cf_name;
224     for(files_work = 1; (cf_name = strstr(cf_name, "\r\n")) != NULL && files_work < in_files; ) {
225         cf_name += 2;
226         in_filenames[files_work] = cf_name;
227         files_work++;
228     }
229
230     /* replace trailing CR NL simply with zeroes (in place), so we get valid terminated strings */
231     cf_name = cf_names_freeme;
232     g_strdelimit(cf_name, "\r\n", '\0');
233
234     /* convert all filenames from URI to local filename (in place) */
235     for(files_work = 0; files_work < in_files; files_work++) {
236         in_filenames[files_work] = dnd_uri2filename(in_filenames[files_work]);
237     }
238
239     switch(in_files) {
240     case(0):
241         /* shouldn't happen */
242         break;
243     case(1):
244         /* open and read the capture file (this will close an existing file) */
245         if (cf_open(&cfile, in_filenames[0], FALSE, &err) == CF_OK) {
246           /* XXX - add this to the menu if the read fails? */
247           cf_read(&cfile);
248           add_menu_recent_capture_file(in_filenames[0]);
249         } else {
250           /* the capture file couldn't be read (doesn't exist, file format unknown, ...) */
251         }
252         break;
253     default:
254         /* build and show the info dialog */
255         dialog_text = g_string_sized_new(200);
256         g_string_append(dialog_text, PRIMARY_TEXT_START 
257             "Merging the following files:" PRIMARY_TEXT_END "\n\n");
258         for(files_work = 0; files_work < in_files; files_work++) {
259             g_string_append(dialog_text, in_filenames[files_work]);
260             g_string_append(dialog_text, "\n");
261         }
262         g_string_append(dialog_text, "\nThe packets in these files will be merged chronologically into a new temporary file.");
263         dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
264                     ESD_BTN_OK,
265                     dialog_text->str);
266         g_string_free(dialog_text, TRUE);
267
268         /* actually merge the files now */
269         dnd_merge_files(in_files, in_filenames);
270     }
271
272     g_free(in_filenames);
273     g_free(cf_names_freeme);
274 }
275
276 /* ask the user to save current unsaved file, before opening the dnd file */
277 static void 
278 dnd_save_file_answered_cb(gpointer dialog _U_, gint btn, gpointer data)
279 {
280     switch(btn) {
281     case(ESD_BTN_SAVE):
282         /* save file first */
283         file_save_as_cmd(after_save_open_dnd_file, data);
284         break;
285     case(ESD_BTN_DONT_SAVE):
286         cf_close(&cfile);
287         dnd_open_file_cmd(data);
288         break;
289     case(ESD_BTN_CANCEL):
290         break;
291     default:
292         g_assert_not_reached();
293     }
294 }
295
296
297 /* we have received some drag and drop data */
298 /* (as we only registered to "text/uri-list", we will only get a file list here) */
299 static void 
300 dnd_data_received(GtkWidget *widget _U_, GdkDragContext *dc _U_, gint x _U_, gint y _U_, 
301 GtkSelectionData *selection_data, guint info, guint t _U_, gpointer data _U_)
302 {
303     gpointer  dialog;
304         gchar *cf_names_freeme;
305
306     if (info == DND_TARGET_URL) {
307         /* Usually we block incoming events by disabling the corresponding menu/toolbar items. 
308          * This is the only place where an incoming event won't be blocked in such a way, 
309          * so we have to take care of NOT loading a new file while a different process 
310          * (e.g. capture/load/...) is still in progress. */
311
312 #ifdef HAVE_LIBPCAP
313         /* if a capture is running, do nothing but warn the user */
314         if((capture_opts->state != CAPTURE_STOPPED)) {
315             dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
316                         ESD_BTN_OK,
317                         PRIMARY_TEXT_START "Drag and Drop currently not possible!" PRIMARY_TEXT_END "\n\n"
318                         "Dropping a file isn't possible while a capture is in progress.");
319             return;
320         }
321 #endif
322
323         /* if another file read is still in progress, do nothing but warn the user */
324         if((cfile.state == FILE_READ_IN_PROGRESS)) {
325             dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
326                         ESD_BTN_OK,
327                         PRIMARY_TEXT_START "Drag and Drop currently not possible!" PRIMARY_TEXT_END "\n\n"
328                         "Dropping a file isn't possible while loading another capture file.");
329             return;
330         }
331
332                 /* the selection_data will soon be gone, make a copy first */
333                 /* the data string is not zero terminated -> make a zero terminated "copy" of it */
334                 cf_names_freeme = g_malloc(selection_data->length + 1);
335                 memcpy(cf_names_freeme, selection_data->data, selection_data->length);
336                 cf_names_freeme[selection_data->length] = '\0';
337
338         /* ask the user to save it's current capture file first */
339         if((cfile.state != FILE_CLOSED) && !cfile.user_saved && prefs.gui_ask_unsaved) {
340             /* user didn't saved his current file, ask him */
341             dialog = simple_dialog(ESD_TYPE_CONFIRMATION,
342                         ESD_BTNS_SAVE_DONTSAVE_CANCEL,
343                         PRIMARY_TEXT_START "Save capture file before opening a new one?" PRIMARY_TEXT_END "\n\n"
344                         "If you open a new capture file without saving, your current capture data will be discarded.");
345             simple_dialog_set_cb(dialog, dnd_save_file_answered_cb, cf_names_freeme );
346         } else {
347             /* unchanged file */
348             dnd_open_file_cmd( cf_names_freeme );
349         }
350     }
351 }
352
353 /* init the drag and drop functionality */
354 void 
355 dnd_init(GtkWidget *w)
356 {
357     /* we are only interested in the URI list containing filenames */
358     static GtkTargetEntry target_entry[] = {
359          /*{"STRING", 0, DND_TARGET_STRING},*/
360          /*{"text/plain", 0, DND_TARGET_STRING},*/
361          {"text/uri-list", 0, DND_TARGET_URL}
362     };
363
364     /* set this window as a dnd destination */
365     gtk_drag_dest_set(
366          w, GTK_DEST_DEFAULT_ALL, target_entry,
367          sizeof(target_entry) / sizeof(GtkTargetEntry),
368          (GdkDragAction)(GDK_ACTION_MOVE | GDK_ACTION_COPY) );
369
370     /* get notified, if some dnd coming in */
371     gtk_signal_connect(GTK_OBJECT(w), "drag_data_received",
372         GTK_SIGNAL_FUNC(dnd_data_received), NULL);
373 }
374
375