summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-05 22:40:24 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-05-05 22:40:24 +0300
commit506cf680728bd7f9124354be8ec6112d973fdac6 (patch)
tree1ebe25b2e2bb019e26a8b039e79646567e8c363e /src
parentAdd support for retrieving the notification events (#33) (diff)
downloadnheko-506cf680728bd7f9124354be8ec6112d973fdac6.tar.xz
Implement desktop notification for mac
Diffstat (limited to 'src')
-rw-r--r--src/Cache.cc36
-rw-r--r--src/ChatPage.cc15
-rw-r--r--src/notifications/ManagerLinux.cpp7
-rw-r--r--src/notifications/ManagerMac.mm22
-rw-r--r--src/notifications/ManagerWin.cpp7
5 files changed, 82 insertions, 5 deletions
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 +}