From 267a4149b3ce66e8947dbeaa19f603bc1c2d7fd8 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 28 Jul 2017 07:31:57 +0200 Subject: [PATCH] Anihilate static singleton webfactory. --- src/gui/dialogs/formmain.cpp | 16 +++++++++++---- src/gui/dialogs/formupdate.cpp | 2 +- src/gui/messagesview.cpp | 4 ++-- src/gui/webviewer.cpp | 2 +- src/main.cpp | 25 ++++++++++++++++------- src/miscellaneous/application.cpp | 11 +++++++--- src/miscellaneous/application.h | 3 +++ src/network-web/adblock/adblockdialog.cpp | 2 +- src/network-web/adblock/adblockmanager.h | 2 +- src/network-web/webfactory.cpp | 10 ++------- src/network-web/webfactory.h | 13 ++++++------ src/services/standard/atomparser.cpp | 5 +++-- src/services/standard/rdfparser.cpp | 5 +++-- src/services/standard/rssparser.cpp | 5 +++-- 14 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/gui/dialogs/formmain.cpp b/src/gui/dialogs/formmain.cpp index 9b27cf6d1..78a363b50 100755 --- a/src/gui/dialogs/formmain.cpp +++ b/src/gui/dialogs/formmain.cpp @@ -55,13 +55,21 @@ #include #include +#if defined (USE_WEBENGINE) +#include "network-web/adblock/adblockmanager.h" +#include "network-web/adblock/adblockicon.h" +#endif + FormMain::FormMain(QWidget* parent, Qt::WindowFlags f) : QMainWindow(parent, f), m_ui(new Ui::FormMain) { m_ui->setupUi(this); - qApp->setMainForm(this); +#if defined (USE_WEBENGINE) + m_ui->m_menuWebBrowserTabs->addAction(AdBlockManager::instance()->adBlockIcon()); +#endif + // Add these actions to the list of actions of the main window. // This allows to use actions via shortcuts // even if main menu is not visible. @@ -724,7 +732,7 @@ void FormMain::showUpdates() { } void FormMain::showWiki() { - if (!WebFactory::instance()->openUrlInExternalBrowser(APP_URL_WIKI)) { + if (!qApp->web()->openUrlInExternalBrowser(APP_URL_WIKI)) { qApp->showGuiMessage(tr("Cannot open external browser"), tr("Cannot open external browser. Navigate to application website manually."), QSystemTrayIcon::Warning, this, true); @@ -739,7 +747,7 @@ void FormMain::showAddAccountDialog() { } void FormMain::reportABug() { - if (!WebFactory::instance()->openUrlInExternalBrowser(QSL(APP_URL_ISSUES_NEW))) { + if (!qApp->web()->openUrlInExternalBrowser(QSL(APP_URL_ISSUES_NEW))) { qApp->showGuiMessage(tr("Cannot open external browser"), tr("Cannot open external browser. Navigate to application website manually."), QSystemTrayIcon::Warning, this, true); @@ -747,7 +755,7 @@ void FormMain::reportABug() { } void FormMain::donate() { - if (!WebFactory::instance()->openUrlInExternalBrowser(QSL(APP_DONATE_URL))) { + if (!qApp->web()->openUrlInExternalBrowser(QSL(APP_DONATE_URL))) { qApp->showGuiMessage(tr("Cannot open external browser"), tr("Cannot open external browser. Navigate to application website manually."), QSystemTrayIcon::Warning, this, true); diff --git a/src/gui/dialogs/formupdate.cpp b/src/gui/dialogs/formupdate.cpp index 69065f3cd..404814de9 100755 --- a/src/gui/dialogs/formupdate.cpp +++ b/src/gui/dialogs/formupdate.cpp @@ -250,7 +250,7 @@ void FormUpdate::startUpdate() { else { // Self-update and package are not available. - if (!WebFactory::instance()->openUrlInExternalBrowser(url_file)) { + if (!qApp->web()->openUrlInExternalBrowser(url_file)) { qApp->showGuiMessage(tr("Cannot update application"), tr("Cannot navigate to installation file. Check new installation downloads manually on project website."), QSystemTrayIcon::Warning, diff --git a/src/gui/messagesview.cpp b/src/gui/messagesview.cpp index 3499ec83c..f204d7da6 100755 --- a/src/gui/messagesview.cpp +++ b/src/gui/messagesview.cpp @@ -286,7 +286,7 @@ void MessagesView::openSelectedSourceMessagesExternally() { foreach (const QModelIndex& index, selectionModel()->selectedRows()) { const QString link = m_sourceModel->messageAt(m_proxyModel->mapToSource(index).row()).m_url; - if (!WebFactory::instance()->openUrlInExternalBrowser(link)) { + if (!qApp->web()->openUrlInExternalBrowser(link)) { qApp->showGuiMessage(tr("Problem with starting external web browser"), tr("External web browser could not be started."), QSystemTrayIcon::Critical); @@ -316,7 +316,7 @@ void MessagesView::sendSelectedMessageViaEmail() { if (selectionModel()->selectedRows().size() == 1) { const Message message = m_sourceModel->messageAt(m_proxyModel->mapToSource(selectionModel()->selectedRows().at(0)).row()); - if (!WebFactory::instance()->sendMessageViaEmail(message)) { + if (!qApp->web()->sendMessageViaEmail(message)) { MessageBox::show(this, QMessageBox::Critical, tr("Problem with starting external e-mail client"), diff --git a/src/gui/webviewer.cpp b/src/gui/webviewer.cpp index b929890df..73e6db4d3 100755 --- a/src/gui/webviewer.cpp +++ b/src/gui/webviewer.cpp @@ -146,7 +146,7 @@ void WebViewer::contextMenuEvent(QContextMenuEvent* event) { event->accept(); QMenu* menu = page()->createStandardContextMenu(); - menu->addAction(AdBlockManager::instance()->adblockIcon()); + menu->addAction(AdBlockManager::instance()->adBlockIcon()); const QPoint pos = event->globalPos(); QPoint p(pos.x(), pos.y() + 1); diff --git a/src/main.cpp b/src/main.cpp index 90c498cef..b0464ea6b 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,13 +57,17 @@ int main(int argc, char* argv[]) { //: Use ISO 639-1 code here combined with ISO 3166-1 (alpha-2) code. //: Examples: "cs", "en", "it", "cs_CZ", "en_GB", "en_US". QObject::tr("LANG_ABBREV"); + //: Name of translator - optional. QObject::tr("LANG_AUTHOR"); + // Ensure that ini format is used as application settings storage on Mac OS. QSettings::setDefaultFormat(QSettings::IniFormat); + // Setup debug output system. qInstallMessageHandler(Debugging::debugHandler); // Instantiate base application object. + Application application(APP_LOW_NAME, argc, argv); qDebug("Instantiated Application class."); @@ -82,31 +86,37 @@ int main(int argc, char* argv[]) { QApplication::setAttribute(Qt::AA_DontShowIconsInMenus); disableWindowTabbing(); #endif + // Register needed metatypes. qRegisterMetaType>("QList"); qRegisterMetaType>("QList"); - // Just call this instance, so that is is created in main GUI thread. - WebFactory::instance(); + // Add an extra path for non-system icon themes and set current icon theme // and skin. qApp->icons()->setupSearchPaths(); qApp->icons()->loadCurrentIconTheme(); qApp->skins()->loadCurrentSkin(); + // These settings needs to be set before any QSettings object. Application::setApplicationName(APP_NAME); Application::setApplicationVersion(APP_VERSION); Application::setOrganizationDomain(APP_URL); Application::setWindowIcon(QIcon(APP_ICON_PATH)); - // Load activated accounts. + + // Load activated accounts. qApp->feedReader()->feedsModel()->loadActivatedServiceAccounts(); - // Setup single-instance behavior. + + // Setup single-instance behavior. QObject::connect(&application, &Application::messageReceived, &application, &Application::processExecutionMessage); qDebug().nospace() << "Creating main application form in thread: \'" << QThread::currentThreadId() << "\'."; - // Instantiate main application window. + + // Instantiate main application window. FormMain main_window; - // Set correct information for main window. + + // Set correct information for main window. main_window.setWindowTitle(APP_LONG_NAME); - // Now is a good time to initialize dynamic keyboard shortcuts. + + // Now is a good time to initialize dynamic keyboard shortcuts. DynamicShortcuts::load(qApp->userActions()); // Display main window. @@ -140,6 +150,7 @@ int main(int argc, char* argv[]) { } qApp->mainForm()->tabWidget()->feedMessageViewer()->feedsView()->loadAllExpandStates(); + // Enter global event loop. return Application::exec(); } diff --git a/src/miscellaneous/application.cpp b/src/miscellaneous/application.cpp index 04328ac47..9cb97336a 100755 --- a/src/miscellaneous/application.cpp +++ b/src/miscellaneous/application.cpp @@ -33,6 +33,7 @@ #include "services/standard/standardserviceentrypoint.h" #include "services/tt-rss/ttrssserviceentrypoint.h" #include "services/owncloud/owncloudserviceentrypoint.h" +#include "network-web/webfactory.h" #include #include @@ -59,7 +60,7 @@ Application::Application(const QString& id, int& argc, char** argv) m_feedReader(nullptr), m_updateFeedsLock(nullptr), m_userActions(QList()), m_mainForm(nullptr), - m_trayIcon(nullptr), m_settings(nullptr), m_system(nullptr), m_skins(nullptr), + m_trayIcon(nullptr), m_settings(nullptr), m_webFactory(new WebFactory(this)), m_system(nullptr), m_skins(nullptr), m_localization(nullptr), m_icons(nullptr), m_database(nullptr), m_downloadManager(nullptr), m_shouldRestart(false) { connect(this, &Application::aboutToQuit, this, &Application::onAboutToQuit); connect(this, &Application::commitDataRequest, this, &Application::onCommitData); @@ -92,7 +93,7 @@ QList Application::userActions() { m_userActions = m_mainForm->allActions(); #if defined(USE_WEBENGINE) - m_userActions.append(AdBlockManager::instance()->adblockIcon()); + m_userActions.append(AdBlockManager::instance()->adBlockIcon()); #endif } @@ -111,7 +112,11 @@ bool Application::isFirstRun(const QString& version) { else { return false; - } + } +} + +WebFactory* Application::web() { + return m_webFactory; } SystemFactory* Application::system() { diff --git a/src/miscellaneous/application.h b/src/miscellaneous/application.h index b685720fb..a96e9d9ed 100755 --- a/src/miscellaneous/application.h +++ b/src/miscellaneous/application.h @@ -47,6 +47,7 @@ class QAction; class Mutex; class QWebEngineDownloadItem; class FeedReader; +class WebFactory; #if defined(USE_WEBENGINE) class NetworkUrlInterceptor; @@ -72,6 +73,7 @@ class Application : public QtSingleApplication { // Check whether GIVEN VERSION of the application starts for the first time. bool isFirstRun(const QString& version); + WebFactory* web(); SystemFactory* system(); SkinFactory* skins(); Localization* localization(); @@ -172,6 +174,7 @@ class Application : public QtSingleApplication { FormMain* m_mainForm; SystemTrayIcon* m_trayIcon; Settings* m_settings; + WebFactory* m_webFactory; SystemFactory* m_system; SkinFactory* m_skins; Localization* m_localization; diff --git a/src/network-web/adblock/adblockdialog.cpp b/src/network-web/adblock/adblockdialog.cpp index ec439c1c3..f7e79f166 100755 --- a/src/network-web/adblock/adblockdialog.cpp +++ b/src/network-web/adblock/adblockdialog.cpp @@ -144,7 +144,7 @@ void AdBlockDialog::aboutToShowMenu() { } void AdBlockDialog::learnAboutRules() { - WebFactory::instance()->openUrlInExternalBrowser(QSL(ADBLOCK_HOWTO_FILTERS)); + qApp->web()->openUrlInExternalBrowser(QSL(ADBLOCK_HOWTO_FILTERS)); } void AdBlockDialog::loadSubscriptions() { diff --git a/src/network-web/adblock/adblockmanager.h b/src/network-web/adblock/adblockmanager.h index 5ae33687c..eba68ee1c 100755 --- a/src/network-web/adblock/adblockmanager.h +++ b/src/network-web/adblock/adblockmanager.h @@ -67,7 +67,7 @@ class AdBlockManager : public QObject { AdBlockCustomList* customList() const; - inline AdBlockIcon* adblockIcon() const { + inline AdBlockIcon* adBlockIcon() const { return m_adblockIcon; } diff --git a/src/network-web/webfactory.cpp b/src/network-web/webfactory.cpp index a8e0871e5..8cf6966e9 100755 --- a/src/network-web/webfactory.cpp +++ b/src/network-web/webfactory.cpp @@ -24,11 +24,9 @@ #include #include -Q_GLOBAL_STATIC(WebFactory, qz_webfactory) - -WebFactory::WebFactory() - : m_escapes(QMap()), m_deEscapes(QMap()) { +WebFactory::WebFactory(QObject* parent) + : QObject(parent), m_escapes(QMap()), m_deEscapes(QMap()) { } WebFactory::~WebFactory() { @@ -70,10 +68,6 @@ bool WebFactory::openUrlInExternalBrowser(const QString& url) { } } -WebFactory* WebFactory::instance() { - return qz_webfactory(); -} - QString WebFactory::stripTags(QString text) { return text.remove(QRegExp(QSL("<[^>]*>"))); } diff --git a/src/network-web/webfactory.h b/src/network-web/webfactory.h index ff50fe889..297cd9e26 100755 --- a/src/network-web/webfactory.h +++ b/src/network-web/webfactory.h @@ -18,6 +18,8 @@ #ifndef WEBFACTORY_H #define WEBFACTORY_H +#include + #include "core/messagesmodel.h" #include @@ -25,10 +27,12 @@ class QWebEngineSettings; -class WebFactory { +class WebFactory : public QObject { + Q_OBJECT + public: - // Constructor. - explicit WebFactory(); + // Constructor. + explicit WebFactory(QObject* parent = nullptr); // Destructor. virtual ~WebFactory(); @@ -42,9 +46,6 @@ class WebFactory { QString toSecondLevelDomain(const QUrl& url); - // Singleton getter. - static WebFactory* instance(); - public slots: // Opens given string URL in external browser. bool openUrlInExternalBrowser(const QString& url); diff --git a/src/services/standard/atomparser.cpp b/src/services/standard/atomparser.cpp index 0de775579..b7dc36e70 100755 --- a/src/services/standard/atomparser.cpp +++ b/src/services/standard/atomparser.cpp @@ -19,6 +19,7 @@ #include "miscellaneous/textfactory.h" #include "network-web/webfactory.h" +#include "miscellaneous/application.h" #include "exceptions/applicationexception.h" @@ -64,9 +65,9 @@ Message AtomParser::extractMessage(const QDomElement& msg_element, QDateTime cur } // Title is not empty, description does not matter. - new_message.m_title = WebFactory::instance()->stripTags(title); + new_message.m_title = qApp->web()->stripTags(title); new_message.m_contents = summary; - new_message.m_author = WebFactory::instance()->escapeHtml(messageAuthor(msg_element)); + new_message.m_author = qApp->web()->escapeHtml(messageAuthor(msg_element)); QString updated = textsFromPath(msg_element, m_atomNamespace, QSL("updated"), true).join(QSL(", ")); // Deal with creation date. new_message.m_created = TextFactory::parseDateTime(updated); diff --git a/src/services/standard/rdfparser.cpp b/src/services/standard/rdfparser.cpp index c3f1e914f..43d38f9f0 100755 --- a/src/services/standard/rdfparser.cpp +++ b/src/services/standard/rdfparser.cpp @@ -19,6 +19,7 @@ #include "miscellaneous/textfactory.h" #include "network-web/webfactory.h" +#include "miscellaneous/application.h" #include @@ -53,14 +54,14 @@ QList RdfParser::parseXmlData(const QString& data) { else { // Title is empty but description is not. - new_message.m_title = WebFactory::instance()->escapeHtml(WebFactory::instance()->stripTags(elem_description.simplified())); + new_message.m_title = qApp->web()->escapeHtml(qApp->web()->stripTags(elem_description.simplified())); new_message.m_contents = elem_description; } } else { // Title is really not empty, description does not matter. - new_message.m_title = WebFactory::instance()->escapeHtml(WebFactory::instance()->stripTags(elem_title)); + new_message.m_title = qApp->web()->escapeHtml(qApp->web()->stripTags(elem_title)); new_message.m_contents = elem_description; } diff --git a/src/services/standard/rssparser.cpp b/src/services/standard/rssparser.cpp index 3a9cd8426..63124f9de 100755 --- a/src/services/standard/rssparser.cpp +++ b/src/services/standard/rssparser.cpp @@ -20,6 +20,7 @@ #include "miscellaneous/textfactory.h" #include "network-web/webfactory.h" #include "miscellaneous/iofactory.h" +#include "miscellaneous/application.h" #include "exceptions/applicationexception.h" #include @@ -64,14 +65,14 @@ Message RssParser::extractMessage(const QDomElement& msg_element, QDateTime curr else { // Title is empty but description is not. - new_message.m_title = WebFactory::instance()->stripTags(elem_description.simplified()); + new_message.m_title = qApp->web()->stripTags(elem_description.simplified()); new_message.m_contents = elem_description; } } else { // Title is really not empty, description does not matter. - new_message.m_title = WebFactory::instance()->stripTags(elem_title); + new_message.m_title = qApp->web()->stripTags(elem_title); new_message.m_contents = elem_description; }