// Called when RecentlyCreated.tx changes (typically twice for some reason).
        // This method runs in a worker thread and may run in multiple threads concurrently.
        private static void _watcher_Changed(object sender, FileSystemEventArgs e)
        {
            using (Log.DebugCall())
            {
                try
                {
                    List <PathItem> files                = null;
                    List <PathItem> folders              = null;
                    bool            didRaiseFilesEvent   = false;
                    bool            didRaiseFoldersEvent = false;

                    // Try not to read the files at the same time they're being written by the logger, but don't wait forever.
                    using (new NamedMutexWait(NamedMutexWait.DataDirMUtexName, timeoutMs: 10000, throwOnTimeout: false))
                    {
                        var lines = MaybeReadFile(_foldersFile, ref _foldersTimestamp);
                        folders = PathItem.MakeRecentlyCreatedPathItems(lines, true);

                        lines = MaybeReadFile(_filesFile, ref _filesTimestamp);
                        files = PathItem.MakeRecentlyCreatedPathItems(lines, false);
                    }

                    if (files != null && folders != null)
                    {
                        // Old versions of the logger don't update the folder list, so there might be folders in the
                        // file list that aren't in the folder list.  Combine the folders from both lists.
                        var combinedFolders = new List <PathItem>();
                        var hashSet         = new HashSet <string>(StringComparer.OrdinalIgnoreCase); // for filtering duplicates.

                        foreach (PathItem file in files)
                        {
                            if (hashSet.Add(file.File.Directory.FullName))
                            {
                                combinedFolders.Add(new PathItem(file.File.Directory, true));
                            }
                        }

                        foreach (PathItem folder in folders)
                        {
                            if (hashSet.Add(folder.Folder.FullName))
                            {
                                combinedFolders.Add(folder);
                            }
                        }

                        if (!Folders.SequenceEqual(combinedFolders))
                        {
                            didRaiseFoldersEvent = true;
                            Folders = combinedFolders;
                        }
                    }

                    if (files != null && !Files.SequenceEqual(files))
                    {
                        didRaiseFilesEvent = true;
                        Files = files;
                    }

                    if (sender is bool && (bool)sender)
                    {
                        if (!didRaiseFilesEvent)
                        {
                            Files = Files;
                        }
                        if (!didRaiseFoldersEvent)
                        {
                            Folders = Folders;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
        }
Beispiel #2
0
        // Called when the user views the specified file so we can keep track of which files
        // have been viewed and when.
        public void UpdateViewedFiles(string viewedFile)
        {
            // First, add/update the file in both file collections (_savedViewedFiles and _dictFiles).

            DateTime   now           = DateTime.Now;
            ViewedPath viewedFileObj = _savedViewedFiles.Find(item => item.Path.Equals(viewedFile, StringComparison.OrdinalIgnoreCase));

            if (viewedFileObj == null)
            {
                viewedFileObj = new ViewedPath {
                    Path = viewedFile, ViewTime = now
                };
                _savedViewedFiles.Add(viewedFileObj);
            }
            else
            {
                viewedFileObj.ViewTime = now;
            }

            PathItem filePathItem;

            if (!_dictFiles.TryGetValue(viewedFile, out filePathItem))
            {
                // We assume the file exists because it was just loaded into the viewer.

                var txInfo = new TXFileInfo()
                {
                    IsFolder = false,
                    FullPath = viewedFile,
                };

                filePathItem           = new PathItem(txInfo);
                _dictFiles[viewedFile] = filePathItem;
            }

            filePathItem.ViewTime = now;

            // Now add/update the folder in both folder collections (_savedViewedFolders and _dictFolders).

            string     folder          = System.IO.Path.GetDirectoryName(viewedFile);
            ViewedPath viewedFolderObj = _savedViewedFolders.Find(item => item.Path.Equals(folder, StringComparison.OrdinalIgnoreCase));

            if (viewedFolderObj == null)
            {
                viewedFolderObj = new ViewedPath {
                    Path = folder, ViewTime = now
                };
                _savedViewedFolders.Add(viewedFolderObj);
            }
            else
            {
                viewedFolderObj.ViewTime = now;
            }

            PathItem folderPathItem;

            if (!_dictFolders.TryGetValue(folder, out folderPathItem))
            {
                var txInfo = new TXFileInfo()
                {
                    IsFolder = true,
                    FullPath = folder
                };

                folderPathItem       = new PathItem(txInfo);
                _dictFolders[folder] = folderPathItem;
            }

            folderPathItem.ViewTime = now;
        }