]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/log.vala
Add tag into log metadata so it's easier to read
[gregoa/zavai.git] / src / log.vala
index b8aac437363cb764d9cf6d0b448daa1ae3fc5590..cd894715d7563c7a15acec4ff01407c1a124f16b 100644 (file)
@@ -90,13 +90,15 @@ public class Log : Object
 {
     public string tag;
     public string title;
+    public bool acked;
     public List<LogEntry> entries;
     public List<TrackEntry> track;
 
-    public Log(string tag, string title)
+    public Log(string tag, string title, bool acked=false)
     {
         this.tag = tag;
         this.title = title;
+        this.acked = acked;
         entries = null;
         track = null;
     }
@@ -118,12 +120,16 @@ public class Log : Object
         if (entries == null) return;
 
         // Directory where we save the log
-        string dir = config.homedir + "/log-" + tag;
+        string dir;
+        if (acked)
+            dir = config.homedir + "/archive";
+        else
+            dir = config.homedir + "/log";
         DirUtils.create(dir, 0777);
 
         // First try with a plain name
         var t = Time.local(entries.data.ts);
-        string basename = dir + "/" + t.format("%Y%m%d-%H%M%S");
+        string basename = dir + "/" + t.format("%Y%m%d-%H%M%S") + "-" + tag;
 
         string pathname = basename + ".gpx";
 
@@ -174,6 +180,7 @@ public class Log : Object
         outfd.puts("     xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
         outfd.puts("  <metadata>\n");
         outfd.printf("    <name>%s</name>\n", Markup.escape_text(title));
+        outfd.printf("    <keywords>%s</keywords>\n", Markup.escape_text(tag));
         outfd.puts("  </metadata>\n");
         if (track != null) writeTrack(outfd);
         if (entries != null) writeEntries(outfd);
@@ -284,6 +291,10 @@ class LogParser: Object
                     break;
             }
         }
+        else if (name == "keywords")
+        {
+            result.tag = cur_text;
+        }
         else if (name == "time")
         {
             Time t = Time();
@@ -306,6 +317,8 @@ public class Logger : Resource, Object
 {
     protected List<Log> logs;
 
+    public signal void entries_changed();
+
     public Logger()
     {
         logs = null;
@@ -349,6 +362,7 @@ public class Logger : Resource, Object
         pop(log);
         log.save();
         if (logs == null) end_trace();
+        entries_changed();
     }
 
     public Log load(string fname)
@@ -361,6 +375,48 @@ public class Logger : Resource, Object
         return parser.result;
     }
 
+    public void set_acknowledged(string name, bool acked)
+    {
+        string from, to;
+        if (acked)
+        {
+            from = config.homedir + "/log/" + name;
+            to = config.homedir + "/archive/" + name;
+            DirUtils.create(config.homedir + "/archive", 0777);
+        } else {
+            from = config.homedir + "/archive/" + name;
+            to = config.homedir + "/log/" + name;
+            DirUtils.create(config.homedir + "/log", 0777);
+        }
+        if (FileUtils.test(from, FileTest.EXISTS))
+        {
+            FileUtils.rename(from, to);
+            entries_changed();
+        }
+    }
+
+    public delegate bool EntriesVisitor(string dir, string name);
+
+    protected void list_dir(string dir, EntriesVisitor visitor)
+    {
+        var d = File.new_for_path(dir);
+        var enumerator = d.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
+        FileInfo file_info;
+        while ((file_info = enumerator.next_file(null)) != null)
+        {
+            if (!file_info.get_name().has_suffix(".gpx")) continue;
+            if (!visitor(dir, file_info.get_name()))
+                break;
+        }
+    }
+
+    public void list_entries(EntriesVisitor visitor, bool only_unacked=true)
+    {
+        if (!only_unacked)
+            list_dir(config.homedir + "/archive", visitor);
+        list_dir(config.homedir + "/log", visitor);
+    }
+
     public void instant(string tag, string msg)
     {
         var log = new Log(tag, msg);