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
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"
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
,
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
),
39 callback_data_(callback_data
)
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();
68 open_cb_(print_function
, this, callback_data_
);
71 IOConsoleDialog::~IOConsoleDialog()
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()
100 QString text
= ui
->inputTextEdit
->toPlainText();
104 char *error_str
= nullptr;
105 char *error_hint
= nullptr;
106 int result
= eval_cb_(qUtf8Printable(text
), &error_str
, &error_hint
, callback_data_
);
109 QString
hint(error_hint
);
110 setHintText(hint
.at(0).toUpper() + hint
.mid(1));
113 else if (result
< 0) {
114 setHintText("Error loading string");
117 setHintText("Error running chunk");
120 appendOutputText(QString(error_str
));
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();