/*
* Copyright (C) 2004-2005 Jonathan Bindel
* Copyright (C) 2006 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.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using DCSharp.Extras;
namespace DCSharp.Xml.FileList{
public class FileListing
{
public FileListing()
{
version = "1";
cid = Runtime.Settings.LocalIdentity.Cid;
generator = "DC++ 0.674";
directoryBase = "/";
directories = new List<Directory>();
}
#region Properties
private string version;
[XmlAttribute()]
public string Version
{
get
{
return version;
}
set
{
version = value;
}
}
private string cid;
[XmlAttribute()]
public string CID
{
get
{
return cid;
}
set
{
cid = value;
}
}
private string generator;
[XmlAttribute()]
public string Generator
{
get
{
return generator;
}
set
{
generator = value;
}
}
private string directoryBase;
[XmlAttribute()]
public string Base
{
get
{
return directoryBase;
}
set
{
directoryBase = value;
}
}
private List<Directory> directories;
[XmlElement("Directory")]
public List<Directory> Directories
{
get
{
return directories;
}
set
{
directories = value;
}
}
[XmlIgnore()]
public Directory this[string name]
{
get
{
return directories.Find(delegate(Directory directory)
{
return directory.Name == name;
});
}
}
#endregion
#region Methods
public void Add(Directory dir)
{
directories.Add(dir);
}
public void Remove(Directory dir)
{
directories.Remove(dir);
}
public bool Contains(Directory dir)
{
return directories.Contains(dir);
}
public static FileListing GrabFileListing(Stream instream,
Util.Compression compression, string nick)
{
string path = Path.Combine(Runtime.ApplicationData, "FileLists");
System.IO.Directory.CreateDirectory(path);
path = Path.Combine(path, nick + ".xml");
using(FileStream fs = new FileStream(path, FileMode.Create,
FileAccess.Write, FileShare.Read))
{
Util.Decompress(instream, fs, compression);
}
FileListing fileListing = new FileListing();
fileListing.Load(path);
System.IO.File.Delete(path);
return fileListing;
}
public long GetSize()
{
long total = 0;
foreach(Directory directory in directories)
{
total += directory.GetSize();
}
return total;
}
public void Save(string filename)
{
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filename));
XmlSerializer xs = new XmlSerializer(typeof(FileListing));
// Remove namespaces
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add(String.Empty, String.Empty);
// Omit the byte order mark, as defined by the ADC protocol.
Encoding encoding = new UTF8Encoding(false);
using(XmlTextWriter writer = new XmlTextWriter(filename, encoding))
{
writer.Formatting = Formatting.Indented;
writer.IndentChar = '\t';
writer.Indentation = 1;
// Include the standalone attribute
writer.WriteStartDocument(true);
xs.Serialize(writer, this, xsn);
}
}
public void Load(string filename)
{
if (filename == null)
{
throw new ArgumentNullException("filename");
}
using (XmlTextReader reader = new XmlTextReader(filename))
{
Load(reader);
}
}
protected void Load(XmlTextReader reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
// TODO: Load version, generator, etc.
reader.MoveToContent();
while (reader.Read())
{
if (reader.Name == "Directory" &&
reader.NodeType == XmlNodeType.Element)
{
Directory directory = ReadDirectory(reader);
directories.Add(directory);
}
}
}
private static Directory ReadDirectory(XmlReader reader)
{
Directory directory = new Directory(reader.GetAttribute("Name"));
if (reader.IsEmptyElement)
{
return directory;
}
while (reader.Read())
{
if (reader.Name == "Directory" &&
reader.NodeType == XmlNodeType.EndElement)
{
break;
}
if (reader.Name == "Directory" &&
reader.NodeType == XmlNodeType.Element)
{
directory.Directories.Add(ReadDirectory(reader));
}
else if (reader.Name == "File" &&
reader.NodeType == XmlNodeType.Element)
{
File file = new File(reader.GetAttribute("Name"),
long.Parse(reader.GetAttribute("Size")),
reader.GetAttribute("TTH"));
directory.Files.Add(file);
}
}
return directory;
}
#endregion
}
}
|