using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using RainmeterStudio.Core.Model; using RainmeterStudio.Core.Storage; using RainmeterStudio.Storage; namespace RainmeterStudio.Business { public class ProjectManager { private List _projectTemplates = new List(); #region Properties /// /// Gets the currently opened project /// public Project ActiveProject { get; protected set; } /// /// Gets or sets the project storage /// protected ProjectStorage Storage { get; set; } #endregion #region Events /// /// Called when a project is opened or the active project closes. /// public event EventHandler ActiveProjectChanged; #endregion #region Constructor /// /// Initializes the project manager /// /// Project storage public ProjectManager(ProjectStorage storage) { Storage = storage; ActiveProject = null; } #endregion #region Project operations /// /// Creates a new project /// /// Name of project /// Path of project file public void CreateProject(string name, string path, IProjectTemplate template) { // If there is an opened project, close it if (ActiveProject != null) Close(); // Create project object ActiveProject = template.CreateProject(); ActiveProject.Name = name; ActiveProject.Path = path; // Save to file Directory.CreateDirectory(Path.GetDirectoryName(path)); SaveActiveProject(); // Raise event if (ActiveProjectChanged != null) ActiveProjectChanged(this, new EventArgs()); } /// /// Opens a project from disk /// /// public void OpenProject(string path) { // If there is an opened project, close it if (ActiveProject != null) Close(); // Open using storage ActiveProject = Storage.Load(path); ActiveProject.Path = path; // Raise event if (ActiveProjectChanged != null) ActiveProjectChanged(this, new EventArgs()); } /// /// Saves the changes to the current project to disk /// public void SaveActiveProject() { // Safety check if (ActiveProject == null) throw new InvalidOperationException("Cannot save a project that is not opened."); // Save Storage.Save(ActiveProject); } /// /// Closes an opened project /// public void Close() { ActiveProject = null; // Raise event if (ActiveProjectChanged != null) ActiveProjectChanged(this, new EventArgs()); } #endregion #region Document templates /// /// Registers a project template /// /// Project template public void RegisterProjectTemplate(IProjectTemplate template) { _projectTemplates.Add(template); } /// /// Gets a list of existing project templates /// public IEnumerable ProjectTemplates { get { return _projectTemplates; } } #endregion } }