| 1 | package Plagger::Plugin::Notify::Balloon; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | our $VERSION = '0.01'; | ||
| 6 | |||
| 7 | use File::Spec; | ||
| 8 | use Encode (); | ||
| 9 | |||
| 10 | sub register { | ||
| 11 | my($self, $context) = @_; | ||
| 12 | |||
| 13 | if ($^O eq 'MSWin32') { | ||
| 14 | if ($self->has_balloon_notify) { | ||
| 15 | $context->register_hook( | ||
| 16 | $self, | ||
| 17 | 'publish.entry' => \¬ify, | ||
| 18 | 'plugin.init' => \&initialize, | ||
| 19 | ); | ||
| 20 | } else { | ||
| 21 | $context->log(error => "BalloonNotify is not in your PATH."); | ||
| 22 | } | ||
| 23 | } else { | ||
| 24 | $context->log(error => "This plugin only works on Win32 systems"); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | sub has_balloon_notify { | ||
| 29 | my $self = shift; | ||
| 30 | grep { -e File::Spec->catfile($_, 'BalloonNotify.exe') } File::Spec->path; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub initialize { | ||
| 34 | my($self, $context) = @_; | ||
| 35 | |||
| 36 | return if $self->conf->{encoding}; | ||
| 37 | |||
| 38 | my $cp = eval { | ||
| 39 | require Win32::Console; | ||
| 40 | Win32::Console::OutputCP(); | ||
| 41 | }; | ||
| 42 | $cp ||= 932; # cp932 by default ... for Japanese environment | ||
| 43 | $self->conf->{encoding} = "cp$cp"; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub notify { | ||
| 47 | my($self, $context, $args) = @_; | ||
| 48 | |||
| 49 | my $title = $self->scrub($args->{entry}->title); | ||
| 50 | my $message = $self->scrub($args->{entry}->body_text); | ||
| 51 | |||
| 52 | my @command = ('BalloonNotify', '/o', 5, '/t', $title, '/c', $message); | ||
| 53 | !system(@command) or $context->log(error => $?); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub scrub { | ||
| 57 | my($self, $string) = @_; | ||
| 58 | $string =~ s/\s+/ /g; | ||
| 59 | Encode::encode($self->conf->{encoding}, $string); | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | __END__ |