UBDownloadManager.h 5.69 KB
Newer Older
1
/*
2
 * Copyright (C) 2012 Webdoc SA
3
 *
4 5 6 7 8 9 10 11 12 13
 * This file is part of Open-Sankoré.
 *
 * Open-Sankoré is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation, version 2,
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
 * Open-Sankoré is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
17
 *
18 19 20
 * You should have received a copy of the GNU Library General Public
 * License along with Open-Sankoré; if not, see
 * <http://www.gnu.org/licenses/>.
21
 */
22 23


24 25 26 27 28 29 30
#ifndef UBDOWNLOADMANAGER_H
#define UBDOWNLOADMANAGER_H

#include <QObject>
#include <QString>
#include <QVector>
#include <QMutex>
31
#include <QDropEvent>
32 33 34 35 36 37 38

#include "UBDownloadThread.h"

#include "network/UBHttpGet.h"

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

39
struct sDownloadFileDesc
40
{
41 42 43 44 45
    enum eDestinations {
        board //default for sDownloadFileDesc
        , library
        , graphicsWidget
    };
46 47 48 49 50 51 52 53
    //creating constructor to make sure to have default values
    sDownloadFileDesc() :
        dest(board)
      , id(0)
      , totalSize(0)
      , currentSize(0)
      , modal(false)
      , isBackground(false)
54 55 56
      , dropActions(Qt::IgnoreAction)
      , dropMouseButtons(Qt::NoButton)
      , dropModifiers(Qt::NoModifier)
57 58 59
    {;}

    eDestinations dest;
60 61 62 63
    QString name;
    int id;
    int totalSize;
    int currentSize;
64
    QString srcUrl;
65
    QString originalSrcUrl;
66
    QString contentTypeHeader;
67 68 69 70
    bool modal;
    QPointF pos;        // For board drop only
    QSize size;         // For board drop only
    bool isBackground;  // For board drop only
71 72 73 74 75

    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
76
};
77

78

79 80 81 82 83 84 85 86 87
class UBDownloadHttpFile : public UBHttpGet
{
    Q_OBJECT
public:
    UBDownloadHttpFile(int fileId, QObject* parent=0);
    ~UBDownloadHttpFile();

signals:
    void downloadProgress(int id, qint64 current,qint64 total);
88
    void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
89
    void downloadError(int id);
90 91 92 93 94 95 96 97 98

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;
};

99 100 101 102 103 104
class UBAsyncLocalFileDownloader : public QThread
{
    Q_OBJECT
public:
    UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0);

105
    UBAsyncLocalFileDownloader *download();    
106
    void run();
107
    void abort();
108 109 110 111 112 113 114 115

signals:
    void finished(QString srcUrl, QString resUrl);
    void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);


private:
    sDownloadFileDesc mDesc;
116
    bool m_bAborting;
117 118 119 120
    QString mFrom;
    QString mTo;
};

121 122 123 124
class UBDownloadManager : public QObject
{
    Q_OBJECT
public:
125 126
    UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager");
    ~UBDownloadManager();
127
    static UBDownloadManager* downloadManager();
128
    int addFileToDownload(sDownloadFileDesc desc);
129 130 131
    QVector<sDownloadFileDesc> currentDownloads();
    QVector<sDownloadFileDesc> pendingDownloads();
    void cancelDownloads();
132
    void cancelDownload(int id);
133

134 135
    static void destroy();

136 137 138 139
signals:
    void fileAddedToDownload();
    void downloadUpdated(int id, qint64 crnt, qint64 total);
    void downloadFinished(int id);
140
    void downloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData);
141
    void downloadFinished(bool pSuccess, sDownloadFileDesc desc, QByteArray pData);
142
    void downloadModalFinished();
143
    void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
144
    void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QString pTitle);
145
    void cancelAllDownloads();
146
    void allDownloadsFinished();
147 148 149 150

private slots:
    void onUpdateDownloadLists();
    void onDownloadProgress(int id, qint64 received, qint64 total);
151
    void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
152
    void onDownloadError(int id);
153 154 155 156 157 158 159

private:
    void init();
    void updateDownloadOrder();
    void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1);
    void startFileDownload(sDownloadFileDesc desc);
    void checkIfModalRemains();
160
    void finishDownloads(bool cancel=false);
161 162 163 164 165 166 167 168 169 170 171

    /** 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;
172
    /** A map containing the replies of the GET operations */
173
    QMap<int, QObject*> mDownloads;
174 175 176
};

#endif // UBDOWNLOADMANAGER_H