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