@@ -0,0 +1,74 @@
1+/*
2+ * Copyright (C) 2014 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "croppedimagesizerasyncworker.h"
18+
19+#include "croppedimagesizer.h"
20+
21+#include <QNetworkReply>
22+#include <QtConcurrentRun>
23+
24+CroppedImageSizerAsyncWorker::CroppedImageSizerAsyncWorker(CroppedImageSizer *sizer, QNetworkReply *reply)
25+ : m_sizer(sizer),
26+ m_reply(reply),
27+ m_ignoreAbort(false)
28+{
29+ connect(m_reply, &QNetworkReply::finished, this, &CroppedImageSizerAsyncWorker::requestFinished);
30+}
31+
32+void CroppedImageSizerAsyncWorker::abort()
33+{
34+ // This runs on main thread
35+ QMutexLocker locker(&m_mutex);
36+ m_sizer = nullptr;
37+ // If we already started the future run we can't abort the reply
38+ // since we don't know at which stage the future is, just let it finish
39+ if (!m_ignoreAbort) {
40+ QMetaObject::invokeMethod(m_reply, "abort", Qt::QueuedConnection);
41+ }
42+}
43+
44+void CroppedImageSizerAsyncWorker::requestFinished()
45+{
46+ // This runs on main thread
47+ QMutexLocker locker(&m_mutex);
48+ if (m_sizer) {
49+ m_ignoreAbort = true;
50+ QtConcurrent::run(processRequestFinished, this);
51+ } else {
52+ // We were aborted delete ourselves
53+ m_reply->deleteLater();
54+ deleteLater();
55+ }
56+}
57+
58+void CroppedImageSizerAsyncWorker::processRequestFinished(CroppedImageSizerAsyncWorker *worker)
59+{
60+ // This runs on non main thread
61+ // m_reply has finished at this point and is protected against change by m_ignoreAbort
62+ QImageReader reader(worker->m_reply);
63+ const QSize imageSize = reader.size();
64+
65+ worker->m_mutex.lock();
66+ if (worker->m_sizer) {
67+ QMetaObject::invokeMethod(worker->m_sizer, "setImageSize", Qt::QueuedConnection, Q_ARG(QSize, imageSize));
68+ }
69+ worker->m_mutex.unlock();
70+
71+ // All work is done, delete ourselves
72+ worker->m_reply->deleteLater();
73+ worker->deleteLater();
74+}