2015-04-10 18:48:57 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2014, Lars Brubaker
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
using System;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using System.Collections.Generic;
|
2014-06-19 15:55:20 -07:00
|
|
|
|
using System.IO;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
using System.Threading;
|
2017-08-20 02:34:39 -07:00
|
|
|
|
using MatterHackers.Agg.Platform;
|
Move to new library model and view
- Add new listview control for library content
- Migrate library providers to containers
- Cloud, Sqlite, Directories, Queue, History
- Migrate SideBar components to containers
- Primatives, Text, Braille, ImageConverter
- Create new library container types
- Zip files, Calibration parts, Printer SDCards
- Reduce leftnav to Library, Settings, Controls, Options
- Add DragDrop support for image content
2017-05-19 22:33:55 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DataStorage
|
|
|
|
|
|
{
|
2015-06-30 11:34:46 -07:00
|
|
|
|
public class Datastore
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2015-09-03 12:21:20 -07:00
|
|
|
|
bool wasExited = false;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
public bool ConnectionError = false;
|
|
|
|
|
|
public ISQLite dbSQLite;
|
2016-05-10 12:57:56 -07:00
|
|
|
|
private string datastoreLocation = ApplicationDataStorage.Instance.DatastorePath;
|
2015-04-10 09:54:05 -07:00
|
|
|
|
private static Datastore globalInstance;
|
|
|
|
|
|
private ApplicationSession activeSession;
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
|
|
|
|
|
private List<Type> dataStoreTables = new List<Type>
|
|
|
|
|
|
{
|
|
|
|
|
|
typeof(PrintItemCollection),
|
|
|
|
|
|
typeof(PrinterSetting),
|
|
|
|
|
|
typeof(CustomCommands),
|
|
|
|
|
|
typeof(SystemSetting),
|
|
|
|
|
|
typeof(UserSetting),
|
|
|
|
|
|
typeof(ApplicationSession),
|
|
|
|
|
|
typeof(PrintItem),
|
|
|
|
|
|
typeof(PrintTask),
|
|
|
|
|
|
typeof(Printer),
|
|
|
|
|
|
typeof(SliceSetting),
|
|
|
|
|
|
typeof(SliceSettingsCollection)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
public Datastore()
|
|
|
|
|
|
{
|
2015-04-10 18:48:57 -07:00
|
|
|
|
if (!File.Exists(datastoreLocation))
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2015-04-10 18:48:57 -07:00
|
|
|
|
ApplicationDataStorage.Instance.FirstRun = true;
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2015-04-10 09:54:05 -07:00
|
|
|
|
public static Datastore Instance
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2015-04-10 09:54:05 -07:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (globalInstance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
globalInstance = new Datastore();
|
|
|
|
|
|
}
|
|
|
|
|
|
return globalInstance;
|
|
|
|
|
|
}
|
2015-12-22 11:34:04 -08:00
|
|
|
|
|
2018-10-19 15:53:16 -07:00
|
|
|
|
// Special case to allow tests to set custom application paths
|
2015-12-21 15:58:53 -08:00
|
|
|
|
internal set
|
|
|
|
|
|
{
|
|
|
|
|
|
globalInstance = value;
|
|
|
|
|
|
}
|
2015-04-10 09:54:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
public void Exit()
|
|
|
|
|
|
{
|
2015-09-08 18:50:35 -07:00
|
|
|
|
if (wasExited)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
2015-09-03 12:21:20 -07:00
|
|
|
|
wasExited = true;
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
2015-08-18 13:01:29 -07:00
|
|
|
|
if (this.activeSession != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.activeSession.SessionEnd = DateTime.Now;
|
|
|
|
|
|
this.activeSession.Commit();
|
|
|
|
|
|
}
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
2015-04-10 09:54:05 -07:00
|
|
|
|
// lets wait a bit to make sure the commit has resolved.
|
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
dbSQLite.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2016-01-04 21:18:57 -08:00
|
|
|
|
// we failed to close so lets wait a bit and try again
|
2015-04-10 09:54:05 -07:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
dbSQLite.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2015-04-10 09:54:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
//Run initial checks and operations on sqlite datastore
|
2018-10-19 15:53:16 -07:00
|
|
|
|
public void Initialize(ISQLite dbSQLite)
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
2018-10-19 15:53:16 -07:00
|
|
|
|
this.dbSQLite = dbSQLite;
|
2018-01-17 21:49:44 -08:00
|
|
|
|
ValidateSchema();
|
Move to new library model and view
- Add new listview control for library content
- Migrate library providers to containers
- Cloud, Sqlite, Directories, Queue, History
- Migrate SideBar components to containers
- Primatives, Text, Braille, ImageConverter
- Create new library container types
- Zip files, Calibration parts, Printer SDCards
- Reduce leftnav to Library, Settings, Controls, Options
- Add DragDrop support for image content
2017-05-19 22:33:55 -07:00
|
|
|
|
|
2018-01-17 22:03:41 -08:00
|
|
|
|
// Construct the root library collection if missing
|
Move to new library model and view
- Add new listview control for library content
- Migrate library providers to containers
- Cloud, Sqlite, Directories, Queue, History
- Migrate SideBar components to containers
- Primatives, Text, Braille, ImageConverter
- Create new library container types
- Zip files, Calibration parts, Printer SDCards
- Reduce leftnav to Library, Settings, Controls, Options
- Add DragDrop support for image content
2017-05-19 22:33:55 -07:00
|
|
|
|
var rootLibraryCollection = Datastore.Instance.dbSQLite.Table<PrintItemCollection>().Where(v => v.Name == "_library").Take(1).FirstOrDefault();
|
|
|
|
|
|
if (rootLibraryCollection == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
rootLibraryCollection = new PrintItemCollection();
|
|
|
|
|
|
rootLibraryCollection.Name = "_library";
|
|
|
|
|
|
rootLibraryCollection.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
StartSession();
|
|
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2015-04-10 09:54:05 -07:00
|
|
|
|
public int RecordCount(string tableName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string query = string.Format("SELECT COUNT(*) FROM {0};", tableName);
|
|
|
|
|
|
string result = Datastore.Instance.dbSQLite.ExecuteScalar<string>(query);
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
2015-04-10 09:54:05 -07:00
|
|
|
|
return Convert.ToInt32(result);
|
|
|
|
|
|
}
|
2016-02-23 13:53:28 -08:00
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
//Begins new application session record
|
2016-02-23 13:53:28 -08:00
|
|
|
|
private void StartSession()
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
|
|
|
|
|
activeSession = new ApplicationSession();
|
|
|
|
|
|
dbSQLite.Insert(activeSession);
|
|
|
|
|
|
}
|
2014-01-29 19:09:30 -08:00
|
|
|
|
|
2016-04-18 11:31:31 -07:00
|
|
|
|
private void GenerateSampleData()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int index = 1; index <= 5; index++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Printer printer = new Printer();
|
|
|
|
|
|
printer.ComPort = string.Format("COM{0}", index);
|
|
|
|
|
|
printer.BaudRate = "250000";
|
|
|
|
|
|
printer.Name = string.Format("Printer {0}", index);
|
|
|
|
|
|
Datastore.Instance.dbSQLite.Insert(printer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-08 15:20:10 -07:00
|
|
|
|
// Checks if the datastore contains the appropriate tables - adds them if necessary
|
2016-02-23 13:53:28 -08:00
|
|
|
|
private void ValidateSchema()
|
2015-04-08 15:20:10 -07:00
|
|
|
|
{
|
|
|
|
|
|
foreach (Type table in dataStoreTables)
|
|
|
|
|
|
{
|
|
|
|
|
|
dbSQLite.CreateTable(table);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-09-03 12:21:20 -07:00
|
|
|
|
|
|
|
|
|
|
public bool WasExited()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Instance.wasExited;
|
|
|
|
|
|
}
|
2015-04-08 15:20:10 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|