Commit 91975960 authored by Christian Kandeler's avatar Christian Kandeler

RemoteLinux: Put PortList class into its own set of files.

This also enables us to replace some includes by forward declarations.

Change-Id: Ica98c1925e49c65d15b9e47abe1c4aeba0790482
Reviewed-on: http://codereview.qt.nokia.com/583Reviewed-by: default avatarChristian Kandeler <christian.kandeler@nokia.com>
parent b86872e7
......@@ -41,6 +41,7 @@
#include <projectexplorer/applicationrunconfiguration.h>
#include <remotelinux/linuxdeviceconfiguration.h>
#include <remotelinux/remotelinuxrunconfiguration.h>
#include <QtCore/QDebug>
......
......@@ -34,7 +34,6 @@
#define ABSTRACTMAEMODEPLOYSTEP_H
#include "abstractlinuxdevicedeploystep.h"
#include "linuxdeviceconfiguration.h"
#include "maemodeployable.h"
#include "maemodeployables.h"
......@@ -53,8 +52,6 @@ namespace Qt4ProjectManager { class Qt4BuildConfiguration; }
namespace Utils { class SshConnection; }
namespace RemoteLinux {
class LinuxDeviceConfiguration;
namespace Internal {
class AbstractMaemoPackageCreationStep;
class Qt4MaemoDeployConfiguration;
......
......@@ -83,7 +83,7 @@ LinuxDeviceConfiguration::Ptr GenericLinuxDeviceConfigurationWizard::deviceConfi
sshParams.privateKeyFile = m_d->setupPage.privateKeyFilePath();
LinuxDeviceConfiguration::Ptr devConf = LinuxDeviceConfiguration::create(m_d->setupPage.configurationName(),
LinuxDeviceConfiguration::GenericLinuxOsType, LinuxDeviceConfiguration::Physical,
QLatin1String("10000-10100"), sshParams);
PortList::fromString(QLatin1String("10000-10100")), sshParams);
Internal::MaemoConfigTestDialog dlg(devConf, this);
dlg.exec();
return devConf;
......
......@@ -58,141 +58,10 @@ const QLatin1String InternalIdKey("InternalId");
const AuthType DefaultAuthType(Utils::SshConnectionParameters::AuthenticationByKey);
const int DefaultTimeout(10);
const LinuxDeviceConfiguration::DeviceType DefaultDeviceType(LinuxDeviceConfiguration::Physical);
class PortsSpecParser
{
struct ParseException {
ParseException(const char *error) : error(error) {}
const char * const error;
};
public:
PortsSpecParser(const QString &portsSpec)
: m_pos(0), m_portsSpec(portsSpec) { }
/*
* Grammar: Spec -> [ ElemList ]
* ElemList -> Elem [ ',' ElemList ]
* Elem -> Port [ '-' Port ]
*/
PortList parse()
{
try {
if (!atEnd())
parseElemList();
} catch (ParseException &e) {
qWarning("Malformed ports specification: %s", e.error);
}
return m_portList;
}
private:
void parseElemList()
{
if (atEnd())
throw ParseException("Element list empty.");
parseElem();
if (atEnd())
return;
if (nextChar() != ',') {
throw ParseException("Element followed by something else "
"than a comma.");
}
++m_pos;
parseElemList();
}
void parseElem()
{
const int startPort = parsePort();
if (atEnd() || nextChar() != '-') {
m_portList.addPort(startPort);
return;
}
++m_pos;
const int endPort = parsePort();
if (endPort < startPort)
throw ParseException("Invalid range (end < start).");
m_portList.addRange(startPort, endPort);
}
int parsePort()
{
if (atEnd())
throw ParseException("Empty port string.");
int port = 0;
do {
const char next = nextChar();
if (!std::isdigit(next))
break;
port = 10*port + next - '0';
++m_pos;
} while (!atEnd());
if (port == 0 || port >= 2 << 16)
throw ParseException("Invalid port value.");
return port;
}
bool atEnd() const { return m_pos == m_portsSpec.length(); }
char nextChar() const { return m_portsSpec.at(m_pos).toAscii(); }
PortList m_portList;
int m_pos;
const QString &m_portsSpec;
};
} // anonymous namespace
void PortList::addPort(int port) { addRange(port, port); }
void PortList::addRange(int startPort, int endPort)
{
m_ranges << Range(startPort, endPort);
}
bool PortList::hasMore() const { return !m_ranges.isEmpty(); }
int PortList::count() const
{
int n = 0;
foreach (const Range &r, m_ranges)
n += r.second - r.first + 1;
return n;
}
int PortList::getNext()
{
Q_ASSERT(!m_ranges.isEmpty());
Range &firstRange = m_ranges.first();
const int next = firstRange.first++;
if (firstRange.first > firstRange.second)
m_ranges.removeFirst();
return next;
}
QString PortList::toString() const
{
QString stringRep;
foreach (const Range &range, m_ranges) {
stringRep += QString::number(range.first);
if (range.second != range.first)
stringRep += QLatin1Char('-') + QString::number(range.second);
stringRep += QLatin1Char(',');
}
if (!stringRep.isEmpty())
stringRep.remove(stringRep.length() - 1, 1); // Trailing comma.
return stringRep;
}
QString PortList::regularExpression()
{
const QLatin1String portExpr("(\\d)+");
const QString listElemExpr = QString::fromLatin1("%1(-%1)?").arg(portExpr);
return QString::fromLatin1("((%1)(,%1)*)?").arg(listElemExpr);
}
LinuxDeviceConfiguration::~LinuxDeviceConfiguration() {}
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QSettings &settings,
Id &nextId)
......@@ -206,17 +75,17 @@ LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const ConstPtr &o
}
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QString &name,
const QString &osType, DeviceType deviceType, const QString &freePortsSpec,
const QString &osType, DeviceType deviceType, const PortList &freePorts,
const Utils::SshConnectionParameters &sshParams)
{
return Ptr(new LinuxDeviceConfiguration(name, osType, deviceType, freePortsSpec, sshParams));
return Ptr(new LinuxDeviceConfiguration(name, osType, deviceType, freePorts, sshParams));
}
LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QString &name, const QString &osType,
DeviceType deviceType, const QString &freePortsSpec,
DeviceType deviceType, const PortList &freePorts,
const Utils::SshConnectionParameters &sshParams)
: m_sshParameters(sshParams), m_name(name), m_osType(osType), m_type(deviceType),
m_portsSpec(freePortsSpec), m_isDefault(false)
m_freePorts(freePorts), m_isDefault(false)
{
}
......@@ -243,7 +112,7 @@ LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QSettings &settings,
}
}
m_portsSpec = settings.value(PortsSpecKey, defaultPortsSpec(m_type)).toString();
m_freePorts = PortList::fromString(settings.value(PortsSpecKey, QLatin1String("10000-10100")).toString());
m_sshParameters.host = settings.value(HostKey).toString();
m_sshParameters.port = settings.value(SshPortKey, 22).toInt();
m_sshParameters.userName = settings.value(UserNameKey).toString();
......@@ -260,17 +129,12 @@ LinuxDeviceConfiguration::LinuxDeviceConfiguration(const LinuxDeviceConfiguratio
m_name(other->m_name),
m_osType(other->m_osType),
m_type(other->type()),
m_portsSpec(other->m_portsSpec),
m_freePorts(other->freePorts()),
m_isDefault(other->m_isDefault),
m_internalId(other->m_internalId)
{
}
QString LinuxDeviceConfiguration::defaultPortsSpec(DeviceType type) const
{
return QLatin1String(type == Physical ? "10000-10100" : "13219,14168");
}
QString LinuxDeviceConfiguration::defaultPrivateKeyFilePath()
{
return QDesktopServices::storageLocation(QDesktopServices::HomeLocation)
......@@ -282,11 +146,6 @@ QString LinuxDeviceConfiguration::defaultPublicKeyFilePath()
return defaultPrivateKeyFilePath() + QLatin1String(".pub");
}
PortList LinuxDeviceConfiguration::freePorts() const
{
return PortsSpecParser(m_portsSpec).parse();
}
void LinuxDeviceConfiguration::save(QSettings &settings) const
{
settings.setValue(NameKey, m_name);
......@@ -294,7 +153,7 @@ void LinuxDeviceConfiguration::save(QSettings &settings) const
settings.setValue(TypeKey, m_type);
settings.setValue(HostKey, m_sshParameters.host);
settings.setValue(SshPortKey, m_sshParameters.port);
settings.setValue(PortsSpecKey, m_portsSpec);
settings.setValue(PortsSpecKey, m_freePorts.toString());
settings.setValue(UserNameKey, m_sshParameters.userName);
settings.setValue(AuthKey, m_sshParameters.authenticationType);
settings.setValue(PasswordKey, m_sshParameters.password);
......
......@@ -32,11 +32,11 @@
#ifndef LINUXDEVICECONFIGURATION_H
#define LINUXDEVICECONFIGURATION_H
#include "portlist.h"
#include "remotelinux_export.h"
#include <utils/ssh/sshconnection.h>
#include <QtCore/QPair>
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
#include <QtCore/QStringList>
......@@ -53,24 +53,6 @@ namespace Internal {
class LinuxDeviceConfigurations;
}
class REMOTELINUX_EXPORT PortList
{
public:
void addPort(int port);
void addRange(int startPort, int endPort);
bool hasMore() const;
int count() const;
int getNext();
QString toString() const;
static QString regularExpression();
private:
typedef QPair<int, int> Range;
QList<Range> m_ranges;
};
class REMOTELINUX_EXPORT LinuxDeviceConfiguration
{
friend class Internal::LinuxDeviceConfigurations;
......@@ -87,13 +69,14 @@ public:
enum DeviceType { Physical, Emulator };
PortList freePorts() const;
~LinuxDeviceConfiguration();
PortList freePorts() const { return m_freePorts; }
Utils::SshConnectionParameters sshParameters() const { return m_sshParameters; }
QString name() const { return m_name; }
void setName(const QString &name) { m_name = name; }
QString osType() const { return m_osType; }
DeviceType type() const { return m_type; }
QString portsSpec() const { return m_portsSpec; }
Id internalId() const { return m_internalId; }
bool isDefault() const { return m_isDefault; }
......@@ -103,10 +86,10 @@ public:
static const Id InvalidId;
static Ptr create(const QString &name, const QString &osType, DeviceType deviceType,
const QString &freePortsSpec, const Utils::SshConnectionParameters &sshParams);
const PortList &freePorts, const Utils::SshConnectionParameters &sshParams);
private:
LinuxDeviceConfiguration(const QString &name, const QString &osType, DeviceType deviceType,
const QString &freePortsSpec, const Utils::SshConnectionParameters &sshParams);
const PortList &freePorts, const Utils::SshConnectionParameters &sshParams);
LinuxDeviceConfiguration(const QSettings &settings, Id &nextId);
LinuxDeviceConfiguration(const ConstPtr &other);
......@@ -118,13 +101,12 @@ private:
static Ptr create(const ConstPtr &other);
void save(QSettings &settings) const;
QString defaultPortsSpec(DeviceType type) const;
Utils::SshConnectionParameters m_sshParameters;
QString m_name;
QString m_osType;
DeviceType m_type;
QString m_portsSpec;
PortList m_freePorts;
bool m_isDefault;
Id m_internalId;
};
......
......@@ -174,10 +174,10 @@ void LinuxDeviceConfigurations::setSshParameters(int i,
m_devConfigs.at(i)->m_sshParameters = params;
}
void LinuxDeviceConfigurations::setPortsSpec(int i, const QString &portsSpec)
void LinuxDeviceConfigurations::setFreePorts(int i, const PortList &freePorts)
{
Q_ASSERT(i >= 0 && i < rowCount());
m_devConfigs.at(i)->m_portsSpec = portsSpec;
m_devConfigs.at(i)->m_freePorts = freePorts;
}
void LinuxDeviceConfigurations::setDefaultDevice(int idx)
......
......@@ -68,7 +68,7 @@ public:
void removeConfiguration(int index);
void setConfigurationName(int i, const QString &name);
void setSshParameters(int i, const Utils::SshConnectionParameters &params);
void setPortsSpec(int i, const QString &portsSpec);
void setFreePorts(int i, const PortList &freePorts);
void setDefaultDevice(int index);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
......
......@@ -28,7 +28,6 @@
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "maemodebugsupport.h"
#include "maemosshrunner.h"
......
......@@ -67,6 +67,8 @@ MaemoDeploymentMounter::MaemoDeploymentMounter(QObject *parent)
SLOT(handlePortListReady()));
}
MaemoDeploymentMounter::~MaemoDeploymentMounter() {}
void MaemoDeploymentMounter::setupMounts(const SshConnection::Ptr &connection,
const LinuxDeviceConfiguration::ConstPtr &devConf,
const QList<MaemoMountSpecification> &mountSpecs,
......
......@@ -33,8 +33,8 @@
#ifndef MAEMODEPLOYMENTMOUNTER_H
#define MAEMODEPLOYMENTMOUNTER_H
#include "linuxdeviceconfiguration.h"
#include "maemomountspecification.h"
#include "portlist.h"
#include <QtCore/QList>
#include <QtCore/QObject>
......@@ -55,6 +55,7 @@ class MaemoDeploymentMounter : public QObject
Q_OBJECT
public:
explicit MaemoDeploymentMounter(QObject *parent = 0);
~MaemoDeploymentMounter();
// Connection must be in connected state.
void setupMounts(const QSharedPointer<Utils::SshConnection> &connection,
......
......@@ -227,7 +227,7 @@ void MaemoDeviceConfigurationsSettingsWidget::fillInValues()
const SshConnectionParameters &sshParams = current->sshParameters();
m_ui->hostLineEdit->setText(sshParams.host);
m_ui->sshPortSpinBox->setValue(sshParams.port);
m_ui->portsLineEdit->setText(current->portsSpec());
m_ui->portsLineEdit->setText(current->freePorts().toString());
m_ui->timeoutSpinBox->setValue(sshParams.timeout);
m_ui->userLineEdit->setText(sshParams.userName);
m_ui->pwdLineEdit->setText(sshParams.password);
......@@ -325,7 +325,7 @@ void MaemoDeviceConfigurationsSettingsWidget::keyFileEditingFinished()
void MaemoDeviceConfigurationsSettingsWidget::handleFreePortsChanged()
{
m_devConfigs->setPortsSpec(currentIndex(), m_ui->portsLineEdit->text());
m_devConfigs->setFreePorts(currentIndex(), PortList::fromString(m_ui->portsLineEdit->text()));
updatePortsWarningLabel();
}
......
......@@ -626,7 +626,8 @@ LinuxDeviceConfiguration::Ptr MaemoDeviceConfigWizard::deviceConfiguration()
doTest = true;
}
const LinuxDeviceConfiguration::Ptr devConf = LinuxDeviceConfiguration::create(d->wizardData.configName,
d->wizardData.osType, LinuxDeviceConfiguration::Physical, freePortsSpec, sshParams);
d->wizardData.osType, LinuxDeviceConfiguration::Physical,
PortList::fromString(freePortsSpec), sshParams);
if (doTest) {
MaemoConfigTestDialog dlg(devConf, this);
dlg.exec();
......
......@@ -32,9 +32,10 @@
#ifndef MAEMOQEMURUNTIME_H
#define MAEMOQEMURUNTIME_H
#include "linuxdeviceconfiguration.h"
#include "maemoqemusettings.h"
#include <remotelinux/portlist.h>
#include <QtCore/QHash>
#include <QtCore/QList>
#include <QtCore/QPair>
......
......@@ -32,7 +32,6 @@
#ifndef MAEMOQTVERSION_H
#define MAEMOQTVERSION_H
#include "linuxdeviceconfiguration.h"
#include <qtsupport/baseqtversion.h>
namespace RemoteLinux {
......
......@@ -33,7 +33,6 @@
#ifndef MAEMOREMOTEMOUNTER_H
#define MAEMOREMOTEMOUNTER_H
#include "linuxdeviceconfiguration.h"
#include "maemomountspecification.h"
#include <QtCore/QList>
......@@ -54,6 +53,7 @@ namespace Qt4ProjectManager { class Qt4BuildConfiguration; }
namespace RemoteLinux {
class LinuxDeviceConfiguration;
class PortList;
namespace Internal {
class MaemoUsedPortsGatherer;
......
......@@ -32,8 +32,6 @@
#ifndef MAEMOUSEDPORTSGATHERER_H
#define MAEMOUSEDPORTSGATHERER_H
#include "linuxdeviceconfiguration.h"
#include <QtCore/QList>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
......@@ -46,6 +44,7 @@ class SshRemoteProcessRunner;
namespace RemoteLinux {
class LinuxDeviceConfiguration;
class PortList;
namespace Internal {
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "portlist.h"
namespace RemoteLinux {
namespace {
class PortsSpecParser
{
struct ParseException {
ParseException(const char *error) : error(error) {}
const char * const error;
};
public:
PortsSpecParser(const QString &portsSpec)
: m_pos(0), m_portsSpec(portsSpec) { }
/*
* Grammar: Spec -> [ ElemList ]
* ElemList -> Elem [ ',' ElemList ]
* Elem -> Port [ '-' Port ]
*/
PortList parse()
{
try {
if (!atEnd())
parseElemList();
} catch (ParseException &e) {
qWarning("Malformed ports specification: %s", e.error);
}
return m_portList;
}
private:
void parseElemList()
{
if (atEnd())
throw ParseException("Element list empty.");
parseElem();
if (atEnd())
return;
if (nextChar() != ',') {
throw ParseException("Element followed by something else "
"than a comma.");
}
++m_pos;
parseElemList();
}
void parseElem()
{
const int startPort = parsePort();
if (atEnd() || nextChar() != '-') {
m_portList.addPort(startPort);
return;
}
++m_pos;
const int endPort = parsePort();
if (endPort < startPort)
throw ParseException("Invalid range (end < start).");
m_portList.addRange(startPort, endPort);
}
int parsePort()
{
if (atEnd())
throw ParseException("Empty port string.");
int port = 0;
do {
const char next = nextChar();
if (!std::isdigit(next))
break;
port = 10*port + next - '0';
++m_pos;
} while (!atEnd());
if (port == 0 || port >= 2 << 16)
throw ParseException("Invalid port value.");
return port;
}
bool atEnd() const { return m_pos == m_portsSpec.length(); }
char nextChar() const { return m_portsSpec.at(m_pos).toAscii(); }
PortList m_portList;
int m_pos;
const QString &m_portsSpec;
};
} // anonymous namespace
PortList PortList::fromString(const QString &portsSpec)
{
return PortsSpecParser(portsSpec).parse();
}
void PortList::addPort(int port) { addRange(port, port); }
void PortList::addRange(int startPort, int endPort)
{
m_ranges << Range(startPort, endPort);
}
bool PortList::hasMore() const { return !m_ranges.isEmpty(); }
int PortList::count() const
{
int n = 0;
foreach (const Range &r, m_ranges)
n += r.second - r.first + 1;
return n;
}
int PortList::getNext()
{
Q_ASSERT(!m_ranges.isEmpty());
Range &firstRange = m_ranges.first();
const int next = firstRange.first++;
if (firstRange.first > firstRange.second)
m_ranges.removeFirst();
return next;
}
QString PortList::toString() const
{
QString stringRep;
foreach (const Range &range, m_ranges) {
stringRep += QString::number(range.first);
if (range.second != range.first)
stringRep += QLatin1Char('-') + QString::number(range.second);
stringRep += QLatin1Char(',');
}
if (!stringRep.isEmpty())
stringRep.remove(stringRep.length() - 1, 1); // Trailing comma.
return stringRep;
}
QString PortList::regularExpression()
{
const QLatin1String portExpr("(\\d)+");
const QString listElemExpr = QString::fromLatin1("%1(-%1)?").arg(portExpr);
return QString::fromLatin1("((%1)(,%1)*)?").arg(listElemExpr);
}
} // namespace RemoteLinux
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage