|
| 1 | +/*************************************************************************** |
| 2 | + qgsnetworkcontentfetchertask.h |
| 3 | + ------------------- |
| 4 | + begin : March, 2018 |
| 5 | + copyright : (C) 2018 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 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 | + |
| 20 | +#ifndef QGSNETWORKCONTENTFETCHERTASK_H |
| 21 | +#define QGSNETWORKCONTENTFETCHERTASK_H |
| 22 | + |
| 23 | +#include "qgstaskmanager.h" |
| 24 | +#include "qgis_core.h" |
| 25 | +#include <QNetworkRequest> |
| 26 | + |
| 27 | +class QgsNetworkContentFetcher; |
| 28 | +class QNetworkReply; |
| 29 | + |
| 30 | +/** |
| 31 | + * \class QgsNetworkContentFetcherTask |
| 32 | + * \ingroup core |
| 33 | + * \brief Handles HTTP network content fetching in a background task. |
| 34 | + * |
| 35 | + * Provides a simple method for fetching remote HTTP content in a QgsTask. |
| 36 | + * Url redirects are automatically handled. |
| 37 | + * |
| 38 | + * After constructing a QgsNetworkContentFetcherTask, callers should |
| 39 | + * connect to the QgsNetworkContentFetcherTask::fetched signal. They can |
| 40 | + * then safely access the network reply() from the connected slot |
| 41 | + * without danger of the task being first removed by the QgsTaskManager. |
| 42 | + * |
| 43 | + * \see QgsNetworkContentFetcher |
| 44 | + * |
| 45 | + * \since QGIS 3.2 |
| 46 | +*/ |
| 47 | +class CORE_EXPORT QgsNetworkContentFetcherTask : public QgsTask |
| 48 | +{ |
| 49 | + Q_OBJECT |
| 50 | + |
| 51 | + public: |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructor for a QgsNetworkContentFetcherTask which fetches |
| 55 | + * the specified \a url. |
| 56 | + */ |
| 57 | + QgsNetworkContentFetcherTask( const QUrl &url ); |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor for a QgsNetworkContentFetcherTask which fetches |
| 61 | + * the specified network \a request. |
| 62 | + */ |
| 63 | + QgsNetworkContentFetcherTask( const QNetworkRequest &request ); |
| 64 | + |
| 65 | + ~QgsNetworkContentFetcherTask(); |
| 66 | + |
| 67 | + bool run() override; |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the network reply. Ownership is not transferred. |
| 71 | + * |
| 72 | + * May return nullptr if the request has not yet completed. |
| 73 | + */ |
| 74 | + QNetworkReply *reply(); |
| 75 | + |
| 76 | + signals: |
| 77 | + |
| 78 | + /** |
| 79 | + * Emitted when the network content has been fetched, regardless |
| 80 | + * of whether the fetch was successful or not. |
| 81 | + * |
| 82 | + * Users of QgsNetworkContentFetcherTask should connect to this signal, |
| 83 | + * and from the associated slot they can then safely access the network reply() |
| 84 | + * without danger of the task being first removed by the QgsTaskManager. |
| 85 | + */ |
| 86 | + void fetched(); |
| 87 | + |
| 88 | + private: |
| 89 | + |
| 90 | + QNetworkRequest mRequest; |
| 91 | + QgsNetworkContentFetcher *mFetcher = nullptr; |
| 92 | + |
| 93 | +}; |
| 94 | + |
| 95 | +#endif //QGSNETWORKCONTENTFETCHERTASK_H |
0 commit comments