Anihilate static singleton webfactory.

This commit is contained in:
Martin Rotter 2017-07-28 07:31:57 +02:00
parent 5d47c53b3c
commit 267a4149b3
14 changed files with 65 additions and 40 deletions

View File

@ -55,13 +55,21 @@
#include <QTimer>
#include <QFileDialog>
#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);

View File

@ -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,

View File

@ -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"),

View File

@ -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);

View File

@ -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<Message>>("QList<Message>");
qRegisterMetaType<QList<RootItem*>>("QList<RootItem*>");
// 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();
}

View File

@ -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 <QSessionManager>
#include <QProcess>
@ -59,7 +60,7 @@ Application::Application(const QString& id, int& argc, char** argv)
m_feedReader(nullptr),
m_updateFeedsLock(nullptr), m_userActions(QList<QAction*>()), 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<QAction*> 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() {

View File

@ -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;

View File

@ -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() {

View File

@ -67,7 +67,7 @@ class AdBlockManager : public QObject {
AdBlockCustomList* customList() const;
inline AdBlockIcon* adblockIcon() const {
inline AdBlockIcon* adBlockIcon() const {
return m_adblockIcon;
}

View File

@ -24,11 +24,9 @@
#include <QUrl>
#include <QDesktopServices>
Q_GLOBAL_STATIC(WebFactory, qz_webfactory)
WebFactory::WebFactory()
: m_escapes(QMap<QString, QString>()), m_deEscapes(QMap<QString, QString>()) {
WebFactory::WebFactory(QObject* parent)
: QObject(parent), m_escapes(QMap<QString, QString>()), m_deEscapes(QMap<QString, QString>()) {
}
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("<[^>]*>")));
}

View File

@ -18,6 +18,8 @@
#ifndef WEBFACTORY_H
#define WEBFACTORY_H
#include <QObject>
#include "core/messagesmodel.h"
#include <QMap>
@ -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);

View File

@ -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);

View File

@ -19,6 +19,7 @@
#include "miscellaneous/textfactory.h"
#include "network-web/webfactory.h"
#include "miscellaneous/application.h"
#include <QDomDocument>
@ -53,14 +54,14 @@ QList<Message> 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;
}

View File

@ -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 <QDomDocument>
@ -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;
}