webenginepartdownloadmanager.h 2.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the KDE project.
 *
 * Copyright (C) 2017 Stefano Crocco <posta@stefanocrocco.it>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library 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 Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef WEBENGINEPARTDOWNLOADMANAGER_H
#define WEBENGINEPARTDOWNLOADMANAGER_H

#include <QObject>
#include <QVector>
26
#include <QWebEngineDownloadItem>
27
#include <QTemporaryDir>
28
#include <QDateTime>
29 30

#include <KJob>
31 32 33 34 35 36 37 38 39 40

class WebEnginePage;

class WebEnginePartDownloadManager : public QObject
{
    Q_OBJECT

public:
    static WebEnginePartDownloadManager* instance();

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
41
    ~WebEnginePartDownloadManager() override;
42

43 44
private:
    WebEnginePartDownloadManager();
Stefano Crocco's avatar
Stefano Crocco committed
45
    void downloadBlob(QWebEngineDownloadItem *it);
46
    QString generateBlobTempFileName(QString const &suggestedName, const QString &ext) const;
47

48 49 50 51
public Q_SLOTS:
    void addPage(WebEnginePage *page);
    void removePage(QObject *page);

52 53
private Q_SLOTS:
    void performDownload(QWebEngineDownloadItem *it);
54
    void saveBlob(QWebEngineDownloadItem *it);
55 56
    void openBlob(QWebEngineDownloadItem *it, WebEnginePage *page);
    void blobDownloadedToFile(QWebEngineDownloadItem *it, WebEnginePage *page);
57

58 59
private:
    QVector<WebEnginePage*> m_pages;
60
    QTemporaryDir m_tempDownloadDir;
61 62
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
class WebEngineBlobDownloadJob : public KJob
{
    Q_OBJECT

public:
    WebEngineBlobDownloadJob(QWebEngineDownloadItem *it, QObject *parent = nullptr);
    ~WebEngineBlobDownloadJob(){}

    void start() override;

    QString errorString() const override;

protected:
    bool doKill() override;
    bool doResume() override;
    bool doSuspend() override;

private slots:
    void downloadProgressed(quint64 received, quint64 total);
    void stateChanged(QWebEngineDownloadItem::DownloadState state);
Stefano Crocco's avatar
Stefano Crocco committed
83 84
    void startDownloading();
    void downloadFinished();
85 86 87 88

private:

    QWebEngineDownloadItem *m_downloadItem;
89
    QDateTime m_startTime;
90 91
};

92
#endif // WEBENGINEPARTDOWNLOADMANAGER_H