Skip to content

Commit

Permalink
MythDownloadManager: Add a cancelDownload method.
Browse files Browse the repository at this point in the history
Removes the given url from the pending queue and aborts any download
that has already started.

Also invalidate the return data and file pointers for removeListener to
ensure we don't leak memory or disk space for returned results that are
no longer required (should removeListener abort the download as well?)
  • Loading branch information
Mark Kendall committed Nov 7, 2011
1 parent cf4d43c commit 99bff1a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
51 changes: 48 additions & 3 deletions mythtv/libs/libmythbase/mythdownloadmanager.cpp
Expand Up @@ -689,7 +689,44 @@ bool MythDownloadManager::downloadNow(MythDownloadInfo *dlInfo, bool deleteInfo)

return success;
}


/** \fn MythDownloadManager::cancelDownload(const QString &)
* \brief Cancel a queued or current download.
* \param url for download to cancel
*/
void MythDownloadManager::cancelDownload(const QString &url)
{
QMutexLocker locker(m_infoLock);
MythDownloadInfo *dlInfo;

QMutableListIterator<MythDownloadInfo*> lit(m_downloadQueue);
while (lit.hasNext())
{
lit.next();
dlInfo = lit.value();
if (dlInfo->m_url == url)
{
// this shouldn't happen
if (dlInfo->m_reply)
dlInfo->m_reply->abort();
lit.remove();
delete dlInfo;
}
}

if (m_downloadInfos.contains(url))
{
dlInfo = m_downloadInfos[url];
if (dlInfo->m_reply)
{
m_downloadReplies.remove(dlInfo->m_reply);
dlInfo->m_reply->abort();
}
m_downloadInfos.remove(url);
delete dlInfo;
}
}

/** \fn MythDownloadManager::removeListener(QObject *caller)
* \brief Disconnects the specify caller from any existing
* MythDownloadInfo instances.
Expand All @@ -705,15 +742,23 @@ void MythDownloadManager::removeListener(QObject *caller)
{
dlInfo = *lit;
if (dlInfo->m_caller == caller)
dlInfo->m_caller = NULL;
{
dlInfo->m_caller = NULL;
dlInfo->m_outFile = QString();
dlInfo->m_data = NULL;
}
}

QMap <QString, MythDownloadInfo*>::iterator mit = m_downloadInfos.begin();
for (; mit != m_downloadInfos.end(); ++mit)
{
dlInfo = mit.value();
if (dlInfo->m_caller == caller)
dlInfo->m_caller = NULL;
{
dlInfo->m_caller = NULL;
dlInfo->m_outFile = QString();
dlInfo->m_data = NULL;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions mythtv/libs/libmythbase/mythdownloadmanager.h
Expand Up @@ -50,6 +50,9 @@ class MBASE_PUBLIC MythDownloadManager : public QObject, public MThread
bool post(const QString &url, QByteArray *data);
bool post(QNetworkRequest *req, QByteArray *data);

// Cancel a download
void cancelDownload(const QString &url);

// Generic helpers
void removeListener(QObject *caller);
QDateTime GetLastModified(const QString &url);
Expand Down

0 comments on commit 99bff1a

Please sign in to comment.