mirror of https://github.com/PurpleI2P/i2pd.git
orignal
9 years ago
committed by
GitHub
11 changed files with 370 additions and 83 deletions
@ -1,2 +1,3 @@ |
|||||||
/build-i2pd_qt-Android_armeabi_v7a_GCC_4_9_Qt_5_6_0-Debug/ |
/build-i2pd_qt-Android_armeabi_v7a_GCC_4_9_Qt_5_6_0-Debug/ |
||||||
/build-i2pd_qt-Desktop_Qt_5_6_0_GCC_64bit-Debug/ |
/build-i2pd_qt-Desktop_Qt_5_6_0_GCC_64bit-Debug/ |
||||||
|
/build-i2pd_qt-Android_x86_GCC_4_9_Qt_5_6_0-Debug/ |
||||||
|
@ -1,64 +1,165 @@ |
|||||||
#include <memory> |
#include "DaemonQT.h" |
||||||
|
#include "../../Daemon.h" |
||||||
#include "mainwindow.h" |
#include "mainwindow.h" |
||||||
|
#include <QMessageBox> |
||||||
#include <QApplication> |
#include <QApplication> |
||||||
#include <stdlib.h> |
#include <QMutexLocker> |
||||||
#include "../../Daemon.h" |
#include <QThread> |
||||||
|
|
||||||
namespace i2p |
namespace i2p |
||||||
{ |
{ |
||||||
namespace util |
namespace qt |
||||||
{ |
{ |
||||||
class DaemonQTImpl: public std::enable_shared_from_this<DaemonQTImpl> |
Worker::Worker (DaemonQTImpl& daemon): |
||||||
|
m_Daemon (daemon) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
void Worker::startDaemon() |
||||||
{ |
{ |
||||||
public: |
qDebug("Performing daemon start..."); |
||||||
|
m_Daemon.start(); |
||||||
|
qDebug("Daemon started."); |
||||||
|
emit resultReady(); |
||||||
|
} |
||||||
|
void Worker::restartDaemon() |
||||||
|
{ |
||||||
|
qDebug("Performing daemon restart..."); |
||||||
|
m_Daemon.restart(); |
||||||
|
qDebug("Daemon restarted."); |
||||||
|
emit resultReady(); |
||||||
|
} |
||||||
|
void Worker::stopDaemon() { |
||||||
|
qDebug("Performing daemon stop..."); |
||||||
|
m_Daemon.stop(); |
||||||
|
qDebug("Daemon stopped."); |
||||||
|
emit resultReady(); |
||||||
|
} |
||||||
|
|
||||||
DaemonQTImpl (int argc, char* argv[]): |
Controller::Controller(DaemonQTImpl& daemon): |
||||||
m_App (argc, argv) |
m_Daemon (daemon) |
||||||
{ |
{ |
||||||
} |
Worker *worker = new Worker (m_Daemon); |
||||||
|
worker->moveToThread(&workerThread); |
||||||
|
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); |
||||||
|
connect(this, &Controller::startDaemon, worker, &Worker::startDaemon); |
||||||
|
connect(this, &Controller::stopDaemon, worker, &Worker::stopDaemon); |
||||||
|
connect(this, &Controller::restartDaemon, worker, &Worker::restartDaemon); |
||||||
|
connect(worker, &Worker::resultReady, this, &Controller::handleResults); |
||||||
|
workerThread.start(); |
||||||
|
} |
||||||
|
Controller::~Controller() |
||||||
|
{ |
||||||
|
qDebug("Closing and waiting for daemon worker thread..."); |
||||||
|
workerThread.quit(); |
||||||
|
workerThread.wait(); |
||||||
|
qDebug("Waiting for daemon worker thread finished."); |
||||||
|
if(m_Daemon.isRunning()) |
||||||
|
{ |
||||||
|
qDebug("Stopping the daemon..."); |
||||||
|
m_Daemon.stop(); |
||||||
|
qDebug("Stopped the daemon."); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
DaemonQTImpl::DaemonQTImpl (): |
||||||
|
mutex(nullptr), m_IsRunning(nullptr), m_RunningChangedCallback(nullptr) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
DaemonQTImpl::~DaemonQTImpl () |
||||||
|
{ |
||||||
|
delete mutex; |
||||||
|
} |
||||||
|
|
||||||
void Run () |
bool DaemonQTImpl::init(int argc, char* argv[]) |
||||||
{ |
{ |
||||||
MainWindow w; |
mutex=new QMutex(QMutex::Recursive); |
||||||
w.show (); |
setRunningCallback(0); |
||||||
m_App.exec(); |
m_IsRunning=false; |
||||||
} |
return Daemon.init(argc,argv); |
||||||
|
|
||||||
private: |
} |
||||||
|
void DaemonQTImpl::deinit() |
||||||
|
{ |
||||||
|
delete mutex; mutex = nullptr; |
||||||
|
} |
||||||
|
|
||||||
void StartDaemon () |
void DaemonQTImpl::start() |
||||||
{ |
{ |
||||||
Daemon.start (); |
QMutexLocker locker(mutex); |
||||||
} |
setRunning(true); |
||||||
|
Daemon.start(); |
||||||
|
} |
||||||
|
|
||||||
void StopDaemon () |
void DaemonQTImpl::stop() |
||||||
{ |
{ |
||||||
Daemon.stop (); |
QMutexLocker locker(mutex); |
||||||
} |
Daemon.stop(); |
||||||
|
setRunning(false); |
||||||
|
} |
||||||
|
|
||||||
bool IsRunning () const |
void DaemonQTImpl::restart() |
||||||
{ |
{ |
||||||
return Daemon.running; |
QMutexLocker locker(mutex); |
||||||
} |
stop(); |
||||||
|
start(); |
||||||
|
} |
||||||
|
|
||||||
private: |
void DaemonQTImpl::setRunningCallback(runningChangedCallback cb) |
||||||
|
{ |
||||||
|
m_RunningChangedCallback = cb; |
||||||
|
} |
||||||
|
|
||||||
QApplication m_App; |
bool DaemonQTImpl::isRunning() |
||||||
}; |
{ |
||||||
|
return m_IsRunning; |
||||||
|
} |
||||||
|
|
||||||
bool DaemonQT::init(int argc, char* argv[]) |
void DaemonQTImpl::setRunning(bool newValue) |
||||||
{ |
{ |
||||||
m_Impl = std::make_shared<DaemonQTImpl> (argc, argv); |
bool oldValue = m_IsRunning; |
||||||
return Daemon_Singleton::init(argc, argv); |
if(oldValue!=newValue) |
||||||
|
{ |
||||||
|
m_IsRunning = newValue; |
||||||
|
if(m_RunningChangedCallback) |
||||||
|
m_RunningChangedCallback(); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
void DaemonQT::run () |
int RunQT (int argc, char* argv[]) |
||||||
{ |
{ |
||||||
if (m_Impl) |
//int result = runGUI(argc, argv);
|
||||||
|
//QMessageBox::information(0,"Debug","runGUI completed");
|
||||||
|
QApplication app(argc, argv); |
||||||
|
DaemonQTImpl daemon; |
||||||
|
qDebug("Initialising the daemon..."); |
||||||
|
bool daemonInitSuccess = daemon.init(argc, argv); |
||||||
|
if(!daemonInitSuccess) |
||||||
|
{ |
||||||
|
QMessageBox::critical(0, "Error", "Daemon init failed"); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
qDebug("Initialised, creating the main window..."); |
||||||
|
MainWindow w; |
||||||
|
qDebug("Before main window.show()..."); |
||||||
|
w.show (); |
||||||
|
int result; |
||||||
{ |
{ |
||||||
m_Impl->Run (); |
/* i2p::qt::Controller daemonQtController(daemon);
|
||||||
m_Impl = nullptr; |
qDebug("Starting the daemon..."); |
||||||
|
emit daemonQtController.startDaemon(); |
||||||
|
qDebug("Starting gui event loop...");*/ |
||||||
|
daemon.start (); |
||||||
|
result = app.exec(); |
||||||
|
daemon.stop (); |
||||||
} |
} |
||||||
|
daemon.deinit(); |
||||||
|
//QMessageBox::information(&w, "Debug", "demon stopped");
|
||||||
|
//exit(result); //return from main() causes intermittent sigsegv bugs in some Androids. exit() is a workaround for this
|
||||||
|
qDebug("Exiting the application"); |
||||||
|
return result; |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
@ -0,0 +1,82 @@ |
|||||||
|
#ifndef DAEMONQT_H |
||||||
|
#define DAEMONQT_H |
||||||
|
|
||||||
|
#include <QObject> |
||||||
|
#include <QThread> |
||||||
|
#include <QMutex> |
||||||
|
|
||||||
|
namespace i2p |
||||||
|
{ |
||||||
|
namespace qt |
||||||
|
{ |
||||||
|
class DaemonQTImpl |
||||||
|
{ |
||||||
|
public: |
||||||
|
|
||||||
|
DaemonQTImpl (); |
||||||
|
~DaemonQTImpl (); |
||||||
|
|
||||||
|
typedef void (*runningChangedCallback)(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief init |
||||||
|
* @param argc |
||||||
|
* @param argv |
||||||
|
* @return success |
||||||
|
*/ |
||||||
|
bool init(int argc, char* argv[]); |
||||||
|
void deinit(); |
||||||
|
void start(); |
||||||
|
void stop(); |
||||||
|
void restart(); |
||||||
|
void setRunningCallback(runningChangedCallback cb); |
||||||
|
bool isRunning(); |
||||||
|
private: |
||||||
|
void setRunning(bool running); |
||||||
|
private: |
||||||
|
QMutex* mutex; |
||||||
|
bool m_IsRunning; |
||||||
|
runningChangedCallback m_RunningChangedCallback; |
||||||
|
}; |
||||||
|
|
||||||
|
class Worker : public QObject |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
|
||||||
|
Worker (DaemonQTImpl& daemon); |
||||||
|
|
||||||
|
private: |
||||||
|
|
||||||
|
DaemonQTImpl& m_Daemon; |
||||||
|
|
||||||
|
public slots: |
||||||
|
void startDaemon(); |
||||||
|
void restartDaemon(); |
||||||
|
void stopDaemon(); |
||||||
|
|
||||||
|
signals: |
||||||
|
void resultReady(); |
||||||
|
}; |
||||||
|
|
||||||
|
class Controller : public QObject |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
QThread workerThread; |
||||||
|
public: |
||||||
|
Controller(DaemonQTImpl& daemon); |
||||||
|
~Controller(); |
||||||
|
private: |
||||||
|
DaemonQTImpl& m_Daemon; |
||||||
|
|
||||||
|
public slots: |
||||||
|
void handleResults(){} |
||||||
|
signals: |
||||||
|
void startDaemon(); |
||||||
|
void stopDaemon(); |
||||||
|
void restartDaemon(); |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#endif // DAEMONQT_H
|
@ -1,14 +1,55 @@ |
|||||||
#include "mainwindow.h" |
#include "mainwindow.h" |
||||||
#include "ui_mainwindow.h" |
//#include "ui_mainwindow.h"
|
||||||
|
#include <QMessageBox> |
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) : |
MainWindow::MainWindow(QWidget *parent) : |
||||||
QMainWindow(parent), |
QMainWindow(parent)/*,
|
||||||
ui(new Ui::MainWindow) |
ui(new Ui::MainWindow)*/ |
||||||
{ |
{ |
||||||
ui->setupUi(this); |
//ui->setupUi(this);
|
||||||
|
if (objectName().isEmpty()) |
||||||
|
setObjectName(QStringLiteral("MainWindow")); |
||||||
|
resize(800, 480); |
||||||
|
centralWidget = new QWidget(this); |
||||||
|
centralWidget->setObjectName(QStringLiteral("centralWidget")); |
||||||
|
verticalLayoutWidget = new QWidget(centralWidget); |
||||||
|
verticalLayoutWidget->setObjectName(QStringLiteral("verticalLayoutWidget")); |
||||||
|
//verticalLayoutWidget->setGeometry(QRect(10, 20, 771, 441));
|
||||||
|
verticalLayout1 = new QVBoxLayout(verticalLayoutWidget); |
||||||
|
verticalLayout1->setSpacing(6); |
||||||
|
verticalLayout1->setContentsMargins(11, 11, 11, 11); |
||||||
|
verticalLayout1->setObjectName(QStringLiteral("verticalLayout1")); |
||||||
|
verticalLayout1->setContentsMargins(0, 0, 0, 0); |
||||||
|
quitButton = new QPushButton(verticalLayoutWidget); |
||||||
|
quitButton->setObjectName(QStringLiteral("quitButton")); |
||||||
|
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); |
||||||
|
sizePolicy.setHorizontalStretch(0); |
||||||
|
sizePolicy.setVerticalStretch(0); |
||||||
|
sizePolicy.setHeightForWidth(quitButton->sizePolicy().hasHeightForWidth()); |
||||||
|
quitButton->setSizePolicy(sizePolicy); |
||||||
|
|
||||||
|
verticalLayout1->addWidget(quitButton); |
||||||
|
|
||||||
|
setCentralWidget(centralWidget); |
||||||
|
|
||||||
|
setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); |
||||||
|
quitButton->setText(QApplication::translate("MainWindow", "Quit", 0)); |
||||||
|
|
||||||
|
QObject::connect(quitButton, SIGNAL(released()), this, SLOT(handleQuitButton())); |
||||||
|
|
||||||
|
//QMetaObject::connectSlotsByName(this);
|
||||||
|
} |
||||||
|
|
||||||
|
void MainWindow::handleQuitButton() { |
||||||
|
qDebug("Quit pressed. Hiding the main window"); |
||||||
|
close(); |
||||||
|
QApplication::instance()->quit(); |
||||||
} |
} |
||||||
|
|
||||||
MainWindow::~MainWindow() |
MainWindow::~MainWindow() |
||||||
{ |
{ |
||||||
delete ui; |
qDebug("Destroying main window"); |
||||||
|
//QMessageBox::information(0, "Debug", "mw destructor 1");
|
||||||
|
//delete ui;
|
||||||
|
//QMessageBox::information(0, "Debug", "mw destructor 2");
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue