Added serializable project, references are now immutable.

This commit is contained in:
Tiberiu Chibici 2014-08-16 17:09:08 +03:00
parent 7f525d0d86
commit a3fdc31caa
9 changed files with 326 additions and 136 deletions

View File

@ -12,14 +12,18 @@ namespace RainmeterStudio.Core.Model
/// </summary>
public class Project
{
#region Name property
#region Private fields
private string _name;
private string _path;
#endregion
#region Properties
/// <summary>
/// Gets or sets the name of the project
/// </summary>
[XmlElement("name")]
public string Name
{
get
@ -35,16 +39,9 @@ namespace RainmeterStudio.Core.Model
}
}
private string _path;
#endregion
#region Path property
/// <summary>
/// Gets or sets the file path of this project
/// </summary>
[XmlIgnore]
public string Path
{
get
@ -60,137 +57,41 @@ namespace RainmeterStudio.Core.Model
}
}
#endregion
#region Author property
/// <summary>
/// Gets or sets the author of the project
/// </summary>
[XmlElement("author")]
public string Author { get; set; }
#endregion
#region Version property
/// <summary>
/// Gets or sets the version of the project
/// </summary>
[XmlIgnore]
public Version Version { get; set; }
/// <summary>
/// Gets or sets the string representation of the project version
/// </summary>
[XmlElement("version")]
public string VersionString
{
get
{
return Version.ToString();
}
set
{
Version = new Version(value);
}
}
#endregion
#region Auto-load file property
/// <summary>
/// Gets or sets the reference to the file to automatically load at package installation
/// </summary>
[XmlElement("autoLoadFile")]
public Reference AutoLoadFile { get; set; }
#endregion
#region Variable files property
/// <summary>
/// Gets or sets the list of variable files
/// </summary>
[XmlArray("variableFiles")]
public List<Reference> VariableFiles { get; set; }
#endregion
#region Minimum rainmeter & windows properties
/// <summary>
/// Gets or sets the minimum rainmeter version
/// </summary>
[XmlIgnore]
public Version MinimumRainmeter { get; set; }
/// <summary>
/// Gets or sets the string representation of the minimum rainmeter version
/// </summary>
[XmlElement("minimumRainmeter")]
public string MinimumRainmeterString
{
get
{
return MinimumRainmeter.ToString();
}
set
{
MinimumRainmeter = new Version(value);
}
}
/// <summary>
/// Gets or sets the minimum Windows version
/// </summary>
[XmlIgnore]
public Version MinimumWindows { get; set; }
/// <summary>
/// Gets or sets the string representation of the minimum Windows version
/// </summary>
[XmlElement("minimumWindows")]
public string MinimumWindowsString
{
get
{
return MinimumWindows.ToString();
}
set
{
MinimumWindows = new Version(value);
}
}
#endregion
#region Root property
/// <summary>
/// Gets or sets the root node
/// </summary>
[XmlIgnore]
public Tree<Reference> Root { get; set; }
/// <summary>
/// Gets or sets the serializable root node
/// </summary>
/// <remarks>Warning: not efficient</remarks>
[XmlElement("root")]
public SerializableTree<Reference> SerializableRoot
{
get
{
return Root.AsSerializableTree();
}
set
{
Root = value.AsTree();
}
}
#endregion
#region Constructor

View File

@ -11,22 +11,32 @@ namespace RainmeterStudio.Core.Model
/// </summary>
public class Reference
{
[XmlAttribute("name")]
public string Name { get; set; }
/// <summary>
/// Gets the name of the reference
/// </summary>
public string Name { get; private set; }
[XmlAttribute("path")]
public string Path { get; set; }
public Reference()
{
}
/// <summary>
/// Gets the path of the reference
/// </summary>
public string Path { get; private set; }
/// <summary>
/// Initializes the reference
/// </summary>
/// <param name="name">Name of reference</param>
/// <param name="path">Path to item referenced</param>
public Reference(string name, string path = null)
{
Name = name;
Path = path;
}
/// <summary>
/// Compares a reference to another objects
/// </summary>
/// <param name="obj">Another object</param>
/// <returns>True if objects are equal</returns>
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);
}
/// <summary>
/// Obtains the hash code of this reference
/// </summary>
/// <returns>Hash code</returns>
public override int GetHashCode()
{
int hash = (Name == null) ? 0 : Name.GetHashCode();

View File

@ -62,6 +62,8 @@
<Compile Include="Model\Reference.cs" />
<Compile Include="Model\Tree.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Storage\SerializableProject.cs" />
<Compile Include="Storage\SerializableReference.cs" />
<Compile Include="Storage\SerializableTree.cs" />
<Compile Include="Utils\DirectoryHelper.cs" />
<Compile Include="Utils\InputHelper.cs" />

View File

@ -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
{
/// <summary>
/// Helper class to serialize or deserialize project objects
/// </summary>
public class SerializableProject
{
private Project _project;
private SerializableReference _autoLoadFile;
private List<SerializableReference> _variableFiles;
private SerializableTree<SerializableReference> _root;
/// <summary>
/// Gets or sets the project
/// </summary>
[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();
}
}
/// <summary>
/// Gets or sets the project name
/// </summary>
[XmlElement("name")]
public string Name
{
get
{
return Project.Name;
}
set
{
Project.Name = value;
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the project path
/// </summary>
[XmlIgnore]
public string Path
{
get
{
return Project.Path;
}
set
{
Project.Path = value;
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the author of the project
/// </summary>
[XmlElement("author")]
public string Author
{
get
{
return Project.Author;
}
set
{
Project.Author = value;
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the project version
/// </summary>
[XmlElement("version")]
public string Version
{
get
{
return Project.Version.ToString();
}
set
{
Project.Version = new Version(value);
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the reference to the file to automatically load at package installation
/// </summary>
[XmlElement("autoLoadFile")]
public SerializableReference AutoLoadFile
{
get
{
return _autoLoadFile;
}
set
{
_autoLoadFile = value;
}
}
/// <summary>
/// Gets or sets the list of variable files
/// </summary>
[XmlArray("variableFiles")]
public List<SerializableReference> VariableFiles
{
get
{
return _variableFiles;
}
set
{
_variableFiles = value;
}
}
/// <summary>
/// Gets or sets the minimum rainmeter version
/// </summary>
public string MinimumRainmeter
{
get
{
return Project.MinimumRainmeter.ToString();
}
set
{
Project.MinimumRainmeter = new Version(value);
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the minimum Windows version
/// </summary>
public string MinimumWindows
{
get
{
return Project.MinimumWindows.ToString();
}
set
{
Project.MinimumWindows = new Version(value);
UpdateSelf();
}
}
/// <summary>
/// Gets or sets the root node
/// </summary>
public SerializableTree<SerializableReference> Root
{
get
{
return _root;
}
set
{
_root = value;
}
}
/// <summary>
/// Initializes the serializable project
/// </summary>
public SerializableProject()
{
Project = new Project();
}
/// <summary>
/// Initializes the serializable project
/// </summary>
/// <param name="project">Base project</param>
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();
}
}
}

View File

@ -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
{
/// <summary>
/// Represents a reference that can be serialized
/// </summary>
public class SerializableReference
{
/// <summary>
/// Gets or sets the name of the reference
/// </summary>
[XmlElement("name")]
public string Name
{
get
{
if (Reference != null)
return Reference.Name;
return null;
}
set
{
Reference = new Reference(value, Path);
}
}
/// <summary>
/// Gets or sets the path of the reference
/// </summary>
[XmlElement("path")]
public string Path
{
get
{
if (Reference != null)
return Reference.Path;
return null;
}
set
{
Reference = new Reference(Name, value);
}
}
/// <summary>
/// Gets or sets the (immutable) reference
/// </summary>
[XmlIgnore]
public Reference Reference
{
get;
set;
}
/// <summary>
/// Initializes this serializable reference
/// </summary>
public SerializableReference()
{
}
public SerializableReference(Reference reference)
{
Reference = reference;
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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"));
}
}
/// <summary>
@ -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"));
}
}