/* $Id: PreferencesDialog.cpp,v 1.9 2003/01/12 16:19:56 rjesse Exp $
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2000-2003, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 */

#include "PreferencesDialog.h"
#include "DocManager.h"
#include "Mango.h"

#include <qapplication.h>
#include <qfiledialog.h>
#include <qlineedit.h>
#include <qlistbox.h>
#include <qstylefactory.h>
#include <qpushbutton.h>
#include <qradiobutton.h>
#include <qspinbox.h>

PreferencesDialog::PreferencesDialog( QWidget* parent, const char* name)
   : PreferencesForm( parent, name, true)
{
   // Populate style list box
   QStringList styleList = QStyleFactory::keys();
   styleList.sort();
   for (QStringList::Iterator it = styleList.begin(); it != styleList.end(); ++it) {
      QString styleStr = *it;
      m_styleListBox->insertItem( styleStr);
   }
   QString oldStyle = Mango::getInstance()->activeStyle();
   for (uint i = 0; i < m_styleListBox->count(); i++) {
      QString s = m_styleListBox->text( i);
      if (s == oldStyle) {
         m_styleListBox->setCurrentItem( i);
         break;
      }
   }

   Mango* mango = Mango::getInstance();
   spinBoxStartupTimeout->setValue( mango->startupTimeout());

   DocManager* docmgr = DocManager::getInstance();
   editDocDir->setText( docmgr->getRootDirName());
   
   connect( this, SIGNAL( preferencesChanged()),
            Mango::getInstance(), SLOT( slotSavePreferences()));
} // Constructor

void
PreferencesDialog::applyStyle()
{
   Mango::getInstance()->setStyle( m_styleListBox->currentText());
}

void
PreferencesDialog::slotApply() {
   applyStyle();
   Mango::getInstance()->setStartupTimeout( spinBoxStartupTimeout->value());
   DocManager::getInstance()->slotSetRootDir( editDocDir->text());
   
   /* Don't touch the Registry here! Save all settings and let somebody
	  else take care of interpreting 'em.
   */
   emit preferencesChanged();
}

/*!
  Opens a file selection dialog for the user to select the
  root directory of all online documentation files.
*/
void
PreferencesDialog::slotAskForDocRootDir()
{
   QString dirStr = QFileDialog::getExistingDirectory( editDocDir->text(), this, "", tr( "Please select the root directory of the online documentation files."));
   editDocDir->setText( dirStr);
} // slotAskForDocRootDir()
