From 194d0c5c2729bc608a6629f62ac1cc6752a75b52 Mon Sep 17 00:00:00 2001
From: Jean-Yves Avenard <jyavenard@mythtv.org>
Date: Fri, 12 Jul 2013 01:16:52 +1000
Subject: [PATCH] Ensure cancelling a download is done in the same thread it
started
This prevent Qt to crash with "Cannot send events to objects owned by a different thread."
Fixes #11663
---
mythtv/libs/libmythbase/mythdownloadmanager.cpp | 24 ++++++++++++++++++++++--
mythtv/libs/libmythbase/mythdownloadmanager.h | 21 +++++++++++++++++++++
2 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/mythtv/libs/libmythbase/mythdownloadmanager.cpp b/mythtv/libs/libmythbase/mythdownloadmanager.cpp
index b782cf0..d5081e9 100644
|
a
|
b
|
bool MythDownloadManager::downloadNow(MythDownloadInfo *dlInfo, bool deleteInfo)
|
| 856 | 856 | */ |
| 857 | 857 | void MythDownloadManager::cancelDownload(const QString &url) |
| 858 | 858 | { |
| | 859 | CancelDownloadRequest *req = new CancelDownloadRequest(url); |
| | 860 | connect(req, SIGNAL(cancelDownloadRequest(QString)), |
| | 861 | this, SLOT(processCancelDownload(QString))); |
| | 862 | // will emit signal as soon as event loop is ready |
| | 863 | MThreadPool::globalInstance()->start(req, "CancelRequest"); |
| | 864 | } |
| | 865 | |
| | 866 | void MythDownloadManager::processCancelDownload(QString url) |
| | 867 | { |
| 859 | 868 | QMutexLocker locker(m_infoLock); |
| 860 | 869 | MythDownloadInfo *dlInfo; |
| 861 | 870 | |
| … |
… |
void MythDownloadManager::cancelDownload(const QString &url)
|
| 869 | 878 | // this shouldn't happen |
| 870 | 879 | if (dlInfo->m_reply) |
| 871 | 880 | { |
| 872 | | LOG(VB_FILE, LOG_DEBUG, |
| | 881 | LOG(VB_FILE, LOG_DEBUG, |
| 873 | 882 | LOC + QString("Aborting download - user request")); |
| 874 | 883 | dlInfo->m_reply->abort(); |
| 875 | 884 | } |
| … |
… |
void MythDownloadManager::cancelDownload(const QString &url)
|
| 886 | 895 | dlInfo = m_downloadInfos[url]; |
| 887 | 896 | if (dlInfo->m_reply) |
| 888 | 897 | { |
| 889 | | LOG(VB_FILE, LOG_DEBUG, |
| | 898 | LOG(VB_FILE, LOG_DEBUG, |
| 890 | 899 | LOC + QString("Aborting download - user request")); |
| 891 | 900 | m_downloadReplies.remove(dlInfo->m_reply); |
| 892 | 901 | dlInfo->m_reply->abort(); |
| … |
… |
void MythCookieJar::save(const QString &filename)
|
| 1571 | 1580 | } |
| 1572 | 1581 | } |
| 1573 | 1582 | |
| | 1583 | /** \brief CancelDownloadRequest Constructor |
| | 1584 | * \param url URL download to cancel |
| | 1585 | */ |
| | 1586 | CancelDownloadRequest::CancelDownloadRequest(const QString &url) : m_url(url) |
| | 1587 | { |
| | 1588 | } |
| | 1589 | |
| | 1590 | void CancelDownloadRequest::run(void) |
| | 1591 | { |
| | 1592 | emit cancelDownloadRequest(m_url); |
| | 1593 | } |
| 1574 | 1594 | |
| 1575 | 1595 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |
| 1576 | 1596 | |
diff --git a/mythtv/libs/libmythbase/mythdownloadmanager.h b/mythtv/libs/libmythbase/mythdownloadmanager.h
index 00733a9..58bf318 100644
|
a
|
b
|
|
| 11 | 11 | #include <QWaitCondition> |
| 12 | 12 | #include <QString> |
| 13 | 13 | #include <QHash> |
| | 14 | #include <QRunnable> |
| 14 | 15 | |
| 15 | 16 | #include "mythbaseexp.h" |
| 16 | 17 | #include "mthread.h" |
| … |
… |
class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
|
| 92 | 93 | // QNetworkAccessManager signals |
| 93 | 94 | void downloadFinished(QNetworkReply* reply); |
| 94 | 95 | void authCallback(QNetworkReply *reply, QAuthenticator *authenticator); |
| | 96 | void processCancelDownload(QString m_url); |
| 95 | 97 | |
| 96 | 98 | // QNetworkReply signals |
| 97 | 99 | void downloadError(QNetworkReply::NetworkError errorCode); |
| … |
… |
class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
|
| 152 | 154 | |
| 153 | 155 | MBASE_PUBLIC MythDownloadManager *GetMythDownloadManager(void); |
| 154 | 156 | |
| | 157 | /** |
| | 158 | * \class CancelDownloadRequest |
| | 159 | */ |
| | 160 | class CancelDownloadRequest : public QObject, public QRunnable |
| | 161 | { |
| | 162 | Q_OBJECT |
| | 163 | |
| | 164 | public: |
| | 165 | CancelDownloadRequest(const QString &url); |
| | 166 | |
| | 167 | void run(); |
| | 168 | |
| | 169 | signals: |
| | 170 | void cancelDownloadRequest(QString); |
| | 171 | |
| | 172 | private: |
| | 173 | QString m_url; |
| | 174 | }; |
| | 175 | |
| 155 | 176 | #endif |
| 156 | 177 | |
| 157 | 178 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |