DownloadManager.cs :  » Business-Application » DC » DCSharp » Backend » Managers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » DC 
DC » DCSharp » Backend » Managers » DownloadManager.cs
/* 
 * 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
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.