2014-07-26 07:12:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-07-27 13:21:06 +00:00
|
|
|
|
using System.IO;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2014-08-12 13:33:13 +00:00
|
|
|
|
using RainmeterStudio.Core.Model;
|
|
|
|
|
using RainmeterStudio.Core.Storage;
|
2014-07-26 10:49:11 +00:00
|
|
|
|
using RainmeterStudio.Storage;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
|
|
|
|
namespace RainmeterStudio.Business
|
|
|
|
|
{
|
|
|
|
|
public class ProjectManager
|
|
|
|
|
{
|
2014-08-15 21:39:31 +00:00
|
|
|
|
private List<IProjectTemplate> _projectTemplates = new List<IProjectTemplate>();
|
|
|
|
|
|
2014-07-26 10:49:11 +00:00
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the currently opened project
|
|
|
|
|
/// </summary>
|
2014-07-26 07:12:56 +00:00
|
|
|
|
public Project ActiveProject { get; protected set; }
|
|
|
|
|
|
2014-07-26 10:49:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the project storage
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ProjectStorage Storage { get; set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Callbacks
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when a project is opened or the active project closes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler ActiveProjectChanged;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the project manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="storage">Project storage</param>
|
|
|
|
|
public ProjectManager(ProjectStorage storage)
|
|
|
|
|
{
|
|
|
|
|
Storage = storage;
|
|
|
|
|
ActiveProject = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new project
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name of project</param>
|
|
|
|
|
/// <param name="path">Path of project file</param>
|
2014-08-16 11:35:54 +00:00
|
|
|
|
public void CreateProject(string name, string path, IProjectTemplate template)
|
2014-07-26 10:49:11 +00:00
|
|
|
|
{
|
|
|
|
|
// If there is an opened project, close it
|
|
|
|
|
if (ActiveProject != null)
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
|
|
// Create project object
|
2014-08-16 11:35:54 +00:00
|
|
|
|
ActiveProject = template.CreateProject();
|
2014-07-26 10:49:11 +00:00
|
|
|
|
ActiveProject.Name = name;
|
2014-07-27 13:21:06 +00:00
|
|
|
|
ActiveProject.Path = path;
|
2014-07-26 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
// Save to file
|
2014-07-27 13:21:06 +00:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2014-07-26 10:49:11 +00:00
|
|
|
|
SaveActiveProject();
|
|
|
|
|
|
|
|
|
|
// Raise event
|
|
|
|
|
if (ActiveProjectChanged != null)
|
|
|
|
|
ActiveProjectChanged(this, new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens a project from disk
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path"></param>
|
|
|
|
|
public void OpenProject(string path)
|
|
|
|
|
{
|
|
|
|
|
// If there is an opened project, close it
|
|
|
|
|
if (ActiveProject != null)
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
|
|
// Open using storage
|
|
|
|
|
ActiveProject = Storage.Load(path);
|
2014-07-27 13:21:06 +00:00
|
|
|
|
ActiveProject.Path = path;
|
2014-07-26 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
// Raise event
|
|
|
|
|
if (ActiveProjectChanged != null)
|
|
|
|
|
ActiveProjectChanged(this, new EventArgs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the changes to the current project to disk
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveActiveProject()
|
|
|
|
|
{
|
|
|
|
|
// Safety check
|
|
|
|
|
if (ActiveProject == null)
|
|
|
|
|
throw new InvalidOperationException("Cannot save a project that is not opened.");
|
|
|
|
|
|
|
|
|
|
// Save
|
2014-07-27 13:21:06 +00:00
|
|
|
|
Storage.Save(ActiveProject.Path, ActiveProject);
|
2014-07-26 10:49:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Closes an opened project
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Close()
|
|
|
|
|
{
|
|
|
|
|
ActiveProject = null;
|
|
|
|
|
|
|
|
|
|
// Raise event
|
|
|
|
|
if (ActiveProjectChanged != null)
|
|
|
|
|
ActiveProjectChanged(this, new EventArgs());
|
|
|
|
|
}
|
2014-08-15 21:39:31 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a project template
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="template">Project template</param>
|
|
|
|
|
public void RegisterProjectTemplate(IProjectTemplate template)
|
|
|
|
|
{
|
|
|
|
|
_projectTemplates.Add(template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IProjectTemplate> ProjectTemplates { get { return _projectTemplates; } }
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|