2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

Notifications management moved to AuthSession.

Also implemented Global::WorkMode() as an base::Variable.
This commit is contained in:
John Preston
2017-03-04 22:36:59 +03:00
parent b14ba398e6
commit 81790b2271
80 changed files with 1299 additions and 1152 deletions

View File

@@ -164,8 +164,10 @@ static gboolean _trayIconCheck(gpointer/* pIn*/) {
if (Libs::gtk_status_icon_is_embedded(_trayIcon)) {
trayIconChecked = true;
cSetSupportTray(true);
if (Global::started()) {
Global::RefWorkMode().setForced(Global::WorkMode().value(), true);
}
if (App::wnd()) {
App::wnd()->psUpdateWorkmode();
Notify::unreadCounterUpdated();
App::wnd()->updateTrayMenu();
}
@@ -196,7 +198,7 @@ void MainWindow::initHook() {
}
bool MainWindow::hasTrayIcon() const {
return trayIcon || ((useAppIndicator || (useStatusIcon && trayIconChecked)) && (cWorkMode() != dbiwmWindowOnly));
return trayIcon || ((useAppIndicator || (useStatusIcon && trayIconChecked)) && (Global::WorkMode().value() != dbiwmWindowOnly));
}
void MainWindow::psStatusIconCheck() {
@@ -272,10 +274,10 @@ void MainWindow::psSetupTrayIcon() {
}
}
void MainWindow::psUpdateWorkmode() {
void MainWindow::workmodeUpdated(DBIWorkMode mode) {
if (!cSupportTray()) return;
if (cWorkMode() == dbiwmWindowOnly) {
if (mode == dbiwmWindowOnly) {
if (noQtTrayIcon) {
if (useAppIndicator) {
Libs::app_indicator_set_status(_trayIndicator, APP_INDICATOR_STATUS_PASSIVE);
@@ -372,10 +374,6 @@ void MainWindow::updateIconCounters() {
}
}
bool MainWindow::psHasNativeNotifications() {
return Notifications::Supported();
}
void MainWindow::LibsLoaded() {
noQtTrayIcon = !DesktopEnvironment::TryQtTrayIcon();
tryAppIndicator = !DesktopEnvironment::PreferAppIndicatorTrayIcon();
@@ -518,7 +516,7 @@ void MainWindow::psCreateTrayIcon() {
Libs::g_idle_add((GSourceFunc)_trayIconCheck, 0);
_psCheckStatusIconTimer.start(100);
} else {
psUpdateWorkmode();
workmodeUpdated(Global::WorkMode().value());
}
}
@@ -555,7 +553,7 @@ void MainWindow::psFirstShow() {
if ((cLaunchMode() == LaunchModeAutoStart && cStartMinimized()) || cStartInTray()) {
setWindowState(Qt::WindowMinimized);
if (cWorkMode() == dbiwmTrayOnly || cWorkMode() == dbiwmWindowAndTray) {
if (Global::WorkMode().value() == dbiwmTrayOnly || Global::WorkMode().value() == dbiwmWindowAndTray) {
hide();
} else {
show();
@@ -577,9 +575,6 @@ void MainWindow::psUpdateSysMenu(Qt::WindowState state) {
void MainWindow::psUpdateMargins() {
}
void MainWindow::psFlash() {
}
MainWindow::~MainWindow() {
if (_trayIcon) {
Libs::g_object_unref(_trayIcon);

View File

@@ -35,16 +35,9 @@ public:
void psUpdateSysMenu(Qt::WindowState state);
void psUpdateMargins();
void psFlash();
void psNotifySettingGot();
void psUpdateWorkmode();
void psRefreshTaskbarIcon() {
}
bool psHasNativeNotifications();
virtual QImage iconWithCounter(int size, int count, style::color bg, style::color fg, bool smallIcon) = 0;
static void LibsLoaded();
@@ -63,6 +56,8 @@ protected:
bool hasTrayIcon() const override;
void workmodeUpdated(DBIWorkMode mode) override;
QSystemTrayIcon *trayIcon = nullptr;
QMenu *trayIconMenu = nullptr;
QImage icon256, iconbig256;

View File

@@ -24,13 +24,12 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "platform/linux/linux_libnotify.h"
#include "platform/linux/linux_libs.h"
#include "lang.h"
#include "core/task_queue.h"
namespace Platform {
namespace Notifications {
namespace {
NeverFreedPointer<Manager> ManagerInstance;
bool LibNotifyLoaded() {
return (Libs::notify_init != nullptr)
&& (Libs::notify_uninit != nullptr)
@@ -100,10 +99,10 @@ QString escapeHtml(const QString &text) {
class NotificationData {
public:
NotificationData(const QString &title, const QString &body, const QStringList &capabilities, PeerId peerId, MsgId msgId)
NotificationData(const std::shared_ptr<Manager*> &guarded, 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)) {
if (valid()) {
init(capabilities, peerId, msgId);
init(guarded, capabilities, peerId, msgId);
}
}
bool valid() const {
@@ -158,7 +157,7 @@ public:
}
private:
void init(const QStringList &capabilities, PeerId peerId, MsgId msgId) {
void init(const std::shared_ptr<Manager*> &guarded, 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"))) {
@@ -169,22 +168,20 @@ private:
auto signalHandler = G_CALLBACK(NotificationData::notificationClosed);
auto signalName = "closed";
auto signalDataFreeMethod = &NotificationData::notificationDataFreeClosure;
auto signalData = new NotificationDataStruct(peerId, msgId);
auto signalData = new NotificationDataStruct(guarded, peerId, msgId);
_handlerId = Libs::g_signal_connect_helper(signalReceiver, signalName, signalHandler, signalData, signalDataFreeMethod);
Libs::notify_notification_set_timeout(_data, Libs::NOTIFY_EXPIRES_DEFAULT);
if (auto manager = ManagerInstance.data()) {
if (manager->hasActionsSupport()) {
auto label = lang(lng_notification_reply).toUtf8();
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);
}
if ((*guarded)->hasActionsSupport()) {
auto label = lang(lng_notification_reply).toUtf8();
auto actionReceiver = _data;
auto actionHandler = &NotificationData::notificationClicked;
auto actionLabel = label.constData();
auto actionName = "default";
auto actionDataFreeMethod = &NotificationData::notificationDataFree;
auto actionData = new NotificationDataStruct(guarded, peerId, msgId);
Libs::notify_notification_add_action(actionReceiver, actionName, actionLabel, actionHandler, actionData, actionDataFreeMethod);
}
}
@@ -194,12 +191,23 @@ private:
}
struct NotificationDataStruct {
NotificationDataStruct(PeerId peerId, MsgId msgId) : peerId(peerId), msgId(msgId) {
NotificationDataStruct(const std::shared_ptr<Manager*> &guarded, PeerId peerId, MsgId msgId)
: weak(guarded)
, peerId(peerId)
, msgId(msgId) {
}
std::weak_ptr<Manager*> weak;
PeerId peerId = 0;
MsgId msgId = 0;
};
static void performOnMainQueue(NotificationDataStruct *data, base::lambda_once<void(Manager *manager)> task) {
base::TaskQueue::Main().Put([weak = data->weak, task = std::move(task)]() mutable {
if (auto strong = weak.lock()) {
task(*strong);
}
});
}
static void notificationDataFree(gpointer data) {
auto notificationData = static_cast<NotificationDataStruct*>(data);
delete notificationData;
@@ -211,15 +219,15 @@ private:
static void notificationClosed(Libs::NotifyNotification *notification, gpointer data) {
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);
}
performOnMainQueue(notificationData, [peerId = notificationData->peerId, msgId = notificationData->msgId](Manager *manager) {
manager->clearNotification(peerId, msgId);
});
}
static void notificationClicked(Libs::NotifyNotification *notification, char *action, gpointer data) {
auto notificationData = static_cast<NotificationDataStruct*>(data);
if (auto manager = ManagerInstance.data()) {
manager->notificationActivated(notificationData->peerId, notificationData->msgId);
}
performOnMainQueue(notificationData, [peerId = notificationData->peerId, msgId = notificationData->msgId](Manager *manager) {
manager->notificationActivated(peerId, msgId);
});
}
Libs::NotifyNotification *_data = nullptr;
@@ -229,47 +237,72 @@ private:
using Notification = QSharedPointer<NotificationData>;
} // namespace
void Start() {
if (LibNotifyLoaded()) {
if (Libs::notify_is_initted() || Libs::notify_init("Telegram Desktop")) {
ManagerInstance.createIfNull();
if (!ManagerInstance->init()) {
ManagerInstance.clear();
LOG(("LibNotify Error: manager failed to init!"));
}
} else {
LOG(("LibNotify Error: failed to init!"));
}
QString GetServerName() {
if (!LibNotifyLoaded()) {
return QString();
}
if (!Libs::notify_is_initted() && !Libs::notify_init("Telegram Desktop")) {
LOG(("LibNotify Error: failed to init!"));
return QString();
}
gchar *name = nullptr;
auto guard = base::scope_guard([&name] {
if (name) Libs::g_free(name);
});
if (!Libs::notify_get_server_info(&name, nullptr, nullptr, nullptr)) {
LOG(("LibNotify Error: could not get server name!"));
return QString();
}
if (!name) {
LOG(("LibNotify Error: successfully got empty server name!"));
return QString();
}
auto result = QString::fromUtf8(static_cast<const char*>(name));
LOG(("Notifications Server: %1").arg(result));
return result;
}
Window::Notifications::Manager *GetManager() {
if (Global::started() && Global::NativeNotifications()) {
return ManagerInstance.data();
auto LibNotifyServerName = QString();
} // namespace
bool Supported() {
static auto Checked = false;
if (!Checked) {
Checked = true;
LibNotifyServerName = GetServerName();
}
return !LibNotifyServerName.isEmpty();
}
std::unique_ptr<Window::Notifications::Manager> Create(Window::Notifications::System *system) {
if (Global::NativeNotifications() && Supported()) {
return std::make_unique<Manager>(system);
}
return nullptr;
}
bool Supported() {
return ManagerInstance.data() != nullptr;
}
void Finish() {
if (GetManager()) {
ManagerInstance.reset();
Libs::notify_uninit();
if (Libs::notify_is_initted && Libs::notify_uninit) {
if (Libs::notify_is_initted()) {
Libs::notify_uninit();
}
}
}
class Manager::Impl {
class Manager::Private {
public:
using Type = Window::Notifications::CachedUserpics::Type;
Impl(Type type) : _cachedUserpics(type) {
explicit Private(Type type)
: _cachedUserpics(type) {
}
bool init();
void init(Manager *manager);
void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton);
void clearAll();
@@ -283,6 +316,8 @@ public:
return _actionsSupported;
}
~Private();
private:
QString escapeNotificationText(const QString &text) const;
void showNextNotification();
@@ -309,9 +344,13 @@ private:
bool _markupSupported = false;
bool _poorSupported = false;
std::shared_ptr<Manager*> _guarded;
};
bool Manager::Impl::init() {
void Manager::Private::init(Manager *manager) {
_guarded = std::make_shared<Manager*>(manager);
if (auto capabilities = Libs::notify_get_server_caps()) {
for (auto capability = capabilities; capability; capability = capability->next) {
auto capabilityText = QString::fromUtf8(static_cast<const char*>(capability->data));
@@ -331,32 +370,19 @@ bool Manager::Impl::init() {
// 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) {
_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;
_actionsSupported = false;
}
} else {
LOG(("LibNotify Error: successfully got empty server name!"));
}
} else {
LOG(("LibNotify Error: could not get server name!"));
_serverName = LibNotifyServerName;
t_assert(!_serverName.isEmpty());
if (_serverName == qstr("notify-osd")) {
// _poorSupported = true;
_actionsSupported = false;
}
return !_serverName.isEmpty();
}
QString Manager::Impl::escapeNotificationText(const QString &text) const {
QString Manager::Private::escapeNotificationText(const QString &text) const {
return _markupSupported ? escapeHtml(text) : text;
}
void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
void Manager::Private::showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton) {
auto titleText = escapeNotificationText(title);
auto subtitleText = escapeNotificationText(subtitle);
auto msgText = escapeNotificationText(msg);
@@ -376,7 +402,7 @@ void Manager::Impl::showNotification(PeerData *peer, MsgId msgId, const QString
showNextNotification();
}
void Manager::Impl::showNextNotification() {
void Manager::Private::showNextNotification() {
// Show only one notification at a time in Unity / Notify OSD.
if (_poorSupported) {
for (auto b = _notifications.begin(); !_notifications.isEmpty() && b->isEmpty();) {
@@ -401,7 +427,7 @@ void Manager::Impl::showNextNotification() {
auto peerId = data.peer->id;
auto msgId = data.msgId;
auto notification = MakeShared<NotificationData>(data.title, data.body, _capabilities, peerId, msgId);
auto notification = MakeShared<NotificationData>(_guarded, data.title, data.body, _capabilities, peerId, msgId);
if (!notification->valid()) {
return;
}
@@ -438,7 +464,7 @@ void Manager::Impl::showNextNotification() {
}
}
void Manager::Impl::clearAll() {
void Manager::Private::clearAll() {
_queuedNotifications.clear();
auto temp = base::take(_notifications);
@@ -449,7 +475,7 @@ void Manager::Impl::clearAll() {
}
}
void Manager::Impl::clearFromHistory(History *history) {
void Manager::Private::clearFromHistory(History *history) {
for (auto i = _queuedNotifications.begin(); i != _queuedNotifications.end();) {
if (i->peer == history->peer) {
i = _queuedNotifications.erase(i);
@@ -471,7 +497,7 @@ void Manager::Impl::clearFromHistory(History *history) {
showNextNotification();
}
void Manager::Impl::clearNotification(PeerId peerId, MsgId msgId) {
void Manager::Private::clearNotification(PeerId peerId, MsgId msgId) {
auto i = _notifications.find(peerId);
if (i != _notifications.cend()) {
i.value().remove(msgId);
@@ -483,37 +509,39 @@ void Manager::Impl::clearNotification(PeerId peerId, MsgId msgId) {
showNextNotification();
}
Manager::Manager() : _impl(std::make_unique<Impl>(Impl::Type::Rounded)) {
Manager::Private::~Private() {
clearAll();
}
bool Manager::init() {
return _impl->init();
Manager::Manager(Window::Notifications::System *system) : NativeManager(system)
, _private(std::make_unique<Private>(Private::Type::Rounded)) {
_private->init(this);
}
void Manager::clearNotification(PeerId peerId, MsgId msgId) {
_impl->clearNotification(peerId, msgId);
_private->clearNotification(peerId, msgId);
}
bool Manager::hasPoorSupport() const {
return _impl->hasPoorSupport();
return _private->hasPoorSupport();
}
bool Manager::hasActionsSupport() const {
return _impl->hasActionsSupport();
return _private->hasActionsSupport();
}
Manager::~Manager() = default;
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);
_private->showNotification(peer, msgId, title, subtitle, msg, hideNameAndPhoto, hideReplyButton);
}
void Manager::doClearAllFast() {
_impl->clearAll();
_private->clearAll();
}
void Manager::doClearFromHistory(History *history) {
_impl->clearFromHistory(history);
_private->clearFromHistory(history);
}
} // namespace Notifications

View File

@@ -36,11 +36,14 @@ inline bool SkipToast() {
return false;
}
inline void FlashBounce() {
}
void Finish();
class Manager : public Window::Notifications::NativeManager {
public:
Manager();
bool init();
Manager(Window::Notifications::System *system);
void clearNotification(PeerId peerId, MsgId msgId);
bool hasPoorSupport() const;
@@ -54,9 +57,8 @@ protected:
void doClearFromHistory(History *history) override;
private:
class Impl;
friend class Impl;
std::unique_ptr<Impl> _impl;
class Private;
const std::unique_ptr<Private> _private;
};

View File

@@ -23,6 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "mainwidget.h"
#include "mainwindow.h"
#include "platform/linux/file_utilities_linux.h"
#include "platform/platform_notifications_manager.h"
#include "storage/localstorage.h"
#include <sys/stat.h>
@@ -370,6 +371,8 @@ void start() {
}
void finish() {
Notifications::Finish();
delete _psEventFilter;
_psEventFilter = nullptr;
}