pgLab/pglab/NotificationService.h

65 lines
1.8 KiB
C
Raw Normal View History

#ifndef NOTIFICATIONSERVICE_H
#define NOTIFICATIONSERVICE_H
#include <QObject>
#include <QString>
#include <QVector>
#include <memory>
#include <shared_mutex>
enum NotificationSeverity {
Informational, ///< Use for notifications of succesful completion and other things the user likely does not need to be bothered with
Warning, ///<
Error, ///< Use for failures like not being able to save the users data
Critical ///< Don't think you should ever need this in a correct program....
};
class Notification: public QObject {
Q_OBJECT
public:
Notification(int64_t id, NotificationSeverity severity, const QString &msg, const QString &detail = {});
int64_t id() const { return m_id; }
NotificationSeverity severity() const { return m_severity; }
QString shortMessage() const { return m_shortMessage; }
QString detailMessage() const { return m_detailMessage; }
private:
int64_t m_id;
NotificationSeverity m_severity;
QString m_shortMessage;
QString m_detailMessage;
};
class NotificationService {
public:
static std::shared_ptr<NotificationService> instance();
NotificationService();
void addInformational(const QString &msg, const QString &detail = {});
void addWarning(const QString &msg, const QString &detail = {});
void addError(const QString &msg, const QString &detail = {});
void add(NotificationSeverity severity, const QString &msg, const QString &detail = {});
/// Returns the number of notifications
int count() const;
const Notification& notification(int index);
signals:
void onNewNotification(const Notification &notification);
private:
static std::shared_ptr<NotificationService> s_instance;
using NotificationContainer = QVector<Notification>;
std::shared_mutex m_nofiticationsMutex;
NotificationContainer m_notifications;
};
Q_DECLARE_METATYPE(NotificationSeverity)
Q_DECLARE_METATYPE(Notification)
#endif // NOTIFICATIONSERVICE_H