From 8fdb38b99be9c588148d576c23212af765ff7302 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Tue, 19 Sep 2017 22:41:33 +0200 Subject: Use GNotification instead of libnotify --- CMakeLists.txt | 1 + README.md | 1 - cmake/FindLibnotify.cmake | 32 ------- libdino/src/service/chat_interaction.vala | 4 + libdino/src/service/conversation_manager.vala | 9 ++ main/CMakeLists.txt | 4 +- main/data/dino.desktop | 13 --- main/data/im.dino.desktop | 13 +++ main/data/im.dino.service | 3 + main/src/ui/application.vala | 1 - main/src/ui/notifications.vala | 116 +++++++++++++++----------- main/src/ui/util/label_hybrid.vala | 20 +++-- 12 files changed, 112 insertions(+), 105 deletions(-) delete mode 100644 cmake/FindLibnotify.cmake delete mode 100644 main/data/dino.desktop create mode 100644 main/data/im.dino.desktop create mode 100644 main/data/im.dino.service diff --git a/CMakeLists.txt b/CMakeLists.txt index 929834ca..caaf80d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ set_path(SHARE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/share" "Installation dire set_path(BIN_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/bin" "Installation directory for user executables") set_path(DATA_INSTALL_DIR "${SHARE_INSTALL_PREFIX}/dino" "Installation directory for dino-specific data") set_path(DESKTOP_FILE_INSTALL_DIR "${SHARE_INSTALL_PREFIX}/applications" "Installation directory for .desktop files") +set_path(SERVICE_FILE_INSTALL_DIR "${SHARE_INSTALL_PREFIX}/dbus-1/service" "Installation directory for .service files") set_path(ICON_INSTALL_DIR "${SHARE_INSTALL_PREFIX}/icons" "Installation directory for icons") set_path(INCLUDE_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/include" "Installation directory for C header files") set_path(LIB_INSTALL_DIR "${EXEC_INSTALL_PREFIX}/${LIBDIR_NAME}" "Installation directory for object code libraries") diff --git a/README.md b/README.md index 7953f8ef..73544815 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ Build * GPGME (For the OpenPGP plugin) * libgee-0.8 (≥ 0.10) * libgcrypt (For the OMEMO plugin) -* libnotify * libsoup (For the HTTP files plugin) * SQLite3 diff --git a/cmake/FindLibnotify.cmake b/cmake/FindLibnotify.cmake deleted file mode 100644 index 61eae949..00000000 --- a/cmake/FindLibnotify.cmake +++ /dev/null @@ -1,32 +0,0 @@ -include(PkgConfigWithFallback) -find_pkg_config_with_fallback(Libnotify - PKG_CONFIG_NAME libnotify - LIB_NAMES notify - INCLUDE_NAMES libnotify/notify.h - DEPENDS GIO GDKPixbuf2 -) - -if(Libnotify_FOUND AND NOT Libnotify_VERSION) - find_file(Libnotify_FEATURES_HEADER "libnotify/notify-features.h" HINTS ${Libnotify_INCLUDE_DIRS}) - mark_as_advanced(Libnotify_FEATURES_HEADER) - - if(Libnotify_FEATURES_HEADER) - file(STRINGS "${Libnotify_FEATURES_HEADER}" Libnotify_MAJOR_VERSION REGEX "^#define NOTIFY_VERSION_MAJOR +\\(?([0-9]+)\\)?$") - string(REGEX REPLACE "^#define NOTIFY_VERSION_MAJOR +\\(?([0-9]+)\\)?$" "\\1" Libnotify_MAJOR_VERSION "${Libnotify_MAJOR_VERSION}") - file(STRINGS "${Libnotify_FEATURES_HEADER}" Libnotify_MINOR_VERSION REGEX "^#define NOTIFY_VERSION_MINOR +\\(?([0-9]+)\\)?$") - string(REGEX REPLACE "^#define NOTIFY_VERSION_MINOR +\\(?([0-9]+)\\)?$" "\\1" Libnotify_MINOR_VERSION "${Libnotify_MINOR_VERSION}") - file(STRINGS "${Libnotify_FEATURES_HEADER}" Libnotify_MICRO_VERSION REGEX "^#define NOTIFY_VERSION_MICRO +\\(?([0-9]+)\\)?$") - string(REGEX REPLACE "^#define NOTIFY_VERSION_MICRO +\\(?([0-9]+)\\)?$" "\\1" Libnotify_MICRO_VERSION "${Libnotify_MICRO_VERSION}") - set(Libnotify_VERSION "${Libnotify_MAJOR_VERSION}.${Libnotify_MINOR_VERSION}.${Libnotify_MICRO_VERSION}") - unset(Libnotify_MAJOR_VERSION) - unset(Libnotify_MINOR_VERSION) - unset(Libnotify_MICRO_VERSION) - endif() -endif() - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(Libnotify - FOUND_VAR Libnotify_FOUND - REQUIRED_VARS Libnotify_LIBRARY - VERSION_VAR Libnotify_VERSION -) \ No newline at end of file diff --git a/libdino/src/service/chat_interaction.vala b/libdino/src/service/chat_interaction.vala index 0aefc418..f08534aa 100644 --- a/libdino/src/service/chat_interaction.vala +++ b/libdino/src/service/chat_interaction.vala @@ -9,6 +9,8 @@ public class ChatInteraction : StreamInteractionModule, Object { public static ModuleIdentity IDENTITY = new ModuleIdentity("chat_interaction"); public string id { get { return IDENTITY.id; } } + public signal void focused_in(Conversation conversation); + public signal void focused_out(Conversation conversation); public signal void conversation_read(Conversation conversation); public signal void conversation_unread(Conversation conversation); @@ -77,6 +79,7 @@ public class ChatInteraction : StreamInteractionModule, Object { private void on_conversation_focused(Conversation? conversation) { focus_in = true; if (conversation == null) return; + focused_in(selected_conversation); conversation_read(selected_conversation); check_send_read(); selected_conversation.read_up_to = stream_interactor.get_module(MessageStorage.IDENTITY).get_last_message(conversation); @@ -85,6 +88,7 @@ public class ChatInteraction : StreamInteractionModule, Object { private void on_conversation_unfocused(Conversation? conversation) { focus_in = false; if (conversation == null) return; + focused_out(selected_conversation); if (last_input_interaction.has_key(conversation)) { send_chat_state_notification(conversation, Xep.ChatStateNotifications.STATE_PAUSED); last_input_interaction.unset(conversation); diff --git a/libdino/src/service/conversation_manager.vala b/libdino/src/service/conversation_manager.vala index 0f827e26..471ec74a 100644 --- a/libdino/src/service/conversation_manager.vala +++ b/libdino/src/service/conversation_manager.vala @@ -74,6 +74,15 @@ public class ConversationManager : StreamInteractionModule, Object { return null; } + public Conversation? get_conversation_by_id(int id) { + foreach (HashMap hm in conversations.values) { + foreach (Conversation conversation in hm.values) { + if (conversation.id == id) return conversation; + } + } + return null; + } + public Gee.List get_active_conversations() { Gee.List ret = new ArrayList(Conversation.equals_func); foreach (Account account in conversations.keys) { diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cd504810..b6f079c7 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -9,7 +9,6 @@ find_packages(MAIN_PACKAGES REQUIRED GModule GObject GTK3>=3.22 - Libnotify ) set(RESOURCE_LIST @@ -145,7 +144,8 @@ if(WIN32) endif(WIN32) install(TARGETS dino ${TARGET_INSTALL}) -install(FILES data/dino.desktop DESTINATION ${DESKTOP_FILE_INSTALL_DIR}) +install(FILES data/im.dino.desktop DESTINATION ${DESKTOP_FILE_INSTALL_DIR}) +install(FILES data/im.dino.service DESTINATION ${SERVICE_FILE_INSTALL_DIR}) install(FILES data/icons/dino.svg DESTINATION ${ICON_INSTALL_DIR}/hicolor/scalable/apps) install(FILES data/icons/dino-symbolic.svg DESTINATION ${ICON_INSTALL_DIR}/hicolor/symbolic/apps) install(FILES diff --git a/main/data/dino.desktop b/main/data/dino.desktop deleted file mode 100644 index 6e2d2016..00000000 --- a/main/data/dino.desktop +++ /dev/null @@ -1,13 +0,0 @@ -[Desktop Entry] -Version=1.0 -Name=Dino -GenericName=Jabber/XMPP Client -Keywords=chat;talk;im;message;xmpp;jabber; -Exec=dino -Icon=dino -StartupNotify=false -Terminal=false -Type=Application -Categories=GTK;Network;Chat;InstantMessaging; -X-GNOME-UsesNotifications=true -MimeType=x-scheme-handler/xmpp; diff --git a/main/data/im.dino.desktop b/main/data/im.dino.desktop new file mode 100644 index 00000000..6e2d2016 --- /dev/null +++ b/main/data/im.dino.desktop @@ -0,0 +1,13 @@ +[Desktop Entry] +Version=1.0 +Name=Dino +GenericName=Jabber/XMPP Client +Keywords=chat;talk;im;message;xmpp;jabber; +Exec=dino +Icon=dino +StartupNotify=false +Terminal=false +Type=Application +Categories=GTK;Network;Chat;InstantMessaging; +X-GNOME-UsesNotifications=true +MimeType=x-scheme-handler/xmpp; diff --git a/main/data/im.dino.service b/main/data/im.dino.service new file mode 100644 index 00000000..02339e92 --- /dev/null +++ b/main/data/im.dino.service @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=im.dino +Exec=dino diff --git a/main/src/ui/application.vala b/main/src/ui/application.vala index 65bb8743..5ce5e063 100644 --- a/main/src/ui/application.vala +++ b/main/src/ui/application.vala @@ -16,7 +16,6 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { public Application() throws Error { Object(application_id: "im.dino", flags: ApplicationFlags.HANDLES_OPEN); init(); - Notify.init("dino"); Environment.set_application_name("Dino"); Window.set_default_icon_name("dino"); diff --git a/main/src/ui/notifications.vala b/main/src/ui/notifications.vala index 470f9d6d..313d0854 100644 --- a/main/src/ui/notifications.vala +++ b/main/src/ui/notifications.vala @@ -11,7 +11,8 @@ public class Notifications : Object { private StreamInteractor stream_interactor; private Gtk.Window window; - private HashMap notifications = new HashMap(Conversation.hash_func, Conversation.equals_func); + private HashMap notifications = new HashMap(Conversation.hash_func, Conversation.equals_func); + private Set? active_notification_ids = null; private enum ClosedReason { // org.freedesktop.Notifications.NotificationClosed EXPIRED = 1, @@ -23,6 +24,51 @@ public class Notifications : Object { public Notifications(StreamInteractor stream_interactor, Gtk.Window window) { this.stream_interactor = stream_interactor; this.window = window; + + stream_interactor.get_module(ChatInteraction.IDENTITY).focused_in.connect(() => { + if (active_notification_ids == null) { + Gee.List conversations = stream_interactor.get_module(ConversationManager.IDENTITY).get_active_conversations(); + foreach (Conversation conversation in conversations) { + GLib.Application.get_default().withdraw_notification(conversation.id.to_string()); + } + active_notification_ids = new HashSet(); + } else { + foreach (string id in active_notification_ids) { + GLib.Application.get_default().withdraw_notification(id); + } + active_notification_ids.clear(); + } + }); + + SimpleAction open_conversation_action = new SimpleAction("open-conversation", VariantType.INT32); + open_conversation_action.activate.connect((variant) => { + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(variant.get_int32()); + if (conversation != null) conversation_selected(conversation); + window.present(); + }); + GLib.Application.get_default().add_action(open_conversation_action); + + SimpleAction accept_subscription_action = new SimpleAction("accept-subscription", VariantType.INT32); + accept_subscription_action.activate.connect((variant) => { + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(variant.get_int32()); + if (conversation == null) return; + stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(conversation.account, conversation.counterpart); + if (stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, conversation.counterpart) == null) { + AddConversation.Chat.AddContactDialog dialog = new AddConversation.Chat.AddContactDialog(stream_interactor); + dialog.jid = conversation.counterpart.bare_jid.to_string(); + dialog.account = conversation.account; + dialog.present(); + } + }); + GLib.Application.get_default().add_action(accept_subscription_action); + + SimpleAction deny_subscription_action = new SimpleAction("deny-subscription", VariantType.INT32); + deny_subscription_action.activate.connect((variant) => { + Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation_by_id(variant.get_int32()); + if (conversation == null) return; + stream_interactor.get_module(PresenceManager.IDENTITY).deny_subscription(conversation.account, conversation.counterpart); + }); + GLib.Application.get_default().add_action(deny_subscription_action); } public void start() { @@ -34,65 +80,33 @@ public class Notifications : Object { if (!should_notify_message(message, conversation)) return; if (!notifications.has_key(conversation)) { - notifications[conversation] = new Notify.Notification("", null, null); - notifications[conversation].set_hint("persistent", true); - notifications[conversation].add_action("default", "Open", () => { - conversation_selected(conversation); -#if GDK3_WITH_X11 - Gdk.X11.Window x11window = window.get_window() as Gdk.X11.Window; - if (x11window != null) { - window.present_with_time(Gdk.X11.get_server_time(x11window)); - } else { - window.present(); - } -#else - window.present(); -#endif - }); + notifications[conversation] = new Notification(""); + notifications[conversation].set_default_action_and_target_value("app.open-conversation", new Variant.int32(conversation.id)); } if (!stream_interactor.get_module(ChatInteraction.IDENTITY).is_active_focus()) { string display_name = Util.get_conversation_display_name(stream_interactor, conversation); string text = message.body; if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)) { string muc_occupant = Util.get_display_name(stream_interactor, message.from, conversation.account); - text = @"$muc_occupant $text"; + text = @"$muc_occupant: $text"; } - notifications[conversation].update(display_name, text, null); - notifications[conversation].set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_conversation(stream_interactor, conversation)); - notifications[conversation].set_timeout(3); - try { - notifications[conversation].show(); - } catch (Error error) { } + notifications[conversation].set_title(display_name); + notifications[conversation].set_body(text); + notifications[conversation].set_icon(get_pixbuf_icon((new AvatarGenerator(40, 40)).draw_conversation(stream_interactor, conversation))); + window.get_application().send_notification(conversation.id.to_string(), notifications[conversation]); + active_notification_ids.add(conversation.id.to_string()); window.urgency_hint = true; } } private void on_received_subscription_request(Jid jid, Account account) { - Notify.Notification notification = new Notify.Notification(_("Subscription request"), jid.bare_jid.to_string(), null); - notification.set_image_from_pixbuf((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account)); - notification.set_hint("persistent", true); - notification.add_action("accept", _("Accept"), () => { - stream_interactor.get_module(PresenceManager.IDENTITY).approve_subscription(account, jid); - - if (stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(account, jid) == null) { - AddConversation.Chat.AddContactDialog dialog = new AddConversation.Chat.AddContactDialog(stream_interactor); - dialog.jid = jid.bare_jid.to_string(); - dialog.account = account; - dialog.present(); - } - try { - notification.close(); - } catch (Error error) { } - }); - notification.add_action("deny", _("Deny"), () => { - stream_interactor.get_module(PresenceManager.IDENTITY).deny_subscription(account, jid); - try { - notification.close(); - } catch (Error error) { } - }); - try { - notification.show(); - } catch (Error error) { } + Notification notification = new Notification(_("Subscription request")); + notification.set_body(jid.bare_jid.to_string()); + notification.set_icon(get_pixbuf_icon((new AvatarGenerator(40, 40)).draw_jid(stream_interactor, jid, account))); + Conversation conversation = stream_interactor.get_module(ConversationManager.IDENTITY).create_conversation(jid, account, Conversation.Type.CHAT); + notification.add_button_with_target_value(_("Accept"), "app.accept-subscription", conversation.id); + notification.add_button_with_target_value(_("Deny"), "app.deny-subscription", conversation.id); + window.get_application().send_notification(null, notification); } private bool should_notify_message(Entities.Message message, Conversation conversation) { @@ -102,6 +116,12 @@ public class Notifications : Object { if (notify == Conversation.NotifySetting.HIGHLIGHT && nick != null && !message.body.contains(nick.resourcepart)) return false; return true; } + + private Icon get_pixbuf_icon(Gdk.Pixbuf avatar) { + uint8[] buffer; + avatar.save_to_buffer(out buffer, "png"); + return new BytesIcon(new Bytes(buffer)); + } } } diff --git a/main/src/ui/util/label_hybrid.vala b/main/src/ui/util/label_hybrid.vala index ab765dd1..8ca56316 100644 --- a/main/src/ui/util/label_hybrid.vala +++ b/main/src/ui/util/label_hybrid.vala @@ -34,13 +34,7 @@ public class EntryLabelHybrid : LabelHybrid { get { return entry.text; } set { entry.text = value; - if (visibility) { - label.label = value; - } else { - string filler = ""; - for (int i = 0; i < value.length; i++) filler += entry.get_invisible_char().to_string(); - label.label = filler; - } + set_label_label(value); } } @@ -83,7 +77,7 @@ public class EntryLabelHybrid : LabelHybrid { if (event.keyval == Gdk.Key.Return) { show_label(); } else { - label.label = entry.text; + set_label_label(entry.text); } return false; }); @@ -93,6 +87,16 @@ public class EntryLabelHybrid : LabelHybrid { }); } + private void set_label_label(string value) { + if (visibility) { + label.label = value; + } else { + string filler = ""; + for (int i = 0; i < value.length; i++) filler += entry.get_invisible_char().to_string(); + label.label = filler; + } + } + private void update_label() { text = text; } -- cgit v1.2.3-54-g00ecf