From b568d65c3028ca4273336b39a795c261c5a31fd7 Mon Sep 17 00:00:00 2001 From: "Tobias Burdow [Kaleidox]" Date: Thu, 25 Aug 2022 21:27:52 +0200 Subject: [PATCH] add support for creating link to onedrive config --- DirLinkerConfig/Configuration.cs | 21 +++++++- DirLinkerConfig/DirLinkerConfig.csproj | 1 + DirLinkerWPF/DirLinker.xaml.cs | 66 ++++++++++++++++++++++++-- DirLinkerWPF/DirLinkerWPF.csproj | 1 + HardLinkTool/HardLinkTool.cs | 24 +++++++++- HardLinkTool/HardLinkTool.csproj | 1 + 6 files changed, 109 insertions(+), 5 deletions(-) diff --git a/DirLinkerConfig/Configuration.cs b/DirLinkerConfig/Configuration.cs index 0da60d1..81a6113 100644 --- a/DirLinkerConfig/Configuration.cs +++ b/DirLinkerConfig/Configuration.cs @@ -12,7 +12,20 @@ namespace DirLinkerConfig public static class DirLinkerInfo { public const string ApplyConfigArgument = "--applyConfig"; + public const string CreateConfigLink = "--createConfigLink"; public const string HaltOnErrorOnly = "--haltOnErrorOnly"; + + public static void MkDirs(this FileInfo file) + { + if (!file.Directory?.Exists ?? false) + file.Directory.MkDirs(); + } + + public static void MkDirs(this DirectoryInfo dir) + { + if (!dir.Parent?.Exists ?? false) + dir.Create(); + } } public class Configuration : IUpdateable @@ -26,7 +39,13 @@ public class Configuration : IUpdateable static Configuration() { DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "org.comroid"); - ConfigFile = Path.Combine(DataDir, "dirLinker.json"); + ConfigFile = Path.Combine(DataDir, +#if DEBUG + "dirLinker-debug.json" +#else + "dirLinker.json" +#endif + ); Directory.CreateDirectory(DataDir); } diff --git a/DirLinkerConfig/DirLinkerConfig.csproj b/DirLinkerConfig/DirLinkerConfig.csproj index 3f55d4a..fcf4451 100644 --- a/DirLinkerConfig/DirLinkerConfig.csproj +++ b/DirLinkerConfig/DirLinkerConfig.csproj @@ -3,6 +3,7 @@ netcoreapp3.1 0.2.0 + 0.1.3 diff --git a/DirLinkerWPF/DirLinker.xaml.cs b/DirLinkerWPF/DirLinker.xaml.cs index 96cd941..d2bc7be 100644 --- a/DirLinkerWPF/DirLinker.xaml.cs +++ b/DirLinkerWPF/DirLinker.xaml.cs @@ -35,9 +35,8 @@ public DirLinker() if (!File.Exists(Configuration.ConfigFile)) { // ask the user to use config from a host; e.g. onedrive - // todo + TryLinkConfig(); } - Configuration.LoadConfig(this); CleanupConfig(); } @@ -55,6 +54,67 @@ private void UpdateUI() ReloadView(); } + private void TryLinkConfig() + { + Window[] window = new Window[1]; + Button yesBtn = new Button() + { + Content = "Yes" + }; + Button noBtn = new Button() + { + Content = "No" + }; + yesBtn.Click += (sender, args) => + { + CreateOneDriveConfigLink(); + window[0].Close(); + }; + noBtn.Click += (sender, args) => window[0].Close(); + window[0] = new Window() + { + Height = 360, + Width = 800, + Title = "DirLinker - Create configuration link?", + Content = new StackPanel() + { + Orientation = Orientation.Vertical, + Children = + { + new TextBlock() + { + HorizontalAlignment = HorizontalAlignment.Stretch, + VerticalAlignment = VerticalAlignment.Stretch, + TextAlignment = TextAlignment.Center, + Text = "Do you want to link configuration to OneDrive?" + }, + new StackPanel() + { + Orientation = Orientation.Horizontal, + Children = + { + yesBtn, + noBtn + } + } + } + } + }; + window[0].ShowDialog(); + } + + private void CreateOneDriveConfigLink() + { + var startInfo = new ProcessStartInfo + { + FileName = "HardLinkTool.exe", + Arguments = DirLinkerInfo.CreateConfigLink + ' ' + DirLinkerInfo.HaltOnErrorOnly, + UseShellExecute = true, + Verb = "runas" + }; + Process.Start(startInfo)!.WaitForExit(); + } + private void ApplyConfigToOS() { var startInfo = new ProcessStartInfo @@ -138,7 +198,7 @@ private void PromptText(object line) new Window { Height = 260, - Width = 800, + Width = 1200, Content = new TextBlock { HorizontalAlignment = HorizontalAlignment.Stretch, diff --git a/DirLinkerWPF/DirLinkerWPF.csproj b/DirLinkerWPF/DirLinkerWPF.csproj index bd1581b..45c5b2b 100644 --- a/DirLinkerWPF/DirLinkerWPF.csproj +++ b/DirLinkerWPF/DirLinkerWPF.csproj @@ -7,6 +7,7 @@ logo-clean.ico DirLinkerWPF.App 0.1.1 + 0.1.3 diff --git a/HardLinkTool/HardLinkTool.cs b/HardLinkTool/HardLinkTool.cs index 176fe5d..1e56e23 100644 --- a/HardLinkTool/HardLinkTool.cs +++ b/HardLinkTool/HardLinkTool.cs @@ -40,7 +40,29 @@ private static void WaitForUserInput() private static void Run(string[] args) { - if (args.Contains(DirLinkerInfo.ApplyConfigArgument)) + if (args.Contains(DirLinkerInfo.CreateConfigLink)) + { + try + { + var targetFile = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "OneDrive", "config", + "org.comroid"); + var target = new FileInfo(targetFile); + if (target.Exists && target.IsSymbolicLink() && target.IsSymbolicLinkValid()) + { + Console.WriteLine("Skipping creation of config symlink because a symlink already exists"); + return; + } + target.MkDirs(); + target.Create(); + Console.WriteLine($"Creating configuration link from {Configuration.ConfigFile} to {targetFile}"); + target.CreateSymbolicLink(Configuration.ConfigFile); + } + catch (UnauthorizedAccessException _) + { + } + } + else if (args.Contains(DirLinkerInfo.ApplyConfigArgument)) { Configuration.LoadConfig(); ApplyConfig(); diff --git a/HardLinkTool/HardLinkTool.csproj b/HardLinkTool/HardLinkTool.csproj index c9e3bdd..c382122 100644 --- a/HardLinkTool/HardLinkTool.csproj +++ b/HardLinkTool/HardLinkTool.csproj @@ -5,6 +5,7 @@ netcoreapp3.1 HardLinkTool.HardLinkTool 0.1.0 + 0.1.3