aboutsummaryrefslogtreecommitdiffstats
path: root/ui/gtk/fileset_dlg.c
blob: 027d945afa89479a16d4b58f6d95d87effcc9f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* fileset_dlg.c
 * Routines for the file set dialog
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include <wsutil/filesystem.h>
#include <wsutil/str_util.h>

#include "../../fileset.h"

#include "ui/gtk/gui_utils.h"
#include "ui/gtk/dlg_utils.h"
#include "ui/gtk/main.h"
#include "ui/gtk/menus.h"
#include "ui/gtk/help_dlg.h"
#include "ui/gtk/fileset_dlg.h"
#include "ui/gtk/old-gtk-compat.h"

#include "globals.h"

/*
 * Keep a static pointer to the current "File Set" window, if
 * any, so that if somebody tries to do "File Set" while there's
 * already a "File Set" window up, we just pop up the existing
 * one, rather than creating a new one.
 */
static GtkWidget *fs_w;



/* various widget related global data */
static int        row;
static GtkWidget *fs_grid;
static GtkWidget *fs_sw;
static GtkWidget *fs_dir_lb;
static GtkWidget *fs_first_rb;
static GtkWidget *fs_grid_vb;



/* open the file corresponding to the given fileset entry */
static void
fs_open_entry(fileset_entry *entry)
{
    char *fname;
    int   err;


    /* make a copy of the filename (cf_close will indirectly destroy it right now) */
    fname = g_strdup(entry->fullname);

    /* close the old and open the new file */
    cf_close(&cfile);
    if (cf_open(&cfile, fname, WTAP_TYPE_AUTO, FALSE, &err) == CF_OK) {
        cf_read(&cfile, FALSE);
    }

    g_free(fname);
}


/* radio button was pressed/released */
static void
fs_rb_cb(GtkWidget *open_bt, gpointer fs_data)
{
    fileset_entry *entry = (fileset_entry *)fs_data;

    /* button release should have no effect */
    if (!gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(open_bt) )) {
        return;
    }

    fs_open_entry(entry);
}


/* the window was closed, cleanup things */
static void
fs_destroy_cb(GtkWidget *win _U_, gpointer user_data _U_)
{
    /* Note that we no longer have a "File Set" dialog box. */
    fs_w = NULL;
}


/* get creation date (converted from filename) */
/* */
static char *
fileset_dlg_name2date_dup(const char * name) {
    char   *pfx;
    char   *filename;
    size_t  pos;


    /* just to be sure ... */
    if (!fileset_filename_match_pattern(name)) {
        return NULL;
    }

    /* find char position behind the last underscore */
    pfx = strrchr(name, '_');
    pfx++;
    pos = pfx - name;

    /* Start conversion behind that underscore */
    /* http://en.wikipedia.org/wiki/ISO_8601 */
    filename = g_strdup_printf("%c%c%c%c-%c%c-%c%c %c%c:%c%c:%c%c",
        /* year  */  name[pos]  ,  name[pos+1], name[pos+2], name[pos+3],
        /* month */  name[pos+4],  name[pos+5],
        /* day   */  name[pos+6],  name[pos+7],
        /* hour */   name[pos+8],  name[pos+9],
        /* min */    name[pos+10], name[pos+11],
        /* second */ name[pos+12], name[pos+13]);

    return filename;
}


/* this file is a part of the current file set, add it to the dialog */
void
fileset_dlg_add_file(fileset_entry *entry, void *window _U_) {
    char      *created;
    char      *modified;
    char      *size;
    struct tm *local;
    GtkWidget *fs_lb;
    GtkWidget *fs_rb;
    gchar     *title;


    if (fs_w == NULL) {
        return;
    }

    created = fileset_dlg_name2date_dup(entry->name);
    if (!created) {
        /* if this file doesn't follow the file set pattern, */
        /* use the creation time of that file if available */
        /*
         * macOS provides 0 if the file system doesn't support the
         * creation time; FreeBSD provides -1.
         *
         * If this OS doesn't provide the creation time with stat(),
         * it will be 0.
         */
        if (entry->ctime > 0) {
            local = localtime(&entry->ctime);
            if (local != NULL) {
                created = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u",
                                          local->tm_year+1900, local->tm_mon+1, local->tm_mday,
                                          local->tm_hour, local->tm_min, local->tm_sec);
            } else {
                created = g_strdup("Time not representable");
            }
        } else
            created = g_strdup("Not available");
    }

    local = localtime(&entry->mtime);
    if (local != NULL) {
        modified = g_strdup_printf("%04u-%02u-%02u %02u:%02u:%02u",
            local->tm_year+1900, local->tm_mon+1, local->tm_mday,
            local->tm_hour, local->tm_min, local->tm_sec);
    } else {
        modified = g_strdup("Time not representable");
    }
    size = g_strdup_printf("%" G_GINT64_MODIFIER "d Bytes", entry->size);

    fs_rb = gtk_radio_button_new_with_label_from_widget(
        fs_first_rb ? GTK_RADIO_BUTTON(fs_first_rb) : NULL, entry->name);
    if (row == 1) {
        fs_first_rb = fs_rb;
    }
    if (entry->current) {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (fs_rb), entry->current);
    }
    gtk_widget_set_tooltip_text(fs_rb, "Open this capture file");
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_rb, 0, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
    g_signal_connect(fs_rb, "toggled", G_CALLBACK(fs_rb_cb), entry);
    gtk_widget_show(fs_rb);

    fs_lb = gtk_label_new(created);
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 1, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
    gtk_widget_set_sensitive(fs_lb, entry->current);
    gtk_widget_show(fs_lb);

    fs_lb = gtk_label_new(modified);
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 2, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
    gtk_widget_set_sensitive(fs_lb, entry->current);
    gtk_widget_show(fs_lb);

    fs_lb = gtk_label_new(size);
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 3, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);
    gtk_widget_set_sensitive(fs_lb, entry->current);
    gtk_widget_show(fs_lb);

    title = g_strdup_printf("Wireshark: %u File%s in Set", row, plurality(row, "", "s"));
    gtk_window_set_title(GTK_WINDOW(fs_w), title);
    g_free(title);

    title = g_strdup_printf("... in directory: %s", fileset_get_dirname());
     gtk_label_set_text(GTK_LABEL(fs_dir_lb), title);
    g_free(title);

    gtk_widget_show_all(fs_grid);

    /* resize the table until we use 18 rows (fits well into 800*600), if it's bigger use a scrollbar */
    /* XXX - I didn't found a way to automatically shrink the table size again */
    if (row <= 18) {
      GtkRequisition requisition;

      gtk_widget_get_preferred_size(fs_grid, &requisition, NULL);
      /* XXX use gtk_window_set_default_size()? */
      gtk_widget_set_size_request(fs_sw, -1, requisition.height);
      gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
    }

    if (row == 18) {
      gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    }

    row++;

    g_free(created);
    g_free(modified);
    g_free(size);
}

void fileset_dlg_begin_add_file(void *window _U_) { } // Qt only
void fileset_dlg_end_add_file(void *window _U_) { } // Qt only

/* init the fileset table */
static void
fileset_init_table(GtkWidget *parent_vb)
{
    GtkWidget *fs_lb;

    fs_grid = ws_gtk_grid_new();
    ws_gtk_grid_set_row_spacing(GTK_GRID(fs_grid), 1);
    ws_gtk_grid_set_column_spacing(GTK_GRID(fs_grid), 12);
    gtk_box_pack_start(GTK_BOX(parent_vb), fs_grid, FALSE, FALSE, 0);

    row = 0;
    fs_first_rb = NULL;

    fs_lb = gtk_label_new("Filename");
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 0, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);

    fs_lb = gtk_label_new("Created");
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 1, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);

    fs_lb = gtk_label_new("Last Modified");
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 2, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);

    fs_lb = gtk_label_new("Size");
    ws_gtk_grid_attach_extended(GTK_GRID(fs_grid), fs_lb, 3, row, 1, 1, (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)0, 0, 0);

    gtk_widget_hide(fs_grid);

    gtk_window_set_title(GTK_WINDOW(fs_w), "Wireshark: 0 Files in Set");

    gtk_label_set_text(GTK_LABEL(fs_dir_lb), "No capture file loaded.");

    row++;
}


/* open the fileset dialog */
void
fileset_cb(GtkWidget *w _U_, gpointer d _U_)
{
    GtkWidget *main_vb, *bbox, *close_bt, *help_bt;


    if (fs_w != NULL) {
        /* There's already a "File Set" dialog box; reactivate it. */
        reactivate_window(fs_w);
        return;
    }

    fs_w = dlg_window_new("");  /* transient_for top_level */
    gtk_window_set_destroy_with_parent (GTK_WINDOW(fs_w), TRUE);

    main_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 5, FALSE);
    gtk_container_set_border_width(GTK_CONTAINER(main_vb), 5);
    gtk_container_add(GTK_CONTAINER(fs_w), main_vb);

    fs_sw = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(fs_sw), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
    gtk_box_pack_start(GTK_BOX(main_vb), fs_sw, TRUE, TRUE, 0);

    /* add a dummy container, so we can replace the table later */
    fs_grid_vb = ws_gtk_box_new(GTK_ORIENTATION_VERTICAL, 0, FALSE);
#if ! GTK_CHECK_VERSION(3,8,0)
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(fs_sw), fs_grid_vb);
#else
    gtk_container_add(GTK_CONTAINER(fs_sw), fs_grid_vb);
#endif

    fs_dir_lb = gtk_label_new("");
    gtk_box_pack_start(GTK_BOX(main_vb), fs_dir_lb, FALSE, FALSE, 0);

    fileset_init_table(fs_grid_vb);

    /* Button row: close and help button */
    bbox = dlg_button_row_new(GTK_STOCK_CLOSE, GTK_STOCK_HELP, NULL);
    gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 5);

    close_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
    window_set_cancel_button(fs_w, close_bt, window_cancel_button_cb);
    gtk_widget_set_tooltip_text(close_bt, "Close this window.");

    help_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_HELP);
    g_signal_connect(help_bt, "clicked", G_CALLBACK(topic_cb), (gpointer)HELP_FILESET_DIALOG);

    gtk_widget_grab_default(close_bt);

    g_signal_connect(fs_w, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
    g_signal_connect(fs_w, "destroy", G_CALLBACK(fs_destroy_cb), NULL);

    /* init the dialog content */
    fileset_update_dlg(NULL);

    gtk_widget_show_all(fs_w);
    window_present(fs_w);
}


/* open the next file in the file set, or do nothing if already the last file */
void
fileset_next_cb(GtkWidget *w _U_, gpointer d _U_)
{
    fileset_entry *entry;

    entry = fileset_get_next();

    if (entry) {
        fs_open_entry(entry);
    }
}


/* open the previous file in the file set, or do nothing if already the first file */
void
fileset_previous_cb(GtkWidget *w _U_, gpointer d _U_)
{
    fileset_entry *entry;

    entry = fileset_get_previous();

    if (entry) {
        fs_open_entry(entry);
    }
}


/* a new capture file was opened, browse the dir and look for files matching the given file set */
void
fileset_file_opened(const capture_file *cf) {
    fileset_add_dir(cf->filename, NULL);
    if (fs_w) {
        window_present(fs_w);
    }

    /* update the menu */
    set_menus_for_file_set(TRUE /* file_set */,
                           fileset_get_previous() != NULL, fileset_get_next() != NULL );
}


/* the capture file was closed */
void
fileset_file_closed(void)
{
    if (fs_w) {
        /* reinit the table, title and alike */
        g_object_ref(G_OBJECT(fs_grid_vb));
        gtk_widget_destroy(fs_grid);
        fileset_delete();
        fileset_init_table(fs_grid_vb);
        window_present(fs_w);
    } else {
        fileset_delete();
    }

    /* update the menu */
    set_menus_for_file_set(FALSE /* file_set */,
                           fileset_get_previous() != NULL,
                           fileset_get_next() != NULL );
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */