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 #ifndef NOTIFICATION_H 0020 #define NOTIFICATION_H 0021 #include "libsnore/snore_exports.h" 0022 #include "icon.h" 0023 #include "notificationaction.h" 0024 #include "libsnore/hint.h" 0025 #include "libsnore/application.h" 0026 #include "libsnore/utils.h" 0027 0028 #include <QDebug> 0029 0030 namespace Snore 0031 { 0032 0033 class NotificationData; 0034 class SnorePlugin; 0035 /** 0036 * Notification contains all relevant data to notify the user. 0037 * Notification uses a shared datamodel, it's content is never copied and automatically released. 0038 * 0039 * @author Hannah von Reth \<vonreth at kde.org\> 0040 */ 0041 0042 class SNORE_EXPORT Notification 0043 { 0044 public: 0045 /** 0046 * The reason why the Notification was closed. 0047 */ 0048 enum CloseReasons { 0049 /** 0050 * The default value, the notification was not closed. 0051 */ 0052 None = 0, 0053 0054 /** 0055 * The Notification was closed becaouse it timed out. 0056 */ 0057 TimedOut = 1, 0058 0059 /** 0060 * The Notification was dismissed by the user, close button. 0061 */ 0062 Dismissed = 2, 0063 0064 /** 0065 * The Notification was activated, an action was invoked. 0066 * @see actionInvoked() 0067 */ 0068 Activated = 3, 0069 0070 /** 0071 * @deprecated same as ACTIVATED 0072 */ 0073 Closed = 3, 0074 0075 /** 0076 * The notification was replaced by an update. 0077 * This value will be used if a notification backend does not support updating. 0078 * 0079 */ 0080 Replaced = 4 0081 }; 0082 Q_ENUMS(CloseReasons) 0083 0084 /** 0085 * The Priority for the Notification. 0086 * Some notification systems support this flag to filter notifications or indicate different prioritys by color. 0087 */ 0088 enum Prioritys { 0089 /** 0090 * Indicates the lowes priority. The backend might ignore the notification. 0091 */ 0092 Lowest = -2, 0093 0094 /** 0095 * Indicates a low priority. 0096 */ 0097 Low = -1, 0098 0099 /** 0100 * The default priority. 0101 */ 0102 Normal = 0, 0103 0104 /** 0105 * Indicates a priority above the normal level. 0106 */ 0107 High = +1, 0108 0109 /** 0110 * Indicates a emegency priority, the notifications is sticky and should be acknowlegded. 0111 */ 0112 Emergency = +2 0113 }; 0114 0115 Notification(); 0116 /** 0117 * Creates a new Notification. 0118 * @param application the application emitting the Notification 0119 * @param alert the associated alert 0120 * @param title the title 0121 * @param text the text body 0122 * @param icon the icon 0123 * @param timeout the timeout 0124 * @param priority the priority 0125 */ 0126 explicit Notification(const Application &application, const Alert &alert, const QString &title, const QString &text, const Icon &icon, int timeout = defaultTimeout(), Notification::Prioritys priority = Normal); 0127 0128 /** 0129 * Creates and update Notification replacing an existing Notification 0130 * @param old the notification to replace 0131 * @param title the new title 0132 * @param text the new text body 0133 * @param icon the icon 0134 * @param timeout the timeout 0135 * @param priority the piority 0136 */ 0137 explicit Notification(const Notification &old, const QString &title, const QString &text, const Icon &icon, int timeout = defaultTimeout(), Snore::Notification::Prioritys priority = Normal); 0138 0139 /** 0140 * The copy constructor 0141 * @param other 0142 */ 0143 Notification(const Notification &other); 0144 0145 /** 0146 * The copy operator 0147 * @param other 0148 */ 0149 Notification &operator= (const Notification &other); 0150 ~Notification(); 0151 0152 /** 0153 * 0154 * @return the internal id 0155 */ 0156 uint id() const; 0157 0158 /** 0159 * The timeout in seconds. 0160 * A timeout of 0 means the notification isSticky and will stay visible until dismissed by the user, if supported by the backend. 0161 * @see isSticky 0162 */ 0163 int timeout() const; 0164 0165 /** 0166 * 0167 * @return a valid Action if one was invoked otherwise an invalid. 0168 */ 0169 const Action &actionInvoked() const; 0170 0171 /** 0172 * 0173 * @return the associated application 0174 */ 0175 Application &application() const; 0176 0177 /** 0178 * Returns the title of the notification. 0179 * @param flags the supported markup flags. 0180 */ 0181 QString title(Utils::MarkupFlags flags = Utils::NoMarkup) const; 0182 0183 /** 0184 * Returns the notification text. 0185 * @param flags the supported markup flags. 0186 */ 0187 QString text(Utils::MarkupFlags flags = Utils::NoMarkup) const; 0188 0189 /** 0190 * 0191 * @return the icon 0192 */ 0193 const Icon &icon() const; 0194 0195 /** 0196 * 0197 * @return the associated alert 0198 */ 0199 const Alert &alert() const; 0200 0201 /** 0202 * A sticki notification will stay visible until dismissed, if supported by the backend. 0203 * @return true if the timeout is 0 0204 */ 0205 bool isSticky() const; 0206 0207 /** 0208 * Some backends support priorities to indicate the urgency of the Notification 0209 * @return the priority 0210 */ 0211 Notification::Prioritys priority() const; 0212 0213 /** 0214 * 0215 * @return the available actions 0216 * @see addAction 0217 */ 0218 const QHash<int, Action> &actions() const; 0219 0220 /** 0221 * Adds an Action to the Notification 0222 * @param a the action 0223 * @see actions 0224 */ 0225 void addAction(const Action &a); 0226 0227 /** 0228 * 0229 * @return the close reason 0230 */ 0231 const Notification::CloseReasons &closeReason(); 0232 0233 /** 0234 * Returns notification specific hints. 0235 * A notification inherits the hints of its application. 0236 * @see Application::hints() 0237 */ 0238 Hint &hints(); 0239 0240 /** 0241 * 0242 * @return hints associated with this notification 0243 */ 0244 const Hint &constHints() const; 0245 0246 /** 0247 * 0248 * @return whether this is a valid Notification 0249 */ 0250 bool isValid() const; 0251 0252 /** 0253 * 0254 * @return the old notification to be replaced 0255 * @see isUpdate 0256 */ 0257 Notification &old() const; 0258 0259 /** 0260 * 0261 * @return whether this is an update to replace an old notification 0262 * @see old 0263 */ 0264 bool isUpdate() const; 0265 0266 /** 0267 * 0268 * @return a data pointer for internal use 0269 */ 0270 NotificationData *data(); 0271 0272 /** 0273 * 0274 * @return the default timeout 0275 */ 0276 static int defaultTimeout(); 0277 0278 //TODO: find a better name. 0279 void addActiveIn(const QObject *o); 0280 bool isActiveIn(const QObject *o) const; 0281 bool removeActiveIn(const QObject *o); 0282 0283 inline bool operator == (const Notification &other); 0284 0285 private: 0286 QExplicitlySharedDataPointer<NotificationData> d; 0287 0288 friend class NotificationData; 0289 }; 0290 0291 inline bool Notification::operator == (const Notification &other) 0292 { 0293 return id() == other.id(); 0294 } 0295 0296 } 0297 Q_DECLARE_METATYPE(Snore::Notification) 0298 0299 QDataStream &operator<< (QDataStream &stream, const Snore::Notification ¬i); 0300 0301 SNORE_EXPORT QDebug operator<< (QDebug, const Snore::Notification::CloseReasons &); 0302 0303 SNORE_EXPORT QDebug operator<< (QDebug, const Snore::Notification::Prioritys &); 0304 0305 inline QDebug operator<< (QDebug debug, const Snore::Notification ¬i) 0306 { 0307 if (noti.isValid()) { 0308 debug.nospace() << "Snore::Notification(" << noti.title() << ", " << noti.text() << ", id = " << noti.id(); 0309 if (noti.isUpdate()) { 0310 debug << ", oldID = " << noti.old().id(); 0311 } 0312 debug << ")" ; 0313 } else { 0314 debug.nospace() << "Snore::Notification(0x00)" ; 0315 } 0316 return debug.maybeSpace(); 0317 } 0318 0319 #endif // NOTIFICATION_H