OSDN Git Service

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