// sort torrents list based on clicked column private void SortTorrentsListView(object sender, ColumnClickEventArgs e) { if (e.Column == lvwColumnSorter.SortColumn) { lvwColumnSorter.Order = lvwColumnSorter.Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending; } else { lvwColumnSorter.SortColumn = e.Column; lvwColumnSorter.Order = SortOrder.Ascending; } TorrentsListView.BeginUpdate(); TorrentsListView.Sort(); TorrentsListView.EndUpdate(); }
// handle user selected a torrent from the torrent list private async void SelectedTorrentChanged(object sender, ListViewItemSelectionChangedEventArgs e) { DialogResult dialogResult; bool doUpdateFileList = false; if (TorrentsListView.SelectedItems.Count > 0) { if (Globals.SelectedTorrent != null) { // retrieve current torrent selection, new selection id's int currentTorrentId = Globals.SelectedTorrent.Torrent.ID; TorrentInfo newSelectedTorrent = (TorrentInfo)e.Item.Tag; int newTorrentId = newSelectedTorrent.ID; if (currentTorrentId != newTorrentId && TorrentFileListTreeView.TotalFilesSelected != 0) { dialogResult = MessageBox.Show("Changing the selected torrent will cause the current file selection to be lost.\r\n\r\nDo you want to continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { Globals.SelectedTorrent = Globals.TorrentsInfo.Find(torrent => torrent.Torrent.ID == newTorrentId); doUpdateFileList = true; } else { return; } } else if (TorrentFileListTreeView.TotalFilesSelected == 0) { Globals.SelectedTorrent = Globals.TorrentsInfo[TorrentsListView.SelectedItems[0].Index]; doUpdateFileList = true; } } else { TorrentInfo newSelectedTorrent = (TorrentInfo)e.Item.Tag; int newTorrentId = newSelectedTorrent.ID; Globals.SelectedTorrent = Globals.TorrentsInfo.Find(torrent => torrent.Torrent.ID == newTorrentId); doUpdateFileList = true; } } TorrentsListView.BeginUpdate(); if (doUpdateFileList) { // update UI state, load the file list of newly selected torrent SelectedTorrentLabel.Text = $"Selected torrent: {TorrentsListView.SelectedItems[0].SubItems[1].Text}"; RenameButton.Enabled = false; currentTorrentListViewItem = TorrentsListView.SelectedItems[0]; await LoadTorrentFilesList(); } // make current torrent selection easier to find in TorrentsListView foreach (ListViewItem torrentItem in TorrentsListView.Items) { if (torrentItem != null) { torrentItem.ForeColor = (torrentItem != currentTorrentListViewItem) ? Color.Black : Color.Blue; } } TorrentsListView.EndUpdate(); }
// load list of torrents from active session private async Task RefreshTorrentList() { DialogResult dialogResult; bool doContinue = true; // warn user current selection will be lost if (TorrentFileListTreeView.TotalFilesSelected != 0) { dialogResult = MessageBox.Show("Refreshing the torrent list will cause the current file selection to be lost.\r\n\r\nDo you want to continue?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.No) { doContinue = false; } } if (doContinue) { // update UI state on continue PagesTabControl.SelectedIndex = 0; SearchTorrentListTextBox.Clear(); TorrentFileListTreeView.TotalFilesSelected = 0; TorrentFileListTreeView.TotalFiles = 0; SelectedTorrentLabel.Text = "Selected torrent: None selected"; SelectedFileCountLabel.Text = "Selected files: 0 (total file count not yet known)"; TorrentTabPage.Text = "Torrents"; FilesTabPage.Text = "Files"; TorrentFileListTreeView.Nodes.Clear(); TorrentsListView.Items.Clear(); FileNamesOldNewListView.Items.Clear(); Globals.TorrentsInfo.Clear(); Globals.SelectedTorrent = null; Globals.SelectedTorrentFiles.Clear(); RenameButton.Enabled = false; RefreshTorrentListButton.Enabled = false; RefreshTorrentListButton.Text = "Waiting (10)"; TimeOutTimer.Start(); ToggleLoadingPanels(true); // retrieve torrents list List <TorrentInfo> torrentsInfo = await Task.Run(() => Globals.SessionHandler.GetTorrents()); TimeOutTimer.Stop(); TimeOutTimer.Tag = Properties.Settings.Default.MaxRequestDuration; // handle list response if (torrentsInfo != null) { if (torrentsInfo.Count == 0) { MessageBox.Show("The host returned 0 torrents.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { List <FriendlyTorrentInfo> globalTorrentsInfo = new List <FriendlyTorrentInfo>(); List <ListViewItem> torrentLVItems = new List <ListViewItem>(); TorrentsListView.BeginUpdate(); foreach (TorrentInfo torrent in torrentsInfo) { FriendlyTorrentInfo fTorrentInfo = new FriendlyTorrentInfo(torrent); globalTorrentsInfo.Add(fTorrentInfo); var torrentLVItem = new ListViewItem(new[] { fTorrentInfo.QueuePosition, fTorrentInfo.Name, fTorrentInfo.Status, fTorrentInfo.Size, fTorrentInfo.Progress }) { Tag = fTorrentInfo.Torrent }; torrentLVItems.Add(torrentLVItem); } TorrentsListView.Items.AddRange(torrentLVItems.ToArray()); TorrentsListView.EndUpdate(); Globals.TorrentsInfo = globalTorrentsInfo; } } else { MessageBox.Show("The torrent list could not be retrieved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error); } allItems.Clear(); allItems.AddRange(TorrentsListView.Items.Cast <ListViewItem>()); ToggleLoadingPanels(false); RefreshTorrentListButton.Text = "Refresh Torrent List"; TorrentTabPage.Text = $"Torrents ({Globals.TorrentsInfo.Count})"; RefreshTorrentListButton.Enabled = true; SearchTorrentListTextBox.Focus(); } }