/***************************************************************************
 *   Copyright (C) 2005 by James Thorniley                                 *
 *   james@uncommonlygood.co.uk                                            *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "burndatacdconfigwidget.h"
#include <k3bdatadoc.h>
#include <kstandarddirs.h>
#include <kdirlister.h>
#include <kfileitem.h>
#include <qlabel.h>
#include <kdebug.h>
#include <qlayout.h>
#include <klocale.h>
#include <k3bglobals.h>
#include <qcheckbox.h>
#include <k3bisooptions.h>
#include <qstyle.h>
#include <ktempdir.h>

#include <sys/types.h>
#include <unistd.h>

#include "burnsidebarplugin.h"
BurnDataCDConfigWidget::BurnDataCDConfigWidget(BurnSidebarPlugin *plugin, QWidget *parent, const char *name, const QString &configGroup)
 : QFrame(parent, name),
   m_plugin(plugin)
{
  setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
  setLineWidth( style().pixelMetric(QStyle::PM_DefaultFrameWidth, this) );
  
  m_sizeLabel = new QLabel(this);
  m_sizeLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
  m_dataCDLabel = new QLabel( i18n( "Data CD" ), this );
  m_dataCDLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );

  m_jolietCheckBox = new QCheckBox( this );
  m_jolietCheckBox->setText( i18n( "Use Microsoft Windows\ncompatible long filenames\n(64 character maximum)" ) );

  m_configGroup = new KConfigGroup( BurnSidebarPlugin::config(), configGroup );
  m_isoOptions = new K3bIsoOptions( K3bIsoOptions::load( m_configGroup ) ); 

  m_jolietCheckBox->setChecked( m_isoOptions->createJoliet() );

  connect( m_jolietCheckBox, SIGNAL( toggled( bool ) ),
           this, SLOT( slotJolietChecked( bool ) ) );

  m_vLayout = new QVBoxLayout( this );
  m_vLayout->addStretch();
  m_vLayout->addWidget( m_dataCDLabel );
  m_vLayout->addWidget( m_sizeLabel );
  m_vLayout->addWidget( m_jolietCheckBox );
  m_vLayout->addStretch();
  m_vLayout->setMargin(5);

  m_totalSize = 0;

  if( m_plugin->dirLister()->isFinished() )
    initSize();
  else
    connect( m_plugin->dirLister(), SIGNAL( completed() ),
             this, SLOT( initSize() ) );
}

void BurnDataCDConfigWidget::slotJolietChecked( bool checked )
{
  m_isoOptions->setCreateJoliet( checked );
}
void BurnDataCDConfigWidget::initSize()
{
  KFileItemList items = m_plugin->dirLister()->items() ;
  
  for( KFileItem *item = items.first(); item; item = items.next() )
  {
    
    if( item->isDir() )
      slotNewItems( m_plugin->dirLister()->itemsForDir(item->url()) );
    m_totalSize += item->size();
  }

  m_sizeLabel->setText( i18n("Size: %1 MB").arg(QString::number( m_totalSize/(1024*1024) )) );
  disconnect( m_plugin->dirLister(), SIGNAL( completed() ) , this, SLOT( initSize() ) );
  
  connect( m_plugin->dirLister(), SIGNAL( newItems(const KFileItemList &) ),
           this, SLOT( slotNewItems(const KFileItemList&) ) );
  connect( m_plugin->dirLister(), SIGNAL( deleteItem( KFileItem * ) ),
           this, SLOT( slotDeleteItem( KFileItem * ) ) );
}

void BurnDataCDConfigWidget::slotNewItems( const KFileItemList &newItems )
{
  KFileItemList items = newItems ;
  
  for( KFileItem *item = items.first(); item; item = items.next() )
  {
    m_totalSize += item->size();

    if( item->isDir() )
    {
      slotNewItems( m_plugin->dirLister()->itemsForDir( item->url() ) );
    }
  }
  m_sizeLabel->setText( i18n("Size: %1 MB").arg(QString::number( m_totalSize/(1024*1024) )) );
}

void BurnDataCDConfigWidget::slotDeleteItem( KFileItem *item  )
{

  //the user should never delete the data directory -
  //if they do it is likely to cause konq to crash
  Q_ASSERT( item != m_plugin->dirLister()->rootItem() );

  if( item->isDir() )
  {
    KFileItemList subItems = m_plugin->dirLister()->itemsForDir( item->url() );

    KFileItem *subItem;

    for( subItem = subItems.first(); subItem; subItem = subItems.next() )
    {
      slotDeleteItem( subItem );
    }
  }

  m_totalSize -= item->size();

  m_sizeLabel->setText( i18n("Size: %1 MB").arg(QString::number( m_totalSize/(1048576) )) );

}
BurnDataCDConfigWidget::~BurnDataCDConfigWidget()
{
  m_isoOptions->save( m_configGroup );
  m_configGroup->sync();
}

K3bDoc *BurnDataCDConfigWidget::k3bDoc()
{
  K3bDataDoc *dataDoc = new K3bDataDoc( this );
  dataDoc->newDocument();
  dataDoc->setWritingMode( K3b::WRITING_MODE_AUTO );
  dataDoc->setOnTheFly( true );
  dataDoc->setSpeed(0);
  dataDoc->setRemoveImages(true);
  dataDoc->setWritingApp( K3b::DEFAULT );
  KTempDir tempDir = KTempDir( "konqburn" );
  dataDoc->setTempDir( tempDir.name() );
  dataDoc->setOnlyCreateImages(false);
  dataDoc->setMultiSessionMode( K3bDataDoc::NONE );
  
  dataDoc->setIsoOptions( *m_isoOptions );

  KFileItem *item;
  KFileItemList items = m_plugin->dirLister()->itemsForDir( locateLocal( "data", "kio_burn/data" ) );
  for( item = items.first(); item; item = items.next() )
  {
    dataDoc->addUrl( item->url() );
  }
  return dataDoc;
}

#include "burndatacdconfigwidget.moc"
