//Copyright (C)2002 Merkur ( merkur-@users.sourceforge.net / http://www.emule-project.net )
//
//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., 675 Mass Ave, Cambridge, MA 02139, USA.

#pragma once
#include "StdAfx.h"
#include "partfile.h"
#include "opcodes.h"
#include "types.h"
#include "sharedfilelist.h"
#include "preferences.h"

class CDownloadQueue
{
	friend class CAddFileThread;
	friend class CServerSocket;
public:
	CDownloadQueue(CPreferences* in_prefs,CSharedFileList* in_sharedfilelist);
	~CDownloadQueue();
	void	Process();
	void	Init();
	void	AddSearchToDownload(CSearchFile* toadd);
	void	AddSearchToDownload(CString link);
	void	AddFileLinkToDownload(class CED2KFileLink* pLink);
	bool	IsFileExisting(uchar* fileid);
	bool	IsPartFile(void* totest);
	CPartFile*	GetFileByID(uchar* filehash);
	void    CheckAndAddSource(CPartFile* sender,CUpDownClient* source, bool saveSources = true); //modified for saving sources (by Mr.Fry)
	void    CheckAndAddKnownSource(CPartFile* sender,CUpDownClient* source);
	bool	RemoveSource(CUpDownClient* toremove, bool updatewindow = true);
	void	DeleteAll();
	void	RemoveFile(CPartFile* toremove);
	uint32	GetDatarate()			{return datarate;}
	void	SortByPriority();
	void	StopUDPRequests();
	CServer*	cur_udpserver;
	void	GetDownloadStats(int results[]);
	void	AddPartFilesToShare();
	void	AddDownload(CPartFile* newfile);
	CUpDownClient* 	GetDownloadClientByIP(uint32 dwIP);
	void	StartNextFile();
	void	AddDownDataRateMSOverhead(uint32 data, uint32 header)	{ m_nDownDataRateMSOverhead = m_nDownDataRateMSOverhead+data+header;}
	uint32	GetDatarateOverhead()		{return m_nDownDatarateOverhead;}
	void	CompDatarateOverhead();
	void	DisableAllA4AFAuto(void); // Added by Tarod -deactivates A4AF auto in all files
	CPartFile* GetA4AFAutoFile(void); // Added by Tarod -returns file with A4AF auto state on
	//////////////////////////////////////////////////
	//for saving sources
		//save source for all part-files to disk
	bool	SaveAllSourcesToFile(bool debug=false);	//added by Mr.Fry
		//keep the object for saving/loading sources open, after loading from disk
		//(if true, there should be less CPU-usage)
	bool	KeepStoredSourcesOpen()		{return this->m_bKeepStoredSourcesOpened;};
		//is the count of client (up- and downloading) greater than the "Max Connections"-setting?
	bool	IsMaxClientsExceeded();
		//if false, for no part-files stored sources are loaded from disk
		//(-> disable loading sources)
	bool	IsSourcesLoadingEnabled()	{return this->m_bEnableSourceLoading;};
		//enable/disable loading of stored sources for all part-files
		//if false, for no part-files stored sources are loaded from disk
		//(-> disable loading sources)
	void	SetSourcesLoading(bool setTo){this->m_bEnableSourceLoading = setTo;};
	//////////////////////////////////////////////////
	
protected:
	bool	SendNextUDPPacket();
	//////////////////////////////////////////////////
	//for saving sources
		//keep the object for saving/loading sources open, after loading from disk
		//(if true, there should be less CPU-usage)
	bool		m_bKeepStoredSourcesOpened;
		//if false, for no part-files stored sources are loaded from disk
		//(-> disable loading sources)
	bool		m_bEnableSourceLoading;
	//////////////////////////////////////////////////

private:
	CTypedPtrList<CPtrList, CPartFile*> filelist;
	CSharedFileList* sharedfilelist;
	CPreferences*	 app_prefs;
	uint16	filesrdy;
	uint32	datarate;
	
	CPartFile*	lastfile;
	uint32		lastudpsearchtime;
	uint32		lastudpstattime;
	uint8		udcounter;

	uint32		m_nDownDatarateOverhead;
	uint32		m_nDownDataRateMSOverhead;
	CList<int,int>									 m_AvarageDDRO_list;

};
