/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2010-2011  Oleg Linkin
 *
 * 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 "bookmarkeditwidget.h"
#include <QTextCodec>
#include <QtDebug>

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	BookmarkEditWidget::BookmarkEditWidget (QWidget *parent)
	: QWidget (parent)
	{
		Ui_.setupUi (this);
		Q_FOREACH (const QByteArray& codec,
				QTextCodec::availableCodecs ())
			Ui_.Encoding_->addItem (QString::fromUtf8 (codec));
		Ui_.Encoding_->model ()->sort (0);
	}

	QVariantMap BookmarkEditWidget::GetIdentifyingData () const
	{
		QVariantMap result;
		result ["HumanReadableName"] = QString ("%1@%2 (%3)")
			.arg (Ui_.Channel_->text ())
			.arg (Ui_.Server_->text ())
			.arg (Ui_.Nickname_->text ());
		result ["StoredName"] = Ui_.Name_->text ();
		result ["Server"] = Ui_.Server_->text ();
		result ["Port"] = Ui_.Port_->value ();
		result ["ServerPassword"] = Ui_.ServerPassword_->text ();
		result ["Encoding"] = Ui_.Encoding_->currentText ();

		QString channel = Ui_.Channel_->text ();
		if (!channel.startsWith ('#') &&
				!channel.startsWith ('&') &&
				!channel.startsWith ('+') &&
				!channel.startsWith ('!'))
			channel.prepend ('#');
		result ["Channel"] = channel;

		result ["Password"] = Ui_.Password_->text ();
		result ["Nickname"] = Ui_.Nickname_->text ();
		result ["SSL"] = Ui_.SSL_->checkState () == Qt::Checked;
		result ["Autojoin"] = Ui_.AutoJoin_->checkState () == Qt::Checked;
		return result;
	}

	void BookmarkEditWidget::SetIdentifyingData (const QVariantMap& map)
	{
		Ui_.HumanReadable_->setText (map ["HumanReadableName"].toString ());
		Ui_.Name_->setText (map ["StoredName"].toString ());
		Ui_.Server_->setText (map ["Server"].toString ());
		Ui_.Port_->setValue (map ["Port"].toInt ());
		Ui_.ServerPassword_->setText (map ["ServerPassword"].toString ());
		Ui_.Encoding_->setCurrentIndex (Ui_.Encoding_->
		findText (map ["Encoding"].toString ()));
		Ui_.Channel_->setText (map ["Channel"].toString ());
		Ui_.Password_->setText (map ["Password"].toString ());
		Ui_.Nickname_->setText (map ["Nickname"].toString ());
		Ui_.SSL_->setCheckState (map ["SSL"].toBool () ? Qt::Checked : Qt::Unchecked);
		Ui_.AutoJoin_->setCheckState (map ["Autojoin"].toBool () ? Qt::Checked : Qt::Unchecked);
	}
}
}
}

