DBBrowser/Options.pm untested

filter: Statementstmt 0.0% 0 / 213 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 343 CC 38 SCAR 57.1
CC
38
Cov
0%
config_groups
650.0
set_options
110.0
__config_plugins
30.0
CRAP 302.3
1 package # hide from PAUSE
2 App::DBBrowser::Options;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Encode qw( decode );
9 use File::Spec::Functions qw( catfile );
10 use FindBin qw( $RealBin $RealScript );
11 #use Pod::Usage qw( pod2usage ); # required
12  
13 use Encode::Locale qw();
14  
15 use Term::Choose qw();
16 use Term::Choose::Util qw();
17  
18 use App::DBBrowser::Auxil;
19 use App::DBBrowser::Options::Defaults;
20 use App::DBBrowser::Options::Menus;
21 use App::DBBrowser::Options::ReadWrite;
22  
23  
24 sub new {
25 my ( $class, $info, $options ) = @_;
26 bless {
27 i => $info,
28 o => $options
29 }, $class;
30 }
31  
32 sub __plugins {
33 my ( $sf ) = @_;
34 my $op_mn = App::DBBrowser::Options::Menus->new( $sf->{i}, $sf->{o} );
35 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
36 my $groups = $op_mn->groups( undef, 1 );
37 $sf->config_groups( $groups );
38 #$sf->config_groups( $groups, undef, 1 );
39 $op_rw->read_config_file();
40 }
41  
42  
43 sub __config_global {
44 my ( $sf ) = @_;
45 my $op_mn = App::DBBrowser::Options::Menus->new( $sf->{i}, $sf->{o} );
46 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
47 my $groups = $op_mn->groups();
48 $sf->config_groups( $groups );
49 $op_rw->read_config_file();
50 }
51  
52  
53 sub __config_plugins {
54 my ( $sf ) = @_;
55 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
56 my $op_mn = App::DBBrowser::Options::Menus->new( $sf->{i}, $sf->{o} );
57 my $chosen_plugins = $sf->{o}{G}{plugins};
58 my $config_old_idx = 0;
59  
60 CONFIG_PLUGIN: while ( 1 ) {
61 if ( @$chosen_plugins == 1 ) {
62 my $plugin = $chosen_plugins->[0];
63 my $groups = $op_mn->groups( $plugin );
64 $sf->config_groups( $groups, $plugin );
65 last CONFIG_PLUGIN;
66 }
67 else {
68 my @pre = ( undef );
69 my $menu = [ @pre, map { '- ' . $_ } @$chosen_plugins ];
70 my $prompt = 'Configure Plugins';
71 # Choose
72 my $config_idx = $tc->choose(
73 $menu,
74 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $config_old_idx, undef => $sf->{i}{_back} }
75 );
76 if ( ! $config_idx ) {
77 return;
78 }
79 if ( $sf->{o}{G}{menu_memory} ) {
80 if ( $config_old_idx == $config_idx && ! $ENV{TC_RESET_AUTO_UP} ) {
81 $config_old_idx = 0;
82 next CONFIG_PLUGIN;
83 }
84 $config_old_idx = $config_idx;
85 }
86 my $plugin = $menu->[$config_idx] =~ s/^-\s//r;
87 my $groups = $op_mn->groups( $plugin );
88 $sf->config_groups( $groups, $plugin );
89 }
90 }
91 }
92  
93  
94 sub set_options {
95 my ( $sf ) = @_;
96 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
97 $sf->{o} = $op_rw->read_config_file();
98 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
99 my $plugins = '- Plugins';
100 my $config_plugins = '- Options';
101 my $global_settings = '- Other';
102 my $app_info = '- App info';
103 my $help = '- Help';
104 my $main_old_idx = 0;
105  
106 OPTION: while( 1 ) {
107 my @pre = ( undef, $sf->{i}{_continue} );
108 my $menu = [ @pre, $plugins, $config_plugins, $global_settings, $app_info, $help ];
109 # Choose
110 my $main_idx = $tc->choose(
111 $menu,
112 { %{$sf->{i}{lyt_v}}, index => 1, default => $main_old_idx, undef => $sf->{i}{_quit} }
113 );
114 if ( ! $main_idx ) {
115 exit();
116 }
117 if ( $sf->{o}{G}{menu_memory} ) {
118 if ( $main_old_idx == $main_idx && ! $ENV{TC_RESET_AUTO_UP} ) {
119 $main_old_idx = 0;
120 next OPTION;
121 }
122 $main_old_idx = $main_idx;
123 }
124 if ( $menu->[$main_idx] eq $sf->{i}{_continue} ) {
125 return;
126 }
127 elsif ( $menu->[$main_idx] eq $plugins ) {
128 $sf->__plugins();
129 }
130 elsif ( $menu->[$main_idx] eq $global_settings ) {
131 $sf->__config_global();
132 }
133 elsif ( $menu->[$main_idx] eq $config_plugins ) {
134 $sf->__config_plugins;
135 }
136 elsif ( $menu->[$main_idx] eq $app_info ) {
137 $sf->__display_info();
138 }
139 elsif ( $menu->[$main_idx] eq $help ) {
140 require Pod::Usage; # ctrl-c
141 Pod::Usage::pod2usage( { -exitval => 'NOEXIT', -verbose => 2 } );
142 }
143 }
144 }
145  
146  
147 sub config_groups {
148 my ( $sf, $groups, $plugin ) = @_;
149 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
150 my $op_mn = App::DBBrowser::Options::Menus->new( $sf->{i}, $sf->{o} );
151 my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} );
152 my $driver = '';
153 if ( $plugin ) {
154 my $plugin_full_name = "App::DBBrowser::DB::$plugin";
155 eval "require $plugin_full_name" or die $@;
156 my $plugin = $plugin_full_name->new( $sf->{i}, $sf->{o} );
157 $driver = $plugin->get_db_driver();
158 }
159 my $lo = $op_rw->read_config_file( $driver, $plugin );
160 my $prompt = '';
161 my $info;
162 my @pre = ( undef );
163 if ( $plugin ) {
164 $info = 'Configure ' . $plugin;
165 $prompt = $info;
166 }
167 else {
168 #$prompt = 'Your choice:';
169 }
170 my $grp_old_idx = 0;
171  
172 GROUP: while( 1 ) {
173 my ( $group, $group_prompt );
174 if ( @$groups == 1 ) {
175 $group = $groups->[0]{name};
176 $group_prompt = $groups->[0]{text};
177 }
178 else {
179 my $menu = [ @pre, map( $_->{text}, @$groups ) ];
180 # Choose
181 my $grp_idx = $tc->choose(
182 $menu,
183 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $grp_old_idx, undef => ' <=' }
184 );
185 if ( ! defined $grp_idx || ! defined $menu->[$grp_idx] ) {
186 $op_rw->write_config_file( $lo, $plugin );
187 return;
188 }
189 if ( $sf->{o}{G}{menu_memory} ) {
190 if ( $grp_old_idx == $grp_idx && ! $ENV{TC_RESET_AUTO_UP} ) {
191 $grp_old_idx = 0;
192 next GROUP;
193 }
194 $grp_old_idx = $grp_idx;
195 }
196 $group = $groups->[$grp_idx-@pre]{name};
197 $group_prompt = $groups->[$grp_idx-@pre]{text};
198 };
199 if ( length $group_prompt ) {
200 $group_prompt = $group_prompt =~ s/^- //r . ':';
201 }
202 my $sub_groups = $op_mn->sub_groups( $group, $driver );
203 my $sub_group_old_idx = 0;
204  
205 OPTION: while ( 1 ) {
206 my ( $section, $sub_group );
207 if ( @$sub_groups == 1 ) {
208 $section = $sub_groups->[0]{section};
209 $sub_group = $sub_groups->[0]{name};
210 }
211 else {
212 my @pre = ( undef );
213 my $menu = [ @pre, map( $_->{text}, @$sub_groups ) ];
214 # Choose
215 my $sub_group_idx = $tc->choose(
216 $menu,
217 { %{$sf->{i}{lyt_v}}, info => $info, prompt => $group_prompt,
218 index => 1, default => $sub_group_old_idx, undef => ' <=' } ##
219 );
220 if ( ! $sub_group_idx ) {
221 if ( @$groups == 1 ) {
222 $op_rw->write_config_file( $lo, $plugin );
223 return;
224 }
225 next GROUP;
226 }
227 if ( $sf->{o}{G}{menu_memory} ) {
228 if ( $sub_group_old_idx == $sub_group_idx && ! $ENV{TC_RESET_AUTO_UP} ) {
229 $sub_group_old_idx = 0;
230 next OPTION;
231 }
232 $sub_group_old_idx = $sub_group_idx;
233 }
234 $section = $sub_groups->[$sub_group_idx-@pre]{section};
235 $sub_group = $sub_groups->[$sub_group_idx-@pre]{name};
236 }
237 if ( $group eq 'group_connect' ) {
238 $op_mn->group_connect( $info, $lo, $section, $sub_group, $driver );
239 }
240 elsif ( $group eq 'group_extensions' ) {
241 $op_mn->group_extensions( $info, $lo, $section, $sub_group );
242 }
243 elsif ( $group eq 'group_sql_settings' ) {
244 $op_mn->group_sql_settings( $info, $lo, $section, $sub_group, $driver );
245 }
246 elsif ( $group eq 'group_create_table' ) {
247 $op_mn->group_create_table( $info, $lo, $section, $sub_group );
248 }
249 elsif ( $group eq 'group_output' ) {
250 $op_mn->group_output( $info, $lo, $section, $sub_group );
251 }
252 elsif ( $group eq 'group_import' ) {
253 $op_mn->group_import( $info, $lo, $section, $sub_group );
254 }
255 elsif ( $group eq 'group_export' ) {
256 $op_mn->group_export( $info, $lo, $section, $sub_group );
257 }
258 elsif ( $group eq 'group_misc' ) {
259 $op_mn->group_misc( $info, $lo, $section, $sub_group, $driver );
260 }
261 elsif ( $group eq 'group_global' ) {
262 $op_mn->group_global( $info, $lo, $section, $sub_group );
263 }
264 elsif ( $group eq 'group_select_plugins' ) {
265 $op_mn->group_select_plugins( $info, $lo, $section, $sub_group );
266 }
267 else {
268 die "Unknown group $group";
269 }
270 if ( @$sub_groups == 1 ) {
271 if ( @$groups == 1 ) {
272 $op_rw->write_config_file( $lo, $plugin );
273 return;
274 }
275 else {
276 next GROUP;
277 }
278 }
279 }
280 }
281 }
282  
283  
284 sub __display_info {
285 my ( $sf ) = @_;
286 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
287 my $app_dir = $sf->{i}{app_dir};
288 eval { $app_dir = decode( 'locale', $app_dir ) };
289 my $info = 'db-browser' . "\n\n";
290 $info .= 'Version: ' . $main::VERSION . "\n\n";
291 $info .= 'Path: ' . catfile( $RealBin, $RealScript ) . "\n\n";
292 $info .= 'App-Dir: ' . $app_dir . "\n";
293 $tc->choose( [ ' << ' ], { prompt => $info, color => 1, margin => [ 1, 1, 1, 1 ] } );
294 }
295  
296  
297  
298  
299  
300 1;
301  
302  
303 __END__