File indexing completed on 2024-05-12 16:23:40
0001 // SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: GPL-2.0-or-later 0004 0005 #include "angelfishwebprofile.h" 0006 0007 #include <KLocalizedString> 0008 #include <QGuiApplication> 0009 #include <QQuickItem> 0010 #include <QQuickWindow> 0011 #include <QWebEngineNotification> 0012 0013 #include <KNotification> 0014 0015 #include "downloadmanager.h" 0016 0017 #include <private/qquickwebenginedownloadrequest_p.h> 0018 0019 AngelfishWebProfile::AngelfishWebProfile(QObject *parent) 0020 : QQuickWebEngineProfile(parent) 0021 , m_questionLoader(nullptr) 0022 , m_urlInterceptor(nullptr) 0023 { 0024 connect(this, &QQuickWebEngineProfile::downloadRequested, this, &AngelfishWebProfile::handleDownload); 0025 connect(this, &QQuickWebEngineProfile::downloadFinished, this, &AngelfishWebProfile::handleDownloadFinished); 0026 connect(this, &QQuickWebEngineProfile::presentNotification, this, &AngelfishWebProfile::showNotification); 0027 } 0028 0029 void AngelfishWebProfile::handleDownload(DownloadItem *downloadItem) 0030 { 0031 // if we don't accept the request right away, it will be deleted 0032 downloadItem->accept(); 0033 // therefore just stop the download again as quickly as possible, 0034 // and ask the user for confirmation 0035 downloadItem->pause(); 0036 0037 DownloadManager::instance().addDownload(std::unique_ptr<DownloadItem>(downloadItem)); 0038 0039 if (m_questionLoader) { 0040 m_questionLoader->setProperty("source", QStringLiteral("qrc:/DownloadQuestion.qml")); 0041 0042 if (auto *question = m_questionLoader->property("item").value<QQuickItem *>()) { 0043 question->setProperty("download", QVariant::fromValue(downloadItem)); 0044 question->setVisible(true); 0045 } 0046 } 0047 } 0048 0049 void AngelfishWebProfile::handleDownloadFinished(DownloadItem *downloadItem) 0050 { 0051 QQuickWindow *window = m_questionLoader->window(); 0052 const auto passiveNotification = [window](const QString &text) { 0053 QMetaObject::invokeMethod(window, "showPassiveNotification", Q_ARG(QVariant, text), Q_ARG(QVariant, {}), Q_ARG(QVariant, {}), Q_ARG(QVariant, {})); 0054 }; 0055 0056 switch (downloadItem->state()) { 0057 case DownloadItem::DownloadCompleted: 0058 qDebug() << "download finished"; 0059 passiveNotification(i18n("Download finished")); 0060 break; 0061 case DownloadItem::DownloadInterrupted: 0062 qDebug() << "Download failed"; 0063 passiveNotification(i18n("Download failed")); 0064 qDebug() << "Download interrupt reason:" << downloadItem->interruptReasonString(); 0065 break; 0066 case DownloadItem::DownloadCancelled: 0067 qDebug() << "Download cancelled by the user"; 0068 break; 0069 default: 0070 break; 0071 } 0072 } 0073 0074 void AngelfishWebProfile::showNotification(QWebEngineNotification *webNotification) 0075 { 0076 auto *notification = new KNotification(QStringLiteral("web-notification")); 0077 notification->setComponentName(QStringLiteral("angelfish")); 0078 notification->setTitle(webNotification->title()); 0079 notification->setText(webNotification->message()); 0080 notification->setPixmap(QPixmap::fromImage(webNotification->icon())); 0081 0082 connect(notification, &KNotification::closed, webNotification, &QWebEngineNotification::close); 0083 0084 auto defaultAction = notification->addDefaultAction(i18n("Open")); 0085 connect(defaultAction, &KNotificationAction::activated, webNotification, &QWebEngineNotification::click); 0086 0087 notification->sendEvent(); 0088 } 0089 0090 QWebEngineUrlRequestInterceptor *AngelfishWebProfile::urlInterceptor() const 0091 { 0092 return m_urlInterceptor; 0093 } 0094 0095 void AngelfishWebProfile::setUrlInterceptor(QWebEngineUrlRequestInterceptor *urlRequestInterceptor) 0096 { 0097 setUrlRequestInterceptor(urlRequestInterceptor); 0098 m_urlInterceptor = urlRequestInterceptor; 0099 Q_EMIT urlInterceptorChanged(); 0100 } 0101 0102 #include "moc_angelfishwebprofile.cpp"