/**********************************************************************
 * 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 "flashonclickwhitelist.h"
#include <QCoreApplication>
#include <QStandardItemModel>
#include <QSettings>
#include <QInputDialog>
#include <QMessageBox>
#include <QtDebug>

namespace LeechCraft
{
namespace Poshuku
{
namespace CleanWeb
{
	FlashOnClickWhitelist::FlashOnClickWhitelist (QWidget *parent)
	: QWidget (parent)
	, Model_ (new QStandardItemModel (this))
	{
		Model_->setHorizontalHeaderLabels (QStringList (tr ("Whitelist")));

		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_CleanWeb");
		settings.beginGroup ("FlashOnClick");
		int size = settings.beginReadArray ("Whitelist");
		for (int i = 0; i < size; ++i)
		{
			settings.setArrayIndex (i);
			Model_->appendRow (new QStandardItem (settings
						.value ("Exception").toString ()));
		}
		settings.endArray ();
		settings.endGroup ();

		Ui_.setupUi (this);
		Ui_.WhitelistTree_->setModel (Model_);
	}

	QStringList FlashOnClickWhitelist::GetWhitelist () const
	{
		QStringList result;
		for (int i = 0, rowCount = Model_->rowCount (); i < rowCount; ++i)
			result << Model_->item (i)->text ();
		return result;
	}

	bool FlashOnClickWhitelist::Matches (const QString& str) const
	{
		Q_FOREACH (QString white, GetWhitelist ())
		{
			if (str.indexOf (white) >= 0 ||
					str.indexOf (QRegExp (white)) >= 0)
				return true;
		}
		return false;
	}

	void FlashOnClickWhitelist::Add (const QString& str)
	{
		AddImpl (str);
	}

	void FlashOnClickWhitelist::on_Add__released ()
	{
		AddImpl ();
	}

	void FlashOnClickWhitelist::on_Modify__released ()
	{
		QModelIndex index = Ui_.WhitelistTree_->currentIndex ();
		if (!index.isValid ())
			return;

		QString str = Model_->itemFromIndex (index)->text ();
		AddImpl (str, index);
	}

	void FlashOnClickWhitelist::on_Remove__released ()
	{
		QModelIndex index = Ui_.WhitelistTree_->currentIndex ();
		if (!index.isValid ())
			return;

		qDeleteAll (Model_->takeRow (index.row ()));
		SaveSettings ();
	}

	void FlashOnClickWhitelist::AddImpl (QString str, const QModelIndex& old)
	{
		bool ok = false;
		str = QInputDialog::getText (this,
				tr ("Add URL to whitelist"),
				tr ("Please enter the URL to add to the FlashOnClick's whitelist"),
				QLineEdit::Normal,
				str,
				&ok);
		if (str.isEmpty () ||
				!ok)
			return;

		if (old.isValid ())
			qDeleteAll (Model_->takeRow (old.row ()));

		if (Matches (str))
		{
			QMessageBox::warning (this,
					"LeechCraft",
					tr ("This URL is already matched by another whitelist entry."));
			return;
		}

		Model_->appendRow (new QStandardItem (str));
		SaveSettings ();
	}

	void FlashOnClickWhitelist::SaveSettings ()
	{
		QSettings settings (QCoreApplication::organizationName (),
				QCoreApplication::applicationName () + "_CleanWeb");
		settings.beginGroup ("FlashOnClick");
		settings.beginWriteArray ("Whitelist");
		settings.remove ("");
		for (int i = 0, rowCount = Model_->rowCount (); i < rowCount; ++i)
		{
			settings.setArrayIndex (i);
			settings.setValue ("Exception", Model_->item (i)->text ());
		}
		settings.endArray ();
		settings.endGroup ();
	}
}
}
}
