Implement desktop notification for mac

This commit is contained in:
Konstantinos Sideris 2018-05-05 22:40:24 +03:00
parent ed9501023a
commit 506cf68072
8 changed files with 107 additions and 7 deletions

View file

@ -24,8 +24,8 @@ fix_project_version()
# Set additional project information
set(COMPANY "Nheko")
set(COPYRIGHT "Copyright (c) 2017 Mujx")
set(IDENTIFIER "com.mujx.nheko")
set(COPYRIGHT "Copyright (c) 2017 Nheko Contributors")
set(IDENTIFIER "com.github.mujx.nheko")
add_project_meta(META_FILES_TO_INCLUDE)
@ -305,6 +305,15 @@ else()
set(NHEKO_LIBS ${COMMON_LIBS} ${LMDB_LIBRARY})
endif()
if (APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation")
set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm)
elseif (WIN32)
set(SRC_FILES ${SRC_FILES} src/notifications/ManagerWin.cpp)
else ()
set(SRC_FILES ${SRC_FILES} src/notifications/ManagerLinux.cpp)
endif ()
set(NHEKO_DEPS
${SRC_FILES}
${UI_HEADERS}

View file

@ -196,6 +196,7 @@ public:
//! Retrieves the saved room avatar.
QImage getRoomAvatar(const QString &id);
QImage getRoomAvatar(const std::string &id);
//! Adds a user to the read list for the given event.
//!
@ -220,6 +221,7 @@ public:
}
void saveImage(const QString &url, const QByteArray &data);
RoomInfo singleRoomInfo(const std::string &room_id);
std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
std::map<QString, RoomInfo> roomUpdates(const mtx::responses::Sync &sync)

View file

@ -0,0 +1,12 @@
#pragma once
#include <QImage>
#include <QString>
class NotificationsManager
{
public:
static void postNotification(const QString &room,
const QString &user,
const QString &message);
};

View file

@ -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)
{
@ -892,12 +920,18 @@ 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();
}

View file

@ -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();

View file

@ -0,0 +1,7 @@
#include "notifications/Manager.h"
void
NotificationsManager::postNotification(const QString &, const QString &, const QString &)
{
// TODO: To be implemented
}

View file

@ -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];
}

View file

@ -0,0 +1,7 @@
#include "notifications/Manager.h"
void
NotificationsManager::postNotification(const QString &, const QString &, const QString &)
{
// TODO: To be implemented
}