/*****************************************************************

   Copyright (c) 2006 Stephan Binner <binner@kde.org>
   Copyright (C) 2006 Debajyoti Bera <dbera.web@gmail.com>

   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 of the License, 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.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.

******************************************************************/

#include "status.h"

#include <QDateTime>

#include <QLayout>
#include <QTimer>
#include <QVBoxLayout>
#include <QLabel>
#include <kdialog.h>
#include <klocale.h>
#include <kpassivepopup.h>
#include <k3process.h>
#include <kvbox.h>

extern "C" {
#include <glib.h>
#include <beagle/beagle.h>
}

KCMBeagleStatus::KCMBeagleStatus(const KComponentData &inst, QWidget *parent)
    : KCModule(inst, parent)
{
    QVBoxLayout* general_layout = new QVBoxLayout( this, KDialog::spacingHint() );

    KHBox *control_box = new KHBox (this);
    control_box->setSpacing (3);
    general_layout->addWidget(control_box);

    label_control = new QLabel (control_box);

    QWidget *dummy = new QWidget( control_box );
    control_box->setStretchFactor( dummy, 1 );

    pb_control = new KPushButton (control_box);
    connect (pb_control, SIGNAL (clicked ()), this, SLOT (controlPressed ()) );

    status_box = new Q3GroupBox (1, Qt::Horizontal, this);
    general_layout->addWidget(status_box);

    version_label = new	QLabel (status_box);

    status_area = new Q3TextEdit (status_box);
    status_area->setReadOnly (true);

    index_info_box = new Q3TextEdit (status_box);
    index_info_box->setReadOnly (true);

    KHBox *footer_box = new KHBox (this);
    general_layout->addWidget(footer_box);

    // Add some spacing to left
    dummy = new QWidget( footer_box );
    footer_box->setStretchFactor( dummy, 1 );
    pb_refresh = new KPushButton (i18n("Refresh Status"), footer_box);
    connect (pb_refresh, SIGNAL (clicked()), this, SLOT (refreshStatus ()) );

    g_type_init ();
    refreshStatus ();

    load();
}

KCMBeagleStatus::~KCMBeagleStatus()
{
}

void KCMBeagleStatus::load()
{
    load( false );
}

void KCMBeagleStatus::load( bool useDefaults )
{
     emit changed( useDefaults );
}

void KCMBeagleStatus::defaults()
{
//     if (!beagle_util_daemon_is_running ())
//         controlPressed();

     load( true );
}

void KCMBeagleStatus::save()
{

}

bool KCMBeagleStatus::refreshDaemonStatus ()
{
    gboolean is_running = beagle_util_daemon_is_running ();
    if (is_running) {
	label_control->setText (i18n("Beagle service is currently running. Click here to stop."));
	pb_control->setText (i18n("Stop"));
	last_status = true;
    } else {
	label_control->setText (i18n("Beagle service is currently stopped. Click here to start."));
	pb_control->setText (i18n("Start"));
	last_status = false;
    }
    return is_running;
}

void KCMBeagleStatus::refreshStatus ()
{
    pb_refresh->setDisabled (TRUE);
    bool is_running = refreshDaemonStatus ();

    status_box->setTitle ( QString ("[%1] ").arg (QDateTime::currentDateTime ().toString ()) );
    if (! is_running) {
	version_label->setText (i18n("Service not started."));
	pb_refresh->setDisabled (FALSE);
	status_area->clear ();
	index_info_box->clear ();
	return;
    }

    BeagleClient *client = beagle_client_new (NULL);
    BeagleDaemonInformationRequest *request = beagle_daemon_information_request_new ();
    BeagleResponse *response = beagle_client_send_request (client, BEAGLE_REQUEST (request), NULL);

    version_label->setText ( i18n ("Beagle service version: %1\n").arg (beagle_daemon_information_response_get_version (BEAGLE_DAEMON_INFORMATION_RESPONSE (response))));

    status_area->append (i18n("Current status:\n"));
    status_area->append (" "); // cheating
    status_area->append ( QString::fromUtf8(beagle_daemon_information_response_get_human_readable_status (BEAGLE_DAEMON_INFORMATION_RESPONSE (response))) );

    index_info_box->append (i18n("Index information:"));
    index_info_box->append (" ");
    index_info_box->append ( QString::fromUtf8(beagle_daemon_information_response_get_index_information (BEAGLE_DAEMON_INFORMATION_RESPONSE (response))) );

    g_object_unref (request);
    g_object_unref (response);
    //g_print ("%s\n", beagle_daemon_information_response_get_human_readable_status  (BEAGLE_DAEMON_INFORMATION_RESPONSE (response)));
    g_object_unref (client);

    pb_refresh->setDisabled (FALSE);
}

void KCMBeagleStatus::controlPressed ()
{
    pb_control->setDisabled (TRUE);
    if (last_status) {
	if (stopBeagle ())
	    QTimer::singleShot (1000, this, SLOT (verifyStatus ()));
    } else {
	if (startBeagle ())
	    QTimer::singleShot (5000, this, SLOT (verifyStatus ()));
    }
}

void KCMBeagleStatus::verifyStatus ()
{
    pb_control->setEnabled (TRUE);
    refreshDaemonStatus ();
}

bool KCMBeagleStatus::stopBeagle ()
{
    gboolean is_running = beagle_util_daemon_is_running ();
    if (! is_running) {
	KPassivePopup::message (i18n("Beagle service was already stopped."), this);
	return false;
    }

    BeagleClient *client = beagle_client_new (NULL);
    BeagleShutdownRequest *request;
    BeagleResponse *response;

    request = beagle_shutdown_request_new ();
    response = beagle_client_send_request (client, BEAGLE_REQUEST (request), NULL);
    g_object_unref (client);

    return true;
}

bool KCMBeagleStatus::startBeagle ()
{
    gboolean is_running = beagle_util_daemon_is_running ();
    if (is_running) {
	KPassivePopup::message (i18n("Beagle service already running."), this);
	return false;
    }

    K3Process *proc = new K3Process;
    *proc << "beagled";
    *proc << "--indexing-delay 2";
    if (!proc->start()) {
	KPassivePopup::message (i18n("Could not start beagle service."), this);
	return false;
    }

    return true;
}

#include "status.moc"
