diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-02-09 11:18:22 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2018-02-09 20:29:08 +0000 |
commit | 25d90cb13c6f4296fd0c698ea8b2b67766bfbc88 (patch) | |
tree | 18b449c143446b9a70e6b57ffbc3c46780ad8da0 /ui | |
parent | be38102eea170403b66d57d3f8f01ea4d49eaf29 (diff) | |
download | wireshark-25d90cb13c6f4296fd0c698ea8b2b67766bfbc88.tar.gz wireshark-25d90cb13c6f4296fd0c698ea8b2b67766bfbc88.tar.bz2 wireshark-25d90cb13c6f4296fd0c698ea8b2b67766bfbc88.zip |
Put the preview-generation loop into a common routine.
Don't have all the file open dialogs have their own copies.
Change-Id: Icd6f2fd44b081575e6481a134027c90046938c64
Reviewed-on: https://code.wireshark.org/review/25717
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'ui')
-rw-r--r-- | ui/CMakeLists.txt | 1 | ||||
-rw-r--r-- | ui/Makefile.am | 1 | ||||
-rw-r--r-- | ui/file_dialog.c | 95 | ||||
-rw-r--r-- | ui/file_dialog.h | 16 | ||||
-rw-r--r-- | ui/gtk/capture_file_dlg.c | 72 | ||||
-rw-r--r-- | ui/qt/capture_file_dialog.cpp | 62 | ||||
-rw-r--r-- | ui/win32/file_dlg_win32.c | 102 |
7 files changed, 188 insertions, 161 deletions
diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 84a2896549..f5ed7b199c 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -31,6 +31,7 @@ set(COMMON_UI_SRC export_pdu_ui_utils.c help_url.c failure_message.c + file_dialog.c filter_files.c firewall_rules.c iface_toolbar.c diff --git a/ui/Makefile.am b/ui/Makefile.am index cd341381a6..e3b468c700 100644 --- a/ui/Makefile.am +++ b/ui/Makefile.am @@ -55,6 +55,7 @@ WIRESHARK_UI_SRC = \ export_object_ui.c \ export_pdu_ui_utils.c \ failure_message.c \ + file_dialog.c \ filter_files.c \ firewall_rules.c \ iface_toolbar.c \ diff --git a/ui/file_dialog.c b/ui/file_dialog.c new file mode 100644 index 0000000000..754d4b6f5c --- /dev/null +++ b/ui/file_dialog.c @@ -0,0 +1,95 @@ +/* file_dialog.c + * Common file dialog routines + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 2006 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later*/ + +#include "config.h" + +#include <time.h> + +#include <glib.h> + +#include <wsutil/nstime.h> + +#include <wiretap/wtap.h> + +#include <epan/prefs.h> + +#include "ui/file_dialog.h" + +ws_file_preview_times_status +get_times_for_preview(wtap *wth, ws_file_preview_times *times, + guint32 *num_packets, int *err, gchar **err_info) +{ + gint64 data_offset; + const wtap_rec *rec; + guint32 packets; + gboolean have_times; + gboolean timed_out; + time_t time_preview, time_current; + double cur_time; + + times->start_time = 0; + times->stop_time = 0; + packets = 0; + have_times = FALSE; + timed_out = FALSE; + time(&time_preview); + while ((wtap_read(wth, err, err_info, &data_offset))) { + rec = wtap_get_rec(wth); + if (rec->presence_flags & WTAP_HAS_TS) { + cur_time = nstime_to_sec(&rec->ts); + if (!have_times) { + times->start_time = cur_time; + times->stop_time = cur_time; + have_times = TRUE; + } + if (cur_time < times->start_time) { + times->start_time = cur_time; + } + if (cur_time > times->stop_time){ + times->stop_time = cur_time; + } + } + + packets++; + if (packets%1000 == 0) { + /* do we have a timeout? */ + time(&time_current); + if (time_current-time_preview >= (time_t) prefs.gui_fileopen_preview) { + timed_out = TRUE; + break; + } + } + } + *num_packets = packets; + if (*err != 0) { + /* Read error. */ + return PREVIEW_READ_ERROR; + } + + if (have_times) { + if (timed_out) + return PREVIEW_TIMED_OUT; + else + return PREVIEW_HAVE_TIMES; + } else + return PREVIEW_HAVE_NO_TIMES; +} + +/* + * Editor modelines + * + * Local Variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ diff --git a/ui/file_dialog.h b/ui/file_dialog.h index f931fa8f9e..f2aab493b4 100644 --- a/ui/file_dialog.h +++ b/ui/file_dialog.h @@ -37,6 +37,22 @@ typedef enum { export_type_json } export_type_e; +typedef struct { + double start_time; /* seconds, with nsec resolution */ + double stop_time; /* seconds, with nsec resolution */ +} ws_file_preview_times; + +typedef enum { + PREVIEW_HAVE_TIMES, + PREVIEW_HAVE_NO_TIMES, + PREVIEW_TIMED_OUT, + PREVIEW_READ_ERROR +} ws_file_preview_times_status; + +extern ws_file_preview_times_status +get_times_for_preview(wtap *wth, ws_file_preview_times *times, + guint32 *num_packets, int *err, gchar **err_info); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/ui/gtk/capture_file_dlg.c b/ui/gtk/capture_file_dlg.c index ce16de01a8..d83ca35cde 100644 --- a/ui/gtk/capture_file_dlg.c +++ b/ui/gtk/capture_file_dlg.c @@ -164,55 +164,22 @@ static void preview_do(GtkWidget *prev, wtap *wth) { GtkWidget *label; - unsigned int elapsed_time; - time_t time_preview; - time_t time_current; - int err = 0; + int err; gchar *err_info; - gint64 data_offset; - double start_time = 0; /* seconds, with nsec resolution */ - double stop_time = 0; /* seconds, with nsec resolution */ - gboolean have_times = FALSE; - double cur_time; - unsigned int packets = 0; - gboolean is_breaked = FALSE; + guint32 packets; + ws_file_preview_times times; + ws_file_preview_times_status status; gchar string_buff[PREVIEW_STR_MAX]; gchar first_buff[PREVIEW_STR_MAX]; time_t ti_time; struct tm *ti_tm; - const wtap_rec *rec; - - - time(&time_preview); - while ( (wtap_read(wth, &err, &err_info, &data_offset)) ) { - rec = wtap_get_rec(wth); - if (rec->presence_flags & WTAP_HAS_TS) { - cur_time = nstime_to_sec(&rec->ts); - if (!have_times) { - start_time = cur_time; - stop_time = cur_time; - have_times = TRUE; - } - if (cur_time < start_time) { - start_time = cur_time; - } - if (cur_time > stop_time) { - stop_time = cur_time; - } - } + unsigned int elapsed_time; - packets++; - if (packets%1000 == 0) { - /* do we have a timeout? */ - time(&time_current); - if (time_current-time_preview >= (time_t) prefs.gui_fileopen_preview) { - is_breaked = TRUE; - break; - } - } - } + status = get_times_for_preview(wth, ×, &packets, &err, &err_info); - if (err != 0) { + if(status == PREVIEW_READ_ERROR) { + /* XXX - give error details? */ + g_free(err_info); g_snprintf(string_buff, PREVIEW_STR_MAX, "error after reading %u packets", packets); label = (GtkWidget *)g_object_get_data(G_OBJECT(prev), PREVIEW_PACKETS_KEY); gtk_label_set_text(GTK_LABEL(label), string_buff); @@ -221,7 +188,7 @@ preview_do(GtkWidget *prev, wtap *wth) } /* packet count */ - if (is_breaked) { + if(status == PREVIEW_TIMED_OUT) { g_snprintf(string_buff, PREVIEW_STR_MAX, "more than %u packets (preview timeout)", packets); } else { g_snprintf(string_buff, PREVIEW_STR_MAX, "%u", packets); @@ -229,11 +196,11 @@ preview_do(GtkWidget *prev, wtap *wth) label = (GtkWidget *)g_object_get_data(G_OBJECT(prev), PREVIEW_PACKETS_KEY); gtk_label_set_text(GTK_LABEL(label), string_buff); - /* first packet */ - if (!have_times) { + /* First packet */ + if(status == PREVIEW_HAVE_NO_TIMES) { g_snprintf(first_buff, PREVIEW_STR_MAX, "unknown"); } else { - ti_time = (long)start_time; + ti_time = (long)times.start_time; ti_tm = localtime( &ti_time ); if (ti_tm) { g_snprintf(first_buff, PREVIEW_STR_MAX, @@ -249,21 +216,20 @@ preview_do(GtkWidget *prev, wtap *wth) } } - /* elapsed time */ - if (!have_times) { + /* Elapsed time */ + if(status == PREVIEW_HAVE_NO_TIMES) { g_snprintf(string_buff, PREVIEW_STR_MAX, "%s / unknown", first_buff); } else { - elapsed_time = (unsigned int)(stop_time-start_time); - if (elapsed_time/86400) { + elapsed_time = (unsigned int)(times.stop_time-times.start_time); + if(status == PREVIEW_TIMED_OUT) { + g_snprintf(string_buff, PREVIEW_STR_MAX, "%s / unknown", first_buff); + } else if (elapsed_time/86400) { g_snprintf(string_buff, PREVIEW_STR_MAX, "%s / %02u days %02u:%02u:%02u", first_buff, elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); } else { g_snprintf(string_buff, PREVIEW_STR_MAX, "%s / %02u:%02u:%02u", first_buff, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); } - if (is_breaked) { - g_snprintf(string_buff, PREVIEW_STR_MAX, "%s / unknown", first_buff); - } } label = (GtkWidget *)g_object_get_data(G_OBJECT(prev), PREVIEW_FIRST_ELAPSED_KEY); gtk_label_set_text(GTK_LABEL(label), string_buff); diff --git a/ui/qt/capture_file_dialog.cpp b/ui/qt/capture_file_dialog.cpp index e172a3bc35..64af1e89b7 100644 --- a/ui/qt/capture_file_dialog.cpp +++ b/ui/qt/capture_file_dialog.cpp @@ -687,24 +687,15 @@ int CaptureFileDialog::mergeType() { void CaptureFileDialog::preview(const QString & path) { wtap *wth; - int err = 0; + int err; gchar *err_info; - gint64 data_offset; - const wtap_rec *rec; - double start_time = 0; /* seconds, with nsec resolution */ - double stop_time = 0; /* seconds, with nsec resolution */ - gboolean have_times = FALSE; - double cur_time; - unsigned int packets = 0; - bool timed_out = FALSE; - time_t time_preview; - time_t time_current; + ws_file_preview_times times; + ws_file_preview_times_status status; + guint32 packets; time_t ti_time; struct tm *ti_tm; unsigned int elapsed_time; - // Follow the same steps as ui/win32/file_dlg_win32.c - foreach (QLabel *lbl, preview_labels_) { lbl->setEnabled(false); } @@ -745,43 +736,18 @@ void CaptureFileDialog::preview(const QString & path) // Finder and Windows Explorer use IEC. What do the various Linux file managers use? QString size_str(gchar_free_to_qstring(format_size(filesize, format_size_unit_bytes|format_size_prefix_iec))); - time(&time_preview); - while ((wtap_read(wth, &err, &err_info, &data_offset))) { - rec = wtap_get_rec(wth); - if (rec->presence_flags & WTAP_HAS_TS) { - cur_time = nstime_to_sec(&rec->ts); - if (!have_times) { - start_time = cur_time; - stop_time = cur_time; - have_times = TRUE; - } - if (cur_time < start_time) { - start_time = cur_time; - } - if (cur_time > stop_time){ - stop_time = cur_time; - } - } - - packets++; - if(packets%1000 == 0) { - /* do we have a timeout? */ - time(&time_current); - if(time_current-time_preview >= (time_t) prefs.gui_fileopen_preview) { - timed_out = TRUE; - break; - } - } - } + status = get_times_for_preview(wth, ×, &packets, &err, &err_info); - if(err != 0) { + if(status == PREVIEW_READ_ERROR) { + // XXX - give error details? + g_free(err_info); preview_size_.setText(tr("%1, error after %Ln packet(s)", "", packets) .arg(size_str)); return; } // Packet count - if(timed_out) { + if(status == PREVIEW_TIMED_OUT) { preview_size_.setText(tr("%1, timed out at %Ln packet(s)", "", packets) .arg(size_str)); } else { @@ -791,10 +757,10 @@ void CaptureFileDialog::preview(const QString & path) // First packet + elapsed time QString first_elapsed; - if(!have_times) { + if(status == PREVIEW_HAVE_NO_TIMES) { first_elapsed = tr("unknown"); } else { - ti_time = (long)start_time; + ti_time = (long)times.start_time; ti_tm = localtime(&ti_time); first_elapsed = "?"; if(ti_tm) { @@ -812,11 +778,11 @@ void CaptureFileDialog::preview(const QString & path) // Elapsed time first_elapsed += " / "; - if(!have_times) { + if(status == PREVIEW_HAVE_NO_TIMES) { first_elapsed += tr("unknown"); } else { - elapsed_time = (unsigned int)(stop_time-start_time); - if(timed_out) { + elapsed_time = (unsigned int)(times.stop_time-times.start_time); + if(status == PREVIEW_TIMED_OUT) { first_elapsed += tr("unknown"); } else if(elapsed_time/86400) { first_elapsed += QString().sprintf("%02u days %02u:%02u:%02u", diff --git a/ui/win32/file_dlg_win32.c b/ui/win32/file_dlg_win32.c index cbf959782f..9bd5b777ee 100644 --- a/ui/win32/file_dlg_win32.c +++ b/ui/win32/file_dlg_win32.c @@ -1105,24 +1105,18 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) { HWND cur_ctrl; int i; wtap *wth; - const wtap_rec *rec; - int err = 0; + int err; gchar *err_info; + guint32 packets; + ws_file_preview_times times; + ws_file_preview_times_status status; TCHAR string_buff[PREVIEW_STR_MAX]; TCHAR first_buff[PREVIEW_STR_MAX]; - gint64 data_offset; - guint packet = 0; gint64 filesize; gchar *size_str; time_t ti_time; struct tm *ti_tm; guint elapsed_time; - time_t time_preview; - time_t time_current; - double start_time = 0; - double stop_time = 0; - double cur_time; - gboolean is_breaked = FALSE; for (i = EWFD_PTX_FORMAT; i <= EWFD_PTX_START_ELAPSED; i++) { cur_ctrl = GetDlgItem(of_hwnd, i); @@ -1176,33 +1170,13 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) { // Windows Explorer uses IEC. size_str = format_size(filesize, format_size_unit_bytes|format_size_prefix_iec); - time(&time_preview); - while ( (wtap_read(wth, &err, &err_info, &data_offset)) ) { - rec = wtap_get_rec(wth); - cur_time = nstime_to_sec( (const nstime_t *) &rec->ts ); - if(packet == 0) { - start_time = cur_time; - stop_time = cur_time; - } - if (cur_time < start_time) { - start_time = cur_time; - } - if (cur_time > stop_time){ - stop_time = cur_time; - } - packet++; - if(packet%100 == 0) { - time(&time_current); - if(time_current-time_preview >= (time_t) prefs.gui_fileopen_preview) { - is_breaked = TRUE; - break; - } - } - } + status = get_times_for_preview(wth, ×, &packets, &err, &err_info); - if(err != 0) { + if(status == PREVIEW_READ_ERROR) { + /* XXX - give error details? */ + g_free(err_info); utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, error after %u packets", - size_str, packet); + size_str, packets); g_free(size_str); cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_SIZE); SetWindowText(cur_ctrl, string_buff); @@ -1210,44 +1184,52 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) { return TRUE; } - /* Packets */ - if(is_breaked) { + /* Packet count */ + if(status == PREVIEW_TIMED_OUT) { utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, timed out at %u packets", - size_str, packet); + size_str, packets); } else { utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, %u packets", - size_str, packet); + size_str, packets); } g_free(size_str); cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_SIZE); SetWindowText(cur_ctrl, string_buff); /* First packet / elapsed time */ - ti_time = (long)start_time; - ti_tm = localtime( &ti_time ); - if(ti_tm) { - StringCchPrintf(first_buff, PREVIEW_STR_MAX, - _T("%04d-%02d-%02d %02d:%02d:%02d"), - ti_tm->tm_year + 1900, - ti_tm->tm_mon + 1, - ti_tm->tm_mday, - ti_tm->tm_hour, - ti_tm->tm_min, - ti_tm->tm_sec); + if(status == PREVIEW_HAVE_NO_TIMES) { + StringCchPrintf(first_buff, PREVIEW_STR_MAX, _T("unknown")); } else { - StringCchPrintf(first_buff, PREVIEW_STR_MAX, _T("?")); + ti_time = (long)times.start_time; + ti_tm = localtime( &ti_time ); + if(ti_tm) { + StringCchPrintf(first_buff, PREVIEW_STR_MAX, + _T("%04d-%02d-%02d %02d:%02d:%02d"), + ti_tm->tm_year + 1900, + ti_tm->tm_mon + 1, + ti_tm->tm_mday, + ti_tm->tm_hour, + ti_tm->tm_min, + ti_tm->tm_sec); + } else { + StringCchPrintf(first_buff, PREVIEW_STR_MAX, _T("?")); + } } - elapsed_time = (unsigned int)(stop_time-start_time); - if(elapsed_time/86400) { - StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / %02u days %02u:%02u:%02u"), - first_buff, elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); + /* Elapsed time */ + if(status == PREVIEW_HAVE_NO_TIMES) { + StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / unknown")); } else { - StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / %02u:%02u:%02u"), - first_buff, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); - } - if(is_breaked) { - StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / unknown"), first_buff); + elapsed_time = (unsigned int)(times.stop_time-times.start_time); + if(status == PREVIEW_TIMED_OUT) { + StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / unknown"), first_buff); + } else if(elapsed_time/86400) { + StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / %02u days %02u:%02u:%02u"), + first_buff, elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); + } else { + StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / %02u:%02u:%02u"), + first_buff, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60); + } } cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_START_ELAPSED); SetWindowText(cur_ctrl, string_buff); |