Esempio n. 1
0
        /// <summary>
        /// Main Rename function
        /// </summary>
        public void Rename(BackgroundWorker worker, DoWorkEventArgs e)
        {
            this.worker = worker;
            this.dwea   = e;

            long TotalBytes = 0;
            long Bytes      = 0;

            foreach (InfoEntry ie in episodes)
            {
                if (ie.ProcessingRequested && ie.Destination != "" && ie.FilePath.Fullfilename.ToLower()[0] != ie.Destination.ToLower()[0])
                {
                    FileInfo info = new FileInfo(ie.FilePath.Fullfilename);
                    TotalBytes += info.Length;
                }
            }
            //Go through all files and do stuff
            for (int i = 0; i < this.episodes.Count; i++)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    Logger.Instance.LogMessage("Renaming Cancelled.", LogLevel.INFO);
                    return;
                }

                InfoEntry ie = InfoEntryManager.Instance[i];
                if (ie.MarkedForDeletion && ie.ProcessingRequested)
                {
                    try
                    {
                        File.Delete(ie.FilePath.Fullfilename);
                        episodes.Remove(ie);
                        //Go back so no entry is skipped after removal of current entry
                        i--;
                        continue;
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.LogMessage("Couldn't delete " + ie.FilePath.Fullfilename + ": " + ex.Message, LogLevel.ERROR);
                    }
                }

                //need to set before ie.Rename because it resets some conditions
                bool copyfile = ie.ProcessingRequested && ie.Destination != "" && ie.FilePath.Fullfilename.ToLower()[0] != ie.Destination.ToLower()[0];

                //if there are files which actually need to be copied since they're on a different drive, only count those for the progress bar status since they take much longer
                if (TotalBytes > 0)
                {
                    ProgressAtCopyStart = ((double)Bytes / (double)TotalBytes * 100);
                    if (copyfile)
                    {
                        ProgressAtCopyEnd = Math.Min(ProgressAtCopyStart + (long)(((double)new FileInfo(ie.FilePath.Fullfilename).Length) / (double)TotalBytes * 100), 100);
                    }
                    else
                    {
                        ProgressAtCopyEnd = ProgressAtCopyStart;
                    }
                }
                else
                {
                    ProgressAtCopyStart = i;
                    ProgressAtCopyEnd   = i;
                }

                worker.ReportProgress((int)ProgressAtCopyStart);

                //if a InfoEntry is moved to a different destination directory that isn't visible in the current basedir, remove it
                int  subdirlevel = Filepath.GetSubdirectoryLevel(Helper.ReadProperty(Config.LastDirectory), ie.Destination);
                bool remove      = subdirlevel > Helper.ReadInt(Config.MaxDepth) || subdirlevel == -1;
                //this call will also report progress more detailed by calling ReportSingleFileProgress()
                ie.Rename(worker, e);
                if (remove)
                {
                    episodes.Remove(ie);
                    //Go back so no entry is skipped after removal of current entry
                    i--;
                }
                //after file is (really) copied, add its size
                if (copyfile)
                {
                    Bytes += new FileInfo(ie.FilePath.Fullfilename).Length;
                }
            }
            if (Helper.ReadBool(Config.DeleteEmptyFolders))
            {
                //Delete all empty folders code
                Helper.DeleteAllEmptyFolders(Helper.ReadProperty(Config.LastDirectory), new List <string>(Helper.ReadProperties(Config.IgnoreFiles)), worker, e);
            }
            if (!e.Cancel)
            {
                Logger.Instance.LogMessage("Renaming (and possibly moving and deleting) finished!", LogLevel.INFO);
            }
            else
            {
                Logger.Instance.LogMessage("Renaming Cancelled.", LogLevel.INFO);
            }
        }
        /// <summary>
        /// Main Rename function
        /// </summary>
        public void Rename(BackgroundWorker someWorker, DoWorkEventArgs someRenameEventArgs)
        {
            this.currentWorker          = someWorker;
            this.currentRenameEventArgs = someRenameEventArgs;

            double copiedFileBytesSum = countCopiedFileBytes();

            double copiedBytes = 0;
            //Go through all files and do stuff
//            for (int i = 0; i < this.mediaFiles.Count; i++) {
            List <MediaFile> filesToCheck = new List <MediaFile>(mediaFiles);

            foreach (MediaFile mediaFile in filesToCheck)
            {
                if (renamingCancelled(someWorker, someRenameEventArgs))
                {
                    return;
                }

                if (fileWillBeDeleted(mediaFile))
                {
                    deleteMediaFile(mediaFile);
                }

                //need to set before ie.Rename because it resets some conditions
                bool willBeCopied = mediaFileChangesPartition(mediaFile);

                //if there are files which actually need to be copied since they're on a different drive, only count those for the progress bar status since they take much longer
                setProgress(copiedBytes, copiedFileBytesSum, mediaFile, willBeCopied);

                someWorker.ReportProgress((int)ProgressAtCopyStart);

                bool removeFromSet = notVisibleAfterRenaming(mediaFile);
                //this call will also report progress more detailed by calling ReportSingleFileProgress()
                mediaFile.Rename(someWorker, someRenameEventArgs);
                if (removeFromSet)
                {
                    mediaFiles.Remove(mediaFile);
                }
                //after file is (really) copied, add its size
                if (willBeCopied)
                {
                    copiedBytes += getFilesize(mediaFile);
                }
            }

            if (Helper.ReadBool(ConfigKeyConstants.DELETE_EMPTIED_FOLDERS_KEY))
            {
                //Delete all empty folders code
                Helper.DeleteAllEmptyFolders(Helper.ReadProperty(ConfigKeyConstants.LAST_DIRECTORY_KEY), new List <string>(Helper.ReadProperties(ConfigKeyConstants.EMPTY_FOLDER_CHECK_IGNORED_FILETYPES_KEY)), someWorker, someRenameEventArgs);
            }
            logEndOfRenaming(someRenameEventArgs.Cancel);
        }