blib/lib/ChordPro/Wx/Preview.pm untested

filter: Statementstmt 0.0% 0 / 242 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 16 Podpod n/a totaltotal 0.0% 0 / 467 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #! perl
2  
3 use v5.26;
4 use Object::Pad;
5 use utf8;
6 use Carp;
7  
8 class ChordPro::Wx::Preview;
9  
10 use ChordPro;
11 use ChordPro::Files;
12 use ChordPro::Paths;
13 use ChordPro::Wx::Config;
14 use ChordPro::Utils qw( demarkup :xp );
15  
16 use Wx ':everything';
17 use Wx::Locale gettext => '_T';
18 use File::Temp qw( tempfile tempdir );
19 use File::Basename qw(basename);
20 use Ref::Util qw( is_hashref );
21  
22 field $panel :param;
23 field $msgs;
24 field $fatal;
25 field $died;
26 field $tmpdir;
27 field $preview_cho :accessor;
28 field $preview_file;
29 field $preview_tmp;
30 field $unsaved_preview :mutator;
31  
32 BUILD {
33 $tmpdir = tempdir( CLEANUP => 1 );
34 $preview_cho = fn_catfile( $tmpdir, "preview.cho" );
35 }
36  
37 method _warn( @m ) {
38 $self->log( 'W', join("",@m) );
39 $msgs++;
40 }
41  
42 method _info( @m ) {
43 $self->log( 'I', join("",@m) );
44 }
45  
46 method _die( @m ) {
47 $self->log( 'E', join("", @m) );
48 $msgs++;
49 $fatal++;
50 $died++;
51 }
52  
53 method log( $level, $msg ) {
54 $panel->log( $level, $msg );
55 }
56  
57 method preview( $args, %opts ) {
58  
59 unlink($preview_file) if $preview_file;
60  
61 my $annotate = eval { $panel->prepare_annotations };
62  
63 #### ChordPro
64  
65 @ARGV = (); # just to make sure
66 push( @ARGV, "--debug" ) if $state{debug};
67 push( @ARGV, "--trace" ) if $state{trace};
68 push( @ARGV, "--verbose" ) for 1..($state{verbose}//0);
69  
70 $msgs = $fatal = $died = 0;
71 local $SIG{__WARN__} = sub {
72 if ( $state{debuginfo} && $_[0] =~ /^ChordPro invoked/ ) {
73 $self->log( 'I', "@_" );
74 }
75 else {
76 $self->_warn(@_);
77 $panel->add_annotation( $1-1, $2 )
78 if $annotate && "@_" =~ /^Line (\d+),\s+(.*)/;
79 }
80 };
81  
82 # $SIG{__DIE__} = \&_die;
83  
84 my $haveconfig = List::Util::any { $_ eq "--config" } @$args;
85 if ( $preferences{skipstdcfg} ) {
86 push( @ARGV, '--nodefaultconfigs' );
87 }
88  
89 for my $preset ( qw( instruments styles stylemods ) ) {
90 for my $p ( @{$preferences{"preset_$preset"}} ) {
91 next if $p->{default};
92 next unless defined $p->{file};
93 push( @ARGV, "--config", $p->{file} );
94 $haveconfig++;
95 }
96 }
97  
98 if ( $preferences{enable_configfile} && $preferences{configfile} ) {
99 $haveconfig++;
100 push( @ARGV, '--config', $preferences{configfile} );
101  
102 }
103 local $ENV{CHORDPRO_LIB};
104 if ( $preferences{enable_customlib} ) {
105 $ENV{CHORDPRO_LIB} = $preferences{customlib};
106 }
107 CP->setup_resdirs;
108  
109 if ( $preferences{enable_xcode} ) {
110 my $c = $preferences{preset_xcodes}[0];
111 unless ( $c->{default} ) {
112 $haveconfig++;
113 push( @ARGV, '--transcode', $c->{system} );
114 }
115 }
116  
117 if ( $preferences{preset_notations} ) {
118 my $c = $preferences{preset_notations}[0];
119 if ( $c && !$c->{default} ) {
120 $haveconfig++;
121 push( @ARGV, '--config', $c->{file} );
122 }
123 }
124  
125 push( @ARGV, '--noconfig' ) unless $haveconfig;
126  
127 if ( $preferences{enable_htmlviewer} ) {
128 $preview_file = fn_catfile( $tmpdir, "preview.html" );
129 push( @ARGV, '--generate', "HTML" );
130 }
131 else {
132 $preview_file = fn_catfile( $tmpdir, "preview.pdf" );
133 push( @ARGV, '--generate', "PDF" );
134 }
135 push( @ARGV, '--output', $preview_file );
136  
137 # Transpose. See also PanelRole.pm.
138 $state{"xpose_$_"} ||= 0
139 for qw( enabled semitones accidentals );
140 if ( $state{xpose_enabled} ) {
141 my $pfx;
142 if ( $state{xpose_accidentals} == XP_SHARP ) {
143 $pfx = "s"
144 }
145 elsif ( $state{xpose_accidentals} == XP_FLAT ) {
146 $pfx = "f"
147 }
148 elsif ( $state{xpose_accidentals} == XP_KEY ) {
149 $pfx = "k"
150 }
151 else {
152 $pfx = "";
153 }
154 push( @ARGV, '--transpose', $state{xpose_semitones} . $pfx );
155 }
156  
157 push( @ARGV, '--define', 'diagnostics.format=Line %n, %m' );
158 push( @ARGV, '--define', 'debug.runtimeinfo=0' ) unless $state{debuginfo};
159  
160 push( @ARGV, @$args ) if @$args;
161 push( @ARGV, $preview_cho ) unless $opts{filelist};
162  
163 if ( $state{trace} || $state{debug}
164 || $state{verbose} && $state{verbose} > 1 ) {
165 $self->log( 'I', "Command line: @ARGV\n" );
166 $self->log( 'I', "CHORDPRO_LIB: $ENV{CHORDPRO_LIB}\n" ) if $ENV{CHORDPRO_LIB};
167 #$self->log( 'I', "$_" ) for split( /\n+/, $panel->GetParent->aboutmsg() );
168 }
169 my $options;
170 my $dialog;
171 my $phase;
172 push( @ARGV, "--progress_callback", sub {
173 my %ctl = @_;
174 $phase = $ctl{phase} if $ctl{phase};
175 $self->log( 'I', "Progress[$phase] " . $ctl{index} .
176 " of " . $ctl{total} . ": " .
177 demarkup($ctl{msg}) )
178 if $ctl{index} && ($ctl{total}||0) > 1;
179  
180 if ( $ctl{index} == 0 ) {
181 return 1 unless ($ctl{total}||0) > 1;
182 $dialog = Wx::ProgressDialog->new
183 ( "Processing...",
184 'Starting',
185 $ctl{total}, $panel,
186 wxPD_CAN_ABORT|wxPD_AUTO_HIDE|wxPD_APP_MODAL|
187 wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME );
188 }
189 elsif ( $dialog ) {
190 $dialog->Update( $ctl{index},
191 "Song " . $ctl{index} . " of " .
192 $ctl{total} . ": " .
193 demarkup($ctl{msg}) )
194 and return 1;
195 $self->log( 'I', "Processing cancelled." );
196 return;
197 }
198  
199 return 1;
200 } );
201  
202 require Encode;
203 for ( @ARGV ) {
204 next if ref ne "";
205 # $_ = Encode::encode_utf8($_);
206 }
207  
208 eval {
209 $options = ChordPro::app_setup( "ChordPro", $ChordPro::VERSION );
210 };
211  
212 $self->_die($@), goto ERROR if $@ && !$died;
213  
214 $options->{verbose} = $state{verbose} || 0;
215 $options->{trace} = $state{trace} || 0;
216 $options->{debug} = $state{debug} || $state{debuginfo};
217 $options->{diagformat} = 'Line %n, %m';
218 # Actual file name.
219 $options->{filesource} = $state{currentfile};
220 $options->{silent} = 1;
221  
222 eval {
223 ChordPro::main($options);
224 };
225 $dialog->Destroy if $dialog;
226 $self->_die($@), goto ERROR if $@ && !$died;
227 goto ERROR unless fs_test( e => $preview_file );
228  
229 $unsaved_preview = 1;
230 if ( $preferences{enable_htmlviewer} ) {
231 fs_copy( CP->findres( $_, class => "styles" ),
232 fn_catfile( $tmpdir, $_ ) )
233 for qw( chordpro.css chordpro_print.css );
234 }
235  
236 if ( !$preferences{enable_pdfviewer}
237 && $panel->{webview}->isa('Wx::WebView') ) {
238  
239 for ( $panel ) {
240 my $top = wxTheApp->GetTopWindow;
241 my ($w,$h) = $top->GetSizeWH;
242 my $want = ref($panel) =~ /Editor/ ? 700 : 900;
243 $top->SetSize( $w+400, $h ) if $w < $want;
244 unless ( $_->{sw_lr}->IsSplit ) {
245 $_->{sw_lr}->SplitVertically ( $_->{p_left},
246 $_->{p_right},
247 $state{sash}{$_->panel."_lr"} // 0.5 );
248 }
249 }
250  
251 Wx::Event::EVT_WEBVIEW_LOADED
252 ( $panel, $panel->{webview}, $self->can("OnWebViewLoaded") );
253 Wx::Event::EVT_WEBVIEW_ERROR
254 ( $panel, $panel->{webview}, $self->can("OnWebViewError") );
255  
256 use URI::file;
257 my $wf = URI::file->new($preview_file);
258 $wf =~ s;///([A-Z]):/;///$1|/;;
259 $self->log( 'I', "Preview " . substr($wf,0,128) );
260 $panel->{webview}->LoadURL($wf);
261 }
262 else {
263 $self->log( 'S', "Output generated, starting previewer");
264  
265 if ( my $cmd = $preferences{pdfviewer} ) {
266 if ( $cmd =~ s/\%f/$preview_file/g ) {
267 }
268 elsif ( $cmd =~ /\%u/ ) {
269 my $u = _makeurl($preview_file);
270 $cmd =~ s/\%u/$u/g;
271 }
272 else {
273 $cmd .= " \"$preview_file\"";
274 }
275 Wx::ExecuteCommand($cmd);
276 }
277 else {
278 my $wxTheMimeTypesManager = Wx::MimeTypesManager->new;
279 my $ft = $wxTheMimeTypesManager->GetFileTypeFromExtension
280 ( $preferences{enable_htmlviewer} ? "html" : "pdf");
281 if ( $ft && ( my $cmd = $ft->GetOpenCommand($preview_file) ) ) {
282 Wx::ExecuteCommand($cmd);
283 }
284 else {
285 Wx::LaunchDefaultBrowser($preview_file);
286 }
287 }
288 }
289  
290 $dialog->Destroy if $dialog;
291 unlink( $preview_cho );
292  
293 ERROR:
294 if ( $msgs ) {
295 $panel->alert(1);
296 $self->log( 'S', $msgs . " message" .
297 ( $msgs == 1 ? "" : "s" ) );
298 if ( $fatal ) {
299 $self->log( 'E', "Fatal problems found." );
300 return;
301 }
302 else {
303 $self->log( 'W', "Problems found." );
304 }
305 }
306 elsif ( !fs_test( s => $preview_file ) ) {
307 $panel->alert(1);
308 $self->log( 'W', "Nothing to view. Empty song?" );
309 }
310 return;
311 }
312  
313 sub OnWebViewLoaded {
314 }
315  
316 sub OnWebViewError {
317 my ( $self, $event ) = @_;
318 my $errorstring = $event->GetString;
319 my $url = $event->GetURL;
320  
321 my $errormap =
322 { wxWEBVIEW_NAV_ERR_CONNECTION() => 'wxWEB_NAV_ERR_CONNECTION',
323 wxWEBVIEW_NAV_ERR_CERTIFICATE() => 'wxWEB_NAV_ERR_CERTIFICATE',
324 wxWEBVIEW_NAV_ERR_AUTH() => 'wxWEB_NAV_ERR_AUTH',
325 wxWEBVIEW_NAV_ERR_SECURITY() => 'wxWEB_NAV_ERR_SECURITY',
326 wxWEBVIEW_NAV_ERR_NOT_FOUND() => 'wxWEB_NAV_ERR_NOT_FOUND',
327 wxWEBVIEW_NAV_ERR_REQUEST() => 'wxWEB_NAV_ERR_REQUEST',
328 wxWEBVIEW_NAV_ERR_USER_CANCELLED() => 'wxWEB_NAV_ERR_USER_CANCELLED',
329 wxWEBVIEW_NAV_ERR_OTHER() => 'wxWEB_NAV_ERR_OTHER',
330 };
331  
332 my $errorid = $event->GetInt;
333 my $errname = exists( $errormap->{$errorid} ) ? $errormap->{$errorid} : '<UNKNOWN ID>';
334  
335 $self->log( 'E',
336 sprintf( 'Getting %s Webview reports the following error code and string : %s : %s',
337 $url, $errname, $errorstring ) );
338 }
339  
340 sub _makeurl {
341 my $u = shift;
342 $u =~ s;\\;/;g;
343 $u =~ s/([^a-z0-9---_\/.~])/sprintf("%%%02X", ord($1))/ieg;
344 $u =~ s/^([a-z])%3a/\/$1:/i; # Windows
345 return "file://$u";
346 }
347  
348 method save {
349 return unless fs_test( s => $preview_file );
350  
351 if ( $preferences{enable_htmlviewer} ) {
352 return $panel->{webview}->Print;
353 }
354  
355 my $savefile = "preview";
356 if ( $state{mode} eq "editor" && $state{currentfile} ) {
357 $savefile = $state{currentfile} =~ s/\.\w+$//r;
358 }
359 if ( $state{mode} eq "sbexport" && $state{sbe_folder} ) {
360 $savefile = $state{sbe_folder} . ".pdf";
361 }
362  
363 my $fd = Wx::FileDialog->new
364 ( $panel,
365 _T("Choose output file"),
366 fn_dirname($savefile), fn_basename($savefile),
367 "*.pdf",
368 0|wxFD_SAVE|wxFD_OVERWRITE_PROMPT );
369 my $ret = $fd->ShowModal;
370 my $fn = $fd->GetPath;
371 $fd->Destroy;
372  
373 if ( $ret == wxID_OK ) {
374 if ( fs_copy( $preview_file, $fn ) ) {
375 $unsaved_preview = 0;
376 }
377 else {
378 my $md = Wx::MessageDialog->new
379 ( $self,
380 "Cannot save to $fn\n$!",
381 "Error saving file",
382 0 | wxOK | wxICON_ERROR);
383 $md->ShowModal;
384 $md->Destroy;
385 }
386 }
387 return $ret;
388 }
389  
390 method have_preview {
391 fs_test( s => $preview_file );
392 }
393  
394 method discard {
395 $unsaved_preview = 0;
396 unlink($preview_file);
397 }
398  
399 1;