File indexing completed on 2024-05-05 04:45:34

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 "notification.h"
0020 #include "snore.h"
0021 #include "snoreconstants.h"
0022 #include "notification/icon.h"
0023 #include "notification/notification_p.h"
0024 #include "plugins/plugincontainer.h"
0025 
0026 using namespace Snore;
0027 
0028 Notification::Notification() :
0029     d(nullptr)
0030 {
0031 }
0032 
0033 Notification::Notification(const Application &application, const Alert &alert, const QString &title, const QString &text, const Icon &icon, int timeout, Notification::Prioritys priority):
0034     d(new  NotificationData(application, alert, title, text, icon, timeout, priority))
0035 {
0036 
0037 }
0038 
0039 Notification::Notification(const Notification &old, const QString &title, const QString &text, const Icon &icon, int timeout, Notification::Prioritys priority):
0040     d(new  NotificationData(old, title, text, icon, timeout, priority))
0041 {
0042 }
0043 
0044 Notification::Notification(const Notification &other) :
0045     d(other.d)
0046 {
0047 }
0048 
0049 Notification &Notification::operator=(const Notification &other)
0050 {
0051     d = other.d;
0052     return *this;
0053 }
0054 
0055 Notification::~Notification()
0056 {
0057 
0058 }
0059 
0060 uint Notification::id() const
0061 {
0062     return d->m_id;
0063 }
0064 
0065 const Icon &Notification::icon() const
0066 {
0067     return d->m_icon;
0068 }
0069 
0070 int Notification::timeout() const
0071 {
0072     return d->m_timeout;
0073 }
0074 
0075 Notification &Notification::old() const
0076 {
0077     return d->m_toReplace;
0078 }
0079 
0080 bool Notification::isUpdate() const
0081 {
0082     return d->m_toReplace.isValid();
0083 }
0084 
0085 const Action &Notification::actionInvoked() const
0086 {
0087     return d->m_actionInvoked;
0088 }
0089 
0090 Application &Notification::application() const
0091 {
0092     return d->m_application;
0093 }
0094 
0095 QString Notification::title(Utils::MarkupFlags flags) const
0096 {
0097     return d->resolveMarkup(d->m_title, flags);
0098 }
0099 
0100 QString Notification::text(Utils::MarkupFlags flags) const
0101 {
0102     return d->resolveMarkup(d->m_text, flags);
0103 }
0104 
0105 const Alert &Notification::alert() const
0106 {
0107     return d->m_alert;
0108 }
0109 
0110 bool Notification::isSticky() const
0111 {
0112     return d->m_timeout == 0;
0113 }
0114 
0115 Notification::Prioritys Notification::priority() const
0116 {
0117     return d->m_priority;
0118 }
0119 
0120 void Notification::addAction(const Action &a)
0121 {
0122     d->m_actions.insert(a.id(), a);
0123 }
0124 
0125 const QHash<int, Action> &Notification::actions() const
0126 {
0127     return d->m_actions;
0128 }
0129 
0130 const Notification::CloseReasons &Notification::closeReason()
0131 {
0132     return d->m_closeReason;
0133 }
0134 
0135 Hint &Notification::hints()
0136 {
0137     return d->m_hints;
0138 }
0139 
0140 const Hint &Notification::constHints() const
0141 {
0142     return  const_cast<Hint &>(const_cast<Notification *>(this)->hints());
0143 }
0144 
0145 bool Notification::isValid() const
0146 {
0147     return d;
0148 }
0149 
0150 void Notification::addActiveIn(const QObject *o)
0151 {
0152     bool contains = d->m_activeIn.contains(o);
0153     Q_ASSERT_X(!contains, Q_FUNC_INFO, "already active");
0154     if (contains) {
0155         qCWarning(SNORE) << o << "already active in" << id();
0156         return;
0157     }
0158     d->m_activeIn.insert(o);
0159     SnoreCorePrivate::instance()->m_activeNotifications[id()] = *this;
0160     qCDebug(SNORE) << SnoreCorePrivate::instance()->m_activeNotifications.size();
0161 }
0162 
0163 bool Notification::isActiveIn(const QObject *o) const
0164 {
0165     return d->m_activeIn.contains(o);
0166 }
0167 
0168 bool Notification::removeActiveIn(const QObject *o)
0169 {
0170     bool out = d->m_activeIn.remove(o);
0171     if (d->m_activeIn.isEmpty()) {
0172         SnoreCorePrivate::instance()->m_activeNotifications.remove(id());
0173         qCDebug(SNORE) << SnoreCorePrivate::instance()->m_activeNotifications.size();
0174     }
0175     return out;
0176 }
0177 
0178 NotificationData *Notification::data()
0179 {
0180     return d.data();
0181 }
0182 
0183 int Notification::defaultTimeout()
0184 {
0185     return SnoreCore::instance().settingsValue(Snore::Constants::SettingsKeys::Timeout).toInt();
0186 }
0187 
0188 QDataStream &operator<< (QDataStream &stream, const Notification &noti)
0189 {
0190     stream << "Title: " << noti.title() << " Text: " << noti.text() << " ID: " << noti.id() ;
0191     return stream;
0192 }
0193 
0194 #define debugPrintEnum(x) case x:  debug << #x ")"; break
0195 
0196 QDebug operator <<(QDebug debug, const Snore::Notification::CloseReasons &flags)
0197 {
0198     debug.nospace() << "CloseReasons(";
0199     switch (flags) {
0200         debugPrintEnum(Notification::None);
0201         debugPrintEnum(Notification::TimedOut);
0202         debugPrintEnum(Notification::Dismissed);
0203         debugPrintEnum(Notification::Activated);
0204         debugPrintEnum(Notification::Replaced);
0205     }
0206     return debug.space();
0207 }
0208 
0209 QDebug operator<< (QDebug debug, const Snore::Notification::Prioritys &flags)
0210 {
0211     debug.nospace() << "Prioritys(";
0212     switch (flags) {
0213         debugPrintEnum(Notification::Low);
0214         debugPrintEnum(Notification::Normal);
0215         debugPrintEnum(Notification::High);
0216     default:
0217         debug << QByteArray::number(flags, 16) << ")";
0218     }
0219     return debug.space();
0220 }
0221