/**
 *  mixconfig.cpp
 *
 *  Copyright (c) 2000 Stefan Schimanski <1Stein@gmx.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <qlayout.h>
#include <qvbox.h>
#include <qgroupbox.h>
#include <qcheckbox.h>
#include <qlistview.h>
#include <qpushbutton.h>
#include <kconfig.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kdialog.h>
#include <kdebug.h>
#include <kapplication.h>
#include <kfile.h>
#include <qtextstream.h>
#include <kiconloader.h>
#include <kprocess.h>
#include <kmessagebox.h>
#include <knuminput.h>
#include <qwhatsthis.h>
#include <qprogressdialog.h>

#include "mixconfig.h"

KMixConfig::KMixConfig(QWidget *parent, const char *name)
  : KCModule(parent, name)
{
  QVBoxLayout *topLayout = new QVBoxLayout(this, 0, KDialog::spacingHint());

  // Restore settings
  QGroupBox *restGrp = new QGroupBox( i18n("Default Volumes"), this );
  restGrp->setColumnLayout( 0, Qt::Horizontal );
  QBoxLayout *restLayout = new QVBoxLayout( restGrp->layout(), 
      KDialog::spacingHint());
  topLayout->addWidget( restGrp );

  // Save profile
  QHBoxLayout *profLayout = new QHBoxLayout( restLayout, 5 );
  QPushButton *saveProf = new QPushButton( i18n("Save Current Volumes"), restGrp );
  profLayout->addWidget( saveProf );
  connect( saveProf, SIGNAL(clicked()), this, SLOT(saveVolumes()) );

  // Load profile
  QPushButton *loadProf = new QPushButton( i18n("Load Volumes"), restGrp );
  profLayout->addWidget( loadProf );
  connect( loadProf, SIGNAL(clicked()), this, SLOT(loadVolumes()) );

  // start in startkde?
  m_startkdeRestore = new QCheckBox( i18n("Load volumes on login"), restGrp );
  restLayout->addWidget( m_startkdeRestore );
  connect( m_startkdeRestore, SIGNAL(clicked()), this, SLOT(configChanged()) );

  // Hardware settings
  QGroupBox *hdwGrp = new QGroupBox( i18n("Hardware Settings"), this );
  topLayout->addWidget( hdwGrp );
  hdwGrp->setColumnLayout( 0, Qt::Horizontal );
  QBoxLayout *hdwLayout = new QVBoxLayout( hdwGrp->layout(), 
      KDialog::spacingHint());

  m_maxDevices = new KIntNumInput( hdwGrp );
  m_maxDevices->setLabel( i18n("Maximum number of probed devices per mixer:") );
  m_maxDevices->setRange( 1, 16 );
  hdwLayout->addWidget( m_maxDevices );
  connect( m_maxDevices, SIGNAL(valueChanged(int)), this, SLOT(configChanged()) );
  QWhatsThis::add( m_maxDevices,
                   i18n("Change this value to optimize the startup time "
                        "of kmix. High values mean that kmix probes for "
                        "many devices per soundcard driver.\n"
                        "If there are more mixer sub devices in a "
                        "driver than kmix detects, increase this value") );

  topLayout->addStretch( 1 );

  load();
}

KMixConfig::~KMixConfig() {}

void KMixConfig::configChanged()
{
  emit changed(true);
}

void KMixConfig::loadVolumes()
{
   QProgressDialog progress( i18n("Restoring default volumes"), i18n("Cancel"), 1, this );
   KProcess *ctrl = new KProcess;
   QString ctrlExe = KGlobal::dirs()->findExe("kmixctrl");
   if (!ctrlExe)
   {
      kdDebug() << "can't find kmixctrl" << endl;

      KMessageBox::sorry ( this, i18n("The kmixctrl executable can't be found.") );
      delete ctrl;
      return;
   }

   *ctrl << ctrlExe;
   *ctrl << "--restore";
   ctrl->start();

   while ( ctrl->isRunning() )
   {
      if ( progress.wasCancelled() ) break;
      kapp->processEvents();
   }
   progress.setProgress( 1 );

   delete ctrl;
}

void KMixConfig::saveVolumes()
{
   QProgressDialog progress( i18n("Saving default volumes"), i18n("Cancel"), 1, this );
   KProcess *ctrl = new KProcess;
   QString ctrlExe = KGlobal::dirs()->findExe("kmixctrl");
   if (!ctrlExe)
   {
      kdDebug() << "can't find kmixctrl" << endl;

      KMessageBox::sorry ( this, i18n("The kmixctrl executable can't be found.") );
      delete ctrl;
      return;
   }

   *ctrl << ctrlExe;
   *ctrl << "--save";
   ctrl->start();

   while ( ctrl->isRunning() )
   {
      if ( progress.wasCancelled() ) break;
      kapp->processEvents();
   }
   progress.setProgress( 1 );

   delete ctrl;
}

void KMixConfig::load()
{
  KConfig *config = new KConfig("kcmkmixrc", true);

  config->setGroup("Misc");
  m_startkdeRestore->setChecked( config->readBoolEntry( "startkdeRestore", true ) );
  m_maxDevices->setValue( config->readNumEntry( "maxDevices", 2 ) );
  delete config;

  emit changed(false);
}

void KMixConfig::save()
{

  KConfig *config= new KConfig("kcmkmixrc", false);

  config->setGroup("Misc");
  config->writeEntry( "startkdeRestore", m_startkdeRestore->isChecked() );
  config->writeEntry( "maxDevices", m_maxDevices->value() );
  config->sync();
  delete config;

  emit changed(false);
}

void KMixConfig::defaults()
{
  m_startkdeRestore->setChecked( true );
  m_maxDevices->setValue( 2 );
  emit changed(true);
}

extern "C"
{
    KCModule *create_kmix(QWidget *parent, const char * /*name*/)
    {
      return new KMixConfig(parent, "kcmkmix");
    }

    void init_kmix()
    {
        KConfig *config = new KConfig("kcmkmixrc", true, false);

        config->setGroup("Misc");
        bool start = config->readBoolEntry( "startkdeRestore", true );
        delete config;

        if ( start )
            kapp->startServiceByDesktopName( "kmixctrl_restore" );
    }
}

#include "mixconfig.moc"
