UBDownloadManager.h 4.82 KB
Newer Older
1 2 3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef UBDOWNLOADMANAGER_H
#define UBDOWNLOADMANAGER_H

#include <QObject>
#include <QString>
#include <QVector>
#include <QMutex>
22
#include <QDropEvent>
23 24 25 26 27 28 29

#include "UBDownloadThread.h"

#include "network/UBHttpGet.h"

#define     SIMULTANEOUS_DOWNLOAD       2   // Maximum 5 because of QNetworkAccessManager limitation!!!

30
struct sDownloadFileDesc
31
{
32 33 34 35 36
    enum eDestinations {
        board //default for sDownloadFileDesc
        , library
        , graphicsWidget
    };
37 38 39 40 41 42 43 44
    //creating constructor to make sure to have default values
    sDownloadFileDesc() :
        dest(board)
      , id(0)
      , totalSize(0)
      , currentSize(0)
      , modal(false)
      , isBackground(false)
45 46 47
      , dropActions(Qt::IgnoreAction)
      , dropMouseButtons(Qt::NoButton)
      , dropModifiers(Qt::NoModifier)
48 49 50
    {;}

    eDestinations dest;
51 52 53 54 55
    QString name;
    int id;
    int totalSize;
    int currentSize;
    QString url;
56
    QString contentTypeHeader;
57 58 59 60
    bool modal;
    QPointF pos;        // For board drop only
    QSize size;         // For board drop only
    bool isBackground;  // For board drop only
61 62 63 64 65

    QPoint dropPoint;    //For widget's Drop event
    Qt::DropActions dropActions; //For widget's Drop event
    Qt::MouseButtons dropMouseButtons; //For widget's Drop event
    Qt::KeyboardModifiers dropModifiers; //For widget's Drop event
66
};
67

68

69 70 71 72 73 74 75 76 77 78
class UBDownloadHttpFile : public UBHttpGet
{
    Q_OBJECT
public:
    UBDownloadHttpFile(int fileId, QObject* parent=0);
    ~UBDownloadHttpFile();

signals:
    void downloadProgress(int id, qint64 current,qint64 total);
    void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
79
    void downloadError(int id);
80 81 82 83 84 85 86 87 88 89 90 91 92

private slots:
    void onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
    void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

private:
    int mId;
};

class UBDownloadManager : public QObject
{
    Q_OBJECT
public:
93 94
    UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager");
    ~UBDownloadManager();
95
    static UBDownloadManager* downloadManager();
96
    int addFileToDownload(sDownloadFileDesc desc);
97 98 99
    QVector<sDownloadFileDesc> currentDownloads();
    QVector<sDownloadFileDesc> pendingDownloads();
    void cancelDownloads();
100
    void cancelDownload(int id);
101

102 103
    static void destroy();

104 105 106 107
signals:
    void fileAddedToDownload();
    void downloadUpdated(int id, qint64 crnt, qint64 total);
    void downloadFinished(int id);
108
    void downloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
109
    void downloadFinished(bool pSuccess, sDownloadFileDesc desc, QByteArray pData);
110 111
    void downloadModalFinished();
    void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
112
    void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
113
    void cancelAllDownloads();
114
    void allDownloadsFinished();
115 116 117 118 119

private slots:
    void onUpdateDownloadLists();
    void onDownloadProgress(int id, qint64 received, qint64 total);
    void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
120
    void onDownloadError(int id);
121 122 123 124 125 126 127

private:
    void init();
    void updateDownloadOrder();
    void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1);
    void startFileDownload(sDownloadFileDesc desc);
    void checkIfModalRemains();
128
    void finishDownloads(bool cancel=false);
129 130 131 132 133 134 135 136 137 138 139

    /** The current downloads */
    QVector<sDownloadFileDesc> mCrntDL;
    /** The pending downloads */
    QVector<sDownloadFileDesc> mPendingDL;
    /** Pending download mutex */
    QMutex mMutex;
    /** The last file ID */
    int mLastID;
    /** The current download availability (-1 = free, otherwise the file ID is recorded)*/
    QVector<int> mDLAvailability;
140 141
    /** A map containing the replies of the GET operations */
    QMap<int, QNetworkReply*> mReplies;
142 143 144
};

#endif // UBDOWNLOADMANAGER_H