From 4408ba275c99b32cfd562ec261a4e6cf56a86d0f Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Sat, 16 Apr 2016 00:19:36 +0200 Subject: [PATCH] Add localUrlForDownload method --- src/libtomahawk/DownloadManager.cpp | 32 +++++++++++++++++++++++++++++ src/libtomahawk/DownloadManager.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/libtomahawk/DownloadManager.cpp b/src/libtomahawk/DownloadManager.cpp index a01297eab..79cd4da8f 100644 --- a/src/libtomahawk/DownloadManager.cpp +++ b/src/libtomahawk/DownloadManager.cpp @@ -24,6 +24,8 @@ #include "TomahawkSettings.h" #include "infosystem/InfoSystem.h" #include "utils/Logger.h" +#include "Result.h" +#include "Query.h" DownloadManager* DownloadManager::s_instance = 0; @@ -85,6 +87,36 @@ DownloadManager::localFileForDownload( const QString& url ) const } +QUrl +DownloadManager::localUrlForDownload( const Tomahawk::query_ptr& query ) const +{ + Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr(); + if ( result ) + { + return localUrlForDownload( result ); + } + + return QUrl(); +} + + +QUrl +DownloadManager::localUrlForDownload( const Tomahawk::result_ptr& result ) const +{ + if ( result && !result->downloadFormats().isEmpty() && + !localFileForDownload( result->downloadFormats().first().url.toString() ).isEmpty() ) + { + return QUrl::fromLocalFile( QFileInfo( DownloadManager::instance()->localFileForDownload( result->downloadFormats().first().url.toString() ) ).absolutePath() ); + } + else if ( result && result->downloadJob() && result->downloadJob()->state() == DownloadJob::Finished ) + { + return QUrl::fromLocalFile( QFileInfo( result->downloadJob()->localFile() ).absolutePath() ); + } + + return QUrl(); +} + + void DownloadManager::storeJobs( const QList& jobs ) { diff --git a/src/libtomahawk/DownloadManager.h b/src/libtomahawk/DownloadManager.h index f4a873581..62c84a884 100644 --- a/src/libtomahawk/DownloadManager.h +++ b/src/libtomahawk/DownloadManager.h @@ -45,6 +45,8 @@ public: void storeJobs( const QList& jobs ); QString localFileForDownload( const QString& url ) const; + QUrl localUrlForDownload( const Tomahawk::result_ptr& result ) const; + QUrl localUrlForDownload( const Tomahawk::query_ptr& query ) const; public slots: bool addJob( const downloadjob_ptr& job );