#!/usr/bin/perl -w --
# Filename: galbum
# Author: David Ljung Madison <DaveSource.com>
# See License: http://MarginalHacks.com/License/
my $VERSION= '1.01';
#
# Description: GUI front-end to MarginalHacks album script
# Uses album output to create option pages
# (so new album options are automatically available to galbum)
# Uses configuration settings in album.conf files
#
# Bugs: Cannot remove array options (such as -lang, -exif, ..)
# This is due to the fact that album doesn't handle losing array
# options very well (due to the fact that they might have come
# from the global album.conf or a parent album, something we can't
# really change)
#
# Hotkey control-Q to quit usually only works with some delay
# between control and Q. wxPerl doesn't support FilterEvent?
#
##################################################
# generated by wxGlade 0.6.3 on Thu Jul 10 19:53:49 2008
# To get wxPerl visit http://wxPerl.sourceforge.net/
##################################################
use IO::File;
##################################################
# Issues
##################################################
# - Changes to array options are not handled well
# They are only additive. This is actually a
# difficulty in "album" itself
# - one_time options get cleared everytime we generate.
# Is this desirable?
##################################################
# Settings
##################################################
sub about {
<<ABOUT;
This is galbum v$VERSION on $^O
Running album v$MAIN::ALBUM_VERSION
Copyright: (c) 2008 David Ljung Madison
Docs: http://MarginalHacks.com/Hacks/album/
License: http://MarginalHacks.com/License/
Please see: http://MarginalHacks.com/Pay/
Description:
'galbum' is a graphical interface that makes it easy to run 'album'
'album' is an HTML photo album generator.
It can make and maintain beautiful photo albums for you to display
on your website on to burn to a CD/DVD. It does not host your
albums for you, you will need to upload them to a website for that.
It has many themes/skins and many options for building, as well
as international language support. See the docs for more info.
Bugs: Cannot remove array options (such as -lang, -exif, ..)
This is due to the fact that album can't easily clear array options.
ABOUT
}
# The executable
my @ALBUM = ('album');
$MAIN::ALBUM_VERSION= '(unknown)';
my $CONVERT = 'convert';
my $OSX = ($^O =~ /darwin/i) ? 1 : 0;
my $WINDOWS = (!$OSX && ($^O =~ /Win/i)) ? 1 : 0;
my $WIN2K = ($^O =~ /MSWin32/i) ? 1 : 0;
my $CYGWIN = ($^O =~ /cygwin/i) ? 1 : 0;
if ($WINDOWS) {
@ALBUM = ('perl','album');
eval "use Win32::API";
die "couldn't load module : $!n" if ($@);
eval "use Win32API::File";
die "couldn't load module : $!n" if ($@);
}
##################################################
# wxGlade generated code (intertwined with my code)
##################################################
use Wx 0.15 qw[:allclasses];
use strict;
##################################################
# The main frame
##################################################
package MyFrame;
use Wx qw[:everything];
use base qw(Wx::Frame);
use strict;
sub new {
my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;
$parent = undef unless defined $parent;
$id = -1 unless defined $id;
$title = "" unless defined $title;
$pos = wxDefaultPosition unless defined $pos;
$size = wxDefaultSize unless defined $size;
$name = "" unless defined $name;
# begin wxGlade: MyFrame::new
$style = wxDEFAULT_FRAME_STYLE
unless defined $style;
$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name );
$self->{notebook_1} = Wx::Notebook->new($self, -1, wxDefaultPosition, wxDefaultSize, 0);
$self->{aboutPanel} = Wx::Panel->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{moreMoreOptionsPanel} = Wx::ScrolledWindow->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
$self->{moreOptionsPanel} = Wx::ScrolledWindow->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
$self->{optionsPanel} = Wx::ScrolledWindow->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
$self->{themePanel} = Wx::ScrolledWindow->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
$self->{albumPanel} = Wx::Panel->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{chooseDirectory} = Wx::Button->new($self->{albumPanel}, -1, "Choose Directory..");
$self->{albumPath} = Wx::TextCtrl->new($self->{albumPanel}, -1, "<path to album directory>", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
$self->{generateAlbum} = Wx::Button->new($self->{albumPanel}, -1, "Generate Album");
$self->{albumProgress} = Wx::Gauge->new($self->{albumPanel}, -1, 100, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL|wxGA_SMOOTH);
$self->{checkboxDescend} = Wx::CheckBox->new($self->{albumPanel}, -1, "Do subalbums", wxDefaultPosition, wxDefaultSize, );
$self->{checkboxAddToParent} = Wx::CheckBox->new($self->{albumPanel}, -1, "Add this album to parent directory", wxDefaultPosition, wxDefaultSize, );
$self->{progressText} = Wx::StaticText->new($self->{albumPanel}, -1, "", wxDefaultPosition, wxDefaultSize, );
$self->{optionlangOption} = Wx::StaticText->new($self->{albumPanel}, -1, "Languages:", wxDefaultPosition, wxDefaultSize, );
$self->{optionlangValue} = Wx::ListBox->new($self->{albumPanel}, -1, wxDefaultPosition, wxDefaultSize, [], wxLB_MULTIPLE);
$self->{optionthemeOption} = Wx::StaticText->new($self->{themePanel}, -1, "Theme:", wxDefaultPosition, wxDefaultSize, );
$self->{optionthemeValue} = Wx::Choice->new($self->{themePanel}, -1, wxDefaultPosition, wxDefaultSize, ["No-Theme"], );
$self->{label_1} = Wx::StaticText->new($self->{optionsPanel}, -1, "Option", wxDefaultPosition, wxDefaultSize, );
$self->{label_2} = Wx::StaticText->new($self->{optionsPanel}, -1, "Setting", wxDefaultPosition, wxDefaultSize, );
$self->{label_3} = Wx::StaticText->new($self->{optionsPanel}, -1, "Usage", wxDefaultPosition, wxDefaultSize, );
$self->{static_line_1} = Wx::StaticLine->new($self->{optionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_2} = Wx::StaticLine->new($self->{optionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_3} = Wx::StaticLine->new($self->{optionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{label_1_copy} = Wx::StaticText->new($self->{moreOptionsPanel}, -1, "Option", wxDefaultPosition, wxDefaultSize, );
$self->{label_2_copy} = Wx::StaticText->new($self->{moreOptionsPanel}, -1, "Setting", wxDefaultPosition, wxDefaultSize, );
$self->{label_3_copy} = Wx::StaticText->new($self->{moreOptionsPanel}, -1, "Usage", wxDefaultPosition, wxDefaultSize, );
$self->{static_line_1_copy} = Wx::StaticLine->new($self->{moreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_2_copy} = Wx::StaticLine->new($self->{moreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_3_copy} = Wx::StaticLine->new($self->{moreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{label_1_copy_1} = Wx::StaticText->new($self->{moreMoreOptionsPanel}, -1, "Option", wxDefaultPosition, wxDefaultSize, );
$self->{label_2_copy_1} = Wx::StaticText->new($self->{moreMoreOptionsPanel}, -1, "Setting", wxDefaultPosition, wxDefaultSize, );
$self->{label_3_copy_1} = Wx::StaticText->new($self->{moreMoreOptionsPanel}, -1, "Usage", wxDefaultPosition, wxDefaultSize, );
$self->{static_line_1_copy_1} = Wx::StaticLine->new($self->{moreMoreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_2_copy_1} = Wx::StaticLine->new($self->{moreMoreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{static_line_3_copy_1} = Wx::StaticLine->new($self->{moreMoreOptionsPanel}, -1, wxDefaultPosition, wxDefaultSize, );
$self->{quitPanel} = Wx::Panel->new($self->{notebook_1}, -1, wxDefaultPosition, wxDefaultSize, );
$self->__set_properties();
$self->__do_layout();
Wx::Event::EVT_BUTTON($self, $self->{chooseDirectory}->GetId, \&onChooseDirectory);
Wx::Event::EVT_TEXT_ENTER($self, $self->{albumPath}->GetId, \&albumPathChanged);
Wx::Event::EVT_BUTTON($self, $self->{generateAlbum}->GetId, \&doGenerateAlbum);
Wx::Event::EVT_LISTBOX($self, $self->{optionlangValue}->GetId, \&langSelectionChanged);
Wx::Event::EVT_CHOICE($self, $self->{optionthemeValue}->GetId, \&themeSelectionChanged);
Wx::Event::EVT_NOTEBOOK_PAGE_CHANGED($self, $self->{notebook_1}->GetId, \&onNotebookChange);
# end wxGlade
# Get keys
Wx::Event::EVT_KEY_UP($self, \&onKeyUp);
Wx::Event::EVT_KILL_FOCUS($self->{albumPath}, \&albumPathChanged);
Wx::Event::EVT_SET_FOCUS($self->{albumPath}, \&albumPathEnter);
return $self;
}
sub __set_properties {
my $self = shift;
# begin wxGlade: MyFrame::__set_properties
$self->SetTitle("galbum - GUI photo album generator");
$self->SetSize(Wx::Size->new(650, 600));
$self->{albumPath}->SetMinSize(Wx::Size->new(400, 27));
$self->{albumPath}->SetToolTipString("The directory of photos to convert into an album.");
$self->{albumProgress}->SetMinSize(Wx::Size->new(400, 28));
$self->{albumProgress}->SetToolTipString("Progress thermometer for album generation");
$self->{checkboxDescend}->SetValue(1);
$self->{optionlangOption}->SetFont(Wx::Font->new(13, wxDEFAULT, wxNORMAL, wxNORMAL, 0, "Sans"));
$self->{optionlangValue}->SetMinSize(Wx::Size->new(140, 200));
$self->{optionlangValue}->SetSelection(0);
$self->{optionthemeOption}->SetMinSize(Wx::Size->new(85, 22));
$self->{optionthemeOption}->SetFont(Wx::Font->new(13, wxDEFAULT, wxNORMAL, wxNORMAL, 0, "Sans"));
$self->{optionthemeValue}->SetMinSize(Wx::Size->new(300, 27));
$self->{optionthemeValue}->SetSelection(0);
$self->{themePanel}->SetScrollRate(10, 10);
$self->{optionsPanel}->SetScrollRate(10, 10);
$self->{moreOptionsPanel}->SetScrollRate(10, 10);
$self->{moreMoreOptionsPanel}->SetScrollRate(10, 10);
# end wxGlade
}
sub __do_layout {
my $self = shift;
# begin wxGlade: MyFrame::__do_layout
$self->{sizer_1} = Wx::BoxSizer->new(wxHORIZONTAL);
$self->{aboutGrid} = Wx::FlexGridSizer->new(2, 2, 0, 0);
$self->{moreMoreOptionsGrid} = Wx::FlexGridSizer->new(3, 3, 4, 7);
$self->{moreOptionsGrid} = Wx::FlexGridSizer->new(3, 3, 4, 7);
$self->{optionsGrid} = Wx::FlexGridSizer->new(3, 3, 4, 7);
$self->{grid_sizer_4} = Wx::FlexGridSizer->new(2, 1, 10, 0);
$self->{themesGrid} = Wx::FlexGridSizer->new(1, 3, 18, 10);
$self->{grid_sizer_5} = Wx::FlexGridSizer->new(1, 2, 0, 0);
$self->{grid_sizer_1} = Wx::FlexGridSizer->new(6, 2, 5, 10);
$self->{grid_sizer_1}->Add(20, 100, 0, 0, 0);
$self->{grid_sizer_1}->Add(100, 20, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{chooseDirectory}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{albumPath}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{generateAlbum}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{albumProgress}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{checkboxDescend}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{checkboxAddToParent}, 0, 0, 0);
$self->{grid_sizer_1}->Add(40, 50, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{progressText}, 0, wxEXPAND, 2);
$self->{grid_sizer_1}->Add($self->{optionlangOption}, 0, 0, 0);
$self->{grid_sizer_1}->Add($self->{optionlangValue}, 0, 0, 0);
$self->{albumPanel}->SetSizer($self->{grid_sizer_1});
$self->{grid_sizer_5}->Add($self->{optionthemeOption}, 0, wxALIGN_CENTER_VERTICAL, 0);
$self->{grid_sizer_5}->Add($self->{optionthemeValue}, 0, 0, 0);
$self->{grid_sizer_4}->Add($self->{grid_sizer_5}, 1, wxEXPAND, 0);
$self->{grid_sizer_4}->Add($self->{themesGrid}, 1, wxEXPAND, 0);
$self->{themePanel}->SetSizer($self->{grid_sizer_4});
$self->{optionsGrid}->Add($self->{label_1}, 0, 0, 0);
$self->{optionsGrid}->Add($self->{label_2}, 0, 0, 0);
$self->{optionsGrid}->Add($self->{label_3}, 0, 0, 0);
$self->{optionsGrid}->Add($self->{static_line_1}, 0, wxEXPAND, 0);
$self->{optionsGrid}->Add($self->{static_line_2}, 0, wxEXPAND, 0);
$self->{optionsGrid}->Add($self->{static_line_3}, 0, wxEXPAND, 0);
$self->{optionsPanel}->SetSizer($self->{optionsGrid});
$self->{moreOptionsGrid}->Add($self->{label_1_copy}, 0, 0, 0);
$self->{moreOptionsGrid}->Add($self->{label_2_copy}, 0, 0, 0);
$self->{moreOptionsGrid}->Add($self->{label_3_copy}, 0, 0, 0);
$self->{moreOptionsGrid}->Add($self->{static_line_1_copy}, 0, wxEXPAND, 0);
$self->{moreOptionsGrid}->Add($self->{static_line_2_copy}, 0, wxEXPAND, 0);
$self->{moreOptionsGrid}->Add($self->{static_line_3_copy}, 0, wxEXPAND, 0);
$self->{moreOptionsPanel}->SetSizer($self->{moreOptionsGrid});
$self->{moreMoreOptionsGrid}->Add($self->{label_1_copy_1}, 0, 0, 0);
$self->{moreMoreOptionsGrid}->Add($self->{label_2_copy_1}, 0, 0, 0);
$self->{moreMoreOptionsGrid}->Add($self->{label_3_copy_1}, 0, 0, 0);
$self->{moreMoreOptionsGrid}->Add($self->{static_line_1_copy_1}, 0, wxEXPAND, 0);
$self->{moreMoreOptionsGrid}->Add($self->{static_line_2_copy_1}, 0, wxEXPAND, 0);
$self->{moreMoreOptionsGrid}->Add($self->{static_line_3_copy_1}, 0, wxEXPAND, 0);
$self->{moreMoreOptionsPanel}->SetSizer($self->{moreMoreOptionsGrid});
$self->{aboutGrid}->Add(100, 80, 0, 0, 0);
$self->{aboutGrid}->Add(5, 5, 0, 0, 0);
$self->{aboutPanel}->SetSizer($self->{aboutGrid});
$self->{notebook_1}->AddPage($self->{albumPanel}, "Album");
$self->{notebook_1}->AddPage($self->{themePanel}, "Theme");
$self->{notebook_1}->AddPage($self->{optionsPanel}, "Options");
$self->{notebook_1}->AddPage($self->{moreOptionsPanel}, "More options");
$self->{notebook_1}->AddPage($self->{moreMoreOptionsPanel}, "More more options");
$self->{notebook_1}->AddPage($self->{aboutPanel}, "About");
$self->{notebook_1}->AddPage($self->{quitPanel}, "Quit (^Q)");
$self->{sizer_1}->Add($self->{notebook_1}, 1, wxEXPAND, 0);
$self->SetSizer($self->{sizer_1});
$self->Layout();
$self->SetSize(Wx::Size->new(650, 600));
# end wxGlade
}
# Get the outer frame given any enclosed object
sub getFrame {
my ($self) = @_;
$self = $self->GetParent() while $self->GetParent();
$self;
}
sub tabAdd {
my ($self,$add) = @_;
my $tab = $self->{notebook_1}->GetSelection();
my $end = $self->{notebook_1}->GetPageCount();
$tab += $add;
# Skip the quit tab at the end ($end-1)
# (So actually this would break if $add > $end*2..)
$tab += $end-1 while $tab<0;
$tab -= $end-1 while $tab>=$end-1;
$self->{notebook_1}->SetSelection($tab);
#$self->{notebook_1}->AdvanceSelection();
}
# Frame: handle keypress
sub onKeyUp {
my ($self, $event) = @_;
my $key = $event->GetKeyCode;
# It seems like it should be easier than this...
QUIT($self) # Control-Q
if ($key==ord('Q')
&& Wx::GetKeyState(WXK_CONTROL)
&& !Wx::GetKeyState(WXK_SHIFT)
## Note that OSX doesn't have this (if we port)
&& !Wx::GetKeyState(WXK_ALT));
# 1,2 cycle through tabs (only if not in a TextCtrl)
if (($key==ord('1') || $key==ord('2')) && Wx::Window::FindFocus() !~ /TextCtrl/) {
my $add = ($key==ord('1')) ? -1 : +1;
tabAdd($self,$add);
}
}
##################################################
##################################################
# OPTIONS
##################################################
##################################################
# These need to match album
sub OPTION_SEP() { 1; } # Separator
sub OPTION_BOOL() { 2; }
sub OPTION_NUM() { 3; }
sub OPTION_STR() { 4; }
sub OPTION_ARR() { 5; } # Array of strings
my @IGNORE_OPTIONS = qw(q d D virgin_check configure theme lang windows cygwin hashes list_themes);
sub optionControls {
my ($option) = @_;
("option${option}Option","option${option}Value","option${option}Usage");
}
# Do we have the option in our frame?
sub hasOption {
my ($self,$option) = @_;
$self->{"option${option}Value"} ? 1 : 0;
}
# Get option value (returns array)
sub getOptionVal {
my ($self, $option) = @_;
my $opt = $self->{"option${option}Value"};
if (ref $opt eq 'Wx::ListBox') {
my @n = $opt->GetSelections();
return map { $opt->GetString($_) } @n;
}
my $val = (ref $opt eq 'Wx::Choice') ? $opt->GetString($opt->GetSelection()) : $opt->GetValue();
wantarray ? split(/\n/, $val) : $val;
}
sub setOptionVal {
my ($self, $option, @val) = @_;
return unless $self->{app}{d}{opt}{$option};
my $type = $self->{app}{d}{opt}{$option}{type};
return unless $type;
my $val = $type==OPTION_ARR ? join("\n",@val) : $val[0];
my $opt = $self->{"option${option}Value"};
return unless $opt;
if (ref $opt eq 'Wx::ListBox') {
for (my $num=$opt->GetCount()-1; $num>=0; $num--) {
my $item = $opt->GetString($num);
$self->{app}{d}{listbox}{lang}[$num] = grep($item eq $_, @val) ? 1 : 0;
$self->{app}{d}{listbox}{lang}[$num] ?
$self->{optionlangValue}->Select($num) :
$self->{optionlangValue}->Deselect($num);
}
return;
}
return $opt->SetValue($val) unless ref $opt eq 'Wx::Choice';
my $n = $opt->FindString($val);
return if $n==wxNOT_FOUND;
$opt->SetSelection($n);
}
# Frame: Find any differences between option values
sub diffOption {
my ($self,$option) = @_;
my $type = $self->{app}{d}{opt}{$option}{type};
return () unless $self->hasOption($option);
my @was = @{$self->{app}{d}{opt}{$option}{val}};
my @now = getOptionVal($self,$option);
# Checkboxes use 'undef' instead of 0 for some reason.
$now[0] = 0 if $type && $type==OPTION_BOOL && !$now[0];
# Special case. Consider () the same as (undef)
return () if $#$a==-1 && $#$b==0 && !$b->[0];
return () if $#$b==-1 && $#$a==0 && !$a->[0];
# Special case. Did we clear an option? Kludge: Return 0
return (0) if @was && $was[0] && !@now;
# Return any elements in @now not in @was
# (Ignores the problem of ARR options that lose some elements,
# but album doesn't really have a good solution for this
# without doing a clear_option - should we check for this??)
my @changed;
foreach my $now ( @now ) {
push(@changed, $now) unless grep($now eq $_, @was);
}
@changed;
}
# Frame: All the options that have been changed
sub changedOptions {
my ($self,@ignore) = @_;
my $d = $self->{app}{d};
return unless $d && $d->{opt};
my @changed;
foreach my $option ( keys %{$d->{opt}} ) {
next if grep($option eq $_, @ignore);
my @diff = diffOption($self,$option);
next unless @diff;
# Hack for 'No-Theme' theme
$diff[0]=0 if $option eq 'theme' && $diff[0] eq 'No-Theme';
# Value changed. Format it for output
if ($d->{opt}{$option}{type}==OPTION_BOOL) {
push(@changed, $diff[0] ? "-$option" : "-no_$option");
} elsif ($#diff==0 && $diff[0] eq '0') {
push(@changed,"-no_$option");
} else {
push(@changed,"-$option",0) unless @diff;
map { push(@changed,"-$option",$_ || 0) } @diff;
}
}
@changed;
}
sub setupProgressText {
my ($self) = @_;
# Resize progressText size and make it purple
my $ptFont = $self->{progressText}->GetFont();
$ptFont->SetPointSize($ptFont->GetPointSize()+1);
$self->{progressText}->SetFont($ptFont);
$self->{progressText}->SetForegroundColour(Wx::Colour->new(255,0,200));
}
my $UNBOUND_RANGE;
sub startUnboundProgress {
my ($self,$what) = @_;
print "$what\n";
$self->{progressText}->SetLabel($what);
$UNBOUND_RANGE = 3;
$self->updateUnboundProgress;
}
sub updateUnboundProgress() {
my ($self) = @_;
$self->{albumProgress}->SetRange($UNBOUND_RANGE+5);
$self->{albumProgress}->SetValue($UNBOUND_RANGE++);
Wx::Yield();
}
sub endUnboundProgress {
my ($self) = @_;
$self->{progressText}->SetLabel("");
$self->{albumProgress}->SetValue(0);
}
# Frame: The user entered something in an option
# Check if it changed and update the color
sub checkOption {
my ($self,$option) = @_;
my ($optTxt,undef,undef) = optionControls($option);
my @diff = diffOption($self,$option);
#my $lbl = @diff ? "!$option!" : $option;
$self->{$optTxt}->SetForegroundColour(@diff ? wxRED : wxNullColour);
# Also update the generateAlbum button if need be
if (@diff) {
$self->{generateAlbum}->SetForegroundColour(wxRED);
} else {
$self->{generateAlbum}->SetForegroundColour(wxNullColour)
unless ($self->changedOptions());
}
$self->Refresh();
}
# Frame: Add/setup the theme panel
sub setupThemes {
my ($self) = @_;
# For now..
return if $self->{app}{d}{themes};
my $dir = $self->getAlbumPath;
#########################
# Get all of the themes based on -theme_path and -data_path
#########################
$self->startUnboundProgress("Reading current themes");
my @cmd = (@ALBUM,'-crf','-list_themes');
push(@cmd, $dir) if $dir;
my ($fh) = openPipe(@cmd);
return unless $fh;
my ($imgSize,$txtSize) = (Wx::Size->new(180,200), Wx::Size->new(180,-1));
my $txtFont;
my %saw;
while (<$fh>) {
$self->updateUnboundProgress;
chomp;
$MAIN::ALBUM_VERSION = $1 if /crf:version:([^:]+):/i;
next unless /^crf:theme:\s*(.*?)([^\/]+)$/;
my ($path,$theme,$ptheme) = ($1.$2,$2,$2);
next if $saw{$theme}++;
push(@{$self->{app}{d}{themes}{paths}}, $path);
push(@{$self->{app}{d}{themes}{themes}}, $theme);
$ptheme =~ s/[_]/ /g;
$self->{optionthemeValue}->Append($theme);
# Make the thumbnail if we have the snapshot
if (-f "$path/snapshot.jpg" && !-f "$path/thumb.jpg") {
print STDERR "Generating theme thumbnail: $theme\n";
$self->{progressText}->SetLabel("Generating theme thumbnail: $theme");
Wx::Yield();
system($CONVERT,'-geometry','180x200',"$path/snapshot.jpg","$path/thumb.jpg");
$self->{progressText}->SetLabel("");
}
# Make the button/label
$self->{theme}{$theme}{grid} = Wx::FlexGridSizer->new(2, 1, 10, 0);
$self->{theme}{$theme}{label} = Wx::StaticText->new($self->{themePanel}, -1, "$ptheme", wxDefaultPosition, $txtSize, wxALIGN_CENTRE);
unless ($txtFont) {
$txtFont = $self->{theme}{$theme}{label}->GetFont();
$txtFont->SetPointSize($txtFont->GetPointSize()+1);
$txtFont->SetWeight(wxBOLD);
}
$self->{theme}{$theme}{label}->SetFont($txtFont);
if (-f "$path/thumb.jpg") {
$self->{theme}{$theme}{thumb} = Wx::BitmapButton->new($self->{themePanel}, -1, Wx::Bitmap->new("$path/thumb.jpg", wxBITMAP_TYPE_ANY), wxDefaultPosition, $imgSize);
$self->{theme}{$theme}{thumb}->SetLabel($theme);
} else {
$self->{theme}{$theme}{thumb} = Wx::Button->new($self->{themePanel}, -1, "$theme\n(no thumbnail)");
$self->{theme}{$theme}{thumb}->SetMinSize(Wx::Size->new(180, 200));
$self->{theme}{$theme}{thumb}->SetFont(Wx::Font->new(13, wxDEFAULT, wxNORMAL, wxNORMAL, 0, "Sans"));
}
$self->{theme}{$theme}{grid}->Add($self->{theme}{$theme}{thumb}, 0, wxADJUST_MINSIZE, 0);
Wx::Event::EVT_BUTTON($self, $self->{theme}{$theme}{thumb}->GetId, \&themeButton);
$self->{theme}{$theme}{grid}->Add($self->{theme}{$theme}{label}, 0, wxADJUST_MINSIZE, 0);
$self->{themesGrid}->Add($self->{theme}{$theme}{grid}, 1, wxEXPAND, 0);
$self->{theme}{$theme}{grid}->Layout(); # Update the layout
}
my $currTheme = $self->{app}{d}{opt}{theme}{val}[0];
setTheme($self, $currTheme) if $currTheme;
$self->endUnboundProgress;
# # Update the theme grid to update scrollbars
$self->{grid_sizer_4}->FitInside($self->{themePanel});
#$self->{themesGrid}->FitInside($self->{themePanel});
#$self->{themesGrid}->Layout(); # Update the layout
#$self->{grid_sizer_4}->Layout();
#$self->{themePanel}->Layout(); # Update the layout
$self->Layout(); # Update the layout
$self->Refresh();
close $fh;
}
# Frame: Add/setup the option pages
sub setupOptions {
my ($self,$path) = @_;
$self->{app}{d}{opt} = {}; # Clean opts
my $d = $self->{app}{d};
#########################
# Get all of the album options for a given album
#########################
$self->startUnboundProgress("Reading current options for [$path]");
my ($fh) = openPipe(@ALBUM,'-list_options',$path);
undef $self->{app}{opts}; # Start clean
my %saw;
while (<$fh>) {
$self->updateUnboundProgress;
if (/Can't find directory/) {
$self->{progressText}->SetLabel($_);
Wx::LogMessage($_);
print;
}
next unless /^crf:option:(\d+):(\d+):([^:]+):"([^"]+)":(.*)/;
my ($lvl,$type,$option,$usage,$val) = ($1,$2,$3,$4,$5);
next if $lvl>3;
my $deep = $lvl==1 ? "options" : $lvl==2 ? "moreOptions" : "moreMoreOptions";
push(@{$self->{app}{deep}{$deep}}, $option)
unless $saw{$option}++;
$d->{opt}{$option}{lvl} = $lvl;
$d->{opt}{$option}{type} = $type;
$d->{opt}{$option}{usage} = $usage;
push(@{$d->{opt}{$option}{val}}, $val);
}
close $fh;
$self->endUnboundProgress;
#########################
# Themes/Langs
#########################
setupThemes($self);
setOptionVal($self,'lang',@{$d->{opt}{lang}{val}}) if $path;
#########################
# If we've already created the forms, just update the values
#########################
if ($self->{app}{setupOptions}++) {
foreach my $option ( keys %{$d->{opt}} ) {
my ($optTxt,$valCtrl,undef) = optionControls($option);
next unless $self->hasOption($option);
my $val = $d->{opt}{$option}{val};
setOptionVal($self,$option,@$val);
$self->{$optTxt}->SetForegroundColour(wxNullColour);
}
$self->Refresh();
return;
}
#########################
# First time, create the forms
#########################
my $usageFont = undef;
my $strSize = Wx::Size->new(150,-1);
my $arrSize = Wx::Size->new(150,40);
foreach my $deep (qw(options moreOptions moreMoreOptions)) {
my $grid = $deep."Grid";
my $panel = $deep."Panel";
foreach my $option ( @{$self->{app}{deep}{$deep}} ) {
next if grep($_ eq $option, @IGNORE_OPTIONS);
my ($optTxt,$valCtrl,$usageTxt) = optionControls($option);
my $type = $d->{opt}{$option}{type};
# The option text
$self->{$optTxt} = Wx::StaticText->new($self->{$panel}, -1, $option, wxDefaultPosition, wxDefaultSize, );
# The value
my $val = $d->{opt}{$option}{val};
$val = $type==OPTION_ARR ? join("\n",@$val) : $val->[0];
# The input/checkbox
$self->{$valCtrl} = $type==OPTION_BOOL ?
Wx::CheckBox->new($self->{$panel}, -1, "", wxDefaultPosition, wxDefaultSize, ) :
$type==OPTION_ARR ?
Wx::TextCtrl->new($self->{$panel}, -1, $val, wxDefaultPosition, $arrSize, wxTE_MULTILINE|wxVSCROLL) :
Wx::TextCtrl->new($self->{$panel}, -1, $val, wxDefaultPosition, $strSize, );
$self->{$valCtrl}->SetValue(1) if $type==OPTION_BOOL && $val;
# Callbacks for enter/leave to check if it's changed
if ($type==OPTION_BOOL) {
Wx::Event::EVT_CHECKBOX($self, $self->{$valCtrl}->GetId, sub { checkOption($self,$option) });
} else {
Wx::Event::EVT_TEXT_ENTER($self, $self->{$valCtrl}->GetId, sub { checkOption($self,$option) });
Wx::Event::EVT_KILL_FOCUS($self->{$valCtrl}, sub { checkOption($self,$option) });
}
# Usage and lay it all out
$self->{$usageTxt} = Wx::StaticText->new($self->{$panel}, -1, $d->{opt}{$option}{usage}, wxDefaultPosition, wxDefaultSize, );
$self->{$usageTxt}->Wrap(700);
unless ($usageFont) {
$usageFont = $self->{$usageTxt}->GetFont();
$usageFont->SetPointSize(8);
# Note that OSX may not have italics (if we port)
$usageFont->SetStyle(wxFONTSTYLE_ITALIC);
}
$self->{$usageTxt}->SetFont($usageFont);
$self->{$grid}->Add($self->{$optTxt}, 0, wxADJUST_MINSIZE, 0);
$self->{$grid}->Add($self->{$valCtrl}, 0, wxADJUST_MINSIZE, 0);
$self->{$grid}->Add($self->{$usageTxt}, 0, wxADJUST_MINSIZE, 0);
}
# To resize the layout (for scrollbars)
$self->{$grid}->FitInside($self->{$panel});
$self->{$panel}->Layout(); # Update the layout
$self->{$grid}->Layout(); # Update the layout
}
$self->Layout();
$self->Refresh(); # Doesn't seem to make the scrollbars show up!
}
# Frame: Language options
# For now, just do it once when we build the app - we may have
# extra languages defined by an albums album.conf, but 'album -list_langs'
# doesn't currently show those anyways...
sub buildLangs {
my ($self) = @_;
#########################
# Get all of the langs based on -theme_path and -data_path
#########################
$self->startUnboundProgress("Reading current languages");
my @cmd = (@ALBUM,'-crf','-list_langs');
#push(@cmd, $dir) if $dir;
my ($fh) = openPipe(@cmd);
return unless $fh;
while (<$fh>) {
$self->updateUnboundProgress;
chomp;
$MAIN::ALBUM_VERSION = $1 if /crf:version:([^:]+):/i;
next unless /^crf:lang:\s*(\S+)$/;
my $lang = $1;
push(@{$self->{app}{d}{langs}}, $lang);
}
$self->{optionlangValue}->InsertItems(\@{$self->{app}{d}{langs}},0);
$self->endUnboundProgress;
$self->Refresh();
close $fh;
}
##################################################
##################################################
# ALBUM GENERATION
##################################################
##################################################
# Frame: Get the current album path
sub getAlbumPath {
my ($self) = @_;
my $dir = $self->{albumPath}->GetValue();
return '' if $dir eq "<path to album directory>";
$dir;
}
# Frame: Set the album path (and update the options)
sub newAlbumPath {
my ($self, $path) = @_;
($path) ?
$self->{albumPath}->SetValue($path) :
($path = $self->{albumPath}->GetValue());
return if $self->{app}{album} && $path eq $self->{app}{album};
$self->{progressText}->SetLabel("");
$self->{albumProgress}->SetValue(0);
$self->{app}{album} = $path;
$self->setupOptions($path) if $path;
}
# Frame: Choose an album dialog box
sub onChooseDirectory {
my ($self, $event) = @_;
# wxGlade: MyFrame::onChooseDirectory <event_handler>
my $dir = $self->getAlbumPath;
#$dir .= '/..' if $dir;
my $fd = Wx::DirDialog->new($self, "Choose a directory", $dir);
if ($fd->ShowModal == wxID_CANCEL) {
#Wx::LogMessage("User cancelled dialog");
} else {
my $path = $fd->GetPath;
$fd->Destroy;
$self->newAlbumPath($path);
}
# end wxGlade
}
# Frame: We enter the album path textCtrl
sub albumPathEnter {
my ($text, $event) = @_;
# First time we enter, make sure we clear the "<type here..>" message
my $self = getFrame($text);
my $path = $self->getAlbumPath();
$self->{albumPath}->SetValue("") unless $path;
}
# Frame: We saw the albumPath text string change
sub albumPathChanged {
my ($text, $event) = @_;
# wxGlade: MyFrame::albumPathChanged <event_handler>
getFrame($text)->newAlbumPath();
# end wxGlade
}
##################################################
# Running album: progress bar / job control
##################################################
# Frame: Kills a currently running album
my $GENERATING = 0;
sub stopAlbumProcess {
my ($self) = @_;
return unless $GENERATING;
print STDERR "Killing album process [$GENERATING]\n";
# this probably needs to be updated to use Win32::Process on Win machines
if (kill 9, $GENERATING) {
$GENERATING = 0;
$self->{generateAlbum}->SetLabel("Generate Album");
$self->{albumProgress}->SetValue(0);
$self->{progressText}->SetLabel("Aborted - album may be corrupt - regenerate to fix");
Wx::LogMessage("Aborted album generation\n\nAlbum may be corrupt.\nRegenerate to fix.\n");
$self->{generateAlbum}->SetForegroundColour(wxRED);
}
# Nothing if we failed
}
#########################
# Stolen from album
#########################
sub file_quote {
my ($file) = @_;
return $file if $CYGWIN;
$WINDOWS ? "\"$file\"" : "\Q$file\E";
}
sub openPipe {
my ($cmd,@args) = @_;
#print STDERR "run: $cmd @args\n" if $opt->{d};
my $fh = new IO::File;
my $qcmd = file_quote($cmd);
@args = map { file_quote($_) } @args;
# Happy Unix
return ($fh,open($fh, "$qcmd @args 2>&1 |"));# unless $WINDOWS;
# Windows2000,XP: -| pipe method, doesn't seem to work on Win98
# (Only works under Cygwin??)
my $pid = (open($fh,"-|"));
return undef unless defined $pid; # Failed
return ($fh,$pid) if $pid; # Parent
# Child
(open(STDERR,">&STDOUT")) || return undef;
exec($cmd,@args);
}
# An non-blocking filehandle read that returns an array of lines read
# Returns: ($eof,@lines)
my %nonblockGetLines_last;
sub nonblockGetLines_UNIX {
my ($fh,$timeout) = @_;
$timeout = 0 unless defined $timeout;
my $rfd = '';
$nonblockGetLines_last{$fh} = ''
unless defined $nonblockGetLines_last{$fh};
vec($rfd,fileno($fh),1) = 1;
return unless select($rfd, undef, undef, $timeout)>=0;
return unless vec($rfd,fileno($fh),1); # I'm not sure this is needed??
my $buf = '';
my $n = sysread($fh,$buf,1024*1024);
# If we're done, make sure to send the last unfinished line
return (1,$nonblockGetLines_last{$fh}) unless $n;
# Prepend the last unfinished line
$buf = $nonblockGetLines_last{$fh}.$buf;
# And save any newly unfinished lines
$nonblockGetLines_last{$fh} =
(substr($buf,-1) !~ /[\r\n]/ && $buf =~ s/([^\r\n]*)$//) ? $1 : '';
$buf ? (0,split(/\n/,$buf)) : (0);
}
my $peekNamedPipe = $WINDOWS ? Win32::API->Import('kernel32','PeekNamedPipe','LPLPPP','L') : undef;
sub nonblockGetLines_WINDOWS {
my ($fh,$timeout) = @_;
# We don't use the timeout..
# Thanks to perlmonks for the code
my $osfh = Win32API::File::GetOsFHandle( $fh ) or die $^E;
my( $bufsize, $buffer, $cAvail, $read ) = ( 1024, chr(0)x1024, 0, 0 );
PeekNamedPipe( $osfh, $buffer, $bufsize, $read, $cAvail, 0 )
or $^E == 109 or die $^E;
return (1) if $^E == 109;
my $eolPos = 1+index $buffer, $/;
return (0) unless $eolPos;
sysread( $fh, $buffer, $eolPos ) or die $!;
return (0) unless $buffer;
return (0,$buffer);
}
sub nonblockGetLines {
$WINDOWS ? nonblockGetLines_WINDOWS(@_) : nonblockGetLines_UNIX(@_);
}
sub absPath {
my ($dir) = @_;
my $save=`pwd`; chomp($save);
if (!chdir($dir)) {
Wx::LogMessage("Couldn't find directory ~[[_1]~]", $dir);
return undef;
}
my $name=`pwd`; chomp($name);
chdir($save);
$name;
}
#########################
# Frame: Runs album and watches the album output
#########################
sub doGenerateAlbum {
my ($self, $event) = @_;
# wxGlade: MyFrame::doGenerateAlbum <event_handler>
return $self->stopAlbumProcess if $GENERATING;
# We don't need the button to be red anymore
$self->{generateAlbum}->SetForegroundColour(wxNullColour);
onChooseDirectory($self) unless $self->getAlbumPath;
my $dir = $self->getAlbumPath;
return Wx::LogMessage("No album directory specified") unless $dir;
return Wx::LogMessage("Album directory not found:\n $dir") unless -d $dir;
print "Generate: $dir\n";
# Figure out the album command
my @cmd = (@ALBUM,'-crf');
my @ignore = ();
my $depth = $self->{checkboxDescend}->GetValue();
push(@ignore,'depth') unless $depth;
push(@cmd,'-depth','1') unless $depth;
my $addToParent = $self->{checkboxAddToParent}->GetValue();
if ($addToParent && (my $abs = absPath($dir))) {
if ($abs =~ m|(.+)/([^/]+)$|) {
($dir, my $add) = ($1,$2);
push(@ignore,'add');
push(@cmd, '-add', $add);
}
}
push(@cmd,$dir);
push(@cmd, $self->changedOptions(@ignore));
# Start up the album process
$self->{generateAlbum}->SetLabel("Stop generating");
#print "CMD @cmd\n";
(my $fh, $GENERATING) = openPipe(@cmd);
while ($GENERATING) {
Wx::Yield();
return if $self->{app}{QUIT}; # In case we died.
return unless $GENERATING; # Or stopped generating
# See if album has any output to parse
my ($eof,@lines) = nonblockGetLines($fh,.25);
foreach my $line ( @lines ) {
($self->{albumProgress}->SetRange($2),$self->{albumProgress}->SetValue($1))
if $line =~ m|^crf:hashes:\s*(\d+)/(\d+)|;
($self->{progressText}->SetLabel($1), print "Album: $1\n")
if $line =~ m|^crf:start_hashes:\s*(\S.*?)\s*$|;
}
$GENERATING=0 if $eof;
close $fh if $eof;
}
# Todo - set a timer to clear this?
$self->{progressText}->SetLabel("Album generation complete");
$self->{generateAlbum}->SetLabel("Generate Album");
# Make sure we have the new options as the "was" for changedOptions
$self->setupOptions($dir);
# Clear the progressText in 2000ms
my $timer = Wx::Timer->new($self,1); # Slight memory leak?
$timer->Start(2400,1);
Wx::Event::EVT_TIMER($self, 1, sub {
return if $self->{progressText}->GetLabel() ne "Album generation complete";
# Slight race condition here..
$self->{progressText}->SetLabel("");
$self->{albumProgress}->SetValue(0);
});
# end wxGlade
}
##################################################
# Quitting
##################################################
sub QUIT() {
my ($self) = @_;
# Check for changes and get confirmation?
if ($self->changedOptions()) {
my $answer = Wx::MessageBox("Album options have changed. Do you want to quit without generating the new album?", "Quit?", Wx::wxYES, undef);
unless ($answer == Wx::wxYES) {
$self->{notebook_1}->SetSelection(0)
if ($self->{notebook_1}->GetSelection()==$self->{notebook_1}->GetPageCount()-1);
return;
}
}
$self->stopAlbumProcess;
$self->{app}{QUIT}=1;
$self->Close(1);
}
sub onNotebookChange {
my ($self, $event) = @_;
# wxGlade: MyFrame::onNotebookChange <event_handler>
my $s = $event->GetSelection();
my $n = $self->{notebook_1}->GetPageCount();
# We need to select an album before dealing with themes/options
if ($s>=1 && $s<=4 && !$self->getAlbumPath) {
$self->{progressText}->SetLabel("SELECT AN ALBUM (before dealing with themes/options)");
return $self->{notebook_1}->SetSelection(0);
}
# Last 'button'/frame is Quit.
return unless $s==$n-1;
$self->QUIT;
# end wxGlade
}
sub aboutText {
my ($self,$msg) = @_;
## The old way, just one aboutText object
#return $self->{aboutText}->SetLabel($msg) if $self->{aboutText};
return if $self->{aboutText};
# Split links out of message so we can make them hyperlinks
my @msg = split(/(https?:\/\/\S+)/, $msg);
foreach my $piece ( @msg ) {
my $text;
$piece =~ s/^[\s\n\r]+//g;
$piece =~ s/[\s\n\r]+$//g;
if ($piece =~ /^https?:\/\/\S/) {
$text = Wx::HyperlinkCtrl->new($self->{aboutPanel}, -1, " $piece ", $piece, wxDefaultPosition, wxDefaultSize, );
} else {
$text = Wx::StaticText->new($self->{aboutPanel}, -1, $piece, wxDefaultPosition, wxDefaultSize, );
}
$self->{aboutGrid}->Add(5, 5, 0, 0, 0); # Spacer (left column)
$self->{aboutGrid}->Add($text, 0, 0, 0);
}
}
# Frame: Set the theme if we haven't already
my $lastTheme = "";
sub setTheme {
my ($self, $theme) = @_;
$theme = $self->getOptionVal('theme') unless $theme;
return 0 if $theme eq $lastTheme;
$self->setOptionVal('theme',$theme);
checkOption($self,'theme');
$lastTheme = $theme;
1;
}
# Frame: One of the theme buttons was pressed
sub themeButton {
my ($self, $event) = @_;
my $theme = $event->GetEventObject()->GetLabel();
$theme =~ s/\n.*//m;
setTheme($self,$theme);
}
# Frame: The theme selector changed
sub themeSelectionChanged {
my ($self, $event) = @_;
# wxGlade: MyFrame::themeSelectionChanged <event_handler>
return unless setTheme($self);
# Make sure it's a legal theme
my $theme = $self->getOptionVal('theme');
return unless $theme;
return if grep($theme eq $_, @{$self->{app}{d}{themes}{themes}});
return if $theme eq 'No-Theme';
print "Internal oops: [$theme] and No-Theme\n";
Wx::LogMessage("Not a theme [$theme]");
# end wxGlade
}
# Frame: The language selector changed
sub langSelectionChanged {
my ($self, $event) = @_;
# wxGlade: MyFrame::langSelectionChanged <event_handler>
# Handle selection
# The wxLB_MULTIPLE doesn't seem to be working right on GTK,
# so I'll just do the select/deselect myself... bah.
my $num = $event->GetInt();
my $lang = $self->{optionlangValue}->GetString($num);
$self->{app}{d}{listbox}{lang}[$num] = $self->{app}{d}{listbox}{lang}[$num] ? 0 : 1;
# reselect everything else
for (my $i=0; $i<=$#{$self->{app}{d}{listbox}{lang}}; $i++) {
$self->{optionlangValue}->Select($i)
if $self->{app}{d}{listbox}{lang}[$i];
}
# Do the current selection last so we leave on that focus.
# (unfortunately this is jumpy. Damn wxLB_MULTIPLE!)
$self->{app}{d}{listbox}{lang}[$num] ?
$self->{optionlangValue}->Select($num) :
$self->{optionlangValue}->Deselect($num);
my @s = $self->{optionlangValue}->GetSelections;
checkOption($self,'lang');
# end wxGlade
}
# end of class MyFrame
1;
##################################################
##################################################
# Glade: Application startup
##################################################
##################################################
package MyApp;
use base qw(Wx::App);
use strict;
sub OnInit {
my( $self ) = shift;
Wx::InitAllImageHandlers();
my $frame_1 = MyFrame->new();
$self->SetTopWindow($frame_1);
$frame_1->Show(1);
#$frame_1->SetFocus; # So we get keys
$frame_1->{notebook_1}->SetFocus; # For some reason it needs to be this
$frame_1->{app} = $self;
$frame_1->setupProgressText;
$frame_1->buildLangs();
# Kludge for now - need to parseArgs eventually
$frame_1->newAlbumPath($ARGV[0]) if @ARGV && -d $ARGV[0];
$self->{frame_1} = $frame_1; # So we can find the frame
return 1;
}
# end of class MyApp
package main;
sub main {
my $app = MyApp->new();
my $frame = $app->{frame_1};
# About text
$frame->aboutText(about());
$app->MainLoop();
} main();