diff --git a/Library/Provider/LibraryProvider.cs b/Library/Provider/LibraryProvider.cs index 1dbbeae5a..87d3ec49b 100644 --- a/Library/Provider/LibraryProvider.cs +++ b/Library/Provider/LibraryProvider.cs @@ -35,6 +35,7 @@ using MatterHackers.PolygonMesh; using System; using System.Collections.Generic; using System.ComponentModel; +using System.IO; using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary.Provider @@ -68,6 +69,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } + public void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) + { + foreach (string file in files) + { + AddItem(new PrintItemWrapper(new PrintItem(Path.GetFileNameWithoutExtension(file), file), this)); + } + } + // A key,value list that threads into the current collection looks like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". public List GetProviderLocator() { @@ -86,7 +95,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider #region Abstract Methods + public abstract void AddItem(PrintItemWrapper itemToAdd); + public abstract void Dispose(); + public abstract int CollectionCount { get; } public abstract int ItemCount { get; } @@ -101,10 +113,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract void AddCollectionToLibrary(string collectionName); - public abstract void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null); - - public abstract void AddItem(PrintItemWrapper itemToAdd); - public abstract PrintItemCollection GetCollectionItem(int collectionIndex); public abstract Task GetPrintItemWrapperAsync(int itemIndex, ReportProgressRatio reportProgress = null); @@ -117,8 +125,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract void RemoveItem(int itemIndexToRemove); - public abstract void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath = null); - #endregion Abstract Methods #region Static Methods diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index af365b515..23e96be57 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -152,20 +152,15 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) + public override void AddItem(PrintItemWrapper itemToAdd) { string destPath = rootPath; - CopyAllFiles(files, destPath); + CopyFile(itemToAdd.FileLocation, destPath); GetFilesAndCollectionsInCurrentDirectory(); } - public override void AddItem(PrintItemWrapper itemToAdd) - { - throw new NotImplementedException(); - } - public override PrintItemCollection GetCollectionItem(int collectionIndex) { string directoryName = currentDirectoryDirectories[collectionIndex]; @@ -213,12 +208,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider GetFilesAndCollectionsInCurrentDirectory(); } - public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath) - { - throw new NotImplementedException(); - } - - private static void CopyAllFiles(IList files, string destPath) + private static void CopyFile(string file, string destPath) { // make sure the directory exists try @@ -230,46 +220,43 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } // save it to the root directory - foreach (string file in files) + string outputFileName = Path.Combine(destPath, Path.GetFileName(file)); + // and copy the file + try { - string outputFileName = Path.Combine(destPath, Path.GetFileName(file)); - // and copy the file - try + if (!File.Exists(outputFileName)) { - if (!File.Exists(outputFileName)) - { - File.Copy(file, outputFileName); - } - else // make a new file and append a number so that we are not destructive - { - string directory = Path.GetDirectoryName(outputFileName); - string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(outputFileName); - string extension = Path.GetExtension(outputFileName); - // get the filename without a number on the end - int lastSpaceIndex = fileNameWithoutExtension.LastIndexOf(' '); - if (lastSpaceIndex != -1) - { - int endingNumber; - // check if the last set of characters is a number - if (int.TryParse(fileNameWithoutExtension.Substring(lastSpaceIndex), out endingNumber)) - { - fileNameWithoutExtension = fileNameWithoutExtension.Substring(0, lastSpaceIndex); - } - } - int numberToAppend = 2; - string fileNameToUse = Path.Combine(directory, fileNameWithoutExtension + " " + numberToAppend.ToString() + extension); - while (File.Exists(fileNameToUse)) - { - numberToAppend++; - fileNameToUse = Path.Combine(directory, fileNameWithoutExtension + " " + numberToAppend.ToString() + extension); - } - File.Copy(file, fileNameToUse); - } + File.Copy(file, outputFileName); } - catch (Exception e) + else // make a new file and append a number so that we are not destructive { + string directory = Path.GetDirectoryName(outputFileName); + string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(outputFileName); + string extension = Path.GetExtension(outputFileName); + // get the filename without a number on the end + int lastSpaceIndex = fileNameWithoutExtension.LastIndexOf(' '); + if (lastSpaceIndex != -1) + { + int endingNumber; + // check if the last set of characters is a number + if (int.TryParse(fileNameWithoutExtension.Substring(lastSpaceIndex), out endingNumber)) + { + fileNameWithoutExtension = fileNameWithoutExtension.Substring(0, lastSpaceIndex); + } + } + int numberToAppend = 2; + string fileNameToUse = Path.Combine(directory, fileNameWithoutExtension + " " + numberToAppend.ToString() + extension); + while (File.Exists(fileNameToUse)) + { + numberToAppend++; + fileNameToUse = Path.Combine(directory, fileNameWithoutExtension + " " + numberToAppend.ToString() + extension); + } + File.Copy(file, fileNameToUse); } } + catch (Exception e) + { + } } private void DiretoryContentsChanged(object sender, EventArgs e) diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index adc2e8c7b..f70c39102 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -198,11 +198,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) - { - throw new NotImplementedException(); - } - public override void AddItem(PrintItemWrapper itemToAdd) { throw new NotImplementedException(); @@ -242,11 +237,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath = null) - { - throw new NotImplementedException(); - } - #endregion Overriden Abstract Methods } } \ No newline at end of file diff --git a/Library/Provider/LibraryProviderSqlite.cs b/Library/Provider/LibraryProviderSqlite.cs index 426c23d1c..fc8207ccc 100644 --- a/Library/Provider/LibraryProviderSqlite.cs +++ b/Library/Provider/LibraryProviderSqlite.cs @@ -252,15 +252,15 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider LoadLibraryItems(); } - public override async void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) + public override void AddItem(PrintItemWrapper itemToAdd) { - if (files != null && files.Count > 0) + if (itemToAdd != null && itemToAdd.FileLocation != null) { // create enough info to show that we have items pending (maybe use names from this file list for them) // refresh the display to show the pending items //LibraryProvider.OnDataReloaded(null); - await Task.Run(() => loadFilesIntoLibraryBackgoundWorker_DoWork(files)); + Task.Run(() => loadFilesIntoLibraryBackgoundWorker_DoWork(new string[] { itemToAdd.FileLocation })); if (baseLibraryCollection != null) { @@ -270,12 +270,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public override void AddItem(PrintItemWrapper itemToAdd) - { - throw new NotImplementedException(); - LibraryProvider.OnDataReloaded(null); - } - public void AddItem(PrintItemWrapper item, int indexToInsert = -1) { if (indexToInsert == -1) @@ -361,11 +355,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider LibraryProvider.OnDataReloaded(null); } - public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List meshGroupsToSave, List providerSavePath) - { - throw new NotImplementedException(); - } - private static void AddStlOrGcode(LibraryProviderSQLite libraryToAddTo, string loadedFileName, string extension) { PrintItem printItem = new PrintItem(); diff --git a/Queue/QueueDataWidget.cs b/Queue/QueueDataWidget.cs index 71580065c..3b92edaf5 100644 --- a/Queue/QueueDataWidget.cs +++ b/Queue/QueueDataWidget.cs @@ -479,7 +479,8 @@ namespace MatterHackers.MatterControl.PrintQueue { foreach (QueueRowItem queueItem in queueDataView.SelectedItems) { - LibraryDataView.CurrentLibraryProvider.AddItem(queueItem.PrintItemWrapper); + // TODO: put up a library chooser and let the user put it where they want + LibraryProviderSQLite.Instance.AddFilesToLibrary(new string[] {queueItem.PrintItemWrapper.FileLocation }); } }