From 7fbe636ec171b6ce33f82d50e81a4e5b9038377e Mon Sep 17 00:00:00 2001 From: Konstantin Kuznetsov Date: Wed, 27 Mar 2024 14:58:55 +0300 Subject: [PATCH] Add mac notification plugin --- plugins/mac-notification/CMakeLists.txt | 28 +++++ .../src/mac_notification_provider.vala | 112 ++++++++++++++++++ plugins/mac-notification/src/plugin.vala | 18 +++ .../mac-notification/src/register_plugin.vala | 3 + 4 files changed, 161 insertions(+) create mode 100644 plugins/mac-notification/CMakeLists.txt create mode 100644 plugins/mac-notification/src/mac_notification_provider.vala create mode 100644 plugins/mac-notification/src/plugin.vala create mode 100644 plugins/mac-notification/src/register_plugin.vala diff --git a/plugins/mac-notification/CMakeLists.txt b/plugins/mac-notification/CMakeLists.txt new file mode 100644 index 00000000..d480d4bc --- /dev/null +++ b/plugins/mac-notification/CMakeLists.txt @@ -0,0 +1,28 @@ +find_packages(MAC_NOTIFICATION_PACKAGES REQUIRED + Gee + GLib + GModule + GObject + GDKPixbuf2 +) + +vala_precompile(MAC_NOTIFICATION_VALA_C +SOURCES + src/plugin.vala + src/mac_notification_provider.vala + src/register_plugin.vala +CUSTOM_VAPIS + ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi + ${CMAKE_BINARY_DIR}/exports/dino.vapi + ${CMAKE_BINARY_DIR}/exports/qlite.vapi +PACKAGES + ${MAC_NOTIFICATION_PACKAGES} +) + +add_definitions(${VALA_CFLAGS}) +add_library(mac-notification SHARED ${MAC_NOTIFICATION_VALA_C}) +target_link_libraries(mac-notification libdino ${MAC_NOTIFICATION_PACKAGES}) +set_target_properties(mac-notification PROPERTIES PREFIX "") +set_target_properties(mac-notification PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins/) + +install(TARGETS mac-notification ${PLUGIN_INSTALL}) diff --git a/plugins/mac-notification/src/mac_notification_provider.vala b/plugins/mac-notification/src/mac_notification_provider.vala new file mode 100644 index 00000000..8fb659c6 --- /dev/null +++ b/plugins/mac-notification/src/mac_notification_provider.vala @@ -0,0 +1,112 @@ +using Dino; +using Dino.Entities; +using Xmpp; + +namespace Dino.Plugins.MacNotification { + +public class MacNotificationProvider : NotificationProvider, Object { + private StreamInteractor stream_interactor; + private Dino.Application app; + + public MacNotificationProvider(Dino.Application app) { + this.stream_interactor = app.stream_interactor; + this.app = app; + } + + private void send_notification(string? message) { + if (message == null) { + return; + } + + try { + // FIXME: unsafe + string[] spawn_args = {"terminal-notifier", "-title", "Dino", "-message", message, "-sound", "default"}; + string[] spawn_env = Environ.get (); + Pid child_pid; + + Process.spawn_async ("/", + spawn_args, + spawn_env, + SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, + null, + out child_pid); + + ChildWatch.add (child_pid, (pid, status) => { + // Triggered when the child indicated by child_pid exits + Process.close_pid (pid); + }); + + } catch (SpawnError e) { + print ("Error: %s\n", e.message); + } + } + + public double get_priority() { + return 2.0; + } + + public async void notify_message(Message message, Conversation conversation, + string conversation_display_name, string? participant_display_name) { + // FIXME: unsafe + // send_notification(message.body); + send_notification("New message"); + } + + public async void notify_file(FileTransfer file_transfer, Conversation conversation, bool is_image, string conversation_display_name, string? participant_display_name) { + warning("TODO: notify file"); + } + + public async void notify_call(Call call, Conversation conversation, bool video, bool multiparty, string conversation_display_name) { + // FIXME: unsafe + // send_notification("Call from " + conversation_display_name); + send_notification("New call"); + } + + public async void retract_call_notification(Call call, Conversation conversation) { + warning("TODO: retract call notification"); + } + + public async void notify_dialing() { + warning("TODO: notify dialing"); + } + + public async void retract_dialing() { + warning("TODO: retract dialing"); + } + + public async void notify_subscription_request(Conversation conversation) { + // FIXME: unsafe + // send_notification("New contact request from " + conversation.counterpart.to_string()); + send_notification("New contact request"); + } + + public async void notify_connection_error(Account account, ConnectionManager.ConnectionError error) { + warning("TODO: notify connection error"); + } + + public async void notify_muc_invite(Account account, Jid room_jid, Jid from_jid, string inviter_display_name) { + string display_room = room_jid.bare_jid.to_string(); + + // FIXME: unsafe + // string body = inviter_display_name + " invited you to " + display_room; + send_notification("New invite"); + + Conversation group_conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation( + room_jid, account, Conversation.Type.GROUPCHAT); + GLib.Application.get_default().activate_action("open-muc-join", new Variant.int32(group_conversation.id)); + } + + public async void notify_voice_request(Conversation conversation, Jid from_jid) { + warning("TODO: notify voice request"); + } + + public async void retract_content_item_notifications() { + warning("TODO: retract content item notifications"); + } + + public async void retract_conversation_notifications(Conversation conversation) { + warning("TODO: retract conversation notifications"); + } +} + +} \ No newline at end of file diff --git a/plugins/mac-notification/src/plugin.vala b/plugins/mac-notification/src/plugin.vala new file mode 100644 index 00000000..a7ff6f2b --- /dev/null +++ b/plugins/mac-notification/src/plugin.vala @@ -0,0 +1,18 @@ +namespace Dino.Plugins.MacNotification { + +public class Plugin : RootInterface, Object { + + public Dino.Application app; + + public void registered(Dino.Application app) { + this.app = app; + + app.stream_interactor.get_module(NotificationEvents.IDENTITY) + .register_notification_provider(new MacNotificationProvider(app)); + info("Registered Mac Notification Provider plugin"); + } + + public void shutdown() { } +} + +} diff --git a/plugins/mac-notification/src/register_plugin.vala b/plugins/mac-notification/src/register_plugin.vala new file mode 100644 index 00000000..c94dd3ea --- /dev/null +++ b/plugins/mac-notification/src/register_plugin.vala @@ -0,0 +1,3 @@ +public Type register_plugin(Module module) { + return typeof (Dino.Plugins.MacNotification.Plugin); +}