- 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(LinboOs* currentOs READ getCurrentOs WRITE setCurrentOs NOTIFY currentOsChanged)
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:
explicit LinboBackend(QObject *parent = nullptr);
@ -147,6 +147,7 @@ private slots:
signals:
void stateChanged(LinboBackend::LinboState state);
void currentOsChanged(LinboOs* os);
void autostartTimeoutProgressChanged();
};

View File

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

View File

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

View File

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

View File

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

View File

@ -27,11 +27,13 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent)
this->setState(Initializing);
this->logger = new LinboLogger(this);
this->config = new LinboConfig(this);
this->autostartTimer = new QTimer(this);
this->autostartTimer = new QTimer();
this->autostartTimer->setSingleShot(true);
this->autostartRemainingTimeRefreshTimer = new QTimer(this);
this->autostartRemainingTimeRefreshTimer->setSingleShot(true);
connect(this->autostartTimer, SIGNAL(timeout()), this, SLOT(handleAutostartTimerTimeout()));
this->autostartRemainingTimeRefreshTimer = new QTimer();
this->autostartRemainingTimeRefreshTimer->setSingleShot(false);
this->autostartRemainingTimeRefreshTimer->setInterval(10);
connect(this->autostartRemainingTimeRefreshTimer, SIGNAL(timeout()), this, SIGNAL(autostartTimeoutProgressChanged()));
this->autostartElapsedTimer = new QElapsedTimer();
this->currentOs = nullptr;
@ -127,7 +129,7 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent)
this->config->setCacheSize(this->executeCommand(true, "size"));
// Harddisk Size
QRegExp *removePartition = new QRegExp("[0-9]{1,2}") ;
QRegExp *removePartition = new QRegExp("[0-9]{1,2}");
QString hd = this->config->getCache();
// e.g. turn /dev/sda1 into /dev/sda
hd.remove( *removePartition );
@ -152,12 +154,43 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent)
// --------------------
void LinboBackend::executeAutostart() {
if(this->currentOs->getDefaultAction() == LinboOs::UnknownAction) {
this->setState(Idle);
return;
}
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 handleAutostartTimerTimeout() {
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() {
@ -171,13 +204,11 @@ void LinboBackend::reboot() {
bool LinboBackend::startCurrentOs() {
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;
this->setState(Starting);
return true;
this->executeCommand(
false,
"start",
@ -195,7 +226,7 @@ bool LinboBackend::startCurrentOs() {
bool LinboBackend::syncCurrentOs() {
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;
this->setState(Syncing);
@ -220,7 +251,7 @@ bool LinboBackend::syncCurrentOs() {
bool LinboBackend::reinstallCurrentOs() {
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;
this->setState(Reinstalling);
@ -329,11 +360,11 @@ void LinboBackend::setCurrentOs(LinboOs* os) {
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() {
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
this->synchronosProcess->readAll();
synchronosProcess->start(command, commandArgs);
synchronosProcess->waitForStarted();
this->synchronosProcess->start(command, commandArgs);
this->synchronosProcess->waitForStarted();
while( !synchronosProcess->waitForFinished(10000) ) {}
this->synchronosProcess->waitForFinished(10000);
return this->synchronosProcess->readAllStandardOutput();
}
else {

View File

@ -5,6 +5,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
this->backend = backend;
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(autostartTimeoutProgressChanged()), this, SLOT(handleAutostartTimeoutProgressChanged()));
connect(this->backend->getLogger(), SIGNAL(latestLogChanged(const LinboLogger::LinboLog&)), this, SLOT(handleLatestLogChanged(const LinboLogger::LinboLog&)));
this->stackView = new QModernStackedWidget(this);
@ -28,6 +29,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
// Progress bar
this->progressBarWidget = new QWidget();
this->progressBar = new QModernProgressBar(this->progressBarWidget);
this->progressBar->setRange(0,1000);
this->progressBar->setIndeterminate(true);
this->logLabel = new QLabel("", this->progressBarWidget);
@ -37,9 +39,10 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q
this->logLabel->setFont(this->logFont);
this->stackView->addWidget(this->progressBarWidget);
this->stackView->setCurrentWidget(nullptr);
connect(this->stackView, SIGNAL(currentChanged(int)), this, SLOT(resizeAndPositionAllItems()));
this->stackView->setCurrentWidget(this->buttonWidget);
this->handleLinboStateChanged(this->backend->getState());
}
void LinboStartActions::resizeAndPositionAllItems() {
@ -172,20 +175,37 @@ void LinboStartActions::handleCurrentOsChanged(LinboOs* newOs) {
}
void LinboStartActions::handleLinboStateChanged(LinboBackend::LinboState newState) {
QWidget* currentWidget = nullptr;
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:
this->stackView->setCurrentWidgetAnimated(this->buttonWidget);
currentWidget = this->buttonWidget;
break;
case LinboBackend::Starting:
case LinboBackend::Syncing:
case LinboBackend::Reinstalling:
this->stackView->setCurrentWidgetAnimated(this->progressBarWidget);
this->progressBar->setIndeterminate(true);
this->progressBar->setReversed(false);
currentWidget = this->progressBarWidget;
break;
default:
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->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) {
this->preIndeterminateValue = this->value();
this->preIndeterminateMinimum = this->minimum();
this->preIndeterminateMaximum = this->maximum();
// finer steps, so the Animation is fluid
this->setMinimum(0);
this->setMaximum(1000);
@ -33,13 +35,21 @@ void QModernProgressBar::setIndeterminate(bool indeterminate) {
}
else {
// reset minimum and maximum
this->setMinimum(0);
this->setMaximum(100);
this->setMinimum(this->preIndeterminateMinimum);
this->setMaximum(this->preIndeterminateMaximum);
this->setValue(this->preIndeterminateValue);
this->indeterminateAnimtion->stop();
}
}
void QModernProgressBar::setReversed(bool reversed) {
if(this->reversed == reversed)
return;
this->reversed = reversed;
this->update();
}
bool QModernProgressBar::getIndeterminate() {
return this->indeterminate;
}
@ -54,18 +64,34 @@ void QModernProgressBar::paintEvent(QPaintEvent *e) {
// background
painter.fillRect(e->rect(), QColor("#c7c7c7"));
double from = 0;
double to = 0;
// progress
if(this->indeterminate) {
int maximum = this->maximum() / 2;
if(this->value() <= maximum)
// 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));
else
to = double(double(this->value()) / double(maximum));
else {
// 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();

View File

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