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

namespace LeechCraft
{
namespace Azoth
{
namespace Xoox
{
	const QString NsTuneNode = "http://jabber.org/protocol/tune";

	QString UserTune::GetNodeString ()
	{
		return NsTuneNode;
	}

	UserTune::UserTune ()
	: Length_ (0)
	, Rating_ (0)
	{
	}

	QXmppElement UserTune::ToXML () const
	{
		QXmppElement result;
		result.setTagName ("item");

		QXmppElement tune;
		tune.setTagName ("tune");
		tune.setAttribute ("xmlns", NsTuneNode);

		auto appendTxt = [&tune] (const QString& tag, const QString& str)
		{
			if (str.isEmpty ())
				return;

			QXmppElement elem;
			elem.setTagName (tag);
			elem.setValue (str);
			tune.appendChild (elem);
		};

		appendTxt ("artist", Artist_);
		appendTxt ("source", Source_);
		appendTxt ("title", Title_);
		appendTxt ("track", Track_);
		appendTxt ("uri", URI_.toEncoded ());
		if (Length_)
			appendTxt ("length", QString::number (Length_));
		if (Rating_)
			appendTxt ("rating", QString::number (Rating_));

		result.appendChild (tune);

		return result;
	}

	void UserTune::Parse (const QDomElement& elem)
	{
		QDomElement tune = elem.firstChildElement ("tune");
		if (tune.namespaceURI () != NsTuneNode)
			return;

		Artist_ = tune.firstChildElement ("artist").text ();
		Source_ = tune.firstChildElement ("source").text ();
		Title_ = tune.firstChildElement ("title").text ();
		Track_ = tune.firstChildElement ("track").text ();
		URI_ = QUrl::fromEncoded (tune.firstChildElement ("uri").text ().toUtf8 ());
		Length_ = tune.firstChildElement ("length").text ().toInt ();
		Rating_ = tune.firstChildElement ("rating").text ().toInt ();
	}

	QString UserTune::Node () const
	{
		return GetNodeString ();
	}

	PEPEventBase* UserTune::Clone () const
	{
		return new UserTune (*this);
	}

	QString UserTune::GetArtist () const
	{
		return Artist_;
	}

	void UserTune::SetArtist (const QString& artist)
	{
		Artist_ = artist;
	}

	QString UserTune::GetSource () const
	{
		return Source_;
	}

	void UserTune::SetSource (const QString& source)
	{
		Source_ = source;
	}

	QString UserTune::GetTitle () const
	{
		return Title_;
	}

	void UserTune::SetTitle (const QString& title)
	{
		Title_ = title;
	}

	QString UserTune::GetTrack () const
	{
		return Track_;
	}

	void UserTune::SetTrack (const QString& track)
	{
		Track_ = track;
	}

	QUrl UserTune::GetURI () const
	{
		return URI_;
	}

	void UserTune::SetURI (const QUrl& uri)
	{
		URI_ = uri;
	}

	int UserTune::GetLength () const
	{
		return Length_;
	}

	void UserTune::SetLength (int length)
	{
		Length_ = length;
	}

	int UserTune::GetRating () const
	{
		return Rating_;
	}

	void UserTune::SetRating (int rating)
	{
		Rating_ = rating;
	}

	bool UserTune::IsNull () const
	{
		return Artist_.isEmpty () &&
				Title_.isEmpty () &&
				Track_.isEmpty () &&
				Source_.isEmpty ();
	}
}
}
}
