summaryrefslogtreecommitdiff
path: root/src/plugins/perforce/perforcesettings.cpp
blob: c49e7dedc07d97d393b407ebe6c7a6eeb90ea2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
241
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/

#include "perforcesettings.h"

#include <qtconcurrent/QtConcurrentTools>
#include <QtCore/QtConcurrentRun>
#include <QtCore/QSettings>
#include <QtCore/QStringList>
#include <QtCore/QCoreApplication>
#include <QtCore/QProcess>

enum { debug = 0 };

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 QString defaultCommand()
{
    QString rc;
    rc = QLatin1String("p4");
#if defined(Q_OS_WIN32)
    rc.append(QLatin1String(".exe"));
#endif
    return rc;
}

namespace Perforce {
namespace Internal {

Settings::Settings() :
    defaultEnv(true)
{
}

bool Settings::equals(const Settings &rhs) const
{
    return defaultEnv == rhs.defaultEnv
            && p4Command == rhs.p4Command && p4Port == rhs.p4Port
            && p4Client == rhs.p4Client && p4User == rhs.p4User;
};

QStringList Settings::basicP4Args() const
{
    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;
}

bool Settings::check(QString *errorMessage) const
{
    return doCheck(p4Command, basicP4Args(), errorMessage);
}

// Check on a p4 view by grepping "view -o" for mapped files
bool Settings::doCheck(const QString &binary, const QStringList &basicArgs, QString *errorMessage)
{
    errorMessage->clear();
    if (binary.isEmpty()) {
        *errorMessage = QCoreApplication::translate("Perforce::Internal",  "No executable specified");
        return false;
    }
    // List the client state and check for mapped files
    QProcess p4;
    QStringList args = basicArgs;
    args << QLatin1String("client") << QLatin1String("-o");
    if (debug)
        qDebug() << binary << args;
    p4.start(binary, args);
    if (!p4.waitForStarted()) {
        *errorMessage = QCoreApplication::translate("Perforce::Internal", "Unable to launch \"%1\": %2").arg(binary, p4.errorString());
        return false;
    }
    p4.closeWriteChannel();
    const int timeOutMS = 5000;
    if (!p4.waitForFinished(timeOutMS)) {
        p4.kill();
        p4.waitForFinished();
        *errorMessage = QCoreApplication::translate("Perforce::Internal", "\"%1\" timed out after %2ms.").arg(binary).arg(timeOutMS);
        return false;
    }
    if (p4.exitStatus() != QProcess::NormalExit) {
        *errorMessage = QCoreApplication::translate("Perforce::Internal", "\"%1\" crashed.").arg(binary);
        return false;
    }
    const QString stdErr = QString::fromLocal8Bit(p4.readAllStandardError());
    if (p4.exitCode()) {
        *errorMessage = QCoreApplication::translate("Perforce::Internal", "\"%1\" terminated with exit code %2: %3").
                        arg(binary).arg(p4.exitCode()).arg(stdErr);
        return false;

    }
    // List the client state and check for "View"
    const QString response = QString::fromLocal8Bit(p4.readAllStandardOutput());
    if (!response.contains(QLatin1String("View:")) && !response.contains(QLatin1String("//depot/"))) {
        *errorMessage = QCoreApplication::translate("Perforce::Internal", "The client does not seem to contain any mapped files.");
        return false;
    }
    return true;
}

// --------------------PerforceSettings
PerforceSettings::PerforceSettings()
    : m_valid(false)
{
    // We do all the initialization in fromSettings
}

PerforceSettings::~PerforceSettings()
{
    // ensure that we are not still running
    m_future.waitForFinished();
}

bool PerforceSettings::isValid() const
{
    m_future.waitForFinished();
    m_mutex.lock();
    bool valid = m_valid;
    m_mutex.unlock();
    return valid;
}

void PerforceSettings::run(QFutureInterface<void> &fi)
{
    m_mutex.lock();
    const QString executable = m_settings.p4Command;
    const QStringList arguments = basicP4Args();
    m_mutex.unlock();

    QString errorString;
    const bool isValid = Settings::doCheck(executable, arguments, &errorString);
    if (debug)
        qDebug() << isValid << errorString;

    m_mutex.lock();
    if (executable == m_settings.p4Command && arguments == basicP4Args()) { // Check that those settings weren't changed in between
        m_errorString = errorString;
        m_valid = isValid;
    }
    m_mutex.unlock();
    fi.reportFinished();
}

void PerforceSettings::fromSettings(QSettings *settings)
{
    m_mutex.lock();
    settings->beginGroup(QLatin1String(groupC));
    m_settings.p4Command = settings->value(QLatin1String(commandKeyC), defaultCommand()).toString();
    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();
    settings->endGroup();
    m_mutex.unlock();

    m_future = QtConcurrent::run(&PerforceSettings::run, this);
}

void PerforceSettings::toSettings(QSettings *settings) const
{
    m_mutex.lock();
    settings->beginGroup(QLatin1String(groupC));
    settings->setValue(commandKeyC, m_settings.p4Command);
    settings->setValue(defaultKeyC, m_settings.defaultEnv);
    settings->setValue(portKeyC, m_settings.p4Port);
    settings->setValue(clientKeyC, m_settings.p4Client);
    settings->setValue(userKeyC, m_settings.p4User);
    settings->endGroup();
    m_mutex.unlock();
}

void PerforceSettings::setSettings(const Settings &newSettings)
{    
    if (newSettings != m_settings) {
        // trigger check
        m_settings = newSettings;
        m_mutex.lock();
        m_valid = false;
        m_mutex.unlock();
        m_future = QtConcurrent::run(&PerforceSettings::run, this);
    }
}

Settings PerforceSettings::settings() const
{
    return m_settings;
}

QString PerforceSettings::p4Command() const
{
    return m_settings.p4Command;
}

QString PerforceSettings::p4Port() const
{
    return m_settings.p4Port;
}

QString PerforceSettings::p4Client() const
{
    return m_settings.p4Client;
}

QString PerforceSettings::p4User() const
{
    return m_settings.p4User;
}

bool PerforceSettings::defaultEnv() const
{
    return m_settings.defaultEnv;
}

QString PerforceSettings::errorString() const
{
    m_mutex.lock();
    const QString rc = m_errorString;
    m_mutex.unlock();
    return rc;
}

QStringList PerforceSettings::basicP4Args() const
{
    return m_settings.basicP4Args();
}

} // Internal
} // Perforce