perforcesettings.cpp 8.57 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** 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.
**
con's avatar
con committed
28
**
29
**************************************************************************/
hjk's avatar
hjk committed
30

con's avatar
con committed
31
#include "perforcesettings.h"
32 33 34 35
#include "perforceplugin.h"
#include "perforceconstants.h"

#include <utils/qtcassert.h>
36
#include <utils/environment.h>
37
#include <utils/hostosinfo.h>
con's avatar
con committed
38

39
#include <QDebug>
40 41 42 43 44
#include <QSettings>
#include <QStringList>
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
Friedemann Kleint's avatar
Friedemann Kleint committed
45

46 47 48 49 50 51 52 53 54 55
static const char groupC[] = "Perforce";
static const char commandKeyC[] = "Command";
static const char defaultKeyC[] = "Default";
static const char portKeyC[] = "Port";
static const char clientKeyC[] = "Client";
static const char userKeyC[] = "User";
static const char promptToSubmitKeyC[] = "PromptForSubmit";
static const char autoOpenKeyC[] = "PromptToOpen";
static const char timeOutKeyC[] = "TimeOut";
static const char logCountKeyC[] = "LogCount";
56

57
enum { defaultTimeOutS = 30, defaultLogCount = 1000 };
con's avatar
con committed
58 59 60

static QString defaultCommand()
{
61
    return QLatin1String("p4" QTC_HOST_EXE_SUFFIX);
con's avatar
con committed
62 63 64 65 66
}

namespace Perforce {
namespace Internal {

Friedemann Kleint's avatar
Friedemann Kleint committed
67
Settings::Settings() :
68
    logCount(defaultLogCount),
69
    defaultEnv(true),
70
    timeOutS(defaultTimeOutS),
71 72
    promptToSubmit(true),
    autoOpen(true)
Friedemann Kleint's avatar
Friedemann Kleint committed
73 74 75 76 77 78
{
}

bool Settings::equals(const Settings &rhs) const
{
    return defaultEnv == rhs.defaultEnv
79
            && logCount == rhs.logCount
Friedemann Kleint's avatar
Friedemann Kleint committed
80
            && p4Command == rhs.p4Command && p4Port == rhs.p4Port
81
            && p4Client == rhs.p4Client && p4User == rhs.p4User
82 83
            && timeOutS == rhs.timeOutS && promptToSubmit == rhs.promptToSubmit
            && autoOpen == rhs.autoOpen;
84
}
Friedemann Kleint's avatar
Friedemann Kleint committed
85

86
QStringList Settings::commonP4Arguments() const
Friedemann Kleint's avatar
Friedemann Kleint committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
    if (defaultEnv)
        return QStringList();
    QStringList lst;
    if (!p4Client.isEmpty())
        lst << QLatin1String("-c") << p4Client;
    if (!p4Port.isEmpty())
        lst << QLatin1String("-p") << p4Port;
    if (!p4User.isEmpty())
        lst << QLatin1String("-u") << p4User;
    return lst;
}

// --------------------PerforceSettings
101
PerforceSettings::PerforceSettings()
con's avatar
con committed
102
{
103 104 105 106
}

PerforceSettings::~PerforceSettings()
{
con's avatar
con committed
107 108 109 110 111
}

void PerforceSettings::fromSettings(QSettings *settings)
{
    settings->beginGroup(QLatin1String(groupC));
Friedemann Kleint's avatar
Friedemann Kleint committed
112
    m_settings.p4Command = settings->value(QLatin1String(commandKeyC), defaultCommand()).toString();
113
    m_settings.p4BinaryPath = Utils::Environment::systemEnvironment().searchInPath(m_settings.p4Command);
Friedemann Kleint's avatar
Friedemann Kleint committed
114 115 116 117
    m_settings.defaultEnv = settings->value(QLatin1String(defaultKeyC), true).toBool();
    m_settings.p4Port = settings->value(QLatin1String(portKeyC), QString()).toString();
    m_settings.p4Client = settings->value(QLatin1String(clientKeyC), QString()).toString();
    m_settings.p4User = settings->value(QLatin1String(userKeyC), QString()).toString();
118
    m_settings.timeOutS = settings->value(QLatin1String(timeOutKeyC), defaultTimeOutS).toInt();
119
    m_settings.promptToSubmit = settings->value(QLatin1String(promptToSubmitKeyC), true).toBool();
120
    m_settings.logCount = settings->value(QLatin1String(logCountKeyC), int(defaultLogCount)).toInt();
121
    m_settings.autoOpen = settings->value(QLatin1String(autoOpenKeyC), true).toBool();
con's avatar
con committed
122 123 124 125 126 127
    settings->endGroup();
}

void PerforceSettings::toSettings(QSettings *settings) const
{
    settings->beginGroup(QLatin1String(groupC));
128 129 130 131 132
    settings->setValue(QLatin1String(commandKeyC), m_settings.p4Command);
    settings->setValue(QLatin1String(defaultKeyC), m_settings.defaultEnv);
    settings->setValue(QLatin1String(portKeyC), m_settings.p4Port);
    settings->setValue(QLatin1String(clientKeyC), m_settings.p4Client);
    settings->setValue(QLatin1String(userKeyC), m_settings.p4User);
133
    settings->setValue(QLatin1String(timeOutKeyC), m_settings.timeOutS);
134
    settings->setValue(QLatin1String(promptToSubmitKeyC), m_settings.promptToSubmit);
135
    settings->setValue(QLatin1String(logCountKeyC), m_settings.logCount);
136
    settings->setValue(QLatin1String(autoOpenKeyC), m_settings.autoOpen);
con's avatar
con committed
137 138 139
    settings->endGroup();
}

Friedemann Kleint's avatar
Friedemann Kleint committed
140
void PerforceSettings::setSettings(const Settings &newSettings)
141
{
Friedemann Kleint's avatar
Friedemann Kleint committed
142 143
    if (newSettings != m_settings) {
        m_settings = newSettings;
144
        clearTopLevel();
Friedemann Kleint's avatar
Friedemann Kleint committed
145 146 147 148
    }
}

Settings PerforceSettings::settings() const
con's avatar
con committed
149
{
Friedemann Kleint's avatar
Friedemann Kleint committed
150
    return m_settings;
con's avatar
con committed
151 152
}

153 154
QString PerforceSettings::p4Command() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
155
    return m_settings.p4Command;
156 157
}

158 159 160 161 162
QString PerforceSettings::p4BinaryPath() const
{
    return m_settings.p4BinaryPath;
}

163 164
QString PerforceSettings::p4Port() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
165
    return m_settings.p4Port;
166 167 168 169
}

QString PerforceSettings::p4Client() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
170
    return m_settings.p4Client;
171 172 173 174
}

QString PerforceSettings::p4User() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
175
    return m_settings.p4User;
176 177 178 179
}

bool PerforceSettings::defaultEnv() const
{
Friedemann Kleint's avatar
Friedemann Kleint committed
180
    return m_settings.defaultEnv;
181 182
}

183 184 185 186 187 188 189 190 191 192
bool PerforceSettings::promptToSubmit() const
{
    return m_settings.promptToSubmit;
}

void PerforceSettings::setPromptToSubmit(bool p)
{
    m_settings.promptToSubmit = p;
}

193 194 195 196 197 198 199 200 201 202
bool PerforceSettings::autoOpen() const
{
    return m_settings.autoOpen;
}

void PerforceSettings::setAutoOpen(bool b)
{
    m_settings.autoOpen = b;
}

203
QString PerforceSettings::topLevel() const
204
{
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    return m_topLevel;
}

QString PerforceSettings::topLevelSymLinkTarget() const
{
    return m_topLevelSymLinkTarget;
}

void PerforceSettings::setTopLevel(const QString &t)
{
    if (m_topLevel == t)
        return;
    clearTopLevel();
    if (!t.isEmpty()) {
        // Check/expand symlinks as creator always has expanded file paths
        QFileInfo fi(t);
        if (fi.isSymLink()) {
            m_topLevel = t;
            m_topLevelSymLinkTarget = QFileInfo(fi.symLinkTarget()).absoluteFilePath();
        } else {
            m_topLevelSymLinkTarget = m_topLevel = t;
        }
        m_topLevelDir.reset(new QDir(m_topLevelSymLinkTarget));
        if (Perforce::Constants::debug)
            qDebug() << "PerforceSettings::setTopLevel" << m_topLevel << m_topLevelSymLinkTarget;
    }
}

void PerforceSettings::clearTopLevel()
{
    m_topLevelDir.reset();
    m_topLevel.clear();
}

QString PerforceSettings::relativeToTopLevel(const QString &dir) const
{
241
    QTC_ASSERT(!m_topLevelDir.isNull(), return QLatin1String("../") + dir);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    return m_topLevelDir->relativeFilePath(dir);
}

QStringList PerforceSettings::relativeToTopLevelArguments(const QString &dir) const
{
    const QString relative = relativeToTopLevel(dir);
    return relative.isEmpty() ? QStringList() : QStringList(relative);
}

// Map the root part of a path:
// Calling "/home/john/foo" with old="/home", new="/user"
// results in "/user/john/foo"

static inline QString mapPathRoot(const QString &path,
                                  const QString &oldPrefix,
                                  const QString &newPrefix)
{
    if (path.isEmpty() || oldPrefix.isEmpty() || newPrefix.isEmpty() || oldPrefix == newPrefix)
        return path;
    if (path == oldPrefix)
        return newPrefix;
    if (path.startsWith(oldPrefix))
        return newPrefix + path.right(path.size() - oldPrefix.size());
    return path;
}

QStringList PerforceSettings::commonP4Arguments(const QString &workingDir) const
{
    QStringList rc;
    if (!workingDir.isEmpty()) {
        /* Determine the -d argument for the working directory for matching relative paths.
         * It is is below the toplevel, replace top level portion by exact specification. */
        rc << QLatin1String("-d")
           << QDir::toNativeSeparators(mapPathRoot(workingDir, m_topLevelSymLinkTarget, m_topLevel));
    }
    rc.append(m_settings.commonP4Arguments());
Friedemann Kleint's avatar
Friedemann Kleint committed
278
    return rc;
279 280
}

281
QString PerforceSettings::mapToFileSystem(const QString &perforceFilePath) const
Friedemann Kleint's avatar
Friedemann Kleint committed
282
{
283
    return mapPathRoot(perforceFilePath, m_topLevel, m_topLevelSymLinkTarget);
Friedemann Kleint's avatar
Friedemann Kleint committed
284
}
285

hjk's avatar
hjk committed
286 287
} // Internal
} // Perforce