2016-10-02 09:30:28 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-10-02 09:30:28 +00:00
|
|
|
*/
|
|
|
|
#include "platform/mac/notifications_manager_mac.h"
|
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "platform/platform_specific.h"
|
2019-06-03 14:41:23 +00:00
|
|
|
#include "platform/platform_info.h"
|
2016-10-02 15:44:54 +00:00
|
|
|
#include "platform/mac/mac_utilities.h"
|
2018-01-25 14:19:14 +00:00
|
|
|
#include "history/history.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "mainwindow.h"
|
2018-01-25 14:19:14 +00:00
|
|
|
#include "styles/style_window.h"
|
2016-10-02 15:44:54 +00:00
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
#include <thread>
|
2016-10-02 15:44:54 +00:00
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-03-08 07:31:29 +00:00
|
|
|
static constexpr auto kQuerySettingsEachMs = 1000;
|
|
|
|
auto DoNotDisturbEnabled = false;
|
|
|
|
auto LastSettingsQueryMs = 0;
|
|
|
|
|
|
|
|
void queryDoNotDisturbState() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-03-08 07:31:29 +00:00
|
|
|
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + kQuerySettingsEachMs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastSettingsQueryMs = ms;
|
|
|
|
|
2017-04-18 19:00:21 +00:00
|
|
|
auto userDefaults = [NSUserDefaults alloc];
|
|
|
|
if ([userDefaults respondsToSelector:@selector(initWithSuiteName:)]) {
|
|
|
|
id userDefaultsValue = [[[NSUserDefaults alloc] initWithSuiteName:@"com.apple.notificationcenterui_test"] objectForKey:@"doNotDisturb"];
|
|
|
|
DoNotDisturbEnabled = ([userDefaultsValue boolValue] == YES);
|
|
|
|
} else {
|
|
|
|
DoNotDisturbEnabled = false;
|
|
|
|
}
|
2017-03-08 07:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using Manager = Platform::Notifications::Manager;
|
2016-10-02 15:44:54 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2016-10-02 17:06:34 +00:00
|
|
|
NSImage *qt_mac_create_nsimage(const QPixmap &pm);
|
|
|
|
|
|
|
|
@interface NotificationDelegate : NSObject<NSUserNotificationCenterDelegate> {
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:33:27 +00:00
|
|
|
- (id) initWithManager:(base::weak_ptr<Manager>)manager managerId:(uint64)managerId;
|
2017-04-12 11:58:37 +00:00
|
|
|
- (void) userNotificationCenter:(NSUserNotificationCenter*)center didActivateNotification:(NSUserNotification*)notification;
|
|
|
|
- (BOOL) userNotificationCenter:(NSUserNotificationCenter*)center shouldPresentNotification:(NSUserNotification*)notification;
|
2016-10-02 17:06:34 +00:00
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
@end // @interface NotificationDelegate
|
2016-10-02 17:06:34 +00:00
|
|
|
|
|
|
|
@implementation NotificationDelegate {
|
2017-11-30 17:33:27 +00:00
|
|
|
base::weak_ptr<Manager> _manager;
|
2017-04-12 12:50:35 +00:00
|
|
|
uint64 _managerId;
|
2017-03-08 07:31:05 +00:00
|
|
|
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 17:33:27 +00:00
|
|
|
- (id) initWithManager:(base::weak_ptr<Manager>)manager managerId:(uint64)managerId {
|
2016-10-02 17:06:34 +00:00
|
|
|
if (self = [super init]) {
|
2017-03-08 07:31:05 +00:00
|
|
|
_manager = manager;
|
2017-04-12 12:50:35 +00:00
|
|
|
_managerId = managerId;
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
|
|
|
|
NSDictionary *notificationUserInfo = [notification userInfo];
|
2017-04-12 12:50:35 +00:00
|
|
|
NSNumber *managerIdObject = [notificationUserInfo objectForKey:@"manager"];
|
|
|
|
auto notificationManagerId = managerIdObject ? [managerIdObject unsignedLongLongValue] : 0ULL;
|
|
|
|
DEBUG_LOG(("Received notification with instance %1, mine: %2").arg(notificationManagerId).arg(_managerId));
|
|
|
|
if (notificationManagerId != _managerId) { // other app instance notification
|
2017-12-30 21:28:38 +00:00
|
|
|
crl::on_main([] {
|
2017-04-12 11:58:37 +00:00
|
|
|
// Usually we show and activate main window when the application
|
|
|
|
// is activated (receives applicationDidBecomeActive: notification).
|
|
|
|
//
|
|
|
|
// This is used for window show in Cmd+Tab switching to the application.
|
|
|
|
//
|
|
|
|
// But when a notification arrives sometimes macOS still activates the app
|
|
|
|
// and we receive applicationDidBecomeActive: notification even if the
|
|
|
|
// notification was sent by another instance of the application. In that case
|
|
|
|
// we set a flag for a couple of seconds to ignore this app activation.
|
|
|
|
objc_ignoreApplicationActivationRightNow();
|
|
|
|
});
|
2016-10-02 17:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSNumber *peerObject = [notificationUserInfo objectForKey:@"peer"];
|
|
|
|
auto notificationPeerId = peerObject ? [peerObject unsignedLongLongValue] : 0ULL;
|
|
|
|
if (!notificationPeerId) {
|
2017-04-12 11:58:37 +00:00
|
|
|
LOG(("App Error: A notification with unknown peer was received"));
|
2016-10-02 17:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSNumber *msgObject = [notificationUserInfo objectForKey:@"msgid"];
|
|
|
|
auto notificationMsgId = msgObject ? [msgObject intValue] : 0;
|
|
|
|
if (notification.activationType == NSUserNotificationActivationTypeReplied) {
|
2017-12-30 21:28:38 +00:00
|
|
|
const auto notificationReply = QString::fromUtf8([[[notification response] string] UTF8String]);
|
|
|
|
const auto manager = _manager;
|
|
|
|
crl::on_main(manager, [=] {
|
2018-05-24 13:03:21 +00:00
|
|
|
manager->notificationReplied(
|
|
|
|
notificationPeerId,
|
|
|
|
notificationMsgId,
|
|
|
|
{ notificationReply, {} });
|
2017-04-12 11:58:37 +00:00
|
|
|
});
|
2016-10-02 17:06:34 +00:00
|
|
|
} else if (notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
|
2017-12-30 21:28:38 +00:00
|
|
|
const auto manager = _manager;
|
|
|
|
crl::on_main(manager, [=] {
|
|
|
|
manager->notificationActivated(notificationPeerId, notificationMsgId);
|
2017-04-12 11:58:37 +00:00
|
|
|
});
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[center removeDeliveredNotification: notification];
|
|
|
|
}
|
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
- (BOOL) userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
|
2016-10-02 17:06:34 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-04-12 11:58:37 +00:00
|
|
|
@end // @implementation NotificationDelegate
|
2016-10-02 17:06:34 +00:00
|
|
|
|
|
|
|
namespace Platform {
|
|
|
|
namespace Notifications {
|
|
|
|
|
2017-03-08 07:31:29 +00:00
|
|
|
bool SkipAudio() {
|
|
|
|
queryDoNotDisturbState();
|
|
|
|
return DoNotDisturbEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkipToast() {
|
|
|
|
if (Supported()) {
|
|
|
|
// Do not skip native notifications because of Do not disturb.
|
|
|
|
// They respect this setting anyway.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
queryDoNotDisturbState();
|
|
|
|
return DoNotDisturbEnabled;
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
bool Supported() {
|
2019-06-03 14:41:23 +00:00
|
|
|
return Platform::IsMac10_8OrGreater();
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
std::unique_ptr<Window::Notifications::Manager> Create(Window::Notifications::System *system) {
|
|
|
|
if (Supported()) {
|
|
|
|
return std::make_unique<Manager>(system);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2017-03-02 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void FlashBounce() {
|
|
|
|
[NSApp requestUserAttention:NSInformationalRequest];
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
class Manager::Private : public QObject, private base::Subscriber {
|
2016-10-02 13:54:27 +00:00
|
|
|
public:
|
2017-03-08 07:31:05 +00:00
|
|
|
Private(Manager *manager);
|
2017-04-12 11:58:37 +00:00
|
|
|
|
2016-10-08 08:38:53 +00:00
|
|
|
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton);
|
2016-10-02 13:54:27 +00:00
|
|
|
void clearAll();
|
|
|
|
void clearFromHistory(History *history);
|
2016-10-02 17:06:34 +00:00
|
|
|
void updateDelegate();
|
2016-10-02 13:54:27 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
~Private();
|
2016-10-02 13:54:27 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-12 12:50:35 +00:00
|
|
|
template <typename Task>
|
|
|
|
void putClearTask(Task task);
|
|
|
|
|
|
|
|
void clearingThreadLoop();
|
|
|
|
|
|
|
|
const uint64 _managerId = 0;
|
|
|
|
QString _managerIdString;
|
|
|
|
|
2017-03-08 07:31:05 +00:00
|
|
|
NotificationDelegate *_delegate = nullptr;
|
2016-10-02 13:54:27 +00:00
|
|
|
|
2017-04-12 12:50:35 +00:00
|
|
|
std::thread _clearingThread;
|
|
|
|
std::mutex _clearingMutex;
|
|
|
|
std::condition_variable _clearingCondition;
|
|
|
|
|
|
|
|
struct ClearFromHistory {
|
|
|
|
PeerId peerId;
|
|
|
|
};
|
|
|
|
struct ClearAll {
|
|
|
|
};
|
|
|
|
struct ClearFinish {
|
|
|
|
};
|
|
|
|
using ClearTask = base::variant<ClearFromHistory, ClearAll, ClearFinish>;
|
|
|
|
std::vector<ClearTask> _clearingTasks;
|
|
|
|
|
2016-10-02 13:54:27 +00:00
|
|
|
};
|
|
|
|
|
2017-03-08 07:31:05 +00:00
|
|
|
Manager::Private::Private(Manager *manager)
|
2017-04-12 12:50:35 +00:00
|
|
|
: _managerId(rand_value<uint64>())
|
|
|
|
, _managerIdString(QString::number(_managerId))
|
2017-04-15 17:33:46 +00:00
|
|
|
, _delegate([[NotificationDelegate alloc] initWithManager:manager managerId:_managerId]) {
|
2017-03-04 19:36:59 +00:00
|
|
|
updateDelegate();
|
|
|
|
subscribe(Global::RefWorkMode(), [this](DBIWorkMode mode) {
|
|
|
|
// We need to update the delegate _after_ the tray icon change was done in Qt.
|
|
|
|
// Because Qt resets the delegate.
|
2017-12-30 21:28:38 +00:00
|
|
|
crl::on_main(this, [=] {
|
2017-03-04 19:36:59 +00:00
|
|
|
updateDelegate();
|
2017-12-30 21:28:38 +00:00
|
|
|
});
|
2017-03-04 19:36:59 +00:00
|
|
|
});
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
|
2016-10-02 15:44:54 +00:00
|
|
|
@autoreleasepool {
|
2016-10-02 13:54:27 +00:00
|
|
|
|
2016-10-02 15:44:54 +00:00
|
|
|
NSUserNotification *notification = [[[NSUserNotification alloc] init] autorelease];
|
|
|
|
if ([notification respondsToSelector:@selector(setIdentifier:)]) {
|
2017-04-12 12:50:35 +00:00
|
|
|
auto identifier = _managerIdString + '_' + QString::number(peer->id) + '_' + QString::number(msgId);
|
2016-10-02 15:44:54 +00:00
|
|
|
auto identifierValue = Q2NSString(identifier);
|
|
|
|
[notification setIdentifier:identifierValue];
|
|
|
|
}
|
2017-04-12 12:50:35 +00:00
|
|
|
[notification setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedLongLong:peer->id],@"peer",[NSNumber numberWithInt:msgId],@"msgid",[NSNumber numberWithUnsignedLongLong:_managerId],@"manager",nil]];
|
2016-10-02 13:54:27 +00:00
|
|
|
|
2016-10-02 15:44:54 +00:00
|
|
|
[notification setTitle:Q2NSString(title)];
|
|
|
|
[notification setSubtitle:Q2NSString(subtitle)];
|
|
|
|
[notification setInformativeText:Q2NSString(msg)];
|
2016-10-08 08:38:53 +00:00
|
|
|
if (!hideNameAndPhoto && [notification respondsToSelector:@selector(setContentImage:)]) {
|
2016-10-02 13:54:27 +00:00
|
|
|
auto userpic = peer->genUserpic(st::notifyMacPhotoSize);
|
2016-10-02 15:44:54 +00:00
|
|
|
NSImage *img = [qt_mac_create_nsimage(userpic) autorelease];
|
2016-10-02 13:54:27 +00:00
|
|
|
[notification setContentImage:img];
|
|
|
|
}
|
|
|
|
|
2016-10-08 08:38:53 +00:00
|
|
|
if (!hideReplyButton && [notification respondsToSelector:@selector(setHasReplyButton:)]) {
|
2016-10-02 13:54:27 +00:00
|
|
|
[notification setHasReplyButton:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
[notification setSoundName:nil];
|
|
|
|
|
2016-10-02 15:44:54 +00:00
|
|
|
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
2016-10-02 13:54:27 +00:00
|
|
|
[center deliverNotification:notification];
|
|
|
|
|
2016-10-02 15:44:54 +00:00
|
|
|
}
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:50:35 +00:00
|
|
|
void Manager::Private::clearingThreadLoop() {
|
|
|
|
auto finished = false;
|
|
|
|
while (!finished) {
|
|
|
|
auto clearAll = false;
|
|
|
|
auto clearFromPeers = std::set<PeerId>(); // Better to use flatmap.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_clearingMutex);
|
|
|
|
|
|
|
|
while (_clearingTasks.empty()) {
|
|
|
|
_clearingCondition.wait(lock);
|
|
|
|
}
|
|
|
|
for (auto &task : _clearingTasks) {
|
|
|
|
if (base::get_if<ClearFinish>(&task)) {
|
|
|
|
finished = true;
|
|
|
|
clearAll = true;
|
|
|
|
} else if (base::get_if<ClearAll>(&task)) {
|
|
|
|
clearAll = true;
|
|
|
|
} else if (auto fromHistory = base::get_if<ClearFromHistory>(&task)) {
|
|
|
|
clearFromPeers.insert(fromHistory->peerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_clearingTasks.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto clearByPeer = [&clearFromPeers](NSDictionary *notificationUserInfo) {
|
|
|
|
if (NSNumber *peerObject = [notificationUserInfo objectForKey:@"peer"]) {
|
|
|
|
auto notificationPeerId = [peerObject unsignedLongLongValue];
|
|
|
|
if (notificationPeerId) {
|
|
|
|
return (clearFromPeers.find(notificationPeerId) != clearFromPeers.cend());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
|
|
|
NSArray *notificationsList = [center deliveredNotifications];
|
|
|
|
for (id notification in notificationsList) {
|
|
|
|
NSDictionary *notificationUserInfo = [notification userInfo];
|
|
|
|
NSNumber *managerIdObject = [notificationUserInfo objectForKey:@"manager"];
|
|
|
|
auto notificationManagerId = managerIdObject ? [managerIdObject unsignedLongLongValue] : 0ULL;
|
|
|
|
if (notificationManagerId == _managerId) {
|
|
|
|
if (clearAll || clearByPeer(notificationUserInfo)) {
|
|
|
|
[center removeDeliveredNotification:notification];
|
|
|
|
}
|
|
|
|
}
|
2016-10-02 15:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 12:50:35 +00:00
|
|
|
template <typename Task>
|
|
|
|
void Manager::Private::putClearTask(Task task) {
|
2017-04-15 17:33:46 +00:00
|
|
|
if (!_clearingThread.joinable()) {
|
|
|
|
_clearingThread = std::thread([this] { clearingThreadLoop(); });
|
|
|
|
}
|
|
|
|
|
2017-04-12 12:50:35 +00:00
|
|
|
std::unique_lock<std::mutex> lock(_clearingMutex);
|
|
|
|
_clearingTasks.push_back(task);
|
|
|
|
_clearingCondition.notify_one();
|
|
|
|
}
|
2016-10-02 13:54:27 +00:00
|
|
|
|
2017-04-12 12:50:35 +00:00
|
|
|
void Manager::Private::clearAll() {
|
|
|
|
putClearTask(ClearAll());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Private::clearFromHistory(History *history) {
|
|
|
|
putClearTask(ClearFromHistory { history->peer->id });
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::updateDelegate() {
|
2016-10-02 17:06:34 +00:00
|
|
|
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
|
|
|
[center setDelegate:_delegate];
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
Manager::Private::~Private() {
|
2017-04-15 17:33:46 +00:00
|
|
|
if (_clearingThread.joinable()) {
|
|
|
|
putClearTask(ClearFinish());
|
|
|
|
_clearingThread.join();
|
|
|
|
}
|
2018-10-10 05:29:31 +00:00
|
|
|
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
|
|
|
|
[center setDelegate:nil];
|
2016-10-02 17:06:34 +00:00
|
|
|
[_delegate release];
|
2016-10-02 15:44:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
Manager::Manager(Window::Notifications::System *system) : NativeManager(system)
|
2017-03-08 07:31:05 +00:00
|
|
|
, _private(std::make_unique<Private>(this)) {
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 13:54:27 +00:00
|
|
|
Manager::~Manager() = default;
|
|
|
|
|
2016-10-08 08:38:53 +00:00
|
|
|
void Manager::doShowNativeNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearAllFast() {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearAll();
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearFromHistory(History *history) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearFromHistory(history);
|
2016-10-02 13:54:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Platform
|