/***************************************************************************
                          preferencedialog.cpp  -  description
                             -------------------
    copyright            : (C) 2007 by Hans Dembinski
    email                : kbandwidth@5th-heaven.org
    original author      : Niko Sommer
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "preferencesdialog.h"

#include <qlineedit.h>
#include <qspinbox.h>
#include <qcheckbox.h>
#include <qradiobutton.h>
#include <kcolorbutton.h>
#include <qfile.h>
#include <qlabel.h>
#include <qcombobox.h>
#include <qstring.h>
#include <iostream>
using namespace std;

PreferencesDialog::PreferencesDialog(const KBandwidth* parent ) :
  kbandwidthview(parent->mainView)
{
//////////////////
  const QString forceInterface = kbandwidthview->forceInterface;
  lastInterface                = kbandwidthview->bandwidth.interface ;

  checkBoxText   ->setChecked( kbandwidthview->showText       );
  checkBoxArrows ->setChecked( kbandwidthview->showArrows     );
  spinInterval   ->setValue  ( kbandwidthview->updateInterval );

  buttonLearn->setOn(kbandwidthview->flagLearn);

  divisor = kbandwidthview->divisor;
  switch(divisor)
  {
    case 1024:
        radioKB  ->setChecked(true);
        textUnit1->setText("kB/s");
        textUnit2->setText("kB/s");
    break;

    case 1048576:
        radioMB  ->setChecked(true);
        textUnit1->setText("MB/s");
        textUnit2->setText("MB/s");
    break;

    case 128:
        radioKBit->setChecked(true);
        textUnit1->setText("kBit/s");
        textUnit2->setText("kBit/s");
    break;

    case 131072:
        radioMBit->setChecked(true);
        textUnit1->setText("MBit/s");
        textUnit2->setText("MBit/s");
    break;
  }

  limits = kbandwidthview->limits;

  ushort iInterface = 0;
  for (QMap<QString,Bandwidth>::const_iterator it = limits.begin();
    it != limits.end(); ++it) {
    comboBoxInterface->insertItem(it.key());
    comboBoxInterfaceLimit->insertItem(it.key());

    // set the comboboxes to the selected interace and last interface
    if (forceInterface == it.key())
      comboBoxInterface->setCurrentItem(iInterface+1);
    if (lastInterface == it.key())
      comboBoxInterfaceLimit->setCurrentItem(iInterface);
    ++iInterface;
  }



  // update the spins and lastInterface
  spinIncoming->setValue(limits[lastInterface].incoming/divisor);
  spinOutgoing->setValue(limits[lastInterface].outgoing/divisor);

//////////////////
  buttonArrowIncoming  ->setColor( kbandwidthview->colorArrowIncoming  );
  buttonArrowOutgoing  ->setColor( kbandwidthview->colorArrowOutgoing  );

  buttonNormalIncoming ->setColor( kbandwidthview->colorNormalIncoming );
  buttonNormalOutgoing ->setColor( kbandwidthview->colorNormalOutgoing );

  spinTransparency     ->setValue( kbandwidthview->arrowTransparency   );

  buttonTextColor      ->setColor( kbandwidthview->colorText );
}

void PreferencesDialog::slotOk(void)
{
  saveConfig();
  accept();
}

void PreferencesDialog::disableSpins(bool disable)
{
  spinIncoming->setDisabled(disable);
  spinOutgoing->setDisabled(disable);
}

void PreferencesDialog::saveConfig(void)
{
//////////////////
  kbandwidthview->forceInterface = comboBoxInterface->currentItem() != 0 ?
    comboBoxInterface->currentText() : "";
  kbandwidthview->showText       = checkBoxText->isChecked();
  kbandwidthview->showArrows     = checkBoxArrows->isChecked();
  kbandwidthview->updateInterval = spinInterval->value();

  // don't forget the last read from spins to limits object
  Bandwidth& limit          = limits[comboBoxInterfaceLimit->currentText()];
  limit.incoming            = spinIncoming->value()*divisor;
  limit.outgoing            = spinOutgoing->value()*divisor;
  kbandwidthview->limits    = limits;

  kbandwidthview->flagLearn = buttonLearn->isOn();
  kbandwidthview->divisor   = divisor;

//////////////////
  kbandwidthview->colorNormalIncoming = buttonNormalIncoming->color();
  kbandwidthview->colorNormalOutgoing = buttonNormalOutgoing->color();

  kbandwidthview->colorArrowIncoming  = buttonArrowIncoming->color();
  kbandwidthview->colorArrowOutgoing  = buttonArrowOutgoing->color();

  kbandwidthview->arrowTransparency   = spinTransparency->value()   ;

  kbandwidthview->colorText           = buttonTextColor->color();

//////////////////
  kbandwidthview->writeConfig();
  kbandwidthview->updateConfig();
}

void PreferencesDialog::updateUnitText(void) {
  ulong tmp1, tmp2;
  tmp1 = spinIncoming->value()*divisor;
  tmp2 = spinOutgoing->value()*divisor;

  if (radioKB       ->isChecked()) {
    textUnit1->setText("kB/s");
    textUnit2->setText("kB/s");
    divisor = 1024;
  }
  else if (radioMB  ->isChecked()) {
    textUnit1->setText("MB/s");
    textUnit2->setText("MB/s");
    divisor = 1048576;
  }
  else if (radioKBit->isChecked()) {
    textUnit1->setText("kBit/s");
    textUnit2->setText("kBit/s");
    divisor = 128;
  }
  else if (radioMBit->isChecked()) {
    textUnit1->setText("MBit/s");
    textUnit2->setText("MBit/s");
    divisor = 131072;
  }

  spinIncoming->setValue(tmp1/divisor);
  spinOutgoing->setValue(tmp2/divisor);
}

void PreferencesDialog::writeReadLimits(const QString& iface) {
  // write
  limits[lastInterface].incoming =
    spinIncoming->value()*divisor;
  limits[lastInterface].outgoing =
    spinOutgoing->value()*divisor;
  // read
  const Bandwidth& tmp = limits[iface];
  spinIncoming->setValue(tmp.incoming/divisor);
  spinOutgoing->setValue(tmp.outgoing/divisor);
  // update lastInterface
  lastInterface = iface;
}

void PreferencesDialog::resetLearnedLimits() {
  spinIncoming->setValue(0);
  spinOutgoing->setValue(0);
}

PreferencesDialog::~PreferencesDialog()
{
}
