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