Codit/Plugins/Sessions.pm untested

filter: Statementstmt 0.0% 0 / 260 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 406 CC 43 SCAR 35.5
CC
43
Cov
0%
sessionOpen
132.0
sessionSave
72.0
sessionValidateName
20.0
CRAP 34.9
1 package App::Codit::Plugins::Sessions;
2  
3 =head1 NAME
4  
5 App::Codit::Plugins::Sessions - plugin for App::Codit
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw( $VERSION );
12 $VERSION = '0.21';
13  
14 use base qw( Tk::AppWindow::BaseClasses::Plugin );
15  
16 use File::Path qw(make_path);
17 require App::Codit::SessionManager;
18  
19 my @saveoptions = (
20 '-contentindent',
21 '-contentposition',
22 '-contentsyntax',
23 '-contenttabs',
24 '-contentwrap',
25 '-showfolds',
26 '-shownumbers',
27 '-showstatus',
28 );
29  
30 =head1 DESCRIPTION
31  
32 Manage your sessions. Saves your named session on exit and reloads it on start.
33  
34 =head1 DETAILS
35  
36 The sessions plugin allows you to save a collection of documents as a session.
37 When re-opening the session the documents are loaded in the exact order as they
38 were when the session was closed. Also the syntax option, tab size, indent style,
39 bookmarks and insert cursor position are saved. The only thing that is lost is your
40 undo stack.
41  
42 The sessions plugin also saves the following from plugins if they are loaded:
43  
44 =over 4
45  
46 =item The working folder from the Console plugin
47  
48 =item The working folder from the FileBrowser plugin
49  
50 =item The project name of the currently loaded project in the Git plugin
51  
52 =back
53  
54 The session manager allows you to keep your sessions orderly.
55  
56 =head1 COMMANDS
57  
58 =over 4
59  
60 =item B<session_dialog>
61  
62 Pops the dialog for managing sessions.
63  
64 =item B<session_fill_menu>
65  
66 Called when the Session menu in the menu bar is opened.
67  
68 =item B<session_new>
69  
70 Closes current session and creates a new, unnamed, one.
71  
72 =item B<session_open>
73  
74 Takes a session name as parameter. Closes current session and opens the new one.
75  
76 =item B<session_save>
77  
78 Save current session. If the session was not saved before, a dialog will pop to enter a name.
79  
80 =item B<session_save_as>
81  
82 Save current session under a new name.
83  
84 =back
85  
86 =cut
87  
88 sub new {
89 my $class = shift;
90 my $self = $class->SUPER::new(@_, 'ConfigFolder', 'MenuBar');
91 return undef unless defined $self;
92  
93 $self->sessionFolder;
94  
95 $self->{CURRENT} = '';
96 $self->{DATA} = {};
97  
98 $self->cmdConfig(
99 session_dialog => ['sessionDialog', $self],
100 session_fill_menu => ['sessionFillMenu', $self],
101 session_new => ['sessionNew', $self],
102 session_open => ['sessionOpen', $self],
103 session_save => ['sessionSave', $self],
104 session_save_as => ['sessionSaveAs', $self],
105 );
106 $self->cmdHookAfter('set_title', 'AdjustTitle', $self);
107 return $self;
108 }
109  
110 sub AdjustTitle {
111 my $self = shift;
112 my $mdi = $self->mdi;
113 my $doc = $mdi->docSelected;
114 my $name = $self->configGet('-appname');
115 if (defined $doc) {
116 my $label = $mdi->docTitle($doc);
117 $name = "$label - $name";
118 }
119 my $current = $self->sessionCurrent;
120 if ($current ne '') {
121 $name = "$current: $name" ;
122 }
123 $self->configPut(-title => $name);
124 }
125  
126 sub bookmarksInitialize {
127 my $self = shift;
128 my $bookmarks = $self->extGet('Plugins')->plugGet('Bookmarks');
129 $self->after(200, ['Initialize', $bookmarks]) if defined $bookmarks;
130 }
131  
132 sub browserDir {
133 my ($self, $dir) = @_;
134 my $b = $self->extGet('Plugins')->plugGet('FileBrowser');
135 return unless defined $b;
136 my $browser = $b->browser;
137 $browser->load($dir) if defined $dir;
138 return $browser->folder;
139 }
140  
141 sub CanQuit {
142 my $self = shift;
143 $self->sessionSave unless $self->sessionCurrent eq '';
144 $self->mdi->docForceClose(1);
145 return 1
146 }
147  
148 sub consoleDir {
149 my ($self, $dir) = @_;
150 my $console = $self->extGet('Plugins')->plugGet('Console');
151 return unless defined $console;
152 $console->dirSet($dir) if defined $dir;
153 return $console->dirGet;
154 }
155  
156 sub MenuItems {
157 my $self = shift;
158 return (
159 # type menupath label cmd icon
160 [ 'menu', undef, '~Session'],
161 [ 'menu', 'Session::', '~Open session', 'session_fill_menu'],
162 [ 'menu_normal', 'Session::', '~New session', 'session_new', 'document-new'],
163 [ 'menu_separator', 'Session::', 'se1'],
164 [ 'menu_normal', 'Session::', '~Save session', 'session_save', 'document-save'],
165 [ 'menu_normal', 'Session::', '~Save session as', 'session_save_as', 'document-save'],
166 [ 'menu_separator', 'Session::', 'se2'],
167 [ 'menu_normal', 'Session::', '~Manage sessions', 'session_dialog', 'configure'],
168 )
169 }
170  
171 sub projectName {
172 my ($self, $proj) = @_;
173 my $git = $self->extGet('Plugins')->plugGet('Git');
174 return '' unless defined $git;
175 $git->projectSelect($proj) if defined $proj;
176 return $git->projectCurrent;
177 }
178  
179 sub sessionClose {
180 my $self = shift;
181 my $session = $self->sessionCurrent;
182 my $mdi = $self->mdi;
183 if ($mdi->docConfirmSaveAll) {
184 $self->sessionSave unless $session eq '';
185 $self->sessionCurrent('');
186 my @list = $self->sessionDocList;
187  
188 my $fc = $mdi->docForceClose;
189 $mdi->docForceClose(1);
190 $mdi->silentMode(1);
191  
192 my $size = @list;
193 my $count = 0;
194 $self->progressAdd('multi_close', 'Close session', $size, \$count);
195 for (@list) {
196 $self->cmdExecute('doc_close', $_);
197 $count ++;
198 # $self->update;
199 $self->pause(20);
200 }
201 $self->progressRemove('multi_close');
202 $mdi->silentMode(0);
203 $mdi->docForceClose($fc);
204 $self->AdjustTitle;
205 return 1;
206 }
207 return 0
208 }
209  
210 sub sessionCurrent {
211 my $self = shift;
212 $self->{CURRENT} = shift if @_;
213 return $self->{CURRENT}
214 }
215  
216 sub sessionDelete {
217 my ($self, $name) = @_;
218 return if $name eq $self->sessionCurrent;
219 my $file = $self->sessionFolder . "/$name";
220 unlink $file if -e $file;
221 }
222  
223 sub sessionDialog {
224 my $self = shift;
225 my $d = $self->SessionManager(
226 -plugin => $self,
227 -popover => $self->toplevel,
228 );
229 $d->Show;
230 $d->destroy;
231 }
232  
233 sub sessionExists {
234 my ($self, $name) = @_;
235 return 1 if -e $self->sessionFolder . "/$name";
236 return 0
237 }
238  
239 sub sessionDocList {
240 my $self = shift;
241 my $interface = $self->mdi->Interface;
242 my $disp = $interface->{DISPLAYED};
243 my $undisp = $interface->{UNDISPLAYED};
244 return @$disp, @$undisp;
245 }
246  
247 sub sessionFillMenu {
248 my $self = shift;
249 my $mnu = $self->extGet('MenuBar');
250 my @list = $self->sessionList;
251 my $var = $self->sessionCurrent;
252 # print "current $var\n";
253 my ($menu, $index) = $mnu->FindMenuEntry('Session::Open session');
254 if (defined($menu)) {
255 my $submenu = $menu->entrycget($index, '-menu');
256 $submenu->delete(1, 'last');
257 for (@list) {
258 my $f = $_;
259 $submenu->add('radiobutton',
260 -variable => \$var,
261 -value => $f,
262 -label => $f,
263 -command => sub { $self->sessionOpen($f) }
264 );
265 }
266 }
267 }
268  
269 sub sessionFolder {
270 my $self = shift;
271 my $config = $self->extGet('ConfigFolder')->ConfigFolder . '/Sessions';
272 make_path($config) unless -e $config;
273 return $config
274 }
275  
276 sub sessionList {
277 my $self = shift;
278 my $dir = $self->sessionFolder;
279 if (opendir(my $dh, $dir)) {
280 my @list = ();
281 while (my $thing = readdir $dh) {
282 push @list, $thing unless $thing =~ /^\.+$/;
283 }
284 return sort @list
285 }
286 }
287  
288 sub sessionNew {
289 my $self = shift;
290 $self->sessionClose;
291 $self->mdi->docSelectFirst;
292 }
293  
294 sub sessionOpen {
295 my ($self, $name) = @_;
296  
297 my $file = "Sessions/$name";
298 my $cff = $self->extGet('ConfigFolder');
299  
300 return if $name eq $self->sessionCurrent;
301 return unless $cff->confExists($file);
302 return unless $self->sessionClose;
303  
304 my @list = $cff->loadSectionedList($file, 'cdt session');
305  
306 my $mdi = $self->mdi;
307 $mdi->silentMode(1);
308  
309 my $count = 0;
310 my $size = @list;
311 $self->progressAdd('multi_open', 'Open session', $size, \$count);
312 my $select;
313 my $workdir; #plugin Console
314 my $project; #plugin Git
315 my $browserdir; #plugin FileBrowser
316 for (@list) {
317 my ($file, $options) = @$_;
318 if ($file eq 'general') {
319 $select = $options->{'selected'};
320 $workdir = $options->{'workdir'};
321 $project = $options->{'project'};
322 $browserdir = $options->{'browserdir'};
323 $count ++
324 } else {
325 next unless -e $file;
326 if ($self->cmdExecute('doc_open', $file)) {
327 $mdi->deferredOptions($file, $options);
328 }
329 $count ++;
330 # $self->update;
331 $self->pause(20);
332 }
333 }
334 $self->progressRemove('multi_open');
335 $self->sessionCurrent($name);
336 $self->AdjustTitle;
337  
338 $mdi->silentMode(0);
339  
340 $mdi->interfaceCollapse;
341 if ((defined $select) and ($mdi->docExists($select))) {
342 $self->cmdExecute('doc_select',$select)
343 } else {
344 $mdi->docSelectFirst
345 }
346 $self->consoleDir($workdir) if defined $workdir;
347 $self->projectName($project) if defined $project;
348 $self->browserDir($browserdir) if defined $browserdir;
349 $self->bookmarksInitialize;
350 $self->update;
351 $self->StatusMessage("Session '$name' loaded");
352 }
353  
354 sub sessionSave {
355 my $self = shift;
356 # print "sessionSave\n";
357  
358 my $name = $self->sessionCurrent;
359 return $self->sessionSaveAs if $name eq '';
360  
361 my $file = "Sessions/$name";
362 my $cff = $self->extGet('ConfigFolder');
363 my $mdi = $self->mdi;
364  
365 #configuring general options
366 my @genopt = ( 'selected', $mdi->docSelected );
367 #workdir of plugin Console
368 my $workdir = $self->consoleDir;
369 push @genopt, 'workdir', $workdir if defined $workdir;
370 #workdir of plugin FileBrowser
371 my $browserdir = $self->browserDir;
372 push @genopt, 'browserdir', $browserdir if defined $browserdir;
373 #project name of plugin Git
374 my $project = $self->projectName;
375 push @genopt, 'project', $project if $project ne '';
376  
377 my @list = (['general', { @genopt }]);
378  
379 #getting all document names ordered as they are on the tab bar.
380 my @items = $self->sessionDocList;
381 for (@items) {
382 my $item = $_;
383 if ($mdi->deferredExists($item)) {
384 my $options = $mdi->deferredOptions($item);
385 my %h = %$options;
386 push @list, [$item, \%h]
387 } else {
388 next unless -e $item;
389 my $doc = $mdi->docGet($item);
390 my %h = ();
391 for (@saveoptions) { $h{$_} = $doc->cget($_) }
392  
393 #saving bookmarks
394 my $w = $doc->CWidg;
395 my (@l) = $w->bookmarkList;
396 if (@l) {
397 my $bm = '';
398 for (@l) {
399 $bm = "$bm$_ ";
400 }
401 $h{'bookmarks'} = $bm;
402 }
403  
404 push @list, [$item, \%h]
405 }
406 }
407 $cff->saveSectionedList($file, 'cdt session', @list)
408 }
409  
410 sub sessionSaveAs {
411 my $self = shift;
412 my $dialog = $self->YADialog(
413 -buttons => ['Ok', 'Cancel'],
414 );
415 $dialog->Label(
416 -text => 'Please enter a session name',
417 -justify => 'left',
418 )->pack(-fill => 'x', -padx => 3, -pady => 3);
419 my $text = '';
420 my $e = $dialog->Entry(
421 -textvariable => \$text,
422 )->pack(-fill => 'x', -padx => 3, -pady => 3);
423 $e->focus;
424 my $but = $dialog->show(-popover => $self->toplevel);
425 if (($but eq 'Ok') and ($text ne '')) {
426 $self->sessionCurrent($text);
427 $self->sessionSave;
428 $self->AdjustTitle;
429 }
430  
431 }
432  
433 sub sessionValidateName {
434 my ($self, $name) = @_;
435 return 0 if $name eq '';
436 return 0 if $name +~ /\//;
437 return 0 if $name +~ /\\/;
438 return 1
439 }
440  
441 sub Unload {
442 my $self = shift;
443 for (qw/
444 session_dialog
445 session_fill_menu
446 session_new
447 session_open
448 session_save
449 session_save_as
450 /) {
451 $self->cmdRemove($_);
452 }
453 $self->sessionCurrent('');
454 $self->cmdUnhookAfter('set_title', 'AdjustTitle', $self);
455 $self->AdjustTitle;
456 return $self->SUPER::Unload
457 }
458  
459 =head1 LICENSE
460  
461 Same as Perl.
462  
463 =head1 AUTHOR
464  
465 Hans Jeuken (hanje at cpan dot org)
466  
467 =head1 TODO
468  
469 =over 4
470  
471 =back
472  
473 =head1 BUGS AND CAVEATS
474  
475 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
476  
477 =head1 SEE ALSO
478  
479 =over 4
480  
481 =back
482  
483 =cut
484  
485  
486  
487 1;
488  
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500