/***************************************************************************
 *   Copyright (C) 2007-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/QCheckBox>
#include <QtGui/QRadioButton>
#include <QtGui/QToolTip>
#include <QtGui/QWhatsThis>

#include <KUniqueApplication>
#include <KSystemTrayIcon>
#include <KLocale>

#include "GuiOptionTab.h"
//#include "CaptureOption.h"

GuiOptionTab::GuiOptionTab( KSystemTrayIcon *tray, QWidget *parent )
  : QWidget(parent), m_tray(tray)
{
  QWidget *widget = new QWidget( this );

  // create the user interface, the parent widget is "widget"
  ui.setupUi(widget); // this is the important part
    
  ui.m_afterDisplay->setToolTip( i18n("The packets will be displayed just when you stop capturing them") );
  ui.m_afterDisplay->setWhatsThis( i18n("<p>The default option is <i>showing packets when you capture "\
    "it</i>. If you check this, when KSniffer will capture packets it will not display them. "\
    "Just when you stop capturing all captured packets will be displayed.</p>"
    "<p>You can change this option during sniffing, but the effect will "\
    "take effect after you make a <b>New capture</b>. If you want simply "\
    "to stop packet-capture for a while you can use the <i>Pause Capture</i> "\
    "feature from the <i>Capture</i> menu.</p>") );

  ui.m_trayBar->setToolTip( i18n("KSniffer can be added to the KDE tray bar clicking here") );
  ui.m_trayBar->setWhatsThis( i18n("<p>The default option is <i>not showing KSniffer icon in the tray "\
    "bar</i>. If you want to add KSniffer to the tray bar you have to click here. "\
    "In this way you can access some useful KSniffer functions like start a new "\
    "capure, stop it, pause/continue it directly from the tray bar.</p>") );

  connect( ui.m_checkWarning, SIGNAL(clicked()), this, SLOT(checkWarning()) );
  ui.m_checkWarning->setToolTip( i18n("Warning when the capture stops and you got no packets") );
  ui.m_checkWarning->setWhatsThis( i18n("<p>If you check this, you will be warn when the capture stops "\
    "and you got no packets. You can choose if you want to be warned with a modal window or with a "\
    "pop up one checking or not the following sub option.</p>") );

  ui.m_checkPassiveWarn->setToolTip( i18n("Warning with a passive pop up when the capture stops and you got no packets") );
  ui.m_checkPassiveWarn->setWhatsThis( i18n("<p>If you check this, you will be warn when the capture stops and you got "\
    "no packets with a passive pop up, otherwise you will warn by a modal window that stops the "\
    "application till when you click on the OK button.</p>") );

  ui.m_checkShowSplash->setToolTip( i18n("It will show a splash screen at the startup") );
  ui.m_checkShowSplash->setWhatsThis( i18n("<p>If you check this, you will see a splash screen for some seconds "\
    "at the KSniffer startup.</p>") );

  ui.m_checkShowProtocolName->setToolTip( i18n("The <b>Protocol</b> column is referred to the packets list. "\
      "Changes are applied just to the new packets") );
  ui.m_checkShowProtocolName->setWhatsThis( i18n("<p>If you check this, when KSniffer will detect the service "\
    "application as could be an IMAP, SMTP, POP3, DNS protocol you will see that names in place "\
    " of just TCP or UDP (according to the relative protocol) in the <b>Protocol</b> column in the packets "\
    " list.</p><p>This option has effect only on the new packets. If you want to apply the change "\
    "to all packets you need to reload the file if you loaded one, or just restart KSniffer to have this option "\
    "enabled for all packets.</p>"\
    "<p>The protocol name is obtained from your OS from the file <b>/etc/services</b>.</p>"));

  // show/hide tray connection
  connect(ui.m_trayBar, SIGNAL(stateChanged(int)), this, SLOT(adjustTray(int)));

  m_config = KGlobal::config()->group("GUI");
}

/*
void GuiOptionTab::getShowAfterOptions( CaptureOptions *options )
{
  // get just the Show After configuration
  options->setShowAfter( m_config.readEntry( "Show packets after stopped capture" , false ) );
}


void GuiOptionTab::getRawFramePosition( CaptureOptions *options )
{
  // get just the Show Raw Frame On the
  options->setShowAfter( m_config.readEntry( "Show Raw Frame On the" , false ) );
}


void GuiOptionTab::getOptions( CaptureOptions *options )
{
  options->setShowAfter( m_config.readEntry( "Show packets after stopped capture" , false ) );

  options->setTrayBar( m_config.readEntry( "Show Tray Bar", false ) );
  options->setWarning( m_config.readEntry( "Warn with no packets", true ) );
  options->setPassiveWarn( m_config.readEntry( "Passive pop up warning with no packets", false ) );
  options->setShowSplash( m_config.readEntry( "Show splash", true ) );

  options->setShowProtocolName( m_config.readEntry( "Show Protocol Name", true ) );
}
*/

void GuiOptionTab::readSettings()
{
  ui.m_afterDisplay->setChecked( m_config.readEntry( "Show packets after stopped capture" , false ) );
  ui.m_trayBar->setChecked( m_config.readEntry( "Show Tray Bar", false ) );

  ui.m_checkWarning->setChecked( m_config.readEntry( "Warn with no packets", true ) );
  if (!ui.m_checkWarning->isChecked())
    ui.m_checkPassiveWarn->setEnabled( false );
  else
    ui.m_checkPassiveWarn->setChecked( m_config.readEntry( "Passive pop up warning with no packets", false ) );

  // check if the Splashscreen has to be displayed
  ui.m_checkShowSplash->setChecked( m_config.readEntry("Show splash", true) );

  ui.m_checkShowProtocolName->setChecked( m_config.readEntry( "Show Protocol Name", true ) );

  // store the origing tray setting to avoid changes visually if user click Cancel button
  m_trayState = ui.m_trayBar->checkState();
}


bool GuiOptionTab::saveSettings()
{
  m_config.writeEntry( "Show splash", ui.m_checkShowSplash->isChecked() );

  m_config.writeEntry( "Show packets after stopped capture", ui.m_afterDisplay->isChecked() );
  m_config.writeEntry( "Show Tray Bar", ui.m_trayBar->isChecked() );

  m_config.writeEntry( "Warn with no packets", ui.m_checkWarning->isChecked() );
  m_config.writeEntry( "Passive pop up warning with no packets", ui.m_checkPassiveWarn->isChecked() );

  m_config.writeEntry( "Show Protocol Name", ui.m_checkShowProtocolName->isChecked() );

  return true;
}


void GuiOptionTab::checkWarning()
{
  ui.m_checkPassiveWarn->setEnabled(ui.m_checkWarning->isChecked());
}


void GuiOptionTab::adjustTray(int state)
{
  switch (state)
  {
    case Qt::Unchecked:
      m_tray->hide();
      break;

    case Qt::Checked:
      m_tray->show();
      break;
  }
}


void GuiOptionTab::restoreTray()
{
  ui.m_trayBar->setCheckState( m_trayState );
}

#include "GuiOptionTab.moc"
