From e9e9d57dba29723b0ec38891f06e77b6b8f4b69b Mon Sep 17 00:00:00 2001 From: Patrick von Reth Date: Wed, 4 Feb 2015 10:36:21 +0100 Subject: [PATCH] use singleton for snorecore --- src/core/plugins/plugins.cpp | 4 ++-- src/core/plugins/plugins.h | 4 ++-- src/core/settingsdialog.cpp | 17 ++++++++--------- src/core/settingsdialog.h | 3 +-- src/core/snore.cpp | 6 ++++++ src/core/snore.h | 3 ++- src/core/snore_p.cpp | 2 -- src/core/snore_p.h | 8 +------- src/daemon/snorenotify.cpp | 8 +++----- src/daemon/snorenotify.h | 1 - src/daemon/trayicon.cpp | 16 +++++++--------- src/daemon/trayicon.h | 3 +-- src/settings/main.cpp | 4 ++-- 13 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/core/plugins/plugins.cpp b/src/core/plugins/plugins.cpp index 6c2dbb2..013ec37 100644 --- a/src/core/plugins/plugins.cpp +++ b/src/core/plugins/plugins.cpp @@ -61,12 +61,12 @@ bool SnorePlugin::isInitialized() const SnoreCore *SnorePlugin::snore() { - return SnoreCorePrivate::snoreInstance(); + return &SnoreCore::instance(); } const SnoreCore *SnorePlugin::snore() const { - return SnoreCorePrivate::snoreInstance(); + return &SnoreCore::instance(); } QVariant SnorePlugin::value(const QString &key) const diff --git a/src/core/plugins/plugins.h b/src/core/plugins/plugins.h index acd04dd..66547ea 100644 --- a/src/core/plugins/plugins.h +++ b/src/core/plugins/plugins.h @@ -48,8 +48,8 @@ public: virtual bool initialize(); virtual bool deinitialize(); bool isInitialized() const; - SnoreCore *snore(); - const SnoreCore *snore() const; + LIBSNORE_DEPRECATED SnoreCore *snore(); + LIBSNORE_DEPRECATED const SnoreCore *snore() const; const QString &name() const; PluginTypes type() const; diff --git a/src/core/settingsdialog.cpp b/src/core/settingsdialog.cpp index f7841fd..2286a17 100644 --- a/src/core/settingsdialog.cpp +++ b/src/core/settingsdialog.cpp @@ -27,10 +27,9 @@ using namespace Snore; -SettingsDialog::SettingsDialog(SnoreCore *snore, QWidget *parent) : +SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SettingsDialog), - m_snore(snore), m_app(new Application("SnoreSettings", Icon(":/root/snore.png"))), m_alert(new Alert("Test", Icon(":/root/snore.png"))) { @@ -38,7 +37,7 @@ SettingsDialog::SettingsDialog(SnoreCore *snore, QWidget *parent) : m_app->addAlert(*m_alert); - for (auto widget : snore->settingWidgets()) { + for (auto widget : SnoreCore::instance().settingWidgets()) { ui->tabWidget->addTab(widget, widget->name()); m_tabs.append(widget); } @@ -52,24 +51,24 @@ SettingsDialog::~SettingsDialog() void Snore::SettingsDialog::on_pushButton_clicked() { - if (!m_snore->aplications().contains(m_app->name())) { - m_snore->registerApplication(*m_app); + if (!SnoreCore::instance().aplications().contains(m_app->name())) { + SnoreCore::instance().registerApplication(*m_app); } Notification noti(*m_app, *m_alert, "Hello World", "This is Snore
" "Project Website
", Icon(":/root/snore.png")); noti.addAction(Action(1, "Test Action")); - m_snore->broadcastNotification(noti); + SnoreCore::instance().broadcastNotification(noti); } void SettingsDialog::load() { snoreDebug(SNORE_DEBUG) << "loading"; ui->primaryBackendComboBox->clear(); - QStringList list = m_snore->pluginNames(SnorePlugin::BACKEND); + QStringList list = SnoreCore::instance().pluginNames(SnorePlugin::BACKEND); ui->primaryBackendComboBox->addItems(list); - ui->primaryBackendComboBox->setCurrentIndex(list.indexOf(m_snore->primaryNotificationBackend())); + ui->primaryBackendComboBox->setCurrentIndex(list.indexOf(SnoreCore::instance().primaryNotificationBackend())); for (auto widget : m_tabs) { widget->loadSettings(); } @@ -78,7 +77,7 @@ void SettingsDialog::load() void SettingsDialog::save() { snoreDebug(SNORE_DEBUG) << "saving"; - m_snore->d()->setBackendIfAvailible(ui->primaryBackendComboBox->currentText()); + SnoreCore::instance().d()->setBackendIfAvailible(ui->primaryBackendComboBox->currentText()); for (auto w : m_tabs) { w->saveSettings(); } diff --git a/src/core/settingsdialog.h b/src/core/settingsdialog.h index 37b156d..774dffe 100644 --- a/src/core/settingsdialog.h +++ b/src/core/settingsdialog.h @@ -45,7 +45,7 @@ class SNORE_EXPORT SettingsDialog : public QDialog Q_OBJECT public: - explicit SettingsDialog(SnoreCore *snore, QWidget *parent = 0); + explicit SettingsDialog(QWidget *parent = 0); ~SettingsDialog(); private slots: @@ -57,7 +57,6 @@ private slots: private: Ui::SettingsDialog *ui; - SnoreCore *m_snore; QScopedPointer m_app; QScopedPointer m_alert; QList m_tabs; diff --git a/src/core/snore.cpp b/src/core/snore.cpp index 3a81f80..353ac45 100644 --- a/src/core/snore.cpp +++ b/src/core/snore.cpp @@ -40,6 +40,12 @@ SnoreCore::SnoreCore() d->q_ptr = this; } +SnoreCore &SnoreCore::instance() +{ + static SnoreCore instance; + return instance; +} + SnoreCore::~SnoreCore() { Q_D(SnoreCore); diff --git a/src/core/snore.h b/src/core/snore.h index f23024f..db03d0b 100644 --- a/src/core/snore.h +++ b/src/core/snore.h @@ -63,7 +63,7 @@ public: /** * Creates a Notification Manager SnoreCore */ - SnoreCore(); + static SnoreCore &instance(); ~SnoreCore(); /** @@ -189,6 +189,7 @@ signals: void notificationClosed(Snore::Notification); private: + SnoreCore(); SnoreCorePrivate *d_ptr; }; diff --git a/src/core/snore_p.cpp b/src/core/snore_p.cpp index de5d718..e7d1b9b 100644 --- a/src/core/snore_p.cpp +++ b/src/core/snore_p.cpp @@ -30,8 +30,6 @@ using namespace Snore; -SnoreCore *SnoreCorePrivate::q_ptr = nullptr; - SnoreCorePrivate::SnoreCorePrivate(): m_defaultApp("SnoreNotify", Icon(":/root/snore.png")), m_settings(new QSettings("Snorenotify", "libsnore", this)) diff --git a/src/core/snore_p.h b/src/core/snore_p.h index 78c18ca..f61d219 100644 --- a/src/core/snore_p.h +++ b/src/core/snore_p.h @@ -63,11 +63,6 @@ public: void addSettingsDescription(const QString &key, const QString &help) const; - static SnoreCore *snoreInstance() - { - return q_ptr; - } - bool initPrimaryNotificationBackend(); signals: @@ -80,8 +75,7 @@ private slots: void slotAboutToQuit(); private: - //TODO: cleanup the static instance - static SnoreCore *q_ptr; + SnoreCore *q_ptr; QHash m_applications; diff --git a/src/daemon/snorenotify.cpp b/src/daemon/snorenotify.cpp index e238b35..2dfa1c0 100644 --- a/src/daemon/snorenotify.cpp +++ b/src/daemon/snorenotify.cpp @@ -30,16 +30,14 @@ using namespace Snore; SnoreNotify::SnoreNotify() { m_trayIcon = new TrayIcon(); - m_snore = new SnoreCore(); - m_snore->loadPlugins(SnorePlugin::ALL); - m_trayIcon->initConextMenu(m_snore); + SnoreCore::instance().loadPlugins(SnorePlugin::ALL); + m_trayIcon->initConextMenu(); - snoreDebug(SNORE_DEBUG) << "Snorenotfiy initialized with" << m_snore->primaryNotificationBackend(); + snoreDebug(SNORE_DEBUG) << "Snorenotfiy initialized with" << SnoreCore::instance().primaryNotificationBackend(); } SnoreNotify::~SnoreNotify() { - delete m_snore; delete m_trayIcon; } diff --git a/src/daemon/snorenotify.h b/src/daemon/snorenotify.h index ebf594f..cc98e79 100644 --- a/src/daemon/snorenotify.h +++ b/src/daemon/snorenotify.h @@ -35,7 +35,6 @@ public: private: class TrayIcon *m_trayIcon; - Snore::SnoreCore *m_snore; }; #endif // SNORENOTIFY_H diff --git a/src/daemon/trayicon.cpp b/src/daemon/trayicon.cpp index 4866325..d639a8f 100644 --- a/src/daemon/trayicon.cpp +++ b/src/daemon/trayicon.cpp @@ -40,11 +40,9 @@ TrayIcon::TrayIcon(): m_app.hints().setValue("tray-icon", m_trayIcon); } -void TrayIcon::initConextMenu(SnoreCore *snore) +void TrayIcon::initConextMenu() { - m_snore = snore; - - m_settings = new SettingsDialog(snore); + m_settings = new SettingsDialog(); m_trayIcon->setVisible(true); m_trayMenu = new QMenu("SnoreNotify"); @@ -76,8 +74,8 @@ QSystemTrayIcon *TrayIcon::trayIcon() void TrayIcon::slotTestNotification() { - if (!m_snore->aplications().contains(m_app.name())) { - m_snore->registerApplication(m_app); + if (!SnoreCore::instance().aplications().contains(m_app.name())) { + SnoreCore::instance().registerApplication(m_app); } Notification noti(m_app, m_alert, "Hello World", "This is Snore
" @@ -88,7 +86,7 @@ void TrayIcon::slotTestNotification() "4
" "5
", Icon(":/root/snore.png")); noti.addAction(Action(1, "Test Action")); - m_snore->broadcastNotification(noti); + SnoreCore::instance().broadcastNotification(noti); QTimer *timer = new QTimer(this); m_notifications[timer] = noti; @@ -97,7 +95,7 @@ void TrayIcon::slotTestNotification() connect(timer, SIGNAL(timeout()), this, SLOT(sloutUpdateTestNotification())); timer->start(); - // m_snore->deregisterApplication(app); + // SnoreCore::instance().deregisterApplication(app); } void TrayIcon::sloutUpdateTestNotification() @@ -109,7 +107,7 @@ void TrayIcon::sloutUpdateTestNotification() "This icon is quite a long line of text, isnt it I think it is what do you think? btw the icon should be in color
" "Project Website", Icon("http://winkde.org/~pvonreth/other/kde-logo.png")); - m_snore->broadcastNotification(update); + SnoreCore::instance().broadcastNotification(update); timer->deleteLater(); } diff --git a/src/daemon/trayicon.h b/src/daemon/trayicon.h index 4505243..8da701b 100644 --- a/src/daemon/trayicon.h +++ b/src/daemon/trayicon.h @@ -32,7 +32,7 @@ class TrayIcon: public QObject Q_OBJECT public: TrayIcon(); - void initConextMenu(Snore::SnoreCore *snore); + void initConextMenu(); void hide(); class QSystemTrayIcon *trayIcon(); @@ -40,7 +40,6 @@ private: class QSystemTrayIcon *m_trayIcon; class QMenu *m_trayMenu; QActionGroup *m_backendActions; - Snore::SnoreCore *m_snore; Snore::Application m_app; Snore::Alert m_alert; Snore::SettingsDialog *m_settings; diff --git a/src/settings/main.cpp b/src/settings/main.cpp index 11c7593..28a9e7c 100644 --- a/src/settings/main.cpp +++ b/src/settings/main.cpp @@ -14,10 +14,10 @@ int main(int argc, char *argv[]) app.setApplicationVersion(Snore::Version::version()); app.setQuitOnLastWindowClosed(true); - Snore::SnoreCore snore; + Snore::SnoreCore &snore = Snore::SnoreCore::instance(); snore.loadPlugins(Snore::SnorePlugin::ALL); - Snore::SettingsDialog diag(&snore); + Snore::SettingsDialog diag; // diag.setWindowIcon(QIcon(":/root/snore.png")); diag.setWindowTitle("SnoreSettings"); diag.show();