File indexing completed on 2025-01-12 04:20:10

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2013-2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 
0019 #include "../snore.h"
0020 #include "../snore_p.h"
0021 #include "../snoreconstants.h"
0022 #include "snorebackend.h"
0023 #include "snorefrontend.h"
0024 #include "../notification/notification_p.h"
0025 #include "plugincontainer.h"
0026 
0027 #include <QDebug>
0028 #include <QMetaEnum>
0029 #include <QGuiApplication>
0030 
0031 using namespace Snore;
0032 
0033 SnorePlugin::SnorePlugin()
0034 {
0035     Q_ASSERT_X(thread() == qApp->thread(), Q_FUNC_INFO, "Plugin initialized in wrong thread");
0036     if (thread() != qApp->thread()) {
0037         qCWarning(SNORE) << "Plugin initialized in wrong thread.";
0038     }
0039 }
0040 
0041 SnorePlugin::~SnorePlugin()
0042 {
0043     qCDebug(SNORE) << name() << this << "deleted";
0044 }
0045 
0046 bool SnorePlugin::isEnabled() const
0047 {
0048     return m_enabled;
0049 }
0050 
0051 QVariant SnorePlugin::settingsValue(const SettingsKey &key) const
0052 {
0053     return SnoreCore::instance().settingsValue(normaliseKey(key));
0054 }
0055 
0056 void SnorePlugin::setSettingsValue(const SettingsKey &key, const QVariant &value)
0057 {
0058     SnoreCore::instance().setSettingsValue(normaliseKey(key), value);
0059 }
0060 
0061 void SnorePlugin::setDefaultSettingsValue(const SettingsKey &key, const QVariant &value)
0062 {
0063     SnoreCore::instance().setDefaultSettingsValue(normaliseKey(key), value);
0064 }
0065 
0066 const Hint &SnorePlugin::constHints() const
0067 {
0068     return const_cast<Hint &>(const_cast<SnorePlugin *>(this)->hints());
0069 }
0070 
0071 Hint &SnorePlugin::hints()
0072 {
0073     return m_hints;
0074 }
0075 
0076 SettingsKey SnorePlugin::normaliseKey(const SettingsKey &key) const
0077 {
0078     return SettingsKey{name() + QLatin1Char('-') + typeName() + QLatin1Char('/') + key.key + QLatin1Char('.') + settingsVersion(), key.type};
0079 }
0080 
0081 const QString &SnorePlugin::name() const
0082 {
0083     return m_name;
0084 }
0085 
0086 const QString SnorePlugin::typeName() const
0087 {
0088     return SnorePlugin::typeToString(type());
0089 }
0090 
0091 bool SnorePlugin::isReady()
0092 {
0093     return m_error.isEmpty();
0094 }
0095 
0096 QString SnorePlugin::errorString() const
0097 {
0098     return m_error;
0099 }
0100 
0101 QString SnorePlugin::settingsVersion() const
0102 {
0103     return QStringLiteral("v1");
0104 }
0105 
0106 void SnorePlugin::setDefaultSettings()
0107 {
0108     setDefaultSettingsValue(Constants::SettingsKeys::Enabled, false);
0109 }
0110 
0111 void SnorePlugin::setErrorString(const QString &_error)
0112 {
0113     m_error = _error;
0114     qCWarning(SNORE) << name() << "encountered an error:" << m_error;
0115     disable();
0116     emit error(_error);
0117 }
0118 
0119 void SnorePlugin::setEnabled(bool enabled)
0120 {
0121     if (enabled != m_enabled) {
0122         emit enabledChanged(enabled);
0123     }
0124     m_enabled = enabled;
0125 }
0126 
0127 void SnorePlugin::enable()
0128 {
0129     setEnabled(true);
0130 }
0131 
0132 void SnorePlugin::disable()
0133 {
0134     setEnabled(false);
0135 }
0136 
0137 SnorePlugin::PluginTypes SnorePlugin::typeFromString(const QString &t)
0138 {
0139     return (SnorePlugin::PluginTypes)SnorePlugin::staticMetaObject.enumerator(SnorePlugin::staticMetaObject.indexOfEnumerator("PluginType")).keyToValue(t.toUpper().toLatin1().constData());
0140 }
0141 
0142 QString SnorePlugin::typeToString(const SnorePlugin::PluginTypes t)
0143 {
0144     return QString::fromLatin1(SnorePlugin::staticMetaObject.enumerator(SnorePlugin::staticMetaObject.indexOfEnumerator("PluginType")).valueToKey(t));
0145 }
0146 
0147 const QList<SnorePlugin::PluginTypes> &SnorePlugin::types()
0148 {
0149     static QList<SnorePlugin::PluginTypes> t;
0150     if (t.isEmpty()) {
0151         QMetaEnum e = SnorePlugin::staticMetaObject.enumerator(SnorePlugin::staticMetaObject.indexOfEnumerator("PluginType"));
0152         t.reserve(e.keyCount());
0153         for (int i = 0; i < e.keyCount(); ++i) {
0154             t << (SnorePlugin::PluginTypes) e.value(i);
0155         }
0156     }
0157     return t;
0158 }
0159 
0160 QDebug operator<<(QDebug debug, const Snore::SnorePlugin::PluginTypes &flags)
0161 {
0162     QMetaEnum e = SnorePlugin::staticMetaObject.enumerator(SnorePlugin::staticMetaObject.indexOfEnumerator("PluginType"));
0163     debug.nospace() << "PluginTypes(";
0164     bool needSeparator = false;
0165     int key;
0166     for (int i = 0; i < e.keyCount(); ++i) {
0167         key = e.value(i);
0168         if (flags.testFlag((SnorePlugin::PluginType)key)) {
0169             if (needSeparator) {
0170                 debug.nospace() << '|';
0171             } else {
0172                 needSeparator = true;
0173             }
0174 
0175             debug.nospace() << e.valueToKey(key);
0176         }
0177     }
0178     debug << ')';
0179     return debug.space();
0180 
0181 }
0182 
0183 QDebug operator<<(QDebug debug, const Snore::SnorePlugin *p)
0184 {
0185     debug.nospace() << p->metaObject()->className() << "(" << (void *)p  << ", " << p->name() << ")";
0186     return debug.space();
0187 }
0188 
0189 QDataStream &operator<<(QDataStream &out, const Snore::SnorePlugin::PluginTypes &type)
0190 {
0191     out << static_cast<int>(type);
0192     return out;
0193 }
0194 
0195 QDataStream &operator>>(QDataStream &in, Snore::SnorePlugin::PluginTypes &type)
0196 {
0197     int key;
0198     in >> key;
0199     type = static_cast<SnorePlugin::PluginTypes>(key);
0200     return in;
0201 }