21 #include <QCoreApplication> 22 #include <QReadWriteLock> 36 m_applicationNameChanged(false)
49 (void)notification->
Setup();
75 foreach (
const QString &item, source)
81 Data->append(QStringLiteral(
" \"%1\" [shape=record style=rounded id=\"%1\" label=<<B>%2</B>%3>];\r\n").arg(
id, label, desc));
84 notification->
Graph(Data);
92 QStringList source = notifier->GetDescription();
93 foreach (
const QString &item, source)
98 Data->append(QStringLiteral(
" \"%1\" [shape=record style=rounded id=\"%1\" label=<<B>%2</B>%3>];\r\n").arg(
id, label, desc));
104 static bool initialised =
false;
105 static QMap<QString,QString> data;
106 static QReadWriteLock *lock =
new QReadWriteLock(QReadWriteLock::Recursive);
108 if (!initialised || m_applicationNameChanged ==
true)
111 QWriteLocker locker(lock);
112 data.insert(QStringLiteral(
"applicationname"), QCoreApplication::applicationName());
117 m_applicationNameChanged =
false;
121 QDateTime datetime = QDateTime::currentDateTime();
122 QTime time = datetime.time();
123 QDate date = datetime.date();
126 QWriteLocker locker(lock);
127 data.insert(QStringLiteral(
"datetime"), datetime.toString(Qt::TextDate));
128 data.insert(QStringLiteral(
"shortdatetime"), datetime.toString(Qt::SystemLocaleShortDate));
129 data.insert(QStringLiteral(
"longdatetime"), datetime.toString(Qt::SystemLocaleLongDate));
130 data.insert(QStringLiteral(
"time"), time.toString(Qt::TextDate));
131 data.insert(QStringLiteral(
"shorttime"), time.toString(Qt::SystemLocaleShortDate));
132 data.insert(QStringLiteral(
"longtime"), time.toString(Qt::SystemLocaleLongDate));
133 data.insert(QStringLiteral(
"date"), date.toString(Qt::TextDate));
134 data.insert(QStringLiteral(
"shortdate"), date.toString(Qt::SystemLocaleShortDate));
135 data.insert(QStringLiteral(
"longdate"), date.toString(Qt::SystemLocaleLongDate));
138 QRegExp regexp(
"%(([^\\|%]+)?\\||\\|(.))?([\\w#]+)(\\|(.+))?%");
139 regexp.setMinimal(
true);
142 QString title = Title;
146 QReadLocker locker(lock);
148 while ((pos = regexp.indexIn(Title, pos)) != -1)
150 QString key = regexp.cap(4).toLower().trimmed();
151 if (data.contains(key))
152 title.replace(regexp.cap(0), QStringLiteral(
"%1%2%3%4").arg(regexp.cap(2), regexp.cap(3), data.value(key), regexp.cap(6)));
153 pos += regexp.matchedLength();
156 while ((pos = regexp.indexIn(Body, pos)) != -1)
158 QString key = regexp.cap(4).toLower().trimmed();
159 if (data.contains(key))
160 body.replace(regexp.cap(0), QStringLiteral(
"%1%2%3%4").arg(regexp.cap(2), regexp.cap(3), data.value(key), regexp.cap(6)));
161 pos += regexp.matchedLength();
166 while ((pos = regexp.indexIn(Title, pos)) != -1)
168 QString key = regexp.cap(4).toLower().trimmed();
169 if (Custom.contains(key))
170 title.replace(regexp.cap(0), QStringLiteral(
"%1%2%3%4").arg(regexp.cap(2), regexp.cap(3), Custom.value(key), regexp.cap(6)));
171 pos += regexp.matchedLength();
174 while ((pos = regexp.indexIn(Body, pos)) != -1)
176 QString key = regexp.cap(4).toLower().trimmed();
177 if (Custom.contains(key))
178 body.replace(regexp.cap(0), QStringLiteral(
"%1%2%3%4").arg(regexp.cap(2), regexp.cap(3), Custom.value(key), regexp.cap(6)));
179 pos += regexp.matchedLength();
187 QMap<QString,QString>::const_iterator it = Custom.constBegin();
188 for ( ; it != Custom.constEnd(); ++it)
189 message.insert(it.key(), it.value());
196 m_applicationNameChanged =
true;
203 QVariantMap::const_iterator ii = Details.constBegin();
204 for ( ; ii != Details.constEnd(); ++ii)
206 if (ii.key() == QStringLiteral(
"notify"))
208 QVariantMap notify = ii.value().toMap();
209 QVariantMap::const_iterator i = notify.constBegin();
210 for ( ; i != notify.constEnd(); ++i)
212 if (i.key() == QStringLiteral(
"notifiers"))
214 QVariantMap notifiers = i.value().toMap();
215 QVariantMap::iterator it = notifiers.begin();
216 for ( ; it != notifiers.end(); ++it)
218 QVariantMap details = it.value().toMap();
219 if (!details.contains(QStringLiteral(
"name")))
221 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Notifier '%1' has no name").arg(it.key()));
231 m_notifiers.append(notifier);
232 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"New notifier '%1'").arg(notifier->
GetUniqueId()));
238 else if (i.key() == QStringLiteral(
"notifications"))
240 QVariantMap notifications = i.value().toMap();
241 QVariantMap::iterator it = notifications.begin();
242 for ( ; it != notifications.end(); ++it)
244 QVariantMap details = it.value().toMap();
245 if (!details.contains(QStringLiteral(
"name")))
247 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Notification '%1' has no name").arg(it.key()));
251 if (!details.contains(QStringLiteral(
"outputs")))
253 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Notification '%1' does not specify 'outputs' (notififiers)").arg(it.key()));
257 if (!details.contains(QStringLiteral(
"message")))
259 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Notificaiton '%1' does not specify 'message'").arg(it.key()));
269 m_notifications.append(notification);
270 LOG(VB_GENERAL, LOG_INFO, QStringLiteral(
"New notification '%1'").arg(notification->
GetUniqueId()));
290 m_notifications.clear();
virtual TorcNotifier * Create(const QString &Type, const QVariantMap &Details)=0
QString GetUserName(void)
void Graph(QByteArray *Data)
virtual bool DownRef(void)
void Create(const QVariantMap &Details) override
TorcNotificationFactory * NextFactory(void) const
virtual void Graph(QByteArray *Data)=0
void ApplicationNameChanged(void)
static TorcNotifierFactory * GetTorcNotifierFactory(void)
void Destroy(void) override
TorcNotifierFactory * NextFactory(void) const
#define NOTIFICATION_BODY
#define LOG(_MASK_, _LEVEL_, _STRING_)
QVariantMap SetNotificationText(const QString &Title, const QString &Body, const QMap< QString, QString > &Custom)
QReadWriteLock m_handlerLock
TorcNotifier * FindNotifierByName(const QString &Name)
#define NOTIFICATION_TITLE
virtual QStringList GetDescription(void)=0
static TorcNotify * gNotify
virtual TorcNotification * Create(const QString &Type, const QVariantMap &Details)=0
QString GetUniqueId(void)
static TorcNotificationFactory * GetTorcNotificationFactory(void)