- Implemented autostart

This commit is contained in:
Dorian Zedler 2020-11-24 21:42:47 +01:00
parent 06dd8c4be3
commit 2535dcb282
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
9 changed files with 134 additions and 29 deletions

View file

@ -46,7 +46,7 @@ class LinboBackend : public QObject
Q_PROPERTY(LinboBackend::LinboState state READ getState NOTIFY stateChanged) Q_PROPERTY(LinboBackend::LinboState state READ getState NOTIFY stateChanged)
Q_PROPERTY(LinboOs* currentOs READ getCurrentOs WRITE setCurrentOs NOTIFY currentOsChanged) Q_PROPERTY(LinboOs* currentOs READ getCurrentOs WRITE setCurrentOs NOTIFY currentOsChanged)
Q_PROPERTY(double autostartTimeoutProgress READ getAutostartTimeoutProgress NOTIFY autostartTimeoutProgressChanged) Q_PROPERTY(double autostartTimeoutProgress READ getAutostartTimeoutProgress NOTIFY autostartTimeoutProgressChanged)
Q_PROPERTY(int autostartTimeoutRemainingSeconds READ getAutostartTimeoutRemainingSeconds NOTIFY autostartTimeoutProgressChanged) //Q_PROPERTY(int autostartTimeoutRemainingSeconds READ getAutostartTimeoutRemainingSeconds NOTIFY autostartTimeoutProgressChanged)
public: public:
explicit LinboBackend(QObject *parent = nullptr); explicit LinboBackend(QObject *parent = nullptr);
@ -147,6 +147,7 @@ private slots:
signals: signals:
void stateChanged(LinboBackend::LinboState state); void stateChanged(LinboBackend::LinboState state);
void currentOsChanged(LinboOs* os); void currentOsChanged(LinboOs* os);
void autostartTimeoutProgressChanged();
}; };

View file

@ -29,7 +29,7 @@ public:
const LinboLog& getLatestLog(); const LinboLog& getLatestLog();
QString logTypeToString(LinboLogType logType); static QString logTypeToString(LinboLogType logType);
private: private:

View file

@ -65,7 +65,7 @@ public:
} }
} }
LinboOsStartAction startActionFromString(const QString& name) const { static LinboOsStartAction startActionFromString(const QString& name) {
if(name == "start") if(name == "start")
return StartOs; return StartOs;
else if(name == "sync") else if(name == "sync")

View file

@ -47,6 +47,7 @@ private slots:
void handleCurrentOsChanged(LinboOs* newOs); void handleCurrentOsChanged(LinboOs* newOs);
void handleLinboStateChanged(LinboBackend::LinboState newState); void handleLinboStateChanged(LinboBackend::LinboState newState);
void handleLatestLogChanged(const LinboLogger::LinboLog& latestLog); void handleLatestLogChanged(const LinboLogger::LinboLog& latestLog);
void handleAutostartTimeoutProgressChanged();
signals: signals:
void selectedOsChanged(); void selectedOsChanged();

View file

@ -17,6 +17,7 @@ public:
void setIndeterminate(bool indeterminate); void setIndeterminate(bool indeterminate);
bool getIndeterminate(); bool getIndeterminate();
void setReversed(bool reversed);
protected: protected:
QTimer* refreshTimer; QTimer* refreshTimer;
@ -26,7 +27,10 @@ protected:
private: private:
bool indeterminate; bool indeterminate;
bool reversed;
int preIndeterminateValue; int preIndeterminateValue;
int preIndeterminateMinimum;
int preIndeterminateMaximum;
protected slots: protected slots:
void updateIndeterminate(); void updateIndeterminate();

View file

@ -27,11 +27,13 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent)
this->setState(Initializing); this->setState(Initializing);
this->logger = new LinboLogger(this); this->logger = new LinboLogger(this);
this->config = new LinboConfig(this); this->config = new LinboConfig(this);
this->autostartTimer = new QTimer(this); this->autostartTimer = new QTimer();
this->autostartTimer->setSingleShot(true); this->autostartTimer->setSingleShot(true);
this->autostartRemainingTimeRefreshTimer = new QTimer(this); connect(this->autostartTimer, SIGNAL(timeout()), this, SLOT(handleAutostartTimerTimeout()));
this->autostartRemainingTimeRefreshTimer->setSingleShot(true); this->autostartRemainingTimeRefreshTimer = new QTimer();
this->autostartRemainingTimeRefreshTimer->setSingleShot(false);
this->autostartRemainingTimeRefreshTimer->setInterval(10); this->autostartRemainingTimeRefreshTimer->setInterval(10);
connect(this->autostartRemainingTimeRefreshTimer, SIGNAL(timeout()), this, SIGNAL(autostartTimeoutProgressChanged()));
this->autostartElapsedTimer = new QElapsedTimer(); this->autostartElapsedTimer = new QElapsedTimer();
this->currentOs = nullptr; this->currentOs = nullptr;
@ -152,12 +154,43 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent)
// -------------------- // --------------------
void LinboBackend::executeAutostart() { void LinboBackend::executeAutostart() {
this->autostartTimer->setInterval(this->currentOs->getAutostartTimeout() * 1000); if(this->currentOs->getDefaultAction() == LinboOs::UnknownAction) {
this->autostartElapsedTimer->restart(); this->setState(Idle);
return;
} }
void handleAutostartTimerTimeout() { this->setState(Autostarting);
this->logger->log("Beginning autostart timeout for " + this->currentOs->getName(), LinboLogger::LinboGuiInfo);
this->autostartTimer->setInterval(this->currentOs->getAutostartTimeout() * 1000);
this->autostartTimer->start();
this->autostartElapsedTimer->restart();
this->autostartRemainingTimeRefreshTimer->start();
}
void LinboBackend::handleAutostartTimerTimeout() {
this->autostartElapsedTimer->invalidate();
this->autostartRemainingTimeRefreshTimer->stop();
this->logger->log("Executing autostart for " + this->currentOs->getName(), LinboLogger::LinboGuiInfo);
LinboOs::LinboOsStartAction defaultAction = this->currentOs->getDefaultAction();
bool wasSuccessfull = false;
switch (defaultAction) {
case LinboOs::StartOs:
wasSuccessfull = this->startCurrentOs();
break;
case LinboOs::SyncOs:
wasSuccessfull = this->syncCurrentOs();
break;
case LinboOs::ReinstallOs:
wasSuccessfull = this->reinstallCurrentOs();
break;
default: break;
}
if(wasSuccessfull)
this->logger->log("Executed autostart successfully!", LinboLogger::LinboGuiInfo);
else
this->logger->log("An error occured when executing autostart for " + this->currentOs->getName(), LinboLogger::LinboGuiError);
} }
void LinboBackend::shutdown() { void LinboBackend::shutdown() {
@ -171,13 +204,11 @@ void LinboBackend::reboot() {
bool LinboBackend::startCurrentOs() { bool LinboBackend::startCurrentOs() {
LinboOs* os = this->currentOs; LinboOs* os = this->currentOs;
if(os == nullptr || this->state != Idle || !this->currentOs->getStartbutton()) if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getStartbutton())
return false; return false;
this->setState(Starting); this->setState(Starting);
return true;
this->executeCommand( this->executeCommand(
false, false,
"start", "start",
@ -195,7 +226,7 @@ bool LinboBackend::startCurrentOs() {
bool LinboBackend::syncCurrentOs() { bool LinboBackend::syncCurrentOs() {
LinboOs* os = this->currentOs; LinboOs* os = this->currentOs;
if(os == nullptr || this->state != Idle || !this->currentOs->getSyncbutton()) if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getSyncbutton())
return false; return false;
this->setState(Syncing); this->setState(Syncing);
@ -220,7 +251,7 @@ bool LinboBackend::syncCurrentOs() {
bool LinboBackend::reinstallCurrentOs() { bool LinboBackend::reinstallCurrentOs() {
LinboOs* os = this->currentOs; LinboOs* os = this->currentOs;
if(os == nullptr || this->state != Idle || !this->currentOs->getNewbutton()) if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getNewbutton())
return false; return false;
this->setState(Reinstalling); this->setState(Reinstalling);
@ -329,11 +360,11 @@ void LinboBackend::setCurrentOs(LinboOs* os) {
double LinboBackend::getAutostartTimeoutProgress() { double LinboBackend::getAutostartTimeoutProgress() {
return this->autostartElapsedTimer->elapsed() / this->autostartTimer->interval(); return this->autostartTimer->isActive() ? double(this->autostartElapsedTimer->elapsed()) / double(this->autostartTimer->interval()) : 1;
} }
int LinboBackend::getAutostartTimeoutRemainingSeconds() { int LinboBackend::getAutostartTimeoutRemainingSeconds() {
return (this->autostartTimer->interval() - this->autostartElapsedTimer->elapsed()) / 1000; return this->autostartTimer->isActive() ? (this->autostartTimer->interval() - this->autostartElapsedTimer->elapsed()) / 1000 : 0;
} }
// ----------- // -----------
@ -348,10 +379,10 @@ QString LinboBackend::executeCommand(bool waitForFinished, QString command, QStr
// clear old output // clear old output
this->synchronosProcess->readAll(); this->synchronosProcess->readAll();
synchronosProcess->start(command, commandArgs); this->synchronosProcess->start(command, commandArgs);
synchronosProcess->waitForStarted(); this->synchronosProcess->waitForStarted();
while( !synchronosProcess->waitForFinished(10000) ) {} this->synchronosProcess->waitForFinished(10000);
return this->synchronosProcess->readAllStandardOutput(); return this->synchronosProcess->readAllStandardOutput();
} }
else { else {

View file

@ -5,6 +5,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
this->backend = backend; this->backend = backend;
connect(this->backend, SIGNAL(currentOsChanged(LinboOs*)), this, SLOT(handleCurrentOsChanged(LinboOs*))); connect(this->backend, SIGNAL(currentOsChanged(LinboOs*)), this, SLOT(handleCurrentOsChanged(LinboOs*)));
connect(this->backend, SIGNAL(stateChanged(LinboBackend::LinboState)), this, SLOT(handleLinboStateChanged(LinboBackend::LinboState))); connect(this->backend, SIGNAL(stateChanged(LinboBackend::LinboState)), this, SLOT(handleLinboStateChanged(LinboBackend::LinboState)));
connect(this->backend, SIGNAL(autostartTimeoutProgressChanged()), this, SLOT(handleAutostartTimeoutProgressChanged()));
connect(this->backend->getLogger(), SIGNAL(latestLogChanged(const LinboLogger::LinboLog&)), this, SLOT(handleLatestLogChanged(const LinboLogger::LinboLog&))); connect(this->backend->getLogger(), SIGNAL(latestLogChanged(const LinboLogger::LinboLog&)), this, SLOT(handleLatestLogChanged(const LinboLogger::LinboLog&)));
this->stackView = new QModernStackedWidget(this); this->stackView = new QModernStackedWidget(this);
@ -28,6 +29,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
// Progress bar // Progress bar
this->progressBarWidget = new QWidget(); this->progressBarWidget = new QWidget();
this->progressBar = new QModernProgressBar(this->progressBarWidget); this->progressBar = new QModernProgressBar(this->progressBarWidget);
this->progressBar->setRange(0,1000);
this->progressBar->setIndeterminate(true); this->progressBar->setIndeterminate(true);
this->logLabel = new QLabel("", this->progressBarWidget); this->logLabel = new QLabel("", this->progressBarWidget);
@ -37,9 +39,10 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
this->logLabel->setFont(this->logFont); this->logLabel->setFont(this->logFont);
this->stackView->addWidget(this->progressBarWidget); this->stackView->addWidget(this->progressBarWidget);
this->stackView->setCurrentWidget(nullptr);
connect(this->stackView, SIGNAL(currentChanged(int)), this, SLOT(resizeAndPositionAllItems())); connect(this->stackView, SIGNAL(currentChanged(int)), this, SLOT(resizeAndPositionAllItems()));
this->stackView->setCurrentWidget(this->buttonWidget); this->handleLinboStateChanged(this->backend->getState());
} }
void LinboStartActions::resizeAndPositionAllItems() { void LinboStartActions::resizeAndPositionAllItems() {
@ -172,20 +175,37 @@ void LinboStartActions::handleCurrentOsChanged(LinboOs* newOs) {
} }
void LinboStartActions::handleLinboStateChanged(LinboBackend::LinboState newState) { void LinboStartActions::handleLinboStateChanged(LinboBackend::LinboState newState) {
QWidget* currentWidget = nullptr;
switch (newState) { switch (newState) {
case LinboBackend::Autostarting:
qDebug() << "Linbo state is: autostart";
this->progressBar->setIndeterminate(false);
this->progressBar->setReversed(true);
this->progressBar->setValue(0);
currentWidget = this->progressBarWidget;
break;
case LinboBackend::Idle: case LinboBackend::Idle:
this->stackView->setCurrentWidgetAnimated(this->buttonWidget); currentWidget = this->buttonWidget;
break; break;
case LinboBackend::Starting: case LinboBackend::Starting:
case LinboBackend::Syncing: case LinboBackend::Syncing:
case LinboBackend::Reinstalling: case LinboBackend::Reinstalling:
this->stackView->setCurrentWidgetAnimated(this->progressBarWidget); this->progressBar->setIndeterminate(true);
this->progressBar->setReversed(false);
currentWidget = this->progressBarWidget;
break; break;
default: default:
break; break;
} }
if(this->inited)
this->stackView->setCurrentWidgetAnimated(currentWidget);
else
this->stackView->setCurrentWidget(currentWidget);
} }
@ -204,3 +224,25 @@ void LinboStartActions::handleLatestLogChanged(const LinboLogger::LinboLog& late
this->logLabel->setStyleSheet("QLabel { color : " + logColor + "; }"); this->logLabel->setStyleSheet("QLabel { color : " + logColor + "; }");
this->logLabel->setText(latestLog.message); this->logLabel->setText(latestLog.message);
} }
void LinboStartActions::handleAutostartTimeoutProgressChanged() {
if(this->backend->getState() != LinboBackend::Autostarting)
return;
this->progressBar->setValue(1000 - this->backend->getAutostartTimeoutProgress() * 1000);
QString actionString;
switch (this->backend->getCurrentOs()->getDefaultAction()) {
case LinboOs::SyncOs:
actionString = "Syncing and starting";
break;
case LinboOs::ReinstallOs:
actionString = "Reinstalling and starting";
break;
default:
actionString = "Starting";
break;
}
this->logLabel->setText(actionString + " " + this->backend->getCurrentOs()->getName() + " in " + QString::number(this->backend->getAutostartTimeoutRemainingSeconds()) + " seconds.");
}

View file

@ -26,6 +26,8 @@ void QModernProgressBar::setIndeterminate(bool indeterminate) {
if(this->indeterminate) { if(this->indeterminate) {
this->preIndeterminateValue = this->value(); this->preIndeterminateValue = this->value();
this->preIndeterminateMinimum = this->minimum();
this->preIndeterminateMaximum = this->maximum();
// finer steps, so the Animation is fluid // finer steps, so the Animation is fluid
this->setMinimum(0); this->setMinimum(0);
this->setMaximum(1000); this->setMaximum(1000);
@ -33,13 +35,21 @@ void QModernProgressBar::setIndeterminate(bool indeterminate) {
} }
else { else {
// reset minimum and maximum // reset minimum and maximum
this->setMinimum(0); this->setMinimum(this->preIndeterminateMinimum);
this->setMaximum(100); this->setMaximum(this->preIndeterminateMaximum);
this->setValue(this->preIndeterminateValue); this->setValue(this->preIndeterminateValue);
this->indeterminateAnimtion->stop(); this->indeterminateAnimtion->stop();
} }
} }
void QModernProgressBar::setReversed(bool reversed) {
if(this->reversed == reversed)
return;
this->reversed = reversed;
this->update();
}
bool QModernProgressBar::getIndeterminate() { bool QModernProgressBar::getIndeterminate() {
return this->indeterminate; return this->indeterminate;
} }
@ -54,18 +64,34 @@ void QModernProgressBar::paintEvent(QPaintEvent *e) {
// background // background
painter.fillRect(e->rect(), QColor("#c7c7c7")); painter.fillRect(e->rect(), QColor("#c7c7c7"));
double from = 0;
double to = 0;
// progress // progress
if(this->indeterminate) { if(this->indeterminate) {
int maximum = this->maximum() / 2; int maximum = this->maximum() / 2;
if(this->value() <= maximum) if(this->value() <= maximum)
// for the first half -> fill from left // for the first half -> fill from left
painter.fillRect(QRect(0,0, e->rect().width() * double(double(this->value()) / double(maximum)), e->rect().height()), QColor(0,0,0)); to = double(double(this->value()) / double(maximum));
else else {
// for the second half -> empty from right // for the second half -> empty from right
painter.fillRect(QRect(e->rect().width() * double(double(this->value()- (maximum)) / double(maximum)),0, e->rect().width(), e->rect().height()), QColor(0,0,0)); from = double(double(this->value()- (maximum)) / double(maximum));
to = 1;
} }
else }
painter.fillRect(QRect(0,0, e->rect().width() * float(float(this->value()) / float(this->maximum())), e->rect().height()), QColor(0,0,0)); else {
to = double(double(this->value()) / double(this->maximum()));
}
if(this->reversed) {
// if reversed -> reverse and swap from and to
double tmp = 1 - from;
from = 1 - to;
to = tmp;
}
painter.fillRect(QRect(e->rect().width() * from, 0, e->rect().width() * to, e->rect().height()), QColor(0,0,0));
painter.end(); painter.end();

View file

@ -11,7 +11,7 @@ QModernStackedWidget::QModernStackedWidget(QWidget* parent) : QStackedWidget(par
} }
void QModernStackedWidget::setCurrentWidgetAnimated(QWidget* widget) { void QModernStackedWidget::setCurrentWidgetAnimated(QWidget* widget) {
if(widget == nullptr) if(widget == nullptr || widget == this->currentWidget())
return; return;
this->newWidget = widget; this->newWidget = widget;