add FindFileOptions parm to EnumerateDataPaths

This commit is contained in:
Michael Becker 2022-12-04 13:59:30 -05:00
parent ab32a58e05
commit 53fefe96f5
No known key found for this signature in database
GPG Key ID: DA394832305DA332
2 changed files with 67 additions and 22 deletions

View File

@ -79,7 +79,7 @@ namespace MBS.Framework
}
List<string> files = new List<string>();
string[] paths = EnumerateDataPaths();
string[] paths = EnumerateDataPaths(options);
foreach (string path in paths)
{
string file = System.IO.Path.Combine(new string[] { path, filename });
@ -104,6 +104,11 @@ namespace MBS.Framework
{
return files[0];
}
if ((options & FindFileOptions.Create) == FindFileOptions.Create)
{
string[] paths = EnumerateDataPaths(options);
return System.IO.Path.Combine(paths[0], filename);
}
return null;
}
@ -212,31 +217,56 @@ namespace MBS.Framework
}
}
public string[] EnumerateDataPaths()
protected virtual string DataDirectoryName => ShortName;
public string[] EnumerateDataPaths() => EnumerateDataPaths(FindFileOptions.All);
public string[] EnumerateDataPaths(FindFileOptions options)
{
return new string[]
List<string> list = new List<string>();
if ((options & FindFileOptions.All) == FindFileOptions.All)
{
// first look in the application root directory since this will override everything else
BasePath,
list.Add(BasePath);
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
// if we are on Unix or Mac OS X, look in /etc/...
list.Add(String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
{
String.Empty, // *nix root directory
"etc",
DataDirectoryName
}));
}
// then look in /usr/share/universal-editor or C:\ProgramData\Mike Becker's Software\Universal Editor
String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
list.Add(String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
{
System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData),
ShortName
}),
DataDirectoryName
}));
// then look in ~/.local/share/universal-editor or C:\Users\USERNAME\AppData\Local\Mike Becker's Software\Universal Editor
String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
list.Add(String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
{
System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData),
ShortName
}),
// then look in ~/.universal-editor or C:\Users\USERNAME\AppData\Roaming\Mike Becker's Software\Universal Editor
String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
{
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),
ShortName
})
};
DataDirectoryName
}));
}
//fixme: addd finddfileoption.userconfig, localdata, etc.
// now for the user-writable locations...
// then look in ~/.universal-editor or C:\Users\USERNAME\AppData\Roaming\Mike Becker's Software\Universal Editor
list.Add(String.Join(System.IO.Path.DirectorySeparatorChar.ToString(), new string[]
{
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData),
DataDirectoryName
}));
return list.ToArray();
}
public string ShortName { get; set; }
@ -364,7 +394,7 @@ namespace MBS.Framework
{
for (i++; i < args.Length; i++)
{
if (ParseOption(args, ref i, null, cmd.Options))
if (ParseOption(args, ref i, cline.Options, cmd.Options))
break;
}
@ -382,8 +412,17 @@ namespace MBS.Framework
}
}
OnActivated(new ApplicationActivatedEventArgs(true, ApplicationActivationType.CommandLineLaunch, cline));
return 0;
ApplicationActivatedEventArgs e = new ApplicationActivatedEventArgs(true, ApplicationActivationType.CommandLineLaunch, cline);
if (cline.Command != null && cline.Command.ActivationDelegate != null)
{
// use the activation delegate instead of calling OnActivated
cline.Command.ActivationDelegate(e);
}
else
{
OnActivated(e);
}
return e.ExitCode;
}

View File

@ -24,17 +24,23 @@ namespace MBS.Framework
/// <summary>
/// Controls the behavior of relative file resolution.
/// </summary>
[Flags()]
public enum FindFileOptions
{
/// <summary>
/// Returns all matching fully-qualified file paths across all global,
/// application, and user directories.
/// </summary>
All = 0,
All = 1,
/// <summary>
/// Returns only file paths that are writable by the user (i.e., in the
/// user's local or roaming data directory).
/// </summary>
UserWritable = 1
UserWritable = 2,
/// <summary>
/// Allows the user to create a file if it does not exist (i.e., returns
/// a file name even if it does not exist)
/// </summary>
Create = 4
}
}