/**********************************************************************
 * 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 <QtDebug>
#include "rss10parser.h"

namespace LeechCraft
{
namespace Aggregator
{
	RSS10Parser::RSS10Parser ()
	{
	}
	
	RSS10Parser::~RSS10Parser ()
	{
	}
	
	RSS10Parser& RSS10Parser::Instance ()
	{
		static RSS10Parser inst;
		return inst;
	}
	
	bool RSS10Parser::CouldParse (const QDomDocument& doc) const
	{
		QDomElement root = doc.documentElement ();
		return root.tagName () == "RDF";
	}
	
	channels_container_t RSS10Parser::Parse (const QDomDocument& doc,
			const IDType_t& feedId) const
	{
		channels_container_t result;
	
		QMap<QString, Channel_ptr> item2Channel;
		QDomElement root = doc.documentElement ();
		QDomElement channelDescr = root.firstChildElement ("channel");
		while (!channelDescr.isNull ())
		{
			Channel_ptr channel (new Channel (feedId));
			channel->Title_ = channelDescr.firstChildElement ("title").text ().trimmed ();
			channel->Link_ = channelDescr.firstChildElement ("link").text ();
			channel->Description_ =
				channelDescr.firstChildElement ("description").text ();
			channel->PixmapURL_ =
				channelDescr.firstChildElement ("image")
				.firstChildElement ("url").text ();
			channel->LastBuild_ = GetDCDateTime (channelDescr);
	
			QDomElement itemsRoot = channelDescr.firstChildElement ("items");
			QDomNodeList seqs = itemsRoot.elementsByTagNameNS (RDF_, "Seq");
	
			channelDescr = channelDescr.nextSiblingElement ("channel");
	
			if (!seqs.size ())
				continue;
	
			QDomElement seqElem = seqs.at (0).toElement ();
			QDomNodeList lis = seqElem.elementsByTagNameNS (RDF_, "li");
			for (int i = 0; i < lis.size (); ++i)
				item2Channel [lis.at (i).toElement ().attribute ("resource")] = channel;
	
			result.push_back (channel);
		}
	
		QDomElement itemDescr = root.firstChildElement ("item");
		while (!itemDescr.isNull ())
		{
			QString about = itemDescr.attributeNS (RDF_, "about");
			if (item2Channel.contains (about))
			{
				Item_ptr item (new Item (item2Channel [about]->ChannelID_));
				item->Title_ = itemDescr.firstChildElement ("title").text ();
				item->Link_ = itemDescr.firstChildElement ("link").text ();
				item->Description_ = itemDescr.firstChildElement ("description").text ();
				GetDescription (itemDescr, item->Description_);
	
				item->Categories_ = GetAllCategories (itemDescr);
				item->Author_ = GetAuthor (itemDescr);
				item->PubDate_ = GetDCDateTime (itemDescr);
				item->Unread_ = true;
				item->NumComments_ = GetNumComments (itemDescr);
				item->CommentsLink_ = GetCommentsRSS (itemDescr);
				item->CommentsPageLink_ = GetCommentsLink (itemDescr);
				item->Enclosures_ = GetEncEnclosures (itemDescr, item->ItemID_);
				QPair<double, double> point = GetGeoPoint (itemDescr);
				item->Latitude_ = point.first;
				item->Longitude_ = point.second;
				if (item->Guid_.isEmpty ())
					item->Guid_ = "empty";
	
				item2Channel [about]->Items_.push_back (item);
			}
			itemDescr = itemDescr.nextSiblingElement ("item");
		}
	
		return result;
	}
}
}
