

#include "staticservicebrowsermodel.h"
#include <QtGui>

#include "libraryinfo.h"

StaticServiceBrowserModel::StaticServiceBrowserModel(QObject *parent)
    : QAbstractListModel(parent), m_initing(true)
{
    QHash<int, QByteArray> roleNames = QAbstractListModel::roleNames();
    roleNames[Qt::DisplayRole] = "display";
    roleNames[AddressRole] = "address";
    roleNames[PortRole] = "port";
    setRoleNames(roleNames);

    QString storageLocation = LibraryInfo::dataPath();
    QString servicesFile = storageLocation + "/services.conf";
    QDir(storageLocation).mkpath(".");
    if (QFile::exists(servicesFile)) {
        initModelFromFile(servicesFile);
    } else {
        initModelFromFile(":/services.conf");
        save();
    }
    m_initing = false;
}

StaticServiceBrowserModel::~StaticServiceBrowserModel()
{
}

void StaticServiceBrowserModel::initModelFromFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        qWarning("Failed to open static services file");
        return;
    }

    QTextStream stream(&file);
    while (!stream.atEnd()) {
        QString hostName;
        stream >> hostName;
        if (hostName.startsWith('#') || hostName.isEmpty()) {
            stream.readLine();
            continue;
        }
        QString ip, port;
        stream >> ip >> port;

        addService(hostName, ip, port);
    }
}

void StaticServiceBrowserModel::addService(const QString &hostName, const QString &ip, const QString &port)
{
    QStringList list;
    list << hostName << ip << port;

    emit beginInsertRows(QModelIndex(), m_model.count(), m_model.count());
    m_model.append(list);
    emit endInsertRows();

    if (!m_initing)
        save();
}

void StaticServiceBrowserModel::removeService(int i)
{
    if (i < 0 || i >= m_model.count())
        return;
    beginRemoveRows(QModelIndex(), i, i);
    m_model.removeAt(i);
    endRemoveRows();

    save();
}

void StaticServiceBrowserModel::save()
{
    QString servicesFileName = LibraryInfo::dataPath() + "/services.conf";
    QFile file(servicesFileName);
    file.open(QFile::WriteOnly);
    QTextStream stream(&file);
    stream << "# Static configuration file auto-created by qmh remotecontrol" << endl;
    for (int i = 0; i < m_model.count(); i++) {
        QString hostName = m_model.at(i).at(0);
        QString ip = m_model.at(i).at(1);
        QString port = m_model.at(i).at(2);
        stream << hostName << " " << ip << " " << port << endl;
    }
    qDebug() << servicesFileName;
}

QVariant StaticServiceBrowserModel::data(const QModelIndex &index, int role) const
{
    QVariant rv;

    if (!index.isValid())
        return rv;

    if (0 <= index.row() && index.row() < m_model.count()) {
        if (role == Qt::DisplayRole)
            return m_model.at(index.row()).at(0);
        else if (role == StaticServiceBrowserModel::AddressRole)
            return m_model.at(index.row()).at(1);
        else if (role == StaticServiceBrowserModel::PortRole)
            return m_model.at(index.row()).at(2);
    }

    return rv;
}
