]> 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 607fffe24fe9b0105957e8310a32e19170b8c392..cd894715d7563c7a15acec4ff01407c1a124f16b 100644 (file)
@@ -47,7 +47,7 @@ public class Waypoint : Object
     public void writeInside(FileStream outfd)
     {
         var t = Time.gm(ts);
-        outfd.printf("   <time>%s</time>\n", t.format("%Y-%m-%dT%H:%M:%SZ"));
+        outfd.printf("   <time>%s</time>\n", t.format("%Y-%m-%dT%H:%M:%S%z"));
     }
 }
 
@@ -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";
 
@@ -143,6 +149,11 @@ public class Log : Object
         outfd.flush();
     }
 
+    public void dump()
+    {
+        write(stderr);
+    }
+
     protected void writeTrack(FileStream outfd)
     {
         outfd.puts("   <trk>\n");
@@ -155,10 +166,8 @@ public class Log : Object
 
     protected void writeEntries(FileStream outfd)
     {
-        outfd.puts("   <wpt>\n");
         for (weak List<LogEntry> i = entries; i != null; i = i.next)
             i.data.write(outfd);
-        outfd.puts("   </wpt>\n");
     }
 
     protected void write(FileStream outfd)
@@ -171,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);
@@ -199,6 +209,8 @@ class LogParser: Object
     public Log result = null;
     LogParserState state = LogParserState.NONE;
     string cur_text = "";
+    LogEntry cur_logentry = null;
+    TrackEntry cur_trackentry = null;
 
     construct
     {
@@ -213,23 +225,34 @@ class LogParser: Object
     void destroy()
     {
         cur_text = "";
+        cur_logentry = null;
+        cur_trackentry = null;
     }
 
     public bool parse(string content, ssize_t len = -1) throws MarkupError
     {
-        return context.parse(content, len);
+        string oldtz = Environment.get_variable("TZ");
+        Environment.set_variable("TZ", "UTC", true);
+        bool res = context.parse(content, len);
+        if (oldtz == null)
+            Environment.unset_variable("TZ");
+        else
+            Environment.set_variable("TZ", oldtz, true);
+        return res;
     }
 
-
-        /*
-        outfd.puts("  <metadata>\n");
-        outfd.printf("    <name>%s</name>\n", Markup.escape_text(title));
-        outfd.puts("  </metadata>\n");
-        if (track != null) writeTrack(outfd);
-        if (entries != null) writeEntries(outfd);
-        outfd.puts(" </gpx>\n");
-        */
-
+    void parse_attrs(Waypoint w, string[] attr_names, string[] attr_values)
+    {
+        w.lat = 1000;
+        w.lon = 1000;
+        for (int i = 0; attr_names[i] != null; ++i)
+        {
+            if (attr_names[i] == "lat")
+                w.lat = attr_values[i].to_double();
+            else if (attr_names[i] == "lon")
+                w.lon = attr_values[i].to_double();
+        }
+    }
 
     void start (MarkupParseContext context, string name,
                 string[] attr_names, string[] attr_values) throws MarkupError
@@ -241,8 +264,14 @@ class LogParser: Object
         } else if (name == "metadata") {
             state = LogParserState.METADATA;
         } else if (name == "wpt") {
+            cur_logentry = new LogEntry();
+            parse_attrs(cur_logentry, attr_names, attr_values);
+            result.entries.append(cur_logentry);
             state = LogParserState.WPT;
-        } else if (name == "trkseg") {
+        } else if (name == "trkpt") {
+            cur_trackentry = new TrackEntry();
+            parse_attrs(cur_trackentry, attr_names, attr_values);
+            result.track.append(cur_trackentry);
             state = LogParserState.TRACK;
         }
         cur_text = "";
@@ -250,9 +279,31 @@ class LogParser: Object
 
     void end (MarkupParseContext context, string name) throws MarkupError
     {
-        if (name == "state")
-            if (state == LogParserState.METADATA)
-                result.title = cur_text;
+        if (name == "name")
+        {
+            switch (state)
+            {
+                case LogParserState.METADATA:
+                    result.title = cur_text;
+                    break;
+                case LogParserState.WPT:
+                    cur_logentry.msg = cur_text;
+                    break;
+            }
+        }
+        else if (name == "keywords")
+        {
+            result.tag = cur_text;
+        }
+        else if (name == "time")
+        {
+            Time t = Time();
+            t.strptime(cur_text, "%Y-%m-%dT%H:%M:%S%z");
+            if (state == LogParserState.WPT)
+                cur_logentry.ts = t.mktime();
+            else if (state == LogParserState.TRACK)
+                cur_trackentry.ts = t.mktime();
+        }
     }
 
     void text (MarkupParseContext context,
@@ -266,6 +317,8 @@ public class Logger : Resource, Object
 {
     protected List<Log> logs;
 
+    public signal void entries_changed();
+
     public Logger()
     {
         logs = null;
@@ -309,6 +362,7 @@ public class Logger : Resource, Object
         pop(log);
         log.save();
         if (logs == null) end_trace();
+        entries_changed();
     }
 
     public Log load(string fname)
@@ -321,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);