/***************************************************************************
 *   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 "./forwardingmodel.h"
#include <klocale.h>
#include <QUrl>

ForwardingModel::ForwardingModel(QObject* parent) : QAbstractTableModel(parent), VariantSerializable() {
}

void ForwardingModel::fromVariant(const QVariant& data)
{
  if (!data.canConvert( QVariant::StringList )) return;
  Q_FOREACH (const QString& str, data.toStringList()) {
    Item itm;
    if (str.startsWith( "-L " )) itm.remote=false;
    else if (str.startsWith("-R ")) itm.remote=true;
    else continue;
    
    QStringList exploded=str.mid(3).split( ':' );
    if (exploded.size()!=3) continue;
    itm.lport=exploded[0].toUShort(  );
    itm.tport=exploded[2].toUShort();
    itm.host=exploded[1];
    if (!itm.lport || !itm.tport) continue;
    items.append( itm );
    
  }
  reset();

}

bool ForwardingModel::insertRows ( int row, int count, const QModelIndex & parent)
{
    if (parent.isValid()) return false;
    if (row>items.size()) return false;
    beginInsertRows( parent,row, row+count-1 );
    for (int i=0;i<count;i++) items.insert( row, Item() );
    endInsertRows();
    return true;
}

int ForwardingModel::rowCount ( const QModelIndex & parent) const
{
  return (parent.isValid()) ? 0 : items.size();
}
int ForwardingModel::columnCount ( const QModelIndex & parent ) const
{
  return (parent.isValid()) ? 0 : 4;
}
QVariant ForwardingModel::data ( const QModelIndex & index, int role ) const
{
  if (!index.isValid() || index.parent().isValid() || role!=Qt::DisplayRole) return QVariant();
  if (index.column()>3 || index.row()>=items.size() ) return QVariant();
  switch (index.column()) {
    case 0: return items[index.row()].remote ? i18n("Remote side") : i18n("Local side");
    case 1: return items[index.row()].tport;
    case 2: return QUrl::fromAce(items[index.row()].host.toLatin1());
    case 3: return items[index.row()].lport;
  }
  return QVariant();
}
QVariant ForwardingModel::headerData ( int section, Qt::Orientation orientation, int role ) const
{
    if (orientation!=Qt::Horizontal || role!=Qt::DisplayRole) return QVariant();
    switch (section) {
      case 0: return i18n("Target on"); 
      case 1: return i18n("Target port");
      case 2: return i18n("Target host");
      case 3: return i18n("Listening port");
    }
    return QVariant();
}
bool ForwardingModel::removeRows(int row, int count, const QModelIndex& parent) {
  if (parent.isValid()) return false;
  if (row+count>items.size()) return false;
  beginRemoveRows( parent,row, row+count-1 );
  for (int i=0;i<count;i++) items.removeAt( row );
  endRemoveRows();
  return true;
}

QVariant ForwardingModel::toVariant() const
{
  QStringList ret;
  Q_FOREACH (Item itm, items) 
    ret+=QString("-%1 %2:%3:%4").arg(itm.remote ? "R" : "L").arg(itm.lport).arg(itm.host).arg(itm.tport);
  return ret;
}

bool ForwardingModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
  if (!index.isValid() || role!=Qt::EditRole || index.column()>3) return false;
  switch (index.column()) {
    case 0: items[index.row()].remote=value.toBool(); break;
    case 1: items[index.row()].tport=value.toUInt(); break;
    case 2: items[index.row()].host=QUrl::toAce( value.toString()) ; break;
    case 3: items[index.row()].lport=value.toUInt(); break;
  }
  emit dataChanged( index, index );
  return true;
}
Qt::ItemFlags ForwardingModel::flags(const QModelIndex &index) const
 {
     if (!index.isValid())
         return Qt::ItemIsEnabled;

     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
 }
