2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2018-05-05 22:40:24 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QImage>
|
2018-07-11 17:33:02 +03:00
|
|
|
#include <QObject>
|
2018-05-05 22:40:24 +03:00
|
|
|
#include <QString>
|
|
|
|
|
2021-02-13 20:59:50 +03:00
|
|
|
#include <mtx/responses/notifications.hpp>
|
2021-02-13 20:10:49 +03:00
|
|
|
|
2021-03-17 21:17:57 +03:00
|
|
|
// convenience definition
|
2021-02-03 23:40:14 +03:00
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(Q_OS_HAIKU)
|
2021-03-17 21:17:57 +03:00
|
|
|
#define NHEKO_DBUS_SYS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NHEKO_DBUS_SYS)
|
2018-07-11 17:33:02 +03:00
|
|
|
#include <QtDBus/QDBusArgument>
|
|
|
|
#include <QtDBus/QDBusInterface>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct roomEventId
|
2018-05-05 22:40:24 +03:00
|
|
|
{
|
2018-07-11 17:33:02 +03:00
|
|
|
QString roomId;
|
|
|
|
QString eventId;
|
|
|
|
};
|
|
|
|
|
2020-04-09 21:52:50 +03:00
|
|
|
inline bool
|
|
|
|
operator==(const roomEventId &a, const roomEventId &b)
|
|
|
|
{
|
|
|
|
return a.roomId == b.roomId && a.eventId == b.eventId;
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:33:02 +03:00
|
|
|
class NotificationsManager : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2018-05-05 22:40:24 +03:00
|
|
|
public:
|
2018-07-11 17:33:02 +03:00
|
|
|
NotificationsManager(QObject *parent = nullptr);
|
|
|
|
|
2021-02-13 20:10:49 +03:00
|
|
|
void postNotification(const mtx::responses::Notification ¬ification, const QImage &icon);
|
2018-07-11 17:33:02 +03:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void notificationClicked(const QString roomId, const QString eventId);
|
2021-01-07 12:44:59 +03:00
|
|
|
void sendNotificationReply(const QString roomId, const QString eventId, const QString body);
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2020-04-09 21:52:50 +03:00
|
|
|
public slots:
|
|
|
|
void removeNotification(const QString &roomId, const QString &eventId);
|
|
|
|
|
2021-02-16 02:36:10 +03:00
|
|
|
private:
|
2021-03-17 21:17:57 +03:00
|
|
|
QString cacheImage(const mtx::events::collections::TimelineEvents &event);
|
|
|
|
QString formatNotification(const mtx::responses::Notification ¬ification);
|
2021-02-16 02:36:10 +03:00
|
|
|
|
2021-03-17 21:17:57 +03:00
|
|
|
#if defined(NHEKO_DBUS_SYS)
|
2020-04-09 21:52:50 +03:00
|
|
|
public:
|
|
|
|
void closeNotifications(QString roomId);
|
|
|
|
|
2018-07-11 17:33:02 +03:00
|
|
|
private:
|
|
|
|
QDBusInterface dbus;
|
2021-03-17 21:17:57 +03:00
|
|
|
|
|
|
|
void systemPostNotification(const QString &room_id,
|
|
|
|
const QString &event_id,
|
|
|
|
const QString &roomName,
|
|
|
|
const QString &text,
|
|
|
|
const QImage &icon);
|
2020-04-09 21:52:50 +03:00
|
|
|
void closeNotification(uint id);
|
2018-07-11 17:33:02 +03:00
|
|
|
|
|
|
|
// notification ID to (room ID, event ID)
|
|
|
|
QMap<uint, roomEventId> notificationIds;
|
2021-03-17 21:17:57 +03:00
|
|
|
|
|
|
|
const bool hasMarkup_;
|
|
|
|
const bool hasImages_;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(Q_OS_MACOS)
|
|
|
|
private:
|
|
|
|
// Objective-C(++) doesn't like to do lots of regular C++, so the actual notification
|
|
|
|
// posting is split out
|
|
|
|
void objCxxPostNotification(const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &informativeText,
|
2021-03-03 04:35:03 +03:00
|
|
|
const QImage &bodyImage);
|
2021-03-17 21:17:57 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(Q_OS_WINDOWS)
|
|
|
|
private:
|
2021-02-26 23:38:15 +03:00
|
|
|
void systemPostNotification(const QString &line1,
|
|
|
|
const QString &line2,
|
|
|
|
const QString &iconPath);
|
2018-07-11 17:33:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// these slots are platform specific (D-Bus only)
|
|
|
|
// but Qt slot declarations can not be inside an ifdef!
|
|
|
|
private slots:
|
|
|
|
void actionInvoked(uint id, QString action);
|
|
|
|
void notificationClosed(uint id, uint reason);
|
2021-01-07 12:44:59 +03:00
|
|
|
void notificationReplied(uint id, QString reply);
|
2018-05-05 22:40:24 +03:00
|
|
|
};
|
2018-07-11 17:33:02 +03:00
|
|
|
|
2021-03-17 21:17:57 +03:00
|
|
|
#if defined(NHEKO_DBUS_SYS)
|
2018-07-11 17:33:02 +03:00
|
|
|
QDBusArgument &
|
|
|
|
operator<<(QDBusArgument &arg, const QImage &image);
|
|
|
|
const QDBusArgument &
|
|
|
|
operator>>(const QDBusArgument &arg, QImage &);
|
|
|
|
#endif
|