/***************************************************************************
 *   Copyright (C) 2010 by Volker Lanz <vl@fidra.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 Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include "veneer/filedialog.h"
#include "veneer/url.h"
#include "veneer/messagebox.h"

#include <kio/netaccess.h>
#include <kfiledialog.h>

#include <QStringList>
#include <QFileDialog>
#include <QDebug>

namespace Veneer
{

	QString getOpenFileName(QWidget* parent, const QString& dir)
	{
		const Url url = getOpenUrl(parent, dir);

		if (url.isEmpty())
			return QString();

		QString fileName;
		if (!KIO::NetAccess::download(url, fileName, parent))
		{
			Veneer::messageBoxError(parent, QObject::tr("Could not open file %1: %2").arg(url.fileName()).arg(KIO::NetAccess::lastErrorString()), QObject::tr("Error Opening File"));
			return QString();
		}

		return fileName;
	}

	Url getOpenUrl(QWidget*, const QString& id)
	{
		return KFileDialog::getOpenUrl(KUrl("kfiledialog:///" + id));
	}

	QStringList getOpenFileNames(QWidget* parent, const QString& dir, const QString& patterns)
	{
		QList<Url> urls = getOpenUrls(parent, dir, patterns);

		QStringList result;

		foreach(const KUrl& url, urls)
		{
			QString s;

			if (!KIO::NetAccess::download(url, s, parent))
			{
				Veneer::messageBoxError(parent, QObject::tr("Could not open file %1: %2").arg(url.fileName()).arg(KIO::NetAccess::lastErrorString()), QObject::tr("Error Opening File"));
				return QStringList();
			}

			result << s;
		}

		return result;
	}

	QList<Url> getOpenUrls(QWidget*, const QString& dir, const QString& patterns)
	{
		QList<Url> result;

		const KUrl::List urls = KFileDialog::getOpenUrls(KUrl("kfiledialog:///" + dir), patterns);

		foreach(const KUrl& u, urls)
			result << u;

		return result;
	}

	Url getSaveUrl(QWidget*, const QString& dir, const QString& patterns, const QString& fileName)
	{
		QString startUrl = "kfiledialog:///" + dir;

		if (!fileName.isEmpty())
			startUrl += "/" + fileName;

		return KFileDialog::getSaveUrl(KUrl(startUrl), patterns);
	}
}

