Skip to content

Commit aa3c28d

Browse files
committed
updated internal schedule handling
1 parent 1910698 commit aa3c28d

File tree

9 files changed

+116
-72
lines changed

9 files changed

+116
-72
lines changed

AutoUpdaterCore/AutoUpdaterCore.pro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ CONFIG += staticlib
1313

1414
SOURCES += \
1515
updater.cpp \
16-
updater_p.cpp
16+
updater_p.cpp \
17+
simplescheduler.cpp
1718

1819
HEADERS += \
1920
updater.h \
2021
updater_p.h \
21-
adminauthoriser.h
22+
adminauthoriser.h \
23+
simplescheduler.h
2224

2325
DISTFILES += \
2426
adminauthoriser.dox \
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "simplescheduler.h"
2+
#include <QTimerEvent>
3+
#include "updater.h"
4+
5+
SimpleScheduler::SimpleScheduler(QObject *parent) :
6+
QObject(parent),
7+
timerHash()
8+
{}
9+
10+
int SimpleScheduler::startSchedule(int msecs, bool repeated, const QVariant &parameter)
11+
{
12+
if(msecs < 0) {
13+
qCWarning(logQtAutoUpdater, "Cannot schedule update tasks for the past!");
14+
return 0;
15+
}
16+
17+
const int id = this->startTimer(msecs, Qt::VeryCoarseTimer);
18+
if(id != 0)
19+
this->timerHash.insert(id, {repeated, parameter});
20+
return id;
21+
}
22+
23+
int SimpleScheduler::startSchedule(const QDateTime &when, const QVariant &parameter)
24+
{
25+
const qint64 delta = QDateTime::currentDateTime().msecsTo(when);
26+
if(delta > INT_MAX) {
27+
qCWarning(logQtAutoUpdater, "Time interval to big, timepoint to far in the future.");
28+
return 0;
29+
} else
30+
return this->startSchedule((int)delta, false, parameter);
31+
}
32+
33+
void SimpleScheduler::cancelSchedule(int id)
34+
{
35+
this->killTimer(id);
36+
this->timerHash.remove(id);
37+
}
38+
39+
void SimpleScheduler::timerEvent(QTimerEvent *event)
40+
{
41+
const int id = event->timerId();
42+
TimerInfo info = this->timerHash.value(id, {false, QVariant()});
43+
if(!info.first)
44+
this->cancelSchedule(id);
45+
emit scheduleTriggered(id, info.second);
46+
event->accept();
47+
}

AutoUpdaterCore/simplescheduler.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef SIMPLESCHEDULER_H
2+
#define SIMPLESCHEDULER_H
3+
4+
#include <QObject>
5+
#include <QVariant>
6+
#include <QDateTime>
7+
#include <QPair>
8+
#include <QHash>
9+
10+
class SimpleScheduler : public QObject
11+
{
12+
Q_OBJECT
13+
public:
14+
explicit SimpleScheduler(QObject *parent = 0);
15+
16+
public slots:
17+
int startSchedule(int msecs, bool repeated = false, const QVariant &parameter = QVariant());
18+
int startSchedule(const QDateTime &when, const QVariant &parameter = QVariant());
19+
void cancelSchedule(int id);
20+
21+
signals:
22+
void scheduleTriggered(int id, const QVariant &parameter);
23+
24+
protected:
25+
void timerEvent(QTimerEvent *event) override;
26+
27+
private:
28+
typedef QPair<bool, QVariant> TimerInfo;
29+
30+
QHash<int, TimerInfo> timerHash;
31+
};
32+
33+
#endif // SIMPLESCHEDULER_H

AutoUpdaterCore/updater.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,24 @@ void Updater::abortUpdateCheck(int maxDelay, bool async)
8484

8585
int Updater::scheduleUpdate(int delaySeconds, bool repeated)
8686
{
87-
if(delaySeconds < 0) {
88-
qCWarning(logQtAutoUpdater, "Cannot schedule update tasks for the past!");
87+
if((((qint64)delaySeconds) * 1000) > INT_MAX) {
88+
qCWarning(logQtAutoUpdater) << "delaySeconds to big to be converted to msecs";
8989
return 0;
9090
}
91-
9291
Q_D(Updater);
93-
int id = d->startTimer(delaySeconds * 1000, Qt::VeryCoarseTimer);
94-
if(repeated && id != 0)
95-
d->repeatTasks.insert(id);
96-
return id;
92+
return d->scheduler->startSchedule(delaySeconds * 1000, repeated);
9793
}
9894

9995
int Updater::scheduleUpdate(const QDateTime &when)
10096
{
101-
qint64 delta = QDateTime::currentDateTime().secsTo(when);
102-
if(delta > INT_MAX) {
103-
qCWarning(logQtAutoUpdater, "Time interval to big, timepoint to far in the future.");
104-
return 0;
105-
} else
106-
return this->scheduleUpdate((int)delta, false);
97+
Q_D(Updater);
98+
return d->scheduler->startSchedule(when);
10799
}
108100

109101
void Updater::cancelScheduledUpdate(int taskId)
110102
{
111103
Q_D(Updater);
112-
d->killTimer(taskId);
113-
d->repeatTasks.remove(taskId);
104+
d->scheduler->cancelSchedule(taskId);
114105
}
115106

116107
void Updater::runUpdaterOnExit(const QStringList &arguments, AdminAuthoriser *authoriser)

AutoUpdaterCore/updater_p.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <QFileInfo>
66
#include <QDir>
77
#include <QXmlStreamReader>
8-
#include <QTimerEvent>
8+
#include <QTimer>
99
using namespace QtAutoUpdater;
1010

1111
UpdaterPrivate::UpdaterPrivate(Updater *q_ptr) :
@@ -18,13 +18,15 @@ UpdaterPrivate::UpdaterPrivate(Updater *q_ptr) :
1818
lastErrorLog(),
1919
running(false),
2020
mainProcess(nullptr),
21-
repeatTasks(),
21+
scheduler(new SimpleScheduler(this)),
2222
runOnExit(false),
2323
runArguments(),
2424
adminAuth(nullptr)
2525
{
2626
connect(qApp, &QCoreApplication::aboutToQuit,
2727
this, &UpdaterPrivate::appAboutToExit);
28+
connect(this->scheduler, &SimpleScheduler::scheduleTriggered,
29+
this, &UpdaterPrivate::startUpdateCheck);
2830
}
2931

3032
UpdaterPrivate::~UpdaterPrivate()
@@ -223,12 +225,3 @@ void UpdaterPrivate::appAboutToExit()
223225
this->runOnExit = false;//prevent warning
224226
}
225227
}
226-
227-
void UpdaterPrivate::timerEvent(QTimerEvent *event)
228-
{
229-
if(!this->repeatTasks.contains(event->timerId()))
230-
this->killTimer(event->timerId());
231-
event->accept();
232-
233-
this->startUpdateCheck();
234-
}

AutoUpdaterCore/updater_p.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#define UPDATER_P_H
33

44
#include "updater.h"
5-
#include <QTimer>
65
#include <QProcess>
76
#include <QScopedPointer>
87
#include <exception>
8+
#include "simplescheduler.h"
99

1010
template<typename... Args> struct SELECT {
1111
template<typename C, typename R>
@@ -45,7 +45,7 @@ namespace QtAutoUpdater
4545
bool running;
4646
QProcess *mainProcess;
4747

48-
QSet<int> repeatTasks;
48+
SimpleScheduler *scheduler;
4949

5050
bool runOnExit;
5151
QStringList runArguments;
@@ -65,9 +65,6 @@ namespace QtAutoUpdater
6565
void updaterError(QProcess::ProcessError error);
6666

6767
void appAboutToExit();
68-
69-
protected:
70-
void timerEvent(QTimerEvent *event) Q_DECL_OVERRIDE;
7168
};
7269
}
7370

AutoUpdaterGui/updatecontroller.cpp

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <QCoreApplication>
55
#include <QDir>
66
#include <QFileInfo>
7-
#include <QTimerEvent>
87
#include <dialogmaster.h>
98
#include "adminauthorization.h"
109
#include "updatebutton.h"
@@ -205,47 +204,24 @@ bool UpdateController::cancelUpdate(int maxDelay)
205204

206205
int UpdateController::scheduleUpdate(int delaySeconds, bool repeated, UpdateController::DisplayLevel displayLevel)
207206
{
207+
if((((qint64)delaySeconds) * 1000) > INT_MAX) {
208+
qCWarning(logQtAutoUpdater) << "delaySeconds to big to be converted to msecs";
209+
return 0;
210+
}
208211
Q_D(UpdateController);
209-
int id = this->startTimer(delaySeconds * 1000, Qt::VeryCoarseTimer);
210-
if(id != 0)
211-
d->updateTasks.insert(id, {displayLevel, repeated});
212-
return id;
212+
return d->scheduler->startSchedule(delaySeconds * 1000, repeated, QVariant::fromValue(displayLevel));
213213
}
214214

215215
int UpdateController::scheduleUpdate(const QDateTime &when, UpdateController::DisplayLevel displayLevel)
216-
{
217-
qint64 delta = QDateTime::currentDateTime().secsTo(when);
218-
if(delta > INT_MAX) {
219-
qCWarning(logQtAutoUpdater, "Time interval to big, timepoint to far in the future.");
220-
return 0;
221-
} else
222-
return this->scheduleUpdate((int)delta, false, displayLevel);
223-
}
224-
225-
void UpdateController::cancelScheduledUpdate(int taskId)
226216
{
227217
Q_D(UpdateController);
228-
this->killTimer(taskId);
229-
d->updateTasks.remove(taskId);
218+
return d->scheduler->startSchedule(when, QVariant::fromValue(displayLevel));
230219
}
231220

232-
void UpdateController::timerEvent(QTimerEvent *event)
221+
void UpdateController::cancelScheduledUpdate(int taskId)
233222
{
234223
Q_D(UpdateController);
235-
int tId = event->timerId();
236-
if(d->updateTasks.contains(tId)) {
237-
UpdateControllerPrivate::UpdateTask task = d->updateTasks[tId];
238-
if(!task.second) {
239-
d->updateTasks.remove(tId);
240-
this->killTimer(tId);
241-
}
242-
event->accept();
243-
244-
this->start(task.first);
245-
} else {
246-
this->killTimer(tId);
247-
event->ignore();
248-
}
224+
d->scheduler->cancelSchedule(taskId);
249225
}
250226

251227
void UpdateController::checkUpdatesDone(bool hasUpdates, bool hasError)
@@ -336,13 +312,18 @@ void UpdateController::checkUpdatesDone(bool hasUpdates, bool hasError)
336312
emit runningChanged(false);
337313
}
338314

315+
void UpdateController::timerTriggered(int, const QVariant &parameter)
316+
{
317+
if(parameter.canConvert<DisplayLevel>())
318+
this->start(parameter.value<DisplayLevel>());
319+
}
320+
339321
//-----------------PRIVATE IMPLEMENTATION-----------------
340322

341323
UpdateControllerPrivate::UpdateControllerPrivate(UpdateController *q_ptr, QWidget *window) :
342324
UpdateControllerPrivate(q_ptr, QString(), window)
343325
{}
344326

345-
#include <QDebug>
346327
UpdateControllerPrivate::UpdateControllerPrivate(UpdateController *q_ptr, const QString &toolPath, QWidget *window) :
347328
q_ptr(q_ptr),
348329
window(window),
@@ -355,11 +336,13 @@ UpdateControllerPrivate::UpdateControllerPrivate(UpdateController *q_ptr, const
355336
detailedInfo(true),
356337
checkUpdatesProgress(nullptr),
357338
wasCanceled(false),
358-
updateTasks()
339+
scheduler(new SimpleScheduler(q_ptr))
359340
{
360341
QObject::connect(this->mainUpdater, &Updater::checkUpdatesDone,
361342
q_ptr, &UpdateController::checkUpdatesDone,
362343
Qt::QueuedConnection);
344+
QObject::connect(this->scheduler, &SimpleScheduler::scheduleTriggered,
345+
q_ptr, &UpdateController::timerTriggered);
363346

364347
#ifdef Q_OS_UNIX
365348
//TODO test

AutoUpdaterGui/updatecontroller.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@ namespace QtAutoUpdater
103103
//! NOTIFY-Accessor for UpdateController::runAsAdmin
104104
void runAsAdminChanged(bool runAsAdmin);
105105

106-
protected:
107-
//! Reimplemented to allow update scheduling
108-
void timerEvent(QTimerEvent *event) Q_DECL_FINAL;
109-
110106
private slots:
111107
void checkUpdatesDone(bool hasUpdates, bool hasError);
108+
void timerTriggered(int, const QVariant &parameter);
112109

113110
private:
114111
UpdateControllerPrivate *d_ptr;

AutoUpdaterGui/updatecontroller_p.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "updatecontroller.h"
77
#include "progressdialog.h"
88
#include "updateinfodialog.h"
9+
#include "simplescheduler.h"
910

1011
namespace QtAutoUpdater
1112
{
@@ -29,7 +30,7 @@ namespace QtAutoUpdater
2930
QPointer<ProgressDialog> checkUpdatesProgress;
3031
bool wasCanceled;
3132

32-
QHash<int, UpdateTask> updateTasks;
33+
SimpleScheduler *scheduler;
3334

3435
UpdateControllerPrivate(UpdateController *q_ptr, QWidget *window);
3536
UpdateControllerPrivate(UpdateController *q_ptr, const QString &toolPath, QWidget *window);

0 commit comments

Comments
 (0)