/***************************************************************************
 *   Copyright (C) 2005-2007 by Rajko Albrecht                             *
 *   ral@alwins-world.de                                                   *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include "stopdlg.h"
#include "ccontextlistener.h"
#include "tdesvnsettings.h"
#include "stringhelper.h"

#include <tdeapplication.h>
#include <tdelocale.h>
#include <twin.h>
#include <tqtimer.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqwidgetlist.h>
#include <kprogress.h>
#include <kdebug.h>
#include <ktextbrowser.h>

StopDlg::StopDlg(TQObject*listener,TQWidget *parent, const char *name,const TQString&caption,const TQString&text)
 : KDialogBase(KDialogBase::Plain,caption,KDialogBase::Cancel, KDialogBase::Cancel,parent, name,true)
    ,m_Context(listener),m_MinDuration(1000),mCancelled(false),mShown(false),m_BarShown(false),
    cstack(0)
{
    KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
    m_lastLogLines = 0;
    m_lastLog = "";

    mShowTimer = new TQTimer(this);
    m_StopTick.start();
    showButton(KDialogBase::Close, false);
    mCancelText = actionButton(KDialogBase::Cancel)->text();

    TQFrame* mainWidget = plainPage();
    layout = new TQVBoxLayout(mainWidget, 10);
    mLabel = new TQLabel(text, mainWidget);
    layout->addWidget(mLabel);
    m_ProgressBar=new KProgress(15,mainWidget);
    m_ProgressBar->setCenterIndicator (false);
    m_ProgressBar->setTextEnabled(false);
    layout->addWidget(m_ProgressBar);
    m_NetBar = new KProgress(15,mainWidget);
    layout->addWidget(m_NetBar);

    mWait = false;
    m_LogWindow = 0;

    connect(mShowTimer, TQ_SIGNAL(timeout()), this, TQ_SLOT(slotAutoShow()));
    if (m_Context) {
        connect(m_Context,TQ_SIGNAL(tickProgress()),this,TQ_SLOT(slotTick()));
        connect(m_Context,TQ_SIGNAL(waitShow(bool)),this,TQ_SLOT(slotWait(bool)));
        connect(m_Context,TQ_SIGNAL(netProgress(long long int, long long int)),
                this,TQ_SLOT(slotNetProgres(long long int, long long int)));
        connect(this,TQ_SIGNAL(sigCancel(bool)),m_Context,TQ_SLOT(setCanceled(bool)));
    }
    mShowTimer->start(m_MinDuration, true);
    setMinimumSize(280,160);
    adjustSize();
}

void StopDlg::showEvent( TQShowEvent*)
{
    cstack = new CursorStack(TQt::BusyCursor);
}

void StopDlg::hideEvent(TQHideEvent*)
{
    delete cstack; cstack = 0;
}

void StopDlg::slotWait(bool how)
{
    mWait = how;
    if (mShown && mWait) {
        hide();
        mShown = false;
    }
}

StopDlg::~StopDlg()
{
    delete cstack;
}

void StopDlg::slotAutoShow()
{
    bool hasDialogs = false;
    TQWidget * w = kapp->activeModalWidget();
    if (w && w!=this && w!=parentWidget() ) {
        hasDialogs = true;
    }
    if (hasDialogs) {
        kdDebug()<<"Hide me! (" << caption() << ")" << endl;
        hide();
    }
    if (mShown||mWait||hasDialogs) {
        if (mWait) {
            //kdDebug() << "Waiting for show"<<endl;
            mShowTimer->start(m_MinDuration, true);
        }
        mShowTimer->start(m_MinDuration, true);
        return;
    }
    m_ProgressBar->hide();
    m_NetBar->hide();
    m_BarShown=false;
    m_netBarShown=false;
    show();
    kapp->processEvents();
    mShown = true;
    mShowTimer->start(m_MinDuration, true);
}

void StopDlg::slotCancel()
{
    mCancelled = true;
    emit sigCancel(true);
}

bool StopDlg::cancelld()
{
    return mCancelled;
}

void StopDlg::slotTick()
{
    if (m_StopTick.elapsed()>500) {
        if (!m_BarShown) {
            m_ProgressBar->show();
            m_BarShown=true;
        }
        if (m_ProgressBar->progress()==15) {
            m_ProgressBar->reset();
        } else {
            m_ProgressBar->setProgress(m_ProgressBar->progress()+1);
        }
        m_StopTick.restart();
        kapp->processEvents();
    }
}

void StopDlg::slotExtraMessage(const TQString&msg)
{
    ++m_lastLogLines;
    if (!m_LogWindow) {
        TQFrame* mainWidget = plainPage();
        m_LogWindow = new KTextBrowser(mainWidget);
        layout->addWidget(m_LogWindow);
        m_LogWindow->show();
        resize( TQSize(500, 400).expandedTo(minimumSizeHint()) );
    }
    if (m_lastLogLines >= Kdesvnsettings::self()->cmdline_log_minline() &&
        isHidden() ) {
        slotAutoShow();
    }
    m_LogWindow->append(msg);
    kapp->processEvents();
}

void StopDlg::slotNetProgres(long long int current, long long int max)
{
    if (m_StopTick.elapsed()>300||(m_BarShown&&!m_netBarShown)) {
        if (!m_netBarShown) {
            m_NetBar->show();
            m_netBarShown=true;
        }
        TQString s1 = helpers::ByteToString()(current);
        if (max > -1 && max != m_NetBar->totalSteps()) {
            TQString s2 = helpers::ByteToString()(max);
            m_NetBar->setFormat(i18n("%1 of %2").arg(s1).arg(s2));
            m_NetBar->setTotalSteps(max);
        }
        if (max == -1) {
            m_NetBar->setFormat(i18n("%1 transferred.").arg(s1));
            m_NetBar->setTotalSteps(current+1);
        }
        m_NetBar->setValue(current);
        m_StopTick.restart();
        kapp->processEvents();
    }
}

StopSimpleDlg::StopSimpleDlg(TQWidget *parent, const char *name,const TQString&caption,const TQString&text)
    : StopDlg(0,parent,name,caption,text),cancelld(false)
{
    connect(this,TQ_SIGNAL(sigCancel(bool)),this,TQ_SLOT(slotSimpleCancel(bool)));
}

void StopSimpleDlg::slotSimpleCancel(bool how)
{
    cancelld = how;
}

void StopSimpleDlg::makeCancel()
{
    slotSimpleCancel(true);
}

#include "stopdlg.moc"
