diff --git a/perl/Gnome-Tomboy/s2.pl b/perl/Gnome-Tomboy/s2.pl index e571139ac57422bc402a10a3ca54ca9858c29722..8ffac7a358e1d0d6fd43fe4cd1f6e0f66d12ebac 100755 --- a/perl/Gnome-Tomboy/s2.pl +++ b/perl/Gnome-Tomboy/s2.pl @@ -24,13 +24,20 @@ $Data::Dumper::Indent= 1; use Tomboy::Directory; my $toc_file= 'Tomboy-TOC.csv'; +my $note_dir= '~/Tomboy'; + +my $mode= 'scan'; +my $start_tb= 0; my @PAR= (); while (my $arg= shift (@ARGV)) { if ($arg =~ /^-/) { - usage(); + if ($arg eq '-f') { $mode= 'find'; } + elsif ($arg eq '-d') { $note_dir= shift (@ARGV); } + elsif ($arg eq '-e') { $start_tb= 1; } + else { usage(); } } else { @@ -38,45 +45,160 @@ while (my $arg= shift (@ARGV)) } } -my $note_dir= shift (@PAR); -die "no note dir specified" unless ($note_dir); +my $bli= new bli('note_dir' => $note_dir, 'toc_file' => $toc_file); +print "bli: ", Dumper ($bli); + +if ($mode eq 'scan') +{ + $bli->scan_dir(); +} +elsif ($mode eq 'find') +{ + my %uuid; + foreach my $pattern (@PAR) + { + my @res= $bli->find ($pattern); + foreach my $res (@res) { $uuid{$res->{'uuid'}}= $res; } # TODO: count hits + } + + foreach my $uuid (keys %uuid) + { + if ($start_tb) + { + start_tb ('uuid', $uuid); + } + else + { + print Dumper ($uuid{$uuid}); + } + } +} -$note_dir=~ s#/+$##; -my $tb_d= new Tomboy::Directory (); -print "tb_d: ", Dumper ($tb_d); +=begin comment -my $toc= new Util::Simple_CSV ('UTF8' => 1, 'no_array' => 1, 'separator' => "\t", 'UTF8' => 1); +hmm... starting Tomboy does not work as expected. When tomboy is +already running, calling it is fine, otherwise, it has to be started +in the background and given some time before it can be called again. +Probably it would make sense to do something with D-Bus here... -my $mode= 0; -my $rows= []; +=end comment +=cut -if (-f $toc_file) +sub start_tb { - $toc->load_csv_file ($toc_file); + my $what= shift; + my $par= shift; - $mode= 1; - $rows= $toc->{'data'}; - print "preparing quick scan, loaded [$toc_file]\n"; - # print "toc: ", Dumper ($toc); - # print "rows: ", Dumper ($rows); - # exit; + if ($what eq 'uuid') + { + my @cmd= ('tomboy', '--open-note', 'note://tomboy/'. $par); + print ">>> ", join (' ', @cmd), "\n"; + + my $pid= fork(); + if ($pid == 0) { exec @cmd; } + print "started pid=[$pid]\n"; + } } -else + +exit (0); + +package bli; + +sub new { - # $toc->define_columns (Tomboy::Directory::TB_attrs()); - $toc->define_columns ($tb_d->TB_attrs()); + my $class= shift; + + my $self= {}; + bless $self, $class; + $self->set (@_); + + $self; } -my $toc_data= $tb_d->scan_dir ($note_dir, $rows, $mode); -# TODO: if verbose or so print "toc_data: ", Dumper ($toc_data); +sub set +{ + my $self= shift; + my %par= @_; -$toc->{'data'}= $toc_data; -# TODO: optionally sort the returned values -$toc->sort ('uuid'); -# print "toc: ", Dumper ($toc); -$toc->save_csv_file ('filename' => $toc_file, 'separator' => "\t", 'UTF8' => 1); + foreach my $par (keys %par) { $self->{$par}= $par{$par}; } +} -exit (0); +sub find +{ + my $self= shift; + my $pattern= shift; + +print "find: pattern=[$pattern]\n"; + my ($mode, $toc, $rows)= $self->load_toc(); + my @res; + foreach my $row (@$rows) + { + next unless ($row->{'title'} =~ m#$pattern#i); + # print "row: ", main::Dumper ($row); + push (@res, $row); + } + @res; +} + +sub scan_dir +{ + my $self= shift; + +print main::Dumper ($self); + my ($note_dir, $toc_file)= map { $self->{$_} } qw(note_dir toc_file); + + $note_dir=~ s#/+$##; + my $tb_d= new Tomboy::Directory (); + # print "tb_d: ", Dumper ($tb_d); + print "scanning [$note_dir]\n"; + + my ($mode, $toc, $rows)= $self->load_toc(); + + my $toc_data= $tb_d->scan_dir ($note_dir, $rows, $mode); + # TODO: if verbose or so print "toc_data: ", Dumper ($toc_data); + + $toc->{'data'}= $toc_data; + # TODO: optionally sort the returned values + $toc->sort ('uuid'); + # print "toc: ", Dumper ($toc); + $toc->save_csv_file ('filename' => $toc_file, 'separator' => "\t", 'UTF8' => 1); +} + +sub load_toc +{ + my $self= shift; + + my $mode= 0; + my $toc; + +print "load_toc\n"; + return (1, $toc, $toc->{'data'}) if (defined ($toc= $self->{'_toc'})); + + my ($toc_file)= map { $self->{$_} } qw(toc_file); +print "load_toc: toc_file=[$toc_file]\n"; + + my $toc= new Util::Simple_CSV ('UTF8' => 1, 'no_array' => 1, 'separator' => "\t", 'UTF8' => 1); + my $rows= []; + if (-f $toc_file) + { + print "loading $toc_file\n"; + $toc->load_csv_file ($toc_file); + + $mode= 1; + $rows= $toc->{'data'}; + print "preparing quick scan, loaded [$toc_file]\n"; + # print "toc: ", Dumper ($toc); + # print "rows: ", Dumper ($rows); + # exit; + } + else + { + $toc->define_columns (Tomboy::Directory::TB_attrs()); + } + $self->{'_toc'}= $toc; + + ($mode, $toc, $rows); +} __END__ @@ -86,6 +208,15 @@ __END__ * cache the data in ~/.gcache ?? check out how this is supposed to work * optionally, integrate with version control for quick synchronization +=head1 NOTES + +=head2 Directories used by Tomboy on Ubuntu + + * ~/.cache/tomboy/ ... empty + * ~/.gconf/tomboy/ + %gconf.xml + * ~/.config/tomboy/ ... various stuff +