/**********************************************************************
 * 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 "xmppbobiq.h"
#include <QDomElement>

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	const QString NSBob = "urn:xmpp:bob";

	XMPPBobIq::XMPPBobIq (QXmppIq::Type type)
	: QXmppIq (type)
	, MaxAge_ (86400)
	{
	}

	QString XMPPBobIq::GetCid () const
	{
		return Cid_;
	}

	void XMPPBobIq::SetCid (const QString& cid)
	{
		Cid_ = cid;
	}

	QByteArray XMPPBobIq::GetData () const
	{
		return Data_;
	}

	void XMPPBobIq::SetData (const QByteArray& data)
	{
		Data_ = data;
	}

	QString XMPPBobIq::GetMimeType () const
	{
		return MimeType_;
	}

	void XMPPBobIq::SetMimeType (const QString& type)
	{
		MimeType_ = type;
	}

	int XMPPBobIq::GetMaxAge () const
	{
		return MaxAge_;
	}

	void XMPPBobIq::SetMaxAge (int maxAge)
	{
		MaxAge_ = maxAge;
	}

	bool XMPPBobIq::IsBobIq (const QDomElement& element)
	{
		const auto& dataElement = element.firstChildElement ("data");
		return dataElement.namespaceURI () == NSBob;
	}

	void XMPPBobIq::parseElementFromChild (const QDomElement& element)
	{
		const auto& dataElement = element.firstChildElement ("data");
		Cid_ = dataElement.attribute ("cid");
		MimeType_ = dataElement.attribute ("type");
		MaxAge_ = dataElement.attribute ("max-age", "-1").toInt ();
		Data_ = QByteArray::fromBase64 (dataElement.text ().toLatin1 ());
	}

	void XMPPBobIq::toXmlElementFromChild(QXmlStreamWriter *writer) const
	{
		writer->writeStartElement ("data");
		writer->writeAttribute ("xmlns", NSBob);
		writer->writeAttribute ("cid", Cid_);

		if (!MimeType_.isEmpty ())
			writer->writeAttribute ("type", MimeType_);

		if (MaxAge_ >= 0)
			writer->writeAttribute ("max-age", QString::number (MaxAge_));

		if (!Data_.isEmpty ())
			writer->writeCharacters (Data_.toBase64 ());

		writer->writeEndElement ();
	}
}
}
}
