/**********************************************************************
 * 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 "addtask.h"
#include <QUrl>
#include <QFileInfo>
#include <QValidator>
#include <QMessageBox>
#include <QFileDialog>
#include "xmlsettingsmanager.h"

namespace LC
{
namespace CSTP
{
	class URLValidator : public QValidator
	{
	public:
		URLValidator (QObject *parent = 0)
		: QValidator (parent)
		{
		}

		virtual ~URLValidator ()
		{
		}

		virtual State validate (QString& input, int&) const
		{
			if (QUrl (input).isValid () || input.isEmpty ())
				return Acceptable;
			else
				return Intermediate;
		}
	};

	AddTask::Task::Task (const QUrl& url,
			const QString& localPath,
			const QString& filename,
			const QString& comment)
	: URL_ (url)
	, LocalPath_ (localPath)
	, Filename_ (filename)
	, Comment_ (comment)
	{
	}

	AddTask::AddTask (QWidget *parent)
	: QDialog (parent)
	, UserModifiedFilename_ (false)
	{
		Ui_.setupUi (this);
		Ui_.ButtonBox_->button (QDialogButtonBox::Ok)->setEnabled (false);
		Ui_.URL_->setValidator (new URLValidator (this));
		Ui_.LocalPath_->setText (XmlSettingsManager::Instance ().Property ("LocalPath",
					QDir::homePath ()).toString ());
	}

	AddTask::AddTask (const QUrl& url, const QString& where, QWidget *parent)
	: QDialog (parent)
	, UserModifiedFilename_ (false)
	{
		Ui_.setupUi (this);
		Ui_.ButtonBox_->button (QDialogButtonBox::Ok)->setEnabled (false);
		Ui_.URL_->setValidator (new URLValidator (this));
		Ui_.LocalPath_->setText (where);
		Ui_.URL_->setText (url.toString ());
		on_LocalPath__textChanged ();
		on_URL__textEdited (url.toString ());
		CheckOK ();
	}

	AddTask::~AddTask ()
	{
	}

	AddTask::Task AddTask::GetTask () const
	{
		return Task (Ui_.URL_->text (),
				Ui_.LocalPath_->text (),
				Ui_.Filename_->text (),
				Ui_.Comment_->toPlainText ());
	}

	void AddTask::accept ()
	{
		QFileInfo dir (Ui_.LocalPath_->text ());
		QString message;
		if (!dir.exists ())
			message = tr ("Directory %1 doesn't exist, would you like to "
					"select another?").arg (dir.absolutePath ());
		else if (!dir.isReadable ())
			message = tr ("Directory %1 isn't readable, would you like to "
					"select another?").arg (dir.absolutePath ());
		else if (!dir.isWritable ())
			message = tr ("Directory %1 isn't writable, would you like to "
					"select another?").arg (dir.absolutePath ());
		else if (!dir.isDir ())
			message = tr ("%1 isn't a directory at all, would you like to "
					"select another?").arg (dir.absolutePath ());
		else
		{
			QDialog::accept ();
			return;
		}

		if (QMessageBox::question (this,
					"LeechCraft",
					message,
					QMessageBox::Ok | QMessageBox::Cancel) ==
				QMessageBox::Ok)
			on_BrowseButton__released ();
		else
			QDialog::reject ();
	}

	void AddTask::on_URL__textEdited (const QString& str)
	{
		CheckOK ();
		if (UserModifiedFilename_)
			return;

		Ui_.Filename_->setText (QFileInfo (QUrl (str).path ()).fileName ());
	}

	void AddTask::on_LocalPath__textChanged ()
	{
		CheckOK ();
		XmlSettingsManager::Instance ().setProperty ("LocalPath", Ui_.LocalPath_->text ());
	}

	void AddTask::on_Filename__textEdited ()
	{
		UserModifiedFilename_ = true;
		CheckOK ();
	}

	void AddTask::on_BrowseButton__released ()
	{
		QString dir = QFileDialog::getExistingDirectory (this, tr ("Select directory"),
				XmlSettingsManager::Instance ().property ("LocalPath").toString ());
		if (dir.isEmpty ())
			return;

		Ui_.LocalPath_->setText (dir);
		on_LocalPath__textChanged ();
	}

	void AddTask::CheckOK ()
	{
		bool valid = QUrl (Ui_.URL_->text ()).isValid () &&
				!Ui_.LocalPath_->text ().isEmpty () &&
				!Ui_.Filename_->text ().isEmpty ();
		Ui_.ButtonBox_->button (QDialogButtonBox::Ok)->setEnabled (valid);
	}
}
}
