From a7579d16c9c332312e4611c82f41a8289e141d9b Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Mon, 22 Jun 2015 11:50:19 -0700 Subject: [PATCH] Change the function signature of AddFilesToLibrary Made File system provider work with AddFilesToLibrary Made Drag and Drop work by trying to add to the correct collection. --- PrintLibrary/PrintLibraryWidget.cs | 5 ++- PrintLibrary/Provider/LibraryProvider.cs | 2 +- .../Provider/LibraryProviderFileSystem.cs | 39 +++++++++++++++- .../Provider/LibraryProviderSelector.cs | 45 +++++++++++++------ .../Provider/LibraryProviderSqlite.cs | 2 +- 5 files changed, 74 insertions(+), 19 deletions(-) diff --git a/PrintLibrary/PrintLibraryWidget.cs b/PrintLibrary/PrintLibraryWidget.cs index dd040c10c..54556b3f0 100644 --- a/PrintLibrary/PrintLibraryWidget.cs +++ b/PrintLibrary/PrintLibraryWidget.cs @@ -234,6 +234,7 @@ namespace MatterHackers.MatterControl.PrintLibrary } breadCrumbDisplay.Text = path.ToString(); + libraryDataView.ClearSelectedItems(); } public override void OnClosed(EventArgs e) @@ -395,7 +396,7 @@ namespace MatterHackers.MatterControl.PrintLibrary public override void OnDragDrop(FileDropEventArgs fileDropEventArgs) { - LibraryProvider.Instance.AddFilesToLibrary(fileDropEventArgs.DroppedFiles); + LibraryProvider.Instance.AddFilesToLibrary(fileDropEventArgs.DroppedFiles, LibraryProvider.Instance.GetProviderLocator()); base.OnDragDrop(fileDropEventArgs); } @@ -415,7 +416,7 @@ namespace MatterHackers.MatterControl.PrintLibrary { if (openParams.FileNames != null) { - LibraryProvider.Instance.AddFilesToLibrary(openParams.FileNames); + LibraryProvider.Instance.AddFilesToLibrary(openParams.FileNames, null); } } } diff --git a/PrintLibrary/Provider/LibraryProvider.cs b/PrintLibrary/Provider/LibraryProvider.cs index ff69b6842..9b6cd8c25 100644 --- a/PrintLibrary/Provider/LibraryProvider.cs +++ b/PrintLibrary/Provider/LibraryProvider.cs @@ -86,7 +86,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract void AddCollectionToLibrary(string collectionName); - public abstract void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null); + public abstract void AddFilesToLibrary(IList files, List providerSavePath, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null); // A key,value list that threads into the current collection looks like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". public abstract List GetProviderLocator(); diff --git a/PrintLibrary/Provider/LibraryProviderFileSystem.cs b/PrintLibrary/Provider/LibraryProviderFileSystem.cs index a466d71de..48ba126ee 100644 --- a/PrintLibrary/Provider/LibraryProviderFileSystem.cs +++ b/PrintLibrary/Provider/LibraryProviderFileSystem.cs @@ -123,7 +123,40 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override void AddFilesToLibrary(IList files, List providerLocator, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + { + if (providerLocator == null || providerLocator.Count <= 1) + { + string destPath = rootPath; + + CopyAllFiles(files, destPath); + } + else // we have a path that we need to save to + { + string destPath = GetPathFromLocator(providerLocator); + + CopyAllFiles(files, destPath); + } + + GetFilesInCurrentDirectory(); + LibraryProvider.OnDataReloaded(null); + } + + private static void CopyAllFiles(IList files, string destPath) + { + // make sure the directory exists + Directory.CreateDirectory(destPath); + + // save it to the root directory + foreach (string file in files) + { + string outputFileName = Path.Combine(destPath, Path.GetFileName(file)); + // and copy the file + File.Copy(file, outputFileName); + } + } + + private string GetPathFromLocator(List providerLocator) { throw new NotImplementedException(); } @@ -174,7 +207,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override void RemoveItem(PrintItemWrapper printItemWrapper) { - throw new NotImplementedException(); + File.Delete(printItemWrapper.PrintItem.FileLocation); + GetFilesInCurrentDirectory(); + LibraryProvider.OnDataReloaded(null); } public override void SetCollectionBase(PrintItemCollection collectionBase) diff --git a/PrintLibrary/Provider/LibraryProviderSelector.cs b/PrintLibrary/Provider/LibraryProviderSelector.cs index 8610c809b..ebd947dc7 100644 --- a/PrintLibrary/Provider/LibraryProviderSelector.cs +++ b/PrintLibrary/Provider/LibraryProviderSelector.cs @@ -171,16 +171,33 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override void AddFilesToLibrary(IList files, List providerSavePath, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) { - if (selectedLibraryProvider == -1) + List subProviderSavePath; + int libraryProviderToUseIndex = GetProviderIndex(providerSavePath, out subProviderSavePath); + + libraryProviders[libraryProviderToUseIndex].AddFilesToLibrary(files, subProviderSavePath, reportProgress, callback); + } + + private int GetProviderIndex(List providerSavePath, out List subProviderSavePath) + { + subProviderSavePath = null; + + if (providerSavePath != null + && providerSavePath.Count > 1) // key 0 is this provider so we want to look at the next provider { - throw new NotImplementedException(); - } - else - { - libraryProviders[selectedLibraryProvider].AddFilesToLibrary(files, reportProgress, callback); + for (int i = 0; i < libraryProviders.Count; i++) + { + if (libraryProviders[i].ProviderKey == providerSavePath[1].Key) + { + subProviderSavePath = new List(providerSavePath); + subProviderSavePath.RemoveAt(0); + return i; + } + } } + + return 0; } // A key,value list that threads into the current collection loos like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN". @@ -268,14 +285,16 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override void RemoveItem(PrintItemWrapper printItemWrapper) { - if (selectedLibraryProvider == -1) + List providerPath = null; + if (printItemWrapper.PrintItem.LibraryProviderLocatorJson != null) { - throw new NotImplementedException(); - } - else - { - libraryProviders[selectedLibraryProvider].RemoveItem(printItemWrapper); + providerPath = JsonConvert.DeserializeObject>(printItemWrapper.PrintItem.LibraryProviderLocatorJson); } + + List subProviderSavePath; + int libraryProviderToUseIndex = GetProviderIndex(providerPath, out subProviderSavePath); + + libraryProviders[libraryProviderToUseIndex].RemoveItem(printItemWrapper); } public override void SetCollectionBase(PrintItemCollection collectionBase) diff --git a/PrintLibrary/Provider/LibraryProviderSqlite.cs b/PrintLibrary/Provider/LibraryProviderSqlite.cs index 7dab2a02a..104ea30a7 100644 --- a/PrintLibrary/Provider/LibraryProviderSqlite.cs +++ b/PrintLibrary/Provider/LibraryProviderSqlite.cs @@ -132,7 +132,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override void AddFilesToLibrary(IList files, List providerSavePath, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) { LibrarySQLiteData.Instance.LoadFilesIntoLibrary(files, reportProgress, callback); }