OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / perforce / perforcesettings.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "perforcesettings.h"
34 #include "perforceplugin.h"
35 #include "perforceconstants.h"
36
37 #include <utils/qtcassert.h>
38 #include <utils/environment.h>
39
40 #include <QtCore/QSettings>
41 #include <QtCore/QStringList>
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/QDir>
44 #include <QtCore/QFileInfo>
45
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";
56
57 enum { defaultTimeOutS = 30, defaultLogCount = 1000 };
58
59 static QString defaultCommand()
60 {
61     Utils::Environment env = Utils::Environment::systemEnvironment();
62     QString rc;
63     rc = QLatin1String("p4");
64 #if defined(Q_OS_WIN32)
65     rc.append(QLatin1String(".exe"));
66 #endif
67     return env.searchInPath(rc);
68 }
69
70 namespace Perforce {
71 namespace Internal {
72
73 Settings::Settings() :
74     logCount(defaultLogCount),
75     defaultEnv(true),
76     timeOutS(defaultTimeOutS),
77     promptToSubmit(true),
78     autoOpen(true)
79 {
80 }
81
82 bool Settings::equals(const Settings &rhs) const
83 {
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;
90 }
91
92 QStringList Settings::commonP4Arguments() const
93 {
94     if (defaultEnv)
95         return QStringList();
96     QStringList lst;
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;
103     return lst;
104 }
105
106 // --------------------PerforceSettings
107 PerforceSettings::PerforceSettings()
108 {
109 }
110
111 PerforceSettings::~PerforceSettings()
112 {
113 }
114
115 void PerforceSettings::fromSettings(QSettings *settings)
116 {
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();
128 }
129
130 void PerforceSettings::toSettings(QSettings *settings) const
131 {
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();
143 }
144
145 void PerforceSettings::setSettings(const Settings &newSettings)
146 {
147     if (newSettings != m_settings) {
148         m_settings = newSettings;
149         clearTopLevel();
150     }
151 }
152
153 Settings PerforceSettings::settings() const
154 {
155     return m_settings;
156 }
157
158 QString PerforceSettings::p4Command() const
159 {
160     return m_settings.p4Command;
161 }
162
163 QString PerforceSettings::p4Port() const
164 {
165     return m_settings.p4Port;
166 }
167
168 QString PerforceSettings::p4Client() const
169 {
170     return m_settings.p4Client;
171 }
172
173 QString PerforceSettings::p4User() const
174 {
175     return m_settings.p4User;
176 }
177
178 bool PerforceSettings::defaultEnv() const
179 {
180     return m_settings.defaultEnv;
181 }
182
183 bool PerforceSettings::promptToSubmit() const
184 {
185     return m_settings.promptToSubmit;
186 }
187
188 void PerforceSettings::setPromptToSubmit(bool p)
189 {
190     m_settings.promptToSubmit = p;
191 }
192
193 bool PerforceSettings::autoOpen() const
194 {
195     return m_settings.autoOpen;
196 }
197
198 void PerforceSettings::setAutoOpen(bool b)
199 {
200     m_settings.autoOpen = b;
201 }
202
203 QString PerforceSettings::topLevel() const
204 {
205     return m_topLevel;
206 }
207
208 QString PerforceSettings::topLevelSymLinkTarget() const
209 {
210     return m_topLevelSymLinkTarget;
211 }
212
213 void PerforceSettings::setTopLevel(const QString &t)
214 {
215     if (m_topLevel == t)
216         return;
217     clearTopLevel();
218     if (!t.isEmpty()) {
219         // Check/expand symlinks as creator always has expanded file paths
220         QFileInfo fi(t);
221         if (fi.isSymLink()) {
222             m_topLevel = t;
223             m_topLevelSymLinkTarget = QFileInfo(fi.symLinkTarget()).absoluteFilePath();
224         } else {
225             m_topLevelSymLinkTarget = m_topLevel = t;
226         }
227         m_topLevelDir.reset(new QDir(m_topLevelSymLinkTarget));
228         if (Perforce::Constants::debug)
229             qDebug() << "PerforceSettings::setTopLevel" << m_topLevel << m_topLevelSymLinkTarget;
230     }
231 }
232
233 void PerforceSettings::clearTopLevel()
234 {
235     m_topLevelDir.reset();
236     m_topLevel.clear();
237 }
238
239 QString PerforceSettings::relativeToTopLevel(const QString &dir) const
240 {
241     QTC_ASSERT(!m_topLevelDir.isNull(), return QLatin1String("../") + dir)
242     return m_topLevelDir->relativeFilePath(dir);
243 }
244
245 QStringList PerforceSettings::relativeToTopLevelArguments(const QString &dir) const
246 {
247     const QString relative = relativeToTopLevel(dir);
248     return relative.isEmpty() ? QStringList() : QStringList(relative);
249 }
250
251 // Map the root part of a path:
252 // Calling "/home/john/foo" with old="/home", new="/user"
253 // results in "/user/john/foo"
254
255 static inline QString mapPathRoot(const QString &path,
256                                   const QString &oldPrefix,
257                                   const QString &newPrefix)
258 {
259     if (path.isEmpty() || oldPrefix.isEmpty() || newPrefix.isEmpty() || oldPrefix == newPrefix)
260         return path;
261     if (path == oldPrefix)
262         return newPrefix;
263     if (path.startsWith(oldPrefix))
264         return newPrefix + path.right(path.size() - oldPrefix.size());
265     return path;
266 }
267
268 QStringList PerforceSettings::commonP4Arguments(const QString &workingDir) const
269 {
270     QStringList rc;
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));
276     }
277     rc.append(m_settings.commonP4Arguments());
278     return rc;
279 }
280
281 QString PerforceSettings::mapToFileSystem(const QString &perforceFilePath) const
282 {
283     return mapPathRoot(perforceFilePath, m_topLevel, m_topLevelSymLinkTarget);
284 }
285
286 } // Internal
287 } // Perforce