Wrangler/Wx/Dialog/ListingToText.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 142 CC 13 SCAR 40.9
CC
13
Cov
0%
populate_display
110.0
collect_path
20.0
new
2.0
CRAP 59.6
1 package Wrangler::Wx::Dialog::ListingToText;
2  
3 use strict;
4 use warnings;
5  
6 use Wx qw/:id :misc :sizer :dialog :textctrl :filedialog wxTheClipboard/;
7 use Wx::Event qw(EVT_RADIOBUTTON EVT_BUTTON);
8 # use Wx::DND;
9  
10 use base 'Wx::Dialog';
11  
12 sub new {
13 my $class = shift;
14 my $parent = shift;
15 my $self = $class->SUPER::new( $parent, -1, "Export file-listing as text", wxDefaultPosition, [500,300],
16 wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER
17 );
18  
19 # hook-up access to $wrangler
20 $self->{wrangler} = $parent->{wrangler};
21  
22 ## element 1: radio buttons
23 my $btn_radio1 = new Wx::RadioButton($self, -1, 'filenames only');
24 my $btn_radio2 = new Wx::RadioButton($self, -1, 'prepend full path');
25 my $btn_radio3 = new Wx::RadioButton($self, -1, 'prepend relative dir-up');
26 my $radio_sizer = new Wx::BoxSizer(wxHORIZONTAL);
27 $radio_sizer->Add($btn_radio1);
28 $radio_sizer->Add($btn_radio2);
29 $radio_sizer->Add($btn_radio3);
30  
31 ## element 2: a text field for diplaying
32 $self->{text} = Wx::TextCtrl->new($self, -1, '', wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
33  
34 ## element 3: some buttons
35 my $btn_clipboard = new Wx::Button($self, -1, 'Copy to Clipboard');
36 my $btn_save = new Wx::Button($self, -1, 'Save as...');
37 my $btn_sizer = new Wx::BoxSizer(wxHORIZONTAL);
38 $btn_sizer->Add($btn_clipboard, 0, wxRIGHT, 15);
39 $btn_sizer->Add($btn_save, 0, wxRIGHT, 2);
40 $btn_sizer->Add(new Wx::Button($self, wxID_CANCEL, 'Cancel'), 0, wxRIGHT, 5);
41  
42 my $sizer = Wx::FlexGridSizer->new(3, 1, 0, 0); # rows,cols,vgap,hgap
43 $sizer->AddGrowableCol(0); # zerobased
44 $sizer->AddGrowableRow(1); # zerobased
45 $sizer->Add($radio_sizer, 0, wxALL|wxGROW, 5);
46 $sizer->Add($self->{text}, 0, wxALL|wxGROW, 5);
47 $sizer->Add($btn_sizer, 0, wxTOP|wxALIGN_RIGHT, 10);
48  
49 EVT_RADIOBUTTON($self, $btn_radio1, sub { $self->{mode} = 1; $self->populate_display(); });
50 EVT_RADIOBUTTON($self, $btn_radio2, sub { $self->{mode} = 2; $self->populate_display(); });
51 EVT_RADIOBUTTON($self, $btn_radio3, sub { $self->{mode} = 3; $self->populate_display(); });
52 EVT_BUTTON($self, $btn_save, \&collect_path );
53 EVT_BUTTON($self, $btn_clipboard, sub {
54 Wrangler::debug("ListingToText: copy to clipboard");
55  
56 my $tdo = Wx::TextDataObject->new();
57 $tdo->SetText( $self->{text}->GetValue() );
58  
59 wxTheClipboard->Open();
60 wxTheClipboard->SetData( $tdo );
61 wxTheClipboard->Close();
62 });
63  
64 $self->populate_display();
65  
66 $self->SetSizer($sizer);
67 $self->Layout();
68 $self->Centre();
69 $self->Show;
70  
71 return $self;
72 }
73  
74 sub populate_display {
75 my $self = shift;
76  
77 my $richlist;
78 if($self->{wrangler}->{main}->{filebrowser}->{current_selection}){
79 $richlist = $self->{wrangler}->{main}->{filebrowser}->{current_selection};
80 }else{
81 $richlist = $self->{wrangler}->{fs}->richlist( $self->{wrangler}->{main}->{filebrowser}->{current_dir}, ['Filesystem'] );
82 $self->{wrangler}->{main}->{filebrowser}->sort_richlist($richlist);
83 }
84  
85 my $text;
86 if($self->{mode} == 3){ # prepend relative dir-up
87 for( @$richlist ){
88 next if $_->{'Filesystem::Filename'} eq '.';
89 next if $_->{'Filesystem::Filename'} eq '..';
90 $text .= '..' . $Wrangler::Config::env{PathSeparator} . $_->{'Filesystem::Filename'} ."\n";
91 }
92 }elsif($self->{mode} == 2){ # prepend full path
93 for( @$richlist ){
94 next if $_->{'Filesystem::Filename'} eq '.';
95 next if $_->{'Filesystem::Filename'} eq '..';
96 $text .= $_->{'Filesystem::Path'} ."\n";
97 }
98 }else{ # filenames only
99 for( @$richlist ){
100 next if $_->{'Filesystem::Filename'} eq '.';
101 next if $_->{'Filesystem::Filename'} eq '..';
102 $text .= $_->{'Filesystem::Filename'} ."\n";
103 }
104 }
105  
106 $self->{text}->SetValue($text);
107 }
108  
109 sub collect_path {
110 my $self = shift;
111 my $event = shift;
112  
113 Wrangler::debug("ListingToText::collect_path: collect output path");
114  
115 my $file_dialog = Wx::FileDialog->new($self,
116 "Save file-listing as", "$self->{outputDir}", "$self->{outputFile}",
117 "Text files (*.txt)|*.txt",
118 wxFD_SAVE );
119  
120 if( $file_dialog->ShowModal == wxID_CANCEL ) {
121 # nothing
122 $file_dialog->Destroy;
123 }else{
124 if(-e $file_dialog->GetPaths){
125 # $file_dialog->Destroy;
126  
127 my $warn_dialog = Wx::MessageDialog->new( $self, "This file exists! (Overwriting currently disabled.)",
128 "Save file-listing to", wxOK );
129 $warn_dialog->ShowModal;
130 $warn_dialog->Destroy;
131  
132 $file_dialog->Destroy;
133 }else{
134 ## replace newlines with system-default, for example \r on MAC (untested)
135 if($Wrangler::Config::env{CRLF} ne "\n"){ # TextCtrl uses \n (also on MAC?)
136 my $text = $self->{text}->GetValue();
137 $text =~ s/\n/$Wrangler::Config::env{CRLF}/g;
138 }
139  
140 open(my $fh, '>', $file_dialog->GetPath );
141 binmode($fh);
142 print $fh $self->{text}->GetValue();
143 close($fh);
144  
145 Wrangler::debug("ListingToText::collect_path: written to ".$file_dialog->GetPaths );
146  
147 $file_dialog->Destroy;
148 $self->Destroy;
149 }
150 }
151 }
152  
153 1;