Get rid of a bunch of "Ethereal"s and "ethereal"s in comments, GUI
[obnox/wireshark/wip.git] / gtk / fileset_dlg.c
1 /* fileset_dlg.c
2  * Routines for the file set dialog
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 #include <string.h>
30
31 #ifdef HAVE_FCNTL_H
32 #include <fcntl.h>
33 #endif
34
35 #include <gtk/gtk.h>
36
37 #include "globals.h"
38
39
40 #include "compat_macros.h"
41 #include "simple_dialog.h"
42
43 #include "gui_utils.h"
44 #include "dlg_utils.h"
45
46 #include "main.h"
47 #include "menu.h"
48 #include "help_dlg.h"
49
50 #include <epan/filesystem.h>
51
52 #include "fileset.h"
53 #include "fileset_dlg.h"
54
55
56
57 /*
58  * Keep a static pointer to the current "File Set" window, if
59  * any, so that if somebody tries to do "File Set" while there's
60  * already a "File Set" window up, we just pop up the existing
61  * one, rather than creating a new one.
62  */
63 static GtkWidget *fs_w;
64
65
66
67 /* various widget related global data */
68 int           row;
69 GtkWidget     *fs_tb;
70 GtkTooltips   *tooltips;
71 GtkWidget     *fs_dir_lb;
72 GtkWidget     *fs_first_rb;
73 GtkWidget     *fs_tb_vb;
74
75
76
77 /* open the file corresponding to the given fileset entry */
78 static void
79 fs_open_entry(fileset_entry *entry)
80 {
81     char            *fname;
82     int             err;
83
84
85     /* make a copy of the filename (cf_close will indirectly destroy it right now) */
86     fname = g_strdup(entry->fullname);
87
88     /* close the old and open the new file */
89     cf_close(&cfile);
90     if (cf_open(&cfile, fname, FALSE, &err) == CF_OK) {
91         cf_read(&cfile);
92     }
93
94     g_free(fname);
95 }
96
97
98 /* radio button was pressed/released */
99 static void
100 fs_rb_cb(GtkWidget *open_bt, gpointer fs_data)
101 {
102     fileset_entry   *entry = fs_data;
103
104     /* button release should have no effect */
105     if(!gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(open_bt) )) {
106         return;
107     }
108
109     fs_open_entry(entry);
110 }
111
112
113 /* the window was closed, cleanup things */
114 static void
115 fs_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
116 {
117     /* Note that we no longer have a "File Set" dialog box. */
118     fs_w = NULL;
119 }
120
121
122 /* get creation date (converted from filename) */
123 /* */
124 static char *
125 fileset_dlg_name2date_dup(const char * name) {
126     char        *pfx;
127     char        *filename;
128     int         pos;
129
130
131     /* just to be sure ... */
132         if(!fileset_filename_match_pattern(name)) {
133                 return NULL;
134         }
135
136     /* find char position behind the last underscore */
137     pfx = strrchr(name, '_');
138     pfx++;
139     pos = pfx - name;
140
141     /* start conversion behind that underscore */
142     filename = g_strdup_printf("%c%c%c%c.%c%c.%c%c %c%c:%c%c:%c%c",
143         /* year  */  name[pos]  ,  name[pos+1], name[pos+2], name[pos+3],
144         /* month */  name[pos+4],  name[pos+5],
145         /* day   */  name[pos+6],  name[pos+7],
146         /* hour */   name[pos+8],  name[pos+9],
147         /* min */    name[pos+10], name[pos+11],
148         /* second */ name[pos+12], name[pos+13]);
149
150     return filename;
151 }
152
153
154 /* this file is a part of the current file set, add it to the dialog */
155 void
156 fileset_dlg_add_file(fileset_entry *entry) {
157     char *created;
158     char *modified;
159     char *size;
160     struct tm *local;
161     GtkWidget     *fs_lb;
162     GtkWidget     *fs_rb;
163     gchar *title;
164
165
166     if (fs_w == NULL) {
167         return;
168     }
169
170     created = fileset_dlg_name2date_dup(entry->name);
171         if(!created) {
172                 /* if this file doesn't follow the fiel set pattern, */
173                 /* use the creation time of that file */
174                 local = localtime(&entry->ctime);
175                 created = g_strdup_printf("%04u.%02u.%02u %02u:%02u:%02u", 
176                         local->tm_year+1900, local->tm_mon+1, local->tm_mday,
177                         local->tm_hour, local->tm_min, local->tm_sec);
178         }
179
180     local = localtime(&entry->mtime);
181     modified = g_strdup_printf("%04u.%02u.%02u %02u:%02u:%02u", 
182         local->tm_year+1900, local->tm_mon+1, local->tm_mday,
183         local->tm_hour, local->tm_min, local->tm_sec);
184     size = g_strdup_printf("%ld Bytes", entry->size);
185
186     fs_rb = RADIO_BUTTON_NEW_WITH_LABEL(fs_first_rb, entry->name);
187     if(row == 1) {
188         fs_first_rb = fs_rb;
189     }
190     if(entry->current) {
191         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (fs_rb), entry->current);
192     }
193     gtk_tooltips_set_tip(tooltips, fs_rb, "Open this capture file", NULL);
194     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_rb, 0, 1, row, row+1);
195     SIGNAL_CONNECT(fs_rb, "toggled", fs_rb_cb, entry);
196     gtk_widget_show(fs_rb);
197
198     fs_lb = gtk_label_new(created);
199     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 1, 2, row, row+1);
200     gtk_widget_set_sensitive(fs_lb, entry->current);
201     gtk_widget_show(fs_lb);
202
203     fs_lb = gtk_label_new(modified);
204     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 2, 3, row, row+1);
205     gtk_widget_set_sensitive(fs_lb, entry->current);
206     gtk_widget_show(fs_lb);
207
208     fs_lb = gtk_label_new(size);
209     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 3, 4, row, row+1);
210     gtk_widget_set_sensitive(fs_lb, entry->current);
211     gtk_widget_show(fs_lb);
212
213     title = g_strdup_printf("Wireshark: %u File%s in Set", row, plurality(row, "", "s"));
214     gtk_window_set_title(GTK_WINDOW(fs_w), title);
215     g_free(title);
216
217     title = g_strdup_printf("... in directory: %s", fileset_get_dirname());
218     gtk_label_set(GTK_LABEL(fs_dir_lb), title);
219     g_free(title);
220
221     row++;
222     
223     gtk_widget_show_all(fs_tb);
224
225     g_free(created);
226     g_free(modified);
227     g_free(size);
228 }
229
230
231 /* init the fileset table */
232 static void
233 fileset_init_table(GtkWidget *parent)
234 {
235   GtkWidget     *fs_lb;
236
237   
238   fs_tb = gtk_table_new(6,1, FALSE);
239   gtk_table_set_row_spacings(GTK_TABLE(fs_tb), 1);
240   gtk_table_set_col_spacings(GTK_TABLE(fs_tb), 12);
241   gtk_container_add(GTK_CONTAINER(parent), fs_tb);
242
243   row = 0;
244   fs_first_rb = NULL;
245
246   fs_lb = gtk_label_new("Filename");
247   gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 0, 1, row, row+1);
248
249   fs_lb = gtk_label_new("Created");
250   gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 1, 2, row, row+1);
251
252   fs_lb = gtk_label_new("Last Modified");
253   gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 2, 3, row, row+1);
254
255   fs_lb = gtk_label_new("Size");
256   gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 3, 4, row, row+1);
257
258   gtk_widget_hide(fs_tb);
259
260   gtk_window_set_title(GTK_WINDOW(fs_w), "Wireshark: 0 Files in Set");
261
262   gtk_label_set(GTK_LABEL(fs_dir_lb), "No capture file loaded!");
263
264   row++;
265 }
266
267
268 /* open the fileset dialog */
269 void
270 fileset_cb(GtkWidget *w _U_, gpointer d _U_)
271 {
272   GtkWidget     *main_vb, *bbox, *close_bt, *help_bt;
273 #if GTK_MAJOR_VERSION < 2
274   GtkAccelGroup *accel_group;
275 #endif
276
277
278   if (fs_w != NULL) {
279     /* There's already a "File Set" dialog box; reactivate it. */
280     reactivate_window(fs_w);
281     return;
282   }
283
284   fs_w = window_new(GTK_WINDOW_TOPLEVEL, "");
285
286   tooltips = gtk_tooltips_new();
287
288 #if GTK_MAJOR_VERSION < 2
289   /* Accelerator group for the accelerators (or, as they're called in
290      Windows and, I think, in Motif, "mnemonics"; Alt+<key> is a mnemonic,
291      Ctrl+<key> is an accelerator). */
292   accel_group = gtk_accel_group_new();
293   gtk_window_add_accel_group(GTK_WINDOW(fs_w), accel_group);
294 #endif
295
296   main_vb = gtk_vbox_new(FALSE, 5);
297   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
298   gtk_container_add(GTK_CONTAINER(fs_w), main_vb);
299
300   /* add a dummy container, so we can replace the table later */
301   fs_tb_vb = gtk_vbox_new(FALSE, 0);
302   gtk_container_add(GTK_CONTAINER(main_vb), fs_tb_vb);
303
304   fs_dir_lb = gtk_label_new("");
305   gtk_container_add(GTK_CONTAINER(main_vb), fs_dir_lb);
306
307   fileset_init_table(fs_tb_vb);
308
309   /* Button row: close button */
310   if(topic_available(HELP_FILESET_DIALOG)) {
311     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
312   } else {
313     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
314   }
315   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
316
317   close_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_CLOSE);
318   window_set_cancel_button(fs_w, close_bt, window_cancel_button_cb);
319   gtk_tooltips_set_tip(tooltips, close_bt, "Close this window.", NULL);
320
321   if(topic_available(HELP_FILESET_DIALOG)) {
322     help_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_HELP);
323     SIGNAL_CONNECT(help_bt, "clicked", topic_cb, HELP_FILESET_DIALOG);
324   }
325
326   gtk_widget_grab_default(close_bt);
327
328   SIGNAL_CONNECT(fs_w, "delete_event", window_delete_event_cb, NULL);
329   SIGNAL_CONNECT(fs_w, "destroy", fs_destroy_cb, NULL);
330
331   /* init the dialog content */
332   fileset_update_dlg();
333
334   gtk_widget_show_all(fs_w);
335   window_present(fs_w);
336 }
337
338
339 /* open the next file in the file set, or do nothing if already the last file */
340 void
341 fileset_next_cb(GtkWidget *w _U_, gpointer d _U_)
342 {
343     fileset_entry   *entry;
344
345     entry = fileset_get_next();
346
347     if(entry) {
348         fs_open_entry(entry);
349     }
350 }
351
352
353 /* open the previous file in the file set, or do nothing if already the first file */
354 void
355 fileset_previous_cb(GtkWidget *w _U_, gpointer d _U_)
356 {
357     fileset_entry   *entry;
358
359     entry = fileset_get_previous();
360
361     if(entry) {
362         fs_open_entry(entry);
363     }
364 }
365
366
367 /* a new capture file was opened, browse the dir and look for files matching the given file set */
368 void
369 fileset_file_opened(const char *fname) {
370   fileset_add_dir(fname);
371   if(fs_w) {
372     window_present(fs_w);
373   }
374
375   /* update the menu */
376   set_menus_for_file_set(TRUE /* file_set */, 
377       fileset_get_previous() != NULL, fileset_get_next() != NULL );
378 }
379
380
381 /* the capture file was closed */
382 void
383 fileset_file_closed(void)
384 {
385   if(fs_w) {
386     /* reinit the table, title and alike */
387     gtk_widget_ref(fs_tb_vb);
388     gtk_widget_destroy(fs_tb);
389     fileset_delete();
390     fileset_init_table(fs_tb_vb);
391     window_present(fs_w);
392   } else {
393     fileset_delete();
394   }
395
396   /* update the menu */
397   set_menus_for_file_set(FALSE /* file_set */, 
398       fileset_get_previous() != NULL, fileset_get_next() != NULL );
399 }
400