From 991273a9b18bfd6c5133dc690a895746b7fbf141 Mon Sep 17 00:00:00 2001 From: mcored Date: Tue, 15 Apr 2014 22:04:34 +0800 Subject: [PATCH] Detect changes done in UploaderConfig.json This is useful when the UploaderConfig.json is in a shared location between two or more computers. When one PC updates UploaderConfig.json, the other PC can detect changes and automatically reload new changes to memory. --- ShareX/ApplicationConfig.cs | 3 +++ ShareX/Forms/MainForm.cs | 4 ++- ShareX/Program.cs | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/ShareX/ApplicationConfig.cs b/ShareX/ApplicationConfig.cs index dccfa9abe..d27a1b9c0 100644 --- a/ShareX/ApplicationConfig.cs +++ b/ShareX/ApplicationConfig.cs @@ -114,6 +114,9 @@ public ApplicationConfig() [Category("Application"), DefaultValue(false), Description("By default copying \"Bitmap\" to clipboard. Alternative method copying \"PNG and DIB\" to clipboard.")] public bool UseAlternativeClipboardCopyImage { get; set; } + [Category("Application / Config"), DefaultValue(false), Description("Automatically detect external changes to UploaderConfig file and load settigns to memory.")] + public bool DetectUploaderConfigFileChanges { get; set; } + [Category("Clipboard upload"), DefaultValue(true), Description("Show clipboard content viewer when using clipboard upload in main window.")] public bool ShowClipboardContentViewer { get; set; } diff --git a/ShareX/Forms/MainForm.cs b/ShareX/Forms/MainForm.cs index f51919065..4843d8e62 100644 --- a/ShareX/Forms/MainForm.cs +++ b/ShareX/Forms/MainForm.cs @@ -774,6 +774,8 @@ private void tsbApplicationSettings_Click(object sender, EventArgs e) AfterSettingsJobs(); Program.Settings.SaveAsync(Program.ApplicationConfigFilePath); + + Program.ConfigureUploadersConfigWatcher(); } private void tsbTaskSettings_Click(object sender, EventArgs e) @@ -813,7 +815,7 @@ private void tsbDestinationSettings_Click(object sender, EventArgs e) uploadersConfigForm.ShowDialog(); } - Program.UploadersConfig.SaveAsync(Program.UploadersConfigFilePath); + Program.UploadersConfigSaveAsync(); } private void tsmiCursorHelper_Click(object sender, EventArgs e) diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 722c1619e..1fbea3952 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -32,6 +32,7 @@ You should have received a copy of the GNU General Public License using System.Runtime.InteropServices; using System.Text; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using UploadersLib; @@ -83,6 +84,9 @@ public static string AssemblyCopyright public static string CustomPersonalPath { get; private set; } + private static FileSystemWatcher uploaderConfigWatcher; + private static WatchFolderDuplicateEventTimer uploaderConfigWatcherTimer; + public static string PersonalPath { get @@ -335,6 +339,8 @@ public static void LoadSettings() UploaderSettingsResetEvent.Set(); LoadHotkeySettings(); HotkeySettingsResetEvent.Set(); + + ConfigureUploadersConfigWatcher(); } public static void LoadProgramSettings() @@ -485,5 +491,51 @@ private static bool WaitFormLoad(int wait) return false; } + + public static void ConfigureUploadersConfigWatcher() + { + if (Program.Settings.DetectUploaderConfigFileChanges) + { + if (uploaderConfigWatcher == null) + { + uploaderConfigWatcher = new FileSystemWatcher(Path.GetDirectoryName(Program.UploadersConfigFilePath), Path.GetFileName(Program.UploadersConfigFilePath)); + uploaderConfigWatcher.Changed += uploaderConfigWatcher_Changed; + uploaderConfigWatcherTimer = new WatchFolderDuplicateEventTimer(Program.UploadersConfigFilePath); + uploaderConfigWatcher.EnableRaisingEvents = true; + } + } + else + { + if (uploaderConfigWatcher != null) + { + uploaderConfigWatcher.EnableRaisingEvents = false; + uploaderConfigWatcher = null; + } + } + } + + private static void uploaderConfigWatcher_Changed(object sender, FileSystemEventArgs e) + { + if (!uploaderConfigWatcherTimer.IsDuplicateEvent(e.FullPath)) + { + SynchronizationContext context = SynchronizationContext.Current ?? new SynchronizationContext(); + Action onCompleted = () => context.Post(state => ReloadUploadersConfig(e.FullPath), null); + Helpers.WaitWhileAsync(() => Helpers.IsFileLocked(e.FullPath), 250, 5000, onCompleted, 1000); + uploaderConfigWatcherTimer = new WatchFolderDuplicateEventTimer(e.FullPath); + } + } + + private static void ReloadUploadersConfig(string filePath) + { + DebugHelper.WriteLine("uploaderConfigWatcher_Changed"); + UploadersConfig = UploadersLib.UploadersConfig.Load(filePath); + } + + public async static void UploadersConfigSaveAsync() + { + if (uploaderConfigWatcher != null) uploaderConfigWatcher.EnableRaisingEvents = false; + await TaskEx.Run(() => UploadersConfig.Save(Program.UploadersConfigFilePath)); + if (uploaderConfigWatcher != null) uploaderConfigWatcher.EnableRaisingEvents = true; + } } } \ No newline at end of file