/**********************************************************************
 * 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 <functional>
#include <QObject>
#include <interfaces/structures.h>
#include "xpcconfig.h"

namespace LeechCraft
{
namespace Util
{
	/** @brief Aids in adding actions to notifications.
	 *
	 * This class is used to easily add actions to notification entities
	 * created via MakeNotification() or MakeAN() functions. The usage is
	 * quite simple and best illustrated by an example:
	 * \code
	 * Entity e = Util::MakeNotification ("Header", "Title", PInfo_);
	 * auto nah = new Util::NotificationActionHandler (e);
	 * nah->AddFunction ("Open file", [fileURL] () { QDesktopServices::openUrl (fileURL); });
	 * \endcode
	 *
	 * The entity this handler is created upon takes ownership of the
	 * object, so when the last instance of the entity is destroyed this
	 * object is destroyed itself. The object should \em never be
	 * explicitly deleted.
	 *
	 * Some actions don't make sense if some other object is destroyed
	 * after emitting the entity but before the user has reacted to the
	 * event. For example, a user in an IRC channel writes to us a
	 * message then leaves. In this case the "Reply" action will be
	 * invalid and useless.
	 *
	 * To avoid this NotificationActionHandler supports dependent objects
	 * — objects whose destruction will render its actions useless.
	 * Dependent objects are added via the AddDependentObject() method
	 * and their lifetime is tracked automatically.
	 *
	 * @note Only one handler can be created on an entity. If multiple
	 * handlers are created, the last one is used.
	 *
	 * @sa MakeNotification(), MakeAN().
	 */
	class NotificationActionHandler : public QObject
	{
		Q_OBJECT

		Entity& Entity_;
	public:
		/** @brief Type of functions used as actions in the handler.
		 */
		typedef std::function<void ()> Callback_t;
	private:
		QList<QPair<QString, Callback_t>> ActionName2Callback_;

		QList<QPointer<QObject>> DependentObjects_;
	public:
		/** @brief Creates the handler on the given \em entity.
		 *
		 * The entity takes the ownership of the handler.
		 *
		 * @param[in] entity The entity to add actions to.
		 */
		UTIL_XPC_API NotificationActionHandler (Entity& entity, QObject* = 0);

		/** @brief Adds an action with the given name.
		 *
		 * This function adds an action to this entity. It will be shown
		 * under the given \em name to the user, and when the user
		 * selects it, \em action will be invoked.
		 *
		 * @param[in] name The human-readable name of the action.
		 * @param[in] action The functor to invoke when the user selects
		 * the action.
		 */
		UTIL_XPC_API void AddFunction (const QString& name, Callback_t action);

		/** @brief Adds an object as a dependent object.
		 *
		 * @param[in] object The object actions in this handler depend upon.
		 */
		UTIL_XPC_API void AddDependentObject (QObject *object);
	public slots:
		void notificationActionTriggered (int);
	};
}
}
