File Coverage

blib/lib/Log/Dispatch/Desktop/Notify.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 56 56 100.0


line stmt bran cond sub pod time code
1 5     5   350972 use strict;
  5         8  
  5         117  
2 5     5   16 use warnings;
  5         4  
  5         219  
3             # Copyright (C) 2017 Christian Garbs <mitch@cgarbs.de>
4             # Licensed under GNU GPL v2 or later.
5              
6             package Log::Dispatch::Desktop::Notify;
7             $Log::Dispatch::Desktop::Notify::VERSION = 'v0.0.2';
8             # ABSTRACT: Log::Dispatch notification backend using Desktop::Notify
9              
10 5     5   418 use Desktop::Notify;
  4         4  
  4         70  
11 4     4   1447 use Log::Dispatch::Null;
  4         711293  
  4         108  
12 4     4   27 use Try::Tiny;
  4         5  
  4         200  
13              
14 4     4   16 use parent 'Log::Dispatch::Output';
  4         5  
  4         24  
15              
16              
17             sub new {
18 9     9 1 14165 my ($class, %params) = @_;
19              
20 9 100       21 if (_desktop_notify_unavailable()) {
21 1         9 return Log::Dispatch::Null->new(%params);
22             };
23              
24 8         112 my $self = bless {
25             _timeout => -1,
26             _app_name => $0,
27             }, $class;
28              
29 8         42 $self->_basic_init(%params);
30 8         593 $self->_init(%params);
31              
32 8         68 return $self;
33             };
34              
35             sub _init {
36 8     8   16 my ($self, %params) = @_;
37              
38 8 100       22 $self->{_app_name} = $params{app_name} if defined $params{app_name};
39 8 100       29 $self->{_timeout} = $params{timeout} if defined $params{timeout};
40              
41 8         24 $self->{_notify} = Desktop::Notify->new( app_name => $self->{_app_name} );
42             };
43              
44             sub _desktop_notify_unavailable() {
45             return try {
46 9     9   324 Desktop::Notify->new();
47 8         76 0;
48             } catch {
49 1     1   28 1;
50             }
51 9     9   127 }
52              
53              
54             sub log_message {
55 5     5 1 268 my ($self, %params) = @_;
56              
57             my $notification = $self->{_notify}->create(
58             summary => $params{message},
59             timeout => $self->{_timeout},
60 5         16 );
61              
62 5         67 $notification->show();
63             };
64              
65              
66             1;
67              
68             __END__
69              
70             =pod
71              
72             =encoding UTF-8
73              
74             =head1 NAME
75              
76             Log::Dispatch::Desktop::Notify - Log::Dispatch notification backend using Desktop::Notify
77              
78             =head1 VERSION
79              
80             version v0.0.2
81              
82             =head1 SYNOPSIS
83              
84             use Log::Dispatch;
85             use Log::Dispatch::Desktop::Notify;
86              
87             my $log = Log::Dispatch->new();
88              
89             $log->add( Log::Dispatch::Desktop::Notify->new(
90             min_level => 'warning'
91             ));
92              
93             $log->log( level => 'warning', message => 'a problem!' );
94              
95             =head1 DESCRIPTION
96              
97             Log::Dispatch::Desktop::Notify is a backend for L<Log::Dispatch> that
98             displays messages via the Desktop Notification Framework (think
99             C<libnotify>) using L<Desktop::Notify>.
100              
101             =head1 METHODS
102              
103             =head2 new
104              
105             Creates a new L<Log::Dispatch::Desktop::Notify> object. Expects named
106             parameters as a hash. In addition to the usual parameters of
107             L<Log::Dispatch::Output> these parameters are also supported:
108              
109             =over
110              
111             =item timeout
112              
113             Default value: C<-1>
114              
115             Sets the message timeout in milliseconds. C<0> disables the timeout,
116             the message has to be closed manually. C<-1> uses the default timeout
117             of the notification server.
118              
119             =item app_name
120              
121             Default value: C<$0> (script name)
122              
123             Sets the application name for the message display.
124              
125             =back
126              
127             Note: If L<Desktop::Notify> can't establish a Dbus session (no
128             messages can be sent), a L<Log::Dispatch::Null> object is returned
129             instead.
130              
131             =head2 log_message
132              
133             This message is called internally by C<Log::Dispatch::log()> to
134             display a message. Expects named parameters in a hash. Currently,
135             only the usual L<Log::Dispatch::Output> parameters C<level> and
136             C<message> are supported.
137              
138             =head1 BUGS AND LIMITATIONS
139              
140             To report a bug, please use the github issue tracker:
141             L<https://github.com/mmitch/log-dispatch-desktop-notify/issues>
142              
143             =head1 AVAILABILITY
144              
145             =over
146              
147             =item github repository
148              
149             L<git://github.com/mmitch/log-dispatch-desktop-notify.git>
150              
151             =item github browser
152              
153             L<https://github.com/mmitch/log-dispatch-desktop-notify>
154              
155             =item github issue tracker
156              
157             L<https://github.com/mmitch/log-dispatch-desktop-notify/issues>
158              
159             =back
160              
161             =begin html
162              
163             =head1 BUILD STATUS
164              
165             <p><a href="https://travis-ci.org/mmitch/log-dispatch-desktop-notify"><img src="https://travis-ci.org/mmitch/log-dispatch-desktop-notify.svg?branch=master" alt="Build Status"></a></p>
166              
167              
168             =end html
169              
170             =begin html
171              
172             =head1 TEST COVERAGE
173              
174             <p><a href="https://codecov.io/github/mmitch/log-dispatch-desktop-notify?branch=master"><img src="https://codecov.io/github/mmitch/log-dispatch-desktop-notify/coverage.svg?branch=master" alt="Coverage Status"></a></p>
175              
176              
177             =end html
178              
179             =head1 SEE ALSO
180              
181             =over
182              
183             =item *
184              
185             L<Log::Dispatch>
186              
187             =item *
188              
189             L<Desktop::Notify>
190              
191             =back
192              
193             =head1 AUTHOR
194              
195             Christian Garbs <mitch@cgarbs.de>
196              
197             =head1 COPYRIGHT AND LICENSE
198              
199             Copyright (C) 2017 Christian Garbs
200              
201             This program is free software: you can redistribute it and/or modify it
202             under the terms of the GNU General Public License as published by the Free
203             Software Foundation, either version 2 of the License, or (at your option)
204             any later version.
205              
206             This program is distributed in the hope that it will be useful, but WITHOUT
207             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
208             FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
209             more details.
210              
211             You should have received a copy of the GNU General Public License along
212             with this program. If not, see <http://www.gnu.org/licenses/>.
213              
214             =cut