File indexing completed on 2024-05-19 04:59:18

0001 /* ============================================================
0002 * StatusBarIcons - Extra icons in statusbar for Falkon
0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "sbi_imagesicon.h"
0019 #include "browserwindow.h"
0020 #include "tabwidget.h"
0021 #include "tabbedwebview.h"
0022 #include "webpage.h"
0023 #include "mainapplication.h"
0024 
0025 #include <QGraphicsColorizeEffect>
0026 #include <QSettings>
0027 #include <QMenu>
0028 
0029 SBI_ImagesIcon::SBI_ImagesIcon(BrowserWindow* window, const QString &settingsPath)
0030     : SBI_Icon(window, settingsPath)
0031 {
0032     setObjectName(QSL("sbi_imagesicon"));
0033     setCursor(Qt::PointingHandCursor);
0034     setToolTip(tr("Modify images loading settings per-site and globally"));
0035 
0036     m_icon = QIcon::fromTheme(QSL("image-x-generic"), QIcon(QSL(":sbi/data/images.png")));
0037     setPixmap(m_icon.pixmap(16));
0038 
0039     QSettings settings(m_settingsFile, QSettings::IniFormat);
0040     settings.beginGroup("StatusBarIcons_Images");
0041     m_loadingImages = settings.value("LoadImages", true).toBool();
0042     settings.endGroup();
0043 
0044     mApp->webSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages);
0045 
0046     updateIcon();
0047 
0048     connect(m_window->tabWidget(), SIGNAL(currentChanged(int)), this, SLOT(updateIcon()));
0049     connect(this, &ClickableLabel::clicked, this, &SBI_ImagesIcon::showMenu);
0050 }
0051 
0052 void SBI_ImagesIcon::showMenu(const QPoint &point)
0053 {
0054     QFont boldFont = font();
0055     boldFont.setBold(true);
0056 
0057     QMenu menu;
0058     menu.addAction(m_icon, tr("Current Page Settings"))->setFont(boldFont);
0059 
0060     if (testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages)) {
0061         menu.addAction(tr("Disable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages);
0062     }
0063     else {
0064         menu.addAction(tr("Enable loading images (temporarily)"), this, &SBI_ImagesIcon::toggleLoadingImages);
0065     }
0066 
0067     menu.addSeparator();
0068     menu.addAction(m_icon, tr("Global Settings"))->setFont(boldFont);
0069 
0070     QAction* act = menu.addAction(tr("Automatically load images"));
0071     act->setCheckable(true);
0072     act->setChecked(m_loadingImages);
0073     connect(act, &QAction::toggled, this, &SBI_ImagesIcon::setGlobalLoadingImages);
0074 
0075     menu.exec(point);
0076 }
0077 
0078 void SBI_ImagesIcon::toggleLoadingImages()
0079 {
0080     bool current = testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages);
0081     setCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages, !current);
0082 
0083     // We should reload page on disabling images
0084     if (current) {
0085         m_window->weView()->reload();
0086     }
0087 
0088     updateIcon();
0089 }
0090 
0091 void SBI_ImagesIcon::setGlobalLoadingImages(bool enable)
0092 {
0093     // Save it permanently
0094     QSettings settings(m_settingsFile, QSettings::IniFormat);
0095     settings.beginGroup("StatusBarIcons_Images");
0096     settings.setValue("LoadImages", enable);
0097     settings.endGroup();
0098 
0099     // Switch it in websettings
0100     m_loadingImages = enable;
0101     mApp->webSettings()->setAttribute(QWebEngineSettings::AutoLoadImages, m_loadingImages);
0102     updateIcon();
0103 
0104     // We should reload page on disabling images
0105     if (!enable) {
0106         m_window->weView()->reload();
0107     }
0108 }
0109 
0110 void SBI_ImagesIcon::updateIcon()
0111 {
0112     if (testCurrentPageWebAttribute(QWebEngineSettings::AutoLoadImages)) {
0113         setGraphicsEffect(nullptr);
0114     }
0115     else {
0116         auto* effect = new QGraphicsColorizeEffect(this);
0117         effect->setColor(Qt::gray);
0118         setGraphicsEffect(effect);
0119     }
0120 }