1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "perforcesettings.h"
34 #include "perforceplugin.h"
35 #include "perforceconstants.h"
37 #include <utils/qtcassert.h>
38 #include <utils/environment.h>
40 #include <QtCore/QSettings>
41 #include <QtCore/QStringList>
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/QDir>
44 #include <QtCore/QFileInfo>
46 static const char *groupC = "Perforce";
47 static const char *commandKeyC = "Command";
48 static const char *defaultKeyC = "Default";
49 static const char *portKeyC = "Port";
50 static const char *clientKeyC = "Client";
51 static const char *userKeyC = "User";
52 static const char *promptToSubmitKeyC = "PromptForSubmit";
53 static const char *autoOpenKeyC = "PromptToOpen";
54 static const char *timeOutKeyC = "TimeOut";
55 static const char *logCountKeyC = "LogCount";
57 enum { defaultTimeOutS = 30, defaultLogCount = 1000 };
59 static QString defaultCommand()
61 Utils::Environment env = Utils::Environment::systemEnvironment();
63 rc = QLatin1String("p4");
64 #if defined(Q_OS_WIN32)
65 rc.append(QLatin1String(".exe"));
67 return env.searchInPath(rc);
73 Settings::Settings() :
74 logCount(defaultLogCount),
76 timeOutS(defaultTimeOutS),
82 bool Settings::equals(const Settings &rhs) const
84 return defaultEnv == rhs.defaultEnv
85 && logCount == rhs.logCount
86 && p4Command == rhs.p4Command && p4Port == rhs.p4Port
87 && p4Client == rhs.p4Client && p4User == rhs.p4User
88 && timeOutS == rhs.timeOutS && promptToSubmit == rhs.promptToSubmit
89 && autoOpen == rhs.autoOpen;
92 QStringList Settings::commonP4Arguments() const
97 if (!p4Client.isEmpty())
98 lst << QLatin1String("-c") << p4Client;
99 if (!p4Port.isEmpty())
100 lst << QLatin1String("-p") << p4Port;
101 if (!p4User.isEmpty())
102 lst << QLatin1String("-u") << p4User;
106 // --------------------PerforceSettings
107 PerforceSettings::PerforceSettings()
111 PerforceSettings::~PerforceSettings()
115 void PerforceSettings::fromSettings(QSettings *settings)
117 settings->beginGroup(QLatin1String(groupC));
118 m_settings.p4Command = settings->value(QLatin1String(commandKeyC), defaultCommand()).toString();
119 m_settings.defaultEnv = settings->value(QLatin1String(defaultKeyC), true).toBool();
120 m_settings.p4Port = settings->value(QLatin1String(portKeyC), QString()).toString();
121 m_settings.p4Client = settings->value(QLatin1String(clientKeyC), QString()).toString();
122 m_settings.p4User = settings->value(QLatin1String(userKeyC), QString()).toString();
123 m_settings.timeOutS = settings->value(QLatin1String(timeOutKeyC), defaultTimeOutS).toInt();
124 m_settings.promptToSubmit = settings->value(QLatin1String(promptToSubmitKeyC), true).toBool();
125 m_settings.logCount = settings->value(QLatin1String(logCountKeyC), int(defaultLogCount)).toInt();
126 m_settings.autoOpen = settings->value(QLatin1String(autoOpenKeyC), true).toBool();
127 settings->endGroup();
130 void PerforceSettings::toSettings(QSettings *settings) const
132 settings->beginGroup(QLatin1String(groupC));
133 settings->setValue(QLatin1String(commandKeyC), m_settings.p4Command);
134 settings->setValue(QLatin1String(defaultKeyC), m_settings.defaultEnv);
135 settings->setValue(QLatin1String(portKeyC), m_settings.p4Port);
136 settings->setValue(QLatin1String(clientKeyC), m_settings.p4Client);
137 settings->setValue(QLatin1String(userKeyC), m_settings.p4User);
138 settings->setValue(QLatin1String(timeOutKeyC), m_settings.timeOutS);
139 settings->setValue(QLatin1String(promptToSubmitKeyC), m_settings.promptToSubmit);
140 settings->setValue(QLatin1String(logCountKeyC), m_settings.logCount);
141 settings->setValue(QLatin1String(autoOpenKeyC), m_settings.autoOpen);
142 settings->endGroup();
145 void PerforceSettings::setSettings(const Settings &newSettings)
147 if (newSettings != m_settings) {
148 m_settings = newSettings;
153 Settings PerforceSettings::settings() const
158 QString PerforceSettings::p4Command() const
160 return m_settings.p4Command;
163 QString PerforceSettings::p4Port() const
165 return m_settings.p4Port;
168 QString PerforceSettings::p4Client() const
170 return m_settings.p4Client;
173 QString PerforceSettings::p4User() const
175 return m_settings.p4User;
178 bool PerforceSettings::defaultEnv() const
180 return m_settings.defaultEnv;
183 bool PerforceSettings::promptToSubmit() const
185 return m_settings.promptToSubmit;
188 void PerforceSettings::setPromptToSubmit(bool p)
190 m_settings.promptToSubmit = p;
193 bool PerforceSettings::autoOpen() const
195 return m_settings.autoOpen;
198 void PerforceSettings::setAutoOpen(bool b)
200 m_settings.autoOpen = b;
203 QString PerforceSettings::topLevel() const
208 QString PerforceSettings::topLevelSymLinkTarget() const
210 return m_topLevelSymLinkTarget;
213 void PerforceSettings::setTopLevel(const QString &t)
219 // Check/expand symlinks as creator always has expanded file paths
221 if (fi.isSymLink()) {
223 m_topLevelSymLinkTarget = QFileInfo(fi.symLinkTarget()).absoluteFilePath();
225 m_topLevelSymLinkTarget = m_topLevel = t;
227 m_topLevelDir.reset(new QDir(m_topLevelSymLinkTarget));
228 if (Perforce::Constants::debug)
229 qDebug() << "PerforceSettings::setTopLevel" << m_topLevel << m_topLevelSymLinkTarget;
233 void PerforceSettings::clearTopLevel()
235 m_topLevelDir.reset();
239 QString PerforceSettings::relativeToTopLevel(const QString &dir) const
241 QTC_ASSERT(!m_topLevelDir.isNull(), return QLatin1String("../") + dir)
242 return m_topLevelDir->relativeFilePath(dir);
245 QStringList PerforceSettings::relativeToTopLevelArguments(const QString &dir) const
247 const QString relative = relativeToTopLevel(dir);
248 return relative.isEmpty() ? QStringList() : QStringList(relative);
251 // Map the root part of a path:
252 // Calling "/home/john/foo" with old="/home", new="/user"
253 // results in "/user/john/foo"
255 static inline QString mapPathRoot(const QString &path,
256 const QString &oldPrefix,
257 const QString &newPrefix)
259 if (path.isEmpty() || oldPrefix.isEmpty() || newPrefix.isEmpty() || oldPrefix == newPrefix)
261 if (path == oldPrefix)
263 if (path.startsWith(oldPrefix))
264 return newPrefix + path.right(path.size() - oldPrefix.size());
268 QStringList PerforceSettings::commonP4Arguments(const QString &workingDir) const
271 if (!workingDir.isEmpty()) {
272 /* Determine the -d argument for the working directory for matching relative paths.
273 * It is is below the toplevel, replace top level portion by exact specification. */
274 rc << QLatin1String("-d")
275 << QDir::toNativeSeparators(mapPathRoot(workingDir, m_topLevelSymLinkTarget, m_topLevel));
277 rc.append(m_settings.commonP4Arguments());
281 QString PerforceSettings::mapToFileSystem(const QString &perforceFilePath) const
283 return mapPathRoot(perforceFilePath, m_topLevel, m_topLevelSymLinkTarget);