File indexing completed on 2025-01-26 04:47:03

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "webengineframe.h"
0008 #include "akrwebengineviewer.h"
0009 #include "articleviewer-ng/webengine/articleviewerwebenginewidgetng.h"
0010 
0011 #include <KIO/FavIconRequestJob>
0012 
0013 #include <QVBoxLayout>
0014 
0015 using namespace Akregator;
0016 
0017 WebEngineFrame::WebEngineFrame(KActionCollection *ac, QWidget *parent)
0018     : Frame(parent)
0019 {
0020     auto layout = new QVBoxLayout(this);
0021     layout->setContentsMargins({});
0022     setRemovable(true);
0023     auto viewer = new Akregator::AkrWebEngineViewer(ac, this);
0024     mArticleViewerWidgetNg = new Akregator::ArticleViewerWebEngineWidgetNg(viewer, ac, this);
0025 
0026     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::titleChanged, this, &WebEngineFrame::slotTitleChanged);
0027 
0028     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::loadProgress, this, &WebEngineFrame::slotProgressChanged);
0029 
0030     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::signalOpenUrlRequest, this, &WebEngineFrame::signalOpenUrlRequest);
0031 
0032     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::loadStarted, this, &WebEngineFrame::slotLoadStarted);
0033 
0034     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::loadFinished, this, &WebEngineFrame::slotLoadFinished);
0035 
0036     connect(mArticleViewerWidgetNg->articleViewerNg(), &ArticleViewerWebEngine::showStatusBarMessage, this, &WebEngineFrame::showStatusBarMessage);
0037 
0038     connect(mArticleViewerWidgetNg->articleViewerNg(),
0039             &ArticleViewerWebEngine::webPageMutedOrAudibleChanged,
0040             this,
0041             &WebEngineFrame::slotWebPageMutedOrAudibleChanged);
0042 
0043     layout->addWidget(mArticleViewerWidgetNg);
0044 }
0045 
0046 WebEngineFrame::~WebEngineFrame() = default;
0047 
0048 void WebEngineFrame::slotWebPageMutedOrAudibleChanged(bool isAudioMuted, bool wasRecentlyAudible)
0049 {
0050     Q_EMIT webPageMutedOrAudibleChanged(this, isAudioMuted, wasRecentlyAudible);
0051 }
0052 
0053 void WebEngineFrame::slotLoadFinished()
0054 {
0055     Q_EMIT signalCompleted(this);
0056 }
0057 
0058 void WebEngineFrame::slotLoadStarted()
0059 {
0060     Q_EMIT signalStarted(this);
0061 }
0062 
0063 void WebEngineFrame::slotProgressChanged(int progress)
0064 {
0065     Q_EMIT signalLoadingProgress(this, progress);
0066 }
0067 
0068 void WebEngineFrame::slotTitleChanged(const QString &title)
0069 {
0070     slotSetTitle(title);
0071 }
0072 
0073 QUrl WebEngineFrame::url() const
0074 {
0075     return mArticleViewerWidgetNg->articleViewerNg()->url();
0076 }
0077 
0078 void WebEngineFrame::loadUrl(const QUrl &url)
0079 {
0080     auto job = new KIO::FavIconRequestJob(url);
0081     connect(job, &KIO::FavIconRequestJob::result, this, [job, this](KJob *) {
0082         if (!job->error()) {
0083             Q_EMIT signalIconChanged(this, QIcon(job->iconFile()));
0084         }
0085     });
0086 
0087     mArticleViewerWidgetNg->articleViewerNg()->load(url);
0088 }
0089 
0090 bool WebEngineFrame::openUrl(const OpenUrlRequest &request)
0091 {
0092     const QUrl url = request.url();
0093     loadUrl(url);
0094     return true;
0095 }
0096 
0097 void WebEngineFrame::loadConfig(const KConfigGroup &config, const QString &prefix)
0098 {
0099     const QString url = config.readEntry(QStringLiteral("url").prepend(prefix), QString());
0100     const qreal zf = config.readEntry(QStringLiteral("zoom").prepend(prefix), 1.0);
0101     mArticleViewerWidgetNg->articleViewerNg()->setZoomFactor(zf);
0102     loadUrl(QUrl(url));
0103 }
0104 
0105 bool WebEngineFrame::saveConfig(KConfigGroup &config, const QString &prefix)
0106 {
0107     const QString urlPath(url().url());
0108     if (urlPath != QLatin1StringView("about:blank")) {
0109         config.writeEntry(QStringLiteral("url").prepend(prefix), urlPath);
0110         config.writeEntry(QStringLiteral("zoom").prepend(prefix), zoomFactor());
0111         return true;
0112     } else {
0113         return false;
0114     }
0115 }
0116 
0117 void WebEngineFrame::slotCopyInFrame(int frameId)
0118 {
0119     if (frameId != id()) {
0120         return;
0121     }
0122     mArticleViewerWidgetNg->articleViewerNg()->slotCopy();
0123 }
0124 
0125 void WebEngineFrame::slotPrintInFrame(int frameId)
0126 {
0127     if (frameId != id()) {
0128         return;
0129     }
0130     mArticleViewerWidgetNg->slotPrint();
0131 }
0132 
0133 void WebEngineFrame::slotPrintPreviewInFrame(int frameId)
0134 {
0135     if (frameId != id()) {
0136         return;
0137     }
0138     mArticleViewerWidgetNg->slotPrintPreview();
0139 }
0140 
0141 void WebEngineFrame::slotFindTextInFrame(int frameId)
0142 {
0143     if (frameId != id()) {
0144         return;
0145     }
0146     mArticleViewerWidgetNg->slotFind();
0147 }
0148 
0149 void WebEngineFrame::slotTextToSpeechInFrame(int frameId)
0150 {
0151     if (frameId != id()) {
0152         return;
0153     }
0154     mArticleViewerWidgetNg->slotSpeakText();
0155 }
0156 
0157 void WebEngineFrame::slotZoomChangeInFrame(int frameId, qreal value)
0158 {
0159     if (frameId != id()) {
0160         return;
0161     }
0162     mArticleViewerWidgetNg->articleViewerNg()->setZoomFactor(value);
0163 }
0164 
0165 void WebEngineFrame::slotReload()
0166 {
0167     mArticleViewerWidgetNg->articleViewerNg()->reload();
0168 }
0169 
0170 void WebEngineFrame::slotStop()
0171 {
0172     mArticleViewerWidgetNg->articleViewerNg()->stop();
0173 }
0174 
0175 qreal WebEngineFrame::zoomFactor() const
0176 {
0177     return mArticleViewerWidgetNg->articleViewerNg()->zoomFactor();
0178 }
0179 
0180 void WebEngineFrame::slotSaveLinkAsInFrame(int frameId)
0181 {
0182     if (frameId != id()) {
0183         return;
0184     }
0185     mArticleViewerWidgetNg->articleViewerNg()->slotSaveLinkAs();
0186 }
0187 
0188 void WebEngineFrame::slotCopyLinkAsInFrame(int frameId)
0189 {
0190     if (frameId != id()) {
0191         return;
0192     }
0193     mArticleViewerWidgetNg->articleViewerNg()->slotCopyLinkAddress();
0194 }
0195 
0196 void WebEngineFrame::slotSaveImageOnDiskInFrame(int frameId)
0197 {
0198     if (frameId != id()) {
0199         return;
0200     }
0201     mArticleViewerWidgetNg->articleViewerNg()->slotSaveImageOnDiskInFrame();
0202 }
0203 
0204 void WebEngineFrame::slotCopyImageLocationInFrame(int frameId)
0205 {
0206     if (frameId != id()) {
0207         return;
0208     }
0209     mArticleViewerWidgetNg->articleViewerNg()->slotCopyImageLocationInFrame();
0210 }
0211 
0212 void WebEngineFrame::slotMute(int frameId, bool mute)
0213 {
0214     if (frameId != id()) {
0215         return;
0216     }
0217     mArticleViewerWidgetNg->articleViewerNg()->slotMute(mute);
0218 }
0219 
0220 #include "moc_webengineframe.cpp"