cleanup plugin loading

This commit is contained in:
Patrick von Reth 2015-07-15 11:21:14 +02:00
parent 7d0f7584fd
commit 5112904d32
32 changed files with 89 additions and 61 deletions

View File

@ -52,15 +52,19 @@ SnorePlugin *PluginContainer::load()
snoreDebug(SNORE_WARNING) << "Failed loading plugin: " << m_loader.errorString();
return nullptr;
}
SnorePlugin *plugin = qobject_cast<SnorePlugin *> (m_loader.instance());
plugin->m_container = this;
plugin->setDefaultSettings();
return plugin;
if (!m_plugin) {
m_plugin = qobject_cast<SnorePlugin *> (m_loader.instance());
m_plugin->m_container = this;
m_plugin->setDefaultSettings();
QMetaObject::invokeMethod(m_plugin, "load", Qt::QueuedConnection);
}
return m_plugin;
}
void PluginContainer::unload()
{
m_loader.unload();
m_plugin = nullptr;
}
const QString &PluginContainer::file()

View File

@ -87,6 +87,7 @@ private:
QString m_pluginName;
SnorePlugin::PluginTypes m_pluginType;
QPluginLoader m_loader;
SnorePlugin *m_plugin = nullptr;
};
}

View File

@ -35,7 +35,7 @@ SnorePlugin::SnorePlugin()
if (thread() != qApp->thread()) {
snoreDebug(SNORE_WARNING) << "Plugin initialized in wrong thread.";
}
connect(this, &SnorePlugin::initializeChanged, [this](bool b) {
connect(this, &SnorePlugin::loadedStateChanged, [this](bool b) {
snoreDebug(SNORE_DEBUG) << "Plugin:" << name() << "initialized" << b;
Q_ASSERT_X(!(b && m_initialized), Q_FUNC_INFO, "Plugin initialized multiple times.");
if (!b) {
@ -50,7 +50,7 @@ SnorePlugin::~SnorePlugin()
snoreDebug(SNORE_DEBUG) << name() << this << "deleted";
}
bool SnorePlugin::isInitialized() const
bool SnorePlugin::isLoaded() const
{
return m_initialized;
}
@ -118,7 +118,7 @@ void SnorePlugin::setDefaultSettings()
void SnorePlugin::setEnabled(bool enabled)
{
Q_ASSERT_X(!enabled || isInitialized(), Q_FUNC_INFO, "Plugin not initialized");
Q_ASSERT_X(!enabled || isLoaded(), Q_FUNC_INFO, "Plugin not initialized");
if (enabled != m_enabled) {
emit enabledChanged(enabled);

View File

@ -58,7 +58,7 @@ public:
void enable();
void disable();
bool isInitialized() const;
bool isLoaded() const;
bool isEnabled() const;
@ -84,11 +84,11 @@ public:
virtual PluginSettingsWidget *settingsWidget();
Q_SIGNALS:
void initializeChanged(bool initialized);
void loadedStateChanged(bool loaded);
void enabledChanged(bool enabled);
public Q_SLOTS:
virtual void slotInitialize() = 0;
private Q_SLOTS:
virtual void load() = 0;
protected:
/**

View File

@ -84,7 +84,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
case SnorePlugin::SECONDARY_BACKEND:
case SnorePlugin::FRONTEND:
case SnorePlugin::PLUGIN:
connect(plugin, &SnorePlugin::initializeChanged, [plugin](bool initialized) {
connect(plugin, &SnorePlugin::loadedStateChanged, [plugin](bool initialized) {
if (initialized) {
plugin->setEnabled(plugin->settingsValue(QLatin1String("Enabled"), LOCAL_SETTING).toBool());
}
@ -95,7 +95,7 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
continue;
}
connect(plugin, &SnorePlugin::initializeChanged, [d, plugin](bool initialized) {
connect(plugin, &SnorePlugin::loadedStateChanged, [d, plugin](bool initialized) {
if (!initialized) {
//TODO: improve
d->m_pluginNames[plugin->type()].removeAll(plugin->name());
@ -103,7 +103,6 @@ void SnoreCore::loadPlugins(SnorePlugin::PluginTypes types)
}
});
QMetaObject::invokeMethod(plugin, "slotInitialize", Qt::QueuedConnection);
snoreDebug(SNORE_DEBUG) << info->name() << "is a" << info->type();
d->m_pluginNames[info->type()].append(info->name());
auto key = qMakePair(type, info->name());

View File

@ -86,14 +86,14 @@ bool SnoreCorePrivate::setBackendIfAvailible(const QString &backend)
}
snoreDebug(SNORE_DEBUG) << "Setting Notification Backend to:" << backend;
SnoreBackend *b = qobject_cast<SnoreBackend *>(backends.value(backend)->load());
if (!b->isInitialized()) {
if (!b->isLoaded()) {
snoreDebug(SNORE_DEBUG) << "Failed to initialize" << b->name();
return false;
}
if (m_notificationBackend) {
m_notificationBackend->disable();
}
connect(b, &SnoreBackend::initializeChanged, [this, b](bool initialized) {
connect(b, &SnoreBackend::loadedStateChanged, [this, b](bool initialized) {
if (!initialized) {
slotInitPrimaryNotificationBackend();
}

View File

@ -24,7 +24,7 @@ FreedesktopBackend::FreedesktopBackend()
});
}
void FreedesktopBackend::slotInitialize()
void FreedesktopBackend::load()
{
m_interface = new org::freedesktop::Notifications(QLatin1String("org.freedesktop.Notifications"),
QLatin1String("/org/freedesktop/Notifications"),
@ -33,7 +33,7 @@ void FreedesktopBackend::slotInitialize()
reply.waitForFinished();
QStringList caps = reply.value();
m_supportsRichtext = caps.contains(QLatin1String("body-markup"));
emit initializeChanged(true);
emit loadedStateChanged(true);
}
bool FreedesktopBackend::canCloseNotification() const

View File

@ -16,14 +16,15 @@ public:
bool canUpdateNotification() const override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
void slotCloseNotification(Snore::Notification notification) override;
void slotActionInvoked(const uint &id, const QString &actionID);
void slotNotificationClosed(const uint &id, const uint &reason);
private Q_SLOTS:
void load() override;
private:
org::freedesktop::Notifications *m_interface;
QHash<uint, Snore::Notification> m_dbusIdMap;

View File

@ -63,9 +63,9 @@ GrowlBackend::~GrowlBackend()
Growl::shutdown();
}
void GrowlBackend::slotInitialize()
void GrowlBackend::load()
{
emit initializeChanged(Growl::isRunning(GROWL_TCP, settingsValue(QLatin1String("Host")).toString().toUtf8().constData()));
emit loadedStateChanged(Growl::isRunning(GROWL_TCP, settingsValue(QLatin1String("Host")).toString().toUtf8().constData()));
}
void GrowlBackend::slotRegisterApplication(const Application &application)

View File

@ -43,11 +43,13 @@ private:
QHash<QString, Growl *> m_applications;
public Q_SLOTS:
void slotInitialize() override;
void slotRegisterApplication(const Snore::Application &application) override;
void slotDeregisterApplication(const Snore::Application &application) override;
void slotNotify(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
};
#endif // GROWL_BACKEND_H

View File

@ -121,11 +121,11 @@ SnarlBackend::~SnarlBackend()
delete m_eventLoop;
}
void SnarlBackend::slotInitialize()
void SnarlBackend::load()
{
SnarlInterface *snarlInterface = new SnarlInterface();
emit initializeChanged(snarlInterface->IsSnarlRunning());
emit loadedStateChanged(snarlInterface->IsSnarlRunning());
delete snarlInterface;
}

View File

@ -43,12 +43,14 @@ private:
QHash<QString, Snarl::V42::SnarlInterface *> m_applications;
public Q_SLOTS:
void slotInitialize() override;
void slotRegisterApplication(const Snore::Application &application);
void slotDeregisterApplication(const Snore::Application &application);
void slotNotify(Snore::Notification notification);
void slotCloseNotification(Snore::Notification notification);
private Q_SLOTS:
void load() override;
private:
QHash<LONG32, Snore::Notification> m_idMap;

View File

@ -105,7 +105,7 @@ void SnoreNotifier::slotCloseNotification(Snore::Notification notification)
//the timer will show the next
}
void SnoreNotifier::slotInitialize()
void SnoreNotifier::load()
{
for (int i = 0; i < m_widgets.size(); ++i) {
NotifyWidget *w = new NotifyWidget(i, this);
@ -123,7 +123,7 @@ void SnoreNotifier::slotInitialize()
slotCloseNotification(notification);
});
}
emit initializeChanged(true);
emit loadedStateChanged(true);
}
bool SnoreNotifier::canCloseNotification() const

View File

@ -41,10 +41,12 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
virtual void slotNotify(Snore::Notification notification) override;
virtual void slotCloseNotification(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QList<Snore::Notification> m_queue;

View File

@ -14,13 +14,13 @@
using namespace Snore;
void SnoreToast::slotInitialize()
void SnoreToast::load()
{
if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS8) {
snoreDebug(SNORE_DEBUG) << "SnoreToast does not work on windows" << QSysInfo::windowsVersion();
emit initializeChanged(false);
emit loadedStateChanged(false);
}
emit initializeChanged(true);
emit loadedStateChanged(true);
}
bool SnoreToast::canCloseNotification() const
@ -134,7 +134,7 @@ QProcess *SnoreToast::createProcess(Notification noti)
if (noti.isValid()) {
closeNotification(noti, Notification::NONE);
}
emit initializeChanged(false);
emit loadedStateChanged(false);
p->deleteLater();
});
connect(qApp, &QApplication::aboutToQuit, p, &QProcess::kill);

View File

@ -16,11 +16,13 @@ public:
virtual bool canCloseNotification() const override;
public Q_SLOTS:
virtual void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
void slotRegisterApplication(const Snore::Application &application) override;
void slotCloseNotification(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QString appId(const Snore::Application &application);

View File

@ -6,10 +6,10 @@
#include <QSystemTrayIcon>
using namespace Snore;
void TrayIconNotifer::slotInitialize()
void TrayIconNotifer::load()
{
m_currentlyDisplaying = false;
emit initializeChanged(true);
emit loadedStateChanged(true);
}
bool TrayIconNotifer::canCloseNotification() const

View File

@ -21,12 +21,14 @@ public:
virtual bool canCloseNotification() const override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
void slotCloseNotification(Snore::Notification notification) override;
void slotRegisterApplication(const Snore::Application &application) override;
void slotDeregisterApplication(const Snore::Application &application) override;
private Q_SLOTS:
void load() override;
private:
QSystemTrayIcon *trayIcon(const Snore::Application &app);
QList<Snore::Notification > m_notificationQue;

View File

@ -38,11 +38,11 @@ FreedesktopFrontend::FreedesktopFrontend()
if (dbus.registerService(QLatin1String("org.freedesktop.Notifications"))) {
if (!dbus.registerObject(QLatin1String("/org/freedesktop/Notifications"), this)) {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register object";
emit initializeChanged(false);
emit loadedStateChanged(false);
}
} else {
snoreDebug(SNORE_WARNING) << "Failed to initialize" << name() << "failed to register service";
emit initializeChanged(false);
emit loadedStateChanged(false);
}
} else {
QDBusConnection dbus = QDBusConnection::sessionBus();
@ -54,9 +54,9 @@ FreedesktopFrontend::FreedesktopFrontend()
});
}
void FreedesktopFrontend::slotInitialize()
void FreedesktopFrontend::load()
{
emit initializeChanged(true);
emit loadedStateChanged(true);
}
void FreedesktopFrontend::slotActionInvoked(Notification notification)

View File

@ -43,10 +43,12 @@ Q_SIGNALS:
void ActionInvoked(uint id, const QString &actionKey);
public Q_SLOTS:
void slotInitialize() override;
void slotActionInvoked(Snore::Notification notification) override;
void slotNotificationClosed(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
Snore::Alert m_alert;
NotificationsAdaptor *m_adaptor;

View File

@ -51,9 +51,9 @@ PushoverFrontend::PushoverFrontend()
});
}
void PushoverFrontend::slotInitialize()
void PushoverFrontend::load()
{
emit initializeChanged(true);
emit loadedStateChanged(true);
}
PluginSettingsWidget *PushoverFrontend::settingsWidget()

View File

@ -47,9 +47,11 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
void slotActionInvoked(Snore::Notification notification);
private Q_SLOTS:
void load() override;
Q_SIGNALS:
void loggedInChanged(bool isLoggedIn);
void error(QString error);

View File

@ -44,16 +44,16 @@ SnarlNetworkFrontend::~SnarlNetworkFrontend()
delete parser;
}
void SnarlNetworkFrontend::slotInitialize()
void SnarlNetworkFrontend::load()
{
tcpServer = new QTcpServer(this);
if (!tcpServer->listen(QHostAddress::Any, port)) {
snoreDebug(SNORE_DEBUG) << "The port is already used";
emit initializeChanged(false);
emit loadedStateChanged(false);
} else {
std::cout << "The Snarl Network Protokoll is developed for Snarl <http://www.fullphat.net/>" << std::endl;
}
emit initializeChanged(true);
emit loadedStateChanged(true);
}
void SnarlNetworkFrontend::slotActionInvoked(Snore::Notification notification)

View File

@ -39,11 +39,11 @@ public:
~SnarlNetworkFrontend();
public Q_SLOTS:
void slotInitialize() override;
void slotActionInvoked(Snore::Notification notification) override;
void slotNotificationClosed(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
void handleConnection();
void handleMessages();

View File

@ -59,9 +59,9 @@ void NotifyMyAndroid::slotNotify(Notification notification)
}
void NotifyMyAndroid::slotInitialize()
void NotifyMyAndroid::load()
{
emit initializeChanged(true);
emit loadedStateChanged(true);
}
PluginSettingsWidget *NotifyMyAndroid::settingsWidget()

View File

@ -37,9 +37,11 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QNetworkAccessManager m_manager;

View File

@ -120,9 +120,9 @@ void Pushover::slotNotify(Notification notification)
}
void Pushover::slotInitialize()
void Pushover::load()
{
emit initializeChanged(true);
emit loadedStateChanged(true);
}
PluginSettingsWidget *Pushover::settingsWidget()

View File

@ -37,9 +37,11 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QNetworkAccessManager m_manager;

View File

@ -34,10 +34,10 @@ Sound::Sound():
});
}
void Sound::slotInitialize()
void Sound::load()
{
m_player->setVolume(settingsValue(QLatin1String("Volume")).toInt());
emit initializeChanged(true);
emit loadedStateChanged(true);
}
PluginSettingsWidget *Sound::settingsWidget()

View File

@ -37,8 +37,11 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotificationDisplayed(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QMediaPlayer *m_player;
};

View File

@ -77,9 +77,9 @@ void Toasty::slotNotify(Notification notification)
}
void Toasty::slotInitialize()
void Toasty::load()
{
emit initializeChanged(true);
emit loadedStateChanged(true);
}
PluginSettingsWidget *Toasty::settingsWidget()

View File

@ -37,9 +37,11 @@ protected:
void setDefaultSettings() override;
public Q_SLOTS:
void slotInitialize() override;
void slotNotify(Snore::Notification notification) override;
private Q_SLOTS:
void load() override;
private:
QNetworkAccessManager m_manager;