From a3fdc31caad97059f22432256a759cf034f75d65 Mon Sep 17 00:00:00 2001 From: Tiberiu Chibici Date: Sat, 16 Aug 2014 17:09:08 +0300 Subject: [PATCH] Added serializable project, references are now immutable. --- RainmeterStudio.Core/Model/Project.cs | 111 +-------- RainmeterStudio.Core/Model/Reference.cs | 30 ++- .../RainmeterStudio.Core.csproj | 2 + .../Storage/SerializableProject.cs | 212 ++++++++++++++++++ .../Storage/SerializableReference.cs | 75 +++++++ RainmeterStudio.TextEditor/TextStorage.cs | 3 +- RainmeterStudio/Business/DocumentManager.cs | 3 +- RainmeterStudio/Storage/ProjectStorage.cs | 12 +- .../UI/ViewModel/ReferenceViewModel.cs | 14 -- 9 files changed, 326 insertions(+), 136 deletions(-) create mode 100644 RainmeterStudio.Core/Storage/SerializableProject.cs create mode 100644 RainmeterStudio.Core/Storage/SerializableReference.cs diff --git a/RainmeterStudio.Core/Model/Project.cs b/RainmeterStudio.Core/Model/Project.cs index e7b4fcf0..ee40b754 100644 --- a/RainmeterStudio.Core/Model/Project.cs +++ b/RainmeterStudio.Core/Model/Project.cs @@ -12,14 +12,18 @@ namespace RainmeterStudio.Core.Model /// public class Project { - #region Name property + #region Private fields private string _name; + private string _path; + + #endregion + + #region Properties /// /// Gets or sets the name of the project /// - [XmlElement("name")] public string Name { get @@ -35,16 +39,9 @@ namespace RainmeterStudio.Core.Model } } - private string _path; - - #endregion - - #region Path property - /// /// Gets or sets the file path of this project /// - [XmlIgnore] public string Path { get @@ -60,137 +57,41 @@ namespace RainmeterStudio.Core.Model } } - #endregion - - #region Author property - /// /// Gets or sets the author of the project /// - [XmlElement("author")] public string Author { get; set; } - #endregion - - #region Version property - /// /// Gets or sets the version of the project /// - [XmlIgnore] public Version Version { get; set; } - /// - /// Gets or sets the string representation of the project version - /// - [XmlElement("version")] - public string VersionString - { - get - { - return Version.ToString(); - } - set - { - Version = new Version(value); - } - } - - #endregion - - #region Auto-load file property - /// /// Gets or sets the reference to the file to automatically load at package installation /// - [XmlElement("autoLoadFile")] public Reference AutoLoadFile { get; set; } - #endregion - - #region Variable files property - /// /// Gets or sets the list of variable files /// - [XmlArray("variableFiles")] public List VariableFiles { get; set; } - #endregion - - #region Minimum rainmeter & windows properties - /// /// Gets or sets the minimum rainmeter version /// - [XmlIgnore] public Version MinimumRainmeter { get; set; } - /// - /// Gets or sets the string representation of the minimum rainmeter version - /// - [XmlElement("minimumRainmeter")] - public string MinimumRainmeterString - { - get - { - return MinimumRainmeter.ToString(); - } - set - { - MinimumRainmeter = new Version(value); - } - } - /// /// Gets or sets the minimum Windows version /// - [XmlIgnore] public Version MinimumWindows { get; set; } - /// - /// Gets or sets the string representation of the minimum Windows version - /// - [XmlElement("minimumWindows")] - public string MinimumWindowsString - { - get - { - return MinimumWindows.ToString(); - } - set - { - MinimumWindows = new Version(value); - } - } - - #endregion - - #region Root property - /// /// Gets or sets the root node /// - [XmlIgnore] public Tree Root { get; set; } - /// - /// Gets or sets the serializable root node - /// - /// Warning: not efficient - [XmlElement("root")] - public SerializableTree SerializableRoot - { - get - { - return Root.AsSerializableTree(); - } - set - { - Root = value.AsTree(); - } - } - #endregion #region Constructor diff --git a/RainmeterStudio.Core/Model/Reference.cs b/RainmeterStudio.Core/Model/Reference.cs index 2f9e14b0..d78424a5 100644 --- a/RainmeterStudio.Core/Model/Reference.cs +++ b/RainmeterStudio.Core/Model/Reference.cs @@ -11,22 +11,32 @@ namespace RainmeterStudio.Core.Model /// public class Reference { - [XmlAttribute("name")] - public string Name { get; set; } + /// + /// Gets the name of the reference + /// + public string Name { get; private set; } - [XmlAttribute("path")] - public string Path { get; set; } - - public Reference() - { - } + /// + /// Gets the path of the reference + /// + public string Path { get; private set; } + /// + /// Initializes the reference + /// + /// Name of reference + /// Path to item referenced public Reference(string name, string path = null) { Name = name; Path = path; } + /// + /// Compares a reference to another objects + /// + /// Another object + /// True if objects are equal public override bool Equals(object obj) { var other = obj as Reference; @@ -39,6 +49,10 @@ namespace RainmeterStudio.Core.Model return String.Equals(Name, other.Name) && String.Equals(Path, other.Path); } + /// + /// Obtains the hash code of this reference + /// + /// Hash code public override int GetHashCode() { int hash = (Name == null) ? 0 : Name.GetHashCode(); diff --git a/RainmeterStudio.Core/RainmeterStudio.Core.csproj b/RainmeterStudio.Core/RainmeterStudio.Core.csproj index aa876854..98a0758f 100644 --- a/RainmeterStudio.Core/RainmeterStudio.Core.csproj +++ b/RainmeterStudio.Core/RainmeterStudio.Core.csproj @@ -62,6 +62,8 @@ + + diff --git a/RainmeterStudio.Core/Storage/SerializableProject.cs b/RainmeterStudio.Core/Storage/SerializableProject.cs new file mode 100644 index 00000000..7d590273 --- /dev/null +++ b/RainmeterStudio.Core/Storage/SerializableProject.cs @@ -0,0 +1,212 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Serialization; +using RainmeterStudio.Core.Model; +using RainmeterStudio.Core.Utils; + +namespace RainmeterStudio.Core.Storage +{ + /// + /// Helper class to serialize or deserialize project objects + /// + public class SerializableProject + { + private Project _project; + private SerializableReference _autoLoadFile; + private List _variableFiles; + private SerializableTree _root; + + /// + /// Gets or sets the project + /// + [XmlIgnore] + public Project Project + { + get + { + _project.AutoLoadFile = _autoLoadFile.Reference; + _project.VariableFiles = _variableFiles.Select(x => x.Reference).ToList(); + _project.Root = _root.AsTree().TransformData(x => x.Reference); + return _project; + } + set + { + _project = value; + UpdateSelf(); + } + } + + /// + /// Gets or sets the project name + /// + [XmlElement("name")] + public string Name + { + get + { + return Project.Name; + } + set + { + Project.Name = value; + UpdateSelf(); + } + } + + /// + /// Gets or sets the project path + /// + [XmlIgnore] + public string Path + { + get + { + return Project.Path; + } + set + { + Project.Path = value; + UpdateSelf(); + } + } + + /// + /// Gets or sets the author of the project + /// + [XmlElement("author")] + public string Author + { + get + { + return Project.Author; + } + set + { + Project.Author = value; + UpdateSelf(); + } + } + + /// + /// Gets or sets the project version + /// + [XmlElement("version")] + public string Version + { + get + { + return Project.Version.ToString(); + } + set + { + Project.Version = new Version(value); + UpdateSelf(); + } + } + + /// + /// Gets or sets the reference to the file to automatically load at package installation + /// + [XmlElement("autoLoadFile")] + public SerializableReference AutoLoadFile + { + get + { + return _autoLoadFile; + } + set + { + _autoLoadFile = value; + } + } + + /// + /// Gets or sets the list of variable files + /// + [XmlArray("variableFiles")] + public List VariableFiles + { + get + { + return _variableFiles; + } + set + { + _variableFiles = value; + } + } + + /// + /// Gets or sets the minimum rainmeter version + /// + public string MinimumRainmeter + { + get + { + return Project.MinimumRainmeter.ToString(); + } + set + { + Project.MinimumRainmeter = new Version(value); + UpdateSelf(); + } + } + + /// + /// Gets or sets the minimum Windows version + /// + public string MinimumWindows + { + get + { + return Project.MinimumWindows.ToString(); + } + set + { + Project.MinimumWindows = new Version(value); + UpdateSelf(); + } + } + + /// + /// Gets or sets the root node + /// + public SerializableTree Root + { + get + { + return _root; + } + set + { + _root = value; + } + } + + /// + /// Initializes the serializable project + /// + public SerializableProject() + { + Project = new Project(); + } + + /// + /// Initializes the serializable project + /// + /// Base project + public SerializableProject(Project project) + { + Project = project; + } + + private void UpdateSelf() + { + _autoLoadFile = new SerializableReference(_project.AutoLoadFile); + _variableFiles = _project.VariableFiles.Select(x => new SerializableReference(x)).ToList(); + _root = _project.Root.TransformData(x => new SerializableReference(x)).AsSerializableTree(); + } + } +} diff --git a/RainmeterStudio.Core/Storage/SerializableReference.cs b/RainmeterStudio.Core/Storage/SerializableReference.cs new file mode 100644 index 00000000..880fb4bc --- /dev/null +++ b/RainmeterStudio.Core/Storage/SerializableReference.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.Serialization; +using RainmeterStudio.Core.Model; + +namespace RainmeterStudio.Core.Storage +{ + /// + /// Represents a reference that can be serialized + /// + public class SerializableReference + { + /// + /// Gets or sets the name of the reference + /// + [XmlElement("name")] + public string Name + { + get + { + if (Reference != null) + return Reference.Name; + + return null; + } + set + { + Reference = new Reference(value, Path); + } + } + + /// + /// Gets or sets the path of the reference + /// + [XmlElement("path")] + public string Path + { + get + { + if (Reference != null) + return Reference.Path; + + return null; + } + set + { + Reference = new Reference(Name, value); + } + } + + /// + /// Gets or sets the (immutable) reference + /// + [XmlIgnore] + public Reference Reference + { + get; + set; + } + + /// + /// Initializes this serializable reference + /// + public SerializableReference() + { + } + + public SerializableReference(Reference reference) + { + Reference = reference; + } + } +} diff --git a/RainmeterStudio.TextEditor/TextStorage.cs b/RainmeterStudio.TextEditor/TextStorage.cs index 445334fe..bee9d4b5 100644 --- a/RainmeterStudio.TextEditor/TextStorage.cs +++ b/RainmeterStudio.TextEditor/TextStorage.cs @@ -16,8 +16,7 @@ namespace RainmeterStudio.TextEditorPlugin IDocument IDocumentStorage.Read(string path) { TextDocument document = new TextDocument(); - document.Reference.Path = path; - document.Reference.Name = Path.GetFileName(path); + document.Reference = new Reference(Path.GetFileName(path), path); document.Lines.AddRange(File.ReadAllLines(path)); return document; diff --git a/RainmeterStudio/Business/DocumentManager.cs b/RainmeterStudio/Business/DocumentManager.cs index 4f667fd0..b69e48c8 100644 --- a/RainmeterStudio/Business/DocumentManager.cs +++ b/RainmeterStudio/Business/DocumentManager.cs @@ -176,8 +176,7 @@ namespace RainmeterStudio.Business storage.Write(path, document); // Update reference - document.Reference.Name = Path.GetFileName(path); - document.Reference.Path = path; + document.Reference = new Reference(Path.GetFileName(path), path); // Clear dirty flag document.IsDirty = false; diff --git a/RainmeterStudio/Storage/ProjectStorage.cs b/RainmeterStudio/Storage/ProjectStorage.cs index f8526815..8b45a0c5 100644 --- a/RainmeterStudio/Storage/ProjectStorage.cs +++ b/RainmeterStudio/Storage/ProjectStorage.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Xml.Serialization; using RainmeterStudio.Core.Model; +using RainmeterStudio.Core.Storage; namespace RainmeterStudio.Storage { @@ -16,14 +17,14 @@ namespace RainmeterStudio.Storage var file = File.OpenText(path); // Deserialize file - var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); - Project project = serializer.Deserialize(file) as Project; + var serializer = new XmlSerializer(typeof(SerializableProject), new XmlRootAttribute("project")); + SerializableProject project = serializer.Deserialize(file) as SerializableProject; if (project != null) project.Path = path; // Clean up file.Close(); - return project; + return project.Project; } public void Save(string path, Project project) @@ -32,8 +33,9 @@ namespace RainmeterStudio.Storage var file = File.OpenWrite(path); // Serialize file - var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); - serializer.Serialize(file, project); + var sProject = new SerializableProject(project); + var serializer = new XmlSerializer(typeof(SerializableProject), new XmlRootAttribute("project")); + serializer.Serialize(file, sProject); // Clean up file.Close(); diff --git a/RainmeterStudio/UI/ViewModel/ReferenceViewModel.cs b/RainmeterStudio/UI/ViewModel/ReferenceViewModel.cs index 3ef23576..b9a8c984 100644 --- a/RainmeterStudio/UI/ViewModel/ReferenceViewModel.cs +++ b/RainmeterStudio/UI/ViewModel/ReferenceViewModel.cs @@ -28,13 +28,6 @@ namespace RainmeterStudio.UI.ViewModel { return Reference.Data.Name; } - set - { - Reference.Data.Name = value; - - if (PropertyChanged != null) - PropertyChanged(this, new PropertyChangedEventArgs("Name")); - } } /// @@ -46,13 +39,6 @@ namespace RainmeterStudio.UI.ViewModel { return Reference.Data.Path; } - set - { - Reference.Data.Path = value; - - if (PropertyChanged != null) - PropertyChanged(this, new PropertyChangedEventArgs("Path")); - } }