/*
* Copyright (C) 2004-2005 Jonathan Bindel
* Copyright (C) 2006-2007 Eskil Bylund
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using DCSharp.Backend.Objects;
using DCSharp.Xml;
namespace DCSharp.Backend.Managers{
public class DownloadManager : IEnumerable<DownloadFileInfo>
{
public event EventHandler<DownloadEventArgs> DownloadAdded;
public event EventHandler<DownloadEventArgs> DownloadRemoved;
private List<DownloadFileInfo> downloads;
private Downloader downloader;
#region Constructors
public DownloadManager(ConnectionManager connectionManager,
Listener listener)
{
downloads = new List<DownloadFileInfo>();
downloader = new Downloader(connectionManager, this, listener);
}
#endregion
#region Properties
public int Count
{
get { return downloads.Count; }
}
public object SyncRoot
{
get { return downloads; }
}
#endregion
#region Methods
public void Add(DownloadFileInfo download)
{
if (download == null)
{
throw new ArgumentNullException("download");
}
lock (downloads)
{
if (Contains(download))
{
return;
}
downloads.Add(download);
OnDownloadAdded(download);
}
}
public void Remove(DownloadFileInfo download)
{
if (download == null)
{
throw new ArgumentNullException("download");
}
lock (downloads)
{
if (!Contains(download))
{
return;
}
downloads.Remove(download);
OnDownloadRemoved(download);
}
// Remove the temp file
if (download.TempTarget != null)
{
string tempFile = Path.Combine(download.TempTarget,
download.Name + ".dctmp");
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
public bool Contains(DownloadFileInfo download)
{
return downloads.Contains(download);
}
public DownloadFileInfo Find(Predicate<DownloadFileInfo> match)
{
lock (downloads)
{
for (int i = 0; i < downloads.Count; i++)
{
if (match(downloads[i]))
{
return downloads[i];
}
}
}
return null;
}
public IEnumerator<DownloadFileInfo> GetEnumerator()
{
return downloads.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public DownloadFileInfo AddFileList(User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
DownloadFileInfo download = new DownloadFileInfo(user.Uid);
download.Add(new SourceInfo(user, null));
Add(download);
return download;
}
public bool HasDownload(User user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
DownloadFileInfo download = Find(delegate(DownloadFileInfo d)
{
return d.HasSource(user);
});
return download != null;
}
public DownloadFileInfo GetDownload(string tth)
{
if (tth == null)
{
throw new ArgumentNullException("tth");
}
return Find(delegate(DownloadFileInfo download)
{
return download.TTH == tth;
});
}
public void SetNextDownload(User user, DownloadFileInfo download,
bool forceStart)
{
downloader.SetNextDownload(user, download, forceStart);
}
protected virtual void OnDownloadAdded(DownloadFileInfo download)
{
if (DownloadAdded != null)
{
DownloadAdded(this, new DownloadEventArgs(download));
}
}
protected virtual void OnDownloadRemoved(DownloadFileInfo download)
{
if (DownloadRemoved != null)
{
DownloadRemoved(this, new DownloadEventArgs(download));
}
}
// TODO: Let some other class handle the saving/loading of the downloads.
public void Save(string filename)
{
lock (downloads)
{
Queue queue = new Queue();
queue.Downloads = downloads.FindAll(delegate (DownloadFileInfo d)
{
return !d.IsFileList;
});
XmlHelper.Serialize(queue, filename);
}
}
public void Load(string filename)
{
lock (downloads)
{
Queue queue = (Queue)XmlHelper.Deserialize(typeof(Queue), filename);
foreach (DownloadFileInfo download in queue.Downloads)
{
if (download.Name != null && download.Target != null &&
!download.IsFileList)
{
Add(download);
}
}
}
}
// Helper class for xml serialization of downloads
[XmlRoot("Downloads")]
public class Queue
{
[XmlElement("Download")]
public List<DownloadFileInfo> Downloads = new List<DownloadFileInfo>();
}
#endregion
}
}
|