2016-10-02 09:30:28 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-10-02 09:30:28 +00:00
|
|
|
*/
|
|
|
|
#include "platform/linux/notifications_manager_linux.h"
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
#include "window/notifications_utilities.h"
|
|
|
|
#include "platform/linux/linux_libnotify.h"
|
|
|
|
#include "platform/linux/linux_libs.h"
|
2016-10-03 15:07:50 +00:00
|
|
|
#include "lang.h"
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
namespace Platform {
|
|
|
|
namespace Notifications {
|
2016-10-03 08:56:03 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
NeverFreedPointer<Manager> ManagerInstance;
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
bool LibNotifyLoaded() {
|
2016-10-03 08:56:03 +00:00
|
|
|
return (Libs::notify_init != nullptr)
|
|
|
|
&& (Libs::notify_uninit != nullptr)
|
|
|
|
&& (Libs::notify_is_initted != nullptr)
|
|
|
|
// && (Libs::notify_get_app_name != nullptr)
|
|
|
|
// && (Libs::notify_set_app_name != nullptr)
|
|
|
|
&& (Libs::notify_get_server_caps != nullptr)
|
|
|
|
&& (Libs::notify_get_server_info != nullptr)
|
|
|
|
&& (Libs::notify_notification_new != nullptr)
|
2016-10-04 13:36:50 +00:00
|
|
|
// && (Libs::notify_notification_update != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
&& (Libs::notify_notification_show != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_app_name != nullptr)
|
2016-10-03 15:07:50 +00:00
|
|
|
&& (Libs::notify_notification_set_timeout != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
// && (Libs::notify_notification_set_category != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_urgency != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_icon_from_pixbuf != nullptr)
|
|
|
|
&& (Libs::notify_notification_set_image_from_pixbuf != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_hint != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_hint_int32 != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_hint_uint32 != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_hint_double != nullptr)
|
2016-10-03 15:07:50 +00:00
|
|
|
&& (Libs::notify_notification_set_hint_string != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
// && (Libs::notify_notification_set_hint_byte != nullptr)
|
|
|
|
// && (Libs::notify_notification_set_hint_byte_array != nullptr)
|
|
|
|
// && (Libs::notify_notification_clear_hints != nullptr)
|
2016-10-03 15:07:50 +00:00
|
|
|
&& (Libs::notify_notification_add_action != nullptr)
|
|
|
|
&& (Libs::notify_notification_clear_actions != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
&& (Libs::notify_notification_close != nullptr)
|
|
|
|
&& (Libs::notify_notification_get_closed_reason != nullptr)
|
2016-10-03 15:07:50 +00:00
|
|
|
&& (Libs::g_object_ref_sink != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
&& (Libs::g_object_unref != nullptr)
|
2016-10-03 15:07:50 +00:00
|
|
|
&& (Libs::g_list_free_full != nullptr)
|
|
|
|
&& (Libs::g_error_free != nullptr)
|
|
|
|
&& (Libs::g_signal_connect_data != nullptr)
|
|
|
|
&& (Libs::g_signal_handler_disconnect != nullptr)
|
2016-10-03 08:56:03 +00:00
|
|
|
// && (Libs::gdk_pixbuf_new_from_data != nullptr)
|
|
|
|
&& (Libs::gdk_pixbuf_new_from_file != nullptr);
|
|
|
|
}
|
|
|
|
|
2016-10-22 13:57:13 +00:00
|
|
|
QString escapeHtml(const QString &text) {
|
|
|
|
auto result = QString();
|
|
|
|
auto copyFrom = 0, textSize = text.size();
|
|
|
|
auto data = text.constData();
|
|
|
|
for (auto i = 0; i != textSize; ++i) {
|
|
|
|
auto ch = data[i];
|
|
|
|
if (ch == '<' || ch == '>' || ch == '&') {
|
|
|
|
if (!copyFrom) {
|
|
|
|
result.reserve(textSize * 5);
|
|
|
|
}
|
|
|
|
if (i > copyFrom) {
|
|
|
|
result.append(data + copyFrom, i - copyFrom);
|
|
|
|
}
|
|
|
|
switch (ch.unicode()) {
|
|
|
|
case '<': result.append(qstr("<")); break;
|
|
|
|
case '>': result.append(qstr(">")); break;
|
|
|
|
case '&': result.append(qstr("&")); break;
|
|
|
|
}
|
|
|
|
copyFrom = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (copyFrom > 0) {
|
|
|
|
result.append(data + copyFrom, textSize - copyFrom);
|
|
|
|
return result;
|
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationData {
|
|
|
|
public:
|
2016-10-04 13:36:50 +00:00
|
|
|
NotificationData(const QString &title, const QString &body, const QStringList &capabilities, PeerId peerId, MsgId msgId)
|
|
|
|
: _data(Libs::notify_notification_new(title.toUtf8().constData(), body.toUtf8().constData(), nullptr)) {
|
2016-10-03 15:07:50 +00:00
|
|
|
if (valid()) {
|
2016-10-04 13:36:50 +00:00
|
|
|
init(capabilities, peerId, msgId);
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool valid() const {
|
|
|
|
return (_data != nullptr);
|
|
|
|
}
|
|
|
|
NotificationData(const NotificationData &other) = delete;
|
|
|
|
NotificationData &operator=(const NotificationData &other) = delete;
|
|
|
|
NotificationData(NotificationData &&other) = delete;
|
|
|
|
NotificationData &operator=(NotificationData &&other) = delete;
|
|
|
|
|
|
|
|
void setImage(const QString &imagePath) {
|
|
|
|
auto imagePathNative = QFile::encodeName(imagePath);
|
|
|
|
if (auto pixbuf = Libs::gdk_pixbuf_new_from_file(imagePathNative.constData(), nullptr)) {
|
|
|
|
Libs::notify_notification_set_image_from_pixbuf(_data, pixbuf);
|
|
|
|
Libs::g_object_unref(Libs::g_object_cast(pixbuf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool show() {
|
|
|
|
if (valid()) {
|
|
|
|
GError *error = nullptr;
|
|
|
|
Libs::notify_notification_show(_data, &error);
|
|
|
|
if (!error) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool close() {
|
|
|
|
if (valid()) {
|
|
|
|
GError *error = nullptr;
|
|
|
|
Libs::notify_notification_close(_data, &error);
|
|
|
|
if (!error) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~NotificationData() {
|
|
|
|
if (valid()) {
|
2016-10-04 13:36:50 +00:00
|
|
|
// if (_handlerId > 0) {
|
|
|
|
// Libs::g_signal_handler_disconnect(Libs::g_object_cast(_data), _handlerId);
|
|
|
|
// }
|
|
|
|
// Libs::notify_notification_clear_actions(_data);
|
2016-10-03 15:07:50 +00:00
|
|
|
Libs::g_object_unref(Libs::g_object_cast(_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-10-04 13:36:50 +00:00
|
|
|
void init(const QStringList &capabilities, PeerId peerId, MsgId msgId) {
|
|
|
|
if (capabilities.contains(qsl("append"))) {
|
|
|
|
Libs::notify_notification_set_hint_string(_data, "append", "true");
|
|
|
|
} else if (capabilities.contains(qsl("x-canonical-append"))) {
|
|
|
|
Libs::notify_notification_set_hint_string(_data, "x-canonical-append", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto signalReceiver = Libs::g_object_cast(_data);
|
|
|
|
auto signalHandler = G_CALLBACK(NotificationData::notificationClosed);
|
|
|
|
auto signalName = "closed";
|
|
|
|
auto signalDataFreeMethod = &NotificationData::notificationDataFreeClosure;
|
|
|
|
auto signalData = new NotificationDataStruct(peerId, msgId);
|
|
|
|
_handlerId = Libs::g_signal_connect_helper(signalReceiver, signalName, signalHandler, signalData, signalDataFreeMethod);
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
Libs::notify_notification_set_timeout(_data, Libs::NOTIFY_EXPIRES_DEFAULT);
|
|
|
|
|
|
|
|
if (auto manager = ManagerInstance.data()) {
|
|
|
|
if (manager->hasActionsSupport()) {
|
2016-10-04 18:18:08 +00:00
|
|
|
auto label = lang(lng_notification_reply).toUtf8();
|
2016-10-04 13:36:50 +00:00
|
|
|
auto actionReceiver = _data;
|
|
|
|
auto actionHandler = &NotificationData::notificationClicked;
|
|
|
|
auto actionLabel = label.constData();
|
|
|
|
auto actionName = "default";
|
|
|
|
auto actionDataFreeMethod = &NotificationData::notificationDataFree;
|
|
|
|
auto actionData = new NotificationDataStruct(peerId, msgId);
|
|
|
|
Libs::notify_notification_add_action(actionReceiver, actionName, actionLabel, actionHandler, actionData, actionDataFreeMethod);
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void logError(GError *error) {
|
|
|
|
LOG(("LibNotify Error: domain %1, code %2, message '%3'").arg(error->domain).arg(error->code).arg(QString::fromUtf8(error->message)));
|
|
|
|
Libs::g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2016-10-04 13:36:50 +00:00
|
|
|
struct NotificationDataStruct {
|
|
|
|
NotificationDataStruct(PeerId peerId, MsgId msgId) : peerId(peerId), msgId(msgId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
MsgId msgId = 0;
|
|
|
|
};
|
|
|
|
static void notificationDataFree(gpointer data) {
|
|
|
|
auto notificationData = static_cast<NotificationDataStruct*>(data);
|
|
|
|
delete notificationData;
|
|
|
|
}
|
|
|
|
static void notificationDataFreeClosure(gpointer data, GClosure *closure) {
|
|
|
|
auto notificationData = static_cast<NotificationDataStruct*>(data);
|
|
|
|
delete notificationData;
|
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
static void notificationClosed(Libs::NotifyNotification *notification, gpointer data) {
|
2016-10-04 13:36:50 +00:00
|
|
|
auto closedReason = Libs::notify_notification_get_closed_reason(notification);
|
|
|
|
auto notificationData = static_cast<NotificationDataStruct*>(data);
|
|
|
|
if (auto manager = ManagerInstance.data()) {
|
|
|
|
manager->clearNotification(notificationData->peerId, notificationData->msgId);
|
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
static void notificationClicked(Libs::NotifyNotification *notification, char *action, gpointer data) {
|
2016-10-04 13:36:50 +00:00
|
|
|
auto notificationData = static_cast<NotificationDataStruct*>(data);
|
|
|
|
if (auto manager = ManagerInstance.data()) {
|
|
|
|
manager->notificationActivated(notificationData->peerId, notificationData->msgId);
|
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Libs::NotifyNotification *_data = nullptr;
|
|
|
|
gulong _handlerId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
using Notification = QSharedPointer<NotificationData>;
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
} // namespace
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
void Start() {
|
2016-10-03 15:07:50 +00:00
|
|
|
if (LibNotifyLoaded()) {
|
2016-10-03 08:56:03 +00:00
|
|
|
if (Libs::notify_is_initted() || Libs::notify_init("Telegram Desktop")) {
|
2016-10-12 19:34:25 +00:00
|
|
|
ManagerInstance.createIfNull();
|
2016-10-04 13:36:50 +00:00
|
|
|
if (!ManagerInstance->init()) {
|
|
|
|
ManagerInstance.clear();
|
|
|
|
LOG(("LibNotify Error: manager failed to init!"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("LibNotify Error: failed to init!"));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
Window::Notifications::Manager *GetManager() {
|
2016-10-07 16:45:45 +00:00
|
|
|
if (Global::started() && Global::NativeNotifications()) {
|
2016-10-03 15:07:50 +00:00
|
|
|
return ManagerInstance.data();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
bool Supported() {
|
2016-10-03 15:07:50 +00:00
|
|
|
return ManagerInstance.data() != nullptr;
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
void Finish() {
|
|
|
|
if (GetManager()) {
|
2016-10-03 08:56:03 +00:00
|
|
|
ManagerInstance.reset();
|
|
|
|
Libs::notify_uninit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Manager::Impl {
|
|
|
|
public:
|
2017-01-07 11:55:05 +00:00
|
|
|
using Type = Window::Notifications::CachedUserpics::Type;
|
|
|
|
Impl(Type type) : _cachedUserpics(type) {
|
|
|
|
}
|
|
|
|
|
2016-10-04 13:36:50 +00:00
|
|
|
bool init();
|
2016-10-03 15:07:50 +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-03 08:56:03 +00:00
|
|
|
void clearAll();
|
|
|
|
void clearFromHistory(History *history);
|
2016-10-03 15:07:50 +00:00
|
|
|
void clearNotification(PeerId peerId, MsgId msgId);
|
|
|
|
|
|
|
|
bool hasPoorSupport() const {
|
|
|
|
return _poorSupported;
|
|
|
|
}
|
|
|
|
bool hasActionsSupport() const {
|
|
|
|
return _actionsSupported;
|
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-22 13:57:13 +00:00
|
|
|
QString escapeNotificationText(const QString &text) const;
|
2016-10-03 15:07:50 +00:00
|
|
|
void showNextNotification();
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
struct QueuedNotification {
|
|
|
|
PeerData *peer = nullptr;
|
|
|
|
MsgId msgId = 0;
|
|
|
|
QString title;
|
|
|
|
QString body;
|
2016-10-08 08:38:53 +00:00
|
|
|
bool hideNameAndPhoto = false;
|
2016-10-03 15:07:50 +00:00
|
|
|
};
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2016-10-04 13:36:50 +00:00
|
|
|
QString _serverName;
|
|
|
|
QStringList _capabilities;
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
using QueuedNotifications = QList<QueuedNotification>;
|
|
|
|
QueuedNotifications _queuedNotifications;
|
|
|
|
|
|
|
|
using Notifications = QMap<PeerId, QMap<MsgId, Notification>>;
|
|
|
|
Notifications _notifications;
|
2016-10-03 08:56:03 +00:00
|
|
|
|
|
|
|
Window::Notifications::CachedUserpics _cachedUserpics;
|
2016-10-03 15:07:50 +00:00
|
|
|
bool _actionsSupported = false;
|
2016-10-04 13:36:50 +00:00
|
|
|
bool _markupSupported = false;
|
|
|
|
bool _poorSupported = false;
|
2016-10-03 08:56:03 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-10-04 13:36:50 +00:00
|
|
|
bool Manager::Impl::init() {
|
2016-10-03 15:07:50 +00:00
|
|
|
if (auto capabilities = Libs::notify_get_server_caps()) {
|
|
|
|
for (auto capability = capabilities; capability; capability = capability->next) {
|
2016-10-04 13:36:50 +00:00
|
|
|
auto capabilityText = QString::fromUtf8(static_cast<const char*>(capability->data));
|
|
|
|
_capabilities.push_back(capabilityText);
|
|
|
|
}
|
|
|
|
Libs::g_list_free_full(capabilities, g_free);
|
|
|
|
|
|
|
|
LOG(("LibNotify capabilities: %1").arg(_capabilities.join(qstr(", "))));
|
|
|
|
if (_capabilities.contains(qsl("actions"))) {
|
|
|
|
_actionsSupported = true;
|
|
|
|
} else if (_capabilities.contains(qsl("body-markup"))) {
|
|
|
|
_markupSupported = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("LibNotify Error: could not get capabilities!"));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
// Unity and other Notify OSD users handle desktop notifications
|
|
|
|
// extremely poor, even without the ability to close() them.
|
|
|
|
gchar *name = nullptr;
|
|
|
|
if (Libs::notify_get_server_info(&name, nullptr, nullptr, nullptr)) {
|
|
|
|
if (name) {
|
2016-10-04 13:36:50 +00:00
|
|
|
_serverName = QString::fromUtf8(static_cast<const char*>(name));
|
|
|
|
Libs::g_free(name);
|
|
|
|
|
|
|
|
LOG(("Notifications Server: %1").arg(_serverName));
|
|
|
|
if (_serverName == qstr("notify-osd")) {
|
|
|
|
// _poorSupported = true;
|
2016-10-03 15:07:50 +00:00
|
|
|
_actionsSupported = false;
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2016-10-04 13:36:50 +00:00
|
|
|
} else {
|
|
|
|
LOG(("LibNotify Error: successfully got empty server name!"));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2016-10-04 13:36:50 +00:00
|
|
|
} else {
|
|
|
|
LOG(("LibNotify Error: could not get server name!"));
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2016-10-04 13:36:50 +00:00
|
|
|
|
|
|
|
return !_serverName.isEmpty();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 13:57:13 +00:00
|
|
|
QString Manager::Impl::escapeNotificationText(const QString &text) const {
|
|
|
|
return _markupSupported ? escapeHtml(text) : text;
|
|
|
|
}
|
|
|
|
|
2016-10-08 08:38:53 +00:00
|
|
|
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
|
2016-10-22 13:57:13 +00:00
|
|
|
auto titleText = escapeNotificationText(title);
|
|
|
|
auto subtitleText = escapeNotificationText(subtitle);
|
|
|
|
auto msgText = escapeNotificationText(msg);
|
2016-10-04 13:36:50 +00:00
|
|
|
if (_markupSupported && !subtitleText.isEmpty()) {
|
|
|
|
subtitleText = qstr("<b>") + subtitleText + qstr("</b>");
|
|
|
|
}
|
|
|
|
auto bodyText = subtitleText.isEmpty() ? msgText : (subtitleText + '\n' + msgText);
|
2016-10-03 15:07:50 +00:00
|
|
|
|
|
|
|
QueuedNotification notification;
|
|
|
|
notification.peer = peer;
|
|
|
|
notification.msgId = msgId;
|
|
|
|
notification.title = titleText;
|
|
|
|
notification.body = bodyText;
|
2016-10-08 08:38:53 +00:00
|
|
|
notification.hideNameAndPhoto = hideNameAndPhoto;
|
2016-10-03 15:07:50 +00:00
|
|
|
_queuedNotifications.push_back(notification);
|
|
|
|
|
|
|
|
showNextNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Impl::showNextNotification() {
|
|
|
|
// Show only one notification at a time in Unity / Notify OSD.
|
|
|
|
if (_poorSupported) {
|
|
|
|
for (auto b = _notifications.begin(); !_notifications.isEmpty() && b->isEmpty();) {
|
|
|
|
_notifications.erase(b);
|
|
|
|
}
|
|
|
|
if (!_notifications.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QueuedNotification data;
|
|
|
|
while (!_queuedNotifications.isEmpty()) {
|
|
|
|
data = _queuedNotifications.front();
|
|
|
|
_queuedNotifications.pop_front();
|
|
|
|
if (data.peer) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!data.peer) {
|
2016-10-03 08:56:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
auto peerId = data.peer->id;
|
|
|
|
auto msgId = data.msgId;
|
2016-10-04 13:36:50 +00:00
|
|
|
auto notification = MakeShared<NotificationData>(data.title, data.body, _capabilities, peerId, msgId);
|
2016-10-03 15:07:50 +00:00
|
|
|
if (!notification->valid()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-03 08:56:03 +00:00
|
|
|
|
|
|
|
StorageKey key;
|
2016-10-08 08:38:53 +00:00
|
|
|
if (data.hideNameAndPhoto) {
|
2016-10-03 08:56:03 +00:00
|
|
|
key = StorageKey(0, 0);
|
2016-10-08 08:38:53 +00:00
|
|
|
} else {
|
|
|
|
key = data.peer->userpicUniqueKey();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
notification->setImage(_cachedUserpics.get(key, data.peer));
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
auto i = _notifications.find(peerId);
|
2016-10-03 08:56:03 +00:00
|
|
|
if (i != _notifications.cend()) {
|
|
|
|
auto j = i->find(msgId);
|
|
|
|
if (j != i->cend()) {
|
|
|
|
auto oldNotification = j.value();
|
|
|
|
i->erase(j);
|
2016-10-03 15:07:50 +00:00
|
|
|
oldNotification->close();
|
|
|
|
i = _notifications.find(peerId);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == _notifications.cend()) {
|
2016-10-03 15:07:50 +00:00
|
|
|
i = _notifications.insert(peerId, QMap<MsgId, Notification>());
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
_notifications[peerId].insert(msgId, notification);
|
|
|
|
if (!notification->show()) {
|
|
|
|
i = _notifications.find(peerId);
|
|
|
|
if (i != _notifications.cend()) {
|
|
|
|
i->remove(msgId);
|
|
|
|
if (i->isEmpty()) _notifications.erase(i);
|
|
|
|
}
|
|
|
|
showNextNotification();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Impl::clearAll() {
|
2016-10-03 15:07:50 +00:00
|
|
|
_queuedNotifications.clear();
|
2016-10-03 08:56:03 +00:00
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
auto temp = base::take(_notifications);
|
2016-10-03 08:56:03 +00:00
|
|
|
for_const (auto ¬ifications, temp) {
|
|
|
|
for_const (auto notification, notifications) {
|
2016-10-03 15:07:50 +00:00
|
|
|
notification->close();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Impl::clearFromHistory(History *history) {
|
2016-10-03 15:07:50 +00:00
|
|
|
for (auto i = _queuedNotifications.begin(); i != _queuedNotifications.end();) {
|
|
|
|
if (i->peer == history->peer) {
|
|
|
|
i = _queuedNotifications.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
auto i = _notifications.find(history->peer->id);
|
|
|
|
if (i != _notifications.cend()) {
|
2016-10-07 16:45:45 +00:00
|
|
|
auto temp = base::take(i.value());
|
2016-10-03 08:56:03 +00:00
|
|
|
_notifications.erase(i);
|
|
|
|
|
|
|
|
for_const (auto notification, temp) {
|
2016-10-03 15:07:50 +00:00
|
|
|
notification->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showNextNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Impl::clearNotification(PeerId peerId, MsgId msgId) {
|
|
|
|
auto i = _notifications.find(peerId);
|
|
|
|
if (i != _notifications.cend()) {
|
|
|
|
i.value().remove(msgId);
|
|
|
|
if (i.value().isEmpty()) {
|
|
|
|
_notifications.erase(i);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-03 15:07:50 +00:00
|
|
|
|
|
|
|
showNextNotification();
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
Manager::Manager() : _impl(std::make_unique<Impl>(Impl::Type::Rounded)) {
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 13:36:50 +00:00
|
|
|
bool Manager::init() {
|
|
|
|
return _impl->init();
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:07:50 +00:00
|
|
|
void Manager::clearNotification(PeerId peerId, MsgId msgId) {
|
|
|
|
_impl->clearNotification(peerId, msgId);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::hasPoorSupport() const {
|
|
|
|
return _impl->hasPoorSupport();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::hasActionsSupport() const {
|
|
|
|
return _impl->hasActionsSupport();
|
|
|
|
}
|
|
|
|
|
2016-10-03 08:56:03 +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) {
|
|
|
|
_impl->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
|
2016-10-03 08:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearAllFast() {
|
|
|
|
_impl->clearAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearFromHistory(History *history) {
|
|
|
|
_impl->clearFromHistory(history);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Platform
|