use singleton for snorecore

This commit is contained in:
Patrick von Reth 2015-02-04 10:36:21 +01:00
parent 2713e36f90
commit e9e9d57dba
13 changed files with 35 additions and 44 deletions

View File

@ -61,12 +61,12 @@ bool SnorePlugin::isInitialized() const
SnoreCore *SnorePlugin::snore() SnoreCore *SnorePlugin::snore()
{ {
return SnoreCorePrivate::snoreInstance(); return &SnoreCore::instance();
} }
const SnoreCore *SnorePlugin::snore() const const SnoreCore *SnorePlugin::snore() const
{ {
return SnoreCorePrivate::snoreInstance(); return &SnoreCore::instance();
} }
QVariant SnorePlugin::value(const QString &key) const QVariant SnorePlugin::value(const QString &key) const

View File

@ -48,8 +48,8 @@ public:
virtual bool initialize(); virtual bool initialize();
virtual bool deinitialize(); virtual bool deinitialize();
bool isInitialized() const; bool isInitialized() const;
SnoreCore *snore(); LIBSNORE_DEPRECATED SnoreCore *snore();
const SnoreCore *snore() const; LIBSNORE_DEPRECATED const SnoreCore *snore() const;
const QString &name() const; const QString &name() const;
PluginTypes type() const; PluginTypes type() const;

View File

@ -27,10 +27,9 @@
using namespace Snore; using namespace Snore;
SettingsDialog::SettingsDialog(SnoreCore *snore, QWidget *parent) : SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::SettingsDialog), ui(new Ui::SettingsDialog),
m_snore(snore),
m_app(new Application("SnoreSettings", Icon(":/root/snore.png"))), m_app(new Application("SnoreSettings", Icon(":/root/snore.png"))),
m_alert(new Alert("Test", 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); m_app->addAlert(*m_alert);
for (auto widget : snore->settingWidgets()) { for (auto widget : SnoreCore::instance().settingWidgets()) {
ui->tabWidget->addTab(widget, widget->name()); ui->tabWidget->addTab(widget, widget->name());
m_tabs.append(widget); m_tabs.append(widget);
} }
@ -52,24 +51,24 @@ SettingsDialog::~SettingsDialog()
void Snore::SettingsDialog::on_pushButton_clicked() void Snore::SettingsDialog::on_pushButton_clicked()
{ {
if (!m_snore->aplications().contains(m_app->name())) { if (!SnoreCore::instance().aplications().contains(m_app->name())) {
m_snore->registerApplication(*m_app); SnoreCore::instance().registerApplication(*m_app);
} }
Notification noti(*m_app, *m_alert, "Hello World", Notification noti(*m_app, *m_alert, "Hello World",
"<i>This is Snore</i><br>" "<i>This is Snore</i><br>"
"<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a><br>", "<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a><br>",
Icon(":/root/snore.png")); Icon(":/root/snore.png"));
noti.addAction(Action(1, "Test Action")); noti.addAction(Action(1, "Test Action"));
m_snore->broadcastNotification(noti); SnoreCore::instance().broadcastNotification(noti);
} }
void SettingsDialog::load() void SettingsDialog::load()
{ {
snoreDebug(SNORE_DEBUG) << "loading"; snoreDebug(SNORE_DEBUG) << "loading";
ui->primaryBackendComboBox->clear(); ui->primaryBackendComboBox->clear();
QStringList list = m_snore->pluginNames(SnorePlugin::BACKEND); QStringList list = SnoreCore::instance().pluginNames(SnorePlugin::BACKEND);
ui->primaryBackendComboBox->addItems(list); 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) { for (auto widget : m_tabs) {
widget->loadSettings(); widget->loadSettings();
} }
@ -78,7 +77,7 @@ void SettingsDialog::load()
void SettingsDialog::save() void SettingsDialog::save()
{ {
snoreDebug(SNORE_DEBUG) << "saving"; snoreDebug(SNORE_DEBUG) << "saving";
m_snore->d()->setBackendIfAvailible(ui->primaryBackendComboBox->currentText()); SnoreCore::instance().d()->setBackendIfAvailible(ui->primaryBackendComboBox->currentText());
for (auto w : m_tabs) { for (auto w : m_tabs) {
w->saveSettings(); w->saveSettings();
} }

View File

@ -45,7 +45,7 @@ class SNORE_EXPORT SettingsDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit SettingsDialog(SnoreCore *snore, QWidget *parent = 0); explicit SettingsDialog(QWidget *parent = 0);
~SettingsDialog(); ~SettingsDialog();
private slots: private slots:
@ -57,7 +57,6 @@ private slots:
private: private:
Ui::SettingsDialog *ui; Ui::SettingsDialog *ui;
SnoreCore *m_snore;
QScopedPointer<Application> m_app; QScopedPointer<Application> m_app;
QScopedPointer<Alert> m_alert; QScopedPointer<Alert> m_alert;
QList<Snore::PluginSettingsWidget *> m_tabs; QList<Snore::PluginSettingsWidget *> m_tabs;

View File

@ -40,6 +40,12 @@ SnoreCore::SnoreCore()
d->q_ptr = this; d->q_ptr = this;
} }
SnoreCore &SnoreCore::instance()
{
static SnoreCore instance;
return instance;
}
SnoreCore::~SnoreCore() SnoreCore::~SnoreCore()
{ {
Q_D(SnoreCore); Q_D(SnoreCore);

View File

@ -63,7 +63,7 @@ public:
/** /**
* Creates a Notification Manager SnoreCore * Creates a Notification Manager SnoreCore
*/ */
SnoreCore(); static SnoreCore &instance();
~SnoreCore(); ~SnoreCore();
/** /**
@ -189,6 +189,7 @@ signals:
void notificationClosed(Snore::Notification); void notificationClosed(Snore::Notification);
private: private:
SnoreCore();
SnoreCorePrivate *d_ptr; SnoreCorePrivate *d_ptr;
}; };

View File

@ -30,8 +30,6 @@
using namespace Snore; using namespace Snore;
SnoreCore *SnoreCorePrivate::q_ptr = nullptr;
SnoreCorePrivate::SnoreCorePrivate(): SnoreCorePrivate::SnoreCorePrivate():
m_defaultApp("SnoreNotify", Icon(":/root/snore.png")), m_defaultApp("SnoreNotify", Icon(":/root/snore.png")),
m_settings(new QSettings("Snorenotify", "libsnore", this)) m_settings(new QSettings("Snorenotify", "libsnore", this))

View File

@ -63,11 +63,6 @@ public:
void addSettingsDescription(const QString &key, const QString &help) const; void addSettingsDescription(const QString &key, const QString &help) const;
static SnoreCore *snoreInstance()
{
return q_ptr;
}
bool initPrimaryNotificationBackend(); bool initPrimaryNotificationBackend();
signals: signals:
@ -80,8 +75,7 @@ private slots:
void slotAboutToQuit(); void slotAboutToQuit();
private: private:
//TODO: cleanup the static instance SnoreCore *q_ptr;
static SnoreCore *q_ptr;
QHash<QString, Application> m_applications; QHash<QString, Application> m_applications;

View File

@ -30,16 +30,14 @@ using namespace Snore;
SnoreNotify::SnoreNotify() SnoreNotify::SnoreNotify()
{ {
m_trayIcon = new TrayIcon(); m_trayIcon = new TrayIcon();
m_snore = new SnoreCore(); SnoreCore::instance().loadPlugins(SnorePlugin::ALL);
m_snore->loadPlugins(SnorePlugin::ALL); m_trayIcon->initConextMenu();
m_trayIcon->initConextMenu(m_snore);
snoreDebug(SNORE_DEBUG) << "Snorenotfiy initialized with" << m_snore->primaryNotificationBackend(); snoreDebug(SNORE_DEBUG) << "Snorenotfiy initialized with" << SnoreCore::instance().primaryNotificationBackend();
} }
SnoreNotify::~SnoreNotify() SnoreNotify::~SnoreNotify()
{ {
delete m_snore;
delete m_trayIcon; delete m_trayIcon;
} }

View File

@ -35,7 +35,6 @@ public:
private: private:
class TrayIcon *m_trayIcon; class TrayIcon *m_trayIcon;
Snore::SnoreCore *m_snore;
}; };
#endif // SNORENOTIFY_H #endif // SNORENOTIFY_H

View File

@ -40,11 +40,9 @@ TrayIcon::TrayIcon():
m_app.hints().setValue("tray-icon", m_trayIcon); m_app.hints().setValue("tray-icon", m_trayIcon);
} }
void TrayIcon::initConextMenu(SnoreCore *snore) void TrayIcon::initConextMenu()
{ {
m_snore = snore; m_settings = new SettingsDialog();
m_settings = new SettingsDialog(snore);
m_trayIcon->setVisible(true); m_trayIcon->setVisible(true);
m_trayMenu = new QMenu("SnoreNotify"); m_trayMenu = new QMenu("SnoreNotify");
@ -76,8 +74,8 @@ QSystemTrayIcon *TrayIcon::trayIcon()
void TrayIcon::slotTestNotification() void TrayIcon::slotTestNotification()
{ {
if (!m_snore->aplications().contains(m_app.name())) { if (!SnoreCore::instance().aplications().contains(m_app.name())) {
m_snore->registerApplication(m_app); SnoreCore::instance().registerApplication(m_app);
} }
Notification noti(m_app, m_alert, "Hello World", Notification noti(m_app, m_alert, "Hello World",
"<i>This is Snore</i><br>" "<i>This is Snore</i><br>"
@ -88,7 +86,7 @@ void TrayIcon::slotTestNotification()
"4<br>" "4<br>"
"5<br>", Icon(":/root/snore.png")); "5<br>", Icon(":/root/snore.png"));
noti.addAction(Action(1, "Test Action")); noti.addAction(Action(1, "Test Action"));
m_snore->broadcastNotification(noti); SnoreCore::instance().broadcastNotification(noti);
QTimer *timer = new QTimer(this); QTimer *timer = new QTimer(this);
m_notifications[timer] = noti; m_notifications[timer] = noti;
@ -97,7 +95,7 @@ void TrayIcon::slotTestNotification()
connect(timer, SIGNAL(timeout()), this, SLOT(sloutUpdateTestNotification())); connect(timer, SIGNAL(timeout()), this, SLOT(sloutUpdateTestNotification()));
timer->start(); timer->start();
// m_snore->deregisterApplication(app); // SnoreCore::instance().deregisterApplication(app);
} }
void TrayIcon::sloutUpdateTestNotification() void TrayIcon::sloutUpdateTestNotification()
@ -109,7 +107,7 @@ void TrayIcon::sloutUpdateTestNotification()
"<u>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</u><br>" "<u>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</u><br>"
"<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a>", "<a href=\"https://github.com/TheOneRing/Snorenotify\">Project Website</a>",
Icon("http://winkde.org/~pvonreth/other/kde-logo.png")); Icon("http://winkde.org/~pvonreth/other/kde-logo.png"));
m_snore->broadcastNotification(update); SnoreCore::instance().broadcastNotification(update);
timer->deleteLater(); timer->deleteLater();
} }

View File

@ -32,7 +32,7 @@ class TrayIcon: public QObject
Q_OBJECT Q_OBJECT
public: public:
TrayIcon(); TrayIcon();
void initConextMenu(Snore::SnoreCore *snore); void initConextMenu();
void hide(); void hide();
class QSystemTrayIcon *trayIcon(); class QSystemTrayIcon *trayIcon();
@ -40,7 +40,6 @@ private:
class QSystemTrayIcon *m_trayIcon; class QSystemTrayIcon *m_trayIcon;
class QMenu *m_trayMenu; class QMenu *m_trayMenu;
QActionGroup *m_backendActions; QActionGroup *m_backendActions;
Snore::SnoreCore *m_snore;
Snore::Application m_app; Snore::Application m_app;
Snore::Alert m_alert; Snore::Alert m_alert;
Snore::SettingsDialog *m_settings; Snore::SettingsDialog *m_settings;

View File

@ -14,10 +14,10 @@ int main(int argc, char *argv[])
app.setApplicationVersion(Snore::Version::version()); app.setApplicationVersion(Snore::Version::version());
app.setQuitOnLastWindowClosed(true); app.setQuitOnLastWindowClosed(true);
Snore::SnoreCore snore; Snore::SnoreCore &snore = Snore::SnoreCore::instance();
snore.loadPlugins(Snore::SnorePlugin::ALL); snore.loadPlugins(Snore::SnorePlugin::ALL);
Snore::SettingsDialog diag(&snore); Snore::SettingsDialog diag;
// diag.setWindowIcon(QIcon(":/root/snore.png")); // diag.setWindowIcon(QIcon(":/root/snore.png"));
diag.setWindowTitle("SnoreSettings"); diag.setWindowTitle("SnoreSettings");
diag.show(); diag.show();