Added auto update checker
This commit is contained in:
		
							parent
							
								
									71bb96fe2f
								
							
						
					
					
						commit
						1582bdf294
					
				@ -60,7 +60,15 @@ Manager::Manager()
 | 
			
		||||
#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)
 | 
			
		||||
{
 | 
			
		||||
@ -71,6 +79,13 @@ void Manager::updateSettings(const Settings &settings)
 | 
			
		||||
      {"$translators$", settings.translatorsDir},
 | 
			
		||||
      {"$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);
 | 
			
		||||
  capturer_->updateSettings(settings);
 | 
			
		||||
 | 
			
		||||
@ -38,6 +38,7 @@ private:
 | 
			
		||||
  std::unique_ptr<Translator> translator_;
 | 
			
		||||
  std::unique_ptr<Representer> representer_;
 | 
			
		||||
  std::unique_ptr<update::Loader> updater_;
 | 
			
		||||
  std::unique_ptr<update::AutoChecker> updateAutoChecker_;
 | 
			
		||||
  TaskPtr last_;
 | 
			
		||||
  int activeTaskCount_{0};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,7 @@
 | 
			
		||||
#include <QJsonObject>
 | 
			
		||||
#include <QNetworkReply>
 | 
			
		||||
#include <QStandardPaths>
 | 
			
		||||
#include <QTimer>
 | 
			
		||||
 | 
			
		||||
namespace update
 | 
			
		||||
{
 | 
			
		||||
@ -672,4 +673,69 @@ QString Installer::errorString() const
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
@ -133,4 +133,27 @@ private:
 | 
			
		||||
  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
 | 
			
		||||
 | 
			
		||||
@ -150,7 +150,8 @@ void Settings::save() const
 | 
			
		||||
    settings.remove(qs_proxyPassword);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  settings.setValue(qs_autoUpdateType, int(autoUpdateType));
 | 
			
		||||
  settings.setValue(qs_autoUpdateType, autoUpdateIntervalDays);
 | 
			
		||||
  settings.setValue(qs_lastUpdateCheck, lastUpdateCheck);
 | 
			
		||||
 | 
			
		||||
  settings.endGroup();
 | 
			
		||||
 | 
			
		||||
@ -215,9 +216,10 @@ void Settings::load()
 | 
			
		||||
      settings.value(qs_proxySavePassword, proxySavePassword).toBool();
 | 
			
		||||
  proxyPassword = shuffle(settings.value(qs_proxyPassword).toString());
 | 
			
		||||
 | 
			
		||||
  autoUpdateType = AutoUpdate(
 | 
			
		||||
      std::clamp(settings.value(qs_autoUpdateType, int(autoUpdateType)).toInt(),
 | 
			
		||||
                 int(AutoUpdate::Disabled), int(AutoUpdate::Monthly)));
 | 
			
		||||
  autoUpdateIntervalDays =
 | 
			
		||||
      settings.value(qs_autoUpdateType, autoUpdateIntervalDays).toInt();
 | 
			
		||||
  lastUpdateCheck =
 | 
			
		||||
      settings.value(qs_lastUpdateCheck, lastUpdateCheck).toDateTime();
 | 
			
		||||
 | 
			
		||||
  settings.endGroup();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
#include "stfwd.h"
 | 
			
		||||
 | 
			
		||||
#include <QDateTime>
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
 | 
			
		||||
#include <chrono>
 | 
			
		||||
@ -16,8 +17,6 @@ using Substitutions = std::unordered_multimap<LanguageId, Substitution>;
 | 
			
		||||
 | 
			
		||||
enum class ProxyType { Disabled, System, Socks5, Http };
 | 
			
		||||
 | 
			
		||||
enum class AutoUpdate { Disabled, Daily, Weekly, Monthly };
 | 
			
		||||
 | 
			
		||||
class Settings
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
@ -41,8 +40,8 @@ public:
 | 
			
		||||
  QString proxyPassword;
 | 
			
		||||
  bool proxySavePassword{false};
 | 
			
		||||
 | 
			
		||||
  AutoUpdate autoUpdateType{AutoUpdate::Disabled};
 | 
			
		||||
  QString lastUpdateCheck{""};
 | 
			
		||||
  int autoUpdateIntervalDays{0};
 | 
			
		||||
  QDateTime lastUpdateCheck;
 | 
			
		||||
 | 
			
		||||
  Substitutions userSubstitutions;
 | 
			
		||||
  bool useUserSubstitutions{true};
 | 
			
		||||
 | 
			
		||||
@ -59,13 +59,6 @@ SettingsEditor::SettingsEditor(Manager &manager, update::Loader &updater)
 | 
			
		||||
  updateTranslationLanguages();
 | 
			
		||||
 | 
			
		||||
  // 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);
 | 
			
		||||
  updatesProxy->setSourceModel(updater_.model());
 | 
			
		||||
  ui->updatesView->setModel(updatesProxy);
 | 
			
		||||
@ -136,6 +129,9 @@ Settings SettingsEditor::settings() const
 | 
			
		||||
 | 
			
		||||
  settings.resultShowType =
 | 
			
		||||
      ui->trayRadio->isChecked() ? ResultMode::Tooltip : ResultMode::Widget;
 | 
			
		||||
 | 
			
		||||
  settings.autoUpdateIntervalDays = ui->autoUpdateInterval->value();
 | 
			
		||||
 | 
			
		||||
  return settings;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -179,6 +175,8 @@ void SettingsEditor::setSettings(const Settings &settings)
 | 
			
		||||
 | 
			
		||||
  ui->trayRadio->setChecked(settings.resultShowType == ResultMode::Tooltip);
 | 
			
		||||
  ui->dialogRadio->setChecked(settings.resultShowType == ResultMode::Widget);
 | 
			
		||||
 | 
			
		||||
  ui->autoUpdateInterval->setValue(settings.autoUpdateIntervalDays);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SettingsEditor::updateCurrentPage()
 | 
			
		||||
 | 
			
		||||
@ -485,12 +485,19 @@
 | 
			
		||||
          <item>
 | 
			
		||||
           <widget class="QLabel" name="label_17">
 | 
			
		||||
            <property name="text">
 | 
			
		||||
             <string>Check for updates:</string>
 | 
			
		||||
             <string>Update check interval (days):</string>
 | 
			
		||||
            </property>
 | 
			
		||||
           </widget>
 | 
			
		||||
          </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>
 | 
			
		||||
           <widget class="QPushButton" name="checkUpdates">
 | 
			
		||||
 | 
			
		||||
@ -19,7 +19,8 @@ class Recognizer;
 | 
			
		||||
namespace update
 | 
			
		||||
{
 | 
			
		||||
class Loader;
 | 
			
		||||
}
 | 
			
		||||
class AutoChecker;
 | 
			
		||||
}  // namespace update
 | 
			
		||||
 | 
			
		||||
using TaskPtr = std::shared_ptr<Task>;
 | 
			
		||||
using LanguageId = QString;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user