Skip to content

Commit 7106280

Browse files
committed
Using signals/slots for update checker.
1 parent ca2852d commit 7106280

14 files changed

+182
-321
lines changed

source/client/ui/client_window.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,11 @@ ClientWindow::ClientWindow(QWidget* parent)
133133
update_checker_->setUpdateServer(settings.updateServer());
134134
update_checker_->setPackageName("client");
135135

136+
connect(update_checker_.get(), &common::UpdateChecker::sig_checkedFinished,
137+
this, &ClientWindow::onUpdateCheckedFinished);
138+
136139
LOG(LS_INFO) << "Start update checker";
137-
update_checker_->start(Application::uiTaskRunner(), this);
140+
update_checker_->start();
138141
}
139142
#else
140143
ui.action_check_for_updates->setVisible(false);

source/client/ui/client_window.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030

3131
namespace client {
3232

33-
class ClientWindow final
34-
: public QMainWindow,
35-
public common::UpdateChecker::Delegate
33+
class ClientWindow final : public QMainWindow
3634
{
3735
Q_OBJECT
3836

@@ -44,10 +42,8 @@ class ClientWindow final
4442
// QMainWindow implementation.
4543
void closeEvent(QCloseEvent* event) final;
4644

47-
// common::UpdateChecker::Delegate implementation.
48-
void onUpdateCheckedFinished(const QByteArray& result) final;
49-
5045
private slots:
46+
void onUpdateCheckedFinished(const QByteArray& result);
5147
void onLanguageChanged(QAction* action);
5248
void onSettings();
5349
void onHelp();

source/common/http_file_downloader.cc

+20-95
Original file line numberDiff line numberDiff line change
@@ -24,78 +24,9 @@
2424

2525
namespace common {
2626

27-
class HttpFileDownloader::Runner : public std::enable_shared_from_this<Runner>
28-
{
29-
public:
30-
Runner(std::shared_ptr<base::TaskRunner> owner_task_runner, Delegate* delegate)
31-
: owner_task_runner_(std::move(owner_task_runner)),
32-
delegate_(delegate)
33-
{
34-
DCHECK(owner_task_runner_);
35-
DCHECK(delegate_);
36-
}
37-
38-
~Runner()
39-
{
40-
dettach();
41-
}
42-
43-
void dettach()
44-
{
45-
delegate_ = nullptr;
46-
}
47-
48-
void onError(int error_code)
49-
{
50-
if (!owner_task_runner_->belongsToCurrentThread())
51-
{
52-
owner_task_runner_->postTask(std::bind(&Runner::onError, shared_from_this(), error_code));
53-
return;
54-
}
55-
56-
if (delegate_)
57-
{
58-
delegate_->onFileDownloaderError(error_code);
59-
delegate_ = nullptr;
60-
}
61-
}
62-
63-
void onCompleted()
64-
{
65-
if (!owner_task_runner_->belongsToCurrentThread())
66-
{
67-
owner_task_runner_->postTask(std::bind(&Runner::onCompleted, shared_from_this()));
68-
return;
69-
}
70-
71-
if (delegate_)
72-
{
73-
delegate_->onFileDownloaderCompleted();
74-
delegate_ = nullptr;
75-
}
76-
}
77-
78-
void onProgress(int percentage)
79-
{
80-
if (!owner_task_runner_->belongsToCurrentThread())
81-
{
82-
owner_task_runner_->postTask(std::bind(&Runner::onProgress, shared_from_this(), percentage));
83-
return;
84-
}
85-
86-
if (delegate_)
87-
delegate_->onFileDownloaderProgress(percentage);
88-
}
89-
90-
private:
91-
std::shared_ptr<base::TaskRunner> owner_task_runner_;
92-
Delegate* delegate_ = nullptr;
93-
94-
DISALLOW_COPY_AND_ASSIGN(Runner);
95-
};
96-
9727
//--------------------------------------------------------------------------------------------------
98-
HttpFileDownloader::HttpFileDownloader()
28+
HttpFileDownloader::HttpFileDownloader(QObject* parent)
29+
: QThread(parent)
9930
{
10031
LOG(LS_INFO) << "Ctor";
10132
}
@@ -104,24 +35,20 @@ HttpFileDownloader::HttpFileDownloader()
10435
HttpFileDownloader::~HttpFileDownloader()
10536
{
10637
LOG(LS_INFO) << "Dtor";
107-
108-
if (runner_)
109-
{
110-
runner_->dettach();
111-
runner_.reset();
112-
}
113-
thread_.stop();
38+
interrupted_.store(true, std::memory_order_relaxed);
39+
wait();
11440
}
11541

11642
//--------------------------------------------------------------------------------------------------
117-
void HttpFileDownloader::start(const QString& url,
118-
std::shared_ptr<base::TaskRunner> owner_task_runner,
119-
Delegate* delegate)
43+
void HttpFileDownloader::setUrl(const QString& url)
12044
{
121-
LOG(LS_INFO) << "Starting http file downloader";
12245
url_ = url;
123-
runner_ = std::make_shared<Runner>(std::move(owner_task_runner), delegate);
124-
thread_.start(std::bind(&HttpFileDownloader::run, this));
46+
}
47+
48+
//--------------------------------------------------------------------------------------------------
49+
const QByteArray& HttpFileDownloader::data() const
50+
{
51+
return data_;
12552
}
12653

12754
//--------------------------------------------------------------------------------------------------
@@ -153,7 +80,8 @@ static int debugFunc(
15380
//--------------------------------------------------------------------------------------------------
15481
void HttpFileDownloader::run()
15582
{
156-
LOG(LS_INFO) << "run BEGIN";
83+
LOG(LS_INFO) << "Starting http file downloader:" << url_;
84+
interrupted_.store(false, std::memory_order_relaxed);
15785

15886
base::ScopedCURL curl;
15987

@@ -201,7 +129,7 @@ void HttpFileDownloader::run()
201129
break;
202130
}
203131

204-
if (thread_.isStopping())
132+
if (interrupted_.load(std::memory_order_relaxed))
205133
{
206134
LOG(LS_INFO) << "Downloading canceled";
207135
break;
@@ -211,18 +139,16 @@ void HttpFileDownloader::run()
211139

212140
curl_multi_remove_handle(multi_curl.get(), curl.get());
213141

214-
if (!thread_.isStopping())
142+
if (!interrupted_.load(std::memory_order_relaxed))
215143
{
216144
if (error_code != CURLM_OK)
217145
{
218-
if (runner_)
219-
runner_->onError(error_code);
146+
emit sig_downloadError(error_code);
220147
}
221148
else
222149
{
223150
LOG(LS_INFO) << "Download is finished: " << data_.size() << " bytes";
224-
if (runner_)
225-
runner_->onCompleted();
151+
emit sig_downloadCompleted();
226152
}
227153
}
228154

@@ -238,7 +164,7 @@ size_t HttpFileDownloader::writeDataCallback(
238164

239165
if (self)
240166
{
241-
if (self->thread_.isStopping())
167+
if (self->interrupted_.load(std::memory_order_relaxed))
242168
{
243169
LOG(LS_INFO) << "Interrupted by user";
244170
return 0;
@@ -256,14 +182,13 @@ size_t HttpFileDownloader::writeDataCallback(
256182
int HttpFileDownloader::progressCallback(
257183
HttpFileDownloader* self, double dltotal, double dlnow, double /* ultotal */, double /* ulnow */)
258184
{
259-
if (self && !self->thread_.isStopping())
185+
if (self && !self->interrupted_.load(std::memory_order_relaxed))
260186
{
261187
int percentage = 0;
262188
if (dltotal > 0)
263189
percentage = static_cast<int>((dlnow * 100) / dltotal);
264190

265-
if (self->runner_)
266-
self->runner_->onProgress(percentage);
191+
emit self->sig_downloadProgress(percentage);
267192
}
268193

269194
return 0;

source/common/http_file_downloader.h

+15-24
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,38 @@
2020
#define COMMON_HTTP_FILE_DOWNLOADER_H
2121

2222
#include "base/macros_magic.h"
23-
#include "base/task_runner.h"
24-
#include "base/threading/simple_thread.h"
2523

2624
#include <QByteArray>
27-
#include <QString>
25+
#include <QThread>
2826

2927
namespace common {
3028

31-
class HttpFileDownloader
29+
class HttpFileDownloader final : public QThread
3230
{
31+
Q_OBJECT
32+
3333
public:
34-
HttpFileDownloader();
34+
explicit HttpFileDownloader(QObject* parent = nullptr);
3535
~HttpFileDownloader();
3636

37-
class Delegate
38-
{
39-
public:
40-
virtual ~Delegate() = default;
37+
void setUrl(const QString& url);
38+
const QByteArray& data() const;
4139

42-
virtual void onFileDownloaderError(int error_code) = 0;
43-
virtual void onFileDownloaderCompleted() = 0;
44-
virtual void onFileDownloaderProgress(int percentage) = 0;
45-
};
40+
signals:
41+
void sig_downloadError(int error_code);
42+
void sig_downloadCompleted();
43+
void sig_downloadProgress(int percentage);
4644

47-
void start(const QString& url,
48-
std::shared_ptr<base::TaskRunner> owner_task_runner,
49-
Delegate* delegate);
50-
const QByteArray& data() const { return data_; }
45+
protected:
46+
// QThread implementation.
47+
void run() final;
5148

5249
private:
53-
void run();
54-
5550
static size_t writeDataCallback(void* ptr, size_t size, size_t nmemb, HttpFileDownloader* self);
5651
static int progressCallback(
5752
HttpFileDownloader* self, double dltotal, double dlnow, double ultotal, double ulnow);
5853

59-
base::SimpleThread thread_;
60-
61-
class Runner;
62-
std::shared_ptr<Runner> runner_;
63-
54+
std::atomic_bool interrupted_;
6455
QString url_;
6556
QByteArray data_;
6657

source/common/ui/download_dialog.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "common/ui/download_dialog.h"
2020

2121
#include "base/logging.h"
22-
#include "qt_base/application.h"
2322

2423
#include <QAbstractButton>
2524
#include <QFile>
@@ -48,7 +47,15 @@ DownloadDialog::DownloadDialog(const QString& url, QFile& file, QWidget* parent)
4847
close();
4948
});
5049

51-
downloader_->start(url, qt_base::Application::uiTaskRunner(), this);
50+
connect(downloader_.get(), &HttpFileDownloader::sig_downloadError,
51+
this, &DownloadDialog::onFileDownloaderError);
52+
connect(downloader_.get(), &HttpFileDownloader::sig_downloadCompleted,
53+
this, &DownloadDialog::onFileDownloaderCompleted);
54+
connect(downloader_.get(), &HttpFileDownloader::sig_downloadProgress,
55+
this, &DownloadDialog::onFileDownloaderProgress);
56+
57+
downloader_->setUrl(url);
58+
downloader_->start();
5259
}
5360

5461
//--------------------------------------------------------------------------------------------------

source/common/ui/download_dialog.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,18 @@
2929

3030
namespace common {
3131

32-
class DownloadDialog final
33-
: public QDialog,
34-
public HttpFileDownloader::Delegate
32+
class DownloadDialog final : public QDialog
3533
{
3634
Q_OBJECT
3735

3836
public:
3937
DownloadDialog(const QString& url, QFile& file, QWidget* parent = nullptr);
4038
~DownloadDialog() final;
4139

42-
protected:
43-
// HttpFileDownloader::Delegate implementation.
44-
void onFileDownloaderError(int error_code) final;
45-
void onFileDownloaderCompleted() final;
46-
void onFileDownloaderProgress(int percentage) final;
40+
private slots:
41+
void onFileDownloaderError(int error_code);
42+
void onFileDownloaderCompleted();
43+
void onFileDownloaderProgress(int percentage);
4744

4845
private:
4946
Ui::DownloadDialog ui;

0 commit comments

Comments
 (0)