diff --git a/RainmeterStudio/Editor/ProjectEditor/ProjectStorage.cs b/RainmeterStudio/Editor/ProjectEditor/ProjectStorage.cs new file mode 100644 index 00000000..8c8704a3 --- /dev/null +++ b/RainmeterStudio/Editor/ProjectEditor/ProjectStorage.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml.Serialization; +using RainmeterStudio.Core; +using RainmeterStudio.Core.Model; +using RainmeterStudio.Core.Storage; + +namespace RainmeterStudio.Editor.ProjectEditor +{ + /// + /// Project storage, loads and saves project files + /// + [PluginExport] + public class ProjectStorage : IDocumentStorage + { + /// + /// Loads a project from file + /// + /// Path to file to load + /// Loaded project + public Project Read(string path) + { + // Open file + var file = File.OpenText(path); + + // Deserialize file + var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); + Project project = serializer.Deserialize(file) as Project; + + if (project != null) + { + project.Path = path; + } + + // Clean up + file.Close(); + return project; + } + + /// + /// Saves a project to file + /// + /// Project to save + /// File to save to + public void Write(Project project, string path) + { + // Open file + var file = File.OpenWrite(path); + + // Serialize file + var serializer = new XmlSerializer(typeof(Project), new XmlRootAttribute("project")); + serializer.Serialize(file, project); + + // Clean up + file.Close(); + project.Path = path; + } + + /// + /// Saves a project + /// + /// Saves a project to the path specified in the 'Path' property + public void Write(Project project) + { + Write(project, project.Path); + } + + /// + /// Reads the project as a ProjectDocument. + /// Use Load to get only the Project. + /// + /// Path to project file + /// A project document + public IDocument ReadDocument(string path) + { + Project project = Read(path); + var document = new ProjectDocument(project); + document.Reference = new Reference(Path.GetFileName(path), path); + + return document; + } + + /// + /// Writes a project document to file + /// + /// + /// + public void WriteDocument(IDocument document, string path) + { + var projectDocument = (ProjectDocument)document; + Write(projectDocument.Project, path); + } + + /// + /// Returns true if the file is a project storage + /// + /// Path to file + /// True if the file can be read by this storage + public bool CanReadDocument(string path) + { + return (Path.GetExtension(path) == ".rsproj"); + } + + /// + /// Returns true if this can write specified document type + /// + /// Document type + /// True if document can be written by this storage + public bool CanWriteDocument(Type documentType) + { + return documentType.Equals(typeof(ProjectDocument)); + } + } +}