Commit f7e127f7 authored by Christian Kandeler's avatar Christian Kandeler
Browse files

RemoteLinux: Simplify approach for id assignment.



The current one is awkward and error-prone.

Change-Id: I9450b121d0f01c07507b8cc6d72cb2e1453cf02d
Reviewed-by: default avatarKai Koehne <kai.koehne@nokia.com>
parent 50458524
......@@ -94,10 +94,9 @@ LinuxDeviceConfiguration::~LinuxDeviceConfiguration()
delete d;
}
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QSettings &settings,
Id &nextId)
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QSettings &settings)
{
return Ptr(new LinuxDeviceConfiguration(settings, nextId));
return Ptr(new LinuxDeviceConfiguration(settings));
}
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const ConstPtr &other)
......@@ -126,18 +125,14 @@ LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QString &name, const QS
d->attributes = attributes;
}
LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QSettings &settings, Id &nextId)
LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QSettings &settings)
: d(new LinuxDeviceConfigurationPrivate(SshConnectionParameters::NoProxy))
{
d->origin = ManuallyAdded;
d->displayName = settings.value(NameKey).toString();
d->osType = settings.value(OsTypeKey).toString();
d->deviceType = static_cast<DeviceType>(settings.value(TypeKey, DefaultDeviceType).toInt());
d->internalId = settings.value(InternalIdKey, nextId).toULongLong();
if (d->internalId == nextId)
++nextId;
d->internalId = settings.value(InternalIdKey, InvalidId).toULongLong();
d->attributes = settings.value(AttributesKey).toHash();
// Convert from version < 2.3.
......
......@@ -97,13 +97,13 @@ private:
LinuxDeviceConfiguration(const QString &name, const QString &osType, DeviceType deviceType,
const Utils::PortList &freePorts, const Utils::SshConnectionParameters &sshParams,
const QVariantHash &attributes, Origin origin);
LinuxDeviceConfiguration(const QSettings &settings, Id &nextId);
LinuxDeviceConfiguration(const QSettings &settings);
LinuxDeviceConfiguration(const ConstPtr &other);
LinuxDeviceConfiguration(const LinuxDeviceConfiguration &);
LinuxDeviceConfiguration &operator=(const LinuxDeviceConfiguration &);
static Ptr create(const QSettings &settings, Id &nextId);
static Ptr create(const QSettings &settings);
static Ptr create(const ConstPtr &other);
void setDisplayName(const QString &name);
......
......@@ -43,13 +43,13 @@
#include <QVariantHash>
#include <algorithm>
#include <limits>
namespace RemoteLinux {
namespace Internal {
namespace {
const QLatin1String SettingsGroup("MaemoDeviceConfigs");
const QLatin1String IdCounterKey("IdCounter");
const QLatin1String ConfigListKey("ConfigList");
const QLatin1String DefaultKeyFilePathKey("DefaultKeyFile");
const char DefaultConfigsKey[] = "DefaultConfigs";
......@@ -73,7 +73,6 @@ class LinuxDeviceConfigurationsPrivate
public:
static LinuxDeviceConfigurations *instance;
static LinuxDeviceConfigurations *clonedInstance;
LinuxDeviceConfiguration::Id nextId;
QList<LinuxDeviceConfiguration::Ptr> devConfigs;
QHash<QString, LinuxDeviceConfiguration::Id> defaultConfigs;
QString defaultSshKeyFilePath;
......@@ -138,7 +137,6 @@ void LinuxDeviceConfigurations::copy(const LinuxDeviceConfigurations *source,
target->d->devConfigs = source->d->devConfigs;
}
target->d->defaultSshKeyFilePath = source->d->defaultSshKeyFilePath;
target->d->nextId = source->d->nextId;
target->d->defaultConfigs = source->d->defaultConfigs;
}
......@@ -146,7 +144,6 @@ void LinuxDeviceConfigurations::save()
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(SettingsGroup);
settings->setValue(IdCounterKey, d->nextId);
settings->setValue(DefaultKeyFilePathKey, d->defaultSshKeyFilePath);
QVariantHash defaultDevsHash;
for (QHash<QString, LinuxDeviceConfiguration::Id>::ConstIterator it = d->defaultConfigs.constBegin();
......@@ -184,9 +181,7 @@ void LinuxDeviceConfigurations::addConfiguration(const LinuxDeviceConfiguration:
while (hasConfig(name));
}
devConfig->setDisplayName(name);
devConfig->setInternalId(d->nextId++);
devConfig->setInternalId(unusedId());
if (!defaultDeviceConfig(devConfig->osType()))
d->defaultConfigs.insert(devConfig->osType(), devConfig->internalId());
d->devConfigs << devConfig;
......@@ -292,7 +287,6 @@ void LinuxDeviceConfigurations::load()
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup(SettingsGroup);
d->nextId = settings->value(IdCounterKey, 1).toULongLong();
d->defaultSshKeyFilePath = settings->value(DefaultKeyFilePathKey,
LinuxDeviceConfiguration::defaultPrivateKeyFilePath()).toString();
const QVariantHash defaultDevsHash = settings->value(QLatin1String(DefaultConfigsKey)).toHash();
......@@ -303,8 +297,9 @@ void LinuxDeviceConfigurations::load()
int count = settings->beginReadArray(ConfigListKey);
for (int i = 0; i < count; ++i) {
settings->setArrayIndex(i);
LinuxDeviceConfiguration::Ptr devConf
= LinuxDeviceConfiguration::create(*settings, d->nextId);
LinuxDeviceConfiguration::Ptr devConf = LinuxDeviceConfiguration::create(*settings);
if (devConf->internalId() == LinuxDeviceConfiguration::InvalidId)
devConf->setInternalId(unusedId());
d->devConfigs << devConf;
}
settings->endArray();
......@@ -363,4 +358,15 @@ void LinuxDeviceConfigurations::ensureOneDefaultConfigurationPerOsType()
}
}
LinuxDeviceConfiguration::Id LinuxDeviceConfigurations::unusedId() const
{
typedef LinuxDeviceConfiguration::Id IdType;
for (IdType id = 0; id <= std::numeric_limits<IdType>::max(); ++id) {
if (!find(id))
return id;
}
QTC_CHECK(false);
return LinuxDeviceConfiguration::InvalidId;
}
} // namespace RemoteLinux
......@@ -93,6 +93,7 @@ private:
static void copy(const LinuxDeviceConfigurations *source,
LinuxDeviceConfigurations *target, bool deep);
void ensureOneDefaultConfigurationPerOsType();
LinuxDeviceConfiguration::Id unusedId() const;
Internal::LinuxDeviceConfigurationsPrivate * const d;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment