1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "perforcesettings.h"
35 #include "perforceplugin.h"
36 #include "perforceconstants.h"
38 #include <utils/qtcassert.h>
39 #include <utils/environment.h>
41 #include <QtCore/QSettings>
42 #include <QtCore/QStringList>
43 #include <QtCore/QCoreApplication>
44 #include <QtCore/QDir>
45 #include <QtCore/QFileInfo>
47 static const char *groupC = "Perforce";
48 static const char *commandKeyC = "Command";
49 static const char *defaultKeyC = "Default";
50 static const char *portKeyC = "Port";
51 static const char *clientKeyC = "Client";
52 static const char *userKeyC = "User";
53 static const char *promptToSubmitKeyC = "PromptForSubmit";
54 static const char *autoOpenKeyC = "PromptToOpen";
55 static const char *timeOutKeyC = "TimeOut";
56 static const char *logCountKeyC = "LogCount";
58 enum { defaultTimeOutS = 30, defaultLogCount = 1000 };
60 static QString defaultCommand()
62 Utils::Environment env = Utils::Environment::systemEnvironment();
64 rc = QLatin1String("p4");
65 #if defined(Q_OS_WIN32)
66 rc.append(QLatin1String(".exe"));
68 return env.searchInPath(rc);
74 Settings::Settings() :
75 logCount(defaultLogCount),
77 timeOutS(defaultTimeOutS),
83 bool Settings::equals(const Settings &rhs) const
85 return defaultEnv == rhs.defaultEnv
86 && logCount == rhs.logCount
87 && p4Command == rhs.p4Command && p4Port == rhs.p4Port
88 && p4Client == rhs.p4Client && p4User == rhs.p4User
89 && timeOutS == rhs.timeOutS && promptToSubmit == rhs.promptToSubmit
90 && autoOpen == rhs.autoOpen;
93 QStringList Settings::commonP4Arguments() const
98 if (!p4Client.isEmpty())
99 lst << QLatin1String("-c") << p4Client;
100 if (!p4Port.isEmpty())
101 lst << QLatin1String("-p") << p4Port;
102 if (!p4User.isEmpty())
103 lst << QLatin1String("-u") << p4User;
107 // --------------------PerforceSettings
108 PerforceSettings::PerforceSettings()
112 PerforceSettings::~PerforceSettings()
116 void PerforceSettings::fromSettings(QSettings *settings)
118 settings->beginGroup(QLatin1String(groupC));
119 m_settings.p4Command = settings->value(QLatin1String(commandKeyC), defaultCommand()).toString();
120 m_settings.defaultEnv = settings->value(QLatin1String(defaultKeyC), true).toBool();
121 m_settings.p4Port = settings->value(QLatin1String(portKeyC), QString()).toString();
122 m_settings.p4Client = settings->value(QLatin1String(clientKeyC), QString()).toString();
123 m_settings.p4User = settings->value(QLatin1String(userKeyC), QString()).toString();
124 m_settings.timeOutS = settings->value(QLatin1String(timeOutKeyC), defaultTimeOutS).toInt();
125 m_settings.promptToSubmit = settings->value(QLatin1String(promptToSubmitKeyC), true).toBool();
126 m_settings.logCount = settings->value(QLatin1String(logCountKeyC), int(defaultLogCount)).toInt();
127 m_settings.autoOpen = settings->value(QLatin1String(autoOpenKeyC), true).toBool();
128 settings->endGroup();
131 void PerforceSettings::toSettings(QSettings *settings) const
133 settings->beginGroup(QLatin1String(groupC));
134 settings->setValue(QLatin1String(commandKeyC), m_settings.p4Command);
135 settings->setValue(QLatin1String(defaultKeyC), m_settings.defaultEnv);
136 settings->setValue(QLatin1String(portKeyC), m_settings.p4Port);
137 settings->setValue(QLatin1String(clientKeyC), m_settings.p4Client);
138 settings->setValue(QLatin1String(userKeyC), m_settings.p4User);
139 settings->setValue(QLatin1String(timeOutKeyC), m_settings.timeOutS);
140 settings->setValue(QLatin1String(promptToSubmitKeyC), m_settings.promptToSubmit);
141 settings->setValue(QLatin1String(logCountKeyC), m_settings.logCount);
142 settings->setValue(QLatin1String(autoOpenKeyC), m_settings.autoOpen);
143 settings->endGroup();
146 void PerforceSettings::setSettings(const Settings &newSettings)
148 if (newSettings != m_settings) {
149 m_settings = newSettings;
154 Settings PerforceSettings::settings() const
159 QString PerforceSettings::p4Command() const
161 return m_settings.p4Command;
164 QString PerforceSettings::p4Port() const
166 return m_settings.p4Port;
169 QString PerforceSettings::p4Client() const
171 return m_settings.p4Client;
174 QString PerforceSettings::p4User() const
176 return m_settings.p4User;
179 bool PerforceSettings::defaultEnv() const
181 return m_settings.defaultEnv;
184 bool PerforceSettings::promptToSubmit() const
186 return m_settings.promptToSubmit;
189 void PerforceSettings::setPromptToSubmit(bool p)
191 m_settings.promptToSubmit = p;
194 bool PerforceSettings::autoOpen() const
196 return m_settings.autoOpen;
199 void PerforceSettings::setAutoOpen(bool b)
201 m_settings.autoOpen = b;
204 QString PerforceSettings::topLevel() const
209 QString PerforceSettings::topLevelSymLinkTarget() const
211 return m_topLevelSymLinkTarget;
214 void PerforceSettings::setTopLevel(const QString &t)
220 // Check/expand symlinks as creator always has expanded file paths
222 if (fi.isSymLink()) {
224 m_topLevelSymLinkTarget = QFileInfo(fi.symLinkTarget()).absoluteFilePath();
226 m_topLevelSymLinkTarget = m_topLevel = t;
228 m_topLevelDir.reset(new QDir(m_topLevelSymLinkTarget));
229 if (Perforce::Constants::debug)
230 qDebug() << "PerforceSettings::setTopLevel" << m_topLevel << m_topLevelSymLinkTarget;
234 void PerforceSettings::clearTopLevel()
236 m_topLevelDir.reset();
240 QString PerforceSettings::relativeToTopLevel(const QString &dir) const
242 QTC_ASSERT(!m_topLevelDir.isNull(), return QLatin1String("../") + dir)
243 return m_topLevelDir->relativeFilePath(dir);
246 QStringList PerforceSettings::relativeToTopLevelArguments(const QString &dir) const
248 const QString relative = relativeToTopLevel(dir);
249 return relative.isEmpty() ? QStringList() : QStringList(relative);
252 // Map the root part of a path:
253 // Calling "/home/john/foo" with old="/home", new="/user"
254 // results in "/user/john/foo"
256 static inline QString mapPathRoot(const QString &path,
257 const QString &oldPrefix,
258 const QString &newPrefix)
260 if (path.isEmpty() || oldPrefix.isEmpty() || newPrefix.isEmpty() || oldPrefix == newPrefix)
262 if (path == oldPrefix)
264 if (path.startsWith(oldPrefix))
265 return newPrefix + path.right(path.size() - oldPrefix.size());
269 QStringList PerforceSettings::commonP4Arguments(const QString &workingDir) const
272 if (!workingDir.isEmpty()) {
273 /* Determine the -d argument for the working directory for matching relative paths.
274 * It is is below the toplevel, replace top level portion by exact specification. */
275 rc << QLatin1String("-d")
276 << QDir::toNativeSeparators(mapPathRoot(workingDir, m_topLevelSymLinkTarget, m_topLevel));
278 rc.append(m_settings.commonP4Arguments());
282 QString PerforceSettings::mapToFileSystem(const QString &perforceFilePath) const
284 return mapPathRoot(perforceFilePath, m_topLevel, m_topLevelSymLinkTarget);