From 4ddca269f126e63db757f256a7331cc7cfb4ac80 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bellai=20=C3=81d=C3=A1m?= Date: Tue, 10 Oct 2017 12:52:03 +0000 Subject: [PATCH] git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube;C30576 --- client/Maestro/MaestroForm.Source.cs | 3 +- client/Maestro/MaestroForm.cs | 2 +- client/Maestro/Sources/FileSystemSource.cs | 57 ++++++++++++++++------ 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/client/Maestro/MaestroForm.Source.cs b/client/Maestro/MaestroForm.Source.cs index 7a1af3bc..fd47128f 100644 --- a/client/Maestro/MaestroForm.Source.cs +++ b/client/Maestro/MaestroForm.Source.cs @@ -37,7 +37,8 @@ namespace Maestro { if (Configuration?.Source is UNCSource) { logger.Debug("UNC source initialization"); - source = new FileSystemSource(this, MessageBus, Configuration.Source.Filter); + Action errorReporter = ReportError; + source = new FileSystemSource(this, MessageBus, Configuration.Source.Filter, errorReporter); logger.Debug("UNC source created"); } if (Configuration?.Source is NEXIOSource) { diff --git a/client/Maestro/MaestroForm.cs b/client/Maestro/MaestroForm.cs index 038d9f80..97a9db7c 100644 --- a/client/Maestro/MaestroForm.cs +++ b/client/Maestro/MaestroForm.cs @@ -260,7 +260,7 @@ namespace Maestro { public void ReportError(DateTime time, String message, Color color) { - systemMessageBindingSource.Insert(0, new SystemMessage() { Time = time, message = message}); + systemMessageBindingSource.Insert(0, new SystemMessage() { Time = time, message = message }); UpdateDataChanged(); newMSGColor = color; } diff --git a/client/Maestro/Sources/FileSystemSource.cs b/client/Maestro/Sources/FileSystemSource.cs index 3b24042c..9656966a 100644 --- a/client/Maestro/Sources/FileSystemSource.cs +++ b/client/Maestro/Sources/FileSystemSource.cs @@ -13,6 +13,8 @@ using System.Drawing; using NLog; namespace Maestro.Sources { + public delegate void ClearAndInitialize(); + class FileSystemSource : BindingList, ISource { private readonly Logger logger = LogManager.GetCurrentClassLogger(); private IMessageBus messageBus; @@ -24,6 +26,9 @@ namespace Maestro.Sources { private List cache = new List(); private BackgroundWorker pathWatcherWorker = new BackgroundWorker(); private string path; + private bool initialized; + private ClearAndInitialize clearAndInitialize; + private Action errorReporter; public DataGridViewColumn[] Columns { get { @@ -66,28 +71,46 @@ namespace Maestro.Sources { public bool SupportsFiltering => true; - public FileSystemSource(Control parent, IMessageBus messageBus, String fileExtensionFilter) { + public FileSystemSource(Control parent, IMessageBus messageBus, String fileExtensionFilter, Action errorReporter) { this.parent = parent; this.messageBus = messageBus; this.fileExtensionFilter = fileExtensionFilter; SetAcceptableExtensions(); pathWatcherWorker.DoWork += pathWatcherWorker_watchPath; - pathWatcherWorker.ProgressChanged += pathWatcherWorker_progressChanged; pathWatcherWorker.WorkerReportsProgress = true; - } - - private void pathWatcherWorker_progressChanged(object sender, ProgressChangedEventArgs e) { - InnerStartUp(); + this.errorReporter = errorReporter; } private void pathWatcherWorker_watchPath(object sender, DoWorkEventArgs e) { - while (!(Directory.Exists(path))) { - + while (true) { + if (!(Directory.Exists(path))) { + if (initialized) { + WatchedDirDeleted(); + } + } else { + if (!initialized) + InnerStartUp(); + } } - pathWatcherWorker.ReportProgress(0); } + private void WatchedDirDeleted() { + //errorReporter?.Invoke(DateTime.Now, String.Format("[FileSystemSource] {0} path can't be found.", path), Color.Red); + clearAndInitialize = new ClearAndInitialize(ClearDataGridView); + clearAndInitialize?.Invoke(); + } + + private void ClearDataGridView() { + this.parent.SafeCall(() => { + Clear(); + } + ); + cache.Clear(); + initialized = false; + //todo errorreport küldése + } + private void SetAcceptableExtensions() { if (fileExtensionFilter != null) acceptableExtensions = fileExtensionFilter.Split(',')?.ToList(); @@ -96,14 +119,16 @@ namespace Maestro.Sources { public void Startup(Uri address) { string path = address.LocalPath; this.path = path; - if (Directory.Exists(path)) { - InnerStartUp(); - } else pathWatcherWorker.RunWorkerAsync(); + clearAndInitialize = ClearDataGridView; + //if (Directory.Exists(path)) + // InnerStartUp(); + pathWatcherWorker.RunWorkerAsync(); } private void InnerStartUp() { createWatch(path); Task.Run(() => initializeList(path)); + initialized = true; } @@ -137,15 +162,15 @@ namespace Maestro.Sources { watcher.Created += new FileSystemEventHandler(OnCreated); watcher.Deleted += new FileSystemEventHandler(OnDeleted); watcher.Renamed += new RenamedEventHandler(OnRenamed); - //watcher.Disposed += new EventHandler(OnDispose); + watcher.Error += new ErrorEventHandler(OnError); watcher.EnableRaisingEvents = true; } - public void OnDispose(object sender, RenamedEventArgs e) { - + public void OnError(object sender, ErrorEventArgs e) { + logger.Debug(e.GetException().Message); } - public void OnRenamed(object sender, RenamedEventArgs e) { + public void OnRenamed(object sender, RenamedEventArgs e) { logger.Debug("{0} {1} {2}", e.ChangeType, e.OldName, e.Name); var item = this.Where(x => x.Name == e.OldName).SingleOrDefault(); if (item == null) -- 2.54.0