2015-04-22 19:32:04 +02:00
# ifndef DOWNLOAD_H
# define DOWNLOAD_H
2015-09-08 17:05:59 +02:00
# include "./downloadrange.h"
# include "./optiondata.h"
2015-04-22 19:32:04 +02:00
# include <c++utilities/chrono/timespan.h>
# include <QAuthenticator>
2017-05-01 03:22:50 +02:00
# include <QBuffer>
2019-12-30 22:27:20 +01:00
# include <QElapsedTimer>
2017-05-01 03:22:50 +02:00
# include <QFile>
# include <QNetworkProxy>
2015-04-22 19:32:04 +02:00
# include <QNetworkReply>
# include <QObject>
# include <tuple>
namespace Network {
/*!
* \ brief Specifies the download status .
*/
2017-05-01 03:22:50 +02:00
enum class DownloadStatus {
2015-04-22 19:32:04 +02:00
None , /**< The download has just been created. Call init() to initialize the download. */
Initiating , /**< The download is currently initiating after init() has been called. */
Waiting , /**< The download is currently waiting for a permission or the output device to be set. */
Ready , /**< The download is initiated. It may be started using the start() method. */
Downloading , /**< The download is actually downloading. */
FinishOuputFile , /**< The download is not downloading anymore but still writing data from the buffer to the output file. */
Interrupting , /**< The is being interrupted after interrupt() has been called. */
Aborting , /**< The is being aborted after abort() has been called. */
Interrupted , /**< The download is interrupted after interrupt() has been called. */
Failed , /**< The download failed. The download might enter this status after the initialization has failed or the actual download has failed or has been aborted. */
Finished /**< The download has been finished and all received data has been written to the output file. */
} ;
2017-05-01 03:22:50 +02:00
class Download : public QObject {
2015-04-22 19:32:04 +02:00
Q_OBJECT
public :
virtual ~ Download ( ) ;
2020-03-08 14:06:45 +01:00
public Q_SLOTS :
2015-04-22 19:32:04 +02:00
// public slots to control the download
void init ( ) ;
void start ( ) ;
void start ( const QString & targetPath ) ;
void start ( QIODevice * targetDevice , bool giveOwnership = false ) ;
void stop ( ) ;
void interrupt ( ) ;
public :
// public methods
// to get status information
bool canStart ( QString & reasonIfNot ) ;
DownloadStatus status ( ) const ;
DownloadStatus lastStatus ( ) const ;
int progressPercentage ( ) const ;
double speed ( ) const ;
double shiftSpeed ( ) ;
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan remainingTime ( ) const ;
CppUtilities : : TimeSpan shiftRemainingTime ( ) ;
2015-04-22 19:32:04 +02:00
qint64 bytesReceived ( ) const ;
qint64 newBytesReceived ( ) ;
qint64 bytesToReceive ( ) const ;
qint64 newBytesToReceive ( ) ;
int lastProgressUpdate ( ) const ;
bool hasStatusInfo ( ) const ;
const QString & statusInfo ( ) const ;
QNetworkReply : : NetworkError networkError ( ) const ;
bool isStarted ( ) const ;
bool isWorking ( ) const ;
bool isDownloading ( ) const ;
bool isInitiated ( ) const ;
// to get and set meta data and additional information
const QString & targetPath ( ) const ;
void setTargetPath ( const QString & targetPath ) ;
const QUrl & initialUrl ( ) const ;
const QUrl & downloadUrl ( ) const ;
2019-12-30 22:27:20 +01:00
const QUrl & downloadUrl ( std : : size_t optionIndex ) const ;
2015-04-22 19:32:04 +02:00
const std : : vector < OptionData > & options ( ) const ;
std : : vector < OptionData > & options ( ) ;
const QString & id ( ) const ;
const QString & title ( ) const ;
const QString & collectionName ( ) const ;
2019-12-30 22:27:20 +01:00
std : : size_t availableOptionCount ( ) const ;
2015-04-22 19:32:04 +02:00
bool areOptionsAvailable ( ) const ;
2019-12-30 22:27:20 +01:00
const QString & optionName ( std : : size_t optionIndex ) const ;
2015-04-22 19:32:04 +02:00
bool haveAvailableOptionsChanged ( ) ;
bool isValidOptionChosen ( ) const ;
2019-12-30 22:27:20 +01:00
std : : size_t chosenOption ( ) const ;
2015-04-22 19:32:04 +02:00
const QString & chosenOptionName ( ) const ;
2019-12-30 22:27:20 +01:00
bool setChosenOption ( std : : size_t optionIndex ) ;
2015-04-22 19:32:04 +02:00
bool hasSelectedOptionChanged ( ) ;
const QString & userAgent ( ) ;
void setCustomUserAgent ( const QString & value ) ;
bool isDefaultUserAgentUsed ( ) const ;
void setDefaultUserAgentUsed ( bool defaultUserAgentUsed ) ;
static const QString & defaultUserAgent ( ) ;
static void scrambleDefaultUserAgent ( ) ;
const QNetworkProxy & proxy ( ) const ;
void setProxy ( const QNetworkProxy & value ) ;
const QString & uploader ( ) const ;
int views ( ) const ;
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan duration ( ) const ;
2015-04-22 19:32:04 +02:00
const QString & rating ( ) const ;
int positionInCollection ( ) const ;
int progressUpdateInterval ( ) const ;
void setProgressUpdateInterval ( int value ) ;
virtual QString suitableFilename ( ) const ;
DownloadRange & range ( ) ;
bool setRange ( const DownloadRange & value ) ;
virtual bool supportsRange ( ) const ;
virtual QString typeName ( ) const = 0 ;
2017-05-01 03:22:50 +02:00
void provideMetaData ( const QString & title = QString ( ) , const QString & uploader = QString ( ) ,
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan duration = CppUtilities : : TimeSpan ( ) , const QString & collectionName = QString ( ) , int positionInCollection = 0 ,
2017-05-01 03:22:50 +02:00
int views = 0 , const QString & rating = QString ( ) ) ;
2019-12-30 22:27:20 +01:00
void setOverwritePermission ( std : : size_t optionIndex , PermissionStatus permission ) ;
void setAppendPermission ( std : : size_t optionIndex , PermissionStatus permission ) ;
void setRedirectPermission ( std : : size_t originalOptionIndex , PermissionStatus permission ) ;
void setIgnoreSslErrorsPermission ( std : : size_t optionIndex , PermissionStatus permission ) ;
void provideOutputDevice ( std : : size_t optionIndex , QIODevice * device , bool giveOwnership = false ) ;
void provideAuthenticationCredentials ( std : : size_t optionIndex , const AuthenticationCredentials & credentials ) ;
2015-04-22 19:32:04 +02:00
const AuthenticationCredentials & initialAuthenticationCredentials ( ) const ;
AuthenticationCredentials & initialAuthenticationCredentials ( ) ;
virtual bool isInitiatingInstantlyRecommendable ( ) const ;
// to construct new downloads
static Download * fromUrl ( const QUrl & url ) ;
2020-03-09 18:46:29 +01:00
Q_SIGNALS :
2015-04-22 19:32:04 +02:00
void statusChanged ( Download * download ) ;
void progressChanged ( Download * download ) ;
void statusInfoChanged ( Download * download ) ;
2019-12-30 22:27:20 +01:00
void overwriteingPermissionRequired ( Download * download , std : : size_t optionIndex , const QString & file ) ;
void appendingPermissionRequired ( Download * download , std : : size_t optionIndex , const QString & file , quint64 offset , quint64 fileSize ) ;
void redirectionPermissonRequired ( Download * download , std : : size_t optionIndex , int redirectionOptionIndex ) ;
void outputDeviceRequired ( Download * concerningDownload , std : : size_t optionIndex ) ;
void authenticationRequired ( Download * concerningDownload , std : : size_t optionIndex , const QString & statusInfo ) ;
void sslErrors ( Download * concerningDownload , std : : size_t optionIndex , const QList < QSslError > & sslErrors ) ;
2015-04-22 19:32:04 +02:00
protected :
// c'tor
explicit Download ( const QUrl & url = QUrl ( ) , QObject * parent = nullptr ) ;
// protected methods
// to be implemented by derived classes
virtual void doDownload ( ) = 0 ;
2019-12-30 22:27:20 +01:00
virtual bool followRedirection ( std : : size_t originalOptionIndex ) ;
2015-04-22 19:32:04 +02:00
virtual void abortDownload ( ) = 0 ;
virtual void doInit ( ) = 0 ;
2019-12-30 22:27:20 +01:00
virtual void checkStatusAndClear ( std : : size_t optionIndex ) = 0 ;
2015-04-22 19:32:04 +02:00
// meant to be called by derived classes
2019-12-30 22:27:20 +01:00
std : : size_t addDownloadUrl ( const QString & optionName , const QUrl & url , std : : size_t redirectionOf = InvalidOptionIndex ) ;
void changeDownloadUrl ( std : : size_t optionIndex , const QUrl & value ) ;
2017-05-01 03:22:50 +02:00
void reportInitiated (
bool success , const QString & reasonIfNot = QString ( ) , const QNetworkReply : : NetworkError & networkError = QNetworkReply : : NoError ) ;
2019-12-30 22:27:20 +01:00
void reportFinalDownloadStatus ( std : : size_t optionIndex , bool success , const QString & statusDescription = QString ( ) ,
2017-05-01 03:22:50 +02:00
QNetworkReply : : NetworkError networkError = QNetworkReply : : NoError ) ;
2020-03-08 14:06:45 +01:00
protected Q_SLOTS :
2019-12-30 22:27:20 +01:00
void reportDownloadInterrupted ( std : : size_t optionIndex ) ;
void reportNewDataToBeWritten ( std : : size_t optionIndex , QIODevice * inputDevice ) ;
void reportRedirectionAvailable ( std : : size_t originalOptionIndex ) ;
void reportAuthenticationRequired ( std : : size_t optionIndex , const QString & realm ) ;
void reportSslErrors ( std : : size_t optionIndex , QNetworkReply * reply , const QList < QSslError > & sslErrors ) ;
void reportDownloadComplete ( std : : size_t optionIndex ) ;
void finalizeOutputDevice ( std : : size_t optionIndex ) ;
2015-04-22 19:32:04 +02:00
void setId ( const QString & value ) ;
void setTitle ( const QString & value ) ;
void setTitleFromFilename ( const QString & valueObtainedFromFilename ) ;
void setUploader ( const QString & value ) ;
void setViews ( int value ) ;
2019-06-10 22:50:15 +02:00
void setDuration ( CppUtilities : : TimeSpan value ) ;
2015-04-22 19:32:04 +02:00
void setRating ( const QString & value ) ;
void setPositionInCollection ( int value ) ;
void setCollectionName ( const QString & value ) ;
2019-12-30 22:27:20 +01:00
void reportDownloadProgressUpdate ( std : : size_t optionIndex , qint64 bytesReceived , qint64 bytesToReceive ) ;
2015-04-22 19:32:04 +02:00
private :
// private static methods
static const QUrl & emptyUrl ( ) ;
static const QString & emptyString ( ) ;
// private methods
// to handle the output device
2019-12-30 22:27:20 +01:00
bool writeBufferToOutputDevice ( std : : size_t optionIndex ) ;
bool prepareOutputDevice ( std : : size_t optionIndex , QIODevice * device , bool takeOwnership ) ;
void ensureOutputDeviceIsPrepared ( std : : size_t optionIndex ) ;
2015-04-22 19:32:04 +02:00
// to set status information
void setBytesWritten ( qint64 value ) ;
void setProgress ( qint64 m_bytesReceived = - 1 , qint64 m_bytesToReceive = - 1 ) ;
void setStatusInfo ( const QString & value ) ;
void setStatus ( DownloadStatus value ) ;
void setNetworkError ( QNetworkReply : : NetworkError value ) ;
// private fields
// concerning urls and meta information
QUrl m_initialUrl ;
QString m_id ;
QString m_title ;
bool m_dontAcceptNewTitleFromFilenameAnymore ;
QString m_uploader ;
QString m_collectionName ;
int m_views ;
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan m_duration ;
2015-04-22 19:32:04 +02:00
QString m_rating ;
int m_positionInCollection ;
AuthenticationCredentials m_initAuthData ;
// concerning available options
std : : vector < OptionData > m_optionData ;
2019-12-30 22:27:20 +01:00
std : : size_t m_chosenOption ;
2015-04-22 19:32:04 +02:00
bool m_selectedOptionChanged ;
bool m_availableOptionsChanged ;
// concerning status
DownloadStatus m_status ;
DownloadStatus m_lastState ;
qint64 m_bytesReceived ;
qint64 m_bytesToReceive ;
qint64 m_newBytesReceived ;
qint64 m_newBytesToReceive ;
double m_speed ;
double m_shiftSpeed ;
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan m_shiftRemainingTime ;
2015-04-22 19:32:04 +02:00
QString m_statusInfo ;
2019-12-30 22:27:20 +01:00
QElapsedTimer m_time ;
2015-04-22 19:32:04 +02:00
QNetworkReply : : NetworkError m_networkError ;
bool m_initiated ;
int m_progressUpdateInterval ;
// concerning download
bool m_useDefaultUserAgent ;
QString m_userAgent ;
2019-12-30 22:27:20 +01:00
static int s_defaultUserAgent ;
2015-04-22 19:32:04 +02:00
QNetworkProxy m_proxy ;
QString m_targetPath ;
DownloadRange m_range ;
} ;
/*!
* \ brief Returns an empty QUrl reference .
*/
inline const QUrl & Download : : emptyUrl ( )
{
static const QUrl empty ;
return empty ;
}
/*!
* \ brief Returns an empty QString reference .
*/
inline const QString & Download : : emptyString ( )
{
static const QString empty ;
return empty ;
}
/*!
* \ brief Returns the initial URL .
*
* ( e . g . " https://www.youtube.com/watch?v=eOCzuVrMP28 " )
*/
inline const QUrl & Download : : initialUrl ( ) const
{
return m_initialUrl ;
}
/*!
* \ brief Returns the download URL for the chosen option ( which might differ from the initial URL ) .
* \ remarks
* - The download URL is possibly not available for all kinds of downloads .
* - Some downloads might use additional information ( for instance if the POST method is used ) .
* - This information is possibly not available before the download is initiated .
*/
inline const QUrl & Download : : downloadUrl ( ) const
{
return isValidOptionChosen ( ) ? m_optionData . at ( m_chosenOption ) . m_url : emptyUrl ( ) ;
}
/*!
* \ brief Returns the download URL for the option with the specified \ a index ( which might differ from the initial URL ) .
* \ remarks
* - The download URL is possibly not available for all kinds of downloads .
* - Some downloads might use additional information ( for instance if the POST method is used ) .
* - This information is possibly not available before the download is initiated .
*/
2019-12-30 22:27:20 +01:00
inline const QUrl & Download : : downloadUrl ( std : : size_t optionIndex ) const
2015-04-22 19:32:04 +02:00
{
return m_optionData . at ( optionIndex ) . m_url ;
}
/*!
* \ brief Returns all available download URLs .
* \ remarks For a more detailed description see downloadUrl ( ) .
* \ sa downloadUrl ( )
*/
inline const std : : vector < OptionData > & Download : : options ( ) const
{
return m_optionData ;
}
/*!
* \ brief Returns all available download URLs .
* \ remarks For a more detailed description see downloadUrl ( ) .
* \ sa downloadUrl ( )
*/
inline std : : vector < OptionData > & Download : : options ( )
{
return m_optionData ;
}
/*!
* \ brief Returns the download ID if available .
* \ remarks This information is possibly not available before the download is initiated .
* ( e . g . " eOCzuVrMP28 " for the YouTube video " https://www.youtube.com/watch?v=eOCzuVrMP28 " )
*/
inline const QString & Download : : id ( ) const
{
return m_id ;
}
/*!
* \ brief Returns the title of the download if available .
* \ remarks This information is possibly not available before the download is initiated .
*/
inline const QString & Download : : title ( ) const
{
return m_title ;
}
/*!
* \ brief Returns the name of the collection the download is part of if available .
*
* This is the playlist name for YouTube videos for example .
*
* \ remarks This information is possibly not available before the download is initiated .
*/
inline const QString & Download : : collectionName ( ) const
{
return m_collectionName ;
}
/*!
* \ brief Returns the number of available options .
* \ remarks This information is possibly not available before the download is initiated .
* \ sa availableOption ( )
*/
2019-12-30 22:27:20 +01:00
inline std : : size_t Download : : availableOptionCount ( ) const
2015-04-22 19:32:04 +02:00
{
return m_optionData . size ( ) ;
}
/*!
* \ brief Returns an indication whether options are available .
*
* If no options are available this means that no way to download the requested data could be determined . The download
* can not be started in this case .
*
* \ remarks This information is possibly not available before the download is initiated .
* \ sa availableOption ( )
*/
inline bool Download : : areOptionsAvailable ( ) const
{
return ! m_optionData . empty ( ) ;
}
/*!
* \ brief Returns the name of the option with the specified \ a index .
* \ remarks This information is possibly not available before the download is initiated .
* \ sa availableOptionCount ( )
*/
2019-12-30 22:27:20 +01:00
inline const QString & Download : : optionName ( std : : size_t optionIndex ) const
2015-04-22 19:32:04 +02:00
{
2017-05-01 03:22:50 +02:00
if ( optionIndex < m_optionData . size ( ) ) {
2015-04-22 19:32:04 +02:00
return m_optionData . at ( optionIndex ) . m_name ;
} else {
return emptyString ( ) ;
}
}
/*!
* \ brief Returns whether the available options have been changed since the last call of
* this method .
*/
inline bool Download : : haveAvailableOptionsChanged ( )
{
bool res = m_availableOptionsChanged ;
m_availableOptionsChanged = false ;
return res ;
}
/*!
* \ brief Returns an indication whether a valid option has been chosen .
*/
inline bool Download : : isValidOptionChosen ( ) const
{
return availableOptionCount ( ) > 0 & & m_chosenOption < availableOptionCount ( ) ;
}
/*!
* \ brief Returns the index of the chosen option .
*
* The index might be invalid . Check isValidOptionChosen ( ) before .
*/
2019-12-30 22:27:20 +01:00
inline std : : size_t Download : : chosenOption ( ) const
2015-04-22 19:32:04 +02:00
{
return m_chosenOption ;
}
/*!
* \ brief Returns the name of the chosen option if a valid option has been chosen .
*/
inline const QString & Download : : chosenOptionName ( ) const
{
return optionName ( m_chosenOption ) ;
}
/*!
* \ brief Returns an indication whether the selected option has been changed since the last
* call of this method .
*/
inline bool Download : : hasSelectedOptionChanged ( )
{
bool res = m_selectedOptionChanged ;
m_selectedOptionChanged = false ;
return res ;
}
/*!
* \ brief Returns the user agent which is used for HTTP downloads or other downloads which allow sending such information .
*
* A custom user agent might be assigned using the setCustomUserAgent ( ) method . If no custom user agent is assigned a
* randomly picked default user agent will be used . This behaviour can be disabled / enabled using the setDefaultUserAgentUsed ( )
* method .
*/
inline const QString & Download : : userAgent ( )
{
2017-05-01 03:22:50 +02:00
if ( m_userAgent . isEmpty ( ) & & isDefaultUserAgentUsed ( ) ) {
2015-04-22 19:32:04 +02:00
return defaultUserAgent ( ) ;
}
return m_userAgent ;
}
/*!
* \ brief Sets a custom user agent used for HTTP downloads or other downloads which allow sending such information .
* \ sa userAgent ( )
*/
inline void Download : : setCustomUserAgent ( const QString & value )
{
m_userAgent = value ;
}
/*!
* \ brief Returns whether a default user agent is used when no custom user agent is assigned .
* \ sa userAgent ( )
*/
inline bool Download : : isDefaultUserAgentUsed ( ) const
{
return m_useDefaultUserAgent ;
}
/*!
* \ brief Sets whether a default user agent is used when no custom user agent is assigned .
* \ sa userAgent ( )
*/
inline void Download : : setDefaultUserAgentUsed ( bool defaultUserAgentUsed )
{
m_useDefaultUserAgent = defaultUserAgentUsed ;
}
/*!
* \ brief Returns the proxy .
*/
inline const QNetworkProxy & Download : : proxy ( ) const
{
return m_proxy ;
}
/*!
* \ brief Sets the proxy .
* \ remarks Setting the proxy when downloading does not affect the current download . You need to
* interrupt and restart the download in this case .
*/
inline void Download : : setProxy ( const QNetworkProxy & value )
{
m_proxy = value ;
}
/*!
* \ brief Returns the name of the uploader if available .
* \ remarks This information is possibly not available before the download is initiated .
*/
inline const QString & Download : : uploader ( ) const
{
return m_uploader ;
}
/*!
* \ brief Returns the number of views for visual content if available .
* \ remarks This information is possibly not available before the download is initiated .
*/
inline int Download : : views ( ) const
{
return m_views ;
}
/*!
* \ brief Returns the duration if available .
* \ remarks This information is possibly not available before the download is initiated .
*/
2019-06-10 22:50:15 +02:00
inline CppUtilities : : TimeSpan Download : : duration ( ) const
2015-04-22 19:32:04 +02:00
{
return m_duration ;
}
/*!
* \ brief Returns the rating if available .
* \ remarks This information is possibly not available before the download is initiated .
*/
inline const QString & Download : : rating ( ) const
{
return m_rating ;
}
/*!
* \ brief Returns the position of the download in the collection the download is part of .
*
* ( e . g . the track number for Grooveshark downloads )
*
* \ remarks This information is possibly not available before the download is initiated .
*/
inline int Download : : positionInCollection ( ) const
{
return m_positionInCollection ;
}
/*!
2021-07-03 19:54:06 +02:00
* \ brief Returns the status update interval in milliseconds .
2015-04-22 19:32:04 +02:00
*/
inline int Download : : progressUpdateInterval ( ) const
{
return m_progressUpdateInterval ;
}
/*!
* \ brief Sets the status interval .
2021-07-03 19:54:06 +02:00
* \ param value Specifies the status interval in milliseconds .
2015-04-22 19:32:04 +02:00
*/
inline void Download : : setProgressUpdateInterval ( int value )
{
m_progressUpdateInterval = value ;
}
/*!
* \ brief Returns the range .
* \ sa DownloadRange
*/
inline DownloadRange & Download : : range ( )
{
return m_range ;
}
/*!
* \ brief Sets the download range .
*
* The range can only be set if this feature is supported .
* \ sa supportsRange ( )
*
* \ remarks Setting the range when downloading does not affect the current download . You need to
* abort and restart the download in this case .
*
* \ returns Returns whether the range could be set .
*/
inline bool Download : : setRange ( const DownloadRange & value )
{
2017-05-01 03:22:50 +02:00
if ( supportsRange ( ) ) {
2015-04-22 19:32:04 +02:00
m_range = value ;
return true ;
}
return false ;
}
/*!
* \ brief Returns whether a range can be set .
*/
inline bool Download : : supportsRange ( ) const
{
return false ;
}
/*!
* \ brief Returns whether the download has been started .
*
* Returns false if the download has ended because the download might
* be restarted then .
*/
inline bool Download : : isStarted ( ) const
{
2017-05-01 03:22:50 +02:00
switch ( status ( ) ) {
2015-04-22 19:32:04 +02:00
case DownloadStatus : : Downloading :
case DownloadStatus : : Waiting :
case DownloadStatus : : FinishOuputFile :
case DownloadStatus : : Interrupting :
case DownloadStatus : : Aborting :
return true ;
default :
return false ;
}
}
/*!
* \ brief Returns whether the download is actually doing something .
*/
inline bool Download : : isWorking ( ) const
{
2017-05-01 03:22:50 +02:00
switch ( status ( ) ) {
2015-04-22 19:32:04 +02:00
case DownloadStatus : : Downloading :
case DownloadStatus : : FinishOuputFile :
case DownloadStatus : : Interrupting :
case DownloadStatus : : Aborting :
return true ;
default :
return false ;
}
}
/*!
* \ brief Returns whether the download is actually downloading something .
*/
inline bool Download : : isDownloading ( ) const
{
2017-05-01 03:22:50 +02:00
switch ( status ( ) ) {
2015-04-22 19:32:04 +02:00
case DownloadStatus : : Downloading :
return true ;
default :
return false ;
}
}
/*!
* \ brief Sets the ID .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa id ( )
*/
inline void Download : : setId ( const QString & value )
{
m_id = value ;
}
/*!
* \ brief Sets the title .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa title ( )
*/
inline void Download : : setTitle ( const QString & value )
{
m_title = value ;
m_dontAcceptNewTitleFromFilenameAnymore = true ;
}
/*!
* \ brief Sets the title .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* The difference between this method and setTitle ( ) is that this method
* does nothing if a title has been set previously using the setTitle ( ) method .
* This might be useful since title information which is only derived from a
* filename should generally not overwrite a title which has been obtained previously
2021-07-03 19:54:06 +02:00
* from a better source ( for example the video meta data returned by YouTube in
2015-04-22 19:32:04 +02:00
* case of a YouTube download ) .
*
* \ sa setTitle ( )
* \ sa title ( )
*/
inline void Download : : setTitleFromFilename ( const QString & valueObtainedFromFilename )
{
2017-05-01 03:22:50 +02:00
if ( ! m_dontAcceptNewTitleFromFilenameAnymore ) {
2015-04-22 19:32:04 +02:00
m_title = valueObtainedFromFilename ;
}
}
/*!
* \ brief Sets the uploader .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa uploader ( ) .
*/
inline void Download : : setUploader ( const QString & value )
{
m_uploader = value ;
}
/*!
* \ brief Sets the uploader .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa uploader ( ) .
*/
inline void Download : : setViews ( int value )
{
m_views = value ;
}
/*!
* \ brief Sets the duration .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa duration ( ) .
*/
2019-06-10 22:50:15 +02:00
inline void Download : : setDuration ( CppUtilities : : TimeSpan value )
2015-04-22 19:32:04 +02:00
{
m_duration = value ;
}
/*!
* \ brief Sets the rating .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa rating ( ) .
*/
inline void Download : : setRating ( const QString & value )
{
m_rating = value ;
}
/*!
* \ brief Sets the position of the download in the collection the download is part of .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa positionInCollection ( ) .
*/
inline void Download : : setPositionInCollection ( int value )
{
m_positionInCollection = value ;
}
/*!
* \ brief Sets name of the collection the download is part of .
*
* Might be called when subclassing . To provide meta data from an external source use provideMetaData ( ) .
*
* \ sa collectionName ( ) .
*/
inline void Download : : setCollectionName ( const QString & value )
{
m_collectionName = value ;
}
/*!
* \ brief Returns an indication whether the download is initiated .
*
* \ remarks A download which has been failed might be initiated or not - depending
2021-07-03 19:54:06 +02:00
* whether the failure occurred during initialization or during the actual download .
2015-04-22 19:32:04 +02:00
*/
inline bool Download : : isInitiated ( ) const
{
return m_initiated ;
}
/*!
* \ brief Returns the target path . The download will store downloaded data in that file .
*
* The target path might be set when calling the start ( ) method
* or using the setTargetPath ( ) method .
*
* \ sa setTargetPath ( )
*/
inline const QString & Download : : targetPath ( ) const
{
return m_targetPath ;
}
/*!
* \ brief Sets the target path .
* \ remarks The target path is ignored if there is already an output device present .
* \ sa targetPath ( )
*/
inline void Download : : setTargetPath ( const QString & targetPath )
{
m_targetPath = targetPath ;
}
/*!
* \ brief Returns the current status .
* \ sa DownloadStatus
*/
inline DownloadStatus Download : : status ( ) const
{
return m_status ;
}
/*!
* \ brief Sets the current status .
*
* This method is only intented for internal use . Use the methods start ( ) , stop ( ) , abort ( ) , . . . to
* control the status of the download . When subclassing use the report methods .
*/
inline void Download : : setStatus ( DownloadStatus value )
{
2017-05-01 03:22:50 +02:00
if ( value ! = m_status ) {
2015-04-22 19:32:04 +02:00
m_lastState = m_status ;
m_status = value ;
emit statusChanged ( this ) ;
}
}
/*!
* \ brief Returns the last status .
* \ sa status ( )
*/
inline DownloadStatus Download : : lastStatus ( ) const
{
return m_lastState ;
}
/*!
* \ brief Returns the progress percentage if available ; otherwise - 1.
*/
inline int Download : : progressPercentage ( ) const
{
2017-05-01 03:22:50 +02:00
return ( m_bytesReceived > 0 & & m_bytesToReceive > 0 ) ? ( static_cast < double > ( m_bytesReceived ) / static_cast < double > ( m_bytesToReceive ) * 100.0 )
: - 1 ;
2015-04-22 19:32:04 +02:00
}
/*!
* \ brief Returns the current speed if available ; otherwise 0.
*/
inline double Download : : speed ( ) const
{
return m_status = = DownloadStatus : : Downloading ? m_speed : 0.0 ;
}
/*!
* \ brief Returns the alteration of the speed since the last call of this method .
* \ sa speed ( )
*/
inline double Download : : shiftSpeed ( )
{
double absolute = speed ( ) ;
double shift = absolute - m_shiftSpeed ;
m_shiftSpeed = absolute ;
return shift ;
}
/*!
* \ brief Returns the estimated remaining time .
*/
2019-06-10 22:50:15 +02:00
inline CppUtilities : : TimeSpan Download : : remainingTime ( ) const
2015-04-22 19:32:04 +02:00
{
return ( m_status = = DownloadStatus : : Downloading & & m_bytesToReceive ! = - 1 )
2019-06-10 22:50:15 +02:00
? CppUtilities : : TimeSpan : : fromSeconds ( static_cast < double > ( m_bytesToReceive ) / ( m_speed * 125.0 ) )
: CppUtilities : : TimeSpan ( ) ;
2015-04-22 19:32:04 +02:00
}
/*!
* \ brief Returns the alteration of the estimated remaining time since the last call of this method .
* \ sa remainingTime ( )
*/
2019-06-10 22:50:15 +02:00
inline CppUtilities : : TimeSpan Download : : shiftRemainingTime ( )
2015-04-22 19:32:04 +02:00
{
2019-06-10 22:50:15 +02:00
CppUtilities : : TimeSpan absolute = remainingTime ( ) ;
CppUtilities : : TimeSpan shift = absolute - m_shiftRemainingTime ;
2015-04-22 19:32:04 +02:00
m_shiftRemainingTime = absolute ;
return shift ;
}
/*!
2021-07-03 19:54:06 +02:00
* \ brief Returns the number of bytes received since the download has been started .
2015-04-22 19:32:04 +02:00
*/
inline qint64 Download : : bytesReceived ( ) const
{
return m_bytesReceived ;
}
/*!
* \ brief Returns the number of bytes received since the last call of this method or
* if the method hasn ' t been called yet since the download has been started .
* \ sa bytesReceived ( )
*/
inline qint64 Download : : newBytesReceived ( )
{
qint64 shift = m_bytesReceived - m_newBytesReceived ;
m_newBytesReceived = m_bytesReceived ;
return shift ;
}
/*!
* \ brief Returns the total number of bytes to receive .
*/
inline qint64 Download : : bytesToReceive ( ) const
{
return m_bytesToReceive ;
}
/*!
* \ brief Returns the alteration of the total number of bytes to receive .
* \ sa bytesToReceive ( )
*/
inline qint64 Download : : newBytesToReceive ( )
{
qint64 shift = m_bytesToReceive - m_newBytesToReceive ;
m_newBytesToReceive = m_bytesToReceive ;
return shift ;
}
/*!
2021-07-03 19:54:06 +02:00
* \ brief Returns the number of milliseconds since the last progress update .
2015-04-22 19:32:04 +02:00
*/
inline int Download : : lastProgressUpdate ( ) const
{
return m_time . elapsed ( ) ;
}
/*!
* \ brief Returns an indication whether a status information is available .
*
* A status information is usually available if the download failed . In this case
* the status info contains the reason for the fail .
*/
inline bool Download : : hasStatusInfo ( ) const
{
return ! m_statusInfo . isEmpty ( ) ;
}
/*!
* \ brief Returns the status info .
* \ sa hasStatusInfo ( )
*/
inline const QString & Download : : statusInfo ( ) const
{
return m_statusInfo ;
}
/*!
* \ brief Sets the status info .
*
* This method is only intented for internal use . Use the report methods when subclassing .
*/
inline void Download : : setStatusInfo ( const QString & value )
{
2017-05-01 03:22:50 +02:00
if ( m_statusInfo ! = value ) {
2015-04-22 19:32:04 +02:00
m_statusInfo = value ;
emit statusInfoChanged ( this ) ;
}
}
/*!
2021-07-03 19:54:06 +02:00
* \ brief Returns what kind of network error occurred .
2015-04-22 19:32:04 +02:00
*
2021-07-03 19:54:06 +02:00
* If no network error occurred QNetworkReply : : NoError is returned .
2015-04-22 19:32:04 +02:00
*/
inline QNetworkReply : : NetworkError Download : : networkError ( ) const
{
return m_networkError ;
}
/*!
* \ brief Sets the status info .
*
* This method is only intented for internal use . Use the report methods when subclassing .
*/
inline void Download : : setNetworkError ( QNetworkReply : : NetworkError value )
{
m_networkError = value ;
}
/*!
* \ brief Returns the authentication credentials provided for initialization using provideAuthenticationCredentials ( ) .
*/
inline const AuthenticationCredentials & Download : : initialAuthenticationCredentials ( ) const
{
return m_initAuthData ;
}
/*!
* \ brief Returns the authentication credentials provided for initialization using provideAuthenticationCredentials ( ) .
*/
inline AuthenticationCredentials & Download : : initialAuthenticationCredentials ( )
{
return m_initAuthData ;
}
2019-07-20 20:20:58 +02:00
} // namespace Network
2015-04-22 19:32:04 +02:00
# endif // DOWNLOAD_H