diff --git a/src/Cache.cc b/src/Cache.cc
index 8b00a828..8964c0e0 100644
--- a/src/Cache.cc
+++ b/src/Cache.cc
@@ -528,6 +528,34 @@ Cache::roomsWithStateUpdates(const mtx::responses::Sync &res)
return rooms;
}
+RoomInfo
+Cache::singleRoomInfo(const std::string &room_id)
+{
+ auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
+
+ lmdb::val data;
+
+ // Check if the room is joined.
+ if (lmdb::dbi_get(txn, roomsDb_, lmdb::val(room_id), data)) {
+ try {
+ RoomInfo tmp = json::parse(std::string(data.data(), data.size()));
+ tmp.member_count = getMembersDb(txn, room_id).size(txn);
+
+ txn.commit();
+
+ return tmp;
+ } catch (const json::exception &e) {
+ qWarning()
+ << "failed to parse room info:" << QString::fromStdString(room_id)
+ << QString::fromStdString(std::string(data.data(), data.size()));
+ }
+ }
+
+ txn.commit();
+
+ return RoomInfo();
+}
+
std::map<QString, RoomInfo>
Cache::getRoomInfo(const std::vector<std::string> &rooms)
{
@@ -893,11 +921,17 @@ Cache::getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &db)
QImage
Cache::getRoomAvatar(const QString &room_id)
{
+ return getRoomAvatar(room_id.toStdString());
+}
+
+QImage
+Cache::getRoomAvatar(const std::string &room_id)
+{
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
lmdb::val response;
- if (!lmdb::dbi_get(txn, roomsDb_, lmdb::val(room_id.toStdString()), response)) {
+ if (!lmdb::dbi_get(txn, roomsDb_, lmdb::val(room_id), response)) {
txn.commit();
return QImage();
}
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index 4750e67a..b4691fdd 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -38,6 +38,8 @@
#include "UserSettingsPage.h"
#include "Utils.h"
+#include "notifications/Manager.h"
+
#include "dialogs/ReadReceipts.h"
#include "timeline/TimelineViewManager.h"
@@ -864,12 +866,17 @@ ChatPage::sendDesktopNotifications(const mtx::responses::Notifications &res)
}
if (!cache_->isNotificationSent(event_id)) {
- // TODO: send desktop notification
- // qDebug() << "sender" << utils::event_sender(item.event);
- // qDebug() << "body" << utils::event_body(item.event);
+ const auto room_id = QString::fromStdString(item.room_id);
+ const auto user_id = utils::event_sender(item.event);
+ const auto body = utils::event_body(item.event);
// We should only sent one notification per event.
- // cache_->markSentNotification(event_id);
+ cache_->markSentNotification(event_id);
+
+ NotificationsManager::postNotification(
+ QString::fromStdString(cache_->singleRoomInfo(item.room_id).name),
+ Cache::displayName(room_id, user_id),
+ body);
}
} catch (const lmdb::error &e) {
qWarning() << e.what();
diff --git a/src/notifications/ManagerLinux.cpp b/src/notifications/ManagerLinux.cpp
new file mode 100644
index 00000000..a913128e
--- /dev/null
+++ b/src/notifications/ManagerLinux.cpp
@@ -0,0 +1,7 @@
+#include "notifications/Manager.h"
+
+void
+NotificationsManager::postNotification(const QString &, const QString &, const QString &)
+{
+ // TODO: To be implemented
+}
diff --git a/src/notifications/ManagerMac.mm b/src/notifications/ManagerMac.mm
new file mode 100644
index 00000000..48fb46ca
--- /dev/null
+++ b/src/notifications/ManagerMac.mm
@@ -0,0 +1,22 @@
+#include "notifications/Manager.h"
+
+#include <Foundation/Foundation.h>
+#include <QtMac>
+
+@interface NSUserNotification (CFIPrivate)
+- (void)set_identityImage:(NSImage *)image;
+@end
+
+void
+NotificationsManager::postNotification(const QString &roomName, const QString &userName, const QString &message)
+{
+ NSUserNotification * notif = [[NSUserNotification alloc] init];
+
+ notif.title = roomName.toNSString();
+ notif.subtitle = QString("%1 sent a message").arg(userName).toNSString();
+ notif.informativeText = message.toNSString();
+ notif.soundName = NSUserNotificationDefaultSoundName;
+
+ [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification: notif];
+ [notif autorelease];
+}
diff --git a/src/notifications/ManagerWin.cpp b/src/notifications/ManagerWin.cpp
new file mode 100644
index 00000000..a913128e
--- /dev/null
+++ b/src/notifications/ManagerWin.cpp
@@ -0,0 +1,7 @@
+#include "notifications/Manager.h"
+
+void
+NotificationsManager::postNotification(const QString &, const QString &, const QString &)
+{
+ // TODO: To be implemented
+}
|