From f5edf6f2136b21aca7f7c94ce3d3c9a91cbe12a6 Mon Sep 17 00:00:00 2001 From: Paul I Date: Mon, 27 Aug 2018 14:16:48 +0300 Subject: [PATCH] Add Core()->message() (#658) * Add Core()->message() * Remove MainWindows::addOutput * Use qDebug() for debug messages * Add message() to PythonApi * MainWindow: Replace Core() calls --- src/Cutter.cpp | 12 +++++++ src/Cutter.h | 5 +++ src/MainWindow.cpp | 72 +++++++++++++++++-------------------- src/MainWindow.h | 2 -- src/utils/PythonAPI.cpp | 20 +++++++++++ src/widgets/ConsoleWidget.h | 6 ++-- 6 files changed, 73 insertions(+), 44 deletions(-) diff --git a/src/Cutter.cpp b/src/Cutter.cpp index 631bbf7b..3a2d18da 100644 --- a/src/Cutter.cpp +++ b/src/Cutter.cpp @@ -585,6 +585,18 @@ void CutterCore::triggerGraphOptionsChanged() emit graphOptionsChanged(); } +void CutterCore::message(const QString &msg, bool debug) +{ + if (msg.isEmpty()) + return; + if (debug) { + qDebug() << msg; + emit newDebugMessage(msg); + return; + } + emit newMessage(msg); +} + QString CutterCore::getConfig(const QString &k) { CORE_LOCK(); diff --git a/src/Cutter.h b/src/Cutter.h index 55eb934a..d96394a2 100644 --- a/src/Cutter.h +++ b/src/Cutter.h @@ -598,6 +598,8 @@ public: void triggerAsmOptionsChanged(); void triggerGraphOptionsChanged(); + void message(const QString &msg, bool debug = false); + void setCutterPlugins(QList plugins); QList getCutterPlugins(); @@ -640,6 +642,9 @@ signals: void changeDefinedView(); void changeDebugView(); + void newMessage(const QString &msg); + void newDebugMessage(const QString &msg); + public slots: private: diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index b6cec28c..9fd96a61 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -263,12 +263,17 @@ void MainWindow::initUI() connect(core, &CutterCore::changeDebugView, this, &MainWindow::changeDebugView); connect(core, &CutterCore::changeDefinedView, this, &MainWindow::changeDefinedView); + connect(core, SIGNAL(newMessage(const QString &)), + this->consoleDock, SLOT(addOutput(const QString &))); + connect(core, SIGNAL(newDebugMessage(const QString &)), + this->consoleDock, SLOT(addDebugOutput(const QString &))); + updateTasksIndicator(); connect(core->getAsyncTaskManager(), &AsyncTaskManager::tasksChanged, this, &MainWindow::updateTasksIndicator); /* Load plugins */ - QList plugins = Core()->getCutterPlugins(); + QList plugins = core->getCutterPlugins(); for (auto plugin : plugins) { CutterDockWidget *pluginDock = plugin->setupInterface(this); tabifyDockWidget(dashboardDock, pluginDock); @@ -277,7 +282,7 @@ void MainWindow::initUI() void MainWindow::updateTasksIndicator() { - bool running = Core()->getAsyncTaskManager()->getTasksRunning(); + bool running = core->getAsyncTaskManager()->getTasksRunning(); tasksProgressIndicator->setProgressIndicatorVisible(running); } @@ -387,12 +392,12 @@ void MainWindow::finalizeOpen() // Override any incorrect setting saved in the project core->setSettings(); - addOutput(tr(" > Populating UI")); + core->message(tr(" > Populating UI")); refreshAll(); - addOutput(tr(" > Finished, happy reversing :)")); + core->message(tr(" > Finished, happy reversing :)")); // Add fortune message - addOutput("\n" + core->cmd("fo")); + core->message("\n" + core->cmd("fo")); showMaximized(); } @@ -433,17 +438,17 @@ void MainWindow::closeEvent(QCloseEvent *event) tr("Do you really want to exit?\nSave your project before closing!"), (QMessageBox::StandardButtons)(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel)); if (ret == QMessageBox::Save) { - if (saveProject(true) && !Core()->currentlyDebugging) { + if (saveProject(true) && !core->currentlyDebugging) { saveSettings(); - } else if (Core()->currentlyDebugging) { - Core()->stopDebug(); + } else if (core->currentlyDebugging) { + core->stopDebug(); } QMainWindow::closeEvent(event); } else if (ret == QMessageBox::Discard) { - if (!Core()->currentlyDebugging) { + if (!core->currentlyDebugging) { saveSettings(); - } else if (Core()->currentlyDebugging) { - Core()->stopDebug(); + } else if (core->currentlyDebugging) { + core->stopDebug(); } QMainWindow::closeEvent(event); } else { @@ -534,7 +539,7 @@ void MainWindow::setTabLocation() void MainWindow::refreshAll() { - Core()->triggerRefreshAll(); + core->triggerRefreshAll(); } void MainWindow::lockUnlock_Docks(bool what) @@ -705,7 +710,7 @@ void MainWindow::resetToDefaultLayout() restoreFunctionDock.restoreWidth(functionsDock->widget()); restoreSidebarDock.restoreWidth(sidebarDock->widget()); - Core()->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Disassembly); + core->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Disassembly); } void MainWindow::resetToZenLayout() @@ -723,18 +728,18 @@ void MainWindow::resetToZenLayout() restoreFunctionDock.restoreWidth(functionsDock->widget()); - Core()->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Disassembly); + core->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Disassembly); } void MainWindow::resetToDebugLayout() { - CutterCore::MemoryWidgetType memType = Core()->getMemoryWidgetPriority(); + CutterCore::MemoryWidgetType memType = core->getMemoryWidgetPriority(); bool isMaxim = isMaximized(); hideAllDocks(); restoreDocks(); showDebugDocks(); readDebugSettings(); - Core()->raisePrioritizedMemoryWidget(memType); + core->raisePrioritizedMemoryWidget(memType); if (isMaxim) { showMaximized(); } else { @@ -742,17 +747,6 @@ void MainWindow::resetToDebugLayout() } } -void MainWindow::addOutput(const QString &msg) -{ - consoleDock->addOutput(msg); -} - -void MainWindow::addDebugOutput(const QString &msg) -{ - printf("debug output: %s\n", msg.toLocal8Bit().constData()); - consoleDock->addDebugOutput(msg); -} - void MainWindow::on_actionLock_triggered() { panelLock = !panelLock; @@ -825,7 +819,7 @@ void MainWindow::on_actionRun_Script_triggered() fileName = dialog.getOpenFileName(this, tr("Select radare2 script")); if (!fileName.length()) // Cancel was pressed return; - Core()->loadScript(fileName); + core->loadScript(fileName); } /** @@ -869,22 +863,22 @@ void MainWindow::on_actionQuit_triggered() void MainWindow::on_actionBackward_triggered() { - Core()->seekPrev(); + core->seekPrev(); } void MainWindow::on_actionForward_triggered() { - Core()->seekNext(); + core->seekNext(); } void MainWindow::on_actionUndoSeek_triggered() { - Core()->seekPrev(); + core->seekPrev(); } void MainWindow::on_actionRedoSeek_triggered() { - Core()->seekNext(); + core->seekNext(); } void MainWindow::on_actionDisasAdd_comment_triggered() @@ -940,8 +934,8 @@ void MainWindow::on_actionImportPDB_triggered() QString pdbFile = dialog.selectedFiles().first(); if (!pdbFile.isEmpty()) { - Core()->loadPDB(pdbFile); - addOutput(tr("%1 loaded.").arg(pdbFile)); + core->loadPDB(pdbFile); + core->message(tr("%1 loaded.").arg(pdbFile)); } } @@ -999,7 +993,7 @@ void MainWindow::on_actionExport_as_code_triggered() void MainWindow::projectSaved(const QString &name) { - addOutput(tr("Project saved: ") + name); + core->message(tr("Project saved: ") + name); } void MainWindow::changeDebugView() @@ -1011,21 +1005,21 @@ void MainWindow::changeDebugView() void MainWindow::changeDefinedView() { saveDebugSettings(); - CutterCore::MemoryWidgetType memType = Core()->getMemoryWidgetPriority(); + CutterCore::MemoryWidgetType memType = core->getMemoryWidgetPriority(); hideAllDocks(); restoreDocks(); readSettings(); - Core()->raisePrioritizedMemoryWidget(memType); + core->raisePrioritizedMemoryWidget(memType); } void MainWindow::mousePressEvent(QMouseEvent *event) { switch (event->button()) { case Qt::BackButton: - Core()->seekPrev(); + core->seekPrev(); break; case Qt::ForwardButton: - Core()->seekNext(); + core->seekNext(); break; default: break; diff --git a/src/MainWindow.h b/src/MainWindow.h index 618275a6..32fd98f4 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -93,8 +93,6 @@ public: void readDebugSettings(); void saveDebugSettings(); void setFilename(const QString &fn); - void addOutput(const QString &msg); - void addDebugOutput(const QString &msg); void refreshOmniBar(const QStringList &flags); void addToDockWidgetList(QDockWidget *dockWidget); diff --git a/src/utils/PythonAPI.cpp b/src/utils/PythonAPI.cpp index e639a099..3c337bf9 100644 --- a/src/utils/PythonAPI.cpp +++ b/src/utils/PythonAPI.cpp @@ -62,6 +62,22 @@ PyObject *api_refresh(PyObject *self, PyObject *args) return Py_None; } +PyObject *api_message(PyObject *self, PyObject *args, PyObject *kwargs) +{ + Q_UNUSED(self); + char *message; + int debug = 0; + static const char *kwlist[] = { "", "debug", NULL }; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", + const_cast(kwlist), + &message, &debug)) { + return NULL; + } + Core()->message(QString(message), debug); + Py_INCREF(Py_None); + return Py_None; +} + PyMethodDef CutterMethods[] = { { "version", api_version, METH_NOARGS, @@ -79,6 +95,10 @@ PyMethodDef CutterMethods[] = { "refresh", api_refresh, METH_NOARGS, "Refresh Cutter widgets" }, + { + "message", (PyCFunction) api_message, METH_VARARGS | METH_KEYWORDS, + "Print message" + }, {NULL, NULL, 0, NULL} }; diff --git a/src/widgets/ConsoleWidget.h b/src/widgets/ConsoleWidget.h index 9039ddea..30668523 100644 --- a/src/widgets/ConsoleWidget.h +++ b/src/widgets/ConsoleWidget.h @@ -20,9 +20,6 @@ public: ~ConsoleWidget(); - void addOutput(const QString &msg); - void addDebugOutput(const QString &msg); - void setDebugOutputEnabled(bool enabled) { debugOutputEnabled = enabled; @@ -36,6 +33,9 @@ public: public slots: void focusInputLineEdit(); + void addOutput(const QString &msg); + void addDebugOutput(const QString &msg); + private slots: void setupFont();