update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / ui / qt / io_console_dialog.cpp
blob0b9eb858525626d653b229319b9c17c04b332eef
1 /*
2 * io_console_dialog.c
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include <config.h>
12 #define WS_LOG_DOMAIN LOG_DOMAIN_QTUI
13 #include "io_console_dialog.h"
14 #include <ui_io_console_dialog.h>
16 #include "main_application.h"
18 extern "C" {
19 static void print_function(const char *str, void *ptr);
22 static void print_function(const char *str, void *print_data)
24 IOConsoleDialog *dialog = static_cast<IOConsoleDialog *>(print_data);
25 dialog->appendOutputText(QString(str));
28 IOConsoleDialog::IOConsoleDialog(QWidget &parent,
29 QString title,
30 funnel_console_eval_cb_t eval_cb,
31 funnel_console_open_cb_t open_cb,
32 funnel_console_close_cb_t close_cb,
33 void *callback_data = nullptr) :
34 GeometryStateDialog(&parent),
35 ui(new Ui::IOConsoleDialog),
36 eval_cb_(eval_cb),
37 open_cb_(open_cb),
38 close_cb_(close_cb),
39 callback_data_(callback_data)
41 ui->setupUi(this);
43 if (title.isEmpty())
44 title = QStringLiteral("Console");
46 loadGeometry(0, 0, title);
47 loadSplitterState(ui->splitter);
48 setWindowTitle(mainApp->windowTitleString(title));
50 QPushButton *eval_button = ui->buttonBox->addButton(tr("Evaluate"), QDialogButtonBox::ActionRole);
51 eval_button->setDefault(true);
52 eval_button->setShortcut(QKeySequence("Ctrl+Return"));
53 connect(eval_button, &QPushButton::clicked, this, &IOConsoleDialog::acceptInput);
55 QPushButton *clear_button = ui->buttonBox->addButton(tr("Clear"), QDialogButtonBox::ActionRole);
56 connect(clear_button, &QPushButton::clicked, this, &IOConsoleDialog::on_clearActivated);
58 ui->inputTextEdit->setFont(mainApp->monospaceFont());
59 ui->inputTextEdit->setPlaceholderText(tr("Use %1 to evaluate.")
60 .arg(eval_button->shortcut().toString(QKeySequence::NativeText)));
62 ui->outputTextEdit->setFont(mainApp->monospaceFont());
63 ui->outputTextEdit->setReadOnly(true);
65 ui->hintLabel->clear();
67 // Install print
68 open_cb_(print_function, this, callback_data_);
71 IOConsoleDialog::~IOConsoleDialog()
73 delete ui;
74 // Remove print
75 close_cb_(callback_data_);
78 void IOConsoleDialog::setHintText(const QString &text)
80 ui->hintLabel->setText(QStringLiteral("<small><i>%1.</i></small>").arg(text));
83 void IOConsoleDialog::clearHintText()
85 ui->hintLabel->clear();
88 void IOConsoleDialog::clearSuccessHint()
90 // Text changed so we no longer have a success.
91 ui->hintLabel->clear();
92 // Disconnect this slot until the next success.
93 disconnect(ui->inputTextEdit, &QTextEdit::textChanged, this, &IOConsoleDialog::clearSuccessHint);
96 void IOConsoleDialog::acceptInput()
98 clearHintText();
100 QString text = ui->inputTextEdit->toPlainText();
101 if (text.isEmpty())
102 return;
104 char *error_str = nullptr;
105 char *error_hint = nullptr;
106 int result = eval_cb_(qUtf8Printable(text), &error_str, &error_hint, callback_data_);
107 if (result != 0) {
108 if (error_hint) {
109 QString hint(error_hint);
110 setHintText(hint.at(0).toUpper() + hint.mid(1));
111 g_free(error_hint);
113 else if (result < 0) {
114 setHintText("Error loading string");
116 else {
117 setHintText("Error running chunk");
119 if (error_str) {
120 appendOutputText(QString(error_str));
121 g_free(error_str);
124 else {
125 setHintText("Code evaluated successfully");
126 connect(ui->inputTextEdit, &QTextEdit::textChanged, this, &IOConsoleDialog::clearSuccessHint);
130 void IOConsoleDialog::appendOutputText(const QString &text)
132 ui->outputTextEdit->append(text);
135 void IOConsoleDialog::on_clearActivated()
137 ui->inputTextEdit->clear();
138 ui->outputTextEdit->clear();
139 ui->hintLabel->clear();