App.pm untested

filter: Statementstmt 0.0% 0 / 358 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 123 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 719 CC 67 SCAR 44.6
CC
67
Cov
0%
reduce_hash
306.0
options
90.0
simple_Proto_from_XML
72.0
CRAP 86.1
1 package Glade::Two::App;
2 require 5.000; use strict 'vars', 'refs', 'subs';
3  
4 # Copyright (c) 1999 Dermot Musgrove <dermot.musgrove@virgin.net>
5 #
6 # This library is released under the same conditions as Perl, that
7 # is, either of the following:
8 #
9 # a) the GNU General Public License as published by the Free
10 # Software Foundation; either version 1, or (at your option) any
11 # later version.
12 #
13 # b) the Artistic License.
14 #
15 # If you use this library in a commercial enterprise, you are invited,
16 # but not required, to pay what you feel is a reasonable fee to perl.org
17 # to ensure that useful software is available now and in the future.
18 #
19 # (visit http://www.perl.org/ or email donors@perlmongers.org for details)
20  
21 BEGIN {
22 use Exporter qw( );
23 use POSIX qw( isdigit);
24 use Gtk2; # For message_box
25 use Cwd qw( cwd chdir);
26 use File::Basename;
27 use Data::Dumper;
28 use Text::Wrap qw( wrap $columns); # in options, diag_print
29 use Glade::Two::Run qw(:METHODS :VARS);
30 use vars qw( @ISA
31 $AUTOLOAD
32 %fields
33 @EXPORT @EXPORT_OK %EXPORT_TAGS
34 $PACKAGE $VERSION $AUTHOR $DATE
35 @VARS @METHODS
36 );
37 # Tell interpreter who we are inheriting from
38 @ISA = qw(
39 Exporter
40 Glade::Two::Run
41 );
42  
43 $PACKAGE = __PACKAGE__;
44 $VERSION = q(0.01);
45 $AUTHOR = q(Dermot Musgrove <dermot.musgrove@virgin.net>);
46 $DATE = q(Sun Nov 17 06:02:01 GMT 2002 );
47  
48 # These vars are imported by all Glade-Perl modules for consistency
49 @VARS = qw(
50 );
51 # $Glade_Perl
52 # $indent
53 @METHODS = qw(
54 );
55 # These symbols (globals and functions) are always exported
56 @EXPORT = qw(
57 );
58 # Optionally exported package symbols (globals and functions)
59 @EXPORT_OK = ( @METHODS, @VARS);
60 # Tags (groups of symbols) to export
61 %EXPORT_TAGS = (
62 'METHODS' => [@METHODS] ,
63 'VARS' => [@VARS]
64 );
65 }
66  
67 %fields = (
68 # These are the data fields that you can set/get using the dynamic
69 # calls provided by AUTOLOAD (and their initial values).
70 # eg $class->FORMS($new_value); sets the value of FORMS
71 # $current_value = $class->FORMS; gets the current value of FORMS
72 'app' => {
73 'name' => undef,
74 'author' => undef,
75 'version' => '0.01',
76 'date' => undef,
77 'copying' => # Copying policy to appear in generated source
78 "# Unspecified copying policy, please contact the author
79 # ",
80 'description' => undef, # Description for About box etc.
81 # 'pixmaps_directory' => undef,
82 'logo' => 'Logo.xpm', # Use specified logo for project
83 },
84 'data' => {
85 'directory' => undef,
86 },
87 'diag' => {
88 'verbose' => undef, # Show errors and main diagnostics
89 'wrap_at' => 0, # Max diagnostic line length (approx)
90 'autoflush' => undef, # Dont change the policy
91 'indent' => ' ', # Diagnostics indent to lay out messages
92 'benchmark' => undef, # Dont add time to the diagnostic messages
93 'log' => undef, # Write diagnostics to STDOUT
94 # 'log' => "\&STDOUT",# Write diagnostics to STDOUT
95 # or Filename to write diagnostics to
96 'LANG' => ($ENV{'LANG'} || ''),
97 # Which language we want the diagnostics
98 },
99 'run_options' => {
100 'name' => __PACKAGE__,
101 'version' => $VERSION, # Version of Glade-Perl used
102 'author' => $AUTHOR,
103 'date' => $DATE,
104 'logo' => 'glade2perl_logo.xpm', # Our logo
105 'start_time' => undef, # Time that this run started
106 'mru' => undef,
107 'prune' => undef,
108 'proto' => {
109 'site' => undef,
110 'user' => undef,
111 'project' => undef,
112 'params' => undef,
113 'app_defaults' => undef,
114 'base_defaults' => undef,
115 },
116 'xml' => {
117 'site' => undef,
118 'user' => undef,
119 'project' => undef,
120 'params' => undef,
121 'app_defaults' => "Application defaults",
122 'base_defaults' => __PACKAGE__." defaults",
123 'set_by' => 'DEFAULT', # Who set the options
124 'encoding' => undef, # Character encoding eg ('ISO-8859-1')
125 },
126 },
127 'glade' => {
128 'name_from' => undef,
129 'file' => undef,
130 'encoding' => 'ISO-8859-1', # Character encoding eg ('ISO-8859-1')
131 'version' => undef, # Version of Glade that made file
132 'project' => undef, # project proto
133 'proto' => undef, # widget file proto
134 'string' => undef,
135 },
136 'source' => {
137 'indent' => ' ', # Source code indent per Gtk 'nesting'
138 'tabwidth' => 8, # Replace each 8 spaces with a tab in sources
139 'tab' => '',
140 'write' => undef, # Dont write source code
141 'quick_gen' => 0, # 1 = Don't perform any checks
142 'save_connect_id'=> 0, # 1 = generate code to save signal_connect ids
143 'hierarchy' => '', # Dont generate any hierarchy
144 # widget...
145 # eg $hier->{'vbox2'}{'table1'}...
146 # class... startswith class
147 # eg $hier->{'GtkVBox'}{'vbox2'}{'GtkTable'}{'table1'}...
148 # both... widget and class
149 'style' => 'AUTOLOAD', # Generate code using OO AUTOLOAD code
150 # Libglade generate libglade code
151 # closures generate code using closures
152 # Export generate non-OO code
153 'LANG' => ($ENV{'LANG'} || ''),
154 # Which language we want the source to be in
155 },
156 'module' => {
157 'sigs' => {
158 'class' => undef,
159 'base' => undef,
160 'file' => undef,
161 },
162 'ui' => {
163 'class' => undef,
164 'file' => undef,
165 },
166 'app' => {
167 'class' => undef,
168 'base' => undef,
169 'file' => undef,
170 },
171 'subapp' => {
172 'class' => undef,
173 'file' => undef,
174 },
175 'libglade' => {
176 'class' => undef,
177 'file' => undef,
178 },
179 'onefile' => {
180 'class' => undef,
181 'file' => undef,
182 },
183 'pot' => {
184 'class' => undef,
185 'file' => undef,
186 },
187 },
188 'test' => {
189 'name' => undef,
190 'directory' => undef,
191 'first_form' => undef,
192 'use_module' => undef,
193 },
194 'dist' => {
195 'write' => 'True',
196 'directory' => '',
197 'Makefile_PL' => 'Makefile.PL',
198 'MANIFEST_SKIP' => 'MANIFEST.SKIP',
199 'test_directory'=> 't',
200 'test_pl' => 'test.pl',
201 'bin_directory' => 'bin',
202 'bin' => undef, # name of bin (script) to generate
203 'rpm' => undef, # Name of RPM to produce
204 'spec' => undef, # Name of RPM spec file
205 'type' => undef, # Type of distribution
206 'compress' => undef, # How to compress the distribution
207 'scripts' => undef, # Scripts that should be installed
208 'docs' => undef, # Documentation that should be included
209 },
210 'doc' => {
211 'write' => 'True',
212 'directory' => 'Documentation',
213 'COPYING' => 'COPYING',
214 'Changelog' => 'Changelog',
215 'FAQ' => 'FAQ',
216 'INSTALL' => 'INSTALL',
217 'NEWS' => 'NEWS',
218 'README' => 'README',
219 'ROADMAP' => 'ROADMAP',
220 'TODO' => 'TODO',
221 },
222 'helper' => {
223 'editors' => undef, # Editor calls that are available
224 'active_editor' => undef, # Index of editor that we are using
225 },
226 );
227  
228 my $option_hashes = " ".
229 join(" ",
230 'app',
231 'project',
232 'proto',
233 'run_options',
234 'diag',
235 'glade',
236 'glade2perl',
237 'glade2perl-1',
238 'glade2perl-2',
239 'glade_helper',
240 'source',
241 'xml',
242 'doc',
243 'dist',
244 'data',
245 'module',
246 'helper',
247 'test'
248 )." ";
249  
250 sub DESTROY {
251 # This sub will be called on object destruction
252 } # End of sub DESTROY
253  
254 =pod
255  
256 =head1 NAME
257  
258 Glade::App - Utility methods for Glade-Perl (and generated applications).
259  
260 =head1 SYNOPSIS
261  
262 use vars qw(@ISA);
263 use Glade::App qw(:METHODS :VARS);
264 @ISA = qw( Glade::App);
265  
266 # 1) CLASS methods
267 my $Object = Glade::App->new(%params);
268 $Object->glade->file($supplied_path);
269 $widget = $window->lookup_widget('clist1');
270  
271 # 2) OPTIONS handling
272 $options = Glade::App->options(%params);
273 $normalised_value = Glade::App->normalise('True');
274 $new_hash_ref = Glade::App->merge_into_hash_from(
275 $to_hash_ref, # Hash to be updated
276 $from_hash_ref, # Input data to be merged
277 'set accessors'); # Any value will add AUTOLOAD() accessors
278 # for these keys.
279 $Object->save_app_options($mru_filename);
280 $Object->save_options;
281  
282 my $string = Glade::App->string_from_file('/path/to/file');
283 Glade::App->save_file_from_string('/path/to/file', $string);
284  
285 # 3) Diagnostic message printing
286 $Object->start_log('log_filename');
287 $Object->diag_print(2, "This is a diagnostics message");
288 $Object->diag_print(2, $hashref, "Prefix to message");
289 $Object->stop_log;
290  
291 # 4) I18N
292 Glade::App->load_translations('MyApp', 'fr', '/usr/local/share/locale/',
293 undef, $SOURCE_LANG, 'Merge with already loaded translations');
294 sprintf(_("A message '%s'"), $value);
295 sprintf(gettext($SOURCE_LANG, "A message '%s'"), $value);
296 Glade::App->start_checking_gettext_strings($SOURCE_LANG);
297 Glade::App->stop_checking_gettext_strings($SOURCE_LANG);
298 Glade::App->write_missing_gettext_strings($SOURCE_LANG);
299  
300 # 5) UI methods
301 my $image = Glade::App->create_image('new.xpm', ['dir1', 'dir2']);
302 my $pixmap = Glade::App->create_pixmap($form, 'new.xpm', ['dir1', 'dir2']);
303  
304 Glade::App->show_skeleton_message(
305 $me, \@_, __PACKAGE__, "$Glade::App::pixmaps_directory/Logo.xpm");
306 Glade::App->message_box(
307 $message, # Message to display
308 $title, # Dialog title string
309 [_('Dismiss'), _("Quit")." Program"], # Buttons to show
310 1, # Default button is 1st
311 $pixmap, # pixmap filename
312 [&dismiss, &quit], # Button click handlers
313 $entry_needed); # Whether to show an entry
314 # widget for user data
315  
316 # 6) General methods
317 $path = $Object->full_Path($Object->glade->file, $dir);
318 $path = Glade::App->relative_Path($relative_path, $directory);
319  
320 $Object->reload_any_altered_modules;
321  
322 =head1 DESCRIPTION
323  
324 Glade::App provides some utility methods that Glade-Perl modules and
325 also the generated classes need to run. These methods can be inherited and
326 called in any app that use()s Glade::App and quotes Glade::App
327 in its @ISA array.
328  
329 Broadly, the utilities are of seven types.
330  
331 1) Class methods
332 2) Options handling
333 3) Diagnostic message printing
334 4) I18N
335 5) UI methods
336 6) General methods
337  
338 =head1 1) CLASS METHODS
339  
340 The class methods provide an object constructor and data accessors.
341  
342 =over 4
343  
344 =cut
345  
346 sub new {
347  
348 =item new(%params)
349  
350 Construct a Glade::App object
351  
352 e.g. my $Object = Glade::App->new(%params);
353  
354 =cut
355  
356 my $that = shift;
357 my %params = @_;
358 my $class = ref($that) || $that;
359 # Call our super-class constructor to get an object and reconsecrate it
360 my $self = bless {}, $class;
361  
362 $self->merge_into_hash_from($self, \%fields, (__PACKAGE__." defaults"));
363 $self->run_options->proto->base_defaults(\%fields);
364 $self->merge_into_hash_from($self, \%params, ("$class app defaults"));
365 $self->run_options->proto->app_defaults(\%params);
366  
367 return $self;
368 }
369  
370 sub AUTOLOAD {
371  
372 =item AUTOLOAD()
373  
374 Accesses all class data
375  
376 e.g. my $glade_filename = $Object->glade->file;
377 or $Object->glade->file('path/to/glade/file');
378  
379 =cut
380 my $self = shift;
381 my $class = ref($self)
382 or die "$self is not an object so we cannot '$AUTOLOAD'\n",
383 "We were called from ".join(", ", caller),"\n\n";
384 my $name = $AUTOLOAD;
385 $name =~ s/.*://; # strip fully-qualified portion
386  
387 if (exists $self->{$permitted_fields}->{$name}) {
388 # This allows dynamic data methods - see %fields above
389 # eg $class->UI('new_value');
390 # or $current_value = $class->UI;
391 if (@_) {
392 return $self->{$name} = shift;
393 } else {
394 return $self->{$name};
395 }
396  
397 } elsif ($name ne 'DESTROY'){
398 die "Can't access method `$name' in class $class\n",
399 "We were called from ",join(", ", caller),"\n\n";
400  
401 }
402 }
403  
404 #===============================================================================
405 #=========== Options utilities ============
406 #===============================================================================
407  
408 =back
409  
410 =head1 2) OPTIONS HANDLING METHODS
411  
412 These methods will load, merge, reduce and save a hierarchical options
413 structure that is stored in one or more XML files and accessed with
414 AUTOLOAD methods.
415  
416 =over
417  
418 =cut
419  
420 sub options {
421 my ($class, %params) = @_;
422 my $me = $class."->options";
423  
424 =item options(%params)
425  
426 Loads and merges all app options.
427  
428 e.g. Glade::App->options(%params);
429 my options = $Object->options(%params);
430  
431 =cut
432 my ($self, $global, $type, $key, $defaults, $I18N_name, $log, $report);
433 $global = delete $params{'options_global'} || "\$Glade_Perl";
434 $defaults = delete $params{'options_defaults'} || \%Glade::Two::app_fields;
435 $type = delete $params{'options_key'} || $defaults->{type} || 'glade2perl-2';
436 $I18N_name = delete $params{'options_I18N_name'} || $type || 'Glade-Perl';
437 $report = delete $params{'options_report'};
438  
439 unless (ref $class eq $class) {
440 # This is first time through so construct object and load options
441 @use_modules = ();
442
443 $self = bless __PACKAGE__->new(%$defaults), $class;
444  
445 eval "$global = \$self";
446  
447 # Now set element $type to point to our options hash
448 $self->{$type} = $self->{run_options};
449 push @{$self->{$permitted_fields}{$type}}, $me;
450  
451 $self->load_all_options(%params);
452  
453 print "App defaults ", Dumper(\%fields) if $report;
454 print "App defaults supplied ", Dumper($defaults) if $report;
455 print "App params passed ", Dumper(\%params) if $report;
456 eval "print \"\$report Initial state with app defaults and options loaded \", ".
457 "Dumper($report),\"\n\n\"" if $report;
458  
459 # Merge in all options available
460 foreach $key ('site', 'user', 'project', 'params') {
461 eval "print \"".$self->{$type}->xml->{$key}.
462 " options supplied \", "."Dumper($report),\"\n\n\"" if $report;
463 $self->merge_into_hash_from($self,
464 $self->{$type}->proto->{$key},
465 $self->{$type}->xml->{$key});
466 eval "print \"\$report After \$key options from '".
467 $self->{$type}->xml->{$key}."' merged \", ".
468 "Dumper($report),\"\n\n\"" if $report;
469 }
470  
471 $self->{$type}->start_time($class->get_time);
472 $self->{$type}->name($class);
473 $self->{$type}->version($VERSION);
474 $self->{$type}->author($AUTHOR);
475 $self->{$type}->date($DATE);
476  
477  
478 # Load the diagnostics gettext translations
479 $self->load_translations($I18N_name, $self->diag->LANG, undef,
480 undef, $DIAG_LANG, undef);
481 # $self->load_translations($I18N_name, $self->diag->LANG, undef,
482 # '/home/dermot/Devel/$I18N_name/ppo/en.mo', $DIAG_LANG, undef);
483 # $self->check_gettext_strings($DIAG_LANG);
484  
485 if ($self->diag->wrap_at == 0) {
486 $columns = 1500;
487 } else {
488 $columns = $self->diag->wrap_at;
489 }
490  
491 } else {
492 $self = $class;
493 $self->{$type}->xml->params(
494 $params{$type}{'xml'}{'set_by'} ||
495 $params{'options_set'} ||
496 $self->{$type}->xml->set_by ||
497 $me);
498 $self->{$type}->proto->params($self->convert_old_options(\%params));
499 $self->merge_into_hash_from($self,
500 $self->{$type}->proto->params,
501 $self->{$type}->xml->params);
502 }
503  
504 $self->diag_print (4, $self->{$type}->proto->params);
505 $self->diag_print (5, $self->{$type}->xml);
506 $self->diag_print (6, $self->{$type});
507 $self->diag_print (7, $self);
508
509 $self->{$type}->xml->set_by (
510 $self->{$type}->proto->params->{$type}{xml}{set_by} || $me);
511  
512 return $self;
513 }
514  
515 sub load_all_options {
516 my ($class, %params) = @_;
517 my $me = (ref $class || $class)."->load_all_options";
518  
519 my $type = $class->{type} || $params{type};
520 $class->{'run_options'}->xml->encoding(
521 $params{'run_options'}{xml}{encoding} ||
522 $params{'run_options'."_encoding"} ||
523 $params{glade}{encoding} ||
524 $params{glade_encoding} ||
525 'ISO-8859-1'
526 );
527  
528 # PARAMS supplied
529 $class->{'run_options'}->xml->params(
530 $params{'run_options'}{'xml'}{'set_by'} ||
531 $params{'options_set'} ||
532 $class->{'run_options'}->xml->set_by ||
533 $me);
534 $class->{'run_options'}->proto->params(
535 # %params);
536 $class->convert_old_options(\%params));
537  
538 # USER options file
539 $class->{'run_options'}->xml->user(
540 $class->{'run_options'}->proto->params->{'run_options'}{xml}{user} ||
541 "$ENV{'HOME'}/.$type.xml");
542  
543 $class->{'run_options'}->get_options('user');
544 $class->{'run_options'}->proto->user(
545 $class->convert_old_options($class->{'run_options'}->proto->user,
546 $class->{'run_options'}->xml->user));
547  
548 #print Dumper($class->{'run_options'}->proto);
549 # PROJECT options file (from user mru if not specified in params)
550 # if ($class->{'run_options'}->xml->project eq $NOFILE) {
551 my $base = $class->{'run_options'}->proto->user->{'run_options'}{mru} || '';
552 $base =~ s/(.+)\..*$/$1/;
553 $base =~ s/(.+)\..*$/$1/;
554 $base ? $base .= ".$type.xml" : $base = $NOFILE;
555 $class->{'run_options'}->xml->project(
556 $class->{'run_options'}->proto->params->{'run_options'}{xml}{project} ||
557 $class->{'run_options'}->xml->project ||
558 $base
559 );
560 # }
561 unless ($class->{'run_options'}->xml->project eq $NOFILE) {
562 $class->{'run_options'}->xml->project(
563 $class->full_Path($class->{'run_options'}->xml->project, `pwd`));
564 }
565 #print Dumper($class->{'run_options'}->proto->params);
566 #print Dumper($class->{'run_options'}->xml);exit
567 $class->{'run_options'}->get_options('project');
568 $class->{'run_options'}->proto->project(
569 $class->convert_old_options($class->{'run_options'}->proto->project, $me));
570
571 # SITE options file
572 $class->{'run_options'}->xml->site(
573 $class->{'run_options'}->proto->params->{'run_options'}{xml}{site} ||
574 $class->{'run_options'}->proto->project->{'run_options'}{xml}{site} ||
575 $class->{'run_options'}->proto->user->{'run_options'}{xml}{site} ||
576 "/etc/$type.xml");
577  
578 $class->{'run_options'}->get_options('site');
579 $class->{'run_options'}->proto->site(
580 $class->convert_old_options($class->{'run_options'}->proto->site,
581 $class->{'run_options'}->xml->site));
582  
583 $class->diag_print (5, $class) if ref $class;
584 return $class->{'run_options'};
585 }
586  
587 sub get_options {
588 my ($class, $type, $file) = @_;
589  
590 my $pwd = `pwd`;
591 my ($encoding);
592 $file ||= $class->xml->{$type} || $NOFILE;
593  
594 if ($file eq $NOFILE) {
595 $class->xml->{$type} = $file;
596 $class->proto->{$type} = {};
597 return;
598 }
599 if ($file && -r $file) {
600 ($encoding, $class->proto->{$type}) = $class->simple_Proto_from_File(
601 # ($encoding, $class->proto->{$type}) = Glade::PerlXML->Proto_from_File(
602 $class->xml->{$type},
603 '', $option_hashes,
604 $class->xml->encoding);
605 $class->xml->encoding($encoding);
606  
607 } else {
608 # print "File '$file' could NOT be read\n";
609 $class->proto->{$type} = {};
610 }
611 }
612  
613 sub simple_Proto_from_File {
614 my ($class, $filename, $repeated, $special, $encoding) = @_;
615 my $me = __PACKAGE__."->new_Proto_from_File";
616  
617 my $pos = -1;
618 my $xml = $class->string_from_File($filename);
619 return $class->simple_Proto_from_XML(\$xml, 0, \$pos, $repeated, $special, $encoding);
620 }
621  
622 sub simple_Proto_from_XML {
623 my ($class, $xml, $depth, $pos, $repeated, $special, $encoding) = @_;
624 my $me = __PACKAGE__."->simple_Proto_from_XML";
625  
626 # Loads hash from XML string using regexps (not XML::Parser).
627 my ($self, $tag, $use_tag, $prev_contents, $work);
628 my ($found_encoding, $new_pos);
629 while (($new_pos = index($$xml, "<", $$pos)) > -1) {
630 $prev_contents = substr($$xml, $$pos, $new_pos-$$pos);
631 $$pos = $new_pos;
632 $new_pos = index($$xml, ">", $$pos);
633 $tag = substr($$xml, $$pos+1, $new_pos-$$pos-1);
634 $$pos = $new_pos+1;
635 if ($tag =~ /^\?/) {
636 if ($tag =~ s/\?xml.*\s*encoding\=["'](.*?)['"]\?\n*//) {
637 $found_encoding = $1;
638 } else {
639 $found_encoding = $encoding;
640 }
641 next;
642 }
643 if ($tag =~ s|^/||) {
644 # We are an endtag so return the $prev_contents
645 #print "Found end tag </$tag>\n";
646 if (ref $self) {
647 #print Dumper($self);
648 return $self;
649  
650 } else {
651 return &UnQuoteXMLChars($prev_contents);
652 }
653  
654 } else {
655 # We are a starttag so recurse
656 if ($tag =~ s|/$||) {
657 # We are also an endtag (empty eg. <tagname /> so ignore
658 #print "Found empty tag <$tag />\n";
659 } else {
660 # Ignore tags starting with ? or !
661 next if $tag =~ /^[\?\!]/;
662 $work = $class->simple_Proto_from_XML(
663 $xml, $depth + 1, $pos, $repeated);
664 if (" $repeated " =~ / $tag /) {
665 # Store as the next list item
666 push(@{$self->{list}}, $tag) ;
667 } else {
668 # Store as key
669 $use_tag = $tag;
670 }
671 $self->{$use_tag} = $work;
672 #print "Found start tag <$tag>\n";
673 }
674 }
675 }
676 #print Dumper($self);
677  
678 return ($found_encoding, values %$self);
679 }
680  
681 sub convert_old_options {
682 my ($class, $old, $file) = @_;
683 my $me = __PACKAGE__."->convert_old_options";
684 my $new = {};
685  
686 my $key;
687 my $converted = 0;
688 for $key (keys %$old) {
689 # Normalise any True/False values to 1/0
690 $old->{$key} = $class->normalise($old->{$key});
691 if ($convert->{$key}) {
692 eval $convert->{$key};
693 die @! if @!;
694 $converted++;
695 } elsif (ref $old->{$key}) {
696 $new->{$key} = $class->merge_into_hash_from(
697 $new->{$key}, $old->{$key}, $file);
698 } else {
699 $new->{$key} = $old->{$key};
700 }
701 }
702  
703 if ($file and $converted and $class->diagnostics(2)) {
704 if (-w $file) {
705 # We can rewrite the options file
706 print sprintf("$me has converted options in file %s\n",
707 $file);
708 $class->write_options($new, $file);
709 } else {
710 print "$me cannot rewrite file '$file'\n".
711 sprintf(
712 "You may want to edit '$file' yourself to read: \n%s\n",
713 $class->XML_from_Proto('', ' ', 'G2P-Options', $new));
714 }
715 }
716 return $new;
717 }
718  
719 sub normalise {
720 my ($class, $value) = @_;
721  
722 =item normalise($value)
723  
724 Return a normalised value ie. convert 'True'|'Yes'|'y'|'On' to 1
725 and 'False'|'No'|'n'|'Off' to 0.
726 The comparisons are case-insensitive.
727  
728 e.g. my $normalised_value = Glade::App->normalise('True');
729  
730 =cut
731 if (defined $value) {
732 if ($value =~ /^(true|y|yes|on)$/i) {
733 return 1;
734 } elsif ($value =~ /^(false|n|no|off)$/i) {
735 return 0;
736 } else {
737 return $value;
738 }
739 }
740 }
741  
742 sub merge_into_hash_from {
743 my ($class, $to_hash, $from_hash, $autoload) = @_;
744 my $me = $class."->merge_into_hash_from";
745  
746 =item merge_into_hash_from($to_hash, $from_hash, $autoload)
747  
748 Recursively merge a hash into an existing one - overwriting any keys with
749 a defined value. It will also optionally set accessors for the keys to be
750 used via AUTOLOAD().
751  
752 e.g. $new_hash_ref = Glade::App->merge_into_hash_from(
753 $to_hash_ref, # Hash to be updated
754 $from_hash_ref, # Input data to be merged
755 'set accessors'); # Any value will add AUTOLOAD() accessors
756 # for these keys.
757  
758 =cut
759 my ($key, $value);
760 $autoload ||= '';
761 foreach $key (keys %$from_hash) {
762 next if $key eq $permitted_fields;
763 if (ref $from_hash->{$key} eq 'HASH') {
764 $to_hash->{$key} ||= bless {}, ref $to_hash;
765 $class->merge_into_hash_from(
766 $to_hash->{$key},
767 $from_hash->{$key},
768 $autoload);
769  
770 } else {
771 # Check that we are not overwriting a hash with a scalar
772 unless (ref $to_hash->{$key}) {
773 $to_hash->{$key} = $class->normalise($from_hash->{$key});
774 }
775 }
776 $to_hash->{$permitted_fields}{$key}++ if $autoload;
777 }
778 return $to_hash;
779 }
780  
781 sub save_app_options {
782 my ($class, $mru, %defaults) = @_;
783 my $me = $class."->save_app_options";
784  
785 =item save_app_options($mru, %defaults)
786  
787 Updates mru and saves all app/user options. This will save the mru file
788 in the user options file (if one is named in
789 $class->{$class->type}->xml->user).
790  
791 e.g. Glade::App->save_app_options($mru_filename);
792  
793 =cut
794 %defaults = %{$class->{$class->type}->proto->app_defaults}
795 unless keys %defaults;
796
797 # Store new mru file name and start_time
798 $class->{$class->type}->proto->user->{$class->type}{mru} = $mru;
799 $class->{$class->type}->proto->user->{$class->type}{start_time} =
800 ($class->{$class->type}->start_time);
801 undef $class->{$class->type}{mru};
802  
803 # Save project options
804 $class->diag_print(6, $class, "Options to be saved");
805 $class->save_options(
806 undef,
807 %Glade::App::fields,
808 %defaults
809 );
810  
811 if ($class->{$class->type}->xml->user) {
812 # Save new user options
813 $class->write_options(
814 $class->reduce_hash(
815 $class->{$class->type}->proto->user,
816 {},
817 {},
818 {},
819 {},
820 $class->{$class->type}->prune
821 ),
822 $class->{$class->type}->xml->user);
823 }
824 }
825  
826 sub save_options {
827 my ($class, $filename, %app_defaults) = @_;
828 my $me = __PACKAGE__."->save_options";
829  
830 =item save_options($filename, %app_defaults)
831  
832 Reduce and save the supplied options to the file specified.
833  
834 e.g. $Object->save_options;
835  
836 =cut
837 my $type = $class->type;
838 %app_defaults = %{$class->{$class->type}->proto->app_defaults}
839 unless keys %app_defaults;
840
841 if ($filename) {
842 $class->{$type}->xml->{project} = ($filename);
843 } else {
844 $filename = $class->{$type}->xml->{project};
845 }
846  
847 if ($filename eq $NOFILE) {
848 $class->diag_print(2, "%s- Not saving %s project options",
849 $indent, $type);
850 return;
851 }
852 $class->diag_print(4, $class, "Project options");
853  
854 my $options = $class->reduce_hash(
855 $class,
856 $class->{$type}->proto->user,
857 $class->{$type}->proto->site,
858 \%app_defaults,
859 \%__PACKAGE__::fields,
860 $class->{$type}->prune,
861 $__PACKAGE__::hash_types,
862 );
863  
864 if (ref $options) {
865 bless $options, ref $class;
866 $options->{'type'} = $type;
867 $options->{$type}{start_time} = ($class->{$type}->start_time);
868 $class->write_options($options, $filename);
869 } else {
870 $class->diag_print(2, "%s- No project options need saving",
871 $indent);
872 }
873 }
874  
875 sub write_options {
876 my ($class, $options, $filename) = @_;
877 my $me = __PACKAGE__."->write_options";
878  
879 =item write_options($options, $filename)
880  
881 Write an options hash to XML file.
882  
883 e.g. my options = $Object->write_options($hash_ref, '/path/to/file');
884  
885 =cut
886 my $type = $class->type;
887 my $xml;
888  
889 if ($class->{$type}->xml->encoding) {
890 $xml = "<?xml version=\"1.0\" encoding=\"".
891 $class->{$type}->xml->encoding."\"?>\n";
892 } else {
893 $xml = "<?xml version=\"1.0\"?>\n";
894 }
895 $xml .= $class->XML_from_Proto('', ' ', "$type-Options", $options);
896
897 if ($filename eq $NOFILE) {
898 $class->diag_print(2, "%s- Not saving %s options", $indent, $type);
899 $class->diag_print(2, "%s", "$indent- XML would have been\n'$xml'\n");
900 return;
901 }
902 $class->diag_print(5, $xml, 'DONT_TRANSLATE');
903  
904 $class->save_file_from_string($filename, $xml);
905  
906 $class->diag_print(2, "%s- %s options saved to %s",
907 $class->diag->indent, $type, $filename);
908 }
909  
910 sub reduce_hash {
911 my ($class,
912 $all_options, $user_options, $site_options,
913 $app_defaults, $base_defaults,
914 $prune, $hash_types) = @_;
915 my $me = __PACKAGE__."->reduce_hash";
916  
917 =item reduce_hash($all_options, $user_options, $site_options,
918 $app_defaults, $base_defaults, $prune, $hash_types)
919  
920 Removes any options that are equivalent to site/user/project options
921 or that are specified to be pruned. We will descend into any hash types
922 specified.
923  
924 e.g. my options = $Object->reduce_hash(
925 $options_to_reduce,
926 $user_options,
927 $site_options,
928 $app_defaults,
929 $base_defaults
930 '*work*proto*',
931 '*My::Class*');
932  
933 =cut
934 my ($key, $default, $from, $return, $reftype);
935 my $verbose = 5;
936 $user_options ||= {};
937 $site_options ||= {};
938 $app_defaults ||= {};
939 $base_defaults ||= {};
940 $prune ||= "*".
941 join("*",
942 $permitted_fields,
943 &typeKey,
944 'project',
945 'widgets',
946 'properties',
947 'run_options',
948 'PARTYPE',
949 'module',
950 'tab',
951 'proto',
952 'gtk_style',
953 'generate',
954 'prune',
955 ).
956 "*";
957 $hash_types ||= "*".join("*",
958 (ref $class || $class),
959 'Glade::Two::App',
960 'Glade::Two',
961 'Glade::Two::Run',
962 'Glade::Two::Generate',
963 )."*";
964  
965 $class->diag_print($verbose, "Prune is '$prune'");
966 $class->diag_print($verbose, "Hashtypes is '$hash_types'");
967 foreach $key (keys %{$all_options}) {
968 $reftype = ref $all_options->{$key};
969 $class->diag_print($verbose+1, "%s- Reducing %s object '%s'",
970 $class->diag->indent, $reftype, $key) if $reftype;
971 if ($reftype and "*ARRAY*" =~ /\*$reftype\*/) {
972 $class->diag_print($verbose, "--------------------------------");
973 $all_options->{$key} = join("\n", @{$all_options->{$key}});
974 $class->diag_print($verbose,
975 "%s- Joining '%s' object {'%s'} into newline-separated string '%s'",
976 $class->diag->indent, $reftype, $key, $all_options->{$key});
977 }
978 if (!defined $all_options->{$key}) {
979 $class->diag_print ($verbose,
980 "%s- Removing option '%s' (%s)",
981 $class->diag->indent, $key, 'no value defined');
982  
983 } elsif ($prune =~ /\*$key\*/) {
984 # Ignore the specified keys
985 $class->diag_print ($verbose,
986 "%s- Removing option '%s' (%s)",
987 $class->diag->indent, $key, 'pruned');
988  
989 } elsif ($reftype and "*HASH*$hash_types*" =~ /\*$reftype\*/) {
990 $class->diag_print($verbose, "--------------------------------");
991 $class->diag_print($verbose, "%s- Descending into '%s' object {'%s'}",
992 $class->diag->indent, $reftype, $key);
993 $class->diag_print($verbose+1, $all_options->{$key},
994 $class->diag->indent."- {'$key'} which is a ");
995 $class->diag_print($verbose+1, $all_options->{$key},
996 "Project option element {'$key'}");
997 $class->diag_print($verbose+1, $user_options->{$key},
998 "User options element {'$key'}") if $user_options->{$key};
999 $class->diag_print($verbose+1, $site_options->{$key},
1000 "Site options element {'$key'}") if $site_options->{$key};
1001 $class->diag_print($verbose+1, $app_defaults->{$key},
1002 "App defaults element {'$key'}") if $app_defaults->{$key};
1003 $class->diag_print($verbose+1, $base_defaults->{$key},
1004 __PACKAGE__." defaults element {'$key'}") if $base_defaults->{$key};
1005 $return->{$key} = $class->reduce_hash(
1006 $all_options->{$key},
1007 $user_options->{$key},
1008 $site_options->{$key},
1009 $app_defaults->{$key},
1010 $base_defaults->{$key},
1011 $prune, $hash_types);
1012 unless (keys %{$return->{$key}}) {
1013 delete $return->{$key};
1014 $class->diag_print($verbose, "%s- Losing empty hash {'%s'}",
1015 $class->diag->indent, $key);
1016 } else {
1017 $class->diag_print($verbose, $return->{$key},
1018 "$me reduced {'$key'} so that");
1019 }
1020  
1021 } else {
1022 if (defined $user_options->{$key}) {
1023 $default = $user_options->{$key};
1024 $from = "user options file";
1025  
1026 } elsif (defined $site_options->{$key}) {
1027 $default = $site_options->{$key};
1028 $from = "site options file";
1029  
1030 } elsif (defined $app_defaults->{$key}) {
1031 $default = $app_defaults->{$key};
1032 $from = (ref $all_options)." app defaults";
1033  
1034 } elsif (defined $base_defaults->{$key}) {
1035 $default = $base_defaults->{$key};
1036 $from = __PACKAGE__." defaults";
1037  
1038 } else {
1039 $default = '__NO_DEFAULT_OPTION_AVAILABLE__';
1040 $from = "no default";
1041 }
1042 if ($all_options->{$key} eq $class->normalise($default)) {
1043 $class->diag_print ($verbose,
1044 "%s- Removing {'%s'} => '$all_options->{$key}' (equals default in %s)",
1045 $class->diag->indent, $key, $from);
1046 } elsif (!$all_options->{$key} and $default eq '__NO_DEFAULT_OPTION_AVAILABLE__') {
1047 $class->diag_print ($verbose,
1048 "%s- Removing option '%s' (no default and no value)",
1049 $class->diag->indent, $key, $from);
1050 } else {
1051 $return->{$key} = $all_options->{$key};
1052 }
1053 }
1054 }
1055 return $return;
1056 }
1057  
1058 sub XML_from_Proto {
1059 # usage my $xmlstring =
1060 # XML::UTIL->XML_from_Proto($prefix, ' ', $tag, $protohashref);
1061 # This proc will compose XML from a proto hash in
1062 # Proto_from_XML's return format
1063 my ($class, $prefix, $tab, $tag, $proto) = @_;
1064 my $me = "$class->XML_from_Proto";
1065 my ($key, $val, $xml, $limit);
1066 my $typekey = &typeKey;
1067 my $prune = "*$typekey*$permitted_fields*";
1068 my $contents = '';
1069 my $newprefix = "$tab$prefix";
1070  
1071 # make up the start tag
1072 foreach $key (sort keys %$proto) {
1073 unless ($prune =~ /\*$key\*/) {
1074 if (ref $proto->{$key} eq 'ARRAY') {
1075 print "error- Key '$key' is an ARRAY !!! and has been ignored\n";
1076 next;
1077 } elsif (ref $proto->{$key}) {
1078 # call ourself to expand nested xml
1079 $contents .= "\n".
1080 $class->XML_from_Proto(
1081 $newprefix, $tab,
1082 ($proto->{$key}{$typekey} || $key),
1083 $proto->{$key}, $prune).
1084 "\n";
1085 } else {
1086 # this is a vanilla string so trim and add to output
1087 if (defined $proto->{$key}) {
1088 $contents .= "\n$newprefix<$key>".
1089 &QuoteXMLChars($proto->{$key})."</$key>";
1090 } else {
1091 $contents .= "\n$newprefix<$key></$key>";
1092 # $contents .= "\n$newprefix<$key />";
1093 }
1094 }
1095 }
1096 }
1097  
1098 # make up the string to return
1099 if ($contents eq '') {
1100 if ($tag ne '') {
1101 $xml .= "\n$prefix<$tag />";
1102 }
1103 } else {
1104 if ($tag ne '') {
1105 $xml .= "$prefix<$tag>$contents\n$prefix</$tag>";
1106 } else {
1107 $xml .= "\n$contents\n";
1108 }
1109 }
1110 return $xml
1111 }
1112
1113 =back
1114  
1115 =head1 SEE ALSO
1116  
1117 Glade::Two::Generate(3) glade2perl-2(1)
1118  
1119 =head1 AUTHOR
1120  
1121 Dermot Musgrove <dermot.musgrove@virgin.net>
1122  
1123 =cut
1124  
1125 1;
1126  
1127 __END__