/***************************************************************************
 *   Copyright (C) 2005 by xavier helluy                                   *
 *   xavier.helluy@web.de                                                  *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include "urldispatcher.h"
#include <kdebug.h>

URLDispatcher::URLDispatcher()
{
	buildSupportedArchivesByMimeTypes();
	buildSupportedArchivesProtocols();
}


URLDispatcher::~URLDispatcher()
{
}


/*////////////////////////////////////////////////////////////////////////////////////*/
/*////////////////////////////////////archive code////////////////////////////////////*/
/*////////////////////////////////////////////////////////////////////////////////////*/
void URLDispatcher::buildSupportedArchivesProtocols( )
{
	m_ArchivesProtocols.clear();
	m_ArchivesProtocols.insert (   TAR_FORMAT  , "tar" );
	m_ArchivesProtocols.insert (   ZIP_FORMAT , "zip" );

}

void URLDispatcher::buildSupportedArchivesByMimeTypes( )
{
	m_supportedMimeArchives.clear();
	m_supportedMimeArchives.insert ( "application/x-compressed-tar"  	, TAR_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-tzo"  	, TAR_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-tarz" 	, TAR_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-bzip-compressed-tar"  	, TAR_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-tar"  	, TAR_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-java-archive"  	, ZIP_FORMAT);
	m_supportedMimeArchives.insert ( "application/zip"  	, ZIP_FORMAT);
	
	//web-archive
	m_supportedMimeArchives.insert ( "application/x-webarchive" , TAR_FORMAT);
	
	//openoffice
	m_supportedMimeArchives.insert ( "application/vnd.oasis" , ZIP_FORMAT);
	m_supportedMimeArchives.insert ( "application/vnd.stardivision" , ZIP_FORMAT);
	m_supportedMimeArchives.insert ( "application/vnd.sun.xml" , ZIP_FORMAT);
	
	//koffice
	m_supportedMimeArchives.insert ( "application/x-kword" , ZIP_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-kpresenter" , ZIP_FORMAT);
	m_supportedMimeArchives.insert ( "application/x-kspread" , ZIP_FORMAT);

}

CLuceneArchType URLDispatcher::findArchiveType(QString mimeType)
{
	
	QMap<QString , CLuceneArchType>::Iterator it;
	it =  m_supportedMimeArchives.find(mimeType);
	if ( ! ( it == m_supportedMimeArchives.end() ) )
	{
		CLuceneArchType res = it.data();
		kdDebug() << " URLDispatcher::findArchiveType FOUND"  << mimeType << endl;
		return res;
	}
	return UNKNOWN_FORMAT;
}

//only works with local archives. and one a single recursion
//FIXME: as long as SUBURLS are NOT SUPPORTED
//adapted from kdeutils/ark/archiveformatinfo.cpp kde-3.4-beta2
QString URLDispatcher::archiveFactory(  CLuceneArchType aType , QString path )
{
	switch( aType )
	{
		case TAR_FORMAT:
			return QString( m_ArchivesProtocols[aType] + ":" + path );

		case ZIP_FORMAT:
			return QString( m_ArchivesProtocols[aType] + ":" + path );
		
		case UNKNOWN_FORMAT:
		default:
			return QString::null;
	}
}

bool URLDispatcher::isArchiveProtocol(QString protocol)
{
	QMap< CLuceneArchType , QString>::Iterator it;
	for ( it = m_ArchivesProtocols.begin(); it != m_ArchivesProtocols.end() ; ++it )
	{
		if (it.data() == protocol )
			return TRUE;
			
	}
	return FALSE;
}






