using System;
using System.IO;
namespace DelftTools.Utils.IO
{
///
/// DataItem that references a file.
///
/// TODO: why it is called Watcher, it looks more like a service, should be FileReference probably
///
public class FileWatcher : ICloneable
{
//todo consider making projectlocation static property.
private static string projectLocation;
private readonly FileSystemWatcher fileWatcher;
protected string filePath;
///
/// Default constructor.
///
public FileWatcher()
{
fileWatcher = FileUtils.CreateWatcher();
fileWatcher.Created += OnFileChanged;
fileWatcher.Deleted += OnFileChanged;
fileWatcher.Changed += OnFileChanged;
}
///
/// Reference to the full path.
///
public virtual string FilePath
{
get
{
return filePath;
}
set
{
filePath = value;
}
}
///
/// Reference relative to the project location, only use this when loading/saving
/// project.
///
public virtual string RelativePath
{
get
{
if (filePath != null && FileUtils.IsSubdirectory(projectLocation, filePath))
{
return FileUtils.GetRelativePath(projectLocation, filePath);
}
else
{
return filePath;
}
}
set
{
// combine the relativepath and projectlocation to form the full
//path.
if (projectLocation != null && FileUtils.PathIsRelative(value))
{
filePath = Path.Combine(projectLocation, value);
}
else
{
//use the full path.
filePath = value;
}
}
}
///
/// Projectlocation can be set from the outside indicating that the filePath should be relative.
/// Warning: only use this when saving/loading a project.
///
/// HACK: remove this property!
///
public static string ProjectLocation
{
set
{
projectLocation = Path.GetFullPath(value);
}
get
{
return projectLocation;
}
}
#region ICloneable Members
public virtual object Clone()
{
FileWatcher item = new FileWatcher();
item.FilePath = FilePath;
return item;
}
#endregion
private void OnFileChanged(object sender, FileSystemEventArgs e)
{
// TODO:DataItem
// FireChangedEvent(new DataItemChangeEventArgs(DataItemChangeAction.Value, null));
}
}
}