public void SetupWatchers(string [] stdfolders)
        {
            System.Diagnostics.Debug.Assert(ScanThread == null);        // double check we are not scanning.

            List <int> watchersinuse = new List <int>();

            foreach (string std in stdfolders)          // setup the std folders
            {
                watchersinuse.Add(CheckAddPath(std));
            }

            foreach (var cmdr in EDCommander.GetListCommanders())         // setup any commander folders
            {
                if (!cmdr.ConsoleCommander && cmdr.JournalDir.HasChars()) // not console commanders, and we have a path
                {
                    watchersinuse.Add(CheckAddPath(cmdr.JournalDir));     // try adding
                }
            }

            List <int> del = new List <int>();

            for (int i = 0; i < watchers.Count; i++)            // find any watchers not in the watchersinuse list
            {
                if (!watchersinuse.Contains(i))
                {
                    del.Add(i);
                }
            }

            for (int j = 0; j < del.Count; j++)
            {
                int wi = del[j];
                System.Diagnostics.Trace.WriteLine(string.Format("Delete watch on {0}", watchers[wi].WatcherFolder));
                JournalMonitorWatcher mw = watchers[wi];
                mw.StopMonitor();          // just in case
                watchers.Remove(mw);
                StatusMonitorWatcher sw = statuswatchers[wi];
                sw.StopMonitor();          // just in case
                statuswatchers.Remove(sw);
            }
        }
        // call to update/create watchers on joutnal and UI.  Do it with system stopped

        public int CheckAddPath(string p)           // return index of watcher (and therefore status watcher as we add in parallel)
        {
            try
            {
                string path = Path.GetFullPath(p); // make path normalised. this can throw, so we need to protect and reject the path

                if (Directory.Exists(path))        // sanity check in case folder disappeared
                {
                    int present = watchers.FindIndex(x => x.WatcherFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase));

                    if (present < 0)  // and we are not watching it, add it
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", path));
                        JournalMonitorWatcher mw = new JournalMonitorWatcher(path);
                        watchers.Add(mw);

                        StatusMonitorWatcher sw = new StatusMonitorWatcher(path, ScanTick);
                        sw.UIEventCallBack += UIEvent;
                        statuswatchers.Add(sw);

                        present = watchers.Count - 1;
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Existing watch on {0}", path));
                    }

                    return(present);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Watcher path exception" + ex);
            }

            return(-1);
        }
        // check path exists, and if not already in watchers folder, add it
        // return -1 if not good, else return index of watcher (and therefore status watcher as we add in parallel)
        private int CheckAddPath(string p, string journalmatchpattern, DateTime mindateutc)
        {
            try
            {
                string path = Path.GetFullPath(p); // make path normalised. this can throw, so we need to protect and reject the path

                if (Directory.Exists(path))        // sanity check in case folder disappeared
                {
                    int present = watchers.FindIndex(x => x.WatcherFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase));

                    if (present < 0)  // and we are not watching it, add it
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", path));
                        JournalMonitorWatcher mw = new JournalMonitorWatcher(path, journalmatchpattern, mindateutc);
                        watchers.Add(mw);

                        StatusReader sw = new StatusReader(path);
                        statuswatchers.Add(sw);

                        present = watchers.Count - 1;
                    }
                    else
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Existing watch on {0}", path));
                    }

                    return(present);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Watcher path exception" + ex);
            }

            return(-1);
        }
        public void SetupWatchers(bool storejsoninje = false)
        {
            List <EDCommander> listCommanders = EDCommander.GetListCommanders();

            // add the default frontier folder in

            if (!string.IsNullOrEmpty(frontierfolder) && Directory.Exists(frontierfolder)) // if it exists..
            {
                if (watchers.FindIndex(x => x.WatcherFolder.Equals(frontierfolder)) < 0)   // and we are not watching it..
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", frontierfolder));
                    JournalMonitorWatcher mw = new JournalMonitorWatcher(frontierfolder, storejsoninje);
                    watchers.Add(mw);

                    StatusMonitorWatcher sw = new StatusMonitorWatcher(frontierfolder, ScanTick);
                    sw.UIEventCallBack += UIEvent;
                    statuswatchers.Add(sw);
                }
            }

            for (int i = 0; i < listCommanders.Count; i++)             // see if new watchers are needed
            {
                string datapath = GetWatchFolder(listCommanders[i].JournalDir);

                if (string.IsNullOrEmpty(datapath) || !Directory.Exists(datapath))  // not exist, ignore
                {
                    continue;
                }

                if (watchers.FindIndex(x => x.WatcherFolder.Equals(datapath)) >= 0) // if we already have a watch on this folder..
                {
                    continue;                                                       // already done
                }
                System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", datapath));
                JournalMonitorWatcher mw = new JournalMonitorWatcher(datapath, storejsoninje);
                watchers.Add(mw);

                StatusMonitorWatcher sw = new StatusMonitorWatcher(datapath, ScanTick);
                sw.UIEventCallBack += UIEvent;
                statuswatchers.Add(sw);
            }

            // clean up monitors on journals
            {
                List <int> tobedeleted = new List <int>();
                for (int i = 0; i < watchers.Count; i++)
                {
                    bool found = false;

                    if (frontierfolder != null && watchers[i].WatcherFolder.Equals(frontierfolder))
                    {
                        found = true;
                    }

                    for (int j = 0; j < listCommanders.Count; j++)          // all commanders, see if this watch folder is present
                    {
                        found |= watchers[i].WatcherFolder.Equals(GetWatchFolder(listCommanders[j].JournalDir));
                    }

                    if (!found)
                    {
                        tobedeleted.Add(i);
                    }
                }

                foreach (int i in tobedeleted)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Delete watch on {0}", watchers[i].WatcherFolder));
                    JournalMonitorWatcher mw = watchers[i];
                    mw.StopMonitor();          // just in case
                    watchers.Remove(mw);
                }
            }

            // and on status files
            {
                List <int> statustobedeleted = new List <int>();
                for (int i = 0; i < statuswatchers.Count; i++)
                {
                    bool found = false;

                    if (frontierfolder != null && watchers[i].WatcherFolder.Equals(frontierfolder))
                    {
                        found = true;
                    }

                    for (int j = 0; j < listCommanders.Count; j++)          // all commanders, see if this watch folder is present
                    {
                        found |= statuswatchers[i].WatcherFolder.Equals(GetWatchFolder(listCommanders[j].JournalDir));
                    }

                    if (!found)
                    {
                        statustobedeleted.Add(i);
                    }
                }

                foreach (int i in statustobedeleted)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Delete status watch on {0}", statuswatchers[i].WatcherFolder));
                    StatusMonitorWatcher mw = statuswatchers[i];
                    mw.StopMonitor();          // just in case
                    statuswatchers.Remove(mw);
                }
            }
        }
        public void ParseJournalFiles(Func <bool> cancelRequested, Action <int, string> updateProgress, bool forceReload = false)
        {
            List <EDCommander> listCommanders = EDCommander.GetListCommanders();

            // add the default frontier folder in

            if (!string.IsNullOrEmpty(frontierfolder) && Directory.Exists(frontierfolder)) // if it exists..
            {
                if (watchers.FindIndex(x => x.WatcherFolder.Equals(frontierfolder)) < 0)   // and we are not watching it..
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", frontierfolder));
                    JournalMonitorWatcher mw = new JournalMonitorWatcher(frontierfolder);
                    watchers.Add(mw);

                    StatusMonitorWatcher sw = new StatusMonitorWatcher(frontierfolder, ScanTick);
                    sw.UIEventCallBack += UIEvent;
                    statuswatchers.Add(sw);
                }
            }

            for (int i = 0; i < listCommanders.Count; i++)             // see if new watchers are needed
            {
                string datapath = GetWatchFolder(listCommanders[i].JournalDir);

                if (string.IsNullOrEmpty(datapath) || !Directory.Exists(datapath))  // not exist, ignore
                {
                    continue;
                }

                if (watchers.FindIndex(x => x.WatcherFolder.Equals(datapath)) >= 0) // if we already have a watch on this folder..
                {
                    continue;                                                       // already done
                }
                System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", datapath));
                JournalMonitorWatcher mw = new JournalMonitorWatcher(datapath);
                watchers.Add(mw);

                StatusMonitorWatcher sw = new StatusMonitorWatcher(datapath, ScanTick);
                sw.UIEventCallBack += UIEvent;
                statuswatchers.Add(sw);
            }

            // clean up monitors on journals
            {
                List <int> tobedeleted = new List <int>();
                for (int i = 0; i < watchers.Count; i++)
                {
                    bool found = false;
                    for (int j = 0; j < listCommanders.Count; j++)          // all commanders, see if this watch folder is present
                    {
                        found |= watchers[i].WatcherFolder.Equals(GetWatchFolder(listCommanders[j].JournalDir));
                    }

                    if (!found)
                    {
                        tobedeleted.Add(i);
                    }
                }

                foreach (int i in tobedeleted)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Delete watch on {0}", watchers[i].WatcherFolder));
                    JournalMonitorWatcher mw = watchers[i];
                    mw.StopMonitor();          // just in case
                    watchers.Remove(mw);
                }
            }

            // and on status files
            {
                List <int> statustobedeleted = new List <int>();
                for (int i = 0; i < statuswatchers.Count; i++)
                {
                    bool found = false;
                    for (int j = 0; j < listCommanders.Count; j++)          // all commanders, see if this watch folder is present
                    {
                        found |= statuswatchers[i].WatcherFolder.Equals(GetWatchFolder(listCommanders[j].JournalDir));
                    }

                    if (!found)
                    {
                        statustobedeleted.Add(i);
                    }
                }

                foreach (int i in statustobedeleted)
                {
                    System.Diagnostics.Trace.WriteLine(string.Format("Delete status watch on {0}", statuswatchers[i].WatcherFolder));
                    StatusMonitorWatcher mw = statuswatchers[i];
                    mw.StopMonitor();          // just in case
                    statuswatchers.Remove(mw);
                }
            }

            for (int i = 0; i < watchers.Count; i++)                                         // parse files of all folders being watched
            {
                watchers[i].ParseJournalFiles(cancelRequested, updateProgress, forceReload); // may create new commanders at the end, but won't need any new watchers, because they will obv be in the same folder
            }
        }