diff --git a/MBS.Framework/Application.cs b/MBS.Framework/Application.cs index 6dae0ae..a1b160e 100644 --- a/MBS.Framework/Application.cs +++ b/MBS.Framework/Application.cs @@ -79,7 +79,7 @@ namespace MBS.Framework } List files = new List(); - 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 list = new List(); + + 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; } diff --git a/MBS.Framework/FindFileOptions.cs b/MBS.Framework/FindFileOptions.cs index e08bc94..b352a0e 100644 --- a/MBS.Framework/FindFileOptions.cs +++ b/MBS.Framework/FindFileOptions.cs @@ -24,17 +24,23 @@ namespace MBS.Framework /// /// Controls the behavior of relative file resolution. /// + [Flags()] public enum FindFileOptions { /// /// Returns all matching fully-qualified file paths across all global, /// application, and user directories. /// - All = 0, + All = 1, /// /// Returns only file paths that are writable by the user (i.e., in the /// user's local or roaming data directory). /// - UserWritable = 1 + UserWritable = 2, + /// + /// Allows the user to create a file if it does not exist (i.e., returns + /// a file name even if it does not exist) + /// + Create = 4 } }