From 40939b906d30052491026dc3f0cba12867ad9d79 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Mon, 11 Jan 2016 14:06:47 -0800 Subject: [PATCH] Restore CloudLibrary save functionality, revise convert to AMF behavior - Fixes #111237022 - Unable to save any changes to CloudLibrary items - Add useIncrementedNameDuringTypeChange to PrintItemWrapper to control naming behavior - Pass useIncrementedName value though from provider factory all the way to PrintItemWrapper - Prevent duplicate calls to GetPrintItemWrapperAsync within the same callstack i.e. pass instance through - Only rename files when switching to AMF and use different logic for different provider types - Add path helper methods to PrintItemWrapper for clarity and conciseness in callers --- Library/LibraryRowItemPart.cs | 10 ++-- Library/Provider/LibraryProviderFileSystem.cs | 51 ++++++++++++------- Library/Provider/LibraryProviderSelector.cs | 14 ++++- PartPreviewWindow/View3D/View3DWidget.cs | 44 ++++++++-------- Queue/PrintItemWrapper.cs | 11 ++++ 5 files changed, 87 insertions(+), 43 deletions(-) diff --git a/Library/LibraryRowItemPart.cs b/Library/LibraryRowItemPart.cs index 9a1d0c8b0..da1c11f8e 100644 --- a/Library/LibraryRowItemPart.cs +++ b/Library/LibraryRowItemPart.cs @@ -240,11 +240,15 @@ namespace MatterHackers.MatterControl.PrintLibrary } } - public async void OpenPartViewWindow(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing) + public async void OpenPartViewWindow(View3DWidget.OpenMode openMode = View3DWidget.OpenMode.Viewing, PrintItemWrapper printItemWrapper = null) { if (viewingWindow == null) { - var printItemWrapper = await this.GetPrintItemWrapperAsync(); + // Only call GetPrintItemWrapperAsync if need to avoid unneeded overhead + if (printItemWrapper == null) + { + printItemWrapper = await this.GetPrintItemWrapperAsync(); + } viewingWindow = new PartPreviewMainWindow(printItemWrapper, View3DWidget.AutoRotate.Enabled, openMode); viewingWindow.Closed += new EventHandler(PartPreviewMainWindow_Closed); } @@ -433,7 +437,7 @@ namespace MatterHackers.MatterControl.PrintLibrary string pathAndFile = printItemWrapper.FileLocation; if (File.Exists(pathAndFile)) { - OpenPartViewWindow(openMode); + OpenPartViewWindow(openMode, printItemWrapper); } else { diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index 1e2d87c41..93663a2e7 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -42,26 +42,29 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderFileSystemCreator : ILibraryCreator { - string rootPath; - public string Description { get; set; } + private string rootPath; - public LibraryProviderFileSystemCreator(string rootPath, string description) + private bool useIncrementedNameDuringTypeChange; + + public LibraryProviderFileSystemCreator(string rootPath, string description, bool useIncrementedNameDuringTypeChange = false) { this.rootPath = rootPath; this.Description = description; + this.useIncrementedNameDuringTypeChange = useIncrementedNameDuringTypeChange; } - public string ProviderKey - { - get - { - return "FileSystem_" + rootPath + "_Key"; - } - } + public string Description { get; set; } + + public string ProviderKey => "FileSystem_" + rootPath + "_Key"; public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { - return new LibraryProviderFileSystem(rootPath, Description, parentLibraryProvider, setCurrentLibraryProvider); + return new LibraryProviderFileSystem( + rootPath, + Description, + parentLibraryProvider, + setCurrentLibraryProvider, + this.useIncrementedNameDuringTypeChange); } } @@ -73,8 +76,13 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider private FileSystemWatcher directoryWatcher = new FileSystemWatcher(); private string keywordFilter = string.Empty; private string rootPath; + private bool useIncrementedNameDuringTypeChange; - public LibraryProviderFileSystem(string rootPath, string name, LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) + public LibraryProviderFileSystem( + string rootPath, string name, + LibraryProvider parentLibraryProvider, + Action setCurrentLibraryProvider, + bool useIncrementedNameDuringTypeChange = false) : base(parentLibraryProvider, setCurrentLibraryProvider) { this.Name = name; @@ -82,6 +90,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider directoryWatcher.Path = rootPath; + // Indicates if the new AMF file should use the original file name incremented until no name collision occurs + this.useIncrementedNameDuringTypeChange = useIncrementedNameDuringTypeChange; + directoryWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; directoryWatcher.Changed += DiretoryContentsChanged; @@ -123,12 +134,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public override void ShareItem(int itemIndexToShare) - { + public override void ShareItem(int itemIndexToShare) + { + } - } - - public override bool CanShare { get { return false; } } + public override bool CanShare { get { return false; } } public override int ItemCount { @@ -212,7 +222,12 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { string fileName = currentDirectoryFiles[itemIndex]; - return Task.FromResult(new PrintItemWrapper(new DataStorage.PrintItem(GetPrintItemName(itemIndex), fileName), this.GetProviderLocator())); + var printItemWrapper = new PrintItemWrapper(new DataStorage.PrintItem(GetPrintItemName(itemIndex), fileName), this.GetProviderLocator()) + { + UseIncrementedNameDuringTypeChange = true + }; + + return Task.FromResult(printItemWrapper); } public override LibraryProvider GetProviderForCollection(PrintItemCollection collection) diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index 35f265a21..aacc95bc9 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -137,7 +137,12 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider string downloadsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"); if (Directory.Exists(downloadsDirectory)) { - libraryCreators.Add(new LibraryProviderFileSystemCreator(downloadsDirectory, "Downloads")); + libraryCreators.Add( + new LibraryProviderFileSystemCreator( + downloadsDirectory, + "Downloads", + useIncrementedNameDuringTypeChange: true)); + AddFolderImage("download_folder.png"); } @@ -148,7 +153,12 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { if(Directory.Exists(directory)) { - libraryCreators.Add(new LibraryProviderFileSystemCreator(directory, (new DirectoryInfo(directory).Name))); + libraryCreators.Add( + new LibraryProviderFileSystemCreator( + directory, + (new DirectoryInfo(directory).Name), + useIncrementedNameDuringTypeChange: true)); + AddFolderImage("download_folder.png"); } } diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index 6eb21f3b9..c3227c7a1 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -1937,49 +1937,50 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // If null we are replacing a file from the current print item wrapper if (returnInfo == null) { - // get a new location to save to - string tempFileNameToSaveTo = Path.Combine( - ApplicationDataStorage.Instance.ApplicationLibraryDataPath, - Path.ChangeExtension(Path.GetRandomFileName(), ".amf")); - - string outputPath; - string newFileName; - - bool keepSourceFile = false; - var fileInfo = new FileInfo(printItemWrapper.FileLocation); - if (fileInfo.Extension.Equals(".amf", StringComparison.OrdinalIgnoreCase)) + + bool requiresTypeChange = !fileInfo.Extension.Equals(".amf", StringComparison.OrdinalIgnoreCase); + if (requiresTypeChange && !printItemWrapper.UseIncrementedNameDuringTypeChange) { - outputPath = printItemWrapper.FileLocation; + // Not using incremented file name, simply change to AMF + printItemWrapper.FileLocation = Path.ChangeExtension(printItemWrapper.FileLocation, ".amf"); } - else + else if (requiresTypeChange) { + string newFileName; + string incrementedFileName; + // 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 string fileName = Path.GetFileNameWithoutExtension(fileInfo.Name); - // Ensure the original .stl or .obj is retained after the switch to .amf - keepSourceFile = true; - // Drop bracketed number sections from our source filename to ensure we don't generate something like "file (1) (1).amf" if (fileName.Contains("(")) { fileName = fileNameNumberMatch.Replace(fileName, "").Trim(); } - // Generate and search for an incrementing file name until no match is found at the target directory + // Generate and search for an incremented file name until no match is found at the target directory int foundCount = 0; do { newFileName = string.Format("{0} ({1})", fileName, ++foundCount); - outputPath = Path.Combine(fileInfo.DirectoryName, newFileName + ".amf"); + incrementedFileName = Path.Combine(fileInfo.DirectoryName, newFileName + ".amf"); // Continue incrementing while any matching file exists } while (Directory.GetFiles(fileInfo.DirectoryName, newFileName + ".*").Any()); + + // Change the FileLocation to the new AMF file + printItemWrapper.FileLocation = incrementedFileName; } try { + // get a new location to save to + string tempFileNameToSaveTo = Path.Combine( + ApplicationDataStorage.Instance.ApplicationLibraryDataPath, + Path.ChangeExtension(Path.GetRandomFileName(), ".amf")); + // save to the new temp location bool savedSuccessfully = MeshFileIo.Save(asynchMeshGroups, tempFileNameToSaveTo, outputInfo, ReportProgressChanged); @@ -1987,13 +1988,16 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (savedSuccessfully && File.Exists(tempFileNameToSaveTo)) { // Ensure the target path is clear - if(!keepSourceFile && File.Exists(printItemWrapper.FileLocation)) + if(File.Exists(printItemWrapper.FileLocation)) { File.Delete(printItemWrapper.FileLocation); } // Move the newly saved file back into place - File.Move(tempFileNameToSaveTo, outputPath); + File.Move(tempFileNameToSaveTo, printItemWrapper.FileLocation); + + // Once the file is swapped back into place, update the PrintItem to account for extension change + printItemWrapper.PrintItem.Commit(); } } catch(Exception ex) diff --git a/Queue/PrintItemWrapper.cs b/Queue/PrintItemWrapper.cs index 631f640b8..58b5c66c8 100644 --- a/Queue/PrintItemWrapper.cs +++ b/Queue/PrintItemWrapper.cs @@ -211,6 +211,16 @@ namespace MatterHackers.MatterControl.PrintQueue } } + public string GetFileExtension() + { + return Path.GetExtension(this.PrintItem.FileLocation); + } + + public string GetFileNameWithoutExtension() + { + return Path.GetFileNameWithoutExtension(this.PrintItem.FileLocation); ; + } + public string Name { get { return this.PrintItem.Name; } @@ -238,6 +248,7 @@ namespace MatterHackers.MatterControl.PrintQueue public bool SlicingHadError { get { return slicingHadError; } } public List SourceLibraryProviderLocator { get; private set; } + public bool UseIncrementedNameDuringTypeChange { get; internal set; } public void Delete() {