LCOV - code coverage report
Current view: top level - core/network - qgsnetworkcontentfetcherregistry.h (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 1 11 9.1 %
Date: 2021-04-10 08:29:14 Functions: 0 0 -
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /***************************************************************************
       2                 :            :                        qgsnetworkcontentfetcherregistry.h
       3                 :            :                              -------------------
       4                 :            :     begin                : April, 2018
       5                 :            :     copyright            : (C) 2018 by Denis Rouzaud
       6                 :            :     email                : denis@opengis.ch
       7                 :            : 
       8                 :            :  ***************************************************************************/
       9                 :            : 
      10                 :            : /***************************************************************************
      11                 :            :  *                                                                         *
      12                 :            :  *   This program is free software; you can redistribute it and/or modify  *
      13                 :            :  *   it under the terms of the GNU General Public License as published by  *
      14                 :            :  *   the Free Software Foundation; either version 2 of the License, or     *
      15                 :            :  *   (at your option) any later version.                                   *
      16                 :            :  *                                                                         *
      17                 :            :  ***************************************************************************/
      18                 :            : 
      19                 :            : #ifndef QGSNETWORKCONTENTFETCHERREGISTRY_H
      20                 :            : #define QGSNETWORKCONTENTFETCHERREGISTRY_H
      21                 :            : 
      22                 :            : #include <QObject>
      23                 :            : #include <QMap>
      24                 :            : #include <QMutex>
      25                 :            : #include <QNetworkReply>
      26                 :            : #include <QFile>
      27                 :            : #include <QTemporaryFile>
      28                 :            : 
      29                 :            : #include "qgis_core.h"
      30                 :            : #include "qgstaskmanager.h"
      31                 :            : #include "qgsnetworkcontentfetchertask.h"
      32                 :            : 
      33                 :            : /**
      34                 :            :  * \class QgsFetchedContent
      35                 :            :  * \ingroup core
      36                 :            :  * \brief FetchedContent holds useful information about a network content being fetched
      37                 :            :  * \see QgsNetworkContentFetcherRegistry
      38                 :            :  * \since QGIS 3.2
      39                 :            :  */
      40                 :            : class CORE_EXPORT QgsFetchedContent : public QObject
      41                 :            : {
      42                 :            :     Q_OBJECT
      43                 :            :   public:
      44                 :            :     //! Status of fetched content
      45                 :            :     enum ContentStatus
      46                 :            :     {
      47                 :            :       NotStarted, //!< No download started for such URL
      48                 :            :       Downloading, //!< Currently downloading
      49                 :            :       Finished, //!< Download finished and successful
      50                 :            :       Failed //!< Download failed
      51                 :            :     };
      52                 :            : 
      53                 :            :     //! Constructs a FetchedContent with pointer to the downloaded file and status of the download
      54                 :          0 :     explicit QgsFetchedContent( const QString &url, QTemporaryFile *file = nullptr, ContentStatus status = NotStarted )
      55                 :          0 :       : mUrl( url )
      56                 :          0 :       , mFile( file )
      57                 :          0 :       , mStatus( status )
      58                 :          0 :     {}
      59                 :            : 
      60                 :            :     ~QgsFetchedContent() override
      61                 :            :     {
      62                 :            :       if ( mFile )
      63                 :            :         mFile->close();
      64                 :            :       delete mFile;
      65                 :            :     }
      66                 :            : 
      67                 :            : 
      68                 :            : #ifndef SIP_RUN
      69                 :            :     //! Returns a pointer to the local file, or NULLPTR if the file is not accessible yet.
      70                 :          0 :     QFile *file() const {return mFile;}
      71                 :            : #endif
      72                 :            : 
      73                 :            :     //! Returns the path to the local file, an empty string if the file is not accessible yet.
      74                 :          0 :     const QString filePath() const {return mFilePath;}
      75                 :            : 
      76                 :            :     //! Returns the status of the download
      77                 :          0 :     ContentStatus status() const {return mStatus;}
      78                 :            : 
      79                 :            :     //! Returns the potential error of the download
      80                 :            :     QNetworkReply::NetworkError error() const {return mError;}
      81                 :            : 
      82                 :            :   public slots:
      83                 :            : 
      84                 :            :     /**
      85                 :            :      * \brief Start the download
      86                 :            :      * \param redownload if set to TRUE, it will restart any achieved or pending download.
      87                 :            :      */
      88                 :            :     void download( bool redownload = false );
      89                 :            : 
      90                 :            :     /**
      91                 :            :      * Cancel the download operation.
      92                 :            :      */
      93                 :            :     void cancel();
      94                 :            : 
      95                 :            :   signals:
      96                 :            :     //! Emitted when the file is fetched and accessible
      97                 :            :     void fetched();
      98                 :            : 
      99                 :            :   private slots:
     100                 :            :     void taskCompleted();
     101                 :            : 
     102                 :            :   private:
     103                 :            :     QString mUrl;
     104                 :            :     QTemporaryFile *mFile = nullptr;
     105                 :            :     QString mFilePath;
     106                 :          0 :     QgsNetworkContentFetcherTask *mFetchingTask = nullptr;
     107                 :            :     ContentStatus mStatus = NotStarted;
     108                 :          0 :     QNetworkReply::NetworkError mError = QNetworkReply::NoError;
     109                 :            : };
     110                 :            : 
     111                 :            : /**
     112                 :            :  * \class QgsNetworkContentFetcherRegistry
     113                 :            :  * \ingroup core
     114                 :            :  * \brief Registry for temporary fetched files
     115                 :            :  *
     116                 :            :  * This provides a simple way of downloading and accessing
     117                 :            :  * remote files during QGIS application running.
     118                 :            :  *
     119                 :            :  * \see QgsFetchedContent
     120                 :            :  *
     121                 :            :  * \since QGIS 3.2
     122                 :            : */
     123                 :            : class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
     124                 :            : {
     125                 :            :     Q_OBJECT
     126                 :            :   public:
     127                 :            :     //! Enum to determine when the download should start
     128                 :            :     enum FetchingMode
     129                 :            :     {
     130                 :            :       DownloadLater,       //!< Do not start immediately the download to properly connect the fetched signal
     131                 :            :       DownloadImmediately, //!< The download will start immediately, not need to run QgsFecthedContent::download()
     132                 :            :     };
     133                 :            :     Q_ENUM( FetchingMode )
     134                 :            : 
     135                 :            :     //! Create the registry for temporary downloaded files
     136                 :          5 :     explicit QgsNetworkContentFetcherRegistry() = default;
     137                 :            : 
     138                 :            :     ~QgsNetworkContentFetcherRegistry() override;
     139                 :            : 
     140                 :            :     /**
     141                 :            :      * \brief Initialize a download for the given URL
     142                 :            :      * \param url the URL to be fetched
     143                 :            :      * \param fetchingMode defines if the download will start immediately or shall be manually triggered
     144                 :            :      * \note If the download starts immediately, it will not redownload any already fetched or currently fetching file.
     145                 :            :      */
     146                 :            :     const QgsFetchedContent *fetch( const QString &url, FetchingMode fetchingMode = DownloadLater );
     147                 :            : 
     148                 :            : #ifndef SIP_RUN
     149                 :            : 
     150                 :            :     /**
     151                 :            :      * \brief Returns a QFile from a local file or to a temporary file previously fetched by the registry
     152                 :            :      * \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
     153                 :            :      */
     154                 :            :     QFile *localFile( const QString &filePathOrUrl );
     155                 :            : #endif
     156                 :            : 
     157                 :            :     /**
     158                 :            :      * \brief Returns the path to a local file or to a temporary file previously fetched by the registry
     159                 :            :      * \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
     160                 :            :      */
     161                 :            :     QString localPath( const QString &filePathOrUrl );
     162                 :            : 
     163                 :            :   private:
     164                 :            :     QMap<QString, QgsFetchedContent *> mFileRegistry;
     165                 :            : 
     166                 :            : };
     167                 :            : 
     168                 :            : #endif // QGSNETWORKCONTENTFETCHERREGISTRY_H

Generated by: LCOV version 1.14