/**********************************************************************
 * 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 <QFlags>
#include <QMetaType>

class QObject;
class QIcon;

namespace LeechCraft
{
namespace Azoth
{
	class IAccount;
	class IProtocolPlugin;

	/** @brief Represents a protocol.
	 *
	 * IProtocol class represents a single protocol with its own set of
	 * accounts.
	 *
	 * A protocol may also implement IURIHandler if it supports handling
	 * of various URIs, like xmpp: for XMPP protocol.
	 *
	 * When user decides to add a new account within this protocol, the
	 * GetAccountRegistrationWidgets() function is called to get the
	 * list of widgets that should be filled out by the user by the
	 * exact values of his account, and if the user accepts the
	 * registration, RegisterAccount() would be called with those
	 * widgets.
	 *
	 * @sa IURIHandler
	 */
	class IProtocol
	{
	public:
		virtual ~IProtocol () {}

		/** This enum describes the features that may be supported by a
		 * protocol.
		 */
		enum ProtocolFeature
		{
			/** None of these features are supported by the protocol.
			 */
			PFNone = 0x0,

			/** Multiuser chats are possible in this proto.
			 */
			PFSupportsMUCs = 0x01,

			/** One could join MUCs as he wishes.
			 *
			 * If this feature is supported, a proper widget should be
			 * returned from the GetMUCJoinWidget() method.
			 */
			PFMUCsJoinable = 0x02,

			/** This protocol supports in-band registration: accounts
			 * could be registered right from the client.
			 *
			 * @sa IRegManagedAccount
			 */
			PFSupportsInBandRegistration = 0x04,

			/** This protocol doesn't support registering new accounts.
			 *
			 * This may be usable for dummy protocols for implementing
			 * things like metacontacts, for example.
			 */
			PFNoAccountRegistration = 0x08
		};

		Q_DECLARE_FLAGS (ProtocolFeatures, ProtocolFeature)

		/** This enum describes the options that may be selected by the
		 * user when adding a new account.
		 */
		enum AccountAddOption
		{
			/** No custom options.
			 */
			AAONoOptions = 0,

			/** User has chosen to register a new account (if the
			 * protocol advertises PFSupportsInBandRegistration
			 * feature).
			 */
			AAORegisterNewAccount = 0x01
		};

		Q_DECLARE_FLAGS (AccountAddOptions, AccountAddOption)

		/** @brief Returns the protocol object as a QObject.
		 *
		 * @return Protocol object as QObject.
		 */
		virtual QObject* GetQObject () = 0;

		/** Returns the list of features supported by this protocol.
		 */
		virtual ProtocolFeatures GetFeatures () const = 0;

		/** @brief Returns the accounts within this protocol.
		 *
		 * Each object in the returned list must implement IAccount.
		 *
		 * @return The list of accounts of this protocol.
		 */
		virtual QList<QObject*> GetRegisteredAccounts () = 0;

		/** @brief Returns the pointer to the parent protocol plugin
		 * that this protocol belongs to.
		 *
		 * @return The parent protocol plugin of this protocol, which
		 * must implement IProtocolPlugin.
		 */
		virtual QObject* GetParentProtocolPlugin () const = 0;

		/** @brief Returns the human-readable name of this protocol,
		 * like "Jabber" or "ICQ".
		 *
		 * @return Human-readable name of the protocol.
		 */
		virtual QString GetProtocolName () const = 0;

		/** @brief Returns the icon of this protocol.
		 *
		 * @return The icon of the protocol.
		 */
		virtual QIcon GetProtocolIcon () const = 0;

		/** @brief Returns the protocol ID, which must be unique among
		 * all the protocols.
		 *
		 * @return The unique ID of this protocol.
		 */
		virtual QByteArray GetProtocolID () const = 0;

		/** @brief Returns the widgets used for account addition.
		 *
		 * The widgets from the returned list are shown in the account
		 * addition wizard in the same order they appear in the returned
		 * list.
		 *
		 * If the user accepts registering the account, the
		 * RegisterAccount() method would be called with the same set of
		 * widgets in the same order as returned from this function.
		 *
		 * The ownership is transferred to the caller.
		 *
		 * @param[in] options The options selected by the user to
		 * perform the account addition.
		 *
		 * @return The widgets to be shown and filled by the user.
		 *
		 * @sa RegisterAccount()
		 */
		virtual QList<QWidget*> GetAccountRegistrationWidgets (AccountAddOptions options) = 0;

		/** @brief Adds an account with the given name and widgets.
		 *
		 * The list of widgets is the same (and in the same order) as
		 * returned from the GetAccountRegistrationWidgets() function.
		 * If this method is called, the widgets were shown to the user,
		 * and the user has accepted the account registration, so the
		 * widgets would be filled by the user to contain the required
		 * values. One could use qobject_cast to cast each widget in the
		 * list to the exact widget type and then use the values filled
		 * by the user.
		 *
		 * If the account is added successfully, the accountAdded()
		 * signal should be emitted, otherwise nothing should be done.
		 *
		 * @note Since ownership of the widgets is transferred to the
		 * caller, one shouldn't delete widgets in this method, and one
		 * shouldn't store them to use after this function has returned.
		 *
		 * @param[in] name The name of the account to be registered.
		 * @param[in] widgets The list of widgets returned from the
		 * GetAccountRegistrationWidgets() and filled by the user.
		 *
		 * @sa GetAccountRegistrationWidgets()
		 */
		virtual void RegisterAccount (const QString& name, const QList<QWidget*>& widgets) = 0;

		/** @brief Removes the given account.
		 *
		 * This function shouldn't ask anything from the user, just
		 * remove the account.
		 *
		 * If the account is not registered, this function should do
		 * nothing.
		 *
		 * After the removal, the accountRemoved() signal is expected to
		 * be emitted.
		 *
		 * @param[in] account The account to remove.
		 */
		virtual void RemoveAccount (QObject *account) = 0;

		/** @brief Notifies about new account.
		 *
		 * This signal should be emitted whenever a new account appears
		 * in this protocol.
		 *
		 * @note This function is expected to be a signal.
		 *
		 * @param[out] account The newly added account, which must
		 * implement IAccount.
		 */
		virtual void accountAdded (QObject *account) = 0;

		/** @brief Notifies about an account having been removed.
		 *
		 * This signal should be emitted whenever an already registered
		 * account is removed, for example, as the result of the call to
		 * RemoveAccount().
		 *
		 * @note This function is expected to be a signal.
		 *
		 * @param[out] account The just removed account, which must
		 * implement IAccount.
		 */
		virtual void accountRemoved (QObject *account) = 0;
	};
}
}

Q_DECLARE_METATYPE (LeechCraft::Azoth::IProtocol*)
Q_DECLARE_OPERATORS_FOR_FLAGS (LeechCraft::Azoth::IProtocol::ProtocolFeatures)
Q_DECLARE_INTERFACE (LeechCraft::Azoth::IProtocol,
		"org.Deviant.LeechCraft.Azoth.IProtocol/1.0")
