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> /// </summary>
public class Project public class Project
{ {
#region Name property #region Private fields
private string _name; private string _name;
private string _path;
#endregion
#region Properties
/// <summary> /// <summary>
/// Gets or sets the name of the project /// Gets or sets the name of the project
/// </summary> /// </summary>
[XmlElement("name")]
public string Name public string Name
{ {
get get
@ -35,16 +39,9 @@ namespace RainmeterStudio.Core.Model
} }
} }
private string _path;
#endregion
#region Path property
/// <summary> /// <summary>
/// Gets or sets the file path of this project /// Gets or sets the file path of this project
/// </summary> /// </summary>
[XmlIgnore]
public string Path public string Path
{ {
get get
@ -60,137 +57,41 @@ namespace RainmeterStudio.Core.Model
} }
} }
#endregion
#region Author property
/// <summary> /// <summary>
/// Gets or sets the author of the project /// Gets or sets the author of the project
/// </summary> /// </summary>
[XmlElement("author")]
public string Author { get; set; } public string Author { get; set; }
#endregion
#region Version property
/// <summary> /// <summary>
/// Gets or sets the version of the project /// Gets or sets the version of the project
/// </summary> /// </summary>
[XmlIgnore]
public Version Version { get; set; } 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> /// <summary>
/// Gets or sets the reference to the file to automatically load at package installation /// Gets or sets the reference to the file to automatically load at package installation
/// </summary> /// </summary>
[XmlElement("autoLoadFile")]
public Reference AutoLoadFile { get; set; } public Reference AutoLoadFile { get; set; }
#endregion
#region Variable files property
/// <summary> /// <summary>
/// Gets or sets the list of variable files /// Gets or sets the list of variable files
/// </summary> /// </summary>
[XmlArray("variableFiles")]
public List<Reference> VariableFiles { get; set; } public List<Reference> VariableFiles { get; set; }
#endregion
#region Minimum rainmeter & windows properties
/// <summary> /// <summary>
/// Gets or sets the minimum rainmeter version /// Gets or sets the minimum rainmeter version
/// </summary> /// </summary>
[XmlIgnore]
public Version MinimumRainmeter { get; set; } 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> /// <summary>
/// Gets or sets the minimum Windows version /// Gets or sets the minimum Windows version
/// </summary> /// </summary>
[XmlIgnore]
public Version MinimumWindows { get; set; } 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> /// <summary>
/// Gets or sets the root node /// Gets or sets the root node
/// </summary> /// </summary>
[XmlIgnore]
public Tree<Reference> Root { get; set; } 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 #endregion
#region Constructor #region Constructor

View File

@ -11,22 +11,32 @@ namespace RainmeterStudio.Core.Model
/// </summary> /// </summary>
public class Reference public class Reference
{ {
[XmlAttribute("name")] /// <summary>
public string Name { get; set; } /// Gets the name of the reference
/// </summary>
public string Name { get; private set; }
[XmlAttribute("path")] /// <summary>
public string Path { get; set; } /// Gets the path of the reference
/// </summary>
public Reference() 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) public Reference(string name, string path = null)
{ {
Name = name; Name = name;
Path = path; 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) public override bool Equals(object obj)
{ {
var other = obj as Reference; var other = obj as Reference;
@ -39,6 +49,10 @@ namespace RainmeterStudio.Core.Model
return String.Equals(Name, other.Name) && String.Equals(Path, other.Path); 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() public override int GetHashCode()
{ {
int hash = (Name == null) ? 0 : Name.GetHashCode(); int hash = (Name == null) ? 0 : Name.GetHashCode();

View File

@ -62,6 +62,8 @@
<Compile Include="Model\Reference.cs" /> <Compile Include="Model\Reference.cs" />
<Compile Include="Model\Tree.cs" /> <Compile Include="Model\Tree.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Storage\SerializableProject.cs" />
<Compile Include="Storage\SerializableReference.cs" />
<Compile Include="Storage\SerializableTree.cs" /> <Compile Include="Storage\SerializableTree.cs" />
<Compile Include="Utils\DirectoryHelper.cs" /> <Compile Include="Utils\DirectoryHelper.cs" />
<Compile Include="Utils\InputHelper.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) IDocument IDocumentStorage.Read(string path)
{ {
TextDocument document = new TextDocument(); TextDocument document = new TextDocument();
document.Reference.Path = path; document.Reference = new Reference(Path.GetFileName(path), path);
document.Reference.Name = Path.GetFileName(path);
document.Lines.AddRange(File.ReadAllLines(path)); document.Lines.AddRange(File.ReadAllLines(path));
return document; return document;

View File

@ -176,8 +176,7 @@ namespace RainmeterStudio.Business
storage.Write(path, document); storage.Write(path, document);
// Update reference // Update reference
document.Reference.Name = Path.GetFileName(path); document.Reference = new Reference(Path.GetFileName(path), path);
document.Reference.Path = path;
// Clear dirty flag // Clear dirty flag
document.IsDirty = false; document.IsDirty = false;

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Xml.Serialization; using System.Xml.Serialization;
using RainmeterStudio.Core.Model; using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Storage;
namespace RainmeterStudio.Storage namespace RainmeterStudio.Storage
{ {
@ -16,14 +17,14 @@ namespace RainmeterStudio.Storage
var file = File.OpenText(path); var file = File.OpenText(path);
// Deserialize file // Deserialize file
var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); var serializer = new XmlSerializer(typeof(SerializableProject), new XmlRootAttribute("project"));
Project project = serializer.Deserialize(file) as Project; SerializableProject project = serializer.Deserialize(file) as SerializableProject;
if (project != null) if (project != null)
project.Path = path; project.Path = path;
// Clean up // Clean up
file.Close(); file.Close();
return project; return project.Project;
} }
public void Save(string path, Project project) public void Save(string path, Project project)
@ -32,8 +33,9 @@ namespace RainmeterStudio.Storage
var file = File.OpenWrite(path); var file = File.OpenWrite(path);
// Serialize file // Serialize file
var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); var sProject = new SerializableProject(project);
serializer.Serialize(file, project); var serializer = new XmlSerializer(typeof(SerializableProject), new XmlRootAttribute("project"));
serializer.Serialize(file, sProject);
// Clean up // Clean up
file.Close(); file.Close();

View File

@ -28,13 +28,6 @@ namespace RainmeterStudio.UI.ViewModel
{ {
return Reference.Data.Name; return Reference.Data.Name;
} }
set
{
Reference.Data.Name = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
} }
/// <summary> /// <summary>
@ -46,13 +39,6 @@ namespace RainmeterStudio.UI.ViewModel
{ {
return Reference.Data.Path; return Reference.Data.Path;
} }
set
{
Reference.Data.Path = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Path"));
}
} }