linuxdeviceconfiguration.cpp 9.31 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
**
**
** 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
29
** Nokia at qt-info@nokia.com.
30
31
32
33
**
**************************************************************************/
#include "linuxdeviceconfiguration.h"

34
#include "portlist.h"
35
36
#include "remotelinux_constants.h"

37
38
#include <utils/ssh/sshconnection.h>

39
40
#include <QSettings>
#include <QDesktopServices>
41

42
43
using namespace Utils;
typedef SshConnectionParameters::AuthenticationType AuthType;
44
45

namespace RemoteLinux {
46
namespace Internal {
47
48
namespace {
const QLatin1String NameKey("Name");
49
50
const QLatin1String OldOsVersionKey("OsVersion"); // Outdated, only use for upgrading.
const QLatin1String OsTypeKey("OsType");
51
52
53
54
55
56
57
58
59
60
61
const QLatin1String TypeKey("Type");
const QLatin1String HostKey("Host");
const QLatin1String SshPortKey("SshPort");
const QLatin1String PortsSpecKey("FreePortsSpec");
const QLatin1String UserNameKey("Uname");
const QLatin1String AuthKey("Authentication");
const QLatin1String KeyFileKey("KeyFile");
const QLatin1String PasswordKey("Password");
const QLatin1String TimeoutKey("Timeout");
const QLatin1String IsDefaultKey("IsDefault");
const QLatin1String InternalIdKey("InternalId");
62
const QLatin1String AttributesKey("Attributes");
63

64
const AuthType DefaultAuthType(SshConnectionParameters::AuthenticationByKey);
65
const int DefaultTimeout(10);
66
const LinuxDeviceConfiguration::DeviceType DefaultDeviceType(LinuxDeviceConfiguration::Hardware);
67
68
} // anonymous namespace

69
70
71
72
73
74
75
76
77
class LinuxDeviceConfigurationPrivate
{
public:
    LinuxDeviceConfigurationPrivate(const SshConnectionParameters &sshParameters)
        : sshParameters(sshParameters)
    {
    }

    SshConnectionParameters sshParameters;
78
    QString displayName;
79
80
81
82
    QString osType;
    LinuxDeviceConfiguration::DeviceType deviceType;
    PortList freePorts;
    bool isDefault;
83
    LinuxDeviceConfiguration::Origin origin;
84
    LinuxDeviceConfiguration::Id internalId;
85
    QVariantHash attributes;
86
};
87

88
89
90
91
92
93
} // namespace Internal

using namespace Internal;

LinuxDeviceConfiguration::~LinuxDeviceConfiguration()
{
hjk's avatar
hjk committed
94
    delete d;
95
}
96
97
98
99
100
101
102
103
104
105
106
107

LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QSettings &settings,
    Id &nextId)
{
    return Ptr(new LinuxDeviceConfiguration(settings, nextId));
}

LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const ConstPtr &other)
{
    return Ptr(new LinuxDeviceConfiguration(other));
}

108
LinuxDeviceConfiguration::Ptr LinuxDeviceConfiguration::create(const QString &name,
109
    const QString &osType, DeviceType deviceType, const PortList &freePorts,
110
    const SshConnectionParameters &sshParams, const QVariantHash &attributes, Origin origin)
111
{
112
113
    return Ptr(new LinuxDeviceConfiguration(name, osType, deviceType, freePorts, sshParams,
        attributes, origin));
114
115
}

116
LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QString &name, const QString &osType,
117
        DeviceType deviceType, const PortList &freePorts, const SshConnectionParameters &sshParams,
118
        const QVariantHash &attributes, Origin origin)
hjk's avatar
hjk committed
119
    : d(new LinuxDeviceConfigurationPrivate(sshParams))
120
{
121
    d->displayName = name;
hjk's avatar
hjk committed
122
123
124
125
126
    d->osType = osType;
    d->deviceType = deviceType;
    d->freePorts = freePorts;
    d->isDefault = false;
    d->origin = origin;
127
    d->attributes = attributes;
128
129
}

130
LinuxDeviceConfiguration::LinuxDeviceConfiguration(const QSettings &settings, Id &nextId)
hjk's avatar
hjk committed
131
    : d(new LinuxDeviceConfigurationPrivate(SshConnectionParameters::NoProxy))
132
{
hjk's avatar
hjk committed
133
    d->origin = ManuallyAdded;
134
    d->displayName = settings.value(NameKey).toString();
hjk's avatar
hjk committed
135
136
137
138
    d->osType = settings.value(OsTypeKey).toString();
    d->deviceType = static_cast<DeviceType>(settings.value(TypeKey, DefaultDeviceType).toInt());
    d->isDefault = settings.value(IsDefaultKey, false).toBool();
    d->internalId = settings.value(InternalIdKey, nextId).toULongLong();
139

hjk's avatar
hjk committed
140
    if (d->internalId == nextId)
141
        ++nextId;
142

143
144
    d->attributes = settings.value(AttributesKey).toHash();

145
    // Convert from version < 2.3.
hjk's avatar
hjk committed
146
    if (d->osType.isEmpty()) {
147
148
        const int oldOsType = settings.value(OldOsVersionKey, -1).toInt();
        switch (oldOsType) {
hjk's avatar
hjk committed
149
150
151
152
        case 0: d->osType = QLatin1String("Maemo5OsType"); break;
        case 1: d->osType = QLatin1String("HarmattanOsType"); break;
        case 2: d->osType = QLatin1String("MeeGoOsType"); break;
        default: d->osType = QLatin1String(Constants::GenericLinuxOsType);
153
154
155
        }
    }

hjk's avatar
hjk committed
156
157
158
159
160
    d->freePorts = PortList::fromString(settings.value(PortsSpecKey, QLatin1String("10000-10100")).toString());
    d->sshParameters.host = settings.value(HostKey).toString();
    d->sshParameters.port = settings.value(SshPortKey, 22).toInt();
    d->sshParameters.userName = settings.value(UserNameKey).toString();
    d->sshParameters.authenticationType
161
        = static_cast<AuthType>(settings.value(AuthKey, DefaultAuthType).toInt());
hjk's avatar
hjk committed
162
163
    d->sshParameters.password = settings.value(PasswordKey).toString();
    d->sshParameters.privateKeyFile
164
        = settings.value(KeyFileKey, defaultPrivateKeyFilePath()).toString();
hjk's avatar
hjk committed
165
    d->sshParameters.timeout = settings.value(TimeoutKey, DefaultTimeout).toInt();
166
167
168
}

LinuxDeviceConfiguration::LinuxDeviceConfiguration(const LinuxDeviceConfiguration::ConstPtr &other)
hjk's avatar
hjk committed
169
    : d(new LinuxDeviceConfigurationPrivate(other->d->sshParameters))
170
{
171
    d->displayName = other->d->displayName;
hjk's avatar
hjk committed
172
173
174
175
176
177
    d->osType = other->d->osType;
    d->deviceType = other->deviceType();
    d->freePorts = other->freePorts();
    d->isDefault = other->d->isDefault;
    d->origin = other->d->origin;
    d->internalId = other->d->internalId;
178
    d->attributes = other->d->attributes;
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
}

QString LinuxDeviceConfiguration::defaultPrivateKeyFilePath()
{
    return QDesktopServices::storageLocation(QDesktopServices::HomeLocation)
        + QLatin1String("/.ssh/id_rsa");
}

QString LinuxDeviceConfiguration::defaultPublicKeyFilePath()
{
    return defaultPrivateKeyFilePath() + QLatin1String(".pub");
}

void LinuxDeviceConfiguration::save(QSettings &settings) const
{
194
    settings.setValue(NameKey, d->displayName);
hjk's avatar
hjk committed
195
196
197
198
199
200
201
202
203
204
205
206
    settings.setValue(OsTypeKey, d->osType);
    settings.setValue(TypeKey, d->deviceType);
    settings.setValue(HostKey, d->sshParameters.host);
    settings.setValue(SshPortKey, d->sshParameters.port);
    settings.setValue(PortsSpecKey, d->freePorts.toString());
    settings.setValue(UserNameKey, d->sshParameters.userName);
    settings.setValue(AuthKey, d->sshParameters.authenticationType);
    settings.setValue(PasswordKey, d->sshParameters.password);
    settings.setValue(KeyFileKey, d->sshParameters.privateKeyFile);
    settings.setValue(TimeoutKey, d->sshParameters.timeout);
    settings.setValue(IsDefaultKey, d->isDefault);
    settings.setValue(InternalIdKey, d->internalId);
207
    settings.setValue(AttributesKey, d->attributes);
208
209
}

210
211
SshConnectionParameters LinuxDeviceConfiguration::sshParameters() const
{
hjk's avatar
hjk committed
212
    return d->sshParameters;
213
214
215
}

LinuxDeviceConfiguration::DeviceType LinuxDeviceConfiguration::deviceType() const
216

217
{
hjk's avatar
hjk committed
218
    return d->deviceType;
219
220
221
222
}

LinuxDeviceConfiguration::Id LinuxDeviceConfiguration::internalId() const
{
hjk's avatar
hjk committed
223
    return d->internalId;
224
225
226
227
}

void LinuxDeviceConfiguration::setSshParameters(const SshConnectionParameters &sshParameters)
{
hjk's avatar
hjk committed
228
    d->sshParameters = sshParameters;
229
230
231
232
}

void LinuxDeviceConfiguration::setFreePorts(const PortList &freePorts)
{
hjk's avatar
hjk committed
233
    d->freePorts = freePorts;
234
235
}

236
237
238
239
240
void LinuxDeviceConfiguration::setAttribute(const QString &name, const QVariant &value)
{
    d->attributes[name] = value;
}

241
242
bool LinuxDeviceConfiguration::isAutoDetected() const
{
hjk's avatar
hjk committed
243
    return d->origin == AutoDetected;
244
245
}

246
247
248
249
250
QVariantHash LinuxDeviceConfiguration::attributes() const
{
    return d->attributes;
}

251
252
253
254
255
QVariant LinuxDeviceConfiguration::attribute(const QString &name) const
{
    return d->attributes.value(name);
}

hjk's avatar
hjk committed
256
PortList LinuxDeviceConfiguration::freePorts() const { return d->freePorts; }
257
QString LinuxDeviceConfiguration::displayName() const { return d->displayName; }
hjk's avatar
hjk committed
258
259
QString LinuxDeviceConfiguration::osType() const { return d->osType; }
bool LinuxDeviceConfiguration::isDefault() const { return d->isDefault; }
260

261
void LinuxDeviceConfiguration::setDisplayName(const QString &name) { d->displayName = name; }
hjk's avatar
hjk committed
262
263
void LinuxDeviceConfiguration::setInternalId(Id id) { d->internalId = id; }
void LinuxDeviceConfiguration::setDefault(bool isDefault) { d->isDefault = isDefault; }
264

265
const LinuxDeviceConfiguration::Id LinuxDeviceConfiguration::InvalidId = 0;
266

267
} // namespace RemoteLinux