Beispiel #1
0
        private static void GetHostEntryCallback(IAsyncResult ar)
        {
            PeerListViewItem item = (PeerListViewItem)ar.AsyncState;

            try
            {
                IPHostEntry host = Dns.EndGetHostEntry(ar);
                item.SetHostName(host);
            }
            catch
            {
            }
        }
        // lock torrentListView BEFORE calling this method
        public void UpdateInfoPanel(bool first, Torrent t)
        {
            if (first)
            {
                generalTorrentInfo.torrentName = peersTorrentNameGroupBox.Text
                    = trackersTorrentNameGroupBox.Text = filesTorrentNameGroupBox.Text
                    = t.TorrentName;
                generalTorrentInfo.startedAt = t.Added.ToString();
                generalTorrentInfo.createdAt = t.Created;
                generalTorrentInfo.createdBy = t.Creator;
                generalTorrentInfo.hash = string.Join(" ", Toolbox.Split(t.Hash.ToUpper(), 8));
                generalTorrentInfo.comment = t.Comment;
                trackersListView.BeginUpdate();
                trackersListView.Items.Clear();
                foreach (JsonObject tracker in t.Trackers)
                {
                    int tier = Toolbox.ToInt(tracker[ProtocolConstants.TIER]);
                    string announceUrl = (string)tracker[ProtocolConstants.ANNOUNCE];
                    ListViewItem item = new ListViewItem(tier.ToString());
                    item.SubItems.Add(announceUrl);
                    while (item.SubItems.Count < 7)
                        item.SubItems.Add("");
                    item.Name = Toolbox.ToInt(tracker[ProtocolConstants.FIELD_IDENTIFIER], -1).ToString();
                    trackersListView.Items.Add(item);
                }
                Toolbox.StripeListView(trackersListView);
                trackersListView.Enabled = true;
                trackersListView.EndUpdate();
                downloadProgressLabel.Text = ((piecesGraph.Visible = t.Pieces != null) ? OtherStrings.Pieces : OtherStrings.Progress) + ": ";
                progressBar.Visible = !piecesGraph.Visible;
            }
            if (t.TrackerStats != null)
            {
                trackersListView.BeginUpdate();
                foreach (JsonObject trackerstat in t.TrackerStats)
                {
                    int id = Toolbox.ToInt(trackerstat[ProtocolConstants.FIELD_IDENTIFIER], -1);
                    if (id >= 0 && trackersListView.Items.ContainsKey(id.ToString()))
                    {
                        ListViewItem item = trackersListView.Items[id.ToString()];
                        double nat = Toolbox.ToDouble(trackerstat["nextAnnounceTime"]);
                        int seederCount = Toolbox.ToInt(trackerstat["seederCount"]);
                        int leecherCount = Toolbox.ToInt(trackerstat["leecherCount"]);
                        int downloadCount = Toolbox.ToInt(trackerstat["downloadCount"]);
                        item.SubItems[2].Text = (string)trackerstat["lastAnnounceResult"];
                        if (nat > 0.0)
                        {
                            TimeSpan ts = Toolbox.DateFromEpoch(nat).ToLocalTime().Subtract(DateTime.Now);
                            item.SubItems[3].Text = ts.Ticks > 0 ? Toolbox.FormatTimespanLong(ts) : OtherStrings.UnknownNegativeResult;
                        }
                        else
                        {
                            item.SubItems[3].Text = "";
                        }

                        item.SubItems[4].Text = seederCount >= 0 ? seederCount.ToString() : "";
                        item.SubItems[5].Text = leecherCount >= 0 ? leecherCount.ToString() : "";
                        item.SubItems[6].Text = downloadCount >= 0 ? downloadCount.ToString() : "";
                    }
                }
                trackersListView.EndUpdate();
            }
            generalTorrentInfo.remaining = t.IsFinished ? (t.DoneDate != null ? t.DoneDate.ToString() : "?") : (t.Eta > 0 ? t.LongEta : "");
            generalTorrentInfo.timeLabelText = (t.IsFinished ? torrentCompletedAtCol.Text : torrentEtaCol.Text) + ":";
            generalTorrentInfo.uploaded = t.UploadedString;
            generalTorrentInfo.uploadLimit = t.SpeedLimitUpEnabled ? Toolbox.KbpsString(t.SpeedLimitUp) : "∞";
            generalTorrentInfo.uploadSpeed = t.SecondsDownloading >= 0 && t.SecondsSeeding >= 0 ? string.Format(OtherStrings.SpeedWithAvg, t.UploadRateString, t.UploadAvgRateString) : t.UploadRateString;
            generalTorrentInfo.seeders = string.Format(OtherStrings.XOfYConnected, t.PeersSendingToUs, t.Seeders < 0 ? "?" : t.Seeders.ToString());
            generalTorrentInfo.leechers = string.Format(OtherStrings.XOfYConnected, t.PeersGettingFromUs, t.Leechers < 0 ? "?" : t.Leechers.ToString());
            generalTorrentInfo.ratio = t.LocalRatioString;
            progressBar.Value = (int)t.Percentage;
            if (t.Pieces != null)
            {
                piecesGraph.ApplyBits(t.Pieces, t.PieceCount);
                generalTorrentInfo.piecesInfo = string.Format(OtherStrings.PiecesInfo, t.PieceCount, Toolbox.GetFileSize(t.PieceSize), t.HavePieces);
            }
            else
                generalTorrentInfo.piecesInfo = string.Format("{0} x {1}", t.PieceCount, Toolbox.GetFileSize(t.PieceSize));
            generalTorrentInfo.location = t.DownloadDir + "/" + t.TorrentName;
            percentageLabel.Text = t.Percentage.ToString() + "%";
            if (t.TotalSize == t.SizeWhenDone)
            {
                generalTorrentInfo.totalSize = string.Format(OtherStrings.TotalDoneValidSize, Toolbox.GetFileSize(t.SizeWhenDone), t.HaveTotalString, Toolbox.GetFileSize(t.HaveValid));
            }
            else
            {
                generalTorrentInfo.totalSize = string.Format(OtherStrings.TotalDoneValidTotalSize, Toolbox.GetFileSize(t.SizeWhenDone), t.HaveTotalString, Toolbox.GetFileSize(t.HaveValid), Toolbox.GetFileSize(t.TotalSize));
            }
            //totalSizeLabel.Text = String.Format(OtherStrings.DownloadedValid, t.HaveTotalString, Toolbox.GetFileSize(t.HaveValid));
            generalTorrentInfo.downloaded = Toolbox.GetFileSize(t.Downloaded);
            generalTorrentInfo.downloadSpeed = t.SecondsDownloading >= 0 ? string.Format(OtherStrings.SpeedWithAvg, t.DownloadRateString, t.DownloadAvgRateString) : t.DownloadRateString;
            generalTorrentInfo.downloadLimit = t.SpeedLimitDownEnabled ? Toolbox.KbpsString(t.SpeedLimitDown) : "∞";
            generalTorrentInfo.status = t.Status;
            generalTorrentInfo.errorVisible = !(generalTorrentInfo.error = t.ErrorString).Equals("");
            RefreshElapsedTimer();
            peersListView.Enabled = t.StatusCode != ProtocolConstants.STATUS_STOPPED;
            if (t.Peers != null && peersListView.Enabled)
            {
                PeerListViewItem.CurrentUpdateSerial++;
                lock (peersListView)
                {
                    peersListView.BeginUpdate();
                    IComparer tmp = peersListView.ListViewItemSorter;
                    peersListView.ListViewItemSorter = null;
                    foreach (JsonObject peer in t.Peers)
                    {
                        PeerListViewItem item = FindPeerItem((string)peer[ProtocolConstants.ADDRESS]);
                        if (item == null)
                        {
                            item = new PeerListViewItem(peer);
                            peersListView.Items.Add(item);
                        }
                        else
                        {
                            item.Update(peer);
                        }
                        item.UpdateSerial = PeerListViewItem.CurrentUpdateSerial;
                    }
                    PeerListViewItem[] peers = (PeerListViewItem[])new ArrayList(peersListView.Items).ToArray(typeof(PeerListViewItem));
                    foreach (PeerListViewItem item in peers)
                    {
                        if (item.UpdateSerial != PeerListViewItem.CurrentUpdateSerial)
                            peersListView.Items.Remove(item);
                    }
                    peersListView.ListViewItemSorter = tmp;
                    Toolbox.StripeListView(peersListView);
                    peersListView.EndUpdate();
                }
            }
        }