Codit/SessionManager.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 156 CC 14 SCAR 24.0
CC
14
Cov
0%
Delete
20.0
Duplicate
20.0
Rename
20.0
CRAP 11.0
1 package App::Codit::SessionManager;
2  
3 =head1 NAME
4  
5 App::Codit::SessionManager - Session manager used by the Sessions plugin
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use Carp;
12 require Tk::YAMessage;
13 use File::Copy;
14 use vars qw($VERSION);
15 $VERSION = '0.19';
16  
17 use base qw(Tk::Derived Tk::YADialog);
18 Construct Tk::Widget 'SessionManager';
19  
20  
21 sub Populate {
22 my ($self,$args) = @_;
23
24 my $plug = delete $args->{'-plugin'};
25 carp 'You must specify the -plugin option' unless defined $plug;
26  
27 $self->SUPER::Populate($args);
28  
29 my @padding = (-padx => 4, -pady => 4);
30  
31 my $art = $plug->extGet('Art');
32
33 my @sessions = ();
34 $self->{SESSIONS} = \@sessions;
35 $self->{PLUGIN} = $plug;
36 my $lb = $self->Scrolled('Listbox',
37 -scrollbars => 'osoe',
38 -listvariable => \@sessions,
39 -selectmode => 'single',
40 )->pack(-side => 'left', @padding, -fill => 'y',);
41 $self->Advertise('Listbox', $lb);
42  
43 my $bf = $self->Frame(
44 -relief => 'groove',
45 -borderwidth => 2,
46 )->pack(-side => 'left', -fill => 'y', @padding);
47  
48 $bf->Button(
49 -image => $art->createCompound(
50 -text => 'Open',
51 -image => $art->getIcon('document-open', 22),
52 ),
53 -anchor => 'w',
54 -command => ['Open', $self],
55 )->pack(@padding, -fill => 'x');
56 $bf->Button(
57 -image => $art->createCompound(
58 -text => 'New session',
59 -image => $art->getIcon('document-new', 22),
60 ),
61 -anchor => 'w',
62 -command => ['NewSession', $self],
63 )->pack(@padding, -fill => 'x');
64 $bf->Label(-text => ' ')->pack(@padding);
65 $bf->Button(
66 -image => $art->createCompound(
67 -text => 'Duplicate',
68 -image => $art->createEmptyImage(22),
69 ),
70 -anchor => 'w',
71 -command => ['Duplicate', $self],
72 )->pack(@padding, -fill => 'x');
73 $bf->Button(
74 -image => $art->createCompound(
75 -text => 'Rename',
76 -image => $art->createEmptyImage(22),
77 ),
78 -anchor => 'w',
79 -command => ['Rename', $self],
80 )->pack(@padding, -fill => 'x');
81 $bf->Label(-text => ' ')->pack(@padding);
82 $bf->Button(
83 -image => $art->createCompound(
84 -text => 'Delete',
85 -image => $art->getIcon('edit-delete', 22),
86 ),
87 -anchor => 'w',
88 -command => ['Delete', $self],
89 )->pack(@padding, -fill => 'x');
90  
91 $self->ConfigSpecs(
92 -background => ['SELF', 'DESCENDANTS'],
93 DEFAULT => [$self],
94 );
95 $self->Refresh;
96 }
97  
98 sub Delete {
99 my $self = shift;
100 my $sel = $self->GetSelected;
101 return unless defined $sel;
102 my $plug = $self->{PLUGIN};
103 return if $sel eq $plug->sessionCurrent;
104  
105 my $q = $self->YAMessage(
106 -title => 'Deleting session',
107 -image => $plug->getArt('dialog-warning', 32),
108 -buttons => [qw(Yes No)],
109 -text => "Deleting $sel.\nAre you sure?",
110 -defaultbutton => 'No',
111 );
112  
113 my $answer = $q->Show(-popover => $self);
114 if ($answer eq 'Yes') {
115 $plug->sessionDelete($sel);
116 $self->Refresh;
117 }
118 }
119  
120 sub Duplicate {
121 my $self = shift;
122 my ($sel) = $self->GetSelected;
123 return unless defined $sel;
124 my $name = $self->NameDialog("Enter duplicate name:");
125 my $plug = $self->{PLUGIN};
126 return unless defined $name;
127 return if $plug->sessionExists($name);
128 my $f = $plug->sessionFolder;
129 copy("$f/$sel", "$f/$name");
130 $self->Refresh;
131 }
132  
133 sub GetSelected {
134 my $self = shift;
135 my ($sel) = $self->Subwidget('Listbox')->curselection;
136 return unless defined $sel;
137 return $self->{SESSIONS}->[$sel]
138 }
139  
140 sub NameDialog {
141 my ($self, $text) = @_;
142 return $self->{PLUGIN}->popEntry('Session name', $text);
143 }
144  
145 sub NewSession {
146 my $self = shift;
147 $self->{PLUGIN}->sessionNew;
148 $self->Pressed('Close');
149 }
150  
151 sub Open {
152 my $self = shift;
153 my $sel = $self->GetSelected;
154 my $plug = $self->{PLUGIN};
155 return unless defined $sel;
156 return if $sel eq $plug->sessionCurrent;
157 $plug->sessionOpen($sel);
158 $self->Pressed('Close');
159 }
160  
161 sub Refresh {
162 my $self = shift;
163 my $sessions = $self->{SESSIONS};
164 while (@$sessions) { pop @$sessions }
165 my @list = $self->{PLUGIN}->sessionList;
166 for (@list) { push @$sessions, $_ }
167 }
168  
169 sub Rename {
170 my $self = shift;
171 my ($sel) = $self->GetSelected;
172 return unless defined $sel;
173 my $name = $self->NameDialog("Rename session to:");
174 return unless defined $name;
175 my $plug = $self->{PLUGIN};
176 return if $plug->sessionExists($name);
177 my $f = $plug->sessionFolder;
178 move("$f/$sel", "$f/$name");
179 $self->Refresh;
180 }
181  
182 =head1 LICENSE
183  
184 Same as Perl.
185  
186 =head1 AUTHOR
187  
188 Hans Jeuken (hanje at cpan dot org)
189  
190 =head1 TODO
191  
192 =over 4
193  
194 =back
195  
196 =head1 BUGS AND CAVEATS
197  
198 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
199  
200 =head1 SEE ALSO
201  
202 =over 4
203  
204 =back
205  
206 =cut
207  
208  
209 1;