package RSSReader::Configuration; use strict; use RSSReader::Feed; use RSSReader::Category; use Data::Dumper; use Storable; use base qw(RSSReader::Notifier RSSReader::Logger); our $_config; #singleton config; our $_info; sub config_mem { return $_config; } sub get_category { my $s = shift; my %a = @_; return $_config->{_info}->{categories}->{$a{id}}; } sub get_feed { my $s = shift; my %a = @_; return $_config->{_info}->{sites}->{$a{id}}; } sub get_message { my $s = shift; my %a = @_; unless (exists $s->{_info}->{sites}->{$a{feed_id}}) { $@ = 'No such feed'; return undef; } unless ( exists $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$a{id}} ) { $@ = "No such message"; return undef; } return $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$a{id}}; } sub get_messages_from_feed { my $s = shift; my %a = @_; my @to_return = (); unless (exists $s->{_info}->{sites}->{$a{feed_id}}) { $@ = 'No such feed'; return undef; } foreach my $msg ( keys %{$s->{_info}->{sites}->{$a{feed_id}}->{messages}} ) { push @to_return, $msg; } return wantarray ? @to_return : \@to_return; } sub insert_message { my $s = shift; my %a = @_; unless (exists $s->{_info}->{sites}->{$a{feed_id}}) { $@ = 'No such feed'; return undef; } #ID generatio my $leap = $a{url} || $a{title}; my $id = $a{feed_id}.'-'.$leap; if (exists $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$id}) { $@ = { message => "Message already inserted", id => $id }; return undef; } $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$id} = { read => 0, %a }; return $id; } sub insert_feed { my $s = shift; my %a = @_; my $rand = int(rand(500000)); while ( exists($s->{_info}->{sites}->{$rand}) ) { $rand = int(rand(500000)); } $s->{_info}->{sites}->{$rand} = { label => $a{label}, location => $a{location}, fetch_interval => $a{fetch_interval}, url => $a{url}, }; return $rand; } sub remove_feed { my $s = shift; my %a = @_; delete $s->{_info}->{sites}->{$a{id}}; return 1; } sub insert_category { my $s = shift; my %a = @_; my $rand = int(rand(500000)); while ( exists($s->{_info}->{categories}->{$rand}) ) { $rand = int(rand(500000)); } $s->{_info}->{categories}->{$rand} = { label => $a{label}, location => $a{location}, }; return $rand; } sub remove_category { my $s = shift; my %a = @_; delete $s->{_info}->{categories}->{$a{id}}; return 1; } sub remove_message { my $s = shift; my %a = @_; unless ($a{id} && $a{feed_id}) { $@ = "Need a id and a feed_id"; return undef; die $@; } unless (exists $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$a{id}}) { $@ = "This message does not exist"; die $@; return undef; } delete $s->{_info}->{sites}->{$a{feed_id}}->{messages}->{$a{id}}; $s->log_debug("Message ".$a{title}." ($a{id}) was deleted"); return 1; } sub get_all_feeds_id { my $s = shift; my @ids_to_return; foreach my $f_id (keys %{$_config->{_info}->{sites}}) {push @ids_to_return, $f_id} return wantarray ? @ids_to_return : \@ids_to_return; } sub get_all_categories_id { my $s = shift; my @ids_to_return; foreach my $f_id (keys %{$_config->{_info}->{categories}}) {push @ids_to_return, $f_id} return wantarray ? @ids_to_return : \@ids_to_return; } sub save { my $s = shift; my $pref_dir = $ENV{HOME}.'/.syndigator/'; if ( ! (-d $pref_dir )) { `mkdir $pref_dir` } store $s->{_info}, $pref_dir.'data.sto'; } sub new { my $c = shift; return $_config if defined($_config); my $pref_dir = $ENV{HOME}.'/.syndigator/'; unless ( (-d $pref_dir) && (-f $pref_dir.'data.sto') ) { $c->log_debug("Giving default config"); $_info = { sites => { "1" => { url => 'http://slashdot.org/slashdot.rss', news => {}, location => 12, fetch_interval => 60, fetches_failed => 0, last_fetched => 0, label => 'SlashDot' }, "2" => { url => 'http://www.gamegrene.com/rss/gamegrene.xml', news => {}, location => 13, fetch_interval => 60, fetches_failed => 0, last_fetched => 0, label => 'GameGrene' }, "3" => { url => 'http://lab.nunonunes.org/syndigator/rss2.xml', news => {}, location => 14, fetch_interval => 60, fetches_failed => 0, last_fetched => 0, label => 'Syndigator' }, }, categories => { "1" => { label => 'Feeds', widget => undef, location => undef }, "12" => { label => 'Tech', widget => undef, location => 1 }, "14" => { label => 'Software', widget => undef, location => 1 }, "13" => { label => 'Games', widget => undef, location => 1 }, } }; } else { $c->log_debug("Config loaded sucessfully"); $_info = retrieve($pref_dir.'data.sto'); #Cleanup stuff... (bug fix) foreach my $feed2 (keys %{$_info->{sites}}){ my $messages = $_info->{sites}{$feed2}{messages}; foreach my $key (keys %{$messages}){ delete $messages->{$key}{feed}; } } } $_config = bless { _info => $_info }, $c; return $_config; } 1;