/*
* Copyright (C) 2010 Daniele E. Domenichelli <daniele.domenichelli@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

extern "C"
{
#include <signal.h>
}

#include <KAboutData>
#include <KCmdLineArgs>
#include <KApplication>
#include <KMessageBox>
#include <Nepomuk/ResourceManager>
#include <Nepomuk/Query/QueryServiceClient>
#include <TelepathyQt4/Debug>
#include <TelepathyQt4/ClientRegistrar>
#include <KTelepathy/TelepathyBridge>

#include "p2pdbustube-handler.h"
//#include "p2pdbustube-approver.h"

#include <KDebug>

namespace
{
    static void signal_handler(int signal)
    {
        if ((signal == SIGTERM) || (signal == SIGINT)) {
            QCoreApplication * const app(QCoreApplication::instance());
            if (app != 0) {
                kDebug() << "Signal Handler Called. Exiting...";
                app->quit();
            }
        }
    }
}

int main(int argc, char** argv)
{
    KAboutData aboutData("telepathy_test_p2pdbustube", 0, ki18n("Test P2PDBusTube"), "0.1");

    KCmdLineArgs::init(argc, argv, &aboutData);
    KApplication app;

    Nepomuk::ResourceManager::instance()->init();

    // Set up signal handlers.
    if (signal(SIGINT, signal_handler) == SIG_ERR) {
        kWarning() << "Setting up SIGINT signal handler failed.";
    }

    if (signal(SIGTERM, signal_handler) == SIG_ERR) {
        kWarning() << "Setting up SIGTERM signal handler failed.";
    }

    // Enable telepathy-Qt4 debug
//    Tp::enableDebug(true);
    Tp::enableWarnings(true);

    // Register telepathy types
    Tp::registerTypes();

    // Check if Nepomuk Query service client is up and running
    if (!Nepomuk::Query::QueryServiceClient::serviceAvailable()) {
        // That's a failure
        KMessageBox::error(0, i18n("The Nepomuk Query Service client is not available on your system. "
                                    "Contactlist requires the query service client to be available: please "
                                    "check your system settings"));
        app.quit();
    }

    // Initialize telepathy bridge
    KTelepathy::TelepathyBridge::instance()->init();

    // Create the approver and register it
//    Tp::SharedPtr<Test::P2PDBusTubeApprover> approver = Tp::SharedPtr<Test::P2PDBusTubeApprover>(new Test::P2PDBusTubeApprover(&app));
//    Tp::ClientRegistrarPtr registrar = Tp::ClientRegistrar::create();
//    registrar->registerClient(Tp::AbstractClientPtr::dynamicCast(approver), "telepathy_test_p2pdbustube");

    // Create the handler and register it
    Tp::SharedPtr<Test::P2PDBusTubeHandler> handler = Tp::SharedPtr<Test::P2PDBusTubeHandler>(new Test::P2PDBusTubeHandler(&app));
    Tp::ClientRegistrarPtr registrar = Tp::ClientRegistrar::create();
    registrar->registerClient(Tp::AbstractClientPtr::dynamicCast(handler), "telepathy_test_p2pdbustube");

    // Start event loop.
    return app.exec();
}
