dbMan/Extension/GuiCreateConnection.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 125 CC 10 SCAR 41.3
CC
10
Cov
0%
handle_action
110.0
IDENTIFICATION
2.0
preference
2.0
CRAP 62.0
1 package DBIx::dbMan::Extension::GuiCreateConnection;
2  
3 use strict;
4 use base 'DBIx::dbMan::Extension';
5 use Curses::UI;
6  
7  
8 our $VERSION = '0.01';
9  
10 1;
11  
12 sub IDENTIFICATION { return "000001-000090-000001"; }
13  
14 sub preference { return 800; }
15  
16 sub known_actions { return [ qw/GUI/ ]; }
17  
18 sub menu {
19 return { label => 'Connection', submenu => [
20 { label => 'Create...', preference => -5, action => { action => 'GUI',
21 operation => 'create_connection' } }
22 ] };
23 }
24  
25 sub load_ok {
26 my $obj = shift;
27  
28 return $obj->{-interface}->gui();
29 }
30  
31 sub handle_action {
32 my ($obj,%action) = @_;
33  
34 if ($action{action} eq 'GUI') {
35 if ($action{operation} eq 'create_connection') {
36 if ($obj->{-interface}->can('is_curses')
37 and $obj->{-interface}->is_curses()) {
38 $action{action} = 'NONE';
39  
40 my $ui = $obj->{-interface}->{ui};
41 my $dialog = $ui->add('dialog','Window',
42 -border => 1, -ipad => 1, -centered => 1,
43 -title => 'Create new connection',
44 -height => 18, -width => 50);
45  
46 $dialog->add('label1', 'Label', -text => 'Connection name',
47 -x => 0, -y => 0);
48 my $e_name = $dialog->add('e_name', 'TextEntry',
49 -x => 20, -y => 0, -sbborder => 1);
50  
51 $dialog->add('label2', 'Label', -text => 'Database driver',
52 -x => 0, -y => 2);
53 my @drivers = sort { uc $a <=> uc $b } $obj->{-dbi}->driverlist;
54 my $sel = 0;
55 for my $driver (qw/Oracle Pg mysql XBase CSV/) {
56 my $i = 0;
57 for (@drivers) {
58 last if $driver eq $_;
59 ++$i;
60 }
61 if ($i < @drivers) { $sel = $i; last; }
62 }
63 my $e_driver = $dialog->add('e_driver', 'Popupmenu',
64 -values => \@drivers,
65 -labels => { map { $_ => $_ } @drivers },
66 -selected => $sel,
67 -x => 20, -y => 2);
68
69 $dialog->add('label3', 'Label', -text => 'DSN',
70 -x => 0, -y => 4);
71 my $e_dsn = $dialog->add('e_dsn', 'TextEntry',
72 -x => 20, -y => 4, -sbborder => 1);
73
74 $dialog->add('label4', 'Label', -text => 'Login',
75 -x => 0, -y => 6);
76 my $e_login = $dialog->add('e_login', 'TextEntry',
77 -x => 20, -y => 6, -sbborder => 1);
78
79 $dialog->add('label5', 'Label', -text => 'Password',
80 -x => 0, -y => 8);
81 my $e_pass = $dialog->add('e_pass', 'PasswordEntry',
82 -x => 20, -y => 8, -sbborder => 1);
83  
84 my $e_save = $dialog->add('e_save', 'Checkbox',
85 -x => 0, -y => 10, -label => 'Make connection permanent',
86 -checked => 1);
87
88 my $e_open = $dialog->add('e_open', 'Checkbox',
89 -x => 0, -y => 11, -label => 'Autoopen connection',
90 -checked => 0);
91  
92 my $btns = $dialog->add('buttons', 'Buttonbox', -y => -1,
93 -buttonalignment => 'right', -buttons => [
94 { -label => '< Create >', -value => 1 },
95 { -label => '< Cancel >', -value => 0 } ]);
96 $btns->set_routine('press-button',
97 sub { shift->parent->loose_focus(); });
98  
99 $e_name->focus();
100  
101 while (1) {
102 $dialog->modalfocus();
103  
104 if ($btns->get()) {
105 my $name = $e_name->get();
106 my $driver = $e_driver->get();
107 my $dsn = $e_dsn->get();
108 my $login = $e_login->get();
109 my $pass = $e_pass->get();
110 my $save = $e_save->get();
111 my $open = $e_open->get();
112  
113 $name =~ s/\s+//g;
114 $dsn =~ s/\s+//g;
115 $login =~ s/\s+//g;
116  
117 if ($name) {
118 $action{action} = 'CONNECTION';
119 $action{operation} = 'create';
120 $action{what} = $name;
121 $action{driver} = $driver;
122 $action{dsn} = $dsn;
123 $action{login} = $login;
124 $action{password} = $pass;
125 $action{auto_login} = 'yes' if $open;
126 ++$action{permanent} if $save;
127 last;
128 }
129  
130 $e_dsn->text($dsn);
131 $e_name->text($name);
132 $e_login->text($login);
133 } else {
134 last;
135 }
136 }
137  
138 $ui->delete('dialog');
139 }
140 }
141 }
142  
143 $action{processed} = 1;
144 return %action;
145 }