/**********************************************************************
 * 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 "ircerrorhandler.h"
#include <QTextCodec>
#include <util/xpc/util.h>
#include <util/xpc/notificationactionhandler.h>
#include "core.h"
#include "ircserverhandler.h"

namespace LeechCraft
{
namespace Azoth
{
namespace Acetamide
{
	IrcErrorHandler::IrcErrorHandler (IrcServerHandler *ish)
	: QObject (ish)
	, ISH_ (ish)
	{
		InitErrors ();
	}

	void IrcErrorHandler::HandleError (const IrcMessageOptions& options)
	{
		if (!IsError (options.Command_.toInt ()))
			return;

		QString msg, paramsMessage = QString ();
		QTextCodec *codec = QTextCodec::codecForName (ISH_->GetServerOptions ()
				.ServerEncoding_.toUtf8 ());
		msg = codec->toUnicode (options.Message_.toAscii ());

		if (options.Parameters_.count () > 1)
			Q_FOREACH (const std::string& str, options.Parameters_.mid (1))
				paramsMessage += QString::fromUtf8 (str.c_str ()) + " ";

		Entity e = Util::MakeNotification ("Azoth",
				paramsMessage.isEmpty () ? msg : (paramsMessage + ": " + msg),
				PWarning_);
		Core::Instance ().SendEntity (e);
	}

	bool IrcErrorHandler::IsError (int id)
	{
		return ErrorKeys_.contains (id);
	}

	void IrcErrorHandler::InitErrors ()
	{
		ErrorKeys_ << 401;
		ErrorKeys_ << 402;
		ErrorKeys_ << 403;
		ErrorKeys_ << 404;
		ErrorKeys_ << 405;
		ErrorKeys_ << 406;
		ErrorKeys_ << 407;
		ErrorKeys_ << 408;
		ErrorKeys_ << 409;
		ErrorKeys_ << 411;
		ErrorKeys_ << 412;
		ErrorKeys_ << 413;
		ErrorKeys_ << 414;
		ErrorKeys_ << 415;
		ErrorKeys_ << 421;
		ErrorKeys_ << 422;
		ErrorKeys_ << 424;
		ErrorKeys_ << 431;
		ErrorKeys_ << 432;
		ErrorKeys_ << 433;
		ErrorKeys_ << 436;
		ErrorKeys_ << 437;
		ErrorKeys_ << 441;
		ErrorKeys_ << 442;
		ErrorKeys_ << 443;
		ErrorKeys_ << 444;
		ErrorKeys_ << 445;
		ErrorKeys_ << 446;
		ErrorKeys_ << 451;
		ErrorKeys_ << 461;
		ErrorKeys_ << 462;
		ErrorKeys_ << 463;
		ErrorKeys_ << 464;
		ErrorKeys_ << 465;
		ErrorKeys_ << 466;
		ErrorKeys_ << 467;
		ErrorKeys_ << 471;
		ErrorKeys_ << 472;
		ErrorKeys_ << 473;
		ErrorKeys_ << 474;
		ErrorKeys_ << 475;
		ErrorKeys_ << 476;
		ErrorKeys_ << 477;
		ErrorKeys_ << 478;
		ErrorKeys_ << 481;
		ErrorKeys_ << 482;
		ErrorKeys_ << 483;
		ErrorKeys_ << 484;
		ErrorKeys_ << 485;
		ErrorKeys_ << 491;
		ErrorKeys_ << 501;
		ErrorKeys_ << 502;
	}
}
}
}
