Beispiel #1
0
        private void FillPreview()
        {
            this.lvPreview.Items.Clear();
            if ((string.IsNullOrEmpty(this.txtFolder.Text)) || (!Directory.Exists(this.txtFolder.Text)))
            {
                this.txtFolder.BackColor = Helpers.WarningColor();
                return;
            }
            else
            {
                this.txtFolder.BackColor = System.Drawing.SystemColors.Window;
            }

            if (this.Grid1.RowsCount <= 1) // 1 for header
            {
                return;                    // empty
            }
            this.lvPreview.Enabled = true;

            List <FilenameProcessorRE> rel = new List <FilenameProcessorRE>();

            if (this.chkTestAll.Checked)
            {
                for (int i = 1; i < this.Grid1.RowsCount; i++)
                {
                    FilenameProcessorRE re = this.REForRow(i);
                    if (re != null)
                    {
                        rel.Add(re);
                    }
                }
            }
            else
            {
                int[] rowsIndex = this.Grid1.Selection.GetSelectionRegion().GetRowsIndex();
                if (rowsIndex.Length == 0)
                {
                    return;
                }

                FilenameProcessorRE re2 = this.REForRow(rowsIndex[0]);
                if (re2 != null)
                {
                    rel.Add(re2);
                }
                else
                {
                    return;
                }
            }

            this.lvPreview.BeginUpdate();
            DirectoryInfo d = new DirectoryInfo(this.txtFolder.Text);

            foreach (FileInfo fi in d.GetFiles())
            {
                int seas;
                int ep;

                if (!TVSettings.Instance.UsefulExtension(fi.Extension, true))
                {
                    continue; // move on
                }
                ShowItem     si  = this.cbShowList.SelectedIndex >= 0 ? this.SIL[this.cbShowList.SelectedIndex] : null;
                bool         r   = TVDoc.FindSeasEp(fi, out seas, out ep, si, rel, false);
                ListViewItem lvi = new ListViewItem();
                lvi.Text = fi.Name;
                lvi.SubItems.Add((seas == -1) ? "-" : seas.ToString());
                lvi.SubItems.Add((ep == -1) ? "-" : ep.ToString());
                if (!r)
                {
                    lvi.BackColor = Helpers.WarningColor();
                }
                this.lvPreview.Items.Add(lvi);
            }
            this.lvPreview.EndUpdate();
        }
Beispiel #2
0
        protected void DoTidyup(DirectoryInfo di)
        {
#if DEBUG
            Debug.Assert(this._tidyup != null);
            Debug.Assert(this._tidyup.DeleteEmpty);
#else
            if (_tidyup == null || !_tidyup.DeleteEmpty)
            {
                return;
            }
#endif
            // See if we should now delete the folder we just moved that file from.
            if (di == null)
            {
                return;
            }

            //if there are sub-directories then we shouldn't remove this one
            if (di.GetDirectories().Length > 0)
            {
                return;
            }

            //if the directory is the root download folder do not delete
            if (TVSettings.Instance.MonitorFolders && TVSettings.Instance.SearchFoldersNames.Contains(di.FullName))
            {
                return;
            }

            // Do not delete any monitor folders either
            if (TVSettings.Instance.MonitorFoldersNames.Contains(di.FullName))
            {
                return;
            }


            FileInfo[] files = di.GetFiles();
            if (files.Length == 0)
            {
                // its empty, so just delete it
                di.Delete();
                return;
            }


            if (_tidyup.EmptyIgnoreExtensions && !_tidyup.EmptyIgnoreWords)
            {
                return; // nope
            }
            foreach (FileInfo fi in files)
            {
                bool okToDelete = _tidyup.EmptyIgnoreExtensions &&
                                  Array.FindIndex(_tidyup.EmptyIgnoreExtensionsArray, x => x == fi.Extension) != -1;

                if (okToDelete)
                {
                    continue; // onto the next file
                }
                // look in the filename
                if (_tidyup.EmptyIgnoreWordsArray.Any(word => fi.Name.Contains(word)))
                {
                    okToDelete = true;
                }

                if (!okToDelete)
                {
                    return;
                }
            }

            if (_tidyup.EmptyMaxSizeCheck)
            {
                // how many MB are we deleting?
                long totalBytes = files.Sum(fi => fi.Length);

                if (totalBytes / (1024 * 1024) > _tidyup.EmptyMaxSizeMB)
                {
                    return; // too much
                }
            }
            DeleteOrRecycleFolder(di);
        }
        /// <summary>
        /// Internal method to recursively iterate the file system.
        /// It will fill out the results parameter with items as it finds them.
        /// </summary>
        /// <param name="baseDirectory">Base directory is used to make relative paths. It should always end with a '\' (slash) to generate a uniform and valid output.</param>
        /// <param name="currentDirectory">The directory to iterate.</param>
        /// <param name="results">The results LinkedList to fill out.</param>
        private static void DoFsSnapshot(DirectoryInfo baseDirectory, DirectoryInfo currentDirectory, LinkedList<SnapshotFilesystemItem> results)
        {
            // Self
            SnapshotDirectoryInfo current = new SnapshotDirectoryInfo();
            current.RelativePath = currentDirectory.FullName.Substring(baseDirectory.FullName.Length);

            try
            {
                current.LastAccess = currentDirectory.LastAccessTimeUtc;
                current.LastModified = currentDirectory.LastWriteTimeUtc;
                current.Attributes = (System.IO.FileAttributes)currentDirectory.Attributes;

                current.WasReadable = true;
            }
            catch (System.IO.IOException)
            {
                // Couldn't read details
                current.WasReadable = false;
            }

            // Subfiles
            try
            {
                FileInfo[] files = currentDirectory.GetFiles();

                foreach (FileInfo fileInfo in files)
                {
                    // Single file
                    SnapshotFileInfo file = new SnapshotFileInfo();
                    file.RelativePath = fileInfo.FullName.Substring(baseDirectory.FullName.Length);

                    try
                    {
                        file.LastAccess = fileInfo.LastAccessTimeUtc;
                        file.LastModified = fileInfo.LastWriteTimeUtc;
                        file.Attributes = (System.IO.FileAttributes)fileInfo.Attributes;

                        // Make hash
                        using (System.IO.FileStream fileStream = fileInfo.OpenRead())
                        {
                            byte[] hash = _md5.ComputeHash(fileStream);
                            file.Hash = BitConverter.ToString(hash).Replace("-", "");
                        }

                        file.WasReadable = true;
                    }
                    catch (Exception)
                    {
                        // Couldn't read details
                        file.WasReadable = false;
                    }

                    results.AddLast(file);
                }
            }
            catch (System.IO.IOException)
            {
                // Couldn't read files
                current.WasReadable = false;
            }
            catch (UnauthorizedAccessException)
            {
                // Couldn't read files
                current.WasReadable = false;
            }

            // Subdirs
            try
            {
                DirectoryInfo[] directories = currentDirectory.GetDirectories();

                foreach (DirectoryInfo directoryInfo in directories)
                {
                    DoFsSnapshot(baseDirectory, directoryInfo, results);
                }
            }
            catch (System.IO.IOException)
            {
                // Couldn't read directories
                current.WasReadable = false;
            }
            catch (UnauthorizedAccessException)
            {
                // Couldn't read files
                current.WasReadable = false;
            }

            results.AddLast(current);
        }