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