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

#pragma once

#include <memory>
#include <QObject>
#include <QHash>
#include <interfaces/core/icoreproxy.h>
#include "xdgconfig.h"

namespace LC
{
namespace Util
{
namespace XDG
{
	class UTIL_XDG_API Item;
	using Item_ptr = std::shared_ptr<Item>;

	using Cat2Items_t = QHash<QString, QList<Item_ptr>>;

	enum class Type;

	/** @brief Finds and parses XDG <code>.desktop</code> files.
	 *
	 * The <code>.desktop</code> files are found in the directories
	 * matching the types passed to the object's constructor.
	 *
	 * Parsing is done asynchronously in a separate thread, and the
	 * itemsListChanged() signal is emitted each time the list of files
	 * changes.
	 *
	 * This class does not watch for changes in the said paths. Use the
	 * ItemsDatabase instead if that functionality is required.
	 *
	 * @sa ItemsDatabase
	 */
	class UTIL_XDG_API ItemsFinder : public QObject
	{
		Q_OBJECT

		ICoreProxy_ptr Proxy_;
		Cat2Items_t Items_;

		bool IsReady_ = false;
		bool IsScanning_ = false;

		const QList<Type> Types_;
	public:
		/** @brief Constructs the items finder for the given \em types.
		 *
		 * ToPaths() is used to get the list of directories for each of
		 * the \em types.
		 *
		 * The ItemsFinder will asynchronously update itself
		 * automatically a few moments after creation and emit
		 * itemsListChanged() when the update finishes.
		 *
		 * @param[in] proxy The proxy to use to get the icons of the
		 * items that were found.
		 * @param[in] types The item types to watch for.
		 * @param[in] parent The parent object of this finder.
		 */
		ItemsFinder (ICoreProxy_ptr, const QList<Type>& types, QObject *parent = nullptr);

		/** @brief Checks whether this items finder is ready.
		 *
		 * An items finder is ready if it has finished at least one search
		 * process. In other words, IsReady() returns true iff
		 * itemsListChanged() has been emitted at least once.
		 *
		 * @return Whether the items finder has finished at least one
		 * items search.
		 */
		bool IsReady () const;

		/** @brief Returns the categorized list of XDG items.
		 *
		 * The returned hash contains the mapping from XDG category ID to
		 * the list of items in that category. A single item can appear in
		 * multiple categories at the same time.
		 *
		 * If the finder is not ready (IsReady() returns false), this
		 * function returns an empty hash.
		 *
		 * @return The categorized list of items.
		 */
		Cat2Items_t GetItems () const;

		/** @brief Finds an XDG item for the given permanent ID.
		 *
		 * @param[in] permanentID The permanent ID of the item as returned
		 * by Item::GetPermanentID().
		 * @return The item matching the \em permanentID or a null pointer
		 * if there is no such item.
		 */
		Item_ptr FindItem (const QString& permanentID) const;
	public slots:
		/** @brief Updates the list of items.
		 *
		 * If IsReady() returns false, and thus no items are known to the
		 * finder yet, this function blocks and emits itemsListChanged()
		 * before returning.
		 *
		 * Otherwise, this function spawns an asynchronous update process.
		 */
		void update ();
	signals:
		/** @brief Notifies when the list of items changes in any way.
		 */
		void itemsListChanged ();
	};
}
}
}
