Install the new menu files.
[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 <epan/filesystem.h>
38
39 #include "../simple_dialog.h"
40 #include "../fileset.h"
41
42 #include "gtk/gui_utils.h"
43 #include "gtk/dlg_utils.h"
44 #include "gtk/main.h"
45 #include "gtk/menus.h"
46 #include "gtk/help_dlg.h"
47 #include "gtk/fileset_dlg.h"
48
49
50
51 /*
52  * Keep a static pointer to the current "File Set" window, if
53  * any, so that if somebody tries to do "File Set" while there's
54  * already a "File Set" window up, we just pop up the existing
55  * one, rather than creating a new one.
56  */
57 static GtkWidget *fs_w;
58
59
60
61 /* various widget related global data */
62 static int            row;
63 static GtkWidget     *fs_tb;
64 static GtkWidget     *fs_sw;
65 static GtkWidget     *fs_dir_lb;
66 static GtkWidget     *fs_first_rb;
67 static GtkWidget     *fs_tb_vb;
68
69
70
71 /* open the file corresponding to the given fileset entry */
72 static void
73 fs_open_entry(fileset_entry *entry)
74 {
75     char            *fname;
76     int             err;
77
78
79     /* make a copy of the filename (cf_close will indirectly destroy it right now) */
80     fname = g_strdup(entry->fullname);
81
82     /* close the old and open the new file */
83     cf_close(&cfile);
84     if (cf_open(&cfile, fname, FALSE, &err) == CF_OK) {
85         cf_read(&cfile, FALSE);
86     }
87
88     g_free(fname);
89 }
90
91
92 /* radio button was pressed/released */
93 static void
94 fs_rb_cb(GtkWidget *open_bt, gpointer fs_data)
95 {
96     fileset_entry   *entry = fs_data;
97
98     /* button release should have no effect */
99     if(!gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(open_bt) )) {
100         return;
101     }
102
103     fs_open_entry(entry);
104 }
105
106
107 /* the window was closed, cleanup things */
108 static void
109 fs_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
110 {
111     /* Note that we no longer have a "File Set" dialog box. */
112     fs_w = NULL;
113 }
114
115
116 /* get creation date (converted from filename) */
117 /* */
118 static char *
119 fileset_dlg_name2date_dup(const char * name) {
120     char        *pfx;
121     char        *filename;
122     size_t       pos;
123
124
125     /* just to be sure ... */
126         if(!fileset_filename_match_pattern(name)) {
127                 return NULL;
128         }
129
130     /* find char position behind the last underscore */
131     pfx = strrchr(name, '_');
132     pfx++;
133     pos = pfx - name;
134
135     /* start conversion behind that underscore */
136     filename = g_strdup_printf("%c%c%c%c.%c%c.%c%c %c%c:%c%c:%c%c",
137         /* year  */  name[pos]  ,  name[pos+1], name[pos+2], name[pos+3],
138         /* month */  name[pos+4],  name[pos+5],
139         /* day   */  name[pos+6],  name[pos+7],
140         /* hour */   name[pos+8],  name[pos+9],
141         /* min */    name[pos+10], name[pos+11],
142         /* second */ name[pos+12], name[pos+13]);
143
144     return filename;
145 }
146
147
148 /* this file is a part of the current file set, add it to the dialog */
149 void
150 fileset_dlg_add_file(fileset_entry *entry) {
151     char *created;
152     char *modified;
153     char *size;
154     struct tm *local;
155     GtkWidget     *fs_lb;
156     GtkWidget     *fs_rb;
157     gchar *title;
158
159
160     if (fs_w == NULL) {
161         return;
162     }
163
164     created = fileset_dlg_name2date_dup(entry->name);
165         if(!created) {
166                 /* if this file doesn't follow the file set pattern, */
167                 /* use the creation time of that file */
168                 local = localtime(&entry->ctime);
169                 created = g_strdup_printf("%04u.%02u.%02u %02u:%02u:%02u",
170                         local->tm_year+1900, local->tm_mon+1, local->tm_mday,
171                         local->tm_hour, local->tm_min, local->tm_sec);
172         }
173
174     local = localtime(&entry->mtime);
175     modified = 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     size = g_strdup_printf("%" G_GINT64_MODIFIER "d Bytes", entry->size);
179
180     fs_rb = gtk_radio_button_new_with_label_from_widget(
181         fs_first_rb ? GTK_RADIO_BUTTON(fs_first_rb) : NULL, entry->name);
182     if(row == 1) {
183         fs_first_rb = fs_rb;
184     }
185     if(entry->current) {
186         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (fs_rb), entry->current);
187     }
188         gtk_widget_set_tooltip_text(fs_rb, "Open this capture file");
189     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_rb, 0, 1, row, row+1);
190     g_signal_connect(fs_rb, "toggled", G_CALLBACK(fs_rb_cb), entry);
191     gtk_widget_show(fs_rb);
192
193     fs_lb = gtk_label_new(created);
194     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 1, 2, row, row+1);
195     gtk_widget_set_sensitive(fs_lb, entry->current);
196     gtk_widget_show(fs_lb);
197
198     fs_lb = gtk_label_new(modified);
199     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 2, 3, 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(size);
204     gtk_table_attach_defaults(GTK_TABLE(fs_tb), fs_lb, 3, 4, row, row+1);
205     gtk_widget_set_sensitive(fs_lb, entry->current);
206     gtk_widget_show(fs_lb);
207
208     title = g_strdup_printf("Wireshark: %u File%s in Set", row, plurality(row, "", "s"));
209     gtk_window_set_title(GTK_WINDOW(fs_w), title);
210     g_free(title);
211
212     title = g_strdup_printf("... in directory: %s", fileset_get_dirname());
213      gtk_label_set_text(GTK_LABEL(fs_dir_lb), title);
214     g_free(title);
215
216     gtk_widget_show_all(fs_tb);
217
218     /* resize the table until we use 18 rows (fits well into 800*600), if it's bigger use a scrollbar */
219     /* XXX - I didn't found a way to automatically shrink the table size again */
220     if(row <= 18) {
221       GtkRequisition requisition;
222
223 #if GTK_CHECK_VERSION(3,0,0)
224           gtk_widget_get_preferred_size(fs_tb, &requisition, NULL);
225 #else
226       gtk_widget_size_request(fs_tb, &requisition);
227 #endif
228           /* XXX use gtk_window_set_default_size()? */
229       gtk_widget_set_size_request(fs_sw, -1, requisition.height);
230       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
231     }
232
233     if(row == 18) {
234       gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
235     }
236
237     row++;
238
239     g_free(created);
240     g_free(modified);
241     g_free(size);
242 }
243
244
245 /* init the fileset table */
246 static void
247 fileset_init_table(GtkWidget *parent)
248 {
249   GtkWidget     *fs_lb;
250
251
252   fs_tb = gtk_table_new(6,1, FALSE);
253   gtk_table_set_row_spacings(GTK_TABLE(fs_tb), 1);
254   gtk_table_set_col_spacings(GTK_TABLE(fs_tb), 12);
255   gtk_container_add(GTK_CONTAINER(parent), fs_tb);
256
257   row = 0;
258   fs_first_rb = NULL;
259
260   fs_lb = gtk_label_new("Filename");
261   gtk_table_attach(GTK_TABLE(fs_tb), fs_lb, 0, 1, row, row+1, GTK_EXPAND|GTK_FILL, 0, 0,0);
262
263   fs_lb = gtk_label_new("Created");
264   gtk_table_attach(GTK_TABLE(fs_tb), fs_lb, 1, 2, row, row+1, GTK_EXPAND|GTK_FILL, 0, 0,0);
265
266   fs_lb = gtk_label_new("Last Modified");
267   gtk_table_attach(GTK_TABLE(fs_tb), fs_lb, 2, 3, row, row+1, GTK_EXPAND|GTK_FILL, 0, 0,0);
268
269   fs_lb = gtk_label_new("Size");
270   gtk_table_attach(GTK_TABLE(fs_tb), fs_lb, 3, 4, row, row+1, GTK_EXPAND|GTK_FILL, 0, 0,0);
271
272   gtk_widget_hide(fs_tb);
273
274   gtk_window_set_title(GTK_WINDOW(fs_w), "Wireshark: 0 Files in Set");
275
276    gtk_label_set_text(GTK_LABEL(fs_dir_lb), "No capture file loaded!");
277
278   row++;
279 }
280
281
282 /* open the fileset dialog */
283 void
284 fileset_cb(GtkWidget *w _U_, gpointer d _U_)
285 {
286   GtkWidget     *main_vb, *bbox, *close_bt, *help_bt;
287
288
289   if (fs_w != NULL) {
290     /* There's already a "File Set" dialog box; reactivate it. */
291     reactivate_window(fs_w);
292     return;
293   }
294
295   fs_w = dlg_window_new("");  /* transient_for top_level */
296   gtk_window_set_destroy_with_parent (GTK_WINDOW(fs_w), TRUE);
297
298   main_vb = gtk_vbox_new(FALSE, 5);
299   gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
300   gtk_container_add(GTK_CONTAINER(fs_w), main_vb);
301
302   fs_sw = gtk_scrolled_window_new(NULL, NULL);
303   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
304   gtk_box_pack_start(GTK_BOX(main_vb), fs_sw, TRUE, TRUE, 0);
305
306   /* add a dummy container, so we can replace the table later */
307   fs_tb_vb = gtk_vbox_new(FALSE, 0);
308   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(fs_sw), fs_tb_vb);
309
310   fs_dir_lb = gtk_label_new("");
311   gtk_box_pack_start(GTK_BOX(main_vb), fs_dir_lb, FALSE, FALSE, 0);
312
313   fileset_init_table(fs_tb_vb);
314
315   /* Button row: close and help button */
316   bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
317   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);
318
319   close_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
320   window_set_cancel_button(fs_w, close_bt, window_cancel_button_cb);
321   gtk_widget_set_tooltip_text(close_bt, "Close this window.");
322
323   help_bt = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
324   g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_FILESET_DIALOG);
325
326   gtk_widget_grab_default(close_bt);
327
328   g_signal_connect(fs_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
329   g_signal_connect(fs_w, "destroy", G_CALLBACK(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     g_object_ref(G_OBJECT(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,
399                          fileset_get_next() != NULL );
400 }
401