/// <summary> /// Adds downloaditem related to Source /// Source can be null /// </summary> /// <param name="d">DownloadItem to be added</param> /// <param name="sources">Sources to be related to downloaditem</param> public void AddDownload(DownloadItem d, Source[] sources) { // Downloads FlowSortedList <Source> tmpDwn = null; if (!dwnItems.ContainsKey(d)) { lock (this) { tmpDwn = new FlowSortedList <Source>(); d.DownloadCompleted += new FmdcEventHandler(d_DownloadCompleted); d.SegmentCanceled += new FmdcEventHandler(d_SegmentCanceled); d.SegmentCompleted += new FmdcEventHandler(d_SegmentCompleted); d.SegmentStarted += new FmdcEventHandler(d_SegmentStarted); dwnItems.Add(d, tmpDwn); } DownloadAdded(this, new FmdcEventArgs(0, d)); } else { lock (this) { // This is if we have a fake downloaditem (that we probably have) d = dwnItems.Keys[dwnItems.IndexOfKey(d)]; tmpDwn = dwnItems[d]; } } foreach (Source s in sources) { if (s != null) { lock (this) { tmpDwn.Add(s); // Sources FlowSortedList <DownloadItem> tmpSrc = null; if (!srcItems.ContainsKey(s)) { tmpSrc = new FlowSortedList <DownloadItem>(); srcItems.Add(s, tmpSrc); SourceAdded(this, new FmdcEventArgs(0, s)); } else { tmpSrc = srcItems[s]; } tmpSrc.Add(d); } } } }
/// <summary> /// If source is related to a downloaditem in downloadmanager. /// First match will be returned. /// </summary> /// <param name="s">Source to find related downloaditems for</param> /// <param name="d">DownloadItem found for Source</param> /// <returns>Returns true if downloaditem was found for source</returns> public virtual bool TryGetDownload(Source s, out DownloadItem d) { FlowSortedList <DownloadItem> items = null; lock (this) { if (srcItems.TryGetValue(s, out items) && items.Count > 0) { d = items[0]; return(true); } } d = null; return(false); }
/// <summary> /// If DownloadItem is related to any Sources in DownloadManager. /// They will be returned. /// </summary> /// <param name="d">DownloadItem to find related soures for</param> /// <param name="s">Sources found for DownloadItem</param> /// <returns>Returns true if Sources can be found for DownloadItem</returns> public virtual bool TryGetSources(DownloadItem d, out Source[] s) { lock (this) { if (dwnItems.ContainsKey(d)) { FlowSortedList <Source> tmp = dwnItems[d]; if (tmp != null && tmp.Count > 0) { s = tmp.ToArray(); return(true); } } } s = null; return(false); }
protected void RemoveDownload(DownloadItem d, bool shouldLock) { if (dwnItems.ContainsKey(d)) { if (shouldLock) { System.Threading.Monitor.Enter(this); } try { FlowSortedList <Source> tmpDwn = dwnItems[d]; if (tmpDwn != null) { foreach (Source var in tmpDwn) { if (srcItems.ContainsKey(var)) { FlowSortedList <DownloadItem> tmpSrc = srcItems[var]; if (tmpSrc != null) { tmpSrc.Remove(d); if (tmpSrc.Count == 0) { srcItems.Remove(var); } } } } } dwnItems.Remove(d); } finally { if (shouldLock) { System.Threading.Monitor.Exit(this); } } DownloadRemoved(this, new FmdcEventArgs(0, d)); d.DownloadCompleted -= d_DownloadCompleted; d.SegmentCanceled -= d_SegmentCanceled; d.SegmentCompleted -= d_SegmentCompleted; d.SegmentStarted -= d_SegmentStarted; } }
protected void RemoveSource(Source s, bool shouldLock) { if (srcItems.ContainsKey(s)) { if (shouldLock) { System.Threading.Monitor.Enter(this); } try { FlowSortedList <DownloadItem> tmpSrc = srcItems[s]; if (tmpSrc != null) { foreach (DownloadItem var in tmpSrc) { if (dwnItems.ContainsKey(var)) { FlowSortedList <Source> tmpDwn = dwnItems[var]; if (tmpDwn != null) { tmpDwn.Remove(s); } } } } srcItems.Remove(s); } finally { if (shouldLock) { System.Threading.Monitor.Exit(this); } } SourceRemoved(this, new FmdcEventArgs(0, s)); } }
/// <summary> /// Adds downloaditem related to Source /// Source can be null /// </summary> /// <param name="d">DownloadItem to be added</param> /// <param name="sources">Sources to be related to downloaditem</param> public void AddDownload(DownloadItem d, Source[] sources) { // Downloads FlowSortedList<Source> tmpDwn = null; if (!dwnItems.ContainsKey(d)) { lock (this) { tmpDwn = new FlowSortedList<Source>(); d.DownloadCompleted += new FmdcEventHandler(d_DownloadCompleted); d.SegmentCanceled += new FmdcEventHandler(d_SegmentCanceled); d.SegmentCompleted += new FmdcEventHandler(d_SegmentCompleted); d.SegmentStarted += new FmdcEventHandler(d_SegmentStarted); dwnItems.Add(d, tmpDwn); } DownloadAdded(this, new FmdcEventArgs(0, d)); } else { lock (this) { // This is if we have a fake downloaditem (that we probably have) d = dwnItems.Keys[dwnItems.IndexOfKey(d)]; tmpDwn = dwnItems[d]; } } foreach (Source s in sources) { if (s != null) { lock (this) { tmpDwn.Add(s); // Sources FlowSortedList<DownloadItem> tmpSrc = null; if (!srcItems.ContainsKey(s)) { tmpSrc = new FlowSortedList<DownloadItem>(); srcItems.Add(s, tmpSrc); SourceAdded(this, new FmdcEventArgs(0, s)); } else { tmpSrc = srcItems[s]; } tmpSrc.Add(d); } } } }