Added auto update checker

This commit is contained in:
Gres 2020-03-17 21:32:36 +03:00
parent 71bb96fe2f
commit 1582bdf294
9 changed files with 131 additions and 19 deletions

View File

@ -60,7 +60,15 @@ Manager::Manager()
#endif #endif
} }
Manager::~Manager() = default; Manager::~Manager()
{
if (updateAutoChecker_ && updateAutoChecker_->isLastCheckDateChanged()) {
Settings settings;
settings.load();
settings.lastUpdateCheck = updateAutoChecker_->lastCheckDate();
settings.save();
}
}
void Manager::updateSettings(const Settings &settings) void Manager::updateSettings(const Settings &settings)
{ {
@ -71,6 +79,13 @@ void Manager::updateSettings(const Settings &settings)
{"$translators$", settings.translatorsDir}, {"$translators$", settings.translatorsDir},
{"$tessdata$", settings.tessdataPath}, {"$tessdata$", settings.tessdataPath},
}); });
if (settings.autoUpdateIntervalDays > 0) {
updateAutoChecker_ = std::make_unique<update::AutoChecker>(*updater_);
updateAutoChecker_->setLastCheckDate(settings.lastUpdateCheck);
updateAutoChecker_->setCheckIntervalDays(settings.autoUpdateIntervalDays);
} else {
updateAutoChecker_.reset();
}
tray_->updateSettings(settings); tray_->updateSettings(settings);
capturer_->updateSettings(settings); capturer_->updateSettings(settings);

View File

@ -38,6 +38,7 @@ private:
std::unique_ptr<Translator> translator_; std::unique_ptr<Translator> translator_;
std::unique_ptr<Representer> representer_; std::unique_ptr<Representer> representer_;
std::unique_ptr<update::Loader> updater_; std::unique_ptr<update::Loader> updater_;
std::unique_ptr<update::AutoChecker> updateAutoChecker_;
TaskPtr last_; TaskPtr last_;
int activeTaskCount_{0}; int activeTaskCount_{0};
}; };

View File

@ -8,6 +8,7 @@
#include <QJsonObject> #include <QJsonObject>
#include <QNetworkReply> #include <QNetworkReply>
#include <QStandardPaths> #include <QStandardPaths>
#include <QTimer>
namespace update namespace update
{ {
@ -672,4 +673,69 @@ QString Installer::errorString() const
return errors_.join('\n'); return errors_.join('\n');
} }
AutoChecker::AutoChecker(Loader &loader, QObject *parent)
: QObject(parent)
, loader_(loader)
{
SOFT_ASSERT(loader.model(), return );
connect(loader.model(), &Model::modelReset, //
this, &AutoChecker::handleModelReset);
}
AutoChecker::~AutoChecker() = default;
bool AutoChecker::isLastCheckDateChanged() const
{
return isLastCheckDateChanged_;
}
QDateTime AutoChecker::lastCheckDate() const
{
return lastCheckDate_;
}
void AutoChecker::setCheckIntervalDays(int days)
{
checkIntervalDays_ = days;
scheduleNextCheck();
}
void AutoChecker::setLastCheckDate(const QDateTime &dt)
{
isLastCheckDateChanged_ = false;
lastCheckDate_ = dt;
if (!lastCheckDate_.isValid())
lastCheckDate_ = QDateTime::currentDateTime();
scheduleNextCheck();
}
void AutoChecker::scheduleNextCheck()
{
if (checkIntervalDays_ < 1 || !lastCheckDate_.isValid())
return;
if (!timer_) {
timer_ = std::make_unique<QTimer>();
timer_->setSingleShot(true);
connect(timer_.get(), &QTimer::timeout, //
&loader_, &Loader::checkForUpdates);
}
auto nextTime = lastCheckDate_.addDays(checkIntervalDays_);
const auto now = QDateTime::currentDateTime();
if (nextTime < now)
nextTime = now.addSecs(5);
timer_->start(now.msecsTo(nextTime));
}
void AutoChecker::handleModelReset()
{
lastCheckDate_ = QDateTime::currentDateTime();
isLastCheckDateChanged_ = true;
scheduleNextCheck();
}
} // namespace update } // namespace update

View File

@ -133,4 +133,27 @@ private:
std::unique_ptr<Installer> installer_; std::unique_ptr<Installer> installer_;
}; };
class AutoChecker : public QObject
{
Q_OBJECT
public:
explicit AutoChecker(Loader& loader, QObject* parent = nullptr);
~AutoChecker();
bool isLastCheckDateChanged() const;
QDateTime lastCheckDate() const;
void setCheckIntervalDays(int days);
void setLastCheckDate(const QDateTime& dt);
private:
void handleModelReset();
void scheduleNextCheck();
Loader& loader_;
bool isLastCheckDateChanged_{false};
int checkIntervalDays_{0};
QDateTime lastCheckDate_;
std::unique_ptr<QTimer> timer_;
};
} // namespace update } // namespace update

View File

@ -150,7 +150,8 @@ void Settings::save() const
settings.remove(qs_proxyPassword); settings.remove(qs_proxyPassword);
} }
settings.setValue(qs_autoUpdateType, int(autoUpdateType)); settings.setValue(qs_autoUpdateType, autoUpdateIntervalDays);
settings.setValue(qs_lastUpdateCheck, lastUpdateCheck);
settings.endGroup(); settings.endGroup();
@ -215,9 +216,10 @@ void Settings::load()
settings.value(qs_proxySavePassword, proxySavePassword).toBool(); settings.value(qs_proxySavePassword, proxySavePassword).toBool();
proxyPassword = shuffle(settings.value(qs_proxyPassword).toString()); proxyPassword = shuffle(settings.value(qs_proxyPassword).toString());
autoUpdateType = AutoUpdate( autoUpdateIntervalDays =
std::clamp(settings.value(qs_autoUpdateType, int(autoUpdateType)).toInt(), settings.value(qs_autoUpdateType, autoUpdateIntervalDays).toInt();
int(AutoUpdate::Disabled), int(AutoUpdate::Monthly))); lastUpdateCheck =
settings.value(qs_lastUpdateCheck, lastUpdateCheck).toDateTime();
settings.endGroup(); settings.endGroup();

View File

@ -2,6 +2,7 @@
#include "stfwd.h" #include "stfwd.h"
#include <QDateTime>
#include <QStringList> #include <QStringList>
#include <chrono> #include <chrono>
@ -16,8 +17,6 @@ using Substitutions = std::unordered_multimap<LanguageId, Substitution>;
enum class ProxyType { Disabled, System, Socks5, Http }; enum class ProxyType { Disabled, System, Socks5, Http };
enum class AutoUpdate { Disabled, Daily, Weekly, Monthly };
class Settings class Settings
{ {
public: public:
@ -41,8 +40,8 @@ public:
QString proxyPassword; QString proxyPassword;
bool proxySavePassword{false}; bool proxySavePassword{false};
AutoUpdate autoUpdateType{AutoUpdate::Disabled}; int autoUpdateIntervalDays{0};
QString lastUpdateCheck{""}; QDateTime lastUpdateCheck;
Substitutions userSubstitutions; Substitutions userSubstitutions;
bool useUserSubstitutions{true}; bool useUserSubstitutions{true};

View File

@ -59,13 +59,6 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
updateTranslationLanguages(); updateTranslationLanguages();
// updates // updates
QMap<AutoUpdate, QString> updateTypes;
updateTypes.insert(AutoUpdate::Disabled, tr("Disabled"));
updateTypes.insert(AutoUpdate::Daily, tr("Daily"));
updateTypes.insert(AutoUpdate::Weekly, tr("Weekly"));
updateTypes.insert(AutoUpdate::Monthly, tr("Monthly"));
ui->updateCombo->addItems(updateTypes.values());
auto updatesProxy = new QSortFilterProxyModel(this); auto updatesProxy = new QSortFilterProxyModel(this);
updatesProxy->setSourceModel(updater_.model()); updatesProxy->setSourceModel(updater_.model());
ui->updatesView->setModel(updatesProxy); ui->updatesView->setModel(updatesProxy);
@ -136,6 +129,9 @@ Settings SettingsEditor::settings() const
settings.resultShowType = settings.resultShowType =
ui->trayRadio->isChecked() ? ResultMode::Tooltip : ResultMode::Widget; ui->trayRadio->isChecked() ? ResultMode::Tooltip : ResultMode::Widget;
settings.autoUpdateIntervalDays = ui->autoUpdateInterval->value();
return settings; return settings;
} }
@ -179,6 +175,8 @@ void SettingsEditor::setSettings(const Settings &settings)
ui->trayRadio->setChecked(settings.resultShowType == ResultMode::Tooltip); ui->trayRadio->setChecked(settings.resultShowType == ResultMode::Tooltip);
ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget); ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget);
ui->autoUpdateInterval->setValue(settings.autoUpdateIntervalDays);
} }
void SettingsEditor::updateCurrentPage() void SettingsEditor::updateCurrentPage()

View File

@ -485,12 +485,19 @@
<item> <item>
<widget class="QLabel" name="label_17"> <widget class="QLabel" name="label_17">
<property name="text"> <property name="text">
<string>Check for updates:</string> <string>Update check interval (days):</string>
</property> </property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QComboBox" name="updateCombo"/> <widget class="QSpinBox" name="autoUpdateInterval">
<property name="toolTip">
<string>0 - disabled</string>
</property>
<property name="maximum">
<number>360</number>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="checkUpdates"> <widget class="QPushButton" name="checkUpdates">

View File

@ -19,7 +19,8 @@ class Recognizer;
namespace update namespace update
{ {
class Loader; class Loader;
} class AutoChecker;
} // namespace update
using TaskPtr = std::shared_ptr<Task>; using TaskPtr = std::shared_ptr<Task>;
using LanguageId = QString; using LanguageId = QString;