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


#include "sendtoplugin.h"

#include <KTelepathy/TelepathyBridge>
#include <KTelepathy/contacts-modelmenu.h>
#include <KTelepathy/OfferFileTransferJob>
#include "personcontact.h"
#include "imaccount.h"

#include <konq_popupmenu.h>
#include <konq_popupmenuinformation.h>
#include <KDebug>
#include <KPluginFactory>
#include <kjobtrackerinterface.h>

static ContactsModelMenu* s_ContactsMenu = 0;

SendToPlugin::SendToPlugin( QObject* parent, const QVariantList & )
    : KonqPopupMenuPlugin(parent)
{
    kDebug();
    TelepathyBridge::instance()->init();
    if(!s_ContactsMenu)
        s_ContactsMenu = new ContactsModelMenu(i18n("Send to"));
}

void SendToPlugin::setup( KActionCollection* actionCollection, const KonqPopupMenuInformation& information, QMenu* menu )
{
    kDebug();
    Q_UNUSED(actionCollection)

    KUrl url = information.urlList().first();
    s_ContactsMenu->setData(url.url());

    connect(s_ContactsMenu, SIGNAL(activated(Nepomuk::PersonContact&, QVariant&)),
            this, SLOT(onContactsMenuActivated(Nepomuk::PersonContact&, QVariant&)));
    menu->addMenu(s_ContactsMenu);
}

SendToPlugin::~SendToPlugin ()
{
    kDebug();
}

void SendToPlugin::onContactsMenuActivated(Nepomuk::PersonContact& contact, QVariant& data)
{
    KUrl url(data.toString());
    kDebug() << contact.iMAccounts().first().imIDs().first() << url << url.path();

//    OfferFileTransferJob* job = new OfferFileTransferJob(contact, url.path(), this);
    OfferFileTransferJob* job = dynamic_cast<OfferFileTransferJob*>(TelepathyBridge::instance()->offerFileTransfer(contact, url.path()));
    if (job) {
        connect( job,  SIGNAL(infoMessage(KJob*,QString,QString)),
                 this, SLOT(onOfferFileTransferJobInfoMessage(KJob*,QString,QString)));
        connect( job,  SIGNAL(result(KJob*)),
                 this, SLOT  (handleOfferFileTransferJobResult(KJob*)));
        KIO::getJobTracker()->registerJob(job);
        kDebug() << i18n("Starting job");
        job->start();
    }
    else
        kDebug() << i18n("Cannot create job");
}

void SendToPlugin::onOfferFileTransferJobInfoMessage(KJob* job, const QString &plain, const QString &rich)
{
    Q_UNUSED(job)
    Q_UNUSED(rich)
    kDebug() << plain;
}

void SendToPlugin::handleOfferFileTransferJobResult(KJob* job)
{
    if ( job->error() ) {
        kWarning() << i18n("File transfer failed:") << job->errorString();
        return;
    }
    kDebug()  << i18n("File transfer succedeed");
}

K_PLUGIN_FACTORY(sendToPluginFactory, registerPlugin<SendToPlugin>();)
K_EXPORT_PLUGIN(sendToPluginFactory("sendtoplugin"))
