using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using Terminal.Gui.Resources;
using static System.Environment;
using static Terminal.Gui.ConfigurationManager;
namespace Terminal.Gui {
///
/// Stores style settings for .
///
public class FileDialogStyle {
///
/// Gets or sets the default value to use for .
/// This can be populated from .tui config files via
///
[SerializableConfigurationProperty(Scope = typeof (SettingsScope))]
public static bool DefaultUseColors { get; set; }
///
/// Gets or sets the default value to use for .
/// This can be populated from .tui config files via
///
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static bool DefaultUseUnicodeCharacters { get; set; }
///
/// Gets or Sets a value indicating whether different colors
/// should be used for different file types/directories. Defaults
/// to false.
///
public bool UseColors { get; set; }
///
/// Sets a to use for directories rows of
/// the .
///
public ColorScheme ColorSchemeDirectory { get; set; }
///
/// Sets a to use for file rows with an image extension
/// of the . Defaults to White text on Black background.
///
public ColorScheme ColorSchemeImage { get; set; }
///
/// Sets a to use for file rows with an executable extension
/// or that match in the .
///
public ColorScheme ColorSchemeExeOrRecommended { get; set; }
///
/// Colors to use when is true but file does not match any other
/// classification (, etc).
///
public ColorScheme ColorSchemeOther { get; set; }
///
/// Gets or sets the header text displayed in the Filename column of the files table.
///
public string FilenameColumnName { get; set; } = Strings.fdFilename;
///
/// Gets or sets the header text displayed in the Size column of the files table.
///
public string SizeColumnName { get; set; } = Strings.fdSize;
///
/// Gets or sets the header text displayed in the Modified column of the files table.
///
public string ModifiedColumnName { get; set; } = Strings.fdModified;
///
/// Gets or sets the header text displayed in the Type column of the files table.
///
public string TypeColumnName { get; set; } = Strings.fdType;
///
/// Gets or sets the text displayed in the 'Search' text box when user has not supplied any input yet.
///
public string SearchCaption { get; internal set; } = Strings.fdSearchCaption;
///
/// Gets or sets the text displayed in the 'Path' text box when user has not supplied any input yet.
///
public string PathCaption { get; internal set; } = Strings.fdPathCaption;
///
/// Gets or sets the text on the 'Ok' button. Typically you may want to change this to
/// "Open" or "Save" etc.
///
public string OkButtonText { get; set; } = "Ok";
///
/// Gets or sets error message when user attempts to select a file type that is not one of
///
public string WrongFileTypeFeedback { get; internal set; } = Strings.fdWrongFileTypeFeedback;
///
/// Gets or sets error message when user selects a directory that does not exist and
/// is and is .
///
public string DirectoryMustExistFeedback { get; internal set; } = Strings.fdDirectoryMustExistFeedback;
///
/// Gets or sets error message when user is
/// and user enters the name of an existing file (File system cannot have a folder with the same name as a file).
///
public string FileAlreadyExistsFeedback { get; internal set; } = Strings.fdFileAlreadyExistsFeedback;
///
/// Gets or sets error message when user selects a file that does not exist and
/// is and is .
///
public string FileMustExistFeedback { get; internal set; } = Strings.fdFileMustExistFeedback;
///
/// Gets or sets error message when user is
/// and user enters the name of an existing directory (File system cannot have a folder with the same name as a file).
///
public string DirectoryAlreadyExistsFeedback { get; internal set; } = Strings.fdDirectoryAlreadyExistsFeedback;
///
/// Gets or sets error message when user selects a file/dir that does not exist and
/// is and is .
///
public string FileOrDirectoryMustExistFeedback { get; internal set; } = Strings.fdFileOrDirectoryMustExistFeedback;
///
/// Gets the style settings for the table of files (in currently selected directory).
///
public TableView.TableStyle TableStyle { get; internal set; }
///
/// Gets the style settings for the collapse-able directory/places tree
///
public TreeStyle TreeStyle { get; internal set; }
///
/// Gets or Sets the method for getting the root tree objects that are displayed in
/// the collapse-able tree in the . Defaults to all accessible
/// and unique
/// .
///
/// Must be configured before showing the dialog.
public FileDialogTreeRootGetter TreeRootGetter { get; set; } = DefaultTreeRootGetter;
///
/// Gets or sets whether to use advanced unicode characters which might not be installed
/// on all users computers.
///
public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters;
///
/// User defined delegate for picking which character(s)/unicode
/// symbol(s) to use as an 'icon' for files/folders.
///
public Func IconGetter { get; set; }
///
/// Gets or sets the format to use for date/times in the Modified column.
/// Defaults to
/// of the
///
public string DateFormat { get; set; }
///
/// Creates a new instance of the class.
///
public FileDialogStyle ()
{
IconGetter = DefaultIconGetter;
DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern;
ColorSchemeDirectory = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.Blue, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.Blue, Color.Black),
Focus = Application.Driver.MakeAttribute (Color.Black, Color.Blue),
HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Blue),
};
ColorSchemeImage = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.Magenta, Color.Black),
Focus = Application.Driver.MakeAttribute (Color.Black, Color.Magenta),
HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Magenta),
};
ColorSchemeExeOrRecommended = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.Green, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.Green, Color.Black),
Focus = Application.Driver.MakeAttribute (Color.Black, Color.Green),
HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.Green),
};
ColorSchemeOther = new ColorScheme {
Normal = Application.Driver.MakeAttribute (Color.White, Color.Black),
HotNormal = Application.Driver.MakeAttribute (Color.White, Color.Black),
Focus = Application.Driver.MakeAttribute (Color.Black, Color.White),
HotFocus = Application.Driver.MakeAttribute (Color.Black, Color.White),
};
}
private string DefaultIconGetter (IFileSystemInfo arg)
{
if (arg is IDirectoryInfo) {
return UseUnicodeCharacters ? "\ua909 " : "\\";
}
return UseUnicodeCharacters ? "\u2630 " : "";
}
private static IEnumerable DefaultTreeRootGetter ()
{
var roots = new List ();
try {
foreach (var d in Environment.GetLogicalDrives ()) {
roots.Add (new FileDialogRootTreeNode (d, new DirectoryInfo (d)));
}
} catch (Exception) {
// Cannot get the system disks thats fine
}
try {
foreach (var special in Enum.GetValues (typeof (Environment.SpecialFolder)).Cast ()) {
try {
var path = Environment.GetFolderPath (special);
if (
!string.IsNullOrWhiteSpace (path)
&& Directory.Exists (path)
&& !roots.Any (r => string.Equals (r.Path.FullName, path))) {
roots.Add (new FileDialogRootTreeNode (
special.ToString (),
new DirectoryInfo (Environment.GetFolderPath (special))));
}
} catch (Exception) {
// Special file exists but contents are unreadable (permissions?)
// skip it anyway
}
}
} catch (Exception) {
// Cannot get the special files for this OS oh well
}
return roots;
}
}
}