/**********************************************************************
 * 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 "privacylistsitemdialog.h"
#include "privacylistsmanager.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	PrivacyListsItemDialog::PrivacyListsItemDialog (QWidget *parent)
	: QDialog (parent)
	{
		Ui_.setupUi (this);
	}

	PrivacyListItem PrivacyListsItemDialog::GetItem () const
	{
		PrivacyListItem result;

		if (Ui_.Type_->currentIndex () == TNSubscription)
			result.SetValue (Ui_.Value_->itemData (Ui_.Value_->currentIndex ()).toString ());
		else
			result.SetValue (Ui_.Value_->currentText ());

		result.SetAction (Ui_.Action_->currentIndex () == ANAllow ?
					PrivacyListItem::Action::Allow :
					PrivacyListItem::Action::Deny);

		switch (Ui_.Type_->currentIndex ())
		{
		case TNJID:
			result.SetType (PrivacyListItem::Type::Jid);
			break;
		case TNGroup:
			result.SetType (PrivacyListItem::Type::Group);
			break;
		case TNSubscription:
			result.SetType (PrivacyListItem::Type::Subscription);
			break;
		}

		PrivacyListItem::StanzaTypes stanzas = PrivacyListItem::STNone;
		if (Ui_.StanzaMessage_->checkState () == Qt::Checked)
			stanzas |= PrivacyListItem::STMessage;
		if (Ui_.StanzaInPres_->checkState () == Qt::Checked)
			stanzas |= PrivacyListItem::STPresenceIn;
		if (Ui_.StanzaOutPres_->checkState () == Qt::Checked)
			stanzas |= PrivacyListItem::STPresenceOut;
		if (Ui_.StanzaIQ_->checkState () == Qt::Checked)
			stanzas |= PrivacyListItem::STIq;

		if (stanzas == PrivacyListItem::STNone)
			stanzas = PrivacyListItem::STAll;

		result.SetStanzaTypes (stanzas);

		return result;
	}

	void PrivacyListsItemDialog::SetItem (const PrivacyListItem& item)
	{
		Ui_.Action_->setCurrentIndex (item.GetAction () == PrivacyListItem::Action::Allow ?
					ANAllow :
					ANDeny);

		TypeNum index = TNJID;
		switch (item.GetType ())
		{
		case PrivacyListItem::Type::Jid:
			index = TNJID;
			break;
		case PrivacyListItem::Type::Group:
			index = TNGroup;
			break;
		case PrivacyListItem::Type::Subscription:
			index = TNSubscription;
			break;
		case PrivacyListItem::Type::None:
			break;
		}
		Ui_.Type_->setCurrentIndex (index);
		on_Type__currentIndexChanged (index);

		if (index == TNSubscription)
		{
			const int idx = Ui_.Value_->findData (item.GetValue ());
			if (idx >= 0)
				Ui_.Value_->setCurrentIndex (idx);
		}
		else
			Ui_.Value_->setEditText (item.GetValue ());

		const PrivacyListItem::StanzaTypes stanzas = item.GetStanzaTypes ();
		if (stanzas != PrivacyListItem::STAll)
		{
			if (stanzas & PrivacyListItem::STMessage)
				Ui_.StanzaMessage_->setCheckState (Qt::Checked);
			if (stanzas & PrivacyListItem::STIq)
				Ui_.StanzaIQ_->setCheckState (Qt::Checked);
			if (stanzas & PrivacyListItem::STPresenceIn)
				Ui_.StanzaInPres_->setCheckState (Qt::Checked);
			if (stanzas & PrivacyListItem::STPresenceOut)
				Ui_.StanzaOutPres_->setCheckState (Qt::Checked);
		}
	}

	void PrivacyListsItemDialog::on_Type__currentIndexChanged (int type)
	{
		Ui_.Value_->clear ();
		if (type == TNSubscription)
		{
			Ui_.Value_->setEditable (false);

			Ui_.Value_->addItem (tr ("Both"), "both");
			Ui_.Value_->addItem (tr ("To"), "to");
			Ui_.Value_->addItem (tr ("From"), "from");
			Ui_.Value_->addItem (tr ("None"), "none");
		}
		else
			Ui_.Value_->setEditable (true);
	}
}
}
}
