File indexing completed on 2025-01-19 03:53:53

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-06-21
0007  * Description : a simple web browser dialog based on Qt WebEngine.
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "webbrowserdlg.h"
0016 #include "digikam_config.h"
0017 
0018 // Qt includes
0019 
0020 #include <QGridLayout>
0021 #include <QApplication>
0022 #include <QStyle>
0023 #include <QIcon>
0024 #include <QToolBar>
0025 #include <QDesktopServices>
0026 #include <QNetworkCookieJar>
0027 
0028 #ifdef HAVE_QWEBENGINE
0029 #   include <QWebEngineView>
0030 #   include <QWebEnginePage>
0031 #   include <QWebEngineProfile>
0032 #   include <QWebEngineCookieStore>
0033 #   include <QWebEngineFindTextResult>
0034 #else
0035 #   include <qwebview.h>
0036 #endif
0037 
0038 // KDE includes
0039 
0040 #include <kconfiggroup.h>
0041 #include <ksharedconfig.h>
0042 #include <klocalizedstring.h>
0043 
0044 // Local includes
0045 
0046 #include "statusprogressbar.h"
0047 #include "searchtextbar.h"
0048 #include "dxmlguiwindow.h"
0049 #include "digikam_debug.h"
0050 
0051 namespace Digikam
0052 {
0053 
0054 class Q_DECL_HIDDEN WebBrowserDlg::Private
0055 {
0056 public:
0057 
0058     explicit Private()
0059       : browser    (nullptr),
0060         toolbar    (nullptr),
0061         progressbar(nullptr),
0062         searchbar  (nullptr)
0063     {
0064     }
0065 
0066 public:
0067 
0068     QUrl               home;
0069 
0070 #ifdef HAVE_QWEBENGINE
0071 
0072     QWebEngineView*    browser;
0073 
0074 #else
0075 
0076     QWebView*          browser;
0077 
0078 #endif
0079 
0080     QToolBar*          toolbar;
0081     StatusProgressBar* progressbar;
0082     SearchTextBar*     searchbar;
0083 };
0084 
0085 WebBrowserDlg::WebBrowserDlg(const QUrl& url, QWidget* const parent, bool hideDeskBrowser)
0086     : QDialog(parent),
0087       d      (new Private)
0088 {
0089     setModal(false);
0090     setAttribute(Qt::WA_DeleteOnClose, true);
0091 
0092     d->home    = url;
0093 
0094 #ifdef HAVE_QWEBENGINE
0095 
0096     d->browser = new QWebEngineView(this);
0097     d->browser->page()->profile()->cookieStore()->deleteAllCookies();
0098 
0099 #else
0100 
0101     d->browser = new QWebView(this);
0102     d->browser->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
0103     d->browser->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
0104 
0105 #endif
0106 
0107     // --------------------------
0108 
0109     d->toolbar = new QToolBar(this);
0110     d->toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
0111 
0112 #ifdef HAVE_QWEBENGINE
0113 
0114     d->toolbar->addAction(d->browser->pageAction(QWebEnginePage::Back));
0115     d->toolbar->addAction(d->browser->pageAction(QWebEnginePage::Forward));
0116     d->toolbar->addAction(d->browser->pageAction(QWebEnginePage::Reload));
0117     d->toolbar->addAction(d->browser->pageAction(QWebEnginePage::Stop));
0118 
0119 #else
0120 
0121     d->toolbar->addAction(d->browser->pageAction(QWebPage::Back));
0122     d->toolbar->addAction(d->browser->pageAction(QWebPage::Forward));
0123     d->toolbar->addAction(d->browser->pageAction(QWebPage::Reload));
0124     d->toolbar->addAction(d->browser->pageAction(QWebPage::Stop));
0125 
0126 #endif
0127 
0128     QAction* const gohome  = new QAction(QIcon::fromTheme(QLatin1String("go-home")),
0129                                          i18n("Home"), this);
0130     gohome->setToolTip(i18n("Go back to Home page"));
0131     d->toolbar->addAction(gohome);
0132 
0133     QAction* const deskweb = new QAction(QIcon::fromTheme(QLatin1String("internet-web-browser")),
0134                                          i18n("Desktop Browser"), this);
0135     deskweb->setToolTip(i18n("Open Home page with default desktop Web browser"));
0136 
0137     if (!hideDeskBrowser)
0138     {
0139         d->toolbar->addAction(deskweb);
0140     }
0141 
0142     // --------------------------
0143 
0144     d->searchbar   = new SearchTextBar(this, QLatin1String("WebBrowserDlgSearchBar"));
0145     d->searchbar->setHighlightOnResult(true);
0146 
0147     d->progressbar = new StatusProgressBar(this);
0148     d->progressbar->setProgressTotalSteps(100);
0149     d->progressbar->setAlignment(Qt::AlignLeft);
0150     d->progressbar->setNotify(false);
0151 
0152     // ----------------------
0153 
0154     QGridLayout* const grid = new QGridLayout(this);
0155     grid->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0156                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0157     grid->addWidget(d->toolbar,     0, 0, 1, 1);
0158     grid->addWidget(d->searchbar,   0, 2, 1, 1);
0159     grid->addWidget(d->browser,     1, 0, 1, 3);
0160     grid->addWidget(d->progressbar, 2, 0, 1, 3);
0161     grid->setColumnStretch(1, 10);
0162     grid->setRowStretch(1, 10);
0163     setLayout(grid);
0164 
0165     // ----------------------
0166 /*
0167     connect(d->browser, SIGNAL(iconChanged(const QIcon&)),
0168             this, SLOT(slotIconChanged(const QIcon&)));
0169 */
0170     connect(d->browser, SIGNAL(titleChanged(QString)),
0171             this, SLOT(slotTitleChanged(QString)));
0172 
0173     connect(d->browser, SIGNAL(urlChanged(QUrl)),
0174             this, SLOT(slotUrlChanged(QUrl)));
0175 
0176     connect(d->browser, SIGNAL(loadStarted()),
0177             this, SLOT(slotLoadingStarted()));
0178 
0179     connect(d->browser, SIGNAL(loadFinished(bool)),
0180             this, SLOT(slotLoadingFinished(bool)));
0181 
0182     connect(d->searchbar, SIGNAL(signalSearchTextSettings(SearchTextSettings)),
0183             this, SLOT(slotSearchTextChanged(SearchTextSettings)));
0184 
0185     connect(d->browser, SIGNAL(loadProgress(int)),
0186             d->progressbar, SLOT(setProgressValue(int)));
0187 
0188     connect(gohome, SIGNAL(triggered()),
0189             this, SLOT(slotGoHome()));
0190 
0191     connect(deskweb, SIGNAL(triggered()),
0192             this, SLOT(slotDesktopWebBrowser()));
0193 
0194     // ----------------------
0195 
0196     KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("WebBrowserDlg"));
0197 
0198     winId();
0199     windowHandle()->resize(800, 600);
0200     DXmlGuiWindow::restoreWindowSize(windowHandle(), group);
0201     resize(windowHandle()->size());
0202 
0203     slotGoHome();
0204 }
0205 
0206 WebBrowserDlg::~WebBrowserDlg()
0207 {
0208     delete d;
0209 }
0210 
0211 void WebBrowserDlg::closeEvent(QCloseEvent* e)
0212 {
0213     KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("WebBrowserDlg"));
0214     DXmlGuiWindow::saveWindowSize(windowHandle(), group);
0215 
0216     Q_EMIT closeView(false);
0217     e->accept();
0218 }
0219 
0220 void WebBrowserDlg::slotUrlChanged(const QUrl& url)
0221 {
0222     d->progressbar->setText(url.toString());
0223     Q_EMIT urlChanged(url);
0224 }
0225 
0226 void WebBrowserDlg::slotTitleChanged(const QString& title)
0227 {
0228     setWindowTitle(title);
0229 }
0230 
0231 void WebBrowserDlg::slotIconChanged(const QIcon& icon)
0232 {
0233     setWindowIcon(icon);
0234 }
0235 
0236 void WebBrowserDlg::slotLoadingStarted()
0237 {
0238     d->progressbar->setProgressBarMode(StatusProgressBar::ProgressBarMode);
0239 }
0240 
0241 void WebBrowserDlg::slotLoadingFinished(bool b)
0242 {
0243     QString curUrl = d->browser->url().toString();
0244 
0245     d->progressbar->setProgressBarMode(StatusProgressBar::TextMode, curUrl);
0246 
0247     if (!b)
0248     {
0249         d->progressbar->setText(i18n("Cannot load page %1", curUrl));
0250     }
0251 }
0252 
0253 void WebBrowserDlg::slotSearchTextChanged(const SearchTextSettings& settings)
0254 {
0255 
0256 #ifdef HAVE_QWEBENGINE
0257 
0258 #   if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0259 
0260     d->browser->findText(settings.text,
0261                          (settings.caseSensitive == Qt::CaseSensitive) ? QWebEnginePage::FindCaseSensitively
0262                                                                        : QWebEnginePage::FindFlags(),
0263                          [this](const QWebEngineFindTextResult& result)
0264                             {
0265                                 d->searchbar->slotSearchResult(result.activeMatch());
0266                             }
0267                         );
0268 
0269 #   else
0270 
0271     d->browser->findText(settings.text,
0272                          (settings.caseSensitive == Qt::CaseSensitive) ? QWebEnginePage::FindCaseSensitively
0273                                                                        : QWebEnginePage::FindFlags(),
0274                          [this](bool found)
0275                             {
0276                                 d->searchbar->slotSearchResult(found);
0277                             }
0278                         );
0279 
0280 #   endif
0281 
0282 #else
0283 
0284     bool found = d->browser->findText(
0285                     settings.text,
0286                     (settings.caseSensitive == Qt::CaseInsensitive) ? QWebPage::FindCaseSensitively
0287                                                                     : QWebPage::FindFlags());
0288     d->searchbar->slotSearchResult(found);
0289 
0290 #endif
0291 
0292 }
0293 
0294 void WebBrowserDlg::slotGoHome()
0295 {
0296     d->browser->setUrl(d->home);
0297 }
0298 
0299 void WebBrowserDlg::slotDesktopWebBrowser()
0300 {
0301     QDesktopServices::openUrl(d->home);
0302 }
0303 
0304 } // namespace Digikam
0305 
0306 #include "moc_webbrowserdlg.cpp"