/**********************************************************************
 * 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 "flgetter.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QtDebug>
#include "account.h"
#include "authmanager.h"
#include "urls.h"
#include "common.h"

namespace LeechCraft
{
namespace NetStoreManager
{
namespace YandexDisk
{
	FLGetter::FLGetter (Account *acc)
	: ActorBase (acc)
	{
	}

	QNetworkReply* FLGetter::MakeRequest ()
	{
		emit statusChanged (tr ("Getting filelist..."));
		return Mgr_->get (A_->MakeRequest (QUrl ("http://narod.yandex.ru/disk/all/page1/?sort=cdate%20desc")));
	}

	void FLGetter::HandleReply (QNetworkReply *reply)
	{
		QString page = reply->readAll ();
		if (page.isEmpty ())
		{
			A_->GetAuthManager ()->	GetCookiesFor (A_->GetLogin (), A_->GetPassword (), true);
			return;
		}

		page.replace ("<wbr/>", "");

		QList<FLItem> items;

		QRegExp rx ("class=\"\\S+icon\\s(\\S+)\"[^<]+<img[^<]+</i[^<]+</td[^<]+<td[^<]+<input[^v]+value=\"(\\d+)\" data-token=\"(\\S+)\""
				"[^<]+</td[^<]+<td[^<]+<span\\sclass='b-fname'><a\\shref=\"(\\S+)\">([^<]+)</a>.*"
				"<td class=\"size\">(\\S+)</td>.*data-token=\"(\\S+)\".*<i class=\"([^\"]+)\".*"
				"<td class=\"date prolongate\"><nobr>(\\S+ \\S+)</nobr></td>");
		rx.setMinimal (true);
		int cpos = rx.indexIn (page);
		while (cpos != -1)
		{
			FLItem item =
			{
				QString::fromUtf8 (rx.cap (5).toLatin1 ()),
				rx.cap (2),
				rx.cap (3),
				rx.cap (4),
				GetIconForType (rx.cap (1)),
				QString::fromUtf8 (rx.cap (6).toLatin1 ()).replace ("&nbsp;", " "),
				rx.cap (7),
				QString::fromUtf8 (rx.cap (9).toLatin1 ()),
				rx.cap (8) == "b-old-icon b-old-icon-pwd-on"
			};

			cpos = rx.indexIn (page, cpos + 1);

			items << item;
		}
		emit gotFiles (items);

		QRegExp rxnp ("<a\\sid=\"next_page\"\\shref=\"([^\"]+)\"");
		cpos = rxnp.indexIn (page);
		if (cpos > 0 && !rxnp.cap (1).isEmpty ())
		{
			QNetworkRequest nr = A_->MakeRequest ("http://narod.yandex.ru"+rxnp.cap(1));
			Mgr_->get (nr);
		}
		else
		{
			emit statusChanged (tr ("Filelist downloaded"));
			emit finished ();
		}
	}
}
}
}
