using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using RainmeterStudio.Model;
using RainmeterStudio.Storage;
namespace RainmeterStudio.Business
{
public class ProjectManager
{
#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 Callbacks
///
/// Called when a project is opened or the active project closes.
///
public event EventHandler ActiveProjectChanged;
#endregion
///
/// Initializes the project manager
///
/// Project storage
public ProjectManager(ProjectStorage storage)
{
Storage = storage;
ActiveProject = null;
}
///
/// Creates a new project
///
/// Name of project
/// Path of project file
public void CreateProject(string name, string path)
{
// If there is an opened project, close it
if (ActiveProject != null)
Close();
// Create project object
ActiveProject = new Project();
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.Path, ActiveProject);
}
///
/// Closes an opened project
///
public void Close()
{
ActiveProject = null;
// Raise event
if (ActiveProjectChanged != null)
ActiveProjectChanged(this, new EventArgs());
}
}
}