File Coverage

blib/lib/Padre/Plugin/SpellCheck.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Padre::Plugin::SpellCheck;
2              
3 2     2   148459 use 5.010001;
  2         9  
  2         109  
4 2     2   13 use strict;
  2         19  
  2         84  
5 2     2   11 use warnings;
  2         15  
  2         71  
6              
7 2     2   1296 use Padre::Plugin ();
  0            
  0            
8             use Padre::Unload ();
9             use File::Which ();
10             use Try::Tiny;
11              
12             our $VERSION = '1.33';
13             use parent qw( Padre::Plugin );
14              
15             # Child modules we need to unload when disabled
16             use constant CHILDREN => qw{
17             Padre::Plugin::SpellCheck
18             Padre::Plugin::SpellCheck::Checker
19             Padre::Plugin::SpellCheck::FBP::Checker
20             Padre::Plugin::SpellCheck::Engine
21             Padre::Plugin::SpellCheck::Preferences
22             Padre::Plugin::SpellCheck::FBP::Preferences
23             Text::Aspell
24             Text::Hunspell
25             };
26              
27             #######
28             # Define Plugin Name Spell Checker
29             #######
30             sub plugin_name {
31             return Wx::gettext('Spell Checker');
32             }
33              
34             #######
35             # Define Padre Interfaces required
36             #######
37             sub padre_interfaces {
38             return (
39             'Padre::Plugin' => '0.96',
40             'Padre::Unload' => '0.96',
41              
42             # used by my sub packages
43             'Padre::Locale' => '0.96',
44             'Padre::Logger' => '0.96',
45             'Padre::Wx' => '0.96',
46             'Padre::Wx::Role::Main' => '0.96',
47             'Padre::Util' => '0.97',
48             );
49             }
50              
51             #######
52             # plugin menu
53             #######
54             sub menu_plugins {
55             my $self = shift;
56             my $main = $self->main;
57              
58             # Create a manual menu item
59             my $menu_item = Wx::MenuItem->new( undef, -1, $self->plugin_name . "...\tF7", );
60             Wx::Event::EVT_MENU(
61             $main,
62             $menu_item,
63             sub {
64             $self->spell_check;
65             },
66             );
67              
68             return $menu_item;
69             }
70              
71             #########
72             # We need plugin_enable
73             # as we have an external dependency
74             #########
75             sub plugin_enable {
76             my $self = shift;
77             my $local_dictionary_bin_exists = 0;
78              
79             # Tests for externals used by Preference's
80             try {
81             if ( require Text::Aspell ) {
82             $local_dictionary_bin_exists = 1;
83             }
84             };
85             try {
86             if ( File::Which::which('hunspell') ) {
87             $local_dictionary_bin_exists = 1;
88             }
89             };
90              
91             #Set/ReSet Config data
92             if ($local_dictionary_bin_exists) {
93             $self->_config;
94             }
95              
96             return $local_dictionary_bin_exists;
97             }
98              
99             #######
100             # Composed Method _config
101             # called on enable in plugin manager, bit like run/setup for a Plugin
102             #######
103             sub _config {
104             my $self = shift;
105             my $config = $self->config_read;
106              
107             ###
108             # Info P-P-SpellCheck < 1.21
109             # $config->{dictionary} = iso
110             #
111             # Info P-P-SpellCheck = 1.22
112             # - $config->{dictionary} = iso
113             # + $config->{Aspell} = en_GB
114             # + $config->{Hunspell} = en_AU
115             # + $config->{Version} = $VERSION
116             #
117             # Info P-P-SpellCheck >= 1.23
118             # + $config->{Engine} = 'Aspell'
119             ###
120              
121             try {
122             if ( $config->{Version} >= 1.23 ) {
123             return;
124             }
125             };
126              
127             try {
128             if ( $config->{Version} < 1.23 ) {
129              
130             $config->{Version} = $VERSION;
131             $config->{Engine} = 'Aspell';
132             $self->config_write($config);
133             return;
134             }
135             };
136              
137             try {
138             if ( $config->{dictionary} ) {
139             my $tmp_iso = $config->{dictionary};
140             $self->config_write( {} );
141             $config = $self->config_read;
142             $config->{Aspell} = $tmp_iso;
143             $config->{Hunspell} = $tmp_iso;
144             $config->{Version} = $VERSION;
145             $config->{Engine} = 'Aspell';
146             $self->config_write($config);
147             return;
148             }
149             }
150             catch {
151             $self->config_write( {} );
152             $config->{Aspell} = 'en_GB';
153             $config->{Hunspell} = 'en_GB';
154             $config->{Version} = $VERSION;
155             $config->{Engine} = 'Aspell';
156             $self->config_write($config);
157             return;
158             };
159              
160             return;
161             }
162              
163             ########
164             # plugin_disable
165             ########
166             sub plugin_disable {
167             my $self = shift;
168              
169             # Close the dialog if it is hanging around
170             $self->clean_dialog;
171              
172             # Unload all our child classes
173             for my $package (CHILDREN) {
174             require Padre::Unload;
175             Padre::Unload->unload($package);
176             }
177              
178             $self->SUPER::plugin_disable(@_);
179              
180             return 1;
181             }
182              
183             ########
184             # Composed Method clean_dialog
185             ########
186             sub clean_dialog {
187             my $self = shift;
188              
189             # Close the main dialog if it is hanging around
190             if ( $self->{dialog} ) {
191             $self->{dialog}->Hide;
192             $self->{dialog}->Destroy;
193             delete $self->{dialog};
194             }
195              
196             return 1;
197             }
198              
199              
200             #######
201             # plugin_preferences
202             #######
203             sub plugin_preferences {
204             my $self = shift;
205             my $main = $self->main;
206              
207             # Clean up any previous existing dialog
208             $self->clean_dialog;
209              
210             try {
211             require Padre::Plugin::SpellCheck::Preferences;
212             $self->{dialog} = Padre::Plugin::SpellCheck::Preferences->new($main);
213             $self->{dialog}->ShowModal;
214             }
215             catch {
216             $self->main->error( sprintf Wx::gettext('Error: %s'), $_ );
217             };
218              
219             return;
220             }
221              
222             #######
223             # spell_check
224             #######
225             sub spell_check {
226             my $self = shift;
227             my $main = $self->main;
228              
229             # Clean up any previous existing dialog
230             $self->clean_dialog;
231              
232             try {
233             require Padre::Plugin::SpellCheck::Checker;
234             $self->{dialog} = Padre::Plugin::SpellCheck::Checker->new($main);
235             $self->{dialog}->Show;
236             }
237             catch {
238             $self->main->error( sprintf Wx::gettext('Error: %s'), $_ );
239             };
240              
241             return;
242             }
243              
244             #######
245             # Add icon to Plugin
246             #######
247             sub plugin_icon {
248             my $class = shift;
249             my $share = $class->plugin_directory_share or return;
250             my $file = File::Spec->catfile( $share, 'icons', '16x16', 'spellcheck.png' );
251             return unless -f $file;
252             return unless -r $file;
253             return Wx::Bitmap->new( $file, Wx::wxBITMAP_TYPE_PNG );
254             }
255              
256             #######
257             # Add SpellCheck Preferences to Context Menu
258             #######
259             sub event_on_context_menu {
260             my ( $self, $document, $editor, $menu, $event ) = @_;
261              
262             #Test for valid file type
263             return if not $document->filename;
264              
265             $menu->AppendSeparator;
266              
267             my $item = $menu->Append( -1, Wx::gettext('SpellCheck Preferences...') );
268             Wx::Event::EVT_MENU(
269             $self->main,
270             $item,
271             sub { $self->plugin_preferences },
272             );
273              
274             return;
275             }
276              
277              
278             1;
279              
280             __END__