예제 #1
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;
                }
            }
        }
예제 #2
0
        private async void ConnectButtonClicked(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(HostTextBox.Text))
            {
                MessageBox.Show("The host value may not be empty.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            isConnecting           = true;
            RemoteGroupBox.Enabled = false;
            ConnectButton.Enabled  = false;
            ConnectButton.Text     = "Waiting (20)";
            CloseCancelButton.Text = "Cancel";
            TimeOutTimer.Enabled   = true;
            TimeOutTimer.Start();
            Globals.RequestResult connectionResult = Globals.RequestResult.Unknown;

            if (AuthenticationRequiredCheckBox.Checked)
            {
                Globals.SessionHandler = new SessionHandler(HostTextBox.Text, RPCPathTextBox.Text, (int)PortUpDown.Value, UsernameTextBox.Text, PasswordTextBox.Text);
            }
            else
            {
                Globals.SessionHandler = new SessionHandler(HostTextBox.Text, RPCPathTextBox.Text, (int)PortUpDown.Value, null, null);
            }

            await Task.Run(async() => { connectionResult = await Globals.SessionHandler.TestConnection(); });

            if (isConnecting)
            {
                bool revertUi = true;
                switch (connectionResult)
                {
                case Globals.RequestResult.Success:
                    SaveConfig();
                    Hide();
                    SettingsForm selectTorrentFilesForm = new SettingsForm
                    {
                        Text = $"Transmission Renamer - {Globals.SessionHandler.SessionUrl}"
                    };
                    selectTorrentFilesForm.ShowDialog();
                    Show();
                    break;

                case Globals.RequestResult.Timeout:
                    MessageBox.Show("The connection to the host has timed out.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case Globals.RequestResult.InvalidResponse:
                    MessageBox.Show("The host returned an invalid response.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case Globals.RequestResult.Error:
                    MessageBox.Show("The specified host or RPC path is invalid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case Globals.RequestResult.Unauthorized:
                    MessageBox.Show("The specified port or login credentials are invalid.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case Globals.RequestResult.Cancelled:
                    // show no message but keep UI controls and behavior intact
                    revertUi = false;
                    break;

                case Globals.RequestResult.Unknown:
                    MessageBox.Show("An unknown error has occurred.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                default:
                    MessageBox.Show("An unknown error has occurred.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
                if (revertUi)
                {
                    ConnectButton.Text     = "Connect";
                    CloseCancelButton.Text = "Close";
                    RemoteGroupBox.Enabled = true;
                    ConnectButton.Enabled  = true;
                    isConnecting           = false;
                    TimeOutTimer.Stop();
                    TimeOutTimer.Enabled = false;
                    TimeOutTimer.Tag     = Properties.Settings.Default.MaxRequestDuration;
                }
            }
        }