From 9fda55d86d63ff77350ccb451c03458877f2ca9d Mon Sep 17 00:00:00 2001 From: nowrep Date: Thu, 13 Mar 2014 12:06:28 +0100 Subject: [PATCH] [StatusBarIcons] Fixed crash when restoring session All icons have now same parent SBI_Icon --- src/plugins/StatusBarIcons/StatusBarIcons.pro | 6 +- src/plugins/StatusBarIcons/sbi_icon.cpp | 59 +++++++++++++++++++ src/plugins/StatusBarIcons/sbi_icon.h | 46 +++++++++++++++ src/plugins/StatusBarIcons/sbi_imagesicon.cpp | 18 ++---- src/plugins/StatusBarIcons/sbi_imagesicon.h | 13 +--- .../StatusBarIcons/sbi_javascripticon.cpp | 26 ++++---- .../StatusBarIcons/sbi_javascripticon.h | 13 +--- .../StatusBarIcons/sbi_networkicon.cpp | 5 +- src/plugins/StatusBarIcons/sbi_networkicon.h | 7 +-- 9 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 src/plugins/StatusBarIcons/sbi_icon.cpp create mode 100644 src/plugins/StatusBarIcons/sbi_icon.h diff --git a/src/plugins/StatusBarIcons/StatusBarIcons.pro b/src/plugins/StatusBarIcons/StatusBarIcons.pro index 8fa7efa87..864f5e8c9 100644 --- a/src/plugins/StatusBarIcons/StatusBarIcons.pro +++ b/src/plugins/StatusBarIcons/StatusBarIcons.pro @@ -10,7 +10,8 @@ SOURCES += statusbariconsplugin.cpp \ sbi_proxywidget.cpp \ sbi_networkicondialog.cpp \ sbi_networkmanager.cpp \ - sbi_settingsdialog.cpp + sbi_settingsdialog.cpp \ + sbi_icon.cpp HEADERS += statusbariconsplugin.h \ sbi_iconsmanager.h \ @@ -21,7 +22,8 @@ HEADERS += statusbariconsplugin.h \ sbi_proxywidget.h \ sbi_networkicondialog.h \ sbi_networkmanager.h \ - sbi_settingsdialog.h + sbi_settingsdialog.h \ + sbi_icon.h RESOURCES += statusbaricons.qrc diff --git a/src/plugins/StatusBarIcons/sbi_icon.cpp b/src/plugins/StatusBarIcons/sbi_icon.cpp new file mode 100644 index 000000000..f65bbad7a --- /dev/null +++ b/src/plugins/StatusBarIcons/sbi_icon.cpp @@ -0,0 +1,59 @@ +/* ============================================================ +* StatusBarIcons - Extra icons in statusbar for QupZilla +* Copyright (C) 2014 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "sbi_icon.h" +#include "browserwindow.h" +#include "tabbedwebview.h" +#include "webpage.h" + +SBI_Icon::SBI_Icon(BrowserWindow* window, const QString &settingsPath) + : ClickableLabel(window) + , m_window(window) + , m_settingsFile(settingsPath + QLatin1String("extensions.ini")) +{ +} + +bool SBI_Icon::testCurrentPageWebAttribute(QWebSettings::WebAttribute attr) const +{ + return currentPageSettings() && currentPageSettings()->testAttribute(attr); +} + +void SBI_Icon::setCurrentPageWebAttribute(QWebSettings::WebAttribute attr, bool value) +{ + if (currentPageSettings()) { + currentPageSettings()->setAttribute(attr, value); + } +} + +QWebSettings* SBI_Icon::currentPageSettings() const +{ + if (!m_window->weView()) { + return 0; + } + + return m_window->weView()->page()->settings(); +} + +WebPage* SBI_Icon::currentPage() const +{ + if (!m_window->weView()) { + return 0; + } + + return m_window->weView()->page(); +} + diff --git a/src/plugins/StatusBarIcons/sbi_icon.h b/src/plugins/StatusBarIcons/sbi_icon.h new file mode 100644 index 000000000..65b3ba11f --- /dev/null +++ b/src/plugins/StatusBarIcons/sbi_icon.h @@ -0,0 +1,46 @@ +/* ============================================================ +* StatusBarIcons - Extra icons in statusbar for QupZilla +* Copyright (C) 2014 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef SBI_ICON_H +#define SBI_ICON_H + +#include + +#include "clickablelabel.h" + +class BrowserWindow; +class WebPage; + +class SBI_Icon : public ClickableLabel +{ + Q_OBJECT + +public: + explicit SBI_Icon(BrowserWindow* window, const QString &settingsPath = QString()); + +protected: + bool testCurrentPageWebAttribute(QWebSettings::WebAttribute attr) const; + void setCurrentPageWebAttribute(QWebSettings::WebAttribute attr, bool value); + + QWebSettings* currentPageSettings() const; + WebPage* currentPage() const; + + BrowserWindow* m_window; + QString m_settingsFile; +}; + +#endif // SBI_ICON_H diff --git a/src/plugins/StatusBarIcons/sbi_imagesicon.cpp b/src/plugins/StatusBarIcons/sbi_imagesicon.cpp index bb4c3cbac..bb7667f1e 100644 --- a/src/plugins/StatusBarIcons/sbi_imagesicon.cpp +++ b/src/plugins/StatusBarIcons/sbi_imagesicon.cpp @@ -22,14 +22,11 @@ #include "webpage.h" #include -#include #include #include SBI_ImagesIcon::SBI_ImagesIcon(BrowserWindow* window, const QString &settingsPath) - : ClickableLabel(window) - , m_window(window) - , m_settingsFile(settingsPath + "extensions.ini") + : SBI_Icon(window, settingsPath) { setCursor(Qt::PointingHandCursor); setToolTip(tr("Modify images loading settings per-site and globally")); @@ -58,7 +55,7 @@ void SBI_ImagesIcon::showMenu(const QPoint &point) QMenu menu; menu.addAction(m_icon, tr("Current page settings"))->setFont(boldFont); - if (currentPageSettings()->testAttribute(QWebSettings::AutoLoadImages)) { + if (testCurrentPageWebAttribute(QWebSettings::AutoLoadImages)) { menu.addAction(tr("Disable loading images (temporarily)"), this, SLOT(toggleLoadingImages())); } else { @@ -78,8 +75,8 @@ void SBI_ImagesIcon::showMenu(const QPoint &point) void SBI_ImagesIcon::toggleLoadingImages() { - bool current = currentPageSettings()->testAttribute(QWebSettings::AutoLoadImages); - currentPageSettings()->setAttribute(QWebSettings::AutoLoadImages, !current); + bool current = testCurrentPageWebAttribute(QWebSettings::AutoLoadImages); + setCurrentPageWebAttribute(QWebSettings::AutoLoadImages, !current); // We should reload page on disabling images if (current) { @@ -108,14 +105,9 @@ void SBI_ImagesIcon::setGlobalLoadingImages(bool enable) } } -QWebSettings* SBI_ImagesIcon::currentPageSettings() -{ - return m_window->weView()->page()->settings(); -} - void SBI_ImagesIcon::updateIcon() { - if (currentPageSettings()->testAttribute(QWebSettings::AutoLoadImages)) { + if (testCurrentPageWebAttribute(QWebSettings::AutoLoadImages)) { setGraphicsEffect(0); } else { diff --git a/src/plugins/StatusBarIcons/sbi_imagesicon.h b/src/plugins/StatusBarIcons/sbi_imagesicon.h index a597524e4..4b9c287ac 100644 --- a/src/plugins/StatusBarIcons/sbi_imagesicon.h +++ b/src/plugins/StatusBarIcons/sbi_imagesicon.h @@ -20,13 +20,9 @@ #include -#include "clickablelabel.h" +#include "sbi_icon.h" -class QWebSettings; - -class BrowserWindow; - -class SBI_ImagesIcon : public ClickableLabel +class SBI_ImagesIcon : public SBI_Icon { Q_OBJECT @@ -41,11 +37,6 @@ private slots: void setGlobalLoadingImages(bool enable); private: - QWebSettings* currentPageSettings(); - - BrowserWindow* m_window; - QString m_settingsFile; - QIcon m_icon; bool m_loadingImages; }; diff --git a/src/plugins/StatusBarIcons/sbi_javascripticon.cpp b/src/plugins/StatusBarIcons/sbi_javascripticon.cpp index 4be58afa6..dd1fa4869 100644 --- a/src/plugins/StatusBarIcons/sbi_javascripticon.cpp +++ b/src/plugins/StatusBarIcons/sbi_javascripticon.cpp @@ -27,8 +27,7 @@ #include SBI_JavaScriptIcon::SBI_JavaScriptIcon(BrowserWindow* window) - : ClickableLabel(window) - , m_window(window) + : SBI_Icon(window) { setCursor(Qt::PointingHandCursor); setToolTip(tr("Modify JavaScript settings per-site and globally")); @@ -48,7 +47,7 @@ void SBI_JavaScriptIcon::showMenu(const QPoint &point) QMenu menu; menu.addAction(m_icon, tr("Current page settings"))->setFont(boldFont); - if (currentPageSettings()->testAttribute(QWebSettings::JavascriptEnabled)) { + if (testCurrentPageWebAttribute(QWebSettings::JavascriptEnabled)) { menu.addAction(tr("Disable JavaScript (temporarily)"), this, SLOT(toggleJavaScript())); } else { @@ -56,7 +55,7 @@ void SBI_JavaScriptIcon::showMenu(const QPoint &point) } // JavaScript needs to be always enabled for qupzilla: sites - if (currentPage()->url().scheme() == QLatin1String("qupzilla")) { + if (currentPage() && currentPage()->url().scheme() == QLatin1String("qupzilla")) { menu.actions().at(1)->setEnabled(false); } @@ -68,7 +67,7 @@ void SBI_JavaScriptIcon::showMenu(const QPoint &point) void SBI_JavaScriptIcon::updateIcon() { - if (currentPageSettings()->testAttribute(QWebSettings::JavascriptEnabled)) { + if (testCurrentPageWebAttribute(QWebSettings::JavascriptEnabled)) { setGraphicsEffect(0); } else { @@ -80,7 +79,12 @@ void SBI_JavaScriptIcon::updateIcon() void SBI_JavaScriptIcon::toggleJavaScript() { - bool current = currentPageSettings()->testAttribute(QWebSettings::JavascriptEnabled); + if (!currentPage()) { + return; + + } + + bool current = testCurrentPageWebAttribute(QWebSettings::JavascriptEnabled); currentPage()->setJavaScriptEnabled(!current); m_window->weView()->reload(); @@ -93,13 +97,3 @@ void SBI_JavaScriptIcon::openJavaScriptSettings() JsOptions dialog(m_window); dialog.exec(); } - -WebPage* SBI_JavaScriptIcon::currentPage() -{ - return m_window->weView()->page(); -} - -QWebSettings* SBI_JavaScriptIcon::currentPageSettings() -{ - return currentPage()->settings(); -} diff --git a/src/plugins/StatusBarIcons/sbi_javascripticon.h b/src/plugins/StatusBarIcons/sbi_javascripticon.h index d517a5c7d..8f436ccbc 100644 --- a/src/plugins/StatusBarIcons/sbi_javascripticon.h +++ b/src/plugins/StatusBarIcons/sbi_javascripticon.h @@ -20,14 +20,9 @@ #include -#include "clickablelabel.h" +#include "sbi_icon.h" -class QWebSettings; - -class BrowserWindow; -class WebPage; - -class SBI_JavaScriptIcon : public ClickableLabel +class SBI_JavaScriptIcon : public SBI_Icon { Q_OBJECT @@ -42,10 +37,6 @@ private slots: void openJavaScriptSettings(); private: - WebPage* currentPage(); - QWebSettings* currentPageSettings(); - - BrowserWindow* m_window; QIcon m_icon; }; diff --git a/src/plugins/StatusBarIcons/sbi_networkicon.cpp b/src/plugins/StatusBarIcons/sbi_networkicon.cpp index a44aed816..7d168ab7b 100644 --- a/src/plugins/StatusBarIcons/sbi_networkicon.cpp +++ b/src/plugins/StatusBarIcons/sbi_networkicon.cpp @@ -29,8 +29,7 @@ #include SBI_NetworkIcon::SBI_NetworkIcon(BrowserWindow* window) - : ClickableLabel(window) - , m_window(window) + : SBI_Icon(window) , m_networkConfiguration(new QNetworkConfigurationManager(this)) { setCursor(Qt::PointingHandCursor); @@ -140,5 +139,5 @@ void SBI_NetworkIcon::enterEvent(QEvent* event) { updateToolTip(); - ClickableLabel::enterEvent(event); + SBI_Icon::enterEvent(event); } diff --git a/src/plugins/StatusBarIcons/sbi_networkicon.h b/src/plugins/StatusBarIcons/sbi_networkicon.h index a691893ce..e941f2b7b 100644 --- a/src/plugins/StatusBarIcons/sbi_networkicon.h +++ b/src/plugins/StatusBarIcons/sbi_networkicon.h @@ -20,13 +20,11 @@ #include -#include "clickablelabel.h" +#include "sbi_icon.h" class QNetworkConfigurationManager; -class BrowserWindow; - -class SBI_NetworkIcon : public ClickableLabel +class SBI_NetworkIcon : public SBI_Icon { Q_OBJECT @@ -44,7 +42,6 @@ private: void updateToolTip(); void enterEvent(QEvent* event); - BrowserWindow* m_window; QNetworkConfigurationManager* m_networkConfiguration; };