using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.FileSystem;
using Tango.FileSystem.Network;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Storage;
namespace Tango.FSE.UI.Storage
{
///
/// Represents the default implementation.
///
///
///
public class DefaultStorageProvider : ExtendedObject, IStorageProvider
{
///
/// Gets or sets a value indicating whether use the native file system dialogs.
///
public bool UseNativeDialogs { get; set; } = true;
///
/// Invokes a single file open dialog.
///
/// The title.
/// The filter.
/// The initial folder.
///
///
public Task OpenFile(string title, string filter = null, string initialFolder = null)
{
if (UseNativeDialogs)
{
return OpenFileNative(title, filter, initialFolder);
}
else
{
throw new NotImplementedException();
}
}
///
/// Invokes a multi select file open dialog.
///
/// The title.
/// The filter.
/// The initial folder.
///
///
public Task OpenFiles(string title, string filter = null, string initialFolder = null)
{
if (UseNativeDialogs)
{
return OpenFilesNative(title, filter, initialFolder);
}
else
{
throw new NotImplementedException();
}
}
///
/// Invokes a multi select file/folder open dialog.
///
/// The title.
/// The filter.
/// The initial folder.
///
///
public Task SelectFilesAndFolders(string title, string filter = null, string initialFolder = null)
{
if (UseNativeDialogs)
{
return OpenFilesAndFoldersNative(title, filter, initialFolder);
}
else
{
throw new NotImplementedException();
}
}
///
/// Invokes a file save dialog.
///
/// The title.
/// The filter.
/// Default name of the file.
/// The default extension.
/// The initial folder.
///
///
public Task SaveFile(string title, string filter = null, string defaultFileName = null, string defaultExtension = null, string initialFolder = null)
{
if (UseNativeDialogs)
{
return SaveFileNative(title, filter, defaultFileName, defaultExtension, initialFolder);
}
else
{
throw new NotImplementedException();
}
}
///
/// Invokes a single folder selection dialog.
///
/// The title.
/// The initial folder.
///
///
public async Task SelectFolder(string title, string initialFolder = null)
{
if (UseNativeDialogs)
{
return await SelectFolderNative(title, initialFolder);
}
else
{
throw new NotImplementedException();
}
}
///
/// Opens the windows explorer and select the specified file or folder.
///
/// The file or folder path.
///
public Task ShowInExplorer(string path)
{
return Task.Factory.StartNew(() =>
{
if (File.Exists(path) || Directory.Exists(path))
{
Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path));
}
else
{
path = Path.GetDirectoryName(path);
Process.Start("explorer.exe", path);
}
});
}
#region Native
public Task OpenFileNative(string title, string filter = null, string initialFolder = null)
{
SingleStorageResult result = new SingleStorageResult();
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Title = title;
if (filter != null)
{
dlg.Filter = filter;
}
if (initialFolder != null)
{
dlg.InitialDirectory = initialFolder;
}
if (dlg.ShowDialog(Application.Current.MainWindow).Value)
{
result.Confirmed = true;
result.SelectedItem = dlg.FileName;
}
return Task.FromResult(result);
}
public Task OpenFilesNative(string title, string filter = null, string initialFolder = null)
{
MultiStorageResult result = new MultiStorageResult();
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Title = title;
dlg.Multiselect = true;
if (filter != null)
{
dlg.Filter = filter;
}
if (initialFolder != null)
{
dlg.InitialDirectory = initialFolder;
}
if (dlg.ShowDialog(Application.Current.MainWindow).Value)
{
result.Confirmed = true;
result.SelectedItems = dlg.FileNames.ToList();
}
return Task.FromResult(result);
}
public Task OpenFilesAndFoldersNative(string title, string filter = null, string initialFolder = null)
{
MultiStorageResult result = new MultiStorageResult();
ExplorerControlDialog dlg = new ExplorerControlDialog();
dlg.Owner = Application.Current.MainWindow;
dlg.Title = title;
if (initialFolder != null)
{
dlg.InitialDirectory = initialFolder;
}
if (dlg.ShowDialog().Value)
{
result.Confirmed = true;
result.SelectedItems = dlg.SelectedItems.ToList();
}
return Task.FromResult(result);
}
public Task SaveFileNative(string title, string filter = null, string defaultFileName = null, string defaultExtension = null, string initialFolder = null)
{
SingleStorageResult result = new SingleStorageResult();
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = title;
if (defaultFileName != null)
{
dlg.FileName = defaultFileName;
}
if (defaultExtension != null)
{
dlg.DefaultExt = defaultExtension;
}
if (filter != null)
{
dlg.Filter = filter;
}
if (initialFolder != null)
{
dlg.InitialDirectory = initialFolder;
}
if (dlg.ShowDialog(Application.Current.MainWindow).Value)
{
result.Confirmed = true;
result.SelectedItem = dlg.FileName;
}
return Task.FromResult(result);
}
public Task SelectFolderNative(string title, string initialFolder = null)
{
SingleStorageResult result = new SingleStorageResult();
CommonOpenFileDialog dlg = new CommonOpenFileDialog();
dlg.Title = title;
dlg.IsFolderPicker = true;
if (initialFolder != null)
{
dlg.InitialDirectory = initialFolder;
}
if (dlg.ShowDialog(Application.Current.MainWindow) == CommonFileDialogResult.Ok)
{
result.Confirmed = true;
result.SelectedItem = dlg.FileName;
}
return Task.FromResult(result);
}
#endregion
}
}