internal void Parse(TorrentsList TorrentsToParse, bool IsFresh) { List <string> ListOfHashes = new List <string>(); foreach (string[] TorrentArray in TorrentsToParse) { if (TorrentArray.Length == 0) { throw new FormatException("The array of torrent data was not in the expected format, it contains 0 elements."); } ListOfHashes.Add(TorrentArray[0]); Torrent NewTorrent = GetByHashCode(TorrentArray[0]); if (NewTorrent == null) { NewTorrent = new Torrent(TorrentArray, this); _torrentCollectionInternal.Add(NewTorrent); if (!IsFresh) { ParentClient.CallEvent(UTorrentWebClient.EventType.Added, NewTorrent); } CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, NewTorrent)); } else { NewTorrent.UpdateValuesFromStringArray(TorrentArray); } } IEnumerable <Torrent> RemovedTorrents = RemoveWhereHashCodeIsNotInList(ListOfHashes); foreach (Torrent RemovedTorrent in RemovedTorrents) { CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, RemovedTorrent)); } }
private void RegisterTorrent(Torrent torrent, string downloadFolderPath) { var wrapper = new TorrentWrapper(downloadFolderPath, torrent); TorrentsList.Add(wrapper); SelectedWrapper = wrapper; _engine.Register(SelectedWrapper.Manager); }
private void GetTorrentsAndLabelsUpdate(TorrentsList TorrentsToProcess) { TorrentsAndLabels CurrentTorrents = ServiceClient.GetAllTorrentsAndLabels(_token); _torrents.Parse(CurrentTorrents.Torrents, false); _labels.Parse(CurrentTorrents.Labels); SetCache(CurrentTorrents.CacheID); }
private async void CreateTorrentInfo() { _unitOfWork.GetRepository <TorrentInfo>().Create(new TorrentInfo() { InfoHash = SelectedWrapper.Manager.InfoHash.ToString(), SavePath = SelectedWrapper.Manager.SavePath, LastState = TorrentsList.Last().Manager.State.ToString() }); await _unitOfWork.SaveChangesAsync(); }
internal void Parse(TorrentsList TorrentsToParse, RemovedTorrentsList RemovedTorrentsToParse, ChangedTorrentsList ChangedTorrentsToParse) { if (RemovedTorrentsToParse == null || ChangedTorrentsToParse == null) { if (TorrentsToParse != null) { Parse(TorrentsToParse, false); } } else { List <Torrent> RemovedTorrents = new List <Torrent>(); foreach (string[] TorrentArray in RemovedTorrentsToParse) { if (TorrentArray.Length == 0) { throw new FormatException("The array of torrent data was not in the expected format, it contains 0 elements."); } RemovedTorrents.Clear(); RemovedTorrents.AddRange(RemoveByHashCode(TorrentArray[0])); foreach (Torrent RemovedTorrent in RemovedTorrents) { CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, RemovedTorrent)); } } foreach (string[] TorrentArray in ChangedTorrentsToParse) { if (TorrentArray.Length == 0) { throw new FormatException("The array of torrent data was not in the expected format, it contains 0 elements."); } Torrent NewTorrent = GetByHashCode(TorrentArray[0]); if (NewTorrent == null) { NewTorrent = new Torrent(TorrentArray, this); _torrentCollectionInternal.Add(NewTorrent); ParentClient.CallEvent(UTorrentWebClient.EventType.Added, NewTorrent); CallCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, NewTorrent)); } else { NewTorrent.UpdateValuesFromStringArray(TorrentArray); } } } }
private void GetTorrentsAndLabelsUpdate(TorrentsList TorrentsToProcess) { if (_token == null) { return; } try { TorrentsAndLabels CurrentTorrents = ServiceClient.GetAllTorrentsAndLabels(_token); _torrents.Parse(CurrentTorrents.Torrents, false); _labels.Parse(CurrentTorrents.Labels); SetCache(CurrentTorrents.CacheID); } catch (System.ServiceModel.ProtocolException e) { // Token possibly expired, get new token. Trace.TraceError(e.Message + e.StackTrace); GetToken(); } }
private void DeleteTorrent(object arg) { if (SelectedWrapper != null) { var deleteAction = new Action(async() => { _engine.Unregister(SelectedWrapper.Manager); var torrentHashString = SelectedWrapper.Manager.InfoHash.ToString(); var searchResult = await _unitOfWork.GetRepository <TorrentInfo>().GetSingleOrDefaultAsync(torrentInfo => torrentInfo.InfoHash == torrentHashString); _unitOfWork.GetRepository <TorrentInfo>().Delete(searchResult); await _unitOfWork.SaveChangesAsync(); SelectedWrapper.Manager.Dispose(); SelectedWrapper.DeleteFastResume(); File.Delete(SelectedWrapper.Torrent.TorrentPath); TorrentsList.Remove(SelectedWrapper); }); SelectedWrapper.PretendToDelete = true; if (SelectedWrapper.State == TorrentState.Stopped) { _dispatcher.Invoke(deleteAction); } else { StopCommand.Execute(null); SelectedWrapper.Manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs args) { if (args.NewState == TorrentState.Stopped && SelectedWrapper.PretendToDelete) { _dispatcher.Invoke(deleteAction); } }; } } }
//private void StoreTorrents() //{ // if (_torrentsHashDictianory.Count > 0) // { // var formatter = new BinaryFormatter(); // using (var fileStream = new FileStream(hashedTorrentsListPath, FileMode.Create)) // { // formatter.Serialize(fileStream, _torrentsHashDictianory); // } // } //} private async void RestoreTorrents() { var hashedTorrentsFileNames = Directory.GetFiles(hashedTorrentsFolderPath, "*.torrent"); foreach (var fileName in hashedTorrentsFileNames) { var torrent = Torrent.Load(fileName); var torrentHashString = torrent.InfoHash.ToString(); var torrenInfo = await _unitOfWork.GetRepository <TorrentInfo>() .GetSingleOrDefaultAsync(torrentInfoPredicate => torrentInfoPredicate.InfoHash == torrentHashString); RegisterTorrent(torrent, torrenInfo.SavePath); if (torrenInfo.LastState == "Downloading") { StartDownload(); } else if (torrenInfo.LastState == "Paused") { TorrentsList.Last().Manager.HashCheck(false); } } }
private void GetTorrentsAndLabelsUpdate(TorrentsList TorrentsToProcess) { if(_token == null) return; try { TorrentsAndLabels CurrentTorrents = ServiceClient.GetAllTorrentsAndLabels(_token); _torrents.Parse(CurrentTorrents.Torrents, false); _labels.Parse(CurrentTorrents.Labels); SetCache(CurrentTorrents.CacheID); } catch(System.ServiceModel.ProtocolException e) { // Token possibly expired, get new token. Trace.TraceError(e.Message + e.StackTrace); GetToken(); } }