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