/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#include <QFileDialog>
#include <QDir>
#include "firststep.h"
#include "xmlsettingsmanager.h"

namespace LeechCraft
{
namespace Plugins
{
namespace BitTorrent
{
	FirstStep::FirstStep (QWidget *parent)
	: QWizardPage (parent)
	{
		setupUi (this);
		registerField ("Output", Output_);
		registerField ("AnnounceURL*", AnnounceURL_);
		registerField ("Date", Date_);
		registerField ("Comment", Comment_);
		registerField ("RootPath", RootPath_);
		Date_->setDateTime (QDateTime::currentDateTime ());
		Output_->setText (XmlSettingsManager::Instance ()->
				property ("LastMakeTorrentDirectory").toString ());
		RootPath_->setText (XmlSettingsManager::Instance ()->
				property ("LastAddDirectory").toString ());
		connect (RootPath_,
				SIGNAL (textChanged (const QString&)),
				this,
				SIGNAL (completeChanged ()));
	}

	bool FirstStep::isComplete () const
	{
		QFileInfo info (RootPath_->text ());
		return info.exists () &&
			info.isReadable ();
	}

	QString FirstStep::PrepareDirectory () const
	{
		QString directory = RootPath_->text ();
		if (!QFileInfo (directory).isDir ())
			directory = QFileInfo (directory).absolutePath ();

		if (!QFileInfo (directory).exists ())
			directory = QDir::homePath ();

		if (!directory.endsWith ('/'))
			directory.append ('/');

		return directory;
	}

	void FirstStep::on_BrowseOutput__released ()
	{
		QString last = XmlSettingsManager::Instance ()->
			property ("LastMakeTorrentDirectory").toString ();
		if (!last.endsWith ('/'))
			last += '/';
		if (!QFileInfo (last).exists ())
			last = QDir::homePath ();

		QString directory = QFileDialog::getSaveFileName (this,
				tr ("Select where to save torrent file"),
				last);
		if (directory.isEmpty ())
			return;

		Output_->setText (directory);
		XmlSettingsManager::Instance ()->
			setProperty ("LastMakeTorrentDirectory",
					QFileInfo (directory).absolutePath ());
	}

	void FirstStep::on_BrowseFile__released ()
	{
		QString path = QFileDialog::getOpenFileName (this,
				tr ("Select torrent contents"),
				PrepareDirectory ());
		if (path.isEmpty ())
			return;

		RootPath_->setText (path);
		XmlSettingsManager::Instance ()->
			setProperty ("LastAddDirectory",
					QFileInfo (path).absolutePath ());

		emit completeChanged ();
	}

	void FirstStep::on_BrowseDirectory__released ()
	{
		QString path = QFileDialog::getExistingDirectory (this,
				tr ("Select torrent contents"),
				PrepareDirectory ());
		if (path.isEmpty ())
			return;

		RootPath_->setText (path);
		XmlSettingsManager::Instance ()->
			setProperty ("LastAddDirectory",
					path);

		emit completeChanged ();
	}
}
}
}
