From 49661f954b310825578f719562ace9f89527d3c1 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Mon, 22 Sep 2008 20:51:46 +1200 Subject: [PATCH] Add a config file and start to use it. --- Display.yml | 9 +++++++++ lib/Display/Config.pm | 33 +++++++++++++++++++++++++++++++++ lib/Display/Plugin.pm | 3 ++- lib/Display/Plugins/FSpot.pm | 4 ++-- picture.pl | 31 +++++++++++++++++++++++-------- 5 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 Display.yml create mode 100644 lib/Display/Config.pm diff --git a/Display.yml b/Display.yml new file mode 100644 index 0000000..62e3513 --- /dev/null +++ b/Display.yml @@ -0,0 +1,9 @@ +background: FSpot +enabled: + - FSpot + - Clock + - MPD +plugins: + FSpot: + DB: /home/andrew/.gnome2/f-spot/photos.db + Tags: Brooke diff --git a/lib/Display/Config.pm b/lib/Display/Config.pm new file mode 100644 index 0000000..ca43adc --- /dev/null +++ b/lib/Display/Config.pm @@ -0,0 +1,33 @@ +package Display::Config; + +use Carp; +use Config::Auto qw/find_file/; +use FindBin qw/$Bin/; +use YAML::Syck; + +use constant config_file => "Display.yml"; + +sub new { + my $proto = shift; + my $class = ref($proto) || $proto; + + my $self = {}; + + my $config = Config::Auto::find_file(config_file(), "$Bin/../etc"); + if (! config || $config eq "") { + croak "Failed to find a config file. Looking for ". config_file() . "."; + } + + warn "Loading $config\n"; + my $conf = eval { LoadFile $config }; + if ($@) { + croak "Failed to load config file: $config, error: $@"; + } + + $self->{_config} = $conf; + + bless ($self, $class); + return $self; +} + +1; diff --git a/lib/Display/Plugin.pm b/lib/Display/Plugin.pm index b8e9540..0405ca4 100644 --- a/lib/Display/Plugin.pm +++ b/lib/Display/Plugin.pm @@ -4,12 +4,13 @@ package Display::Plugin; use Clutter; sub new { - my ($class,$kernel,$session,$stage,$notifications) = @_; + my ($class,$kernel,$session,$stage,$config,$notifications) = @_; my $self = { 'kernel' => $kernel, 'session' => $session, 'stage' => $stage, + 'config' => $config, 'notifications' => $notifications, }; diff --git a/lib/Display/Plugins/FSpot.pm b/lib/Display/Plugins/FSpot.pm index 2c7b96d..50b5cb9 100644 --- a/lib/Display/Plugins/FSpot.pm +++ b/lib/Display/Plugins/FSpot.pm @@ -5,7 +5,7 @@ use Clutter; use POE; use POE::Component::EasyDBI; -my $f_spot_db = "/home/andrew/.gnome2/f-spot/photos.db"; +my $default_f_spot_db = "$ENV{HOME}/.gnome2/f-spot/photos.db"; sub new { my $proto = shift; @@ -74,7 +74,7 @@ sub init { POE::Component::EasyDBI->spawn( alias => 'FSpotDB', - dsn => "dbi:SQLite:dbname=$f_spot_db", + dsn => "dbi:SQLite:dbname=" . ($self->{'config'}{'_config'}{'plugings'}{'DB'} || $default_f_spot_db), username => '', password => '', ); diff --git a/picture.pl b/picture.pl index e3e1402..4963e8f 100755 --- a/picture.pl +++ b/picture.pl @@ -10,6 +10,7 @@ use Getopt::Long; use lib "$FindBin::Bin/lib"; +use Display::Config; use Display::Notifications; my $full_screen = 1; @@ -41,29 +42,43 @@ sub start { $stage->set_size(800, 600); } + my $config = Display::Config->new(); my $notifications = Display::Notifications->new($kernel, $session, $stage); - my @modules = loadModules($kernel, $session, $stage, $notifications); + my @modules = loadModules($kernel, $session, $stage, $config, $notifications); $stage->show_all(); } +my @modules; sub loadModules { - my ($kernel, $session, $stage, $notifications) = @_; - my @modules; + my ($kernel, $session, $stage, $config, $notifications) = @_; - for my $module (plugins()) { - warn "Considering $module\n"; + my @plugins = plugins(); + if (defined $config->{_config}{'background'}) { + warn "background module is set\n"; + warn "Considering background: " . join(", ", grep /$config->{_config}{'background'}/, @plugins) . "\n"; + load_plugin($kernel, $session, $stage, $config, $notifications, grep /$config->{_config}{'background'}/, @plugins ); + } + + for my $module (@{ $config->{_config}{'enabled'} }) { + warn "Considering $module: " . join(", ", grep /$module/, @plugins) . "\n"; + load_plugin($kernel, $session, $stage, $config, $notifications, grep /$module/, @plugins); + } +} + +sub load_plugin { + my ($kernel, $session, $stage, $config, $notifications, @plugin) = @_; + + for my $module (@plugin) { eval "use $module"; if ($@) { warn "Failed to load plugin: $module ($@)\n"; } else { - push @modules, $module->new($kernel, $session, $stage, $notifications); + push @modules, $module->new($kernel, $session, $stage, $config, $notifications); } } - - return @modules; } sub linear { -- 2.30.2