public string DoRename(FriendlyTorrentFileInfo torrentFileInfo, int itemIndex = 0)
        {
            try
            {
                StringBuilder newNameSb;
                string        extension = Path.GetExtension(torrentFileInfo.NewestName);
                if (IgnoreExtension)
                {
                    newNameSb = new StringBuilder(Path.GetFileNameWithoutExtension(torrentFileInfo.NewestName));
                }
                else
                {
                    newNameSb = new StringBuilder(Path.GetFileName(torrentFileInfo.NewestName));
                }

                string result = Regex.Unescape(Regex.Replace(newNameSb.ToString(), RegexFindText, ReplaceText));
                newNameSb = new StringBuilder(result);

                if (IgnoreExtension)
                {
                    newNameSb.Append(extension);
                }

                return(newNameSb.ToString());
            }
            catch { return(Path.GetFileName(torrentFileInfo.NewestName)); }
        }
 // update all the new file name previews in FileNamesOldNewListView
 private void UpdateFileRenameListView()
 {
     ResetOldNewFileNameValues();
     if (FileNamesOldNewListView.Items.Count > 0)
     {
         FileNamesOldNewListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
     }
     if (Globals.RenameRules.Count > 0)
     {
         ToggleLoadingPanels(true);
         FileNamesOldNewListView.BeginUpdate();
         foreach (IRenameRule renameRule in Globals.RenameRules)
         {
             if (renameRule.Enabled)
             {
                 foreach (ListViewItem torrentFileItem in FileNamesOldNewListView.Items)
                 {
                     FriendlyTorrentFileInfo torrentFileInfo = (FriendlyTorrentFileInfo)torrentFileItem.Tag;
                     torrentFileInfo.NewestName       = renameRule.DoRename(torrentFileInfo, torrentFileItem.Index);
                     torrentFileItem.SubItems[1].Text = torrentFileInfo.NewestName;
                     if (torrentFileItem.Text != torrentFileInfo.NewestName)
                     {
                         torrentFileItem.BackColor = Color.FromArgb(235, 235, 255);
                     }
                     else
                     {
                         torrentFileItem.BackColor = Color.FromArgb(255, 255, 255);
                     }
                 }
             }
         }
         FileNamesOldNewListView.EndUpdate();
         ToggleLoadingPanels(false);
     }
 }
Exemple #3
0
        public string DoRename(FriendlyTorrentFileInfo torrentFileInfo, int itemIndex = 0)
        {
            try
            {
                StringBuilder newNameSb;
                string        extension = Path.GetExtension(torrentFileInfo.NewestName);
                if (IgnoreExtension)
                {
                    newNameSb = new StringBuilder(Path.GetFileNameWithoutExtension(torrentFileInfo.NewestName));
                }
                else
                {
                    newNameSb = new StringBuilder(Path.GetFileName(torrentFileInfo.NewestName));
                }

                string oldNameStr = newNameSb.ToString();

                string insertNumber = (itemIndex + NumberSequenceStart).ToString($"D{NumberSequenceLeadingZeroes + 1}");

                if (Prefix)
                {
                    newNameSb.Insert(0, NumberSequence ? insertNumber : InsertText);
                }
                else if (Suffix)
                {
                    newNameSb.Append(NumberSequence ? insertNumber : InsertText);
                }
                else if (Position && PositionIndex != -1)
                {
                    if (PositionRightToLeft)
                    {
                        newNameSb.Insert(newNameSb.Length - PositionIndex, NumberSequence ? insertNumber : InsertText);
                    }
                    else
                    {
                        newNameSb.Insert(PositionIndex, NumberSequence ? insertNumber : InsertText);
                    }
                }
                else if (BeforeText && !string.IsNullOrEmpty(BeforeTextStr))
                {
                    newNameSb.Insert(oldNameStr.IndexOf(BeforeTextStr), NumberSequence ? insertNumber : InsertText);
                }
                else if (AfterText && !string.IsNullOrEmpty(AfterTextStr))
                {
                    newNameSb.Insert(oldNameStr.IndexOf(AfterTextStr) + AfterTextStr.Length, NumberSequence ? insertNumber : InsertText);
                }
                else if (ReplaceFileName)
                {
                    newNameSb.Clear().Append(NumberSequence ? insertNumber : InsertText);
                }

                if (IgnoreExtension)
                {
                    newNameSb.Append(extension);
                }

                return(newNameSb.ToString());
            }
            catch { return(Path.GetFileName(torrentFileInfo.NewestName)); }
        }
        // load list of files of the selected torrent
        private async Task LoadTorrentFilesList()
        {
            ToggleLoadingPanels(true);
            Globals.SelectedTorrentFiles.Clear();
            TorrentFileListTreeView.Nodes.Clear();

            // prepare torrent file paths for TorrentFileListTreeView display
            List <string> torrentPaths = new List <string>();

            foreach (TransmissionTorrentFiles torrentFile in Globals.SelectedTorrent.Torrent.Files)
            {
                FriendlyTorrentFileInfo friendlyTorrentFileInfo = new FriendlyTorrentFileInfo(torrentFile, Globals.SelectedTorrent.Torrent);
                torrentPaths.Add(item: friendlyTorrentFileInfo.InitialPath);
            }

            // populate TorrentFileListTreeView
            foreach (TreeNode node in GenerateTorrentFilesTreeViewItems(torrentPaths).Nodes)
            {
                node.Checked = true;
                TorrentFileListTreeView.Nodes.Add(node);
            }

            // update UI state
            TorrentFileListTreeView.BeginUpdate();
            rootNode         = TorrentFileListTreeView.GetNodeAt(0, 0);
            rootNode.Checked = true;
            rootNode.Expand();
            TorrentFileListTreeView.EndUpdate();
            await TorrentFileListTreeView.UpdateCounters();

            ToggleLoadingPanels(false);
        }
Exemple #5
0
        public string DoRename(FriendlyTorrentFileInfo torrentFileInfo, int itemIndex = 0)
        {
            try
            {
                StringBuilder newNameSb;
                string        extension = Path.GetExtension(torrentFileInfo.NewestName);
                if (IgnoreExtension)
                {
                    newNameSb = new StringBuilder(Path.GetFileNameWithoutExtension(torrentFileInfo.NewestName));
                }
                else
                {
                    newNameSb = new StringBuilder(Path.GetFileName(torrentFileInfo.NewestName));
                }

                if (DeleteEntireFileName)
                {
                    newNameSb.Clear();
                    if (IgnoreExtension)
                    {
                        newNameSb.Append(extension);
                    }

                    return(newNameSb.ToString());
                }

                int fromDelimiterPos = 0;
                int toDelimiterPos   = 0;
                if (FromDelimiter)
                {
                    fromDelimiterPos = newNameSb.ToString().IndexOf(FromDelimiterStr);
                }
                if (ToDelimiter)
                {
                    toDelimiterPos = newNameSb.ToString().IndexOf(ToDelimiterStr);
                }

                int removeStartIndex = FromPosition ? FromPositionIndex : fromDelimiterPos + (!KeepDelimiters ? 0 : FromDelimiterStr.Length);
                int removeLength     = ToPosition ? (ToPositionIndex - removeStartIndex) : (toDelimiterPos + ToDelimiterStr.Length) - removeStartIndex - (!KeepDelimiters ? 0 : ToDelimiterStr.Length);

                if (DeleteToEnd)
                {
                    removeLength = newNameSb.Length - removeStartIndex;
                }

                newNameSb.Remove(removeStartIndex, removeLength);

                if (IgnoreExtension)
                {
                    newNameSb.Append(extension);
                }

                return(newNameSb.ToString());
            }
            catch { return(Path.GetFileName(torrentFileInfo.NewestName)); }
        }
 private void ResetOldNewFileNameValues()
 {
     FileNamesOldNewListView.BeginUpdate();
     foreach (ListViewItem torrentFileItem in FileNamesOldNewListView.Items)
     {
         FriendlyTorrentFileInfo torrentFileInfo = (FriendlyTorrentFileInfo)torrentFileItem.Tag;
         torrentFileInfo.NewestName       = torrentFileInfo.InitialPath;
         torrentFileItem.SubItems[1].Text = torrentFileItem.Text;
         torrentFileItem.Tag       = torrentFileInfo;
         torrentFileItem.BackColor = Color.FromArgb(255, 255, 255);
     }
     FileNamesOldNewListView.EndUpdate();
 }
        // generate TorrentFileListTreeView file, directory nodes
        // https://stackoverflow.com/a/24861947 for reference
        public TreeNode GenerateTorrentFilesTreeViewItems(List <string> paths)
        {
            var rootNode = new TreeNode();

            foreach (var path in paths.Where(x => !string.IsNullOrEmpty(x.Trim())))
            {
                var  currentNode = rootNode;
                var  pathItems   = path.Split('/');
                bool isFile      = false;
                foreach (var item in pathItems)
                {
                    if (pathItems.ToList().IndexOf(item) == pathItems.Length - 1)
                    {
                        isFile = true;
                    }
                    var tmp = currentNode.Nodes.Cast <TreeNode>().Where(x => x.Text.Equals(item));
                    if (tmp.Count() > 0)
                    {
                        currentNode = tmp.Single();
                    }
                    else
                    {
                        TreeNode treeNode = new TreeNode(item);
                        // determine node type
                        if (isFile)
                        {
                            FriendlyTorrentFileInfo torrentInfo = new FriendlyTorrentFileInfo(Globals.SelectedTorrent.Torrent.Files[paths.IndexOf(path)], Globals.SelectedTorrent.Torrent);
                            treeNode.ImageIndex         = 0;
                            treeNode.SelectedImageIndex = 0;
                            treeNode.Tag         = torrentInfo;
                            treeNode.ToolTipText = torrentInfo.InitialPath;
                        }
                        else
                        {
                            treeNode.ImageIndex         = 1;
                            treeNode.SelectedImageIndex = 1;
                            treeNode.Tag = "Folder";
                        }
                        currentNode.Nodes.Add(treeNode);
                        currentNode = treeNode;
                    }
                }
            }
            return(rootNode);
        }
        public string DoRename(FriendlyTorrentFileInfo torrentFileInfo, int itemIndex = 0)
        {
            try
            {
                StringBuilder newNameSb;
                string        extension = Path.GetExtension(torrentFileInfo.NewestName);
                if (IgnoreExtension)
                {
                    newNameSb = new StringBuilder(Path.GetFileNameWithoutExtension(torrentFileInfo.NewestName));
                }
                else
                {
                    newNameSb = new StringBuilder(Path.GetFileName(torrentFileInfo.NewestName));
                }

                string result;

                if (AllOccurrences)
                {
                    if (CaseSensitive)
                    {
                        newNameSb.Replace(RemoveText, "");
                    }
                    else
                    {
                        result    = Regex.Replace(newNameSb.ToString(), Regex.Escape(RemoveText), "", RegexOptions.IgnoreCase);
                        newNameSb = new StringBuilder(result);
                    }
                }
                else if (FirstOccurrence)
                {
                    int index;
                    if (CaseSensitive)
                    {
                        index = newNameSb.ToString().IndexOf(RemoveText, StringComparison.InvariantCulture);
                    }
                    else
                    {
                        index = newNameSb.ToString().IndexOf(RemoveText, StringComparison.InvariantCultureIgnoreCase);
                    }
                    if (index != -1)
                    {
                        newNameSb = (index < 0) ? newNameSb : newNameSb.Remove(index, RemoveText.Length);
                    }
                }
                else if (LastOccurrence)
                {
                    int index;
                    if (CaseSensitive)
                    {
                        index = newNameSb.ToString().LastIndexOf(RemoveText, StringComparison.InvariantCulture);
                    }
                    else
                    {
                        index = newNameSb.ToString().LastIndexOf(RemoveText, StringComparison.InvariantCultureIgnoreCase);
                    }
                    if (index != -1)
                    {
                        newNameSb = (index < 0) ? newNameSb : newNameSb.Remove(index, RemoveText.Length);
                    }
                }


                if (IgnoreExtension)
                {
                    newNameSb.Append(extension);
                }

                return(newNameSb.ToString());
            }
            catch { return(Path.GetFileName(torrentFileInfo.NewestName)); }
        }
Exemple #9
0
        private async Task RenameTorrentFiles()
        {
            int success = 0, timeout = 0, failed = 0, current = 1, total = FileNamesOldNewListView.Items.Count;

            Invoke((MethodInvoker) delegate
            {
                TotalFilesLabel.Text        = $"Total files: {FileNamesOldNewListView.Items.Count}";
                RenamingProgressBar.Maximum = total;
            });
            for (int i = 0; i < FileNamesOldNewListView.Items.Count; i++)
            {
                if (!isAbortedOrFinished)
                {
                    string                curFilePath = null, newFileName = null;
                    TorrentInfo           torrent      = null;
                    Globals.RequestResult renameResult = Globals.RequestResult.Unknown;
                    Invoke((MethodInvoker) delegate
                    {
                        ListViewItem fileItem = FileNamesOldNewListView.Items[i];
                        FileNamesOldNewListView.Items[i].EnsureVisible();
                        FriendlyTorrentFileInfo friendlyTorrentFileInfo = (FriendlyTorrentFileInfo)fileItem.Tag;
                        curFilePath = friendlyTorrentFileInfo.InitialPath;
                        newFileName = friendlyTorrentFileInfo.NewestName;
                        torrent     = friendlyTorrentFileInfo.ParentTorrent;
                        CurrentFileRenameLabel.Text = $"File {current} of {total}: {FileNamesOldNewListView.Items[i].Text}";
                        if (RenamingProgressBar.Value + 2 <= RenamingProgressBar.Maximum)
                        {
                            RenamingProgressBar.Value += 2;
                            RenamingProgressBar.Value--;
                        }
                        else
                        {
                            RenamingProgressBar.Maximum++;
                            RenamingProgressBar.Value += 2;
                            RenamingProgressBar.Value--;
                            RenamingProgressBar.Maximum--;
                        }
                    });
                    if (curFilePath != null && newFileName != null && torrent != null && (curFilePath != newFileName))
                    {
                        renameResult = await Globals.SessionHandler.RenameTorrent(curFilePath, newFileName, torrent);

                        switch (renameResult)
                        {
                        case Globals.RequestResult.Success:
                            success++;
                            Invoke((MethodInvoker) delegate
                            {
                                FileNamesOldNewListView.Items[i].ImageIndex = 3;
                                SuccessFilesLabel.Text = $"Success: {success}";
                            });
                            break;

                        case Globals.RequestResult.Timeout:
                            timeout++;
                            Invoke((MethodInvoker) delegate
                            {
                                FileNamesOldNewListView.Items[i].ImageIndex = 4;
                                TimedOutFilesLabel.Text = $"Timed out: {timeout}";
                            }); break;

                        case Globals.RequestResult.Error:
                            failed++;
                            Invoke((MethodInvoker) delegate
                            {
                                FileNamesOldNewListView.Items[i].ImageIndex = 5;
                                ErrorFilesLabel.Text = $"Error: {failed}";
                            }); break;

                        case Globals.RequestResult.Unknown:
                            Invoke((MethodInvoker) delegate
                            {
                                FileNamesOldNewListView.Items[i].ImageIndex = 5;
                            }); break;

                        default:
                            MessageBox.Show("An unknown error has occurred. The renaming process will be cancelled.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            RenamingProgressBar.SetState(1);
                            isAbortedOrFinished = true;
                            break;
                        }
                    }
                    else
                    {
                        success++;
                        Invoke((MethodInvoker) delegate
                        {
                            FileNamesOldNewListView.Items[i].ImageIndex = 3;
                            SuccessFilesLabel.Text = $"Success: {success}";
                        });
                    }
                    current++;
                }
                else
                {
                    break;
                }
            }
        }
Exemple #10
0
        public string DoRename(FriendlyTorrentFileInfo torrentFileInfo, int itemIndex = 0)
        {
            try
            {
                StringBuilder newNameSb;
                string        extension = Path.GetExtension(torrentFileInfo.NewestName);
                if (IgnoreExtension)
                {
                    newNameSb = new StringBuilder(Path.GetFileNameWithoutExtension(torrentFileInfo.NewestName));
                }
                else
                {
                    newNameSb = new StringBuilder(Path.GetFileName(torrentFileInfo.NewestName));
                }

                if (CleanLatinAlphabet)
                {
                    if (CaseSensitive)
                    {
                        for (int i = 0; i < newNameSb.Length - 1; i++)
                        {
                            if (LatinAlphabet.Contains(newNameSb[i]))
                            {
                                foreach (char c in LatinAlphabet)
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < newNameSb.Length - 1; i++)
                        {
                            if (LatinAlphabet.Contains(newNameSb[i].ToString().ToLower()))
                            {
                                foreach (char c in LatinAlphabet)
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                                foreach (char c in LatinAlphabetUpper)
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                            }
                        }
                    }
                }
                if (CleanDigits)
                {
                    for (int i = 0; i < newNameSb.Length - 1; i++)
                    {
                        if (Digits.Contains(newNameSb[i]))
                        {
                            foreach (char c in Digits)
                            {
                                newNameSb.Replace(c.ToString(), "");
                            }
                        }
                    }
                }
                if (CleanBrackets)
                {
                    for (int i = 0; i < newNameSb.Length - 1; i++)
                    {
                        if (Brackets.Contains(newNameSb[i]))
                        {
                            foreach (char c in Brackets)
                            {
                                newNameSb.Replace(c.ToString(), "");
                            }
                        }
                    }
                }
                if (CleanSymbols)
                {
                    for (int i = 0; i < newNameSb.Length - 1; i++)
                    {
                        if (Symbols.Contains(newNameSb[i]))
                        {
                            foreach (char c in Symbols)
                            {
                                newNameSb.Replace(c.ToString(), "");
                            }
                        }
                    }
                }
                if (CleanUserDefined)
                {
                    if (CaseSensitive)
                    {
                        for (int i = 0; i < newNameSb.Length - 1; i++)
                        {
                            if (CleanUserDefinedText.Contains(newNameSb[i]))
                            {
                                foreach (char c in CleanUserDefinedText)
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < newNameSb.Length - 1; i++)
                        {
                            if (CleanUserDefinedText.Contains(newNameSb[i].ToString().ToLower()))
                            {
                                foreach (char c in CleanUserDefinedText.ToLower())
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                                foreach (char c in CleanUserDefinedText.ToUpper())
                                {
                                    newNameSb.Replace(c.ToString(), "");
                                }
                            }
                        }
                    }
                }
                if (IgnoreExtension)
                {
                    newNameSb.Append(extension);
                }

                return(newNameSb.ToString());
            }
            catch { return(Path.GetFileName(torrentFileInfo.NewestName)); }
        }