blib/lib/Padre/Plugin/Alarm.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 190 CC 14 SCAR 26.5
CC
14
Cov
0%
on_timer_alarm
42.0
stop_alarm
20.0
_set_alarm
12.0
CRAP 14.2
1 package Padre::Plugin::Alarm;
2 BEGIN {
3 $Padre::Plugin::Alarm::VERSION = '0.14';
4 }
5  
6 # ABSTRACT: Alarm Clock in Padre
7  
8 use warnings;
9 use strict;
10  
11 use base 'Padre::Plugin';
12  
13 use Padre::Wx ();
14 use Padre::Wx::Dialog ();
15 use Padre::Util ();
16  
17 our $alarm_timer_id;
18  
19 sub padre_interfaces {
20 'Padre::Plugin' => '0.47',;
21 }
22  
23 sub menu_plugins_simple {
24 my $self = shift;
25  
26 # check if we need set timer on
27 my $config = $self->config_read;
28 if ( $config and exists $config->{alarms} and scalar @{ $config->{alarms} } ) {
29 _set_alarm();
30 }
31  
32 return (
33 Wx::gettext('Alarm Clock') => [
34 Wx::gettext('Set Alarm Time'), sub { $self->set_alarm_time },
35 Wx::gettext('Stop Alarm'), sub { $self->stop_alarm },
36 Wx::gettext('Clear Alarm'), sub { $self->clear_alarm },
37 ]
38 );
39 }
40  
41 sub set_alarm_time {
42 my ($self) = shift;
43 my $main = $self->main;
44  
45 my @frequency = ( 'once', 'daily' );
46 my @layout = (
47 [ [ 'Wx::StaticText', undef, Wx::gettext('Time (eg: 23:55):') ],
48 [ 'Wx::TextCtrl', '_alarm_time_', '' ],
49 ],
50 [ [ 'Wx::StaticText', undef, Wx::gettext('Frequency:') ],
51 [ 'Wx::ComboBox', '_frequency_', 'once', \@frequency ],
52 ],
53 [ [ 'Wx::Button', '_ok_', Wx::wxID_OK ],
54 [ 'Wx::Button', '_cancel_', Wx::wxID_CANCEL ],
55 ],
56 );
57 my $dialog = Padre::Wx::Dialog->new(
58 parent => $main,
59 title => Wx::gettext("Set Alarm Time"),
60 layout => \@layout,
61 width => [ 100, 200 ],
62 bottom => 20,
63 );
64 $dialog->{_widgets_}{_ok_}->SetDefault;
65 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}{_ok_}, \&ok_clicked );
66 Wx::Event::EVT_BUTTON( $dialog, $dialog->{_widgets_}{_cancel_}, \&cancel_clicked );
67  
68 $dialog->{_widgets_}{_alarm_time_}->SetFocus;
69 $dialog->Show(1);
70  
71 return 1;
72 }
73  
74 sub cancel_clicked {
75 my ( $dialog, $event ) = @_;
76  
77 $dialog->Destroy;
78  
79 return;
80 }
81  
82 sub ok_clicked {
83 my ( $dialog, $event ) = @_;
84  
85 my $data = $dialog->get_data;
86 $dialog->Destroy;
87  
88 my $main = Padre->ide->wx->main;
89  
90 my $alarm_time = $data->{_alarm_time_};
91 if ( $alarm_time !~ /^\d{1,2}\:\d{2}$/ ) {
92 Wx::MessageBox(
93 Wx::gettext('Possible Value Format: \d:\d\d or \d\d:\d\d like 6:13 or 23:55'),
94 Wx::gettext("Wrong Alarm Time"),
95 Wx::wxOK, $main
96 );
97 return;
98 }
99  
100 my $frequency = $data->{_frequency_};
101  
102 # stupid hack to get $self for config_read
103 my $self = bless {}, __PACKAGE__;
104 my $config = $self->config_read;
105 push @{ $config->{alarms} },
106 {
107 time => $alarm_time,
108 frequency => $frequency,
109 status => 'enabled',
110 };
111 $self->config_write($config);
112  
113 _set_alarm();
114 }
115  
116 sub _set_alarm {
117 my $main = Padre->ide->wx->main;
118  
119 $alarm_timer_id = Wx::NewId unless $alarm_timer_id;
120  
121 my $timer = Wx::Timer->new( $main, $alarm_timer_id );
122 unless ( $timer->IsRunning ) {
123 Wx::Event::EVT_TIMER( $main, $alarm_timer_id, \&on_timer_alarm );
124 $timer->Start( 1000, 0 ); # every second
125 }
126 }
127  
128 sub stop_alarm {
129 my ( $self, $opts ) = @_;
130 my $main = $self->main;
131  
132 if ( not $opts->{no_message} ) {
133 $main->message( Wx::gettext('All Alarms are stopped'), Wx::gettext('Stop Alarm') );
134 }
135  
136 return unless $alarm_timer_id;
137 my $timer = Wx::Timer->new( $main, $alarm_timer_id );
138 if ( $timer->IsRunning ) {
139 $timer->Stop();
140 }
141 }
142  
143 sub clear_alarm {
144 my ($self) = shift;
145 my $main = $self->main;
146  
147 my $config = $self->config_read;
148 $config->{alarms} = [];
149 $self->config_write($config);
150  
151 $self->stop_alarm( { no_message => 1 } );
152 $main->message( Wx::gettext('All Alarms are cleared'), Wx::gettext('Clear Alarm') );
153 }
154  
155 sub on_timer_alarm {
156  
157 # stupid hack to get $self for config_read
158 my $self = bless {}, __PACKAGE__;
159  
160 my $config = $self->config_read;
161  
162 return unless ( $config and exists $config->{alarms} );
163  
164 # get now-time;
165 my @ntime = localtime();
166 my $ntime = sprintf( '%d:%02d', $ntime[2], $ntime[1] );
167  
168 my $did_something = 0;
169 foreach ( @{ $config->{alarms} } ) {
170 return unless ( $_->{status} eq 'enabled' );
171  
172 # check if it's the time
173 my $time = $_->{time};
174 $time =~ s/^0//;
175  
176 if ( $time eq $ntime ) {
177 $did_something = 1;
178 play_alarm();
179 my $frequency = $_->{frequency};
180 if ( $frequency eq 'once' ) {
181 $_->{status} = 'disabled';
182 }
183 }
184 }
185  
186 if ($did_something) {
187 $config->{alarms} = [ grep { $_->{status} eq 'enabled' } @{ $config->{alarms} } ];
188 $self->config_write($config);
189 }
190 }
191  
192 sub play_alarm {
193 my $audio_dir = File::Spec->catdir( Padre::Util::share('Alarm'), 'audio' );
194 my $file = File::Spec->catfile( $audio_dir, 'alarm.wav' );
195 $file = '' unless -f $file;
196 my $sound = Wx::Sound->new($file);
197 $sound->Play(Wx::wxSOUND_ASYNC);
198 }
199  
200 1;
201  
202  
203 =pod
204  
205 =head1 NAME
206  
207 Padre::Plugin::Alarm - Alarm Clock in Padre
208  
209 =head1 VERSION
210  
211 version 0.14
212  
213 =head1 SYNOPSIS
214  
215 $>padre
216 Plugins -> Alarm Clock -> *
217  
218 =head1 DESCRIPTION
219  
220 A simple Alarm Clock plugin
221  
222 =head1 THANKS
223  
224 The alarm sound sample was taken from L<http://www.freesound.org/>.
225 It was made by ryansnook - L<http://www.freesound.org/usersViewSingle.php?id=430094> and
226 is being licensed under L<http://creativecommons.org/licenses/sampling+/1.0/>.
227  
228 =head1 AUTHORS
229  
230 =over 4
231  
232 =item *
233  
234 Fayland Lam <fayland@gmail.com>
235  
236 =item *
237  
238 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
239  
240 =back
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2010 by Fayland Lam.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
250  
251  
252 __END__