File: angelfishwebprofile.cpp

package info (click to toggle)
angelfish 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,740 kB
  • sloc: cpp: 2,703; xml: 611; javascript: 273; makefile: 16; sh: 11; sql: 7
file content (106 lines) | stat: -rw-r--r-- 3,806 bytes parent folder | download
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
//
// SPDX-License-Identifier: GPL-2.0-or-later

#include "angelfishwebprofile.h"

#include <KLocalizedString>
#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickWindow>
#include <QWebEngineNotification>

#include <KNotification>

#include "downloadmanager.h"

class QQuickWebEngineDownloadRequest : public DownloadItem
{
};

AngelfishWebProfile::AngelfishWebProfile(QObject *parent)
    : QQuickWebEngineProfile(parent)
    , m_questionLoader(nullptr)
    , m_urlInterceptor(nullptr)
{
    connect(this, &QQuickWebEngineProfile::downloadRequested, this, &AngelfishWebProfile::handleDownload);
    connect(this, &QQuickWebEngineProfile::downloadFinished, this, &AngelfishWebProfile::handleDownloadFinished);
    connect(this, &QQuickWebEngineProfile::presentNotification, this, &AngelfishWebProfile::showNotification);
}

void AngelfishWebProfile::handleDownload(QQuickWebEngineDownloadRequest *downloadItem)
{
    DownloadItem *download = qobject_cast<DownloadItem *>(downloadItem);

    // if we don't accept the request right away, it will be deleted
    download->accept();
    // therefore just stop the download again as quickly as possible,
    // and ask the user for confirmation
    download->pause();

    DownloadManager::instance().addDownload(std::unique_ptr<DownloadItem>(downloadItem));

    if (m_questionLoader) {
        m_questionLoader->setProperty("source", QStringLiteral("qrc:/DownloadQuestion.qml"));

        if (auto *question = m_questionLoader->property("item").value<QQuickItem *>()) {
            question->setProperty("download", QVariant::fromValue(downloadItem));
            question->setVisible(true);
        }
    }
}

void AngelfishWebProfile::handleDownloadFinished(DownloadItem *downloadItem)
{
    QQuickWindow *window = m_questionLoader->window();
    const auto passiveNotification = [window](const QString &text) {
        QMetaObject::invokeMethod(window, "showPassiveNotification", Q_ARG(QVariant, text), Q_ARG(QVariant, {}), Q_ARG(QVariant, {}), Q_ARG(QVariant, {}));
    };

    switch (downloadItem->state()) {
    case DownloadItem::DownloadCompleted:
        qDebug() << "download finished";
        passiveNotification(i18n("Download finished"));
        break;
    case DownloadItem::DownloadInterrupted:
        qDebug() << "Download failed";
        passiveNotification(i18n("Download failed"));
        qDebug() << "Download interrupt reason:" << downloadItem->interruptReasonString();
        break;
    case DownloadItem::DownloadCancelled:
        qDebug() << "Download cancelled by the user";
        break;
    default:
        break;
    }
}

void AngelfishWebProfile::showNotification(QWebEngineNotification *webNotification)
{
    auto *notification = new KNotification(QStringLiteral("web-notification"));
    notification->setComponentName(QStringLiteral("angelfish"));
    notification->setTitle(webNotification->title());
    notification->setText(webNotification->message());
    notification->setPixmap(QPixmap::fromImage(webNotification->icon()));

    connect(notification, &KNotification::closed, webNotification, &QWebEngineNotification::close);

    auto defaultAction = notification->addDefaultAction(i18n("Open"));
    connect(defaultAction, &KNotificationAction::activated, webNotification, &QWebEngineNotification::click);

    notification->sendEvent();
}

QWebEngineUrlRequestInterceptor *AngelfishWebProfile::urlInterceptor() const
{
    return m_urlInterceptor;
}

void AngelfishWebProfile::setUrlInterceptor(QWebEngineUrlRequestInterceptor *urlRequestInterceptor)
{
    setUrlRequestInterceptor(urlRequestInterceptor);
    m_urlInterceptor = urlRequestInterceptor;
    Q_EMIT urlInterceptorChanged();
}

#include "moc_angelfishwebprofile.cpp"