/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2012  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 "gacts.h"
#include <QIcon>
#include <util/util.h>
#include <qxtglobalshortcut.h>
#include <interfaces/entitytesthandleresult.h>

namespace LeechCraft
{
namespace GActs
{
	void Plugin::Init (ICoreProxy_ptr)
	{
		Util::InstallTranslator ("gacts");
	}

	void Plugin::SecondInit ()
	{
	}

	QByteArray Plugin::GetUniqueID () const
	{
		return "org.LeechCraft.GActs";
	}

	void Plugin::Release ()
	{
		qDeleteAll (RegisteredShortcuts_.values ());
		RegisteredShortcuts_.clear ();
	}

	QString Plugin::GetName () const
	{
		return "GActs";
	}

	QString Plugin::GetInfo () const
	{
		return tr ("Provides support for Global Actions registration for other LeechCraft plugins.");
	}

	QIcon Plugin::GetIcon () const
	{
		return QIcon ();
	}

	EntityTestHandleResult Plugin::CouldHandle (const Entity& e) const
	{
		const bool good = (e.Mime_ == "x-leechcraft/global-action-register" ||
					e.Mime_ == "x-leechcraft/global-action-unregister") &&
				e.Additional_.contains ("ActionID");
		return EntityTestHandleResult (good ?
					EntityTestHandleResult::PIdeal :
					EntityTestHandleResult::PNone);
	}

	void Plugin::Handle (Entity e)
	{
		const QByteArray& id = e.Additional_ ["ActionID"].toByteArray ();

		if (e.Mime_ == "x-leechcraft/global-action-unregister")
		{
			delete RegisteredShortcuts_.take (id);
			return;
		}

		const QKeySequence& seq = e.Additional_ ["Shortcut"].value<QKeySequence> ();
		if (seq.isEmpty ())
			return;

		if (RegisteredShortcuts_.contains (id))
		{
			RegisteredShortcuts_ [id]->setShortcut (seq);
			return;
		}

		QObject *receiver = e.Additional_ ["Receiver"].value<QObject*> ();
		if (!receiver)
			return;

		const QByteArray& method = e.Additional_ ["Method"].toByteArray ();
		if (method.isEmpty ())
			return;

		connect (receiver,
				SIGNAL (destroyed (QObject*)),
				this,
				SLOT (handleReceiverDeleted ()),
				Qt::UniqueConnection);

		QxtGlobalShortcut *sh = new QxtGlobalShortcut (seq, receiver);
		connect (sh,
				SIGNAL (activated ()),
				receiver,
				method);
		RegisteredShortcuts_ [id] = sh;
	}

	void Plugin::handleReceiverDeleted ()
	{
		for (auto i = RegisteredShortcuts_.begin (); i != RegisteredShortcuts_.end (); )
		{
			if ((*i)->parent () != sender ())
				++i;
			else
				i = RegisteredShortcuts_.erase (i);
		}
	}
}
}

LC_EXPORT_PLUGIN (leechcraft_gacts, LeechCraft::GActs::Plugin);
