/***************************************************************************
 *   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 "helpertestsstring.h"
#include <kstandarddirs.h>
#include <kapplication.h>
#include <kdebug.h>


HelperTestsString::HelperTestsString()
{
}


HelperTestsString::~HelperTestsString()
{
}

bool HelperTestsString::isValidPathString(const QString & entry)
{
	if (entry.find(":")>0)
		return false;
	if (!entry.startsWith("/"))
		return false;
	else
	{
		KURL testURL;
		testURL.setPath(entry);
		if (! (testURL.path()==entry))
			return false;
		else
		{
			if (!testURL.isValid())
				return false;
			else
				return true;
		}
	}
}
bool HelperTestsString::isValidURLString(const QString & entry)
{
	//accept ":" once for a single protocol
//	if (!(entry.find(":")==1))
//		return false;
	kdDebug() << "HelperTestsString::isValidURLString:" << entry <<endl;

	KURL testURL(entry);
	
	if (!testURL.isValid())
	{
		kdDebug() << "HelperTestsString::isValidURLString not vaild"  <<endl;

		return false;
	}
	if (!entry.startsWith(testURL.protocol()))
	{
		kdDebug() << "HelperTestsString::isValidURLString BAD PROTOCOLS" << endl;

		return false;
	}
	return true;
}

bool HelperTestsString::isValidFileName(const QString & entry)
{
	kdDebug() << "HelperTestsString::isValidFileName" << entry << endl;

	//no protocol accepted
	if (entry.find(":")>0)
		return false;
	if (entry.find(",")>0)
		return false;
	if (entry.find(" ")>0)
		return false;
	if (entry.find("*")>0)
		return false;
	//no paths accepted	
	KURL tmpURL;
	tmpURL.setPath(entry);
	kdDebug() << "str:" <<  entry << endl;
	kdDebug() << "url:" <<  tmpURL.url() << endl;
	kdDebug() << "nam:" <<  tmpURL.fileName() << endl;
	if ( (!( entry == tmpURL.fileName() ) ) || entry.isEmpty() )
	{
		return false;
	}
	return true;
}
		
//return the path to the local stored indexes
QString HelperTestsString::name2path(const QString & fileName)
{
	QString str(QString::null);
	bool test = isValidFileName(fileName);

	if (!test)
		return QString::null;
	else
	{
		QString path = locateLocal("appdata","stored_indexes/" + fileName);
		kdDebug() << "HelperTestsString::name2path" << path << endl;

		return path ;
	}
}



