File Coverage

blib/lib/Audio/GtkGramofile/Settings.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Audio::GtkGramofile::Settings;
2              
3 1     1   3217 use strict;
  1         3  
  1         39  
4 1     1   7 use warnings;
  1         2  
  1         92  
5 1     1   5 use Carp;
  1         3  
  1         103  
6              
7 1     1   434 use Glib 1.040, qw(TRUE FALSE);
  0            
  0            
8             use IO::File;
9             use Config::IniFiles;
10              
11             use vars qw($VERSION);
12             $VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); shift @r; sprintf("0.%04d",@r) }; # must be all one line, for MakeMaker
13              
14             sub new {
15             my $proto = shift;
16             my $args = shift;
17             my $class = ref($proto) || $proto;
18            
19             my $self = {};
20             bless $self, $class;
21            
22             return $self;
23             }
24              
25             sub gui {
26             my $self = shift;
27             my $gui = shift;
28              
29             $self->{gui} = $gui if defined $gui;
30             $self->{gui};
31             }
32              
33             sub signals {
34             my $self = shift;
35             my $signals = shift;
36              
37             $self->{signals} = $signals if defined $signals;
38             $self->{signals};
39             }
40              
41             sub callbacks {
42             my $self = shift;
43             my $callbacks = shift;
44              
45             $self->{callbacks} = $callbacks if defined $callbacks;
46             $self->{callbacks};
47             }
48              
49             sub logic {
50             my $self = shift;
51             my $logic = shift;
52              
53             $self->{logic} = $logic if defined $logic;
54             $self->{logic};
55             }
56              
57             sub load_settings {
58             my $self = shift;
59              
60             my $cfg_file = $ENV{HOME}."/.gtkgramofilerc";
61             my $handle;
62             if (-e $ENV{HOME}."/.gtkgramofilerc") {
63             $handle = IO::File->new($ENV{HOME}."/.gtkgramofilerc","r");
64             } else {
65             no strict 'refs';
66             my $data = __PACKAGE__ . '::DATA';
67             $handle = *$data;
68             }
69             my $cfg = Config::IniFiles->new(-file => $handle);
70              
71             foreach my $section ($cfg->Sections) {
72             foreach my $parameter ($cfg->Parameters($section)) {
73             my $val = $cfg->val( $section, $parameter);
74             $self->{defaults}->{$section}->{$parameter} = $val;
75             $self->{settings}->{$section}->{$parameter} = $val; # only set this if it changes during the program
76             }
77             }
78             }
79              
80             sub get_value {
81             my $self = shift;
82             my $section = shift;
83             my $parameter = shift;
84            
85             return (defined $self->{settings}->{$section}->{$parameter}) ?
86             $self->{settings}->{$section}->{$parameter} :
87             $self->{defaults}->{$section}->{$parameter};
88             }
89              
90             sub set_value {
91             my $self = shift;
92             my $section = shift;
93             my $parameter = shift;
94             my $value = shift;
95            
96             $self->{settings}->{$section}->{$parameter} = $value;
97             }
98              
99             sub get_defaults {
100             return shift->{defaults};
101             }
102              
103             sub get_default {
104             my $self = shift;
105             my $section = shift;
106             my $parameter = shift;
107            
108             return $self->{defaults}->{$section}->{$parameter};
109             }
110              
111             sub set_default {
112             my $self = shift;
113             my $section = shift;
114             my $parameter = shift;
115            
116             $self->{settings}->{$section}->{$parameter} = $self->{defaults}->{$section}->{$parameter};
117             }
118              
119             sub get_section_keys {
120             my $self = shift;
121             my $section = shift;
122              
123             return keys %{$self->{defaults}->{$section}};
124             }
125              
126             sub restore_settings {
127             shift->{settings} = undef;
128             }
129              
130             sub save_settings {
131             my $self = shift;
132            
133             my @defaults = qw(tracksplit_general process_general);
134             my @settings = qw(general tracksplit_params process_filters process_params);
135              
136             my $cfg_file = $ENV{HOME}."/.gtkgramofilerc";
137             my $cfg_fh = IO::File->new($cfg_file,"w") or croak "Can't get handle for $cfg_file, $!";
138              
139             foreach my $section (@defaults) {
140             print $cfg_fh "\n[$section]\n\n";
141             foreach my $parameter (keys %{$self->{defaults}->{$section}}) {
142             print $cfg_fh "$parameter = ",$self->{defaults}->{$section}->{$parameter},"\n";
143             }
144             }
145              
146             foreach my $section (@settings) {
147             print $cfg_fh "\n[$section]\n\n";
148             foreach my $parameter (keys %{$self->{defaults}->{$section}}) {
149             print $cfg_fh "$parameter = ";
150             print $cfg_fh (defined $self->{settings}->{$section}->{$parameter}) ?
151             $self->{settings}->{$section}->{$parameter} :
152             $self->{defaults}->{$section}->{$parameter};
153             print $cfg_fh "\n";
154             }
155             }
156              
157             close $cfg_fh or croak "Can't close filehandle for $cfg_file, $!";
158             }
159              
160             sub set_warning_text ($$) {
161             my $widget = shift;
162             my $warn = shift;
163             $widget->modify_text('normal', Gtk2::Gdk::Color->new($warn * 65535, 0, 0));
164             }
165              
166             1;
167              
168             __DATA__