/**********************************************************************
 * 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 "userscriptinstallerdialog.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QNetworkRequest>

#if QT_VERSION < 0x050000
#include <QDesktopServices>
#else
#include <QStandardPaths>
#endif

#include "fatape.h"
#include "userscript.h"

namespace LeechCraft
{
namespace Poshuku
{
namespace FatApe
{
	UserScriptInstallerDialog::UserScriptInstallerDialog (Plugin *plugin,
			QNetworkAccessManager *networkManager, const QUrl& scriptUrl, QWidget *parent)
	: QDialog (parent)
	, Plugin_ (plugin)
	{
#if QT_VERSION < 0x050000
		QDir temp (QDesktopServices::storageLocation (QDesktopServices::TempLocation));
#else
		QDir temp (QStandardPaths::writableLocation (QStandardPaths::TempLocation));
#endif

		QFileInfo userScript (temp, QFileInfo (scriptUrl.path ()).fileName ());

		Ui_.setupUi (this);
		TempScriptPath_ = userScript.absoluteFilePath ();

		QNetworkReply *reply = networkManager->get (QNetworkRequest (scriptUrl));
		connect (reply,
				SIGNAL (finished ()),
				this,
				SLOT (scriptFetchFinished ()));

		Ui_.ScriptInfo_->setHtml (QString ("<i>%1</i>").arg (tr ("Fetching script...")));
		connect (Ui_.Install_,
				SIGNAL (released  ()),
				this,
				SLOT (on_Install__released ()));
		connect (Ui_.ShowSource_,
				SIGNAL (released ()),
				this,
				SLOT(on_ShowSource__released ()));
		connect (Ui_.Cancel_,
				SIGNAL (released ()),
				this,
				SLOT (on_Cancel__released ()));
	}

	void UserScriptInstallerDialog::scriptFetchFinished ()
	{
		QNetworkReply *reply = qobject_cast<QNetworkReply*> (sender ());

		QFile tempScript (TempScriptPath_);
		if (tempScript.open (QFile::ReadWrite))
		{
			tempScript.write (reply->readAll ());
			tempScript.close ();
		}

		UserScript script (TempScriptPath_);
		QString scriptDesc = QString ("<b>%1</b><br />%2<hr />%3<br /><i>%4</i>")
				.arg (script.Name ())
				.arg (script.Description ())
				.arg (tr ("runs on:"))
				.arg (script.Include ().join ("<br />"));

		if (!script.Exclude ().isEmpty ())
			scriptDesc.append (QString ("<br /><br />%1<br /><i>%2</i>")
					.arg (tr ("does not run on:"))
					.arg (script.Exclude ().join ("<br />")));
		Ui_.ScriptInfo_->setHtml (scriptDesc);
	}

	void UserScriptInstallerDialog::on_Install__released ()
	{
		done (Install);
	}

	void UserScriptInstallerDialog::on_ShowSource__released ()
	{
		done (ShowSource);
	}

	void UserScriptInstallerDialog::on_Cancel__released ()
	{
		done (Cancel);
	}

	QString UserScriptInstallerDialog::TempScriptPath () const
	{
		return TempScriptPath_;
	}
}
}
}

