/***************************************************************************
 *   Copyright (C) 2004-2010 by Giovanni Venturi                           *
 *   giovanni@ksniffer.org                                                 *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
 ***************************************************************************/

#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtGui/QFrame>

#include <KUniqueApplication>

#include <KVBox>
#include <KLocale>
#include <KSystemTrayIcon>

#include "OptionDialog.h"
#include "SnifferOptionTab.h"

OptionDialog::OptionDialog( KSystemTrayIcon *tray, QWidget *parent )
  : KPageDialog( parent )
{
  // this is the base class for your preferences dialog.  it is now
  // a Treelist dialog, but there are a number of other
  // possibilities (including Tab, Swallow, and just Plain)
  setCaption( i18n("Settings") );
  setModal( true );
  setButtons( Apply|Ok|Cancel );
  setDefaultButton( Ok );
  showButtonSeparator( true );
  setFaceType( List );

  m_snifferOptionTab = new SnifferOptionTab;
  KPageWidgetItem* snifferPage;
  snifferPage = addPage( m_snifferOptionTab, i18n("Capture") );
  snifferPage->setHeader( i18n("Capture Settings") );
  snifferPage->setIcon( KIcon( "hwinfo" ) );

  m_guiOptionTab = new GuiOptionTab(tray);
  KPageWidgetItem* guiPage;
  guiPage = addPage( m_guiOptionTab, i18n("GUI") );
  guiPage->setHeader( i18n("GUI Settings") );
  guiPage->setIcon( KIcon( "configure" ) );

  //connect( m_guiOptionTab, SIGNAL( trayBarShown( bool ) ), this, SIGNAL( trayBarShown( bool ) ) );
  connect( this, SIGNAL( applyClicked()), this, SLOT(slotApply()) );
  connect( this, SIGNAL( okClicked()), this, SLOT(slotOk()) );
  connect( this, SIGNAL( cancelClicked()), this, SLOT(slotCancel()) );

  // read the pages settings
  m_snifferOptionTab->readSettings();
  m_guiOptionTab->readSettings();

  resize( 500, 400 );
  setCurrentPage( snifferPage );
}


void OptionDialog::slotOk()
{
  if ( saveSettings() )
    accept();
}


void OptionDialog::slotApply()
{
  saveSettings();
}


void OptionDialog::slotCancel()
{
  // you need to restore the original tray state bacause the user
  // don't want to accept the changed settings
  m_guiOptionTab->restoreTray();
}


bool OptionDialog::saveSettings()
{
  // save all the configuration
  if ( !m_snifferOptionTab->saveSettings() )
    return false;
  if ( !m_guiOptionTab->saveSettings() )
    return false;

  return true;
}

#include "OptionDialog.moc"
