diff --git a/DataStorage/Datastore.cs b/DataStorage/Datastore.cs index b6ab2b0f3..4e2d2ed0a 100644 --- a/DataStorage/Datastore.cs +++ b/DataStorage/Datastore.cs @@ -166,7 +166,7 @@ namespace MatterHackers.MatterControl.DataStorage } } - internal class Datastore + public class Datastore { public bool ConnectionError = false; public ISQLite dbSQLite; diff --git a/PrintLibrary/PrintLibraryWidget.cs b/PrintLibrary/PrintLibraryWidget.cs index 6bbb58587..0eb78bbf8 100644 --- a/PrintLibrary/PrintLibraryWidget.cs +++ b/PrintLibrary/PrintLibraryWidget.cs @@ -256,10 +256,10 @@ namespace MatterHackers.MatterControl.PrintLibrary { libraryDataView.SelectedItems.OnAdd += onLibraryItemsSelected; libraryDataView.SelectedItems.OnRemove += onLibraryItemsSelected; - LibraryProvider.CollectionChanged.RegisterEvent(CollectionChanged, ref unregisterEvents); + LibraryProvider.DataReloaded.RegisterEvent(LibraryProviderDataReleaded, ref unregisterEvents); } - private void CollectionChanged(object sender, EventArgs e) + private void LibraryProviderDataReleaded(object sender, EventArgs e) { List providerLocator = LibraryDataView.CurrentLibraryProvider.GetProviderLocator(); StringBuilder path = new StringBuilder(); diff --git a/PrintLibrary/Provider/LibraryProvider.cs b/PrintLibrary/Provider/LibraryProvider.cs index 57f1a5e9d..57b757607 100644 --- a/PrintLibrary/Provider/LibraryProvider.cs +++ b/PrintLibrary/Provider/LibraryProvider.cs @@ -39,7 +39,6 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public abstract class LibraryProvider { - public static RootedObjectEventHandler CollectionChanged = new RootedObjectEventHandler(); public static RootedObjectEventHandler DataReloaded = new RootedObjectEventHandler(); public static RootedObjectEventHandler ItemAdded = new RootedObjectEventHandler(); public static RootedObjectEventHandler ItemRemoved = new RootedObjectEventHandler(); @@ -109,7 +108,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract LibraryProvider GetProviderForItem(PrintItemCollection collection); - public abstract void RemoveCollection(string collectionName); + public abstract void RemoveCollection(PrintItemCollection collectionToRemove); public abstract void RemoveItem(PrintItemWrapper printItemWrapper); diff --git a/PrintLibrary/Provider/LibraryProviderFileSystem.cs b/PrintLibrary/Provider/LibraryProviderFileSystem.cs index 7866c4794..796f3360a 100644 --- a/PrintLibrary/Provider/LibraryProviderFileSystem.cs +++ b/PrintLibrary/Provider/LibraryProviderFileSystem.cs @@ -163,7 +163,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override PrintItemCollection GetCollectionItem(int collectionIndex) { string directoryName = currentDirectoryDirectories[collectionIndex]; - return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), directoryName); + return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), Path.Combine(rootPath, directoryName)); } public override PrintItemWrapper GetPrintItemWrapper(int itemIndex) @@ -179,9 +179,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return new LibraryProviderFileSystem(Path.Combine(rootPath, collection.Key), collection.Name, this); } - public override void RemoveCollection(string collectionName) + public override void RemoveCollection(PrintItemCollection collectionToRemove) { - string directoryPath = Path.Combine(rootPath, currentDirectory, collectionName); + string directoryPath = collectionToRemove.Key; if (Directory.Exists(directoryPath)) { Directory.Delete(directoryPath); diff --git a/PrintLibrary/Provider/LibraryProviderSelector.cs b/PrintLibrary/Provider/LibraryProviderSelector.cs index 72de3364c..af3f132e0 100644 --- a/PrintLibrary/Provider/LibraryProviderSelector.cs +++ b/PrintLibrary/Provider/LibraryProviderSelector.cs @@ -47,10 +47,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider : base(null) { // put in the sqlite provider - libraryProviders.Add(new LibraryProviderSQLite(this)); + libraryProviders.Add(new LibraryProviderSQLite(null, this)); // and any directory providers (sd card provider, etc...) - //libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this)); + libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this)); //#if __ANDROID__ //libraryProviders.Add(new LibraryProviderFileSystem(ApplicationDataStorage.Instance.PublicDataStoragePath, "Downloads", this.ProviderKey)); @@ -184,7 +184,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void RemoveCollection(string collectionName) + public override void RemoveCollection(PrintItemCollection collectionToRemove) { throw new NotImplementedException(); } diff --git a/PrintLibrary/Provider/LibraryProviderSqlite.cs b/PrintLibrary/Provider/LibraryProviderSqlite.cs index b21e955e9..b62c548f2 100644 --- a/PrintLibrary/Provider/LibraryProviderSqlite.cs +++ b/PrintLibrary/Provider/LibraryProviderSqlite.cs @@ -35,6 +35,7 @@ using MatterHackers.MatterControl.PrintQueue; using MatterHackers.MatterControl.SettingsManagement; using MatterHackers.PolygonMesh; using MatterHackers.PolygonMesh.Processors; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; @@ -46,17 +47,22 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public class LibraryProviderSQLite : LibraryProvider { private static LibraryProviderSQLite instance = null; - private static PrintItemCollection rotLibraryCollection; + private PrintItemCollection baseLibraryCollection; private List childCollections = new List(); - private PrintItemCollection collectionBase = GetRootLibraryCollection(); private string keywordFilter = string.Empty; private List printItems = new List(); - public LibraryProviderSQLite(LibraryProvider parentLibraryProvider) + public LibraryProviderSQLite(PrintItemCollection baseLibraryCollection, LibraryProvider parentLibraryProvider) : base(parentLibraryProvider) { + if (baseLibraryCollection == null) + { + baseLibraryCollection = GetRootLibraryCollection2(this); + } + + this.baseLibraryCollection = baseLibraryCollection; LoadLibraryItems(); } @@ -66,7 +72,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { if (instance == null) { - instance = new LibraryProviderSQLite(null); + instance = new LibraryProviderSQLite(null, null); } return instance; @@ -120,7 +126,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override string ProviderData { - get { throw new NotImplementedException(); } + get + { + return baseLibraryCollection.Id.ToString(); + } } public override string ProviderKey @@ -139,31 +148,28 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return result; } - public static PrintItemCollection GetRootLibraryCollection() + static PrintItemCollection GetRootLibraryCollection2(LibraryProviderSQLite rootLibrary) { // Attempt to initialize the library from the Datastore if null - if (rotLibraryCollection == null) - { - rotLibraryCollection = Datastore.Instance.dbSQLite.Table().Where(v => v.Name == "_library").Take(1).FirstOrDefault(); - } + PrintItemCollection rootLibraryCollection = Datastore.Instance.dbSQLite.Table().Where(v => v.Name == "_library").Take(1).FirstOrDefault(); // If the _library collection is still missing, create and populate it with default content - if (rotLibraryCollection == null) + if (rootLibraryCollection == null) { - rotLibraryCollection = new PrintItemCollection(); - rotLibraryCollection.Name = "_library"; - rotLibraryCollection.Commit(); + rootLibraryCollection = new PrintItemCollection(); + rootLibraryCollection.Name = "_library"; + rootLibraryCollection.Commit(); // Preload library with Oem supplied list of default parts string[] itemsToAdd = SyncCalibrationFilesToDisk(OemSettings.Instance.PreloadedLibraryFiles); if (itemsToAdd.Length > 0) { // Import any files sync'd to disk into the library, then add them to the queue - LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null); rootLibrary.AddFilesToLibrary(itemsToAdd); } } - return rotLibraryCollection; + + return rootLibraryCollection; } static public void SaveToLibraryFolder(PrintItemWrapper printItemWrapper, List meshGroups, bool AbsolutePositioned) @@ -205,7 +211,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return calibrationPrintFileNames.Where(fileName => { // Filter out items that already exist in the library - LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null); + LibraryProviderSQLite rootLibrary = new LibraryProviderSQLite(null, null); return rootLibrary.GetLibraryItems(Path.GetFileNameWithoutExtension(fileName)).Count() <= 0; }).Select(fileName => { @@ -225,8 +231,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override void AddCollectionToLibrary(string collectionName) { PrintItemCollection newCollection = new PrintItemCollection(collectionName, ""); - newCollection.ParentCollectionID = collectionBase.Id; + newCollection.ParentCollectionID = baseLibraryCollection.Id; newCollection.Commit(); + LoadLibraryItems(); + LibraryProvider.OnDataReloaded(null); } public override void AddFilesToLibrary(IList files, List providerSavePath = null, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) @@ -251,6 +259,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override void AddItem(PrintItemWrapper itemToAdd) { throw new NotImplementedException(); + LibraryProvider.OnDataReloaded(null); + } + + void SetProviderLocator(PrintItemWrapper itemToSetProviderLocatorOn) + { + List providerLocator = GetProviderLocator(); + string providerLocatorJson = JsonConvert.SerializeObject(providerLocator); + itemToSetProviderLocatorOn.PrintItem.LibraryProviderLocatorJson = providerLocatorJson; } public void AddItem(PrintItemWrapper item, int indexToInsert = -1) @@ -259,6 +275,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { indexToInsert = printItems.Count; } + SetProviderLocator(item); printItems.Insert(indexToInsert, item); // Check if the collection we are adding to is the the currently visible collection. List currentDisplayedCollection = GetProviderLocator(); @@ -266,7 +283,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { OnItemAdded(new IndexArgs(indexToInsert)); } - item.PrintItem.PrintItemCollectionID = GetRootLibraryCollection().Id; + item.PrintItem.PrintItemCollectionID = baseLibraryCollection.Id; item.PrintItem.Commit(); } @@ -287,7 +304,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override LibraryProvider GetProviderForItem(PrintItemCollection collection) { - throw new NotImplementedException(); + return new LibraryProviderSQLite(collection, this); } public void LoadLibraryItems() @@ -298,17 +315,26 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { foreach (PrintItem part in partFiles) { - printItems.Add(new PrintItemWrapper(part)); + PrintItemWrapper item = new PrintItemWrapper(part); + SetProviderLocator(item); + printItems.Add(item); } } childCollections.Clear(); - childCollections.AddRange(GetChildCollections()); + GetChildCollections(); + IEnumerable collections = GetChildCollections(); + if(collections != null) + { + childCollections.AddRange(collections); + } } - public override void RemoveCollection(string collectionName) + public override void RemoveCollection(PrintItemCollection collectionToRemove) { - throw new NotImplementedException(); + collectionToRemove.Delete(); + LoadLibraryItems(); + LibraryProvider.OnDataReloaded(null); } public override void RemoveItem(PrintItemWrapper printItemWrapper) @@ -333,12 +359,12 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - private static void AddStlOrGcode(LibraryProvider libraryToAddTo, string loadedFileName, string extension) + private static void AddStlOrGcode(LibraryProviderSQLite libraryToAddTo, string loadedFileName, string extension) { PrintItem printItem = new PrintItem(); printItem.Name = Path.GetFileNameWithoutExtension(loadedFileName); printItem.FileLocation = Path.GetFullPath(loadedFileName); - printItem.PrintItemCollectionID = GetRootLibraryCollection().Id; + printItem.PrintItemCollectionID = libraryToAddTo.baseLibraryCollection.Id; printItem.Commit(); if (MeshFileIo.ValidFileExtensions().Contains(extension)) @@ -393,35 +419,24 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider private IEnumerable GetChildCollections() { - if (collectionBase != null) - { - string query; - query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", collectionBase.Id); - IEnumerable result = (IEnumerable)Datastore.Instance.dbSQLite.Query(query); - return result; - } - - return null; + string query = string.Format("SELECT * FROM PrintItemCollection WHERE ParentCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id); + IEnumerable result = (IEnumerable)Datastore.Instance.dbSQLite.Query(query); + return result; } private IEnumerable GetLibraryItems(string keyphrase = null) { - if (collectionBase != null) + string query; + if (keyphrase == null) { - string query; - if (keyphrase == null) - { - query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", collectionBase.Id); - } - else - { - query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", collectionBase.Id, keyphrase); - } - IEnumerable result = (IEnumerable)Datastore.Instance.dbSQLite.Query(query); - return result; + query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} ORDER BY Name ASC;", baseLibraryCollection.Id); } - - return null; + else + { + query = string.Format("SELECT * FROM PrintItem WHERE PrintItemCollectionID = {0} AND Name LIKE '%{1}%' ORDER BY Name ASC;", baseLibraryCollection.Id, keyphrase); + } + IEnumerable result = (IEnumerable)Datastore.Instance.dbSQLite.Query(query); + return result; } private void loadFilesIntoLibraryBackgoundWorker_DoWork(object sender, DoWorkEventArgs e) diff --git a/Submodules/MatterSlice b/Submodules/MatterSlice index e816e7eb8..e898dff49 160000 --- a/Submodules/MatterSlice +++ b/Submodules/MatterSlice @@ -1 +1 @@ -Subproject commit e816e7eb8d1d5ad6b309e5f8451c4876773b3bc1 +Subproject commit e898dff49a5d0edf67741ca5848c714ead3f0fa5 diff --git a/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs index 8fff8cf9b..f5375e9ad 100644 --- a/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/LibraryProviderTests.cs @@ -46,10 +46,9 @@ namespace MatterControl.Tests [TestFixture] public class LibraryProviderTests { - static string pathToMesh = Path.Combine("..", "..", "..", "TestData", "TestMeshes", "LibraryProviderData"); - static string meshFileName = Path.Combine(pathToMesh, "Box20x20x10.stl"); + string pathToMesh = Path.Combine("..", "..", "..", "TestData", "TestMeshes", "LibraryProviderData"); + string meshFileName; - bool collectionChanged = false; bool dataReloaded = false; bool itemAdded = false; bool itemRemoved = false; @@ -63,22 +62,22 @@ namespace MatterControl.Tests #endif } - [TestFixtureSetUp] - void SetupBeforeTest() + [SetUp] + public void SetupBeforeTest() { - collectionChanged = false; + meshFileName = Path.Combine(pathToMesh, "Box20x20x10.stl"); + dataReloaded = false; itemAdded = false; itemRemoved = false; - LibraryProvider.CollectionChanged.RegisterEvent((sender, e) => { collectionChanged = true; }, ref unregisterEvents); LibraryProvider.DataReloaded.RegisterEvent((sender, e) => { dataReloaded = true; }, ref unregisterEvents); LibraryProvider.ItemAdded.RegisterEvent((sender, e) => { itemAdded = true; }, ref unregisterEvents); LibraryProvider.ItemRemoved.RegisterEvent((sender, e) => { itemRemoved = true; }, ref unregisterEvents); } [TearDown] - void TeardownAfterTest() + public void TeardownAfterTest() { unregisterEvents(this, null); } @@ -97,27 +96,48 @@ namespace MatterControl.Tests string collectionName = "Collection1"; string createdDirectory = Path.Combine(pathToMesh, collectionName); Assert.IsTrue(!Directory.Exists(createdDirectory)); - Assert.IsTrue(collectionChanged == false); + Assert.IsTrue(dataReloaded == false); testProvider.AddCollectionToLibrary(collectionName); - Assert.IsTrue(collectionChanged == true); + Assert.IsTrue(testProvider.CollectionCount == 1); + Assert.IsTrue(dataReloaded == true); Assert.IsTrue(Directory.Exists(createdDirectory)); - collectionChanged = false; + dataReloaded = false; // make sure removing it gets rid of it - Assert.IsTrue(collectionChanged == false); - testProvider.RemoveCollection("Collection1"); - Assert.IsTrue(collectionChanged == true); + Assert.IsTrue(dataReloaded == false); + testProvider.RemoveCollection(testProvider.GetCollectionItem(0)); + Assert.IsTrue(dataReloaded == true); Assert.IsTrue(!Directory.Exists(createdDirectory)); + + // test GetProviderForItem } [Test, Category("LibraryProviderSqlite")] public void LibraryProviderSqlite_NavigationWorking() { - LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null); + LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null); Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); - Assert.IsTrue(testProvider.ItemCount == 0, "Start with a new database for these tests."); - PrintItem printItem = new PrintItem("Test_RootItem", meshFileName); - testProvider.AddItem(new PrintItemWrapper(printItem)); - } + Assert.IsTrue(testProvider.ItemCount == 1, "Start with a new database for these tests."); + PrintItemWrapper itemAtRoot = testProvider.GetPrintItemWrapper(0); + List providerLocator = itemAtRoot.PrintItem.GetLibraryProviderLocator(); + Assert.IsTrue(providerLocator.Count == 1); + + // create a collection and make sure it is on disk + string collectionName = "Collection1"; + //Assert.IsTrue(); // assert that the record does not exist in the DB + Assert.IsTrue(dataReloaded == false); + testProvider.AddCollectionToLibrary(collectionName); + Assert.IsTrue(testProvider.CollectionCount == 1); + Assert.IsTrue(dataReloaded == true); + // Assert.IsTrue(); // assert that the record does exist in the DB + + dataReloaded = false; + // make sure removing it gets rid of it + Assert.IsTrue(dataReloaded == false); + testProvider.RemoveCollection(testProvider.GetCollectionItem(0)); + Assert.IsTrue(dataReloaded == true); + Assert.IsTrue(testProvider.CollectionCount == 0); + //Assert.IsTrue(); // assert that the record does not exist in the DB + } } }