| 1 | package Tk::AppWindow::Ext::ConfigFolder; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow::Ext::ConfigFolder - save your settings files in a ConfigFolder | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION="0.02"; | ||
| 13 | |||
| 14 | use File::Path qw(make_path); | ||
| 15 | use Config; | ||
| 16 | my $mswin = 0; | ||
| 17 | $mswin = 1 if $Config{'osname'} eq 'MSWin32'; | ||
| 18 | |||
| 19 | |||
| 20 | use base qw( Tk::AppWindow::BaseClasses::Extension ); | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | my $app = new Tk::AppWindow(@options, | ||
| 25 | -extensions => ['ConfigFolder'], | ||
| 26 | ); | ||
| 27 | $app->MainLoop; | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | =head1 CONFIG VARIABLES | ||
| 32 | |||
| 33 | =over 4 | ||
| 34 | |||
| 35 | =item Switch: B<-configfolder> I<hookable> | ||
| 36 | |||
| 37 | The default value depends on your operating system. | ||
| 38 | |||
| 39 | On Windows: $ENV{LOCALAPPDATA}/appname | ||
| 40 | Others: $ENV{HOME}/.local/share/appname | ||
| 41 | |||
| 42 | You can overwrite it at launch by setting a folder yourself. | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | my $configfolder; | ||
| 49 | if ($Config{osname} eq 'MSWin32') { | ||
| 50 | $configfolder = $ENV{LOCALAPPDATA} . '\\' | ||
| 51 | } else { | ||
| 52 | $configfolder = $ENV{HOME} . '/.local/share/' | ||
| 53 | } | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my $class = shift; | ||
| 57 | my $self = $class->SUPER::new(@_); | ||
| 58 | |||
| 59 | $self->configInit( | ||
| 60 | -configfolder => ['ConfigFolder', $self, $configfolder . $self->configGet('-appname')], | ||
| 61 | ); | ||
| 62 | |||
| 63 | return $self; | ||
| 64 | } | ||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | =over 4 | ||
| 69 | |||
| 70 | =item B<confFileName>I<($file)> | ||
| 71 | |||
| 72 | Prepends the config folder location to $file and returns it. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub confFileName { | ||
| 77 | my ($self, $file) = @_; | ||
| 78 | if ($mswin) { | ||
| 79 | return $self->ConfigFolder . "\\$file" | ||
| 80 | } else { | ||
| 81 | return $self->ConfigFolder . "/$file" | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | =item B<confFileName>I<($file)> | ||
| 86 | |||
| 87 | Returns true if $file exists in the config folder. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub confExists { | ||
| 92 | my ($self, $file) = @_; | ||
| 93 | my $target = $self->confFileName($file); | ||
| 94 | return -e $target; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub ConfigFolder { | ||
| 98 | my $self = shift; | ||
| 99 | if (@_) { $self->{CONFIGFOLDER} = shift } | ||
| 100 | my $f = $self->{CONFIGFOLDER}; | ||
| 101 | unless (-e $f) { | ||
| 102 | unless (make_path($f)) { | ||
| 103 | die "Could not create path $f"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return $self->{CONFIGFOLDER} | ||
| 107 | } | ||
| 108 | |||
| 109 | =item B<loadHash>I<($file, $id)> | ||
| 110 | |||
| 111 | Loads the hash stored in $file. $id is the file id on the first line of the file it should match. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub loadHash { | ||
| 116 | my ($self, $file, $id) = @_; | ||
| 117 | my $target = $self->confFileName($file); | ||
| 118 | my %hash = (); | ||
| 119 | if (open(OFILE, "<", $target)) { | ||
| 120 | my $fid = <OFILE>; | ||
| 121 | chomp $fid; | ||
| 122 | if ($fid eq $id) { | ||
| 123 | while (<OFILE>) { | ||
| 124 | my $l = $_; | ||
| 125 | chomp($l); | ||
| 126 | if ($l =~ s/^([^=]+)=//) { | ||
| 127 | my $key = $1; | ||
| 128 | $hash{$key} = $l; | ||
| 129 | } else { | ||
| 130 | warn "Error in format of file $file\n"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } else { | ||
| 134 | warn "File id does not match for $file" | ||
| 135 | } | ||
| 136 | close OFILE; | ||
| 137 | } else { | ||
| 138 | warn "cannot open file $file\n"; | ||
| 139 | } | ||
| 140 | return %hash | ||
| 141 | } | ||
| 142 | |||
| 143 | =item B<loadList>I<($file, $id)> | ||
| 144 | |||
| 145 | Loads the list stored in $file. $id is the file id on the first line of the file it should match. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub loadList { | ||
| 150 | my ($self, $file, $id) = @_; | ||
| 151 | my $target = $self->confFileName($file); | ||
| 152 | my @list = (); | ||
| 153 | if (open(OFILE, "<", $target)) { | ||
| 154 | my $fid = <OFILE>; | ||
| 155 | chomp $fid; | ||
| 156 | if ($fid eq $id) { | ||
| 157 | while (<OFILE>) { | ||
| 158 | my $l = $_; | ||
| 159 | chomp($l); | ||
| 160 | push @list, $l | ||
| 161 | } | ||
| 162 | } else { | ||
| 163 | warn "File id does not match for $file" | ||
| 164 | } | ||
| 165 | close OFILE; | ||
| 166 | } else { | ||
| 167 | warn "cannot open file $file\n"; | ||
| 168 | } | ||
| 169 | return @list | ||
| 170 | } | ||
| 171 | |||
| 172 | =item B<loadSectionedList>I<($file, $id)> | ||
| 173 | |||
| 174 | Loads the sectioned list stored in $file. $id is the file id on the first line of the file it should match. | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub loadSectionedList { | ||
| 179 | my ($self, $file, $id) = @_; | ||
| 180 | my $target = $self->confFileName($file); | ||
| 181 | my @list = (); | ||
| 182 | if (open(OFILE, "<", $target)) { | ||
| 183 | my $section; | ||
| 184 | my %inf = (); | ||
| 185 | my $fid = <OFILE>; | ||
| 186 | chomp $fid; | ||
| 187 | if ($fid eq $id) { | ||
| 188 | while (<OFILE>) { | ||
| 189 | my $line = $_; | ||
| 190 | chomp $line; | ||
| 191 | if ($line =~ /^\[([^\]]+)\]/) { #new section | ||
| 192 | # print "new section $1\n"; | ||
| 193 | if (defined $section) { | ||
| 194 | # print "pushing $section\n"; | ||
| 195 | my %o = %inf; | ||
| 196 | push @list, [$section, \%o]; | ||
| 197 | } | ||
| 198 | $section = $1; | ||
| 199 | %inf = (); | ||
| 200 | } elsif ($line =~ s/^([^=]+)=//) { #new key | ||
| 201 | $inf{$1} = $line; | ||
| 202 | } else { | ||
| 203 | warn "Error in format of file $file\n"; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } else { | ||
| 207 | warn "File id does not match for $file" | ||
| 208 | } | ||
| 209 | if ((%inf) and (defined $section)) { | ||
| 210 | push @list, [$section, \%inf]; | ||
| 211 | } | ||
| 212 | close OFILE; | ||
| 213 | } else { | ||
| 214 | warn "cannot open file $file\n"; | ||
| 215 | } | ||
| 216 | return @list | ||
| 217 | } | ||
| 218 | |||
| 219 | =item B<saveHash>I<($file, $id, %hash)> | ||
| 220 | |||
| 221 | Saves %hash to $file. $id is the file id and is written as the first line. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub saveHash { | ||
| 226 | my ($self, $file, $id, %hash) = @_; | ||
| 227 | my $target = $self->confFileName($file); | ||
| 228 | if (open(OFILE, ">", $target)) { | ||
| 229 | print OFILE $id . "\n"; | ||
| 230 | for (sort keys %hash) { | ||
| 231 | my $key = $_; | ||
| 232 | my $val = $hash{$key}; | ||
| 233 | print OFILE "$key=$val\n" | ||
| 234 | } | ||
| 235 | close OFILE | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | =item B<saveList>I<($file, $id, @list)> | ||
| 240 | |||
| 241 | Saves @list to $file. $id is the file id and is written as the first line. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub saveList { | ||
| 246 | my ($self, $file, $id, @list) = @_; | ||
| 247 | my $target = $self->confFileName($file); | ||
| 248 | if (open(OFILE, ">", $target)) { | ||
| 249 | print OFILE $id . "\n"; | ||
| 250 | for (@list) { print OFILE $_ . "\n" } | ||
| 251 | close OFILE | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | =item B<saveSectionedList>I<($file, $id, @list)> | ||
| 256 | |||
| 257 | Saves @list as a sectioned list $file. $id is the file id and is written as the first line. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub saveSectionedList { | ||
| 262 | my ($self, $file, $id, @list) = @_; | ||
| 263 | my $target = $self->confFileName($file); | ||
| 264 | if (open(OFILE, ">", $target)) { | ||
| 265 | print OFILE $id . "\n"; | ||
| 266 | for (@list) { | ||
| 267 | my ($section, $hash) = @$_; | ||
| 268 | print OFILE "[$section]\n"; | ||
| 269 | my %h = %$hash; | ||
| 270 | for (sort keys %h) { | ||
| 271 | my $key = $_; | ||
| 272 | my $val = $h{$key}; | ||
| 273 | print OFILE "$key=$val\n" | ||
| 274 | } | ||
| 275 | } | ||
| 276 | close OFILE | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | =back | ||
| 281 | |||
| 282 | =head1 AUTHOR | ||
| 283 | |||
| 284 | Hans Jeuken (hanje at cpan dot org) | ||
| 285 | |||
| 286 | =head1 BUGS | ||
| 287 | |||
| 288 | Unknown. If you find any, please contact the author. | ||
| 289 | |||
| 290 | =head1 SEE ALSO | ||
| 291 | |||
| 292 | =over 4 | ||
| 293 | |||
| 294 | =item L<Tk::AppWindow> | ||
| 295 | |||
| 296 | =item L<Tk::AppWindow::BaseClasses::Extension> | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | |||
| 307 |