File Coverage

blib/lib/Desktop/Notify.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Desktop::Notify;
2              
3 2     2   30852 use warnings;
  2         4  
  2         60  
4 2     2   8 use strict;
  2         2  
  2         52  
5              
6 2     2   368 use Net::DBus;
  0            
  0            
7             use File::Basename;
8             use Data::Dumper;
9              
10             use Desktop::Notify::Notification;
11              
12             =head1 NAME
13              
14             Desktop::Notify - Communicate with the Desktop Notifications framework
15              
16             =head1 VERSION
17              
18             Version 0.04
19              
20             =cut
21              
22             our $VERSION = '0.04';
23              
24             =head1 SYNOPSIS
25              
26             use Desktop::Notify;
27            
28             # Open a connection to the notification daemon
29             my $notify = Desktop::Notify->new();
30            
31             # Create a notification to display
32             my $notification = $notify->create(summary => 'Desktop::Notify',
33             body => 'Hello, world!',
34             timeout => 5000);
35            
36             # Display the notification
37             $notification->show();
38            
39             # Close the notification later
40             $notification->close();
41              
42             =head1 DESCRIPTION
43              
44             This module provides a Perl interface to the Desktop Notifications framework.
45              
46             The framework allows applications to display pop-up notifications on an X
47             desktop. This is implemented with two components: a daemon that displays the
48             notifications, and a client library used by applications to send notifications
49             to the daemon. These components communicate through the DBus message bus
50             protocol.
51              
52             More information is available from
53             L
54              
55             This module serves the same purpose as C, in an object-oriented Perl
56             interface. It is not, however, an interface to C itself, but a
57             separate implementation of the specification using L.
58              
59             =head1 METHODS
60              
61             =head2 new %opts
62              
63             Connect to the notification daemon. %opts can include the following options:
64              
65             =over
66              
67             =item app_name
68              
69             The application name to use for notifications. Default is C
70              
71             =item app_icon
72              
73             Path to an image to use for notification icons.
74              
75             =item bus
76              
77             The Net::DBus mesage bus to use. Default is to call Net::DBus->session, which
78             is usually where notification-daemon can be reached.
79              
80             =item service
81              
82             The DBus service name of the daemon. Default is
83             I.
84              
85             =item objpath
86              
87             The path to the notifications DBus object. Default is
88             I.
89              
90             =item objiface
91              
92             The DBus interface to access the notifications object as. Default is
93             I.
94              
95             =back
96              
97             =cut
98              
99             sub new {
100             my ($class, %opts) = @_;
101              
102             my $self = {};
103              
104             $self->{bus} = $opts{bus} || Net::DBus->session;
105             $self->{service} = $self->{bus}
106             ->get_service($opts{service} || 'org.freedesktop.Notifications');
107             $self->{notify} = $self->{service}
108             ->get_object($opts{objpath} || '/org/freedesktop/Notifications',
109             $opts{objiface} || 'org.freedesktop.Notifications');
110             $self->{app_name} = $opts{app_name} || basename($0);
111             $self->{app_icon} = $opts{app_icon} || '';
112             $self->{notify}->connect_to_signal('NotificationClosed',
113             sub {$self->_close_cb(@_)});
114             $self->{notify}->connect_to_signal('ActionInvoked',
115             sub {$self->_action_cb(@_)});
116              
117             bless $self, $class;
118             }
119              
120             =head2 create %params
121              
122             Creates a new notification object that can be displayed later. This will return
123             a L object; see that module for information
124             about using it.
125              
126             =cut
127              
128             sub create {
129             my ($self, %params) = @_;
130              
131             return new Desktop::Notify::Notification($self, %params);
132             }
133              
134             sub _register_notification {
135             my ($self, $n) = @_;
136             $self->{notes}->{$n->{id}} = $n;
137             }
138              
139             sub _close_cb {
140             my ($self, $nid) = @_;
141             print __PACKAGE__, ": notification closed\n";
142             if ($self->{close_callback})
143             {
144             print "invoking callback\n";
145             $self->{close_callback}->($self->{notes}->{$nid});
146             }
147             delete $self->{notes}->{$nid};
148             }
149              
150             sub _action_cb {
151             my ($self, $nid, $action_key) = @_;
152             print __PACKAGE__, ": action invoked\n";
153             if ($self->{action_callback})
154             {
155             print "invoking callback\n";
156             $self->{action_callback}->($self->{notes}->{$nid}, $action_key);
157             }
158             # delete $self->{notes}->{$nid};
159             }
160              
161             =head2 close_callback $coderef
162              
163             Sets a user-specified function to be called whenever a notification is closed.
164             It will be called with one argument, which is the Notification object that was
165             just closed.
166              
167             =cut
168              
169             sub close_callback {
170             my ($self, $cb) = @_;
171              
172             print "close callback is $cb\n";
173             $self->{close_callback} = $cb;
174             }
175              
176             =head2 action_callback $coderef
177              
178             Sets a user-specified function to be called whenever an action is invoked.
179             It will be called with two arguments, which are the Notification object on which
180             an action was invoked, and the key of the action invoked.
181              
182             =cut
183              
184             sub action_callback {
185             my ($self, $cb) = @_;
186              
187             print "action callback is $cb\n";
188             $self->{action_callback} = $cb;
189             }
190              
191             =head1 AUTHOR
192              
193             Stephen Cavilia, C<< >>
194              
195             =head1 SEE ALSO
196              
197             L
198              
199             L
200              
201             L
202              
203             =head1 BUGS
204              
205             Please report any bugs or feature requests to
206             C, or through the web interface at
207             L.
208             I will be notified, and then you'll automatically be notified of progress on
209             your bug as I make changes.
210              
211             =head1 SUPPORT
212              
213             You can find documentation for this module with the perldoc command.
214              
215             perldoc Desktop::Notify
216              
217             You can also look for information at:
218              
219             =over 4
220              
221             =item * AnnoCPAN: Annotated CPAN documentation
222              
223             L
224              
225             =item * CPAN Ratings
226              
227             L
228              
229             =item * RT: CPAN's request tracker
230              
231             L
232              
233             =item * Search CPAN
234              
235             L
236              
237             =back
238              
239             =head1 ACKNOWLEDGEMENTS
240              
241             =head1 COPYRIGHT & LICENSE
242              
243             Copyright 2007 Stephen Cavilia, all rights reserved.
244              
245             This program is free software; you can redistribute it and/or modify it
246             under the same terms as Perl itself.
247              
248             =cut
249              
250             1; # End of Desktop::Notify