/***************************************************************************
 *   Copyright (C) 2008 by Jakub Stachowski <qbast@go2.pl>                 *
 *                                                                         *
 *   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 "./optionsdialog.h"
#include "forwardingmodel.h"
#include <kconfigskeleton.h>
#include "ui_protocol.h"
#include "ui_forwarding.h"
#include "ui_forwardedit.h"

OptionsDialog::OptionsDialog(QWidget* parent, const QString& name, KConfigSkeleton* skel) : 
    KConfigDialog( parent, name, skel ), skeleton(skel) {
  setFaceType( Tabbed );
  QFrame* f= new QFrame(this);
  Ui::ProtocolPage ppage;
  ppage.setupUi( f );
  addPage( f, i18n("Protocol") );
  f=new QFrame(this);
  Ui::ForwardingPage ppage2;
  ppage2.setupUi(f);
  addPage(f, i18n("Forwarding"));
  model=new ForwardingModel(this);
  ppage2.kcfg_Forwards->setModel(model);
  selection=ppage2.kcfg_Forwards->selectionModel();
  connect(ppage2.addBtn,SIGNAL(clicked()), this, SLOT(addForward()));
  connect(ppage2.removeBtn, SIGNAL(clicked()), this, SLOT(removeForward()));
  connect(model,SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SIGNAL(widgetModified()));
 
}
void OptionsDialog::addForward() {
    QDialog kd;
    Ui::forwardDialog fd;
    fd.setupUi( &kd );
    int ret=kd.exec();
    if (ret==QDialog::Accepted) {
	model->insertRow( 0 );
	model->setData(model->index(0,0), (fd.remoteCombo->currentIndex()==0));
	model->setData(model->index(0,1), fd.rportInput->value());
	model->setData(model->index(0,2), fd.hostEdit->text());
	model->setData(model->index(0,3), fd.lportInput->value());
    }
}

void OptionsDialog::removeForward() {
    model->removeRow( selection->currentIndex().row() );
}


