Use FileSystemContainer base on history containers

- Issue MatterHackers/MCCentral#3368
Add common FileSystemContainer delete logic to PlatingHistoryContainer
This commit is contained in:
John Lewin 2018-05-14 17:00:00 -07:00
parent 7f2cf2ecae
commit 6b64a36b6c
3 changed files with 22 additions and 60 deletions

View file

@ -41,17 +41,15 @@ namespace MatterHackers.MatterControl.Library
{
public class FileSystemContainer : WritableContainer
{
private string fullPath;
private FileSystemWatcher directoryWatcher;
private bool isActiveContainer;
private bool isDirty;
public FileSystemContainer(string path)
public FileSystemContainer(string fullPath)
{
this.fullPath = path;
this.Name = Path.GetFileName(path);
this.FullPath = fullPath;
this.Name = Path.GetFileName(fullPath);
this.IsProtected = false;
@ -59,9 +57,9 @@ namespace MatterHackers.MatterControl.Library
this.Items = new List<ILibraryItem>();
#if !__ANDROID__
if (AggContext.OperatingSystem == OSType.Windows
&& Directory.Exists(path))
&& Directory.Exists(fullPath))
{
directoryWatcher = new FileSystemWatcher(path);
directoryWatcher = new FileSystemWatcher(fullPath);
directoryWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
directoryWatcher.Changed += DirectoryContentsChanged;
directoryWatcher.Created += DirectoryContentsChanged;
@ -75,6 +73,8 @@ namespace MatterHackers.MatterControl.Library
#endif
}
public string FullPath { get; protected set; }
// Indicates if the new AMF file should use the original file name incremented until no name collision occurs
public bool UseIncrementedNameDuringTypeChange { get; internal set; }
@ -142,7 +142,7 @@ namespace MatterHackers.MatterControl.Library
{
string filter = this.KeywordFilter.Trim();
var allFiles = Directory.GetFiles(fullPath, "*.*", searchDepth);
var allFiles = Directory.GetFiles(FullPath, "*.*", searchDepth);
var zipFiles = allFiles.Where(f => Path.GetExtension(f).IndexOf(".zip", StringComparison.OrdinalIgnoreCase) != -1);
@ -151,7 +151,7 @@ namespace MatterHackers.MatterControl.Library
List<ILibraryContainerLink> containers;
if (filter == "")
{
var directories = Directory.GetDirectories(fullPath, "*.*", searchDepth).Select(p => new DirectoryContainerLink(p)).ToList<ILibraryContainerLink>();
var directories = Directory.GetDirectories(FullPath, "*.*", searchDepth).Select(p => new DirectoryContainerLink(p)).ToList<ILibraryContainerLink>();
containers = directories.Concat(zipFiles.Select(f => new LocalZipContainerLink(f))).OrderBy(d => d.Name).ToList();
}
else
@ -205,7 +205,7 @@ namespace MatterHackers.MatterControl.Library
{
// Switching from .stl, .obj or similar to AMF. Save the file and update the
// the filename with an incremented (n) value to reflect the extension change in the UI
var similarFileNames = Directory.GetFiles(this.fullPath, $"{Path.GetFileNameWithoutExtension(fileName)}.*");
var similarFileNames = Directory.GetFiles(this.FullPath, $"{Path.GetFileNameWithoutExtension(fileName)}.*");
// ;
var validName = agg_basics.GetNonCollidingName(fileName, (testName) => !File.Exists(testName));
@ -222,7 +222,7 @@ namespace MatterHackers.MatterControl.Library
directoryWatcher.EnableRaisingEvents = false;
Directory.CreateDirectory(this.fullPath);
Directory.CreateDirectory(this.FullPath);
await Task.Run(async () =>
{
@ -231,7 +231,7 @@ namespace MatterHackers.MatterControl.Library
switch (item)
{
case CreateFolderItem newFolder:
string targetFolderPath = Path.Combine(this.fullPath, newFolder.Name);
string targetFolderPath = Path.Combine(this.FullPath, newFolder.Name);
// TODO: write adaption of GetNonCollidingName for directories
Directory.CreateDirectory(targetFolderPath);
@ -240,7 +240,7 @@ namespace MatterHackers.MatterControl.Library
break;
case ILibraryAssetStream streamItem:
string targetPath = Path.Combine(this.fullPath, streamItem.FileName);
string targetPath = Path.Combine(this.FullPath, streamItem.FileName);
try
{
@ -290,7 +290,7 @@ namespace MatterHackers.MatterControl.Library
}
else
{
Process.Start(this.fullPath);
Process.Start(this.FullPath);
}
}
}
@ -301,7 +301,7 @@ namespace MatterHackers.MatterControl.Library
{
if (Directory.Exists(directoryLink.Path))
{
Process.Start(this.fullPath);
Process.Start(this.FullPath);
}
}
else if (item is FileSystemFileItem fileItem)

View file

@ -27,13 +27,6 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
@ -43,9 +36,9 @@ namespace MatterHackers.MatterControl.Library
public class PartHistoryContainer : HistoryContainerBase
{
public PartHistoryContainer()
:base (ApplicationDataStorage.Instance.PartHistoryDirectory)
{
this.Name = "Part History".Localize();
this.FullPath = ApplicationDataStorage.Instance.PartHistoryDirectory;
}
}
}

View file

@ -32,7 +32,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using MatterHackers.Agg.Image;
using MatterHackers.DataConverters3D;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.DataStorage;
@ -40,32 +39,27 @@ namespace MatterHackers.MatterControl.Library
{
public class PlatingHistoryContainer : HistoryContainerBase
{
public PlatingHistoryContainer()
public PlatingHistoryContainer():
base (ApplicationDataStorage.Instance.PlatingDirectory)
{
this.Name = "Plating History".Localize();
this.FullPath = ApplicationDataStorage.Instance.PlatingDirectory;
}
}
public class HistoryContainerBase : LibraryContainer, ILibraryWritableContainer
public class HistoryContainerBase : FileSystemContainer, ILibraryWritableContainer
{
public HistoryContainerBase()
public HistoryContainerBase(string fullPath)
: base(fullPath)
{
this.ChildContainers = new List<ILibraryContainerLink>();
this.Items = new List<ILibraryItem>();
this.IsProtected = true;
}
public string FullPath { get; protected set; }
public int PageSize { get; set; } = 25;
public event EventHandler<ItemChangedEventArgs> ItemContentChanged;
public void Add(IEnumerable<ILibraryItem> items)
{
throw new NotImplementedException();
}
public bool AllowAction(ContainerActions containerActions)
{
switch(containerActions)
@ -93,31 +87,6 @@ namespace MatterHackers.MatterControl.Library
}
}
public void Move(IEnumerable<ILibraryItem> items, ILibraryWritableContainer targetContainer)
{
throw new NotImplementedException();
}
public void Remove(IEnumerable<ILibraryItem> items)
{
throw new NotImplementedException();
}
public void Rename(ILibraryItem item, string revisedName)
{
throw new NotImplementedException();
}
public void Save(ILibraryItem item, IObject3D content)
{
if (item is FileSystemFileItem fileItem)
{
// Serialize the scene to disk using a modified Json.net pipeline with custom ContractResolvers and JsonConverters
File.WriteAllText(fileItem.Path, content.ToJson());
this.ItemContentChanged?.Invoke(this, new ItemChangedEventArgs(fileItem));
}
}
public void SetThumbnail(ILibraryItem item, int width, int height, ImageBuffer imageBuffer)
{
}