From e212b364b969b7918fbe9152dc4b7c04da303d12 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:23:34 -0500 Subject: rudimentary dm notifications --- src/abaddon.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/abaddon.cpp') diff --git a/src/abaddon.cpp b/src/abaddon.cpp index ba553bb..4daa53c 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -15,6 +15,7 @@ #include "windows/pinnedwindow.hpp" #include "windows/threadswindow.hpp" #include "startup.hpp" +#include "notifications/notifications.hpp" #ifdef WITH_LIBHANDY #include @@ -272,6 +273,14 @@ int Abaddon::StartGTK() { m_main_window->UpdateMenus(); + auto action_go_to_channel = Gio::SimpleAction::create("go-to-channel", Glib::VariantType("s")); + action_go_to_channel->signal_activate().connect([this](const Glib::VariantBase ¶m) { + const auto id_str = Glib::VariantBase::cast_dynamic>(param); + const Snowflake id = id_str.get(); + ActionChannelOpened(id, false); + }); + m_gtk_app->add_action(action_go_to_channel); + m_gtk_app->hold(); m_main_window->show(); @@ -329,6 +338,7 @@ void Abaddon::DiscordOnReady() { void Abaddon::DiscordOnMessageCreate(const Message &message) { m_main_window->UpdateChatNewMessage(message); + m_notifications.CheckMessage(message); } void Abaddon::DiscordOnMessageDelete(Snowflake id, Snowflake channel_id) { @@ -682,6 +692,10 @@ std::string Abaddon::GetStateCachePath(const std::string &path) { return GetStateCachePath() + path; } +Glib::RefPtr Abaddon::GetApp() { + return m_gtk_app; +} + void Abaddon::ActionConnect() { if (!m_discord.IsStarted()) StartDiscord(); -- cgit v1.2.3 From a09ad5cec016575ef3041367826a1bd1b261a9fc Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Wed, 1 Mar 2023 01:01:44 -0500 Subject: dont notify if message channel is focused --- src/abaddon.cpp | 8 ++++++++ src/abaddon.hpp | 2 ++ src/notifications/notifications.cpp | 2 ++ 3 files changed, 12 insertions(+) (limited to 'src/abaddon.cpp') diff --git a/src/abaddon.cpp b/src/abaddon.cpp index 4daa53c..200183b 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -696,6 +696,14 @@ Glib::RefPtr Abaddon::GetApp() { return m_gtk_app; } +bool Abaddon::IsMainWindowActive() { + return m_main_window->has_toplevel_focus(); +} + +Snowflake Abaddon::GetActiveChannelID() const noexcept { + return m_main_window->GetChatActiveChannel(); +} + void Abaddon::ActionConnect() { if (!m_discord.IsStarted()) StartDiscord(); diff --git a/src/abaddon.hpp b/src/abaddon.hpp index 56f5e04..2fbbcc0 100644 --- a/src/abaddon.hpp +++ b/src/abaddon.hpp @@ -97,6 +97,8 @@ public: static std::string GetStateCachePath(const std::string &path); [[nodiscard]] Glib::RefPtr GetApp(); + [[nodiscard]] bool IsMainWindowActive(); + [[nodiscard]] Snowflake GetActiveChannelID() const noexcept; protected: void RunFirstTimeDiscordStartup(); diff --git a/src/notifications/notifications.cpp b/src/notifications/notifications.cpp index ed4a879..351cf32 100644 --- a/src/notifications/notifications.cpp +++ b/src/notifications/notifications.cpp @@ -10,6 +10,8 @@ void Notifications::CheckMessage(const Message &message) { auto &discord = Abaddon::Get().GetDiscordClient(); // ignore if the channel is muted if (discord.IsChannelMuted(message.ChannelID)) return; + // ignore if focused and message's channel is active + if (Abaddon::Get().IsMainWindowActive() && Abaddon::Get().GetActiveChannelID() == message.ChannelID) return; // notify messages in DMs const auto channel = discord.GetChannel(message.ChannelID); if (channel->IsDM()) { -- cgit v1.2.3 From 02f7bfefe3185a38f8935b31c68857b986d709ac Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Thu, 16 Mar 2023 21:15:56 -0400 Subject: withdraw notifications on channel open --- src/abaddon.cpp | 2 ++ src/notifications/notifications.cpp | 15 +++++++++++++-- src/notifications/notifications.hpp | 4 ++++ src/notifications/notifier.hpp | 3 ++- src/notifications/notifier_gio.cpp | 8 ++++++-- src/notifications/notifier_null.cpp | 4 +++- 6 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src/abaddon.cpp') diff --git a/src/abaddon.cpp b/src/abaddon.cpp index 200183b..e234520 100644 --- a/src/abaddon.cpp +++ b/src/abaddon.cpp @@ -733,6 +733,8 @@ void Abaddon::ActionChannelOpened(Snowflake id, bool expand_to) { } if (id == m_main_window->GetChatActiveChannel()) return; + m_notifications.WithdrawChannel(id); + m_main_window->GetChatWindow()->SetTopic(""); const auto channel = m_discord.GetChannel(id); diff --git a/src/notifications/notifications.cpp b/src/notifications/notifications.cpp index fc7a2d6..5b834ce 100644 --- a/src/notifications/notifications.cpp +++ b/src/notifications/notifications.cpp @@ -99,12 +99,23 @@ void Notifications::CheckMessage(const Message &message) { // notify messages in DMs const auto channel = discord.GetChannel(message.ChannelID); if (channel->IsDM()) { + m_chan_notifications[message.ChannelID].push_back(message.ID); NotifyMessageDM(message); } else if (CheckGuildMessage(message)) { + m_chan_notifications[message.ChannelID].push_back(message.ID); NotifyMessageGuild(message); } } +void Notifications::WithdrawChannel(Snowflake channel_id) { + if (auto it = m_chan_notifications.find(channel_id); it != m_chan_notifications.end()) { + for (const auto notification_id : it->second) { + m_notifier.Withdraw(std::to_string(notification_id)); + } + it->second.clear(); + } +} + Glib::ustring Sanitize(const Message &message) { auto buf = Gtk::TextBuffer::create(); Gtk::TextBuffer::iterator begin, end; @@ -123,7 +134,7 @@ void Notifications::NotifyMessageDM(const Message &message) { const auto body = Sanitize(message); Abaddon::Get().GetImageManager().GetCache().GetFileFromURL(message.Author.GetAvatarURL("png", "64"), [=](const std::string &path) { - m_notifier.Notify(title, body, default_action, path); + m_notifier.Notify(std::to_string(message.ID), title, body, default_action, path); }); } @@ -146,7 +157,7 @@ void Notifications::NotifyMessageGuild(const Message &message) { } const auto body = Sanitize(message); Abaddon::Get().GetImageManager().GetCache().GetFileFromURL(message.Author.GetAvatarURL("png", "64"), [=](const std::string &path) { - m_notifier.Notify(title, body, default_action, path); + m_notifier.Notify(std::to_string(message.ID), title, body, default_action, path); }); } diff --git a/src/notifications/notifications.hpp b/src/notifications/notifications.hpp index 5498938..b4ea584 100644 --- a/src/notifications/notifications.hpp +++ b/src/notifications/notifications.hpp @@ -1,5 +1,7 @@ #pragma once #include "notifier.hpp" +#include +#include class Message; @@ -8,6 +10,7 @@ public: Notifications(); void CheckMessage(const Message &message); + void WithdrawChannel(Snowflake channel_id); private: void NotifyMessageDM(const Message &message); @@ -16,4 +19,5 @@ private: [[nodiscard]] bool IsDND() const; Notifier m_notifier; + std::map> m_chan_notifications; }; diff --git a/src/notifications/notifier.hpp b/src/notifications/notifier.hpp index bed7495..cd3eb71 100644 --- a/src/notifications/notifier.hpp +++ b/src/notifications/notifier.hpp @@ -11,7 +11,8 @@ public: Notifier(); ~Notifier(); - void Notify(const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path); + void Notify(const Glib::ustring &id, const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path); + void Withdraw(const Glib::ustring &id); private: #ifdef ENABLE_NOTIFICATION_SOUNDS diff --git a/src/notifications/notifier_gio.cpp b/src/notifications/notifier_gio.cpp index e5335d0..ed17ad1 100644 --- a/src/notifications/notifier_gio.cpp +++ b/src/notifications/notifier_gio.cpp @@ -18,7 +18,7 @@ Notifier::~Notifier() { #endif } -void Notifier::Notify(const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path) { +void Notifier::Notify(const Glib::ustring &id, const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path) { auto n = Gio::Notification::create(title); n->set_body(text); n->set_default_action(default_action); @@ -29,7 +29,7 @@ void Notifier::Notify(const Glib::ustring &title, const Glib::ustring &text, con auto *icon = g_file_icon_new(file); g_notification_set_icon(n->gobj(), icon); - Abaddon::Get().GetApp()->send_notification(n); + Abaddon::Get().GetApp()->send_notification(id, n); g_object_unref(icon); g_object_unref(file); @@ -38,3 +38,7 @@ void Notifier::Notify(const Glib::ustring &title, const Glib::ustring &text, con ma_engine_play_sound(&m_engine, Abaddon::Get().GetResPath("/sound/message.mp3").c_str(), nullptr); #endif } + +void Notifier::Withdraw(const Glib::ustring &id) { + Abaddon::Get().GetApp()->withdraw_notification(id); +} diff --git a/src/notifications/notifier_null.cpp b/src/notifications/notifier_null.cpp index 356d04d..1da99fd 100644 --- a/src/notifications/notifier_null.cpp +++ b/src/notifications/notifier_null.cpp @@ -4,4 +4,6 @@ Notifier::Notifier() {} Notifier::~Notifier() {} -void Notifier::Notify(const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path) {} +void Notifier::Notify(const Glib::ustring &id, const Glib::ustring &title, const Glib::ustring &text, const Glib::ustring &default_action, const std::string &icon_path) {} + +void Notifier::Withdraw(const Glib::ustring &id) {} -- cgit v1.2.3