//Author:    Max Howell <max.howell@methylblue.com>, (C) 2003-4
//Copyright: See COPYING file that comes with this distribution

#include <tqapplication.h> //Getting desktop width
#include <tqcheckbox.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqslider.h>
#include <tqvbuttongroup.h>

#include <kdirselectdialog.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <knuminput.h>

#include "settingsDialog.h"
#include "Config.h"


SettingsDialog::SettingsDialog( TQWidget *parent, const char *name )
  : Dialog( parent, name, false ) //3rd param => modal
{
    colourSchemeGroup->setFrameShape( TQFrame::NoFrame );

    colourSchemeGroup->insert( new TQRadioButton( i18n("Rainbow"), colourSchemeGroup ), Filelight::Rainbow );
    colourSchemeGroup->insert( new TQRadioButton( i18n("TDE Colors"), colourSchemeGroup ), Filelight::KDE );
    colourSchemeGroup->insert( new TQRadioButton( i18n("High Contrast"), colourSchemeGroup ), Filelight::HighContrast );

    //read in settings before you make all those nasty connections!
    reset(); //makes dialog reflect global settings

    connect( &m_timer, TQ_SIGNAL(timeout()), TQ_SIGNAL(mapIsInvalid()) );

    connect( m_addButton,    TQ_SIGNAL( clicked() ), TQ_SLOT( addDirectory() ) );
    connect( m_removeButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeDirectory() ) );
    connect( m_resetButton,  TQ_SIGNAL( clicked() ), TQ_SLOT( reset() ) );
    connect( m_closeButton,  TQ_SIGNAL( clicked() ), TQ_SLOT( close() ) );

    connect( colourSchemeGroup, TQ_SIGNAL(clicked( int )), TQ_SLOT(changeScheme( int )) );
    connect( contrastSlider, TQ_SIGNAL(valueChanged( int )), TQ_SLOT(changeContrast( int )) );
    connect( contrastSlider, TQ_SIGNAL(sliderReleased()), TQ_SLOT(slotSliderReleased()) );

    connect( scanAcrossMounts,       TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( startTimer() ) );
    connect( dontScanRemoteMounts,   TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( startTimer() ) );
    connect( dontScanRemovableMedia, TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( startTimer() ) );

    connect( useAntialiasing,    TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( toggleUseAntialiasing( bool ) ) );
    connect( varyLabelFontSizes, TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( toggleVaryLabelFontSizes( bool ) ) );
    connect( showSmallFiles,     TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( toggleShowSmallFiles( bool ) ) );

    connect( minFontPitch, TQ_SIGNAL ( valueChanged( int ) ), TQ_SLOT( changeMinFontPitch( int ) ) );

    m_addButton->setIconSet( SmallIcon( "document-open" ) );
    m_resetButton->setIconSet( SmallIcon( "edit-undo" ) );
    m_closeButton->setIconSet( SmallIcon( "window-close" ) );
}


void SettingsDialog::closeEvent( TQCloseEvent* )
{
    //if an invalidation is pending, force it now!
    if( m_timer.isActive() ) m_timer.changeInterval( 0 );

    Config::write();

    deleteLater();
}


void SettingsDialog::reset()
{
    Config::read();

    //tab 1
    scanAcrossMounts->setChecked( Config::scanAcrossMounts );
    dontScanRemoteMounts->setChecked( !Config::scanRemoteMounts );
    dontScanRemovableMedia->setChecked( !Config::scanRemovableMedia );

    dontScanRemoteMounts->setEnabled( Config::scanAcrossMounts );
    //  dontScanRemovableMedia.setEnabled( Config::scanAcrossMounts );

    m_listBox->clear();
    m_listBox->insertStringList( Config::skipList );
    m_listBox->setSelected( 0, true );

    m_removeButton->setEnabled( m_listBox->count() == 0 );

    //tab 2
    if( colourSchemeGroup->id( colourSchemeGroup->selected() ) != Config::scheme )
    {
        colourSchemeGroup->setButton( Config::scheme );
        //setButton doesn't call a single TQButtonGroup signal!
        //so we need to call this ourselves (and hence the detection above)
        changeScheme( Config::scheme );
    }
    contrastSlider->setValue( Config::contrast );

    useAntialiasing->setChecked( (Config::antiAliasFactor > 1) ? true : false );

    varyLabelFontSizes->setChecked( Config::varyLabelFontSizes );
    minFontPitch->setEnabled( Config::varyLabelFontSizes );
    minFontPitch->setValue( Config::minFontPitch );
    showSmallFiles->setChecked( Config::showSmallFiles );
}



void SettingsDialog::toggleScanAcrossMounts( bool b )
{
    Config::scanAcrossMounts = b;

    dontScanRemoteMounts->setEnabled( b );
    //dontScanRemovableMedia.setEnabled( b );
}

void SettingsDialog::toggleDontScanRemoteMounts( bool b )
{
    Config::scanRemoteMounts = !b;
}

void SettingsDialog::toggleDontScanRemovableMedia( bool b )
{
    Config::scanRemovableMedia = !b;
}



void SettingsDialog::addDirectory()
{
    const KURL url = KDirSelectDialog::selectDirectory( "/", false, this );

    //TODO error handling!
    //TODO wrong protocol handling!

    if( !url.isEmpty() )
    {
        const TQString path = url.path( 1 );

        if( !Config::skipList.contains( path ) )
        {
            Config::skipList.append( path );
            m_listBox->insertItem( path );
            m_removeButton->setEnabled( true );
        }
        else KMessageBox::sorry( this, i18n("That directory is already set to be excluded from scans") );
    }
}


void SettingsDialog::removeDirectory()
{
    Config::skipList.remove( m_listBox->currentText() ); //removes all entries that match

    //safest method to ensure consistency
    m_listBox->clear();
    m_listBox->insertStringList( Config::skipList );

    m_removeButton->setEnabled( m_listBox->count() == 0 );
}


void SettingsDialog::startTimer()
{
    m_timer.start( TIMEOUT, true );
}

void SettingsDialog::changeScheme( int s )
{
    Config::scheme = (Filelight::MapScheme)s;
    emit canvasIsDirty( 1 );
}
void SettingsDialog::changeContrast( int c )
{
    Config::contrast = c;
    emit canvasIsDirty( 3 );
}
void SettingsDialog::toggleUseAntialiasing( bool b )
{
    Config::antiAliasFactor = b ? 2 : 1;
    emit canvasIsDirty( 2 );
}
void SettingsDialog::toggleVaryLabelFontSizes( bool b )
{
    Config::varyLabelFontSizes = b;
    minFontPitch->setEnabled( b );
    emit canvasIsDirty( 0 );
}
void SettingsDialog::changeMinFontPitch( int p )
{
    Config::minFontPitch = p;
    emit canvasIsDirty( 0 );
}
void SettingsDialog::toggleShowSmallFiles( bool b )
{
    Config::showSmallFiles = b;
    emit canvasIsDirty( 1 );
}


void SettingsDialog::slotSliderReleased()
{
    emit canvasIsDirty( 2 );
}


void SettingsDialog::reject()
{
    //called when escape is pressed
    reset();
    TQDialog::reject();   //**** doesn't change back scheme so far
}

#include "settingsDialog.moc"
