Subject: | [patch] Add support for a user-defined function to be called whenever an action is invoked |
Hi,
the Desktop Notifications specification says that "Implementations are not required to support hyperlinks", and indeed, e.g. GNOME Shell currently doesn't (https://bugzilla.gnome.org/show_bug.cgi?id=653033).
So, when one wants to display a notification that allows the user to open a link, in a way that works e.g. in GNOME Shell, one needs to use notification actions instead of hyperlinks. The attached patch adds the needed support. I've tested it in a small program that uses Net::DBus::Reactor (so that it's still around, ready to react, whenever the action callback is called), and it works fine for me.
Please review and consider applying :)
Subject: | 0001-Add-support-for-a-user-defined-function-to-be-called.patch |
From e84e1f23168ebfb51fc73c272061c295ed9952d8 Mon Sep 17 00:00:00 2001
From: intrigeri <intrigeri@boum.org>
Date: Sun, 8 Mar 2015 23:22:02 +0000
Subject: [PATCH] Add support for a user-defined function to be called whenever
an action is invoked.
---
lib/Desktop/Notify.pm | 30 +++++++++++++++++++++++++++++-
lib/Desktop/Notify/Notification.pm | 18 +++++++++++-------
2 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/lib/Desktop/Notify.pm b/lib/Desktop/Notify.pm
index b8bb248..78f31d4 100644
--- a/lib/Desktop/Notify.pm
+++ b/lib/Desktop/Notify.pm
@@ -106,6 +106,8 @@ sub new {
$self->{app_name} = $opts{app_name} || basename($0);
$self->{notify}->connect_to_signal('NotificationClosed',
sub {$self->_close_cb(@_)});
+ $self->{notify}->connect_to_signal('ActionInvoked',
+ sub {$self->_action_cb(@_)});
bless $self, $class;
}
@@ -140,6 +142,17 @@ sub _close_cb {
delete $self->{notes}->{$nid};
}
+sub _action_cb {
+ my ($self, $nid, $action_key) = @_;
+ print __PACKAGE__, ": action invoked\n";
+ if ($self->{action_callback})
+ {
+ print "invoking callback\n";
+ $self->{action_callback}->($self->{notes}->{$nid}, $action_key);
+ }
+ # delete $self->{notes}->{$nid};
+}
+
=head2 close_callback $coderef
Sets a user-specified function to be called whenever a notification is closed.
@@ -151,10 +164,25 @@ just closed.
sub close_callback {
my ($self, $cb) = @_;
- print "callback is $cb\n";
+ print "close callback is $cb\n";
$self->{close_callback} = $cb;
}
+=head2 action_callback $coderef
+
+Sets a user-specified function to be called whenever an action is invoked.
+It will be called with two arguments, which are the Notification object on which
+an action was invoked, and the key of the action invoked.
+
+=cut
+
+sub action_callback {
+ my ($self, $cb) = @_;
+
+ print "action callback is $cb\n";
+ $self->{action_callback} = $cb;
+}
+
=head1 AUTHOR
Stephen Cavilia, C<< <sac at atomicradi.us> >>
diff --git a/lib/Desktop/Notify/Notification.pm b/lib/Desktop/Notify/Notification.pm
index ee3fe4a..e7710e8 100644
--- a/lib/Desktop/Notify/Notification.pm
+++ b/lib/Desktop/Notify/Notification.pm
@@ -59,6 +59,7 @@ sub new {
my $self = \%params;
$self->{server} = $server;
$self->{id} = undef;
+ $self->{actions} ||= {};
bless $self, $class;
}
@@ -81,7 +82,7 @@ sub show {
'',
$self->{summary},
$self->{body},
- [],
+ [%{$self->{actions}}],
{},
$self->{timeout} || 0,
);
@@ -125,6 +126,15 @@ The summary text briefly describing the notification.
The optional detailed body text. Can be empty.
+=item actions
+
+Actions are sent over as a list of pairs. Each even element in the list
+(starting at index 0) represents the identifier for the action. Each odd
+element in the list is the localized string that will be displayed to the user.
+
+A user-specified function to be called whenever an action is invoked can be
+specified with L<Desktop::Notify>'s L<action_callback> method.
+
=item timeout
The timeout time in milliseconds since the display of the notification at which
@@ -145,12 +155,6 @@ supported by L<Desktop::Notify> at this time
The optional program icon of the calling application.
-=item actions
-
-Actions are sent over as a list of pairs. Each even element in the list
-(starting at index 0) represents the identifier for the action. Each odd
-element in the list is the localized string that will be displayed to the user.
-
=item hints
Optional hints that can be passed to the server from the client program.
--
2.1.4