diff options
author | Gerald Combs <gerald@wireshark.org> | 2014-09-24 14:06:23 -0700 |
---|---|---|
committer | Gerald Combs <gerald@wireshark.org> | 2014-09-25 22:02:43 +0000 |
commit | ea6fa049c9c43a6852c2a1e8b72598e13a27786b (patch) | |
tree | 317fec804ce7aaaf619f813bba9c0efda22020b6 /ui/qt | |
parent | 2ee45fe2daffe10b519dc54699480db1c0a4f5ed (diff) |
Update the simple dialog code.
Rename simple_dialog_qt.{cpp,h} to simple_dialog.{cpp,h}. Make it a
subclass of QMessageBox. Queue messages at startup similar to GTK+.
Move the GTK+-specific simple_dialog declarations to
gtk/simple_dialog.h.
Don't yell at the user so much. Replace exclamation points with periods.
Change-Id: I1cc771106222d5e06f1f52d67ac29d6dc367cce4
Reviewed-on: https://code.wireshark.org/review/4288
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Diffstat (limited to 'ui/qt')
-rw-r--r-- | ui/qt/CMakeLists.txt | 4 | ||||
-rw-r--r-- | ui/qt/Makefile.common | 5 | ||||
-rw-r--r-- | ui/qt/Wireshark.pro | 4 | ||||
-rw-r--r-- | ui/qt/main.cpp | 4 | ||||
-rw-r--r-- | ui/qt/main_window.cpp | 63 | ||||
-rw-r--r-- | ui/qt/simple_dialog.cpp | 266 | ||||
-rw-r--r-- | ui/qt/simple_dialog.h (renamed from ui/qt/simple_dialog_qt.h) | 32 | ||||
-rw-r--r-- | ui/qt/simple_dialog_qt.cpp | 268 |
8 files changed, 362 insertions, 284 deletions
diff --git a/ui/qt/CMakeLists.txt b/ui/qt/CMakeLists.txt index bacb144fb9..295b41bd05 100644 --- a/ui/qt/CMakeLists.txt +++ b/ui/qt/CMakeLists.txt @@ -88,7 +88,7 @@ set(WIRESHARK_QT_HEADERS search_frame.h sequence_diagram.h sequence_dialog.h - simple_dialog_qt.h + simple_dialog.h splash_overlay.h stats_tree_dialog.h summary_dialog.h @@ -184,7 +184,7 @@ set(WIRESHARK_QT_SRC search_frame.cpp sequence_diagram.cpp sequence_dialog.cpp - simple_dialog_qt.cpp + simple_dialog.cpp splash_overlay.cpp sparkline_delegate.cpp stock_icon.cpp diff --git a/ui/qt/Makefile.common b/ui/qt/Makefile.common index 9e9941e8e1..1c3f67b37e 100644 --- a/ui/qt/Makefile.common +++ b/ui/qt/Makefile.common @@ -70,6 +70,7 @@ NODIST_GENERATED_HEADER_FILES = \ ui_stats_tree_dialog.h \ ui_search_frame.h \ ui_sequence_dialog.h \ + ui_simple_dialog.h \ ui_splash_overlay.h \ ui_summary_dialog.h \ ui_tcp_stream_dialog.h \ @@ -180,7 +181,7 @@ MOC_HDRS = \ sctp_graph_byte_dialog.h \ sequence_diagram.h \ sequence_dialog.h \ - simple_dialog_qt.h \ + simple_dialog.h \ sparkline_delegate.h \ splash_overlay.h \ stats_tree_dialog.h \ @@ -372,7 +373,7 @@ WIRESHARK_QT_SRC = \ search_frame.cpp \ sequence_diagram.cpp \ sequence_dialog.cpp \ - simple_dialog_qt.cpp \ + simple_dialog.cpp \ sparkline_delegate.cpp \ splash_overlay.cpp \ stats_tree_dialog.cpp \ diff --git a/ui/qt/Wireshark.pro b/ui/qt/Wireshark.pro index c01681ff42..0bcdc85b5b 100644 --- a/ui/qt/Wireshark.pro +++ b/ui/qt/Wireshark.pro @@ -573,7 +573,7 @@ HEADERS += \ related_packet_delegate.h \ sequence_diagram.h \ sequence_dialog.h \ - simple_dialog_qt.h \ + simple_dialog.h \ sparkline_delegate.h \ syntax_line_edit.h \ time_shift_dialog.h \ @@ -654,7 +654,7 @@ SOURCES += \ search_frame.cpp \ sequence_diagram.cpp \ sequence_dialog.cpp \ - simple_dialog_qt.cpp \ + simple_dialog.cpp \ sparkline_delegate.cpp \ splash_overlay.cpp \ stats_tree_dialog.cpp \ diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index 7281105bc0..82dc1d09fd 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -21,8 +21,9 @@ #include "config.h" -#include "wireshark_application.h" +#include "simple_dialog.h" #include "main_window.h" +#include "wireshark_application.h" #include <ctype.h> #include "globals.h" @@ -799,6 +800,7 @@ int main(int argc, char *argv[]) // Init the main window (and splash) main_w = new(MainWindow); main_w->show(); + SimpleDialog::displayQueuedMessages(main_w); // We may not need a queued connection here but it would seem to make sense // to force the issue. main_w->connect(&ws_app, SIGNAL(openCaptureFile(QString&,QString&,unsigned int)), diff --git a/ui/qt/main_window.cpp b/ui/qt/main_window.cpp index ce51981129..9ca45f6bf4 100644 --- a/ui/qt/main_window.cpp +++ b/ui/qt/main_window.cpp @@ -49,6 +49,7 @@ #include "export_dissection_dialog.h" #include "import_text_dialog.h" #include "proto_tree.h" +#include "simple_dialog.h" #include "stock_icon.h" #include "wireshark_application.h" @@ -80,6 +81,68 @@ void pipe_input_set_handler(gint source, gpointer user_data, int *child_process, gbl_cur_main_window_->setPipeInputHandler(source, user_data, child_process, input_cb); } +gpointer +simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...) +{ + va_list ap; + + va_start(ap, msg_format); + SimpleDialog sd(gbl_cur_main_window_, type, btn_mask, msg_format, ap); + va_end(ap); + + sd.exec(); + return NULL; +} + +/* + * Alert box, with optional "don't show this message again" variable + * and checkbox, and optional secondary text. + */ +void +simple_message_box(ESD_TYPE_E type, gboolean *notagain, + const char *secondary_msg, const char *msg_format, ...) +{ + if (notagain && *notagain) { + return; + } + + va_list ap; + + va_start(ap, msg_format); + SimpleDialog sd(gbl_cur_main_window_, type, ESD_BTN_OK, msg_format, ap); + va_end(ap); + + sd.setDetailedText(secondary_msg); + +#if (QT_VERSION > QT_VERSION_CHECK(5, 2, 0)) + QCheckBox *cb = new QCheckBox(); + if (notagain) { + cb->setChecked(true); + cb->setText(QObject::tr("Don't show this message again.")); + sd.setCheckBox(cb); + } +#endif + + sd.exec(); + +#if (QT_VERSION > QT_VERSION_CHECK(5, 2, 0)) + if (notagain) { + *notagain = cb->isChecked(); + } +#endif +} + +/* + * Error alert box, taking a format and a va_list argument. + */ +void +vsimple_error_message_box(const char *msg_format, va_list ap) +{ + SimpleDialog sd(NULL, ESD_TYPE_ERROR, ESD_BTN_OK, msg_format, ap); + sd.exec(); +} + + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), main_ui_(new Ui::MainWindow), diff --git a/ui/qt/simple_dialog.cpp b/ui/qt/simple_dialog.cpp new file mode 100644 index 0000000000..9f6335fd20 --- /dev/null +++ b/ui/qt/simple_dialog.cpp @@ -0,0 +1,266 @@ +/* simple_dialog.cpp + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 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. + */ + +#include "simple_dialog.h" + +#include "epan/strutil.h" + +#include "ui/utf8_entities.h" + +#include "qt_ui_utils.h" + +#include <QMessageBox> +#include <QTextCodec> + +/* Simple dialog function - Displays a dialog box with the supplied message + * text. + * + * This is meant to be used as a backend for the functions defined in + * ui/simple_dialog.h. Qt code should use QMessageBox directly. + * + * Args: + * type : One of ESD_TYPE_*. + * btn_mask : The value passed in determines which buttons are displayed. + * msg_format : Sprintf-style format of the text displayed in the dialog. + * ... : Argument list for msg_format + */ + +QList<MessagePair> message_queue_; +ESD_TYPE_E max_severity_ = ESD_TYPE_INFO; + +const char *primary_delimiter_ = "\uffff"; + +const char * +simple_dialog_primary_start(void) { + return primary_delimiter_; +} + +const char * +simple_dialog_primary_end(void) { + return primary_delimiter_; +} + +char * +simple_dialog_format_message(const char *msg) +{ + char *str; + + if (msg) { + str = xml_escape(msg); + } else { + str = NULL; + } + return str; +} + +/* + * Error alert box, taking a format and a list of arguments. + */ +void +simple_error_message_box(const char *msg_format, ...) +{ + va_list ap; + + va_start(ap, msg_format); + vsimple_error_message_box(msg_format, ap); + va_end(ap); +} + +#include <QDebug> +SimpleDialog::SimpleDialog(QWidget *parent, ESD_TYPE_E type, int btn_mask, const char *msg_format, va_list ap) : + QMessageBox(parent) +{ + gchar *vmessage; + QString message; + + vmessage = g_strdup_vprintf(msg_format, ap); + message = QTextCodec::codecForLocale()->toUnicode(vmessage); + g_free(vmessage); + + MessagePair msg_pair = splitMessage(message); + QString primary = msg_pair.first; + QString secondary = msg_pair.second; + + if (primary.isEmpty()) { + return; + } + + if (!parent) { + message_queue_ << msg_pair; + if (type > max_severity_) { + max_severity_ = type; + } + return; + } + + switch(type) { + case ESD_TYPE_ERROR: + setIcon(QMessageBox::Critical); + break; + case ESD_TYPE_WARN: + setIcon(QMessageBox::Warning); + break; + case ESD_TYPE_CONFIRMATION: + setIcon(QMessageBox::Question); + break; + case ESD_TYPE_INFO: + default: + setIcon(QMessageBox::Information); + break; + } + + if (btn_mask & ESD_BTN_OK) { + addButton(QMessageBox::Ok); + } + if (btn_mask & ESD_BTN_CANCEL) { + addButton(QMessageBox::Cancel); + } + if (btn_mask & ESD_BTN_YES) { + addButton(QMessageBox::Yes); + } + if (btn_mask & ESD_BTN_NO) { + addButton(QMessageBox::No); + } +// if (btn_mask & ESD_BTN_CLEAR) { +// addButton(QMessageBox::); +// } + if (btn_mask & ESD_BTN_SAVE) { + addButton(QMessageBox::Save); + } + if (btn_mask & ESD_BTN_DONT_SAVE) { + addButton(QMessageBox::Discard); + } +// if (btn_mask & ESD_BTN_QUIT_DONT_SAVE) { +// addButton(QMessageBox::); +// } + + setText(primary); + setInformativeText(secondary); +} + +SimpleDialog::~SimpleDialog() +{ +} + +void SimpleDialog::displayQueuedMessages(QWidget *parent) +{ + if (message_queue_.isEmpty()) { + return; + } + + QMessageBox mb(parent); + + switch(max_severity_) { + case ESD_TYPE_ERROR: + mb.setIcon(QMessageBox::Critical); + break; + case ESD_TYPE_WARN: + mb.setIcon(QMessageBox::Warning); + break; + case ESD_TYPE_CONFIRMATION: + mb.setIcon(QMessageBox::Question); + break; + case ESD_TYPE_INFO: + default: + mb.setIcon(QMessageBox::Information); + break; + } + + mb.addButton(QMessageBox::Ok); + + if (message_queue_.length() > 1) { + QStringList msg_details; + QString first_primary = message_queue_[0].first; + first_primary.append(UTF8_HORIZONTAL_ELLIPSIS); + + mb.setText(tr("Multiple problems found")); + mb.setInformativeText(first_primary); + + foreach (MessagePair msg_pair, message_queue_) { + msg_details << msg_pair.first; + if (!msg_pair.second.isEmpty()) { + msg_details.append(msg_pair.second); + } + } + mb.setDetailedText(msg_details.join("\n\n")); + } else { + mb.setText(message_queue_[0].first); + mb.setInformativeText(message_queue_[0].second); + } + + message_queue_.clear(); + max_severity_ = ESD_TYPE_INFO; + + mb.exec(); +} + +int SimpleDialog::exec() +{ + if (!parentWidget()) { + return 0; + } + + switch (QMessageBox::exec()) { + case QMessageBox::Ok: + return ESD_BTN_OK; + case QMessageBox::Yes: + return ESD_BTN_YES; + case QMessageBox::No: + return ESD_BTN_NO; + case QMessageBox::Save: + return ESD_BTN_SAVE; + case QMessageBox::Discard: + return ESD_BTN_DONT_SAVE; + case QMessageBox::Cancel: // XXX Should OK be the default? + default: + return ESD_BTN_CANCEL; + } +} + +const MessagePair SimpleDialog::splitMessage(QString &message) const +{ + if (message.startsWith(primary_delimiter_)) { + QStringList parts = message.split(primary_delimiter_, QString::SkipEmptyParts); + switch (parts.length()) { + case 0: + return MessagePair(QString(), QString()); + case 1: + return MessagePair(parts[0], QString()); + default: + QString first = parts.takeFirst(); + return MessagePair(first, parts.join(" ")); + } + } + return MessagePair(message, QString()); +} + +/* + * 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/qt/simple_dialog_qt.h b/ui/qt/simple_dialog.h index 7646658fc4..98ab98598a 100644 --- a/ui/qt/simple_dialog_qt.h +++ b/ui/qt/simple_dialog.h @@ -1,4 +1,4 @@ -/* simple_dialog_qt.h +/* simple_dialog.h * * Wireshark - Network traffic analyzer * By Gerald Combs <gerald@wireshark.org> @@ -19,24 +19,38 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef SIMPLE_DIALOG_QT_H -#define SIMPLE_DIALOG_QT_H +#ifndef SIMPLE_DIALOG_H +#define SIMPLE_DIALOG_H -#include <QErrorMessage> +#include "config.h" -class SimpleDialog : public QErrorMessage +#include <stdio.h> + +#include <glib.h> + +#include "ui/simple_dialog.h" + +#include <QMessageBox> + +typedef QPair<QString,QString> MessagePair; + +class SimpleDialog : public QMessageBox { Q_OBJECT -public: - explicit SimpleDialog(QWidget *parent = 0); -signals: +public: + explicit SimpleDialog(QWidget *parent, ESD_TYPE_E type, int btn_mask, const char *msg_format, va_list ap); + ~SimpleDialog(); + static void displayQueuedMessages(QWidget *parent = 0); public slots: + int exec(); +private: + const MessagePair splitMessage(QString &message) const; }; -#endif // SIMPLE_DIALOG_QT_H +#endif // SIMPLE_DIALOG_H /* * Editor modelines diff --git a/ui/qt/simple_dialog_qt.cpp b/ui/qt/simple_dialog_qt.cpp deleted file mode 100644 index a45fb12bba..0000000000 --- a/ui/qt/simple_dialog_qt.cpp +++ /dev/null @@ -1,268 +0,0 @@ -/* simple_dialog_qt.cpp - * - * Wireshark - Network traffic analyzer - * By Gerald Combs <gerald@wireshark.org> - * Copyright 1998 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. - */ - -#include "config.h" - -#include <stdio.h> - -#include <glib.h> - -#include <epan/strutil.h> - -#include "ui/simple_dialog.h" - -#include "simple_dialog_qt.h" - -#include <QMessageBox> - -/* Simple dialog function - Displays a dialog box with the supplied message - * text. - * - * Args: - * type : One of ESD_TYPE_*. - * btn_mask : The value passed in determines which buttons are displayed. - * msg_format : Sprintf-style format of the text displayed in the dialog. - * ... : Argument list for msg_format - */ - -gpointer -vsimple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, va_list ap) -{ - - gchar *vmessage; - gchar *message; - SimpleDialog *dlg = NULL; -// queued_message_t *queued_message; -// GtkWidget *win; -// GdkWindowState state = 0; - - /* Format the message. */ - vmessage = g_strdup_vprintf(msg_format, ap); - - /* convert character encoding from locale to UTF8 (using iconv) */ - message = g_locale_to_utf8(vmessage, -1, NULL, NULL, NULL); - g_free(vmessage); - - g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: vsimple_dialog t: %d bm: %d m: %s", type, btn_mask, message); - - -// if (top_level != NULL) { -// state = gdk_window_get_state(top_level->window); -// } - -// /* If we don't yet have a main window or it's iconified, don't show the -// dialog. If showing up a dialog, while main window is iconified, program -// will become unresponsive! */ -// if (top_level == NULL || state & GDK_WINDOW_STATE_ICONIFIED) { - -// queued_message = g_malloc(sizeof (queued_message_t)); -// queued_message->type = type; -// queued_message->btn_mask = btn_mask; -// queued_message->message = message; -// message_queue = g_slist_append(message_queue, queued_message); -// return NULL; -// } - -// /* -// * Do we have any queued up messages? If so, pop them up. -// */ -// display_queued_messages(); - -// win = display_simple_dialog(type, btn_mask, message); - - g_free(message); - - return dlg; -} - -gpointer -simple_dialog(ESD_TYPE_E type, gint btn_mask, const gchar *msg_format, ...) -{ - va_list ap; - gpointer ret; - - va_start(ap, msg_format); - ret = vsimple_dialog(type, btn_mask, msg_format, ap); - va_end(ap); - return ret; -} - -const char * -simple_dialog_primary_start(void) { - return ""; -} - -const char * -simple_dialog_primary_end(void) { - return ""; -} - -char * -simple_dialog_format_message(const char *msg) -{ - char *str; - - if (msg) { - str = xml_escape(msg); - } else { - str = NULL; - } - return str; -} - -void simple_dialog_set_cb(gpointer dialog, simple_dialog_cb_t callback_fct, gpointer data) -{ - g_log(NULL, G_LOG_LEVEL_DEBUG, "FIX: simple_dialog_set_cb d: %p cf: %p d: %p", dialog, callback_fct, data); - -// g_object_set_data(G_OBJECT(GTK_WIDGET(dialog)), CALLBACK_FCT_KEY, callback_fct); -// g_object_set_data(G_OBJECT(GTK_WIDGET(dialog)), CALLBACK_DATA_KEY, data); -} - - -SimpleDialog::SimpleDialog(QWidget *parent) : - QErrorMessage(parent) -{ -} - -static void -do_simple_message_box(ESD_TYPE_E type, gboolean *notagain, - const char *secondary_msg, const char *msg_format, - va_list ap) -{ - QMessageBox *msg_dialog; - gchar *message; - - if (notagain != NULL) { - if (*notagain) { - /* - * The user had checked the "Don't show this message again" checkbox - * in the past; don't bother showing it. - */ - return; - } - } - - /* - * XXX - this should be passed the main window. - * Also, this should be set to window modal by setting its window - * modality to Qt::WindowModal, so it shows up as a sheet in - * OS X. - */ - msg_dialog = new QMessageBox(NULL); - switch (type) { - - case ESD_TYPE_INFO: - msg_dialog->setIcon(QMessageBox::Information); - break; - - case ESD_TYPE_WARN: - msg_dialog->setIcon(QMessageBox::Warning); - break; - - case ESD_TYPE_ERROR: - msg_dialog->setIcon(QMessageBox::Critical); - break; - - default: - g_assert_not_reached(); - return; - } - - /* Format the message. */ - message = g_strdup_vprintf(msg_format, ap); - msg_dialog->setText(message); - g_free(message); - - /* Add the secondary text. */ - if (secondary_msg != NULL) - msg_dialog->setInformativeText(secondary_msg); - -#if 0 - if (notagain != NULL) { - checkbox = gtk_check_button_new_with_label("Don't show this message again."); - gtk_container_set_border_width(GTK_CONTAINER(checkbox), 12); - gtk_box_pack_start(GTK_BOX(gtk_message_dialog_get_message_area(GTK_MESSAGE_DIALOG(msg_dialog))), checkbox, - TRUE, TRUE, 0); - gtk_widget_show(checkbox); - } -#endif - - msg_dialog->exec(); -#if 0 - if (notagain != NULL) { - /* - * OK, did they check the checkbox? - */ - *notagain = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox)); - } -#endif - delete msg_dialog; -} - -/* - * Alert box, with optional "don't show this message again" variable - * and checkbox, and optional secondary text. - */ -void -simple_message_box(ESD_TYPE_E type, gboolean *notagain, - const char *secondary_msg, const char *msg_format, ...) -{ - va_list ap; - - va_start(ap, msg_format); - do_simple_message_box(type, notagain, secondary_msg, msg_format, ap); - va_end(ap); -} - -/* - * Error alert box, taking a format and a va_list argument. - */ -void -vsimple_error_message_box(const char *msg_format, va_list ap) -{ - do_simple_message_box(ESD_TYPE_ERROR, NULL, NULL, msg_format, ap); -} - -/* - * Error alert box, taking a format and a list of arguments. - */ -void -simple_error_message_box(const char *msg_format, ...) -{ - va_list ap; - - va_start(ap, msg_format); - do_simple_message_box(ESD_TYPE_ERROR, NULL, NULL, msg_format, ap); - va_end(ap); -} - -/* - * 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: - */ |