2015-06-11 12:06:40 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2015, 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 MatterHackers.Agg;
|
2016-01-12 16:44:05 -08:00
|
|
|
|
using MatterHackers.Agg.PlatformAbstract;
|
2015-06-25 12:19:19 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2015-06-12 17:21:51 -07:00
|
|
|
|
using MatterHackers.MatterControl.DataStorage;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
using MatterHackers.MatterControl.PrintQueue;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2015-07-01 12:11:38 -07:00
|
|
|
|
using System.Diagnostics;
|
2015-06-11 16:25:12 -07:00
|
|
|
|
using System.IO;
|
2015-07-01 12:11:38 -07:00
|
|
|
|
using System.Threading;
|
2015-07-13 13:07:26 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.PrintLibrary.Provider
|
|
|
|
|
|
{
|
2015-08-05 15:55:55 -07:00
|
|
|
|
public class LibraryProviderFileSystemCreator : ILibraryCreator
|
|
|
|
|
|
{
|
2016-01-11 14:06:47 -08:00
|
|
|
|
private string rootPath;
|
|
|
|
|
|
|
|
|
|
|
|
private bool useIncrementedNameDuringTypeChange;
|
2015-08-05 15:55:55 -07:00
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
public LibraryProviderFileSystemCreator(string rootPath, string description, bool useIncrementedNameDuringTypeChange = false)
|
2015-08-05 15:55:55 -07:00
|
|
|
|
{
|
|
|
|
|
|
this.rootPath = rootPath;
|
|
|
|
|
|
this.Description = description;
|
2016-01-11 14:06:47 -08:00
|
|
|
|
this.useIncrementedNameDuringTypeChange = useIncrementedNameDuringTypeChange;
|
2015-08-05 15:55:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public string ProviderKey => "FileSystem_" + rootPath + "_Key";
|
2015-08-05 15:55:55 -07:00
|
|
|
|
|
2015-08-25 11:56:21 -07:00
|
|
|
|
public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action<LibraryProvider> setCurrentLibraryProvider)
|
2015-08-05 15:55:55 -07:00
|
|
|
|
{
|
2016-01-11 14:06:47 -08:00
|
|
|
|
return new LibraryProviderFileSystem(
|
|
|
|
|
|
rootPath,
|
|
|
|
|
|
Description,
|
|
|
|
|
|
parentLibraryProvider,
|
|
|
|
|
|
setCurrentLibraryProvider,
|
|
|
|
|
|
this.useIncrementedNameDuringTypeChange);
|
2015-08-05 15:55:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-11 12:06:40 -07:00
|
|
|
|
public class LibraryProviderFileSystem : LibraryProvider
|
|
|
|
|
|
{
|
2015-06-11 16:25:12 -07:00
|
|
|
|
private string currentDirectory = ".";
|
2015-06-12 09:48:20 -07:00
|
|
|
|
private List<string> currentDirectoryDirectories = new List<string>();
|
2015-06-15 18:31:43 -07:00
|
|
|
|
private List<string> currentDirectoryFiles = new List<string>();
|
2016-01-12 16:44:05 -08:00
|
|
|
|
private FileSystemWatcher directoryWatcher;
|
2015-06-11 16:25:12 -07:00
|
|
|
|
private string keywordFilter = string.Empty;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
private string rootPath;
|
2016-01-11 14:06:47 -08:00
|
|
|
|
private bool useIncrementedNameDuringTypeChange;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
public LibraryProviderFileSystem(
|
|
|
|
|
|
string rootPath, string name,
|
|
|
|
|
|
LibraryProvider parentLibraryProvider,
|
|
|
|
|
|
Action<LibraryProvider> setCurrentLibraryProvider,
|
|
|
|
|
|
bool useIncrementedNameDuringTypeChange = false)
|
2015-09-02 11:05:50 -07:00
|
|
|
|
: base(parentLibraryProvider, setCurrentLibraryProvider)
|
2015-06-11 12:06:40 -07:00
|
|
|
|
{
|
2015-08-13 17:48:34 -07:00
|
|
|
|
this.Name = name;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
this.rootPath = rootPath;
|
2015-06-11 16:25:12 -07:00
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
// Indicates if the new AMF file should use the original file name incremented until no name collision occurs
|
|
|
|
|
|
this.useIncrementedNameDuringTypeChange = useIncrementedNameDuringTypeChange;
|
|
|
|
|
|
|
2016-01-12 16:44:05 -08:00
|
|
|
|
if (OsInformation.OperatingSystem == OSType.Windows)
|
|
|
|
|
|
{
|
|
|
|
|
|
directoryWatcher = new FileSystemWatcher();
|
|
|
|
|
|
directoryWatcher.Path = rootPath;
|
|
|
|
|
|
|
|
|
|
|
|
directoryWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
|
|
|
|
|
|
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
|
|
|
|
|
|
directoryWatcher.Changed += DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Created += DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Deleted += DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Renamed += DiretoryContentsChanged;
|
|
|
|
|
|
|
|
|
|
|
|
// Begin watching.
|
|
|
|
|
|
directoryWatcher.EnableRaisingEvents = true;
|
|
|
|
|
|
}
|
2015-06-29 18:03:56 -07:00
|
|
|
|
|
|
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-15 18:31:43 -07:00
|
|
|
|
public override int CollectionCount
|
2015-06-15 12:12:18 -07:00
|
|
|
|
{
|
2015-06-15 18:31:43 -07:00
|
|
|
|
get
|
2015-06-15 12:12:18 -07:00
|
|
|
|
{
|
2015-06-15 18:31:43 -07:00
|
|
|
|
return currentDirectoryDirectories.Count;
|
2015-06-15 12:12:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-27 19:03:54 -07:00
|
|
|
|
public override void RenameItem(int itemIndexToRename, string newName)
|
|
|
|
|
|
{
|
2015-07-28 14:21:17 -07:00
|
|
|
|
string sourceFile = Path.Combine(rootPath, currentDirectoryFiles[itemIndexToRename]);
|
|
|
|
|
|
if (File.Exists(sourceFile))
|
|
|
|
|
|
{
|
2015-09-09 11:05:17 -07:00
|
|
|
|
string extension = Path.GetExtension(sourceFile);
|
2015-07-28 14:21:17 -07:00
|
|
|
|
string destFile = Path.Combine(Path.GetDirectoryName(sourceFile), newName);
|
2015-09-09 11:05:17 -07:00
|
|
|
|
destFile = Path.ChangeExtension(destFile, extension);
|
2015-07-28 14:21:17 -07:00
|
|
|
|
File.Move(sourceFile, destFile);
|
|
|
|
|
|
Stopwatch time = Stopwatch.StartNew();
|
|
|
|
|
|
// Wait for up to some amount of time for the directory to be gone.
|
|
|
|
|
|
while (File.Exists(destFile)
|
|
|
|
|
|
&& time.ElapsedMilliseconds < 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(1); // make sure we are not eating all the cpu time.
|
|
|
|
|
|
}
|
|
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
|
|
|
|
|
}
|
2015-07-27 19:03:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
public override void ShareItem(int itemIndexToShare)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2015-09-17 18:03:02 -07:00
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
public override bool CanShare { get { return false; } }
|
2015-09-17 18:03:02 -07:00
|
|
|
|
|
2015-06-11 16:25:12 -07:00
|
|
|
|
public override int ItemCount
|
2015-06-11 12:06:40 -07:00
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2015-06-11 16:25:12 -07:00
|
|
|
|
return currentDirectoryFiles.Count;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string KeywordFilter
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2015-06-11 16:25:12 -07:00
|
|
|
|
return keywordFilter;
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
2015-06-11 16:25:12 -07:00
|
|
|
|
if (keywordFilter != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
keywordFilter = value;
|
2015-08-25 13:04:14 -07:00
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory(keywordFilter.Trim() != "");
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-03 17:31:53 -07:00
|
|
|
|
public void ChangeName(string newName)
|
|
|
|
|
|
{
|
2015-08-13 17:48:34 -07:00
|
|
|
|
this.Name = newName;
|
2015-06-29 18:03:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-17 15:43:37 -07:00
|
|
|
|
public override string ProviderKey
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2015-08-05 15:55:55 -07:00
|
|
|
|
return "FileSystem_" + rootPath + "_Key";
|
2015-06-17 15:43:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-11 16:25:12 -07:00
|
|
|
|
public override void AddCollectionToLibrary(string collectionName)
|
|
|
|
|
|
{
|
2015-06-24 16:59:10 -07:00
|
|
|
|
string directoryPath = Path.Combine(rootPath, currentDirectory, collectionName);
|
|
|
|
|
|
if (!Directory.Exists(directoryPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(directoryPath);
|
2015-06-25 12:19:19 -07:00
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
2015-06-24 16:59:10 -07:00
|
|
|
|
}
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-23 11:48:31 -07:00
|
|
|
|
public override void AddItem(PrintItemWrapper itemToAdd)
|
2015-06-22 11:50:19 -07:00
|
|
|
|
{
|
2015-06-30 18:49:20 -07:00
|
|
|
|
string destPath = rootPath;
|
2015-06-22 11:50:19 -07:00
|
|
|
|
|
2015-08-04 15:21:56 -07:00
|
|
|
|
itemToAdd.FileLocation = CopyFile(itemToAdd.FileLocation, itemToAdd.Name, destPath);
|
2015-06-22 11:50:19 -07:00
|
|
|
|
|
2015-06-25 12:19:19 -07:00
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
2015-06-22 11:50:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-24 14:00:27 -07:00
|
|
|
|
public override void Dispose()
|
|
|
|
|
|
{
|
2016-01-12 16:44:05 -08:00
|
|
|
|
if (directoryWatcher != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
directoryWatcher.EnableRaisingEvents = false;
|
2015-07-24 14:00:27 -07:00
|
|
|
|
|
2016-01-12 16:44:05 -08:00
|
|
|
|
directoryWatcher.Changed -= DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Created -= DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Deleted -= DiretoryContentsChanged;
|
|
|
|
|
|
directoryWatcher.Renamed -= DiretoryContentsChanged;
|
|
|
|
|
|
}
|
2015-07-24 14:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-15 18:31:43 -07:00
|
|
|
|
public override PrintItemCollection GetCollectionItem(int collectionIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
string directoryName = currentDirectoryDirectories[collectionIndex];
|
2015-06-30 11:34:46 -07:00
|
|
|
|
return new PrintItemCollection(Path.GetFileNameWithoutExtension(directoryName), Path.Combine(rootPath, directoryName));
|
2015-06-15 18:31:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-13 13:07:26 -07:00
|
|
|
|
public override string GetPrintItemName(int itemIndex)
|
|
|
|
|
|
{
|
2015-09-09 11:05:17 -07:00
|
|
|
|
return Path.GetFileNameWithoutExtension(currentDirectoryFiles[itemIndex]);
|
2015-07-13 13:07:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 16:56:51 -08:00
|
|
|
|
public override Task<PrintItemWrapper> GetPrintItemWrapperAsync(int itemIndex)
|
2015-06-11 16:25:12 -07:00
|
|
|
|
{
|
|
|
|
|
|
string fileName = currentDirectoryFiles[itemIndex];
|
2015-07-24 14:00:27 -07:00
|
|
|
|
|
2016-01-11 14:06:47 -08:00
|
|
|
|
var printItemWrapper = new PrintItemWrapper(new DataStorage.PrintItem(GetPrintItemName(itemIndex), fileName), this.GetProviderLocator())
|
|
|
|
|
|
{
|
|
|
|
|
|
UseIncrementedNameDuringTypeChange = true
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(printItemWrapper);
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-22 18:30:22 -07:00
|
|
|
|
public override LibraryProvider GetProviderForCollection(PrintItemCollection collection)
|
2015-06-22 18:21:56 -07:00
|
|
|
|
{
|
2015-09-02 11:05:50 -07:00
|
|
|
|
return new LibraryProviderFileSystem(Path.Combine(rootPath, collection.Key), collection.Name, this, SetCurrentLibraryProvider);
|
2015-06-22 18:21:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-27 19:03:54 -07:00
|
|
|
|
public override void RenameCollection(int collectionIndexToRename, string newName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string sourceDir = Path.Combine(rootPath, currentDirectoryDirectories[collectionIndexToRename]);
|
|
|
|
|
|
if (Directory.Exists(sourceDir))
|
|
|
|
|
|
{
|
2015-07-28 12:12:42 -07:00
|
|
|
|
string destDir = Path.Combine(Path.GetDirectoryName(sourceDir), newName);
|
2015-07-27 19:03:54 -07:00
|
|
|
|
Directory.Move(sourceDir, destDir);
|
|
|
|
|
|
Stopwatch time = Stopwatch.StartNew();
|
|
|
|
|
|
// Wait for up to some amount of time for the directory to be gone.
|
|
|
|
|
|
while (Directory.Exists(destDir)
|
|
|
|
|
|
&& time.ElapsedMilliseconds < 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(1); // make sure we are not eating all the cpu time.
|
|
|
|
|
|
}
|
|
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-22 18:30:22 -07:00
|
|
|
|
public override void RemoveCollection(int collectionIndexToRemove)
|
2015-06-11 12:06:40 -07:00
|
|
|
|
{
|
2015-07-22 18:30:22 -07:00
|
|
|
|
string directoryPath = Path.Combine(rootPath, currentDirectoryDirectories[collectionIndexToRemove]);
|
2015-06-29 18:03:56 -07:00
|
|
|
|
if (Directory.Exists(directoryPath))
|
|
|
|
|
|
{
|
2015-07-02 18:34:10 -07:00
|
|
|
|
Directory.Delete(directoryPath, true);
|
2015-07-27 19:03:54 -07:00
|
|
|
|
Stopwatch time = Stopwatch.StartNew();
|
2015-07-01 12:11:38 -07:00
|
|
|
|
// Wait for up to some amount of time for the directory to be gone.
|
2015-07-24 14:00:27 -07:00
|
|
|
|
while (Directory.Exists(directoryPath)
|
2015-07-01 12:11:38 -07:00
|
|
|
|
&& time.ElapsedMilliseconds < 100)
|
|
|
|
|
|
{
|
|
|
|
|
|
Thread.Sleep(1); // make sure we are not eating all the cpu time.
|
|
|
|
|
|
}
|
2015-06-29 18:03:56 -07:00
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
|
|
|
|
|
}
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-22 18:30:22 -07:00
|
|
|
|
public override void RemoveItem(int itemToRemoveIndex)
|
2015-06-11 12:06:40 -07:00
|
|
|
|
{
|
2015-07-22 18:30:22 -07:00
|
|
|
|
string filePath = currentDirectoryFiles[itemToRemoveIndex];
|
|
|
|
|
|
File.Delete(filePath);
|
2015-06-25 12:19:19 -07:00
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
2015-06-11 16:25:12 -07:00
|
|
|
|
|
2015-08-04 15:21:56 -07:00
|
|
|
|
private static string CopyFile(string sourceFile, string destFileName, string destPath)
|
2015-06-22 18:21:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
// make sure the directory exists
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(destPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2015-06-22 18:21:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// save it to the root directory
|
2015-08-04 15:21:56 -07:00
|
|
|
|
string outputFileName = Path.Combine(destPath, destFileName);
|
|
|
|
|
|
outputFileName = Path.ChangeExtension(outputFileName, Path.GetExtension(sourceFile));
|
2015-07-23 11:48:31 -07:00
|
|
|
|
// and copy the file
|
|
|
|
|
|
try
|
2015-06-22 18:21:56 -07:00
|
|
|
|
{
|
2015-07-23 11:48:31 -07:00
|
|
|
|
if (!File.Exists(outputFileName))
|
2015-06-22 18:21:56 -07:00
|
|
|
|
{
|
2015-08-04 15:21:56 -07:00
|
|
|
|
File.Copy(sourceFile, outputFileName);
|
2015-07-23 11:48:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
2015-06-25 19:24:37 -07:00
|
|
|
|
{
|
2015-07-23 11:48:31 -07:00
|
|
|
|
int endingNumber;
|
|
|
|
|
|
// check if the last set of characters is a number
|
|
|
|
|
|
if (int.TryParse(fileNameWithoutExtension.Substring(lastSpaceIndex), out endingNumber))
|
2015-06-25 19:24:37 -07:00
|
|
|
|
{
|
2015-07-23 11:48:31 -07:00
|
|
|
|
fileNameWithoutExtension = fileNameWithoutExtension.Substring(0, lastSpaceIndex);
|
2015-06-25 19:24:37 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-07-23 11:48:31 -07:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2015-08-04 15:21:56 -07:00
|
|
|
|
File.Copy(sourceFile, fileNameToUse);
|
2015-06-22 18:21:56 -07:00
|
|
|
|
}
|
2015-07-23 11:48:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2015-06-22 18:21:56 -07:00
|
|
|
|
}
|
2015-08-04 15:21:56 -07:00
|
|
|
|
|
|
|
|
|
|
return outputFileName;
|
2015-06-22 18:21:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-25 12:19:19 -07:00
|
|
|
|
private void DiretoryContentsChanged(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
GetFilesAndCollectionsInCurrentDirectory();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-25 13:04:14 -07:00
|
|
|
|
private async void GetFilesAndCollectionsInCurrentDirectory(bool recursive = false)
|
2015-06-11 16:25:12 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
List<string> newReadDirectoryDirectories = new List<string>();
|
|
|
|
|
|
List<string> newReadDirectoryFiles = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
await Task.Run(() =>
|
2015-06-12 09:48:20 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
try
|
2015-06-12 09:48:20 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
string[] directories = null;
|
|
|
|
|
|
if (recursive)
|
|
|
|
|
|
{
|
|
|
|
|
|
directories = Directory.GetDirectories(Path.Combine(rootPath, currentDirectory), "*.*", SearchOption.AllDirectories);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
directories = Directory.GetDirectories(Path.Combine(rootPath, currentDirectory));
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (string directoryName in directories)
|
2015-07-23 10:40:36 -07:00
|
|
|
|
{
|
|
|
|
|
|
string subPath = directoryName.Substring(rootPath.Length + 1);
|
2015-08-25 13:04:14 -07:00
|
|
|
|
newReadDirectoryDirectories.Add(subPath);
|
2015-07-23 10:40:36 -07:00
|
|
|
|
}
|
2015-06-12 09:48:20 -07:00
|
|
|
|
}
|
2015-08-25 13:04:14 -07:00
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2015-08-25 13:04:14 -07:00
|
|
|
|
}
|
2015-06-12 09:48:20 -07:00
|
|
|
|
|
2015-08-25 13:04:14 -07:00
|
|
|
|
try
|
2015-06-11 16:25:12 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
string upperFilter = keywordFilter.ToUpper();
|
|
|
|
|
|
string[] files = Directory.GetFiles(Path.Combine(rootPath, currentDirectory));
|
|
|
|
|
|
foreach (string filename in files)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ApplicationSettings.LibraryFilterFileExtensions.Contains(Path.GetExtension(filename).ToLower()))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (upperFilter.Trim() == string.Empty
|
2016-01-11 17:28:59 -08:00
|
|
|
|
|| FileNameContainsFilter(filename, upperFilter))
|
2015-08-25 13:04:14 -07:00
|
|
|
|
{
|
|
|
|
|
|
newReadDirectoryFiles.Add(filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (recursive)
|
2015-06-11 16:25:12 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
foreach (string directory in newReadDirectoryDirectories)
|
2015-07-23 10:40:36 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
string subDirectory = Path.Combine(rootPath, directory);
|
2016-01-11 17:28:59 -08:00
|
|
|
|
string[] subDirectoryFiles = Directory.GetFiles(subDirectory);
|
2015-08-25 13:04:14 -07:00
|
|
|
|
foreach (string filename in subDirectoryFiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ApplicationSettings.LibraryFilterFileExtensions.Contains(Path.GetExtension(filename).ToLower()))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (keywordFilter.Trim() == string.Empty
|
2016-01-11 17:28:59 -08:00
|
|
|
|
|| FileNameContainsFilter(filename, upperFilter) )
|
2015-08-25 13:04:14 -07:00
|
|
|
|
{
|
|
|
|
|
|
newReadDirectoryFiles.Add(filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-07-23 10:40:36 -07:00
|
|
|
|
}
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-08-25 13:04:14 -07:00
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
2015-09-17 13:45:26 -07:00
|
|
|
|
GuiWidget.BreakInDebugger();
|
2015-08-25 13:04:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentDirectoryDirectories.Clear();
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
2015-08-25 13:04:14 -07:00
|
|
|
|
else
|
2015-07-23 10:40:36 -07:00
|
|
|
|
{
|
2015-08-25 13:04:14 -07:00
|
|
|
|
currentDirectoryDirectories = newReadDirectoryDirectories;
|
2015-07-23 10:40:36 -07:00
|
|
|
|
}
|
2015-08-25 13:04:14 -07:00
|
|
|
|
currentDirectoryFiles = newReadDirectoryFiles;
|
2015-07-01 16:26:37 -07:00
|
|
|
|
|
2015-07-27 10:27:22 -07:00
|
|
|
|
OnDataReloaded(null);
|
2015-06-11 16:25:12 -07:00
|
|
|
|
}
|
2015-06-22 18:21:56 -07:00
|
|
|
|
|
2016-01-11 17:28:59 -08:00
|
|
|
|
private bool FileNameContainsFilter(string filename, string upperFilter)
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] mustContains = upperFilter.Split(' ');
|
|
|
|
|
|
foreach (string check in mustContains)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!Path.GetFileNameWithoutExtension(filename.ToUpper().Replace('_', ' ')).Contains(check))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-22 18:21:56 -07:00
|
|
|
|
private string GetPathFromLocator(List<ProviderLocatorNode> providerLocator)
|
|
|
|
|
|
{
|
|
|
|
|
|
string pathWithDot = Path.Combine(rootPath, providerLocator[providerLocator.Count - 1].Key);
|
|
|
|
|
|
string pathWithoutDot = pathWithDot.Replace("." + Path.DirectorySeparatorChar, "");
|
|
|
|
|
|
return pathWithoutDot;
|
|
|
|
|
|
}
|
2015-06-11 12:06:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|