/**********************************************************************
 * 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.
 **********************************************************************/

#ifndef PLUGINS_LACKMAN_REPOINFOFETCHER_H
#define PLUGINS_LACKMAN_REPOINFOFETCHER_H
#include <QObject>
#include <QUrl>
#include <QProcess>
#include <QHash>
#include <interfaces/idownload.h>
#include <interfaces/core/icoreproxyfwd.h>
#include "repoinfo.h"

namespace LC
{
namespace LackMan
{
	class RepoInfoFetcher : public QObject
	{
		Q_OBJECT

		const ICoreProxy_ptr Proxy_;

		struct ScheduledPackageFetch
		{
			QUrl BaseUrl_;
			QString PackageName_;
			QList<QString> NewVersions_;
			int ComponentId_;
		};
		QList<ScheduledPackageFetch> ScheduledPackages_;

	public:
		struct PendingPackage
		{
			QUrl URL_;
			QUrl BaseURL_;
			QString Location_;
			QString PackageName_;
			QList<QString> NewVersions_;
			int ComponentId_;
		};

		RepoInfoFetcher (const ICoreProxy_ptr& proxy, QObject*);

		void FetchFor (QUrl);
		void FetchComponent (QUrl, int, const QString& component);
		void ScheduleFetchPackageInfo (const QUrl& url,
				const QString& name,
				const QList<QString>& newVers,
				int componentId);
	private:
		void FetchPackageInfo (const QUrl& url,
				const QString& name,
				const QList<QString>& newVers,
				int componentId);

		void HandleRIFinished (const QString&, const QUrl&);
		void HandleComponentFinished (const QUrl&, const QString&, const QString&, int);
		void HandlePackageFinished (const PendingPackage&);
	private slots:
		void rotatePackageFetchQueue ();
	signals:
		void infoFetched (const RepoInfo&);
		void componentFetched (const PackageShortInfoList& packages,
				const QString& component, int repoId);
		void packageFetched (const PackageInfo&, int componentId);
	};
}
}

#endif
