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 { readonly IFileSystem _fileSystem; /// /// 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; } = DefaultUseColors; /// /// Gets or sets the class responsible for determining which symbol /// to use to represent files and directories. /// public FileSystemIconProvider IconProvider { get; set;} = new FileSystemIconProvider(); /// /// Gets or sets the class thatis responsible for determining which color /// to use to represent files and directories when is /// . /// public FileSystemColorProvider ColorProvider { get;set;} = new FileSystemColorProvider(); /// /// Gets or sets the culture to use (e.g. for number formatting). /// Defaults to . /// public CultureInfo Culture {get;set;} = CultureInfo.CurrentUICulture; /// /// 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; 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; 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 the text on the 'Cancel' button. /// public string CancelButtonText { get; set; } = "Cancel"; /// /// Gets or sets error message when user attempts to select a file type that is not one of /// public string WrongFileTypeFeedback { get; 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; 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; 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; 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; 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; set; } = Strings.fdFileOrDirectoryMustExistFeedback; /// /// Gets the style settings for the table of files (in currently selected directory). /// public 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 Func> TreeRootGetter { get; set; } /// /// Gets or sets whether to use advanced unicode characters which might not be installed /// on all users computers. /// public bool UseUnicodeCharacters { get; set; } = DefaultUseUnicodeCharacters; /// /// 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 (IFileSystem fileSystem) { _fileSystem = fileSystem; TreeRootGetter = DefaultTreeRootGetter; DateFormat = CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern; } private Dictionary DefaultTreeRootGetter () { var roots = new Dictionary (); try { foreach (var d in Environment.GetLogicalDrives ()) { var dir = _fileSystem.DirectoryInfo.New (d); if (!roots.ContainsKey(dir)) { roots.Add (dir, 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)) { continue; } var dir = _fileSystem.DirectoryInfo.New (path); if (!roots.ContainsKey (dir) && dir.Exists) { roots.Add (dir, special.ToString()); } } 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; } } }