/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.digikam.org
 *
 * Date        : 2013-11-18
 * Description : a kipi plugin to import/export images to Dropbox web service
 *
 * Copyright (C) 2013 by Pankaj Kumar <me at panks dot me>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * ============================================================ */

// To disable warnings under MSVC2008 about POSIX methods().
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif

#include "plugin_dropbox.h"

// C ANSI includes

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

// Qt includes

#include <QApplication>
#include <QAction>
#include <QStandardPaths>
#include <QDir>

// KDE includes

#include <kconfig.h>
#include <kactioncollection.h>
#include <kwindowsystem.h>
#include <kpluginfactory.h>
#include <klocalizedstring.h>

// Libkipi includes

#include <KIPI/Interface>

// Local includes

#include "kipiplugins_debug.h"
#include "dbwindow.h"

namespace KIPIDropboxPlugin
{

K_PLUGIN_FACTORY(DropboxFactory, registerPlugin<Plugin_Dropbox>(); )

Plugin_Dropbox::Plugin_Dropbox(QObject* const parent,const QVariantList& /*args*/)
    : Plugin(parent, "Dropbox")
{
    qCDebug(KIPIPLUGINS_LOG) << "Plugin_Dropbox Plugin Loaded";

    setUiBaseName("kipiplugin_dropboxui.rc");
    setupXML();
    
    m_actionExport = 0;
    m_dlgExport    = 0;
}

Plugin_Dropbox::~Plugin_Dropbox()
{
}

void Plugin_Dropbox::setup(QWidget* const widget)
{
    m_dlgExport = 0;

    Plugin::setup(widget);

    if(!interface())
    {
        qCCritical(KIPIPLUGINS_LOG) << "kipi interface is null";
        return;
    }

    setupActions();
}

void Plugin_Dropbox::setupActions()
{
    setDefaultCategory(ExportPlugin);// uncomment if import feature is added to google drive
    m_actionExport = new QAction(this);
    m_actionExport->setText(i18n("Export to &Dropbox..."));
    m_actionExport->setIcon(QIcon::fromTheme(QStringLiteral("kipi-dropbox")));
    m_actionExport->setShortcut(QKeySequence(Qt::ALT+Qt::SHIFT+Qt::CTRL+Qt::Key_D));

    connect(m_actionExport,SIGNAL(triggered(bool)),
            this,SLOT(slotExport()));

    addAction(QStringLiteral("dropboxexport"), m_actionExport);
}

void Plugin_Dropbox::slotExport()
{
    QString tmp = QStandardPaths::writableLocation(QStandardPaths::TempLocation)+ QStringLiteral("/")+ QStringLiteral("kipi-dropboxplugin-") + QString::number(getpid()) + QStringLiteral("/");
    QDir().mkpath(tmp);

    if (!m_dlgExport)
    {
        m_dlgExport = new DBWindow(tmp, QApplication::activeWindow());
    }
    else
    {
        if (m_dlgExport->isMinimized())
        {
            KWindowSystem::unminimizeWindow(m_dlgExport->winId());
        }

        KWindowSystem::activateWindow(m_dlgExport->winId());
    }

    m_dlgExport->reactivate();
}

} // namespace KIPIDropboxPlugin

#include "plugin_dropbox.moc"
