/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-01-04
 * Description : a Digikam image editor plugin for superimpose a 
 *               template to an image.
 *
 * Copyright (C) 2005-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2006-2008 by Marcel Wiesweg <marcel dot wiesweg at gmx dot 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, 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.
 *
 * ============================================================ */

// TQt includes.

#include <tqlayout.h>
#include <tqheader.h>
#include <tqlistview.h>
#include <tqdir.h>

// KDE includes.

#include <klocale.h>

// Local includes.

#include "ddebug.h"
#include "dirselectwidget.h"
#include "dirselectwidget.moc"

namespace DigikamSuperImposeImagesPlugin
{

struct DirSelectWidget::Private
{
    KFileTreeBranch* m_item;
    TQStringList      m_pendingPath;
    TQString          m_handled;
    KURL             m_rootUrl;
};

DirSelectWidget::DirSelectWidget(TQWidget* parent, const char* name, TQString headerLabel)
               : KFileTreeView( parent, name)
{
    d = new Private;
    
    addColumn( headerLabel );
    
    if ( headerLabel.isNull() )
        header()->hide();
        
    setAlternateBackground(TQColor());
}

DirSelectWidget::DirSelectWidget(KURL rootUrl, KURL currentUrl, 
                                 TQWidget* parent, const char* name, TQString headerLabel)
               : KFileTreeView( parent, name)
{
    d = new Private;
    
    addColumn( headerLabel );
    
    if ( headerLabel.isNull() )
        header()->hide();
        
    setAlternateBackground(TQColor());
    setRootPath(rootUrl, currentUrl);
}

DirSelectWidget::~DirSelectWidget()
{
    delete d;
}

KURL DirSelectWidget::path() const
{
    return currentURL();
}

void DirSelectWidget::load()
{
    if ( d->m_pendingPath.isEmpty() ) 
    {
        disconnect( d->m_item, TQT_SIGNAL( populateFinished(KFileTreeViewItem *) ), 
                    this, TQT_SLOT( load() ) );
        
        emit folderItemSelected(currentURL());
        return;
    }

    TQString item = d->m_pendingPath.front();
    d->m_pendingPath.pop_front();
    d->m_handled += item;    
    KFileTreeViewItem* branch = findItem( d->m_item, d->m_handled );
    
    if ( !branch ) 
    {
        DDebug() << "Unable to open " << d->m_handled << endl;
    }
    else
    {
        branch->setOpen( true );
        setSelected( branch, true );
        ensureItemVisible ( branch );
        d->m_handled += '/';
        
        if ( branch->alreadyListed() )
            load();
    }
}

void DirSelectWidget::setCurrentPath(KURL currentUrl)
{
    if ( !currentUrl.isValid() )
       return;
    
    TQString currentPath = TQDir::cleanDirPath(currentUrl.path());
    currentPath = currentPath.mid( d->m_rootUrl.path().length() );
    d->m_pendingPath.clear();    
    d->m_handled = TQString("");
    d->m_pendingPath = TQStringList::split( "/", currentPath, true );
    
    if ( !d->m_pendingPath[0].isEmpty() )
        d->m_pendingPath.prepend( "" ); // ensure we open the root first.        
        
    connect( d->m_item, TQT_SIGNAL( populateFinished(KFileTreeViewItem *) ),
             this, TQT_SLOT( load() ) );
    load();
}

void DirSelectWidget::setRootPath(KURL rootUrl, KURL currentUrl)
{
    d->m_rootUrl = rootUrl;
    clear();
    TQString root = TQDir::cleanDirPath(rootUrl.path());
    
    if ( !root.endsWith("/"))
       root.append("/");
    
    TQString currentPath = TQDir::cleanDirPath(currentUrl.isValid() ? currentUrl.path() : root);
    
    d->m_item = addBranch( rootUrl, rootUrl.fileName() );    
    setDirOnlyMode( d->m_item, true );
    currentPath = currentPath.mid( root.length() );
    d->m_pendingPath = TQStringList::split( "/", currentPath, true );

    if ( !d->m_pendingPath[0].isEmpty() )
        d->m_pendingPath.prepend( "" ); // ensure we open the root first.
                    
    connect( d->m_item, TQT_SIGNAL( populateFinished(KFileTreeViewItem *) ),
             this, TQT_SLOT( load() ) );
    
    load();
    
    connect( this, TQT_SIGNAL( executed(TQListViewItem *) ),
             this, TQT_SLOT( slotFolderSelected(TQListViewItem *) ) );
}

KURL DirSelectWidget::rootPath(void)
{
    return d->m_rootUrl;
}

void DirSelectWidget::slotFolderSelected(TQListViewItem *)
{
    emit folderItemSelected(currentURL());
}

}   // NameSpace DigikamSuperImposeImagesPlugin

