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.
This commit is contained in:
mcored 2014-04-15 22:04:34 +08:00
parent 110277b419
commit 991273a9b1
3 changed files with 58 additions and 1 deletions

View file

@ -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; }

View file

@ -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)

View file

@ -32,6 +32,7 @@
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;
}
}
}