From 9ea5cf1ceed8f52d879516f4b70c6fae1c9c7091 Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Tue, 1 Oct 2013 03:27:47 +0200
Subject: [PATCH] added module Tomboy::Directory to scan dir with notes in
 Tomboy format

---
 perl/Gnome-Tomboy/.gitignore                |  1 +
 perl/Gnome-Tomboy/lib/Tomboy/Directory.pm   | 89 +++++++++++++++++++++
 perl/Gnome-Tomboy/lib/Tomboy/Note/Simple.pm |  9 ++-
 perl/Gnome-Tomboy/s2.pl                     | 32 ++++++++
 4 files changed, 130 insertions(+), 1 deletion(-)
 create mode 100644 perl/Gnome-Tomboy/.gitignore
 create mode 100644 perl/Gnome-Tomboy/lib/Tomboy/Directory.pm
 create mode 100755 perl/Gnome-Tomboy/s2.pl

diff --git a/perl/Gnome-Tomboy/.gitignore b/perl/Gnome-Tomboy/.gitignore
new file mode 100644
index 0000000..dd6181f
--- /dev/null
+++ b/perl/Gnome-Tomboy/.gitignore
@@ -0,0 +1 @@
+Tomboy-TOC.csv
diff --git a/perl/Gnome-Tomboy/lib/Tomboy/Directory.pm b/perl/Gnome-Tomboy/lib/Tomboy/Directory.pm
new file mode 100644
index 0000000..3026419
--- /dev/null
+++ b/perl/Gnome-Tomboy/lib/Tomboy/Directory.pm
@@ -0,0 +1,89 @@
+#
+# scan directory with note files in Tomboy format
+#
+
+=head1 NAME
+
+  Tomboy::Directory;
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+process a directory containing note files in Tomboy format
+
+=cut
+
+package Tomboy::Directory;
+
+use strict;
+
+use Tomboy::Note::Simple;
+
+my @TB_note_attrs= qw(title create-date last-change-date last-metadata-change-date);
+my @TB_meta_attrs= qw(uid notebook);
+sub TB_attrs { return (@TB_meta_attrs, @TB_note_attrs) }
+
+sub new
+{
+  my $class= shift;
+  my %par= @_;
+
+  my $obj= {};
+  bless $obj, $class;
+
+  foreach my $par (keys %par)
+  {
+    $obj->{$par}= $par{$par};
+    if ($par eq 'dir') { $obj->scan_dir ($par{$par}) }
+  }
+
+  $obj;
+}
+
+sub scan_dir
+{
+  my $obj= shift;
+  my $dir= shift;
+
+  unless (opendir (DIR, $dir))
+  {
+    print "ATTN: can't read directory [$dir]\n";
+    return undef;
+  }
+
+  my @res= ();
+  NOTE: while (my $e= readdir (DIR))
+  {
+    next NOTE if ($e eq '.' || $e eq '..');
+    next NOTE unless ($e =~ /(.+)\.note$/);
+    my $uid= $1;
+
+    my $fp= join ('/', $dir, $e);
+    # print "reading note [$fp]\n"; # TODO: if verbose...
+    my $n= parse Tomboy::Note::Simple ($fp);
+
+    unless (defined ($n))
+    {
+      print "ATTN: parsing [$fp] returned undefined note!\n";
+      next NOTE;
+    }
+
+    my %rec= map { $_ => $n->{$_} } @TB_note_attrs;
+    $rec{'uid'}= $uid;
+
+    foreach my $tag (@{$n->{'tags'}})
+    {
+      if ($tag =~ m#system:notebook:(.+)#) { $rec{'notebook'}= $1 }
+    }
+
+    push (@res, \%rec);
+  }
+
+  closedir (DIR);
+
+  (wantarray) ? @res : \@res;
+}
+
+1;
+
diff --git a/perl/Gnome-Tomboy/lib/Tomboy/Note/Simple.pm b/perl/Gnome-Tomboy/lib/Tomboy/Note/Simple.pm
index b6c96a8..048bc7f 100755
--- a/perl/Gnome-Tomboy/lib/Tomboy/Note/Simple.pm
+++ b/perl/Gnome-Tomboy/lib/Tomboy/Note/Simple.pm
@@ -88,10 +88,11 @@ sub parse
   my $fnm= shift;
 
   my $note;
+
      if (ref ($c) eq 'Tomboy::Note::Simple') { $note= $c; }
   elsif (ref ($c) eq '')
   {
-    print "create new c=[$c]\n";
+    # print "create new c=[$c]\n";
     $note= new Tomboy::Note::Simple;
   }
   else
@@ -99,6 +100,7 @@ sub parse
     print "unknown c=[$c] r=[", ref ($c), "]\n";
   }
   # print "note=[$note]\n";
+
   $note->{'fnm'}= $fnm;
 
   my $p= new XML::Parser (Style => 'Tree');
@@ -170,4 +172,9 @@ __END__
 
   Gerhard Gonter <ggonter@gmail.com>
 
+=head1 BUGS
+
+* XML::Parser throws exceptions, these are currently not handled.
+
 =cut
+
diff --git a/perl/Gnome-Tomboy/s2.pl b/perl/Gnome-Tomboy/s2.pl
new file mode 100755
index 0000000..8158829
--- /dev/null
+++ b/perl/Gnome-Tomboy/s2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+use strict;
+
+use lib 'lib';
+
+use Util::Simple_CSV;
+use Data::Dumper;
+$Data::Dumper::Indent= 1;
+
+# use Tomboy::Note::Simple;
+use Tomboy::Directory;
+
+my $note_dir= shift (@ARGV);
+my $toc_file= 'Tomboy-TOC.csv';
+
+die "no note dir specified" unless ($note_dir);
+
+my $tb_d= new Tomboy::Directory ('dir' => $note_dir);
+
+print "tb_d: ", Dumper ($tb_d);
+
+my $toc_data= $tb_d->scan_dir ($note_dir);
+# TODO: if verbose or so print "toc_data: ", Dumper ($toc_data);
+
+my $toc= new Util::Simple_CSV ('UTF8' => 1, 'no_hash' => 1);
+$toc->define_columns (Tomboy::Directory::TB_attrs());
+$toc->{'data'}= $toc_data;
+$toc->save_csv_file ('filename' => $toc_file);
+
+exit (0);
+
-- 
GitLab