/***************************************************************************
 *   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 "./mainwindow.h"
#include <KDE/DNSSD/ServiceModel>
#include <KDE/DNSSD/ServiceBrowser>
#include "configuration.h"
#include "optionsdialog.h"
#include <kdebug.h>

#include <KConfigDialog>

MainWindow::MainWindow() : QDialog(), current(0)
{
    setupUi( this );
    model=new DNSSD::ServiceModel(new DNSSD::ServiceBrowser("_ssh._tcp", true), this);
    lanView->setModel(model);
    lanView->setSelectionBehavior( QAbstractItemView::SelectRows );
    connect(lanView->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)),
	SLOT(rowSelected(const QModelIndex&)));
    connect(optionsBtn, SIGNAL(clicked()), SLOT(showOptions()));
    connect(hostCombo, SIGNAL(editTextChanged(const QString&)),this, SLOT(setupHost( const QString& )));
    hostCombo->setHistoryItems( KGlobal::config()->group( "Global" ).readEntry( "Hosts", QStringList() ) );
}

MainWindow::~MainWindow() {
    delete current;
}


void MainWindow::rowSelected(const QModelIndex& sel)
{
    DNSSD::RemoteService::Ptr service=sel.data(DNSSD::ServiceModel::ServicePtrRole).
    value<DNSSD::RemoteService::Ptr>();
    hostCombo->setEditText(service->hostName()+":"+QString::number( service->port() ));
}

void MainWindow::setupHost(const QString& host) {
    optionsBtn->setEnabled( !host.isEmpty() );
    connectBtn->setEnabled( !host.isEmpty() );
    userCombo->setEnabled( !host.isEmpty() );
    delete current;
    current=new Configuration(host);
    userCombo->setHistoryItems( current->users() );
    if (!current->users().empty())
      userCombo->setEditText( current->users().first() );
}

void MainWindow::showOptions() {
    if (!current) return;
    if (!KConfigDialog::showDialog( current->currentGroup() )) {
	OptionsDialog* d=new OptionsDialog(this, current->currentGroup(), current);
	d->show();
    }
    
}

void MainWindow::accept() {
  if (!current) return;
  if (!userCombo->currentText().isEmpty()) {
    QStringList users=userCombo->historyItems();
    users.removeAll(userCombo->currentText());
    users.prepend( userCombo->currentText());
    current->setUsers(users);
  }
  if (!hostCombo->currentText().isEmpty()) {
    QStringList hosts=hostCombo->historyItems();
    hosts.removeAll(hostCombo->currentText());
    hosts.prepend(hostCombo->currentText() );
    current->config()->group( "Global" ).writeEntry( "Hosts", hosts );
  }
  current->writeConfig();
  
  
  QDialog::accept();
}

QList<QByteArray> MainWindow::arguments() const {
  if (!current) return QList<QByteArray>();
  QList<QByteArray> ret=current->arguments();
  QString hp=hostCombo->currentText();
  if (hp.contains( ':' )) {
    bool ok;
    quint16 port=hp.section( ':', 1 ).toUInt( &ok );
    if (ok) {
      ret.append( "-p" );
      ret.append(QString::number(port).toLocal8Bit());
    }
    hp=hp.section(':',0,0);
  }
  if (userCombo->currentText().isEmpty()) ret.prepend( QUrl::toAce(hp) );
  else ret.prepend((userCombo->currentText()+"@").toLocal8Bit()+QUrl::toAce(hp));
  ret.prepend( "ssh" );
  return ret;
}



