Skip to content

Commit

Permalink
Move GNotiftcation action handlers to Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya-fedin committed Feb 18, 2025
1 parent f810d7c commit fcfe946
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ class NotificationData final : public base::has_weak_ptr {
ulong _notificationRepliedSignalId = 0;
ulong _notificationClosedSignalId = 0;

rpl::lifetime _lifetime;

};

using Notification = std::unique_ptr<NotificationData>;
Expand All @@ -214,57 +212,6 @@ bool NotificationData::init(const Info &info) {
const auto &subtitle = info.subtitle;

if (_application) {
auto actionMap = Gio::ActionMap(_application);

const auto dictToNotificationId = [](GLib::VariantDict dict) {
using ContextId = Window::Notifications::Manager::ContextId;
return NotificationId{
.contextId = ContextId{
.sessionId = dict.lookup_value("session").get_uint64(),
.peerId = PeerId(dict.lookup_value("peer").get_uint64()),
.topicRootId = dict.lookup_value("topic").get_int64(),
},
.msgId = dict.lookup_value("msgid").get_int64(),
};
};

auto activate = gi::wrap(
G_SIMPLE_ACTION(
actionMap.lookup_action("notification-activate").gobj_()),
gi::transfer_none);

const auto activateSig = activate.signal_activate().connect([=](
Gio::SimpleAction,
GLib::Variant parameter) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
_manager->notificationActivated(
dictToNotificationId(GLib::VariantDict::new_(parameter)));
});
});

_lifetime.add([=]() mutable {
activate.disconnect(activateSig);
});

auto markAsRead = gi::wrap(
G_SIMPLE_ACTION(
actionMap.lookup_action("notification-mark-as-read").gobj_()),
gi::transfer_none);

const auto markAsReadSig = markAsRead.signal_activate().connect([=](
Gio::SimpleAction,
GLib::Variant parameter) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
_manager->notificationReplied(
dictToNotificationId(GLib::VariantDict::new_(parameter)),
{});
});
});

_lifetime.add([=]() mutable {
markAsRead.disconnect(markAsReadSig);
});

_notification = Gio::Notification::new_(
subtitle.isEmpty()
? title.toStdString()
Expand Down Expand Up @@ -632,6 +579,7 @@ class Manager::Private : public base::has_weak_ptr {

XdgNotifications::NotificationsProxy _proxy;
XdgNotifications::Notifications _interface;
rpl::lifetime _lifetime;

};

Expand Down Expand Up @@ -810,6 +758,56 @@ Manager::Private::Private(not_null<Manager*> manager)
return a + (a.empty() ? "" : ", ") + b;
}).c_str()));
}

if (auto actionMap = Gio::ActionMap(Gio::Application::get_default())) {
const auto dictToNotificationId = [](GLib::VariantDict dict) {
return NotificationId{
.contextId = ContextId{
.sessionId = dict.lookup_value("session").get_uint64(),
.peerId = PeerId(dict.lookup_value("peer").get_uint64()),
.topicRootId = dict.lookup_value("topic").get_int64(),
},
.msgId = dict.lookup_value("msgid").get_int64(),
};
};

auto activate = gi::wrap(
G_SIMPLE_ACTION(
actionMap.lookup_action("notification-activate").gobj_()),
gi::transfer_none);

const auto activateSig = activate.signal_activate().connect([=](
Gio::SimpleAction,
GLib::Variant parameter) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
_manager->notificationActivated(
dictToNotificationId(GLib::VariantDict::new_(parameter)));
});
});

_lifetime.add([=]() mutable {
activate.disconnect(activateSig);
});

auto markAsRead = gi::wrap(
G_SIMPLE_ACTION(
actionMap.lookup_action("notification-mark-as-read").gobj_()),
gi::transfer_none);

const auto markAsReadSig = markAsRead.signal_activate().connect([=](
Gio::SimpleAction,
GLib::Variant parameter) {
Core::Sandbox::Instance().customEnterFromEventLoop([&] {
_manager->notificationReplied(
dictToNotificationId(GLib::VariantDict::new_(parameter)),
{});
});
});

_lifetime.add([=]() mutable {
markAsRead.disconnect(markAsReadSig);
});
}
}

void Manager::Private::init(XdgNotifications::NotificationsProxy proxy) {
Expand Down

0 comments on commit fcfe946

Please sign in to comment.