/***************************************************************************
*                                transferlist.cpp
*                             -------------------
*
*    Revision     : $Id$
*    begin        : Tue Jan 29 2002
*    copyright    : (C) 2002 by Patrick Charbonnier
*                 : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
*    email        : pch@freeshell.org
*
****************************************************************************/

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

#include <tdeapplication.h>
#include <kstandarddirs.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <tdemessagebox.h>
#include <ksimpleconfig.h>
#include <tdeio/netaccess.h>

#include "transfer.h"
#include "transferlist.h"

#define NUM_COLS  9

static int defaultColumnWidth[] = {
                                      26,                         // PIXMAP
                                      160,                        // LOCAL FILENAME
                                      40,                         // RESUME
                                      60,                         // COUNT
                                      30,                         // PROGRESS
                                      65,                         // TOTAL
                                      70,                         // SPEED
                                      70,                         // REMAINING TIME
                                      450                         // URL
                                  };


TransferList::TransferList(TQWidget * parent, const char *name)
    : TDEListView(parent, name)
{
    // enable selection of more than one item
    setSelectionMode( TQListView::Extended );

    // // disable sorting and clicking on headers
    // setSorting( -1 );

    setAllColumnsShowFocus(true);

    lv_pixmap = addColumn(i18n("S"));
    lv_filename = addColumn(i18n("Local File Name"));
    lv_resume = addColumn(i18n("Resumed"));
    lv_count = addColumn(i18n("Count"));
    lv_progress = addColumn(i18n("%"));
    lv_total = addColumn(i18n("Total"));
    lv_speed = addColumn(i18n("Speed"));
    lv_remaining = addColumn(i18n("Rem. Time"));
    lv_url = addColumn(i18n("Address (URL)"));

    jobid=1;

    // initial layout
    TDEConfig *config = TDEGlobal::config();
    config->setGroup("ListView");
    if ( config->readListEntry("ColumnWidths").isEmpty() )
    {
        for (int i = 0; i < NUM_COLS; i++)
            setColumnWidth(i, defaultColumnWidth[i]);
    }
    else
        restoreLayout( TDEGlobal::config(), "ListView" );

    TQString connectPath = "pics/connect%2.png";
    TQString tryPath = "pics/try%2.png";

    // Load animations
    TQPixmap* curPix;
    if (animConn.count() == 0) {
        animConn.setAutoDelete(true);
        animTry.setAutoDelete(true);
        for (int i = 0; i < 8; i++) {
            curPix = new TQPixmap();
            curPix->load(locate("appdata", connectPath.arg(i)));
            animConn.append(curPix);

            curPix = new TQPixmap();
            curPix->load(locate("appdata", tryPath.arg(i)));
            animTry.append(curPix);
        }
    }

    pixQueued = UserIcon("md_queued");
    pixScheduled = UserIcon("md_scheduled");
    pixDelayed = UserIcon("md_delayed.png");
    pixFinished = UserIcon("md_finished");
    pixRetrying = UserIcon("retrying");

    phasesNum = animConn.count();

    connect(this, TQ_SIGNAL(doubleClicked(TQListViewItem *)), TQ_SLOT(slotTransferSelected(TQListViewItem *)));
    connect(this, TQ_SIGNAL(rightButtonPressed(TQListViewItem *, const TQPoint &, int)), TQ_SLOT(slotPopupMenu(TQListViewItem *)));
}


TransferList::~TransferList()
{
    saveLayout( TDEGlobal::config(), "ListView" );
}


Transfer *TransferList::addTransfer(const KURL & _source, const KURL & _dest,
                                    bool canShow)
{
    Transfer *last = static_cast<Transfer*>( lastItem() );
    Transfer *new_item = new Transfer(this, last, _source, _dest, jobid);
    jobid++;
    if ( canShow )
        new_item->maybeShow();

    return new_item;
}


void TransferList::slotTransferSelected(TQListViewItem * item)
{
    emit transferSelected((Transfer *) item);
}


void TransferList::slotPopupMenu(TQListViewItem * item)
{
    if (!item)
        return;
    emit popupMenu((Transfer *) item);
}


void TransferList::setSelected(TQListViewItem * item, bool selected)
{
    bool tmpb = selected;

    if (tmpb && item->isSelected()) {
        tmpb = false;
    }

    TQListView::setSelected(item, tmpb);
}


void TransferList::moveToBegin(Transfer * item)
{
    //        ASSERT(item);

    Transfer *oldfirst=static_cast<Transfer*>(firstChild());
    item->moveItem(oldfirst); //move item after oldfirst
    oldfirst->moveItem(item); //move oldfirst after item
}


void TransferList::moveToEnd(Transfer * item)
{
    //        ASSERT(item);

    Transfer *oldlast=static_cast<Transfer*>(lastItem());
    item->moveItem(oldlast);
}


bool TransferList::updateStatus(int counter)
{
    bool isTransfer = false;

    TransferIterator it(this);

    for (; it.current(); ++it) {
        isTransfer |= it.current()->updateStatus(counter);
    }

    return isTransfer;
}


bool TransferList::areTransfersQueuedOrScheduled()
{
    TransferIterator it(this);

    if (childCount() > 0) {
        for (; it.current(); ++it) {
            if ((it.current()->getMode() == Transfer::MD_QUEUED)|| \
                (it.current()->getMode() == Transfer::MD_SCHEDULED))
                return true;
        }
    }
    return false;
}


Transfer * TransferList::find(const KURL& _src)
{
    TransferIterator it(this);

    for (; it.current(); ++it) {
        if (it.current()->getSrc() == _src) {
            return it.current();
        }
    }

    return 0L;
}


void TransferList::readTransfers(const KURL& file)
{
    TQString tmpFile;

    if (TDEIO::NetAccess::download(file, tmpFile, (TQWidget*)parent())) {
        KSimpleConfig config(tmpFile);

        config.setGroup("Common");
        int num = config.readNumEntry("Count", 0);

        Transfer *item;
        KURL src, dest;

        for ( int i = 0; i < num; i++ )
        {
            TQString str;

            str.sprintf("Item%d", i);
            config.setGroup(str);

            src  = KURL::fromPathOrURL( config.readPathEntry("Source") );
            dest = KURL::fromPathOrURL( config.readPathEntry("Dest") );
            item = addTransfer( src, dest, false ); // don't show!

            if (!item->read(&config, i))
                delete item;
            else
            {
                // configuration read, now we know the status to determine
                // whether to show or not
                item->maybeShow();
            }
        }
    }
}

void TransferList::writeTransfers(const TQString& file)
{
    sDebug << ">>>>Entering with file =" << file << endl;

    KSimpleConfig config(file);
    int num = childCount();

    config.setGroup("Common");
    config.writeEntry("Count", num);

    TransferIterator it(this);

    for (int id = 0; it.current(); ++it, ++id)
        it.current()->write(&config, id);
    config.sync();

    sDebug << "<<<<Leaving" << endl;
}

#include "transferlist.moc"
