using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using IReaper.FileData;
using System.Reflection;
using IReaper.Properties;
namespace IReaper.PathManager{
class Manager
{
/// <summary>
/// PathManager
/// </summary>
static Dictionary<PathPolicyType, IManager> managerDic;
/// <summary>
///
/// </summary>
static Manager()
{
managerDic = new Dictionary<PathPolicyType, IManager>();
Settings.Default.SettingChanging += new SettingChangingEventHandler(Default_SettingChanging);
LoadCommandFromFile();
policy = (PathPolicyType) Settings.Default.PathPolicy;
}
static void Default_SettingChanging(object sender, SettingChangingEventArgs e)
{
if (e.SettingName == "PathPolicy")
{
if (Enum.IsDefined(typeof(PathPolicyType), Settings.Default.PathPolicy))
{
policy = (PathPolicyType)e.NewValue;
}
}
}
static void InitlizePathPolicy()
{
if (Enum.IsDefined(typeof(PathPolicyType), Settings.Default.PathPolicy))
{
policy = (PathPolicyType)Settings.Default.PathPolicy;
}
else
{
policy = PathPolicyType.Plain;
}
}
/// <summary>
///
/// </summary>
public static PathPolicyType policy;
/// <summary>
/// <see cref="CourseFileData"/>
/// </summary>
/// <param name="cFile"><see cref="CourseFileData"/></param>
/// <returns></returns>
public static string GetRelativePath(CourseFileData cFile)
{
return GetRelativePath(cFile, policy);
}
/// <summary>
/// ,<see cref="CourseFileData"/>
/// </summary>
/// <param name="cFile"><see cref="CourseFileData"/></param>
/// <param name="Policy">Policy</param>
/// <returns></returns>
public static string GetRelativePath(CourseFileData cFile,PathPolicyType Policy)
{
IManager manager = managerDic[Policy];
if (manager == null)
return null;
else
{
string path = manager.GetRelativePath(cFile);
if (path.Length > 0 && path[0] == '.')
{
// This is a work-around for PSP related files.
// PSP filesystem cannot process folders start with '.' as its name.
// hence a _ is added.
path = '_' + path;
}
return path;
}
}
private static void LoadCommandFromFile()
{
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
for (int i = 0; i < types.Length; i++)
{
Type t = types[i];
if (typeof(IManager).IsAssignableFrom(t) && !t.IsInterface)
{
IManager manager = Activator.CreateInstance(t) as IManager;
if (manager != null)
RegisteManager(manager);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="Family"></param>
/// <param name="command"></param>
internal static void RegisteManager(IManager manager)
{
if (!managerDic.ContainsKey(manager.Policy))
managerDic.Add(manager.Policy,manager);
}
}
}
|