Have the frame_tvbuff.c routines not use the global cfile.
[metze/wireshark/wip.git] / ui / win32 / file_dlg_win32.c
index b7630dfd14b82c2bd87db48472b0602f5b751ac7..ebfc57590043305a5026409a8391bc4ff9534f5a 100644 (file)
@@ -1,23 +1,11 @@
-/* win32-file-dlg.c
+/* file_dlg_win32.c
  * Native Windows file dialog routines
  *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 2004 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0+
  */
 
 #include "config.h"
 #include <richedit.h>
 #include <strsafe.h>
 
-#include "file.h"
+#include "globals.h"
 
 #include "wsutil/file_util.h"
 #include "wsutil/str_util.h"
 #include "wsutil/unicode-utils.h"
 
 #include "wsutil/filesystem.h"
-#include "epan/addr_resolv.h"
 #include "epan/prefs.h"
 
 #include "epan/color_filters.h"
@@ -49,7 +36,7 @@
 #include "ui/simple_dialog.h"
 #include "ui/ssl_key_export.h"
 #include "ui/util.h"
-#include "ui/ui_util.h"
+#include "ui/ws_ui_util.h"
 #include "ui/all_files_wildcard.h"
 
 #include "file_dlg_win32.h"
@@ -64,7 +51,8 @@
     _T("CSV (Comma Separated Values summary) (*.csv)\0") _T("*.csv\0")   \
     _T("PSML (XML packet summary) (*.psml)\0")           _T("*.psml\0")  \
     _T("PDML (XML packet detail) (*.pdml)\0")            _T("*.pdml\0")  \
-    _T("C Arrays (packet bytes) (*.c)\0")                _T("*.c\0")
+    _T("C Arrays (packet bytes) (*.c)\0")                _T("*.c\0")     \
+    _T("JSON (*.json)\0")                                _T("*.json\0")
 
 #define FILE_TYPES_RAW \
     _T("Raw data (*.bin, *.dat, *.raw)\0")               _T("*.bin;*.dat;*.raw\0") \
@@ -271,24 +259,22 @@ win32_check_save_as_with_comments(HWND parent, capture_file *cf, int file_type)
            format you selected", or "Cancel", meaning "don't bother
            saving the file at all".
 
-           XXX - sadly, customizing buttons in a MessageBox() is
-           Really Painful; there are tricks out there to do it
-           with a "computer-based training" hook that gets called
-           before the window is activated and sets the text of the
-           buttons, but if you change the text of the buttons you
-           also have to make the buttons bigger.  There *has* to
-           be a better way of doing that, given that Microsoft's
-           own UI guidelines have examples of dialog boxes with
-           action buttons that have custom labels, but maybe we'd
-           have to go with Windows Forms or XAML or whatever the
-           heck the technology of the week is.
-
-           Therefore, we ask a yes-or-no question - "do you want
-           to discard the comments and save in the format you
-           chose?" - and have "no" mean "I want to save the
-           file but I don't want to discard the comments, meaning
-           we should reopen the dialog and not offer the user any
-           choices that would involve discarding the comments. */
+           XXX - given that we no longer support releases prior to
+           Windows Vista, we should use a task dialog:
+
+               https://msdn.microsoft.com/en-us/library/windows/desktop/ff486057(v=vs.85).aspx
+
+           created with TaskDialogIndirect():
+
+               https://msdn.microsoft.com/en-us/library/windows/desktop/bb760544(v=vs.85).aspx
+
+           because the TASKDIALOGCONFIG structure
+
+               https://msdn.microsoft.com/en-us/library/windows/desktop/bb787473(v=vs.85).aspx
+
+           supports adding custom buttons, with custom labels, unlike
+           a MessageBox(), which doesn't appear to offer a clean way to
+           do that. */
         response = MessageBox(parent,
   _T("The capture has comments, but the file format you chose ")
   _T("doesn't support comments.  Do you want to discard the comments ")
@@ -686,6 +672,7 @@ win32_export_file(HWND h_wnd, capture_file *cf, export_type_e export_type) {
     print_args.print_dissections   = print_dissections_as_displayed;
     print_args.print_hex           = FALSE;
     print_args.print_formfeed      = FALSE;
+    print_args.stream              = NULL;
 
     if (GetSaveFileName(ofn)) {
         print_args.file = utf_16to8(file_name);
@@ -697,7 +684,7 @@ win32_export_file(HWND h_wnd, capture_file *cf, export_type_e export_type) {
                     g_free( (void *) ofn);
                     return;
                 }
-                status = cf_print_packets(cf, &print_args);
+                status = cf_print_packets(cf, &print_args, TRUE);
                 break;
             case export_type_ps:        /* PostScript (r) */
                 print_args.stream = print_stream_ps_new(TRUE, print_args.file);
@@ -706,7 +693,7 @@ win32_export_file(HWND h_wnd, capture_file *cf, export_type_e export_type) {
                     g_free( (void *) ofn);
                     return;
                 }
-                status = cf_print_packets(cf, &print_args);
+                status = cf_print_packets(cf, &print_args, TRUE);
                 break;
             case export_type_csv:       /* CSV */
                 status = cf_write_csv_packets(cf, &print_args);
@@ -720,6 +707,9 @@ win32_export_file(HWND h_wnd, capture_file *cf, export_type_e export_type) {
             case export_type_pdml:      /* PDML */
                 status = cf_write_pdml_packets(cf, &print_args);
                 break;
+            case export_type_json:      /* JSON */
+                status = cf_write_json_packets(cf, &print_args);
+                break;
             default:
                 g_free( (void *) ofn);
                 return;
@@ -1119,9 +1109,11 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
     int         err = 0;
     gchar      *err_info;
     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;
@@ -1132,14 +1124,14 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
     double      cur_time;
     gboolean    is_breaked = FALSE;
 
-    for (i = EWFD_PTX_FORMAT; i <= EWFD_PTX_ELAPSED; i++) {
+    for (i = EWFD_PTX_FORMAT; i <= EWFD_PTX_START_ELAPSED; i++) {
         cur_ctrl = GetDlgItem(of_hwnd, i);
         if (cur_ctrl) {
             EnableWindow(cur_ctrl, FALSE);
         }
     }
 
-    for (i = EWFD_PTX_FORMAT; i <= EWFD_PTX_ELAPSED; i++) {
+    for (i = EWFD_PTX_FORMAT; i <= EWFD_PTX_START_ELAPSED; i++) {
         cur_ctrl = GetDlgItem(of_hwnd, i);
         if (cur_ctrl) {
             SetWindowText(cur_ctrl, _T("-"));
@@ -1168,7 +1160,7 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
     }
 
     /* Success! */
-    for (i = EWFD_PT_FORMAT; i <= EWFD_PTX_ELAPSED; i++) {
+    for (i = EWFD_PT_FORMAT; i <= EWFD_PTX_START_ELAPSED; i++) {
         cur_ctrl = GetDlgItem(of_hwnd, i);
         if (cur_ctrl) {
             EnableWindow(cur_ctrl, TRUE);
@@ -1181,9 +1173,8 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
 
     /* Size */
     filesize = wtap_file_size(wth, &err);
-    utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%" G_GINT64_FORMAT " bytes", filesize);
-    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_SIZE);
-    SetWindowText(cur_ctrl, string_buff);
+    // 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)) ) {
@@ -1210,8 +1201,10 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
     }
 
     if(err != 0) {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("error after reading %u packets"), packet);
-        cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_PACKETS);
+        utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, error after %u packets",
+            size_str, packet);
+        g_free(size_str);
+        cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_SIZE);
         SetWindowText(cur_ctrl, string_buff);
         wtap_close(wth);
         return TRUE;
@@ -1219,18 +1212,21 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
 
     /* Packets */
     if(is_breaked) {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("more than %u packets (preview timeout)"), packet);
+        utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, timed out at %u packets",
+            size_str, packet);
     } else {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%u"), packet);
+        utf_8to16_snprintf(string_buff, PREVIEW_STR_MAX, "%s, %u packets",
+            size_str, packet);
     }
-    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_PACKETS);
+    g_free(size_str);
+    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_SIZE);
     SetWindowText(cur_ctrl, string_buff);
 
-    /* First packet */
+    /* First packet / elapsed time */
     ti_time = (long)start_time;
     ti_tm = localtime( &ti_time );
     if(ti_tm) {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX,
+        StringCchPrintf(first_buff, PREVIEW_STR_MAX,
                  _T("%04d-%02d-%02d %02d:%02d:%02d"),
                  ti_tm->tm_year + 1900,
                  ti_tm->tm_mon + 1,
@@ -1239,24 +1235,21 @@ preview_set_file_info(HWND of_hwnd, gchar *preview_file) {
                  ti_tm->tm_min,
                  ti_tm->tm_sec);
     } else {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("?"));
+        StringCchPrintf(first_buff, PREVIEW_STR_MAX, _T("?"));
     }
-    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_FIRST_PKT);
-    SetWindowText(cur_ctrl, string_buff);
 
-    /* Elapsed time */
     elapsed_time = (unsigned int)(stop_time-start_time);
     if(elapsed_time/86400) {
-        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%02u days %02u:%02u:%02u"),
-        elapsed_time/86400, elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
+        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("%02u:%02u:%02u"),
-        elapsed_time%86400/3600, elapsed_time%3600/60, elapsed_time%60);
+        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("unknown"));
+        StringCchPrintf(string_buff, PREVIEW_STR_MAX, _T("%s / unknown"), first_buff);
     }
-    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_ELAPSED);
+    cur_ctrl = GetDlgItem(of_hwnd, EWFD_PTX_START_ELAPSED);
     SetWindowText(cur_ctrl, string_buff);
 
     wtap_close(wth);
@@ -1318,8 +1311,7 @@ filter_tb_syntax_check(HWND hwnd, TCHAR *filter_text) {
         SendMessage(hwnd, EM_SETBKGNDCOLOR, (WPARAM) 1, COLOR_WINDOW);
         return;
     } else if (dfilter_compile(utf_16to8(strval), &dfp, NULL)) { /* colorize filter string entry */
-        if (dfp != NULL)
-            dfilter_free(dfp);
+        dfilter_free(dfp);
         /* Valid (light green) */
         SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, RGB(0xe4, 0xff, 0xc7)); /* tango_chameleon_1 */
     } else {
@@ -1327,7 +1319,7 @@ filter_tb_syntax_check(HWND hwnd, TCHAR *filter_text) {
         SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, RGB(0xff, 0xcc, 0xcc)); /* tango_scarlet_red_1 */
     }
 
-    if (strval) g_free(strval);
+    g_free(strval);
 }
 
 
@@ -1347,22 +1339,12 @@ open_file_hook_proc(HWND of_hwnd, UINT msg, WPARAM w_param, LPARAM l_param) {
             }
 
             cur_ctrl = GetDlgItem(of_hwnd, EWFD_FORMAT_TYPE);
-            SendMessage(cur_ctrl, CB_ADDSTRING, 0, (WPARAM) _T("Automatic"));
+            SendMessage(cur_ctrl, CB_ADDSTRING, 0, (WPARAM) _T("Automatically detect file type"));
             for (i = 0; open_routines[i].name != NULL; i += 1) {
                 SendMessage(cur_ctrl, CB_ADDSTRING, 0, (WPARAM) utf_8to16(open_routines[i].name));
             }
             SendMessage(cur_ctrl, CB_SETCURSEL, 0, 0);
 
-            /* Fill in our resolution values */
-            cur_ctrl = GetDlgItem(of_hwnd, EWFD_MAC_NR_CB);
-            SendMessage(cur_ctrl, BM_SETCHECK, gbl_resolv_flags.mac_name, 0);
-            cur_ctrl = GetDlgItem(of_hwnd, EWFD_NET_NR_CB);
-            SendMessage(cur_ctrl, BM_SETCHECK, gbl_resolv_flags.network_name, 0);
-            cur_ctrl = GetDlgItem(of_hwnd, EWFD_TRANS_NR_CB);
-            SendMessage(cur_ctrl, BM_SETCHECK, gbl_resolv_flags.transport_name, 0);
-            cur_ctrl = GetDlgItem(of_hwnd, EWFD_EXTERNAL_NR_CB);
-            SendMessage(cur_ctrl, BM_SETCHECK, gbl_resolv_flags.use_external_net_name_resolver, 0);
-
             preview_set_file_info(of_hwnd, NULL);
             break;
         case WM_NOTIFY:
@@ -1370,26 +1352,12 @@ open_file_hook_proc(HWND of_hwnd, UINT msg, WPARAM w_param, LPARAM l_param) {
                 case CDN_FILEOK:
                     /* Fetch the read filter */
                     cur_ctrl = GetDlgItem(of_hwnd, EWFD_FILTER_EDIT);
-                    if (g_dfilter_str)
-                        g_free(g_dfilter_str);
+                    g_free(g_dfilter_str);
                     g_dfilter_str = filter_tb_get(cur_ctrl);
 
                     cur_ctrl = GetDlgItem(of_hwnd, EWFD_FORMAT_TYPE);
                     g_format_type = (unsigned int) SendMessage(cur_ctrl, CB_GETCURSEL, 0, 0);
 
-                    /* Fetch our resolution values */
-                    cur_ctrl = GetDlgItem(of_hwnd, EWFD_MAC_NR_CB);
-                    if (SendMessage(cur_ctrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
-                        gbl_resolv_flags.mac_name = TRUE;
-                    cur_ctrl = GetDlgItem(of_hwnd, EWFD_NET_NR_CB);
-                    if (SendMessage(cur_ctrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
-                        gbl_resolv_flags.network_name = TRUE;
-                    cur_ctrl = GetDlgItem(of_hwnd, EWFD_TRANS_NR_CB);
-                    if (SendMessage(cur_ctrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
-                        gbl_resolv_flags.transport_name = TRUE;
-                    cur_ctrl = GetDlgItem(of_hwnd, EWFD_EXTERNAL_NR_CB);
-                    if (SendMessage(cur_ctrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
-                        gbl_resolv_flags.use_external_net_name_resolver = TRUE;
                     break;
                 case CDN_SELCHANGE:
                     /* This _almost_ works correctly. We need to handle directory
@@ -1438,7 +1406,7 @@ append_file_extension_type(GArray *sa, int et)
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
-    TCHAR *str16;
+    const TCHAR *str16;
     guint16 zero = 0;
 
     /* Construct the list of patterns. */
@@ -1471,7 +1439,7 @@ append_file_extension_type(GArray *sa, int et)
 
 static TCHAR *
 build_file_open_type_list(void) {
-    TCHAR *str16;
+    const TCHAR *str16;
     int et;
     GArray* sa;
     static const guint16 zero = 0;
@@ -1507,19 +1475,18 @@ build_file_open_type_list(void) {
     sa = g_array_append_val(sa, zero);
 
     /*
-     * Add an "All Capture Files" entry, with all the extensions we
-     * know about.
+     * Add an "All Capture Files" entry, with all the capture file
+     * extensions we know about.
      */
     str16 = utf_8to16("All Capture Files");
     sa = g_array_append_vals(sa, str16, (guint) strlen("All Capture Files"));
     sa = g_array_append_val(sa, zero);
 
     /*
-     * Construct its list of patterns from a list of all extensions
-     * we support.
+     * Construct its list of patterns.
      */
     pattern_str = g_string_new("");
-    extensions_list = wtap_get_all_file_extensions_list();
+    extensions_list = wtap_get_all_capture_file_extensions_list();
     sep = '\0';
     for (extension = extensions_list; extension != NULL;
          extension = g_slist_next(extension)) {
@@ -1565,7 +1532,7 @@ append_file_type(GArray *sa, int ft)
     GString* description_str = g_string_new("");
     gchar sep;
     GSList *extensions_list, *extension;
-    TCHAR *str16;
+    const TCHAR *str16;
     guint16 zero = 0;
 
     extensions_list = wtap_get_file_extensions_list(ft, TRUE);
@@ -2239,8 +2206,7 @@ merge_file_hook_proc(HWND mf_hwnd, UINT msg, WPARAM w_param, LPARAM l_param) {
                 case CDN_FILEOK:
                     /* Fetch the read filter */
                     cur_ctrl = GetDlgItem(mf_hwnd, EWFD_FILTER_EDIT);
-                    if (g_dfilter_str)
-                        g_free(g_dfilter_str);
+                    g_free(g_dfilter_str);
                     g_dfilter_str = filter_tb_get(cur_ctrl);
 
                     cur_ctrl = GetDlgItem(mf_hwnd, EWFD_MERGE_CHRONO_BTN);