/// <summary> /// Initializes the specified torrent entity task. /// </summary> /// <param name="torrentEntityTask">The torrent entity task.</param> /// <param name="torrents">The torrents.</param> public async void Initialize(Task <TorrentEntity> torrentEntityTask, ObservableCollection <TorrentEntity> torrents) { try { _torrentTask = torrentEntityTask; Torrent = null; IsWorking = true; InfoText = Res.LoadingTorrentMetadata; Torrent = await torrentEntityTask; IsWorking = false; InfoText = string.Empty; if (Torrent == null) { Torrent = null; _dialogService.ShowMessageBox(Res.InvalidTorrent, messageBoxImage: MessageBoxImage.Error); } else if (torrents.Any(t => t.TorrentUri.Equals(_torrent.TorrentUri))) { Torrent = null; _dialogService.ShowMessageBox(Res.TorrentAlreadyExist, messageBoxImage: MessageBoxImage.Error); } } catch (Exception) { Torrent = null; InfoText = string.Empty; IsWorking = false; _dialogService.ShowMessageBox(Res.InvalidTorrent, messageBoxImage: MessageBoxImage.Error); _dialogService.Close(this); } }
/// <summary> /// Sets the individual file priority. /// </summary> /// <param name="torrent">The torrent.</param> /// <param name="file">The file.</param> /// <param name="priority">The priority.</param> public void SetIndividualFilePriority(TorrentEntity torrent, TorrentFileEntity file, TorrentPriority priority) { var index = torrent.TorrentFiles.IndexOf(file); torrent.TorrentHandle.SetFilePriority(index, (int)priority); file.Priority = priority; file.PriorityText = priority.ToString(); }
/// <summary> /// Performs the torrent finished activities. /// </summary> /// <param name="torrent">The torrent.</param> private void PerformTorrentFinishedActivities(TorrentEntity torrent) { torrent.HasFinishedOnce = true; if (_appSettingsService.ApplicationSettings.StopTorrentsWhenFinished) { StopTorrent(torrent); } }
/// <summary> /// Stops the torrent. /// </summary> /// <param name="torrent">The torrent.</param> public void StopTorrent(TorrentEntity torrent) { if (torrent.TorrentHandle != null) { torrent.TorrentHandle.AutoManaged = false; torrent.TorrentHandle.Pause(); } torrent.BasicTorrentState = TorrentBaseState.Stopped; torrent.Eta = string.Empty; torrent.UpSpeed = string.Empty; torrent.DownSpeed = string.Empty; torrent.State = TorrentBaseState.Stopped.ToString(); }
/// <summary> /// Adds the torrent to session. /// </summary> public void AddTorrentToSession() { _torrentTask.Wait(3000); if (_torrentTask.IsCompleted) { _torrentService.AddTorrentToSession(Torrent); _dialogService.Close(this); } else { Torrent = null; InfoText = string.Empty; IsWorking = false; _dialogService.ShowMessageBox(Res.InvalidTorrent, messageBoxImage: MessageBoxImage.Error); _dialogService.Close(this); } }
/// <summary> /// Updates the progress of an individual file in a torrent /// </summary> /// <param name="torrent">The torrent.</param> public void UpdateTorrentFilesStats(TorrentEntity torrent) { if (torrent.TorrentFiles.Count > 0) { var index = 0; var fileProgresses = torrent.TorrentHandle.GetFileProgresses(); foreach (var file in torrent.TorrentFiles) { if (file.Priority != TorrentPriority.Skip && fileProgresses[index] > 0) { file.Progress = (float)fileProgresses[index] / file.Length * 100; file.ProgressText = string.Format(NumberFormatInfo.InvariantInfo, "{0:0} %", file.Progress); } index++; } } }
/// <summary> /// Starts the torrent. /// </summary> /// <param name="torrent">The torrent.</param> public void StartTorrent(TorrentEntity torrent) { if (torrent.TorrentHandle == null) { AddTorrentToSession(torrent); } if (torrent.TorrentHandle != null) { torrent.TorrentHandle.AutoManaged = true; torrent.TorrentHandle.Resume(); torrent.BasicTorrentState = TorrentBaseState.Started; if (_isSpeedLimitModeActived) { torrent.TorrentHandle.DownloadLimit = _appSettingsService.ApplicationSettings.TurtleModeDownloadLimit; torrent.TorrentHandle.UploadLimit = _appSettingsService.ApplicationSettings.UploadLimit; } } }
/// <summary> /// Loads the new torrent. /// </summary> /// <param name="metadata">The metadata.</param> /// <param name="uri">The URI.</param> /// <returns>TorrentEntity.</returns> private TorrentEntity LoadNewTorrent(byte[] metadata, string uri) { using (var torrentInfo = new TorrentInfo(metadata)) { var torrent = new TorrentEntity { Id = Guid.NewGuid(), BasicTorrentState = TorrentBaseState.Started, DateAdded = DateTime.Now.ToShortDateString(), TorrentMetadata = metadata, TorrentFiles = new ObservableCollection <TorrentFileEntity>(), TorrentUri = uri, SavePath = _appSettingsService.ApplicationSettings.DownloadFolderPath, Size = GeneralMethods.NumberToFileSize(torrentInfo.TotalSize), Name = torrentInfo.Name }; for (var i = 0; i < torrentInfo.NumFiles; i++) { using (var entry = torrentInfo.FileAt(i)) { torrent.TorrentFiles.Add(new TorrentFileEntity { Id = Guid.NewGuid(), Name = Path.GetFileName(entry.Path), Path = Path.Combine(_appSettingsService.ApplicationSettings.DownloadFolderPath, entry.Path), Size = GeneralMethods.NumberToFileSize(entry.Size), Length = entry.Size, IsSelected = true, Priority = TorrentPriority.Normal, PriorityText = TorrentPriority.Normal.ToString() }); } } return(torrent); } }
/// <summary> /// Adds the torrent to session. /// </summary> /// <param name="torrent">The torrent.</param> public void AddTorrentToSession(TorrentEntity torrent) { using (var parameters = new AddTorrentParams()) { parameters.SavePath = torrent.SavePath; parameters.TorrentInfo = new TorrentInfo(torrent.TorrentMetadata); torrent.TorrentHandle = _session.AddTorrent(parameters); } if (!Torrents.Contains(torrent)) { for (var i = 0; i < torrent.TorrentFiles.Count; i++) { if (!torrent.TorrentFiles[i].IsSelected || torrent.TorrentFiles[i].Priority == TorrentPriority.Skip) { torrent.TorrentHandle.SetFilePriority(i, 0); torrent.TorrentFiles[i].Priority = TorrentPriority.Skip; } } Torrents.Add(torrent); _torrentRepository.InsertOrUpdate(torrent); } }
/// <summary> /// Updates the torrent stats. /// </summary> /// <param name="torrent">The torrent.</param> /// <param name="status">The status.</param> private void UpdateTorrentStats(TorrentEntity torrent, TorrentStatus status) { torrent.DownSpeed = GeneralMethods.NumberToSpeed(status.DownloadRate); torrent.UpSpeed = GeneralMethods.NumberToSpeed(status.UploadRate); torrent.Progress = status.Progress * 100; torrent.Seeds = status.NumSeeds; torrent.Peers = status.NumPeers; switch (status.State) { case TorrentState.Downloading: torrent.State = string.Format(NumberFormatInfo.InvariantInfo, " {0} {1:0.0} %", "Downloading", torrent.Progress); break; case TorrentState.CheckingFiles: torrent.State = "Checking files"; break; case TorrentState.CheckingResumeData: torrent.State = "Checking resume data"; break; case TorrentState.DownloadingMetadata: torrent.State = "Downloading metadata"; break; case TorrentState.QueuedForChecking: torrent.State = "Queued for checking"; break; default: torrent.State = status.State.ToString(); break; } if (status.DownloadRate > 0) { var timeSpan = new TimeSpan(0, 0, (int)((status.TotalWanted - status.TotalDone) / status.DownloadRate)); if (timeSpan.TotalDays > 1) { torrent.Eta = $"{timeSpan.TotalDays:0} days {timeSpan.Hours:0} hours"; } else if (timeSpan.TotalHours > 1) { torrent.Eta = $"{timeSpan.TotalHours:0} hours {timeSpan.Minutes:0} minutes"; } else if (timeSpan.TotalMinutes > 1) { torrent.Eta = $"{timeSpan.TotalMinutes:0} minutes {timeSpan.Seconds:0} seconds"; } else if (timeSpan.TotalSeconds > 1) { torrent.Eta = $"{timeSpan.TotalSeconds:0} seconds"; } } if (status.IsFinished && !torrent.HasFinishedOnce) { PerformTorrentFinishedActivities(torrent); } }