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

#include "server.h"
#include <fstream>
#include <map>
#include <iomanip>
#include <QDBusConnection>
#include <QDBusMetaType>
#include <QCoreApplication>
#include <QLocalSocket>
#include <QDir>
#include <QThread>
#include <QMutex>
#include <QDateTime>
#include <QtDebug>
#include "../sopluginloader.h"
#include "../../debugmessagehandler.h"
#include "marshalling.h"
#include "infoserverwrapper.h"

namespace LC
{
namespace DBus
{
	Server::Server ()
	: QObject ()
	{
		qInstallMessageHandler ([] (QtMsgType type, const QMessageLogContext& ctx, const QString& msg)
				{
					DebugHandler::Write (type, ctx, msg.toLocal8Bit ().constData (), DebugHandler::DWFNone);
				});

		const auto pid = QCoreApplication::applicationPid ();

		RegisterTypes ();

		auto sb = QDBusConnection::sessionBus ();
		const auto& serviceName = QString ("org.LeechCraft.Wrapper_%1").arg (pid);
		qDebug () << "registering service..." << sb.registerService (serviceName);
		qDebug () << "registering primary object..." << sb.registerObject ("/org/LeechCraft/Control",
				this, QDBusConnection::ExportAllContents);

		QLocalSocket socket;
		socket.connectToServer (QString ("lc_waiter_%1").arg (pid));
		qDebug () << "notifying master..." << socket.waitForConnected ();
	}

	bool Server::Load (const QString& path)
	{
		qDebug () << Q_FUNC_INFO << path;
		Loader_.reset (new Loaders::SOPluginLoader (path));
		if (!Loader_->Load ())
			return false;

		qDebug () << "done";
		QDBusConnection::sessionBus ().registerObject ("/org/LeechCraft/Plugin",
				Loader_->Instance (), QDBusConnection::ExportAllContents);

		if (auto ii = qobject_cast<IInfo*> (Loader_->Instance ()))
		{
			auto w = new InfoServerWrapper { ii };
			QDBusConnection::sessionBus ().registerObject ("/org/LeechCraft/Info",
					w, QDBusConnection::ExportAllContents);
		}

		return true;
	}

	bool Server::Unload (const QString&)
	{
		return Loader_->Unload ();
	}

	void Server::SetLcIconsPaths (const QStringList& paths)
	{
		for (const auto& path : paths)
			QDir::addSearchPath ("lcicons", path);
	}
}
}
