You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
6.3 KiB
234 lines
6.3 KiB
// SPDX-License-Identifier: BSD-3-Clause
|
|
// Copyright (c) 2021, The Monero Project.
|
|
|
|
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "Configurator.h"
|
|
#include "Config.h"
|
|
|
|
#include <QMessageBox>
|
|
#include <QIcon>
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
this->setWindowIcon(QIcon(":/assets/monero_grey.png"));
|
|
|
|
// Setup tray
|
|
m_trayIcon = new QSystemTrayIcon(QIcon(":/assets/monero_grey.png"));
|
|
m_trayIcon->show();
|
|
m_showWindow = m_trayMenu.addAction("Show window");
|
|
m_trayIcon->setContextMenu(&m_trayMenu);
|
|
|
|
// Daemon manager
|
|
daemonManager = new DaemonManager(this);
|
|
|
|
QString monerodPath = config()->get(Config::pathToMonerod).toString();
|
|
if (monerodPath.isEmpty()) {
|
|
monerodPath = this->defaultMonerodPath();
|
|
}
|
|
config()->set(Config::pathToMonerod, monerodPath);
|
|
daemonManager->setMonerodPath(monerodPath);
|
|
connect(daemonManager, &DaemonManager::daemonConsoleUpdated, this, &MainWindow::writeLogLine);
|
|
|
|
connect(m_showWindow, &QAction::triggered, [this]{
|
|
this->show();
|
|
this->raise();
|
|
this->setFocus();
|
|
});
|
|
|
|
connect(ui->btn_Configure, &QPushButton::clicked, this, &MainWindow::showConfigurator);
|
|
connect(ui->commandPrompt, &CommandLineEdit::commandExecuted, this, &MainWindow::sendCommand);
|
|
|
|
this->setStatus("not running");
|
|
connect(ui->btn_ToggleDaemon, &QPushButton::pressed, [this]{
|
|
if (daemonManager->isRunning()) {
|
|
this->setStatus("stopping");
|
|
daemonManager->stop();
|
|
} else {
|
|
QStringList flags = this->getStartupFlags();
|
|
this->setStatus("starting");
|
|
ui->btn_ToggleDaemon->setEnabled(false);
|
|
daemonManager->start(flags, this->getPort());
|
|
}
|
|
});
|
|
connect(daemonManager, &DaemonManager::daemonStarted, [this]{
|
|
this->setStatus("running");
|
|
ui->btn_ToggleDaemon->setEnabled(true);
|
|
ui->btn_ToggleDaemon->setText("Stop daemon");
|
|
});
|
|
connect(daemonManager, &DaemonManager::daemonStopped, [this]{
|
|
this->setStatus("stopped");
|
|
ui->btn_ToggleDaemon->setText("Start daemon");
|
|
});
|
|
|
|
// qApp->setStyleSheet(this->loadStylesheet(":/qdarkstyle/style.qss"));
|
|
|
|
this->adjustSize();
|
|
// showMinimized();
|
|
}
|
|
|
|
void MainWindow::writeLogLine(const QString &line) {
|
|
auto l = line.trimmed();
|
|
if (!l.isEmpty()) {
|
|
ui->logs->append(l);
|
|
}
|
|
}
|
|
|
|
void MainWindow::changeEvent(QEvent* e)
|
|
{
|
|
switch (e->type())
|
|
{
|
|
case QEvent::WindowStateChange:
|
|
{
|
|
if (this->windowState() & Qt::WindowMinimized)
|
|
{
|
|
QTimer::singleShot(250, this, SLOT(hide()));
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
QMainWindow::changeEvent(e);
|
|
}
|
|
|
|
void MainWindow::sendCommand(const QStringList &command) {
|
|
daemonManager->sendCommandAsync(command, [](const QVariantList& l){
|
|
bool success = l[0].toBool();
|
|
QString message = l[1].toString();
|
|
|
|
qDebug() << QString("Command executed: %1, with message: %2").arg(success ? "Success" : "Fail", message);
|
|
});
|
|
}
|
|
|
|
void MainWindow::showConfigurator() {
|
|
auto *dialog = new Configurator(this);
|
|
dialog->exec();
|
|
if (dialog->configUpdated()) {
|
|
this->askRestartMonerod();
|
|
}
|
|
dialog->deleteLater();
|
|
}
|
|
|
|
void MainWindow::askRestartMonerod() {
|
|
QMessageBox::information(this, "Config Updated", "The config has been altered, do you want to restart monerod now?");
|
|
}
|
|
|
|
void MainWindow::setStatus(const QString &status) {
|
|
ui->labelStatus->setText(QString("Daemon status: %1").arg(status));
|
|
}
|
|
|
|
int MainWindow::getPort() {
|
|
int RPCBindPort = config()->get(Config::RPCBindPort).toInt();
|
|
if (RPCBindPort) {
|
|
return RPCBindPort;
|
|
}
|
|
|
|
switch (config()->get(Config::network).toInt()) {
|
|
case 0:
|
|
return 18081;
|
|
case 1:
|
|
return 38081;
|
|
case 2:
|
|
return 28081;
|
|
}
|
|
return 18081;
|
|
}
|
|
|
|
QStringList MainWindow::getStartupFlags() {
|
|
QStringList startupFlags;
|
|
|
|
if (config()->get(Config::pruneBlockchain).toBool()) {
|
|
startupFlags << "--prune-blockchain";
|
|
}
|
|
|
|
QString dataDir = config()->get(Config::dataDir).toString();
|
|
if (!dataDir.isEmpty()) {
|
|
// Todo: check if dir exists
|
|
startupFlags << "--data-dir" << dataDir;
|
|
}
|
|
|
|
int limitRateDown = config()->get(Config::limitRateDown).toInt();
|
|
if (limitRateDown > 0) {
|
|
startupFlags << "--limit-rate-down" << QString::number(limitRateDown);
|
|
}
|
|
|
|
switch (config()->get(Config::network).toInt()) {
|
|
case 1:
|
|
startupFlags << "--stagenet";
|
|
break;
|
|
case 2:
|
|
startupFlags << "--testnet";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (config()->get(Config::noZeroMQ).toBool()) {
|
|
startupFlags << "--no-zmq";
|
|
}
|
|
|
|
QString RPCBindIP = config()->get(Config::RPCBindIP).toString();
|
|
if (!RPCBindIP.isEmpty()) {
|
|
startupFlags << "--rpc-bind-ip" << RPCBindIP;
|
|
}
|
|
int RPCBindPort = config()->get(Config::RPCBindPort).toInt();
|
|
if (RPCBindPort > 0) {
|
|
startupFlags << "--rpc-bind-port" << QString::number(RPCBindPort);
|
|
}
|
|
|
|
QString P2PBindIP = config()->get(Config::P2PBindIP).toString();
|
|
if (!RPCBindIP.isEmpty()) {
|
|
startupFlags << "--p2p-bind-ip" << P2PBindIP;
|
|
}
|
|
int P2PBindPort = config()->get(Config::P2PBindPort).toInt();
|
|
if (RPCBindPort > 0) {
|
|
startupFlags << "--p2p-bind-port" << QString::number(P2PBindPort);
|
|
}
|
|
|
|
return startupFlags;
|
|
}
|
|
|
|
QString MainWindow::defaultMonerodPath() {
|
|
QString monerod;
|
|
|
|
// Platform dependent path to monerod
|
|
#ifdef Q_OS_WIN
|
|
monerod = QApplication::applicationDirPath() + "/monerod.exe";
|
|
#elif defined(Q_OS_UNIX)
|
|
monerod = QApplication::applicationDirPath() + "/monerod";
|
|
#endif
|
|
|
|
if (monerod.length() == 0) {
|
|
qCritical() << "no daemon binary defined for current platform";
|
|
}
|
|
|
|
return monerod;
|
|
}
|
|
|
|
QString MainWindow::loadStylesheet(const QString &resource) {
|
|
QFile f(resource);
|
|
if (!f.exists()) {
|
|
printf("Unable to set stylesheet, file not found\n");
|
|
f.close();
|
|
return "";
|
|
}
|
|
|
|
f.open(QFile::ReadOnly | QFile::Text);
|
|
QTextStream ts(&f);
|
|
QString data = ts.readAll();
|
|
f.close();
|
|
|
|
return data;
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|