« Return to Thread: WIP Note Directory Watcher Patch (was Watch for Changes Patch)

WIP Note Directory Watcher Patch (was Watch for Changes Patch)

by Michael Fletcher-3 :: Rate this Message:

Reply to Author | View in Thread

This is an updated version of the Note Directory Watcher Patch.  This
version is now useful.

It can detect changes to notes in ~/.tomboy.  It CANNOT detect new
notes or notes that have been deleted.  I will finish that tonight or
next week.

[watch.patch]

diff --git a/Tomboy/Addins/Makefile.am b/Tomboy/Addins/Makefile.am
index 57f5f56..9520b67 100644
--- a/Tomboy/Addins/Makefile.am
+++ b/Tomboy/Addins/Makefile.am
@@ -13,5 +13,6 @@ SUBDIRS = \
  SshSyncService \
  StickyNoteImport \
  Tasque \
+ NoteDirectoryWatcher \
  WebDavSyncService
 
diff --git a/Tomboy/Addins/NoteDirectoryWatcher/Makefile.am b/Tomboy/Addins/NoteDirectoryWatcher/Makefile.am
new file mode 100644
index 0000000..8250fd2
--- /dev/null
+++ b/Tomboy/Addins/NoteDirectoryWatcher/Makefile.am
@@ -0,0 +1,39 @@
+include $(top_srcdir)/Makefile.include
+
+CSFLAGS = \
+ -debug \
+ -define:DEBUG \
+ -target:library
+
+ASSEMBLIES = \
+ $(LINK_TOMBOY_EXE) \
+ $(GTKSHARP_LIBS) \
+ $(LINK_MONO_ADDINS) \
+ -r:Mono.Posix
+
+ADDIN_NAME = NoteDirectoryWatcher
+TARGET = $(ADDIN_NAME).dll
+CSFILES = \
+ $(srcdir)/NoteDirectoryWatcherApplicationAddin.cs
+RESOURCES = \
+ -resource:$(srcdir)/$(ADDIN_NAME).addin.xml
+
+$(TARGET).mdb: $(TARGET)
+
+$(TARGET): $(CSFILES) $(top_builddir)/Tomboy/Tomboy.exe
+ $(CSC) -out:$@ $(CSFLAGS) $(ASSEMBLIES) $(CSFILES) $(RESOURCES)
+
+
+addinsdir = $(pkglibdir)/addins
+addins_DATA = \
+ $(TARGET) \
+ $(TARGET).mdb
+
+EXTRA_DIST =             \
+ $(CSFILES) \
+ $(srcdir)/$(ADDIN_NAME).addin.xml
+
+CLEANFILES = \
+ $(TARGET).mdb \
+ $(TARGET)
+
diff --git a/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcher.addin.xml b/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcher.addin.xml
new file mode 100644
index 0000000..6bfd500
--- /dev/null
+++ b/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcher.addin.xml
@@ -0,0 +1,22 @@
+<Addin id="NoteDirectoryWatcher"
+ namespace="Tomboy"
+ name="Note Directory Watcher"
+ author="Tomboy Project"
+ description="Watch your Tomboy note directory for changes to your notes."
+ category="Tools"
+ defaultEnabled="false"
+ version="0.1">
+
+ <Runtime>
+ <Import assembly="NoteDirectoryWatcher.dll" />
+ </Runtime>
+
+ <Dependencies>
+ <Addin id="Tomboy" version="0.10" />
+ </Dependencies>
+
+ <Extension path="/Tomboy/ApplicationAddins">
+ <ApplicationAddin type="Tomboy.NoteDirectoryWatcher.NoteDirectoryWatcherApplicationAddin" />
+ </Extension>
+
+</Addin>
diff --git a/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcherApplicationAddin.cs b/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcherApplicationAddin.cs
new file mode 100644
index 0000000..c63cf93
--- /dev/null
+++ b/Tomboy/Addins/NoteDirectoryWatcher/NoteDirectoryWatcherApplicationAddin.cs
@@ -0,0 +1,160 @@
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+using Tomboy;
+
+namespace Tomboy.NoteDirectoryWatcher
+{
+ class NoteFileChangeRecord {
+ public DateTime last_change;
+ public bool deleted;
+ public bool changed;
+ }
+
+ public class NoteDirectoryWatcherApplicationAddin : ApplicationAddin
+ {
+ private FileSystemWatcher file_system_watcher;
+ private bool initialized;
+
+ private Dictionary<string, NoteFileChangeRecord> file_change_records;
+
+ public override void Initialize ()
+ {
+ string note_path = Tomboy.DefaultNoteManager.NoteDirectoryPath;
+
+ file_change_records = new Dictionary<string, NoteFileChangeRecord>();
+
+ file_system_watcher = new FileSystemWatcher (note_path);
+
+ file_system_watcher.Changed += HandleFileSystemChangeEvent;
+ file_system_watcher.Deleted += HandleFileSystemChangeEvent;
+ file_system_watcher.Created += HandleFileSystemChangeEvent;
+ file_system_watcher.Renamed += HandleFileSystemChangeEvent;
+
+ file_system_watcher.Error += HandleFileSystemErrorEvent;
+
+ // Setting to true will starts the FileSystemWatcher.
+ file_system_watcher.EnableRaisingEvents = true;
+
+ initialized = true;
+ }
+
+ public override void Shutdown ()
+ {
+ file_system_watcher.EnableRaisingEvents = false;
+
+ initialized = false;
+ }
+
+ public override bool Initialized
+ {
+ get {
+ return initialized;
+ }
+ }
+
+ private void HandleFileSystemErrorEvent (Object sender, ErrorEventArgs arg) {
+ // TODO Rescan the local notes in case some of them have changed.
+ Console.WriteLine("Handling error event: " + arg);
+ }
+
+ private void HandleFileSystemChangeEvent (Object sender, FileSystemEventArgs arg) {
+ // Record that the file has been added/changed/deleted.  adds/changes trump
+ // deletes.  Record the date.
+ lock (file_change_records)
+ {
+ string note_id = GetId(arg.FullPath);
+
+ NoteFileChangeRecord record = null;
+
+ if (file_change_records.ContainsKey(note_id)) {
+ record = file_change_records[note_id];
+ } else {
+ record = new NoteFileChangeRecord();
+ file_change_records[note_id] = record;
+ }
+
+ if (arg.ChangeType == WatcherChangeTypes.Changed) {
+ record.changed = true;
+ } else if (arg.ChangeType == WatcherChangeTypes.Created) {
+ record.changed = true;
+ } else if (arg.ChangeType == WatcherChangeTypes.Deleted) {
+ if (!record.changed) {
+ record.deleted = true;
+ }
+ } else {
+ throw new Exception("Unexpected WatcherChangeType " + arg.ChangeType);
+ }
+
+ record.last_change = DateTime.Now;
+ }
+
+ GLib.Timeout.Add(5000, new GLib.TimeoutHandler(HandleTimeout));
+ }
+
+ private bool HandleTimeout() {
+ lock (file_change_records) {
+ List<string> keysToRemove = new List<string>(file_change_records.Count);
+
+ foreach (KeyValuePair<string, NoteFileChangeRecord> pair in file_change_records) {
+ if (pair.Key != "") { // TODO
+ if (DateTime.Now > pair.Value.last_change.Add (new TimeSpan(4000)) ) {
+ Console.WriteLine(pair);
+ if (pair.Value.deleted) {
+ DeleteNote(pair.Key);
+ } else {
+ AddOrUpdateNote(pair.Key);
+ }
+
+ keysToRemove.Add(pair.Key);
+ }
+ }
+ }
+
+ foreach (string note_id in keysToRemove) {
+ file_change_records.Remove(note_id);
+ }
+ }
+
+ return false;
+ }
+
+ private static void DeleteNote(string note_id) {
+ // TODO Delete the note.
+ }
+
+ private static void AddOrUpdateNote(string note_id) {
+ bool found = false;
+
+ foreach (Note note in Tomboy.DefaultNoteManager.Notes) {
+ if (note.Id == note_id) {
+ found = true;
+
+ string note_path = Tomboy.DefaultNoteManager.NoteDirectoryPath +
+ Path.DirectorySeparatorChar + note_id + ".note";
+ string note_url = "note://tomboy/" + note_id;
+
+ NoteData data = NoteArchiver.Instance.ReadFile(note_path, note_url);
+
+ if (data.Text != note.XmlContent) {
+ note.XmlContent = data.Text;
+ note.Title = data.Title;
+ }
+ }
+ }
+
+ if (!found) {
+ // TODO Add the note.
+ }
+ }
+
+ private static string GetId(string path) {
+ int lastSlash = path.LastIndexOf(Path.DirectorySeparatorChar);
+ int firstPeriod = path.IndexOf('.', lastSlash);
+
+ return path.Substring(lastSlash + 1, firstPeriod - lastSlash - 1);
+ }
+ }
+}
diff --git a/configure.in b/configure.in
index ab9b951..8563e58 100644
--- a/configure.in
+++ b/configure.in
@@ -333,6 +333,7 @@ Tomboy/Addins/SshSyncService/Makefile
 Tomboy/Addins/StickyNoteImport/Makefile
 Tomboy/Addins/Tasque/Makefile
 Tomboy/Addins/WebDavSyncService/Makefile
+Tomboy/Addins/NoteDirectoryWatcher/Makefile
 test/Makefile
 po/Makefile.in
 ])


_______________________________________________
Tomboy-list mailing list
Tomboy-list@...
http://lists.beatniksoftware.com/listinfo.cgi/tomboy-list-beatniksoftware.com

 « Return to Thread: WIP Note Directory Watcher Patch (was Watch for Changes Patch)