Adding first code drop
[demorecorder] / src / ApplicationSettings.vala
diff --git a/src/ApplicationSettings.vala b/src/ApplicationSettings.vala
new file mode 100644 (file)
index 0000000..e352213
--- /dev/null
@@ -0,0 +1,652 @@
+/*  Demo Recorder for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+namespace IdWorks {
+
+public class ApplicationSettings {
+
+  public enum OpenAction {
+    LAST_PROJECT,
+    NEW_PROJECT,
+    RECENT_PROJECTS,
+    NAMED_PROJECT;
+    
+    public static OpenAction from_label(string val) {
+      switch (val) {
+        case "Last open project":
+          return LAST_PROJECT;
+        case "New project":
+          return NEW_PROJECT;
+        case "Recent projects list":
+          return RECENT_PROJECTS;
+        case "Specified project":
+          return NAMED_PROJECT;
+        default:
+          return LAST_PROJECT;
+      }
+    }
+    public static OpenAction from_name(string val) {
+      switch (val) {
+        case "LAST_PROJECT":
+          return LAST_PROJECT;
+        case "NEW_PROJECT":
+          return NEW_PROJECT;
+        case "RECENT_PROJECTS":
+          return RECENT_PROJECTS;
+        case "NAMED_PROJECT":
+          return NAMED_PROJECT;
+        default:
+          return LAST_PROJECT;
+      }
+    }
+    public static string to_label(OpenAction val) {
+      switch (val) {
+        case LAST_PROJECT:
+          return "Last open project";
+        case NEW_PROJECT:
+          return "New project";
+        case RECENT_PROJECTS:
+          return "Recent projects list";
+        case NAMED_PROJECT:
+          return "Specified project";
+        default:
+          return "Last open project";
+      }
+    }
+    public static string to_name(OpenAction val) {
+      switch(val) {
+        case LAST_PROJECT:
+          return "LAST_PROJECT";
+        case NEW_PROJECT:
+          return "NEW_PROJECT";
+        case RECENT_PROJECTS:
+          return "RECENT_PROJECTS";
+        case NAMED_PROJECT:
+          return "NAMED_PROJECT";
+        default:
+          return "LAST_PROJECT";
+      }
+    }
+  }
+  public enum CloseAction {
+    AUTO_SAVE_PROJECT,
+    PROMPT_SAVE_PROJECT,
+    DO_NOTHING;
+    
+    public static CloseAction from_label(string val) {
+      switch (val) {
+        case "Auto-save the project":
+          return AUTO_SAVE_PROJECT; 
+        case "Prompt to save the project":
+          return PROMPT_SAVE_PROJECT;
+        case "Do nothing":
+          return DO_NOTHING;
+        default:
+          return AUTO_SAVE_PROJECT;
+      }
+    }
+    public static CloseAction from_name(string val) {
+      switch (val) {
+        case "AUTO_SAVE_PROJECT":
+          return AUTO_SAVE_PROJECT;
+        case "PROMPT_SAVE_PROJECT":
+          return PROMPT_SAVE_PROJECT;
+        case "DO_NOTHING":
+          return DO_NOTHING;
+        default:
+          return PROMPT_SAVE_PROJECT;
+      }
+    }
+    public static string to_label(CloseAction val) {
+      switch (val) {
+        case AUTO_SAVE_PROJECT:
+          return "Auto-save the project";
+        case PROMPT_SAVE_PROJECT:
+          return "Prompt to save the project";
+        case DO_NOTHING:
+          return "Do nothing";
+        default:
+          return "Auto-save the project";
+      }
+    } 
+    public static string to_name(CloseAction val) {
+      switch(val) {
+        case AUTO_SAVE_PROJECT:
+          return "AUTO_SAVE_PROJECT";
+        case PROMPT_SAVE_PROJECT:
+          return "PROMPT_SAVE_PROJECT";
+        case DO_NOTHING:
+          return "DO_NOTHING";
+        default:
+          return "PROMPT_SAVE_PROJECT";
+      }
+    }
+  }
+  public enum AddTrackAction {
+    SET_ACTIVE,
+    SET_SOLO,
+    SET_INACTIVE,
+    SET_MUTE;
+    
+    public static AddTrackAction from_label(string val) {
+      switch (val) {
+        case "Enable track":
+          return SET_ACTIVE;
+        case "Solo track":
+          return SET_SOLO;
+        case "Disable track":
+          return SET_INACTIVE;
+        case "Mute track":
+          return SET_MUTE;
+        default:
+          return SET_ACTIVE;
+      }
+    }
+    public static AddTrackAction from_name(string val) {
+      switch (val) {
+        case "SET_ACTIVE":
+          return SET_ACTIVE;
+        case "SET_SOLO":
+          return SET_SOLO;
+        case "SET_INACTIVE":
+          return SET_INACTIVE;
+        case "SET_MUTE":
+          return SET_MUTE;
+        default:
+          return SET_ACTIVE;
+      }
+    }
+    public static string to_label(AddTrackAction val) {
+      switch (val) {
+        case SET_ACTIVE:
+          return "Enable track";
+        case SET_SOLO:
+          return "Solo track";
+        case SET_INACTIVE:
+          return "Disable track";
+        case SET_MUTE:
+          return "Mute track";
+        default:
+          return "Enable track";
+      }
+    }
+    public static string to_name(AddTrackAction val) {
+      switch(val) {
+        case SET_ACTIVE:
+          return "SET_ACTIVE";
+        case SET_SOLO:
+          return "SET_SOLO";
+        case SET_INACTIVE:
+          return "SET_INACTIVE";
+        case SET_MUTE:
+          return "SET_MUTE";
+        default:
+          return "SET_ACTIVE";
+      }
+    }
+  }
+  public enum BounceTracksAction {
+    SET_SOURCE_INACTIVE,
+    SET_SOURCE_MUTE,
+    SET_TARGET_SOLO,
+    SET_TARGET_INACTIVE,
+    SET_TARGET_MUTE;
+    
+    public static BounceTracksAction from_label(string val) {
+      switch (val) {
+        case "Disable source tracks":
+          return SET_SOURCE_INACTIVE;
+        case "Mute source tracks":
+          return SET_SOURCE_MUTE;
+        case "Solo new track":
+          return SET_TARGET_SOLO;
+        case "Disable new track":
+          return SET_TARGET_INACTIVE;
+        case "Mute new track":
+          return SET_TARGET_MUTE;
+        default:
+          return SET_SOURCE_INACTIVE;
+      }
+    }
+    public static BounceTracksAction from_name(string val) {
+      switch (val) {
+        case "SET_SOURCE_INACTIVE":
+          return SET_SOURCE_INACTIVE;
+        case "SET_SOURCE_MUTE":
+          return SET_SOURCE_MUTE;
+        case "SET_TARGET_SOLO":
+          return SET_TARGET_SOLO;
+        case "SET_TARGET_INACTIVE":
+          return SET_TARGET_INACTIVE;
+        case "SET_TARGET_MUTE":
+          return SET_TARGET_MUTE;
+        default:
+          return SET_SOURCE_INACTIVE;
+      }
+    }
+    public static string to_label(BounceTracksAction val) {
+      switch (val) {
+        case SET_SOURCE_INACTIVE:
+          return "Disable source tracks";
+        case SET_SOURCE_MUTE:
+          return "Mute source tracks";
+        case SET_TARGET_SOLO:
+          return "Solo new track";
+        case SET_TARGET_INACTIVE:
+          return "Disable new track";
+        case SET_TARGET_MUTE:
+          return "Mute new track";
+        default:
+          return "Disable source tracks";
+      }
+    }
+    public static string to_name(BounceTracksAction val) {
+      switch (val) {
+        case SET_SOURCE_INACTIVE:
+          return "SET_SOURCE_INACTIVE";
+        case SET_SOURCE_MUTE:
+          return "SET_SOURCE_MUTE";
+        case SET_TARGET_SOLO:
+          return "SET_TARGET_SOLO";
+        case SET_TARGET_INACTIVE:
+          return "SET_TARGET_INACTIVE";
+        case SET_TARGET_MUTE:
+          return "SET_TARGET_MUTE";
+        default:
+          return "SET_SOURCE_INACTIVE";
+      }
+    }
+  }
+  public enum RemoveTrackAction {
+    PROMPT,
+    DELETE_FILE,
+    PRESERVE_FILE;
+    
+    public static RemoveTrackAction from_label(string val) {
+      switch (val) {
+        case "Prompt with choices":
+          return PROMPT;
+        case "Delete the file":
+          return DELETE_FILE;
+        case "Preserve the file":
+          return PRESERVE_FILE;
+        default:
+          return PROMPT;
+      }
+    }
+    public static RemoveTrackAction from_name(string val) {
+      switch (val) {
+        case "PROMPT":
+          return PROMPT;
+        case "DELETE_FILE":
+          return DELETE_FILE;
+        case "PRESERVE_FILE":
+          return PRESERVE_FILE;
+        default:
+          return PROMPT;
+      }
+    }
+    public static string to_label(RemoveTrackAction val) {
+      switch (val) {
+        case PROMPT:
+          return "Prompt with choices";
+        case DELETE_FILE:
+          return "Delete the file";
+        case PRESERVE_FILE:
+          return "Preserve the file";
+        default:
+          return "Prompt with choices";
+      }
+    }
+    public static string to_name(RemoveTrackAction val) {
+      switch(val) {
+        case PROMPT:
+          return "PROMPT";
+        case DELETE_FILE:
+          return "DELETE_FILE";
+        case PRESERVE_FILE:
+          return "PRESERVE_FILE";
+        default:
+          return "PROMPT";
+      }
+    }
+  }
+  public enum MixdownAction {
+    NONE,
+    PLAY_TARGET;
+    
+    public static MixdownAction from_label(string val) {
+      switch (val) {
+        case "Do nothing":
+          return NONE;
+        case "Play mix":
+          return PLAY_TARGET;
+        default:
+          return NONE;
+      }
+    }
+    public static MixdownAction from_name(string val) {
+      switch (val) {
+        case "NONE":
+          return NONE;
+        case "PLAY_TARGET":
+          return PLAY_TARGET;
+        default:
+          return NONE;
+      }
+    }
+    public static string to_label(MixdownAction val) {
+      switch (val) {
+        case NONE:
+          return "Do nothing";
+        case PLAY_TARGET:
+          return "Play mix";
+        default:
+          return "Do nothing";
+      }
+    }
+    public static string to_name(MixdownAction val) {
+      switch(val) {
+        case NONE:
+          return "NONE";
+        case PLAY_TARGET:
+          return "PLAY_TARGET";
+        default:
+          return "NONE";
+      }
+    }
+  }
+  
+  public string working_directory {public get; public set;}
+  public string last_project_name {public get; public set;}
+  public string last_project_location {public get; public set;}
+  public string named_project_name {public get; public set;}
+  public string named_project_location {public get; public set;}
+  public string[] recent_project_names {public get; public set;}
+  public string[] recent_project_locations {public get; public set;}
+  public uint64 delay_max_delay {public get; public set;}
+  public bool mixdown_enable_lamenc {public get; public set;}
+  public bool mixdown_enable_mpegaudio {public get; public set;}
+  public OpenAction open_action {public get; public set;}
+  public CloseAction close_action {public get; public set;}
+  public AddTrackAction add_track_action {public get; public set;}
+  public BounceTracksAction bounce_tracks_action {public get; public set;}
+  public RemoveTrackAction remove_track_action {public get; public set;}
+  public MixdownAction mixdown_action {public get; public set;}
+  
+  public ApplicationSettings() {
+    working_directory = Environment.get_home_dir() + "/DemoRecordings";
+    last_project_name = "";
+    last_project_location = "";
+    named_project_name = "";
+    named_project_location = "";
+    recent_project_names = new string[]{"","","","","","","","","",""};
+    recent_project_locations = new string[]{"","","","","","","","","",""};
+    delay_max_delay = Time.Nanoseconds.SECOND / 2;
+    mixdown_enable_lamenc = false;
+    mixdown_enable_mpegaudio = false;
+    open_action = OpenAction.LAST_PROJECT;
+    close_action = CloseAction.PROMPT_SAVE_PROJECT;
+    add_track_action = AddTrackAction.SET_ACTIVE;
+    bounce_tracks_action = BounceTracksAction.SET_SOURCE_INACTIVE;
+    remove_track_action = RemoveTrackAction.PROMPT;
+  }
+  
+  public static ApplicationSettings load_settings(string filename, string version, out string errors) {
+    errors = "";
+    ApplicationSettings settings = new ApplicationSettings();
+    bool success = false;
+    // prepare the Xml Parser
+    Xml.Parser.init();
+    // load the document
+    Xml.Doc* doc = Xml.Parser.parse_file(filename);
+    if (null != doc) {
+      // load the root node
+      Xml.Node* root = doc->get_root_element();
+      if (null != root) {
+        // check element is ProjectSettings
+        if ("ApplicationSettings" == root->name) {
+          // check version
+          string? doc_version = XmlHelpers.get_node_attribute(root, "version");
+          if (null != doc_version && version == doc_version) {
+            // looking good let's get parsing.
+            try {
+              settings.from_xml_node(root);
+              success = true;
+            }
+            catch (GLib.Error ex) {
+              errors = "ApplicationSettings parser threw an error.";
+            }
+            finally {
+              // no op
+            }
+          }
+          else {
+            errors = "Incorrect version or unversioned document.";
+          }
+        }
+        else {
+          errors = "Not a valid settings file.";
+        }
+      }
+      else {
+        errors = "No root node found.";
+      }
+      // clean up
+      delete doc;
+    }
+    else {
+      errors = "Couldn't parse file as valid XML.";
+    }
+    // clean up the parser
+    Xml.Parser.cleanup();
+    return (success) ? settings : null;
+  }
+  
+  public static bool save_settings(string filename, ApplicationSettings settings, ref string errors) {
+    //stdout.printf("save settings called\n");
+    //stdout.flush();
+    string contents = settings.to_xml_string();
+    errors = "";
+    if (0 < contents.length) {
+      contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + contents;
+      //stdout.printf(contents);
+      //stdout.printf("\n" + filename + "\n");
+      //stdout.flush();
+      try {
+        if (FileUtils.set_contents(filename, contents, contents.length)) {
+          //stdout.printf("save settings worked\n");
+          //stdout.flush();
+          return true;
+        }
+        else {
+          errors = "Couldn't save the settings file!\n%s".printf("FILE ERROR");
+        }
+      }
+      catch (FileError ex) {
+        errors = "Couldn't save the settings file!\n%s".printf("FILE ERROR");
+      }
+    }
+    else {
+      errors = "Couldn't serialize the settings!.";
+    }
+    //stdout.printf("save settings failed\n");
+    //stdout.flush();
+    return false;
+  }
+  
+  private string to_xml_string() {
+    return ("<ApplicationSettings version=\"1.0\">\n" +
+            "<working_directory type=\"string\"><![CDATA[%s]]></working_directory>\n" +
+            "<last_project_name type=\"string\"><![CDATA[%s]]></last_project_name>\n" +
+            "<last_project_location type=\"string\"><![CDATA[%s]]></last_project_location>\n" +
+            "<named_project_name type=\"string\"><![CDATA[%s]]></named_project_name>\n" +
+            "<named_project_location type=\"string\"><![CDATA[%s]]></named_project_location>\n" +
+            "<recent_project_names type=\"CDL\"><![CDATA[%s]]></recent_project_names>\n" +
+            "<recent_project_locations type=\"CDL\"><![CDATA[%s]]></recent_project_locations>\n" +
+            "<delay_max_delay type=\"uint64\">%llu</delay_max_delay>\n" +
+            "<mixdown_enable_lamenc type=\"bool\">%s</mixdown_enable_lamenc>\n" +
+            "<mixdown_enable_mpegaudio type=\"bool\">%s</mixdown_enable_mpegaudio>\n" +
+            "<open_action type=\"OpenAction\">%s</open_action>\n" +
+            "<close_action type=\"CloseAction\">%s</close_action>\n" +
+            "<add_track_action type=\"AddTrackAction\">%s</add_track_action>\n" +
+            "<bounce_tracks_action type=\"BounceTracksAction\">%s</bounce_tracks_action>\n" +
+            "<remove_track_action type=\"RemoveTrackAction\">%s</remove_track_action>\n" +
+            "</ApplicationSettings>\n").printf(
+            working_directory,
+            last_project_name,
+            last_project_location,
+            "",
+            "",
+            "",
+            "",
+            delay_max_delay,
+            mixdown_enable_lamenc.to_string(),
+            mixdown_enable_mpegaudio.to_string(),
+            OpenAction.to_name(open_action),
+            CloseAction.to_name(close_action),
+            AddTrackAction.to_name(add_track_action),
+            BounceTracksAction.to_name(bounce_tracks_action),
+            RemoveTrackAction.to_name(remove_track_action)
+            );
+  }
+  
+  private void from_xml_node(Xml.Node* node) {
+    // Loop over the passed node's children
+    for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
+      // Spaces between tags are also nodes, discard them
+      if (iter->type != Xml.ElementType.ELEMENT_NODE) {
+        continue;
+      }
+      // Get the node's name
+      string node_name = iter->name;
+      // Get the nodes type
+      string? type = XmlHelpers.get_node_attribute(iter, "type");
+      // Get the node's content with <tags> stripped
+      if (null != type) {
+        // check the type
+        switch(type) {
+          case "string":
+            switch(node_name) {
+              case "working_directory":
+                this.working_directory = iter->get_content();
+                break;
+              case "last_project_name":
+                this.last_project_name = iter->get_content();
+                break;
+              case "last_project_location":
+                this.last_project_location = iter->get_content();
+                break;
+              case "named_project_name":
+                this.named_project_name = iter->get_content();
+                break;
+              case "named_project_location":
+                this.named_project_location = iter->get_content();
+                break;
+              default:
+                break;
+            }
+            break;
+          case "bool":
+            switch(node_name) {
+              case "mixdown_enable_lamenc":
+                this.mixdown_enable_lamenc = iter->get_content().to_bool();
+                break;
+              case "mixdown_enable_mpegaudio":
+                this.mixdown_enable_mpegaudio = iter->get_content().to_bool();
+                break;
+              default:
+                break;
+            }
+            break;
+          case "uint64":
+            switch(node_name) {
+              case "delay_max_delay":
+                this.delay_max_delay = iter->get_content().to_uint64();
+                break;
+              default:
+                break;
+            }
+            break;
+          case "CDL":
+            switch(node_name) {
+              case "recent_project_names":
+                /// TODO this.recent_project_names = iter->get_content().to_uint64();
+                break;
+              case "recent_project_locations":
+                /// TODO this.recent_project_locations = iter->get_content().to_uint64();
+                break;
+              default:
+                break;
+            }
+            break;
+          case "OpenAction":
+            switch(node_name) {
+              case "open_action":
+                this.open_action = OpenAction.from_name(iter->get_content());
+                break;
+              default:
+                break;
+            }
+            break;
+          case "CloseAction":
+            switch(node_name) {
+              case "close_action":
+                this.close_action = CloseAction.from_name(iter->get_content());
+                break;
+              default:
+                break;
+            }
+            break;
+          case "AddTrackAction":
+            switch(node_name) {
+              case "add_track_action":
+                this.add_track_action = AddTrackAction.from_name(iter->get_content());
+                break;
+              default:
+                break;
+            }
+            break;
+          case "BounceTracksAction":
+            switch(node_name) {
+              case "bounce_tracks_action":
+                this.bounce_tracks_action = BounceTracksAction.from_name(iter->get_content());
+                break;
+              default:
+                break;
+            }
+            break;
+          case "RemoveTrackAction":
+            switch(node_name) {
+              case "remove_track_action":
+                this.remove_track_action = RemoveTrackAction.from_name(iter->get_content());
+                break;
+              default:
+                break;
+            }
+            break;
+          default:
+            continue;
+        }
+      }
+      
+    }
+  }
+
+}
+
+}