1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2015 The Qt Company Ltd. 4 ** Contact: http://www.qt.io/licensing/ 5 ** 6 ** This file is part of the demonstration applications of the Qt Toolkit. 7 ** 8 ** $QT_BEGIN_LICENSE:LGPL21$ 9 ** Commercial License Usage 10 ** Licensees holding valid commercial Qt licenses may use this file in 11 ** accordance with the commercial license agreement provided with the 12 ** Software or, alternatively, in accordance with the terms contained in 13 ** a written agreement between you and The Qt Company. For licensing terms 14 ** and conditions see http://www.qt.io/terms-conditions. For further 15 ** information use the contact form at http://www.qt.io/contact-us. 16 ** 17 ** GNU Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 2.1 or version 3 as published by the Free 20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and 21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the 22 ** following information to ensure the GNU Lesser General Public License 23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and 24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 25 ** 26 ** As a special exception, The Qt Company gives you certain additional 27 ** rights. These rights are described in The Qt Company LGPL Exception 28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 29 ** 30 ** $QT_END_LICENSE$ 31 ** 32 ****************************************************************************/ 33 34 #ifndef DOWNLOADMANAGER_H 35 #define DOWNLOADMANAGER_H 36 37 #include "ui_downloads.h" 38 #include "ui_downloaditem.h" 39 40 #include <QtNetwork/QNetworkReply> 41 42 #include <QtCore/QFile> 43 #include <QtCore/QTime> 44 45 class DownloadItem : public QWidget, public Ui_DownloadItem 46 { 47 Q_OBJECT 48 49 signals: 50 void statusChanged(); 51 52 public: 53 DownloadItem(QNetworkReply *reply = 0, bool requestFileName = false, QWidget *parent = 0); 54 bool downloading() const; 55 bool downloadedSuccessfully() const; 56 57 QUrl m_url; 58 59 QFile m_output; 60 QNetworkReply *m_reply; 61 62 private slots: 63 void stop(); 64 void tryAgain(); 65 void open(); 66 67 void downloadReadyRead(); 68 void error(QNetworkReply::NetworkError code); 69 void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); 70 void metaDataChanged(); 71 void finished(); 72 73 private: 74 void getFileName(); 75 void init(); 76 void updateInfoLabel(); 77 QString dataString(int size) const; 78 79 QString saveFileName(const QString &directory) const; 80 81 bool m_requestFileName; 82 qint64 m_bytesReceived; 83 QTime m_downloadTime; 84 }; 85 86 class AutoSaver; 87 class DownloadModel; 88 QT_BEGIN_NAMESPACE 89 class QFileIconProvider; 90 QT_END_NAMESPACE 91 92 class DownloadManager : public QDialog, public Ui_DownloadDialog 93 { 94 Q_OBJECT 95 Q_PROPERTY(RemovePolicy removePolicy READ removePolicy WRITE setRemovePolicy) 96 Q_ENUMS(RemovePolicy) 97 98 public: 99 enum RemovePolicy { 100 Never, 101 Exit, 102 SuccessFullDownload 103 }; 104 105 DownloadManager(QWidget *parent = 0); 106 ~DownloadManager(); 107 int activeDownloads() const; 108 109 RemovePolicy removePolicy() const; 110 void setRemovePolicy(RemovePolicy policy); 111 112 public slots: 113 void download(const QNetworkRequest &request, bool requestFileName = false); 114 inline void download(const QUrl &url, bool requestFileName = false) 115 { download(QNetworkRequest(url), requestFileName); } 116 void handleUnsupportedContent(QNetworkReply *reply, bool requestFileName = false); 117 void cleanup(); 118 119 private slots: 120 void save() const; 121 void updateRow(); 122 123 private: 124 void addItem(DownloadItem *item); 125 void updateItemCount(); 126 void load(); 127 128 AutoSaver *m_autoSaver; 129 DownloadModel *m_model; 130 QNetworkAccessManager *m_manager; 131 QFileIconProvider *m_iconProvider; 132 QList<DownloadItem*> m_downloads; 133 RemovePolicy m_removePolicy; 134 friend class DownloadModel; 135 }; 136 137 class DownloadModel : public QAbstractListModel 138 { 139 friend class DownloadManager; 140 Q_OBJECT 141 142 public: 143 DownloadModel(DownloadManager *downloadManager, QObject *parent = 0); 144 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 145 int rowCount(const QModelIndex &parent = QModelIndex()) const; 146 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); 147 148 private: 149 DownloadManager *m_downloadManager; 150 151 }; 152 153 #endif // DOWNLOADMANAGER_H 154 155