From 3fe40cf70fa39f4640515e9cf40bd8a4739635f6 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 17 Jan 2018 21:47:05 -0800 Subject: [PATCH] Extract embedded type to new file --- DataStorage/ApplicationDataStorage.cs | 218 ++++++++++++++++++++++++++ DataStorage/Datastore.cs | 184 ---------------------- MatterControl.csproj | 1 + 3 files changed, 219 insertions(+), 184 deletions(-) create mode 100644 DataStorage/ApplicationDataStorage.cs diff --git a/DataStorage/ApplicationDataStorage.cs b/DataStorage/ApplicationDataStorage.cs new file mode 100644 index 000000000..be663087a --- /dev/null +++ b/DataStorage/ApplicationDataStorage.cs @@ -0,0 +1,218 @@ +/* +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. +*/ + +using System; +using System.IO; + +namespace MatterHackers.MatterControl.DataStorage +{ + public class ApplicationDataStorage + { + public bool FirstRun = false; + + //Describes the location for storing all local application data + private static ApplicationDataStorage globalInstance; + private static readonly string applicationDataFolderName = "MatterControl"; + private readonly string datastoreName = "MatterControl.db"; + private string applicationPath; + private static string applicationUserDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), applicationDataFolderName); + + public ApplicationDataStorage() + //Constructor - validates that local storage folder exists, creates if necessary + { + DirectoryInfo dir = new DirectoryInfo(ApplicationUserDataPath); + if (!dir.Exists) + { + dir.Create(); + } + + Directory.CreateDirectory(this.LibraryAssetsPath); + } + + /// + /// Creates a global instance of ApplicationDataStorage + /// + public static ApplicationDataStorage Instance + { + get + { + if (globalInstance == null) + { + globalInstance = new ApplicationDataStorage(); + } + return globalInstance; + } + } + + public string GetTempFileName(string fileExtension = null) + { + string tempFileName = string.IsNullOrEmpty(fileExtension) ? + Path.GetRandomFileName() : + Path.ChangeExtension(Path.GetRandomFileName(), "." + fileExtension.TrimStart('.')); + + return Path.Combine(this.ApplicationTempDataPath, tempFileName); + } + + public string GetNewLibraryFilePath(string extension) + { + string filePath; + + // Loop until we've found a non-conflicting library path for the given extension + do + { + filePath = Path.Combine( + ApplicationDataStorage.Instance.ApplicationLibraryDataPath, + Path.ChangeExtension(Path.GetRandomFileName(), extension)); + + } while (File.Exists(filePath)); + + return filePath; + } + + public string ApplicationLibraryDataPath + { + get + { + string libraryPath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "Library"); + + //Create library path if it doesn't exist + DirectoryInfo dir = new DirectoryInfo(libraryPath); + if (!dir.Exists) + { + dir.Create(); + } + return libraryPath; + } + } + + public string LibraryAssetsPath => Path.Combine(this.ApplicationLibraryDataPath, "Assets"); + + /// + /// Overrides the AppData location. + /// + /// The new AppData path. + internal void OverrideAppDataLocation(string path) + { + Console.WriteLine(" Overriding ApplicationUserDataPath: " + path); + + // Ensure the target directory exists + Directory.CreateDirectory(path); + + applicationUserDataPath = path; + + // Initialize a fresh datastore instance after overriding the AppData path + Datastore.Instance = new Datastore(); + Datastore.Instance.Initialize(); + } + + public string ApplicationPath + { + get + { + if (this.applicationPath == null) + { + applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + } + return applicationPath; + } + } + + /// + /// Returns the application temp data folder + /// + /// + public string ApplicationTempDataPath + { + get + { + return Path.Combine(applicationUserDataPath, "data", "temp"); + } + } + + public string PlatingDirectory + { + get + { + string platingDirectory = Path.Combine(ApplicationTempDataPath, "Plating"); + Directory.CreateDirectory(platingDirectory); + + return platingDirectory; + } + } + + public string DownloadsDirectory { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"); + + public string CustomLibraryFoldersPath { get; } = Path.Combine(applicationUserDataPath, "LibraryFolders.conf"); + + /// + /// Returns the application user data folder + /// + /// + public static string ApplicationUserDataPath + { + get + { + return applicationUserDataPath; + } + } + + /// + /// Returns the path to the sqlite database + /// + /// + public string DatastorePath + { + get { return Path.Combine(ApplicationUserDataPath, datastoreName); } + } + + /// + /// Returns the gcode output folder + /// + /// + public string GCodeOutputPath + { + get + { + string gcodeOutputPath = Path.Combine(ApplicationUserDataPath, "data", "gcode"); + if (!Directory.Exists(gcodeOutputPath)) + { + Directory.CreateDirectory(gcodeOutputPath); + } + return gcodeOutputPath; + } + } + +#if __ANDROID__ + /// + /// Returns the public storage folder (ex. download folder on Android) + /// + public string PublicDataStoragePath { get; } = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath; +#endif + } +} \ No newline at end of file diff --git a/DataStorage/Datastore.cs b/DataStorage/Datastore.cs index 95d8fd2d7..1f34a50fd 100644 --- a/DataStorage/Datastore.cs +++ b/DataStorage/Datastore.cs @@ -36,190 +36,6 @@ using MatterHackers.Agg.UI; namespace MatterHackers.MatterControl.DataStorage { - public class ApplicationDataStorage - { - public bool FirstRun = false; - - //Describes the location for storing all local application data - private static ApplicationDataStorage globalInstance; - private static readonly string applicationDataFolderName = "MatterControl"; - private readonly string datastoreName = "MatterControl.db"; - private string applicationPath; - private static string applicationUserDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), applicationDataFolderName); - - public ApplicationDataStorage() - //Constructor - validates that local storage folder exists, creates if necessary - { - DirectoryInfo dir = new DirectoryInfo(ApplicationUserDataPath); - if (!dir.Exists) - { - dir.Create(); - } - - Directory.CreateDirectory(this.LibraryAssetsPath); - } - - /// - /// Creates a global instance of ApplicationDataStorage - /// - public static ApplicationDataStorage Instance - { - get - { - if (globalInstance == null) - { - globalInstance = new ApplicationDataStorage(); - } - return globalInstance; - } - } - - public string GetTempFileName(string fileExtension = null) - { - string tempFileName = string.IsNullOrEmpty(fileExtension) ? - Path.GetRandomFileName() : - Path.ChangeExtension(Path.GetRandomFileName(), "." + fileExtension.TrimStart('.')); - - return Path.Combine(this.ApplicationTempDataPath, tempFileName); - } - - public string GetNewLibraryFilePath(string extension) - { - string filePath; - - // Loop until we've found a non-conflicting library path for the given extension - do - { - filePath = Path.Combine( - ApplicationDataStorage.Instance.ApplicationLibraryDataPath, - Path.ChangeExtension(Path.GetRandomFileName(), extension)); - - } while (File.Exists(filePath)); - - return filePath; - } - - public string ApplicationLibraryDataPath - { - get - { - string libraryPath = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "Library"); - - //Create library path if it doesn't exist - DirectoryInfo dir = new DirectoryInfo(libraryPath); - if (!dir.Exists) - { - dir.Create(); - } - return libraryPath; - } - } - - public string LibraryAssetsPath => Path.Combine(this.ApplicationLibraryDataPath, "Assets"); - - /// - /// Overrides the AppData location. - /// - /// The new AppData path. - internal void OverrideAppDataLocation(string path) - { - Console.WriteLine(" Overriding ApplicationUserDataPath: " + path); - - // Ensure the target directory exists - Directory.CreateDirectory(path); - - applicationUserDataPath = path; - - // Initialize a fresh datastore instance after overriding the AppData path - Datastore.Instance = new Datastore(); - Datastore.Instance.Initialize(); - } - - public string ApplicationPath - { - get - { - if (this.applicationPath == null) - { - applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); - } - return applicationPath; - } - } - - /// - /// Returns the application temp data folder - /// - /// - public string ApplicationTempDataPath - { - get - { - return Path.Combine(applicationUserDataPath, "data", "temp"); - } - } - - public string PlatingDirectory - { - get - { - string platingDirectory = Path.Combine(ApplicationTempDataPath, "Plating"); - Directory.CreateDirectory(platingDirectory); - - return platingDirectory; - } - } - - public string DownloadsDirectory { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"); - - public string CustomLibraryFoldersPath { get; } = Path.Combine(applicationUserDataPath, "LibraryFolders.conf"); - - /// - /// Returns the application user data folder - /// - /// - public static string ApplicationUserDataPath - { - get - { - return applicationUserDataPath; - } - } - - /// - /// Returns the path to the sqlite database - /// - /// - public string DatastorePath - { - get { return Path.Combine(ApplicationUserDataPath, datastoreName); } - } - - /// - /// Returns the gcode output folder - /// - /// - public string GCodeOutputPath - { - get - { - string gcodeOutputPath = Path.Combine(ApplicationUserDataPath, "data", "gcode"); - if (!Directory.Exists(gcodeOutputPath)) - { - Directory.CreateDirectory(gcodeOutputPath); - } - return gcodeOutputPath; - } - } - -#if __ANDROID__ - /// - /// Returns the public storage folder (ex. download folder on Android) - /// - public string PublicDataStoragePath { get; } = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath; -#endif - } - public class Datastore { bool wasExited = false; diff --git a/MatterControl.csproj b/MatterControl.csproj index 36d17b23a..1afbd1f14 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -85,6 +85,7 @@ +