SpellCheck/Preferences.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« SpellCheck/FBP/Preferences.pm ↑ Summary
1 package Padre::Plugin::SpellCheck::Preferences;
2  
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Padre::Logger;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Padre::Util ();
10 use Padre::Locale ();
11 use Padre::Unload ();
12 use Padre::Plugin::SpellCheck::FBP::Preferences ();
13  
14 our $VERSION = '1.33';
15 use parent qw(
16 Padre::Plugin::SpellCheck::FBP::Preferences
17 Padre::Plugin
18 );
19  
20 #######
21 # Method new
22 #######
23 sub new {
24 my $class = shift;
25 my $main = shift;
26  
27 # Create the dialogue
28 my $self = $class->SUPER::new($main);
29  
30 # define where to display main dialogue
31 $self->CenterOnParent;
32 $self->SetTitle( sprintf Wx::gettext('Spell-Checker-Preferences v%s'), $VERSION );
33 $self->_set_up;
34  
35 return $self;
36 }
37  
38 #######
39 # Method _set_up
40 #######
41 sub _set_up {
42 my $self = shift;
43  
44 # set preferred dictionary from config
45 try {
46 $self->{dictionary} = $self->config_read->{Engine};
47 }
48 catch {
49 $self->{dictionary} = 'Aspell';
50 };
51  
52 if ( $self->{dictionary} eq 'Aspell' ) {
53  
54 # use Aspell as default, as the aspell engine works
55 $self->chosen_dictionary->SetSelection(0);
56 $self->_local_aspell_dictionaries;
57 } else {
58 $self->chosen_dictionary->SetSelection(1);
59 $self->_local_hunspell_dictionaries;
60 }
61  
62 # update dialogue with locally install dictionaries;
63 $self->_display_dictionaries;
64  
65 return;
66 }
67  
68 #######
69 # Method _local_aspell_dictionaries
70 #######
71 sub _local_aspell_dictionaries {
72 my $self = shift;
73  
74 my @local_dictionaries_names = ();
75  
76 try {
77 require Text::Aspell;
78 my $speller = Text::Aspell->new;
79  
80 my @local_dictionaries = grep { $_ =~ /^\w+$/ } map { $_->{name} } $speller->dictionary_info;
81 $self->{local_dictionaries} = \@local_dictionaries;
82 TRACE("Aspell locally installed dictionaries found = @local_dictionaries") if DEBUG;
83 TRACE("Aspell iso to dictionary names = $self->{dictionary_names}") if DEBUG;
84  
85 for (@local_dictionaries) {
86 push @local_dictionaries_names, $self->padre_locale_label($_);
87 $self->{dictionary_names}{$_} = $self->padre_locale_label($_);
88 }
89  
90 @local_dictionaries_names = sort @local_dictionaries_names;
91 $self->{local_dictionaries_names} = \@local_dictionaries_names;
92  
93 TRACE("Aspell local dictionaries names = $self->{local_dictionaries_names}") if DEBUG;
94 }
95 catch {
96 $self->{local_dictionaries_names} = \@local_dictionaries_names;
97 $self->main->info( Wx::gettext('Text::Aspell is not installed') );
98 };
99 return;
100 }
101  
102  
103 #######
104 # Method _local_aspell_dictionaries
105 #######
106 sub _local_hunspell_dictionaries {
107 my $self = shift;
108  
109 my @local_dictionaries_names;
110 my @local_dictionaries;
111  
112 # if ( require Text::Hunspell ) {
113 try {
114 require Text::Hunspell;
115 require Padre::Util;
116  
117 my $speller = Padre::Util::run_in_directory_two( cmd => 'hunspell -D </dev/null', option => '0' );
118 TRACE("hunspell speller = $speller") if DEBUG;
119  
120 #TODO this is yuck must do better
121 my @speller_raw = grep { $_ =~ /\w{2}_\w{2}$/m } split /\n/, $speller->{error};
122 my %temp_speller;
123 foreach (@speller_raw) {
124 if ( $_ !~ m/hyph/ ) {
125 m/(\w{2}_\w{2})$/;
126 my $tmp = $1;
127 $temp_speller{$tmp}++;
128 }
129 }
130  
131 while ( my ( $key, $value ) = each %temp_speller ) {
132 push @local_dictionaries, $key;
133 }
134  
135 $self->{local_dictionaries} = \@local_dictionaries;
136 TRACE("Hunspell locally installed dictionaries found = $self->{local_dictionaries}") if DEBUG;
137 TRACE("Hunspell iso to dictionary names = $self->{dictionary_names}") if DEBUG;
138  
139 for (@local_dictionaries) {
140 push( @local_dictionaries_names, $self->padre_locale_label($_) );
141 $self->{dictionary_names}{$_} = $self->padre_locale_label($_);
142 }
143  
144 @local_dictionaries_names = sort @local_dictionaries_names;
145 $self->{local_dictionaries_names} = \@local_dictionaries_names;
146 TRACE("Hunspell local dictionaries names = $self->{local_dictionaries_names}") if DEBUG;
147 return;
148  
149 }
150 catch {
151 $self->{local_dictionaries_names} = \@local_dictionaries_names;
152 $self->main->info( Wx::gettext('Text::Hunspell is not installed') );
153 return;
154 };
155 return;
156 }
157  
158 #######
159 # Method _display_dictionaries
160 #######
161 sub _display_dictionaries {
162 my $self = shift;
163  
164 # my $main = $self->main;
165  
166 my $prefered_dictionary;
167 try {
168 $prefered_dictionary = $self->config_read->{ $self->{dictionary} };
169 }
170 catch {
171 $prefered_dictionary = 'Aspell';
172 };
173  
174 TRACE("iso prefered_dictionary = $prefered_dictionary ") if DEBUG;
175  
176 # set local_dictionaries_index to zero in case prefered_dictionary not found
177 my $local_dictionaries_index = 0;
178 require Padre::Locale;
179 for ( 0 .. $#{ $self->{local_dictionaries_names} } ) {
180 if ( $self->{local_dictionaries_names}->[$_] eq $self->padre_locale_label($prefered_dictionary) ) {
181 $local_dictionaries_index = $_;
182 }
183 }
184  
185 TRACE("local_dictionaries_index = $local_dictionaries_index ") if DEBUG;
186  
187 $self->language->Clear;
188  
189 # load local_dictionaries_names
190 $self->language->Append( $self->{local_dictionaries_names} );
191  
192 # highlight prefered_dictionary
193 $self->language->SetSelection($local_dictionaries_index);
194  
195 return;
196 }
197  
198 #######
199 # event handler _on_button_ok_clicked
200 #######
201 sub _on_button_save_clicked {
202 my $self = shift;
203  
204 my $select_dictionary_name = $self->{local_dictionaries_names}->[ $self->language->GetSelection() ];
205 TRACE("selected dictionary name = $select_dictionary_name ") if DEBUG;
206  
207 my $select_dictionary_iso = 0;
208  
209 # require Padre::Locale;
210 for my $iso ( keys %{ $self->{dictionary_names} } ) {
211 if ( $self->padre_locale_label($iso) eq $select_dictionary_name ) {
212 $select_dictionary_iso = $iso;
213 }
214 }
215 TRACE("selected dictionary iso = $select_dictionary_iso ") if DEBUG;
216  
217 # save config info
218 my $config = $self->config_read;
219 $config->{ $self->{dictionary} } = $select_dictionary_iso;
220 $config->{Engine} = $self->{dictionary};
221 $self->config_write($config);
222  
223 $self->Hide;
224 return;
225 }
226  
227 #######
228 # event handler on_dictionary_chosen
229 #######
230 sub on_dictionary_chosen {
231 my $self = shift;
232  
233 if ( $self->chosen_dictionary->GetSelection() == 0 ) {
234 $self->{dictionary} = 'Aspell';
235 TRACE("Aspell chosen") if DEBUG;
236 $self->_local_aspell_dictionaries;
237 } else {
238 $self->{dictionary} = 'Hunspell';
239 TRACE("Hunspell chosen") if DEBUG;
240 $self->_local_hunspell_dictionaries;
241 }
242 $self->_display_dictionaries;
243  
244 return;
245 }
246  
247 #######
248 # Composed Method padre_local_label
249 # aspell to padre local label
250 #######
251 sub padre_locale_label {
252 my $self = shift;
253 my $local_dictionary = shift;
254  
255 my $lc_local_dictionary = lc( $local_dictionary ? $local_dictionary : 'en_GB' );
256 $lc_local_dictionary =~ s/_/-/;
257 require Padre::Locale;
258 my $label = Padre::Locale::label($lc_local_dictionary);
259  
260 return $label;
261 }
262  
263 1;
264  
265 __END__
« SpellCheck/FBP/Preferences.pm ↑ Summary