perforceplugin.cpp 64 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "perforceplugin.h"
hjk's avatar
hjk committed
34

con's avatar
con committed
35
#include "changenumberdialog.h"
hjk's avatar
hjk committed
36
#include "pendingchangesdialog.h"
con's avatar
con committed
37
38
#include "perforceconstants.h"
#include "perforceeditor.h"
hjk's avatar
hjk committed
39
40
#include "perforcesubmiteditor.h"
#include "perforceversioncontrol.h"
41
#include "perforcechecker.h"
hjk's avatar
hjk committed
42
#include "settingspage.h"
con's avatar
con committed
43

44
#include <coreplugin/actionmanager/actionmanager.h>
45
46
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
47
#include <coreplugin/uniqueidmanager.h>
con's avatar
con committed
48
#include <coreplugin/coreconstants.h>
hjk's avatar
hjk committed
49
#include <coreplugin/editormanager/editormanager.h>
con's avatar
con committed
50
#include <coreplugin/filemanager.h>
hjk's avatar
hjk committed
51
#include <coreplugin/icore.h>
con's avatar
con committed
52
#include <coreplugin/messagemanager.h>
hjk's avatar
hjk committed
53
#include <coreplugin/mimedatabase.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
54
#include <locator/commandlocator.h>
hjk's avatar
hjk committed
55
#include <utils/qtcassert.h>
con's avatar
con committed
56
#include <utils/synchronousprocess.h>
57
#include <utils/parameteraction.h>
58
#include <utils/fileutils.h>
con's avatar
con committed
59
60
61
#include <vcsbase/basevcseditorfactory.h>
#include <vcsbase/basevcssubmiteditorfactory.h>
#include <vcsbase/vcsbaseeditor.h>
62
#include <vcsbase/vcsbaseoutputwindow.h>
63
#include <vcsbase/vcsbaseeditorparameterwidget.h>
con's avatar
con committed
64

hjk's avatar
hjk committed
65
#include <QtCore/QtPlugin>
con's avatar
con committed
66
67
#include <QtCore/QDebug>
#include <QtCore/QDir>
hjk's avatar
hjk committed
68
#include <QtCore/QFileInfo>
con's avatar
con committed
69
70
#include <QtCore/QSettings>
#include <QtCore/QTextCodec>
hjk's avatar
hjk committed
71

con's avatar
con committed
72
#include <QtGui/QAction>
hjk's avatar
hjk committed
73
74
#include <QtGui/QFileDialog>
#include <QtGui/QMainWindow>
con's avatar
con committed
75
76
77
78
79
80
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>

static const VCSBase::VCSBaseEditorParameters editorParameters[] = {
{
    VCSBase::RegularCommandOutput,
81
    Perforce::Constants::PERFORCE_COMMANDLOG_EDITOR_ID,
82
    Perforce::Constants::PERFORCE_COMMANDLOG_EDITOR_DISPLAY_NAME,
83
    Perforce::Constants::PERFORCE_COMMANDLOG_EDITOR_CONTEXT,
con's avatar
con committed
84
85
86
    "application/vnd.nokia.text.scs_commandlog",
    "scslog"},
{   VCSBase::LogOutput,
87
    Perforce::Constants::PERFORCE_LOG_EDITOR_ID,
88
    Perforce::Constants::PERFORCE_LOG_EDITOR_DISPLAY_NAME,
89
    Perforce::Constants::PERFORCE_LOG_EDITOR_CONTEXT,
con's avatar
con committed
90
91
92
    "application/vnd.nokia.text.scs_filelog",
    "scsfilelog"},
{    VCSBase::AnnotateOutput,
93
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_ID,
94
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_DISPLAY_NAME,
95
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_CONTEXT,
con's avatar
con committed
96
97
98
    "application/vnd.nokia.text.scs_annotation",
    "scsannotate"},
{   VCSBase::DiffOutput,
99
    Perforce::Constants::PERFORCE_DIFF_EDITOR_ID,
100
    Perforce::Constants::PERFORCE_DIFF_EDITOR_DISPLAY_NAME,
101
    Perforce::Constants::PERFORCE_DIFF_EDITOR_CONTEXT,
con's avatar
con committed
102
103
104
105
106
107
108
    "text/x-patch","diff"}
};

// Utility to find a parameter set by type
static inline const VCSBase::VCSBaseEditorParameters *findType(int ie)
{
    const VCSBase::EditorContentType et = static_cast<VCSBase::EditorContentType>(ie);
109
    return  VCSBase::VCSBaseEditorWidget::findType(editorParameters, sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters), et);
con's avatar
con committed
110
111
112
113
114
115
116
}

static inline QString debugCodec(const QTextCodec *c)
{
    return c ? QString::fromAscii(c->name()) : QString::fromAscii("Null codec");
}

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Ensure adding "..." to relative paths which is p4's convention
// for the current directory
static inline QStringList perforceRelativeFileArguments(const QStringList &args)
{
    if (args.isEmpty())
        return QStringList(QLatin1String("..."));
    QTC_ASSERT(args.size() == 1, return QStringList())
    QStringList p4Args = args;
    p4Args.front() += QLatin1String("/...");
    return p4Args;
}

static inline QStringList perforceRelativeProjectDirectory(const VCSBase::VCSBasePluginState &s)
{
    return perforceRelativeFileArguments(s.relativeCurrentProject());
}

// Clean user setting off diff-binary for 'p4 resolve' and 'p4 diff'.
static inline QProcessEnvironment overrideDiffEnvironmentVariable()
{
    QProcessEnvironment rc = QProcessEnvironment::systemEnvironment();
    rc.remove(QLatin1String("P4DIFF"));
    return rc;
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
static const char CMD_ID_PERFORCE_MENU[] = "Perforce.Menu";
static const char CMD_ID_EDIT[] = "Perforce.Edit";
static const char CMD_ID_ADD[] = "Perforce.Add";
static const char CMD_ID_DELETE_FILE[] = "Perforce.Delete";
static const char CMD_ID_OPENED[] = "Perforce.Opened";
static const char CMD_ID_PROJECTLOG[] = "Perforce.ProjectLog";
static const char CMD_ID_REPOSITORYLOG[] = "Perforce.RepositoryLog";
static const char CMD_ID_REVERT[] = "Perforce.Revert";
static const char CMD_ID_DIFF_CURRENT[] = "Perforce.DiffCurrent";
static const char CMD_ID_DIFF_PROJECT[] = "Perforce.DiffProject";
static const char CMD_ID_UPDATE_PROJECT[] = "Perforce.UpdateProject";
static const char CMD_ID_REVERT_PROJECT[] = "Perforce.RevertProject";
static const char CMD_ID_REVERT_UNCHANGED_PROJECT[] = "Perforce.RevertUnchangedProject";
static const char CMD_ID_DIFF_ALL[] = "Perforce.DiffAll";
static const char CMD_ID_RESOLVE[] = "Perforce.Resolve";
static const char CMD_ID_SUBMIT[] = "Perforce.Submit";
static const char CMD_ID_PENDING_CHANGES[] = "Perforce.PendingChanges";
static const char CMD_ID_DESCRIBE[] = "Perforce.Describe";
static const char CMD_ID_ANNOTATE_CURRENT[] = "Perforce.AnnotateCurrent";
static const char CMD_ID_ANNOTATE[] = "Perforce.Annotate";
static const char CMD_ID_FILELOG_CURRENT[] = "Perforce.FilelogCurrent";
static const char CMD_ID_FILELOG[] = "Perforce.Filelog";
static const char CMD_ID_UPDATEALL[] = "Perforce.UpdateAll";
static const char CMD_ID_SEPARATOR1[] = "Perforce.Separator1";
static const char CMD_ID_SEPARATOR2[] = "Perforce.Separator2";
static const char CMD_ID_SEPARATOR3[] = "Perforce.Separator3";
con's avatar
con committed
168
169
170
171
172

////
// PerforcePlugin
////

173
174
175
176
177
178
179
180
181
namespace Perforce {
namespace Internal {

PerforceResponse::PerforceResponse() :
    error(true),
    exitCode(-1)
{
}

con's avatar
con committed
182
183
184
PerforcePlugin *PerforcePlugin::m_perforcePluginInstance = NULL;

PerforcePlugin::PerforcePlugin() :
185
    VCSBase::VCSBasePlugin(QLatin1String(Constants::PERFORCE_SUBMIT_EDITOR_ID)),
Friedemann Kleint's avatar
Friedemann Kleint committed
186
    m_commandLocator(0),
con's avatar
con committed
187
188
189
190
    m_editAction(0),
    m_addAction(0),
    m_deleteAction(0),
    m_openedAction(0),
191
192
    m_revertFileAction(0),
    m_diffFileAction(0),
con's avatar
con committed
193
    m_diffProjectAction(0),
194
    m_updateProjectAction(0),
195
196
    m_revertProjectAction(0),
    m_revertUnchangedAction(0),
con's avatar
con committed
197
    m_diffAllAction(0),
198
    m_submitProjectAction(0),
con's avatar
con committed
199
200
201
202
203
204
    m_pendingAction(0),
    m_describeAction(0),
    m_annotateCurrentAction(0),
    m_annotateAction(0),
    m_filelogCurrentAction(0),
    m_filelogAction(0),
205
206
    m_logProjectAction(0),
    m_logRepositoryAction(0),
207
    m_submitCurrentLogAction(0),
208
    m_updateAllAction(0),
209
210
211
    m_submitActionTriggered(false),
    m_diffSelectedFiles(0),
    m_undoAction(0),
212
    m_redoAction(0)
con's avatar
con committed
213
214
215
216
217
{
}

static const VCSBase::VCSBaseSubmitEditorParameters submitParameters = {
    Perforce::Constants::SUBMIT_MIMETYPE,
218
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_ID,
219
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME,
220
    Perforce::Constants::PERFORCESUBMITEDITOR_CONTEXT
con's avatar
con committed
221
222
};

223
224
static inline Core::Command *createSeparator(QObject *parent,
                                             Core::ActionManager *ami,
225
226
                                             const char *id,
                                             const Core::Context &globalcontext)
227
228
229
230
231
232
{
    QAction *tmpaction = new QAction(parent);
    tmpaction->setSeparator(true);
    return ami->registerAction(tmpaction, id, globalcontext);
}

233
bool PerforcePlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
con's avatar
con committed
234
235
236
237
{
    typedef VCSBase::VCSEditorFactory<PerforceEditor> PerforceEditorFactory;
    typedef VCSBase::VCSSubmitEditorFactory<PerforceSubmitEditor> PerforceSubmitEditorFactory;

238
    initializeVcs(new PerforceVersionControl(this));
239

240
241
    Core::ICore *core = Core::ICore::instance();
    if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/trolltech.perforce/Perforce.mimetypes.xml"), errorMessage))
con's avatar
con committed
242
243
244
        return false;
    m_perforcePluginInstance = this;

245
    if (QSettings *settings = core->settings())
con's avatar
con committed
246
247
        m_settings.fromSettings(settings);

248
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
249
250

    // Editor factories
251
    addAutoReleasedObject(new PerforceSubmitEditorFactory(&submitParameters));
con's avatar
con committed
252
253
254

    static const char *describeSlot = SLOT(describe(QString,QString));
    const int editorCount = sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters);
255
256
    for (int i = 0; i < editorCount; i++)
        addAutoReleasedObject(new PerforceEditorFactory(editorParameters + i, this, describeSlot));
con's avatar
con committed
257

Friedemann Kleint's avatar
Friedemann Kleint committed
258
259
260
261
    const QString prefix = QLatin1String("p4");
    m_commandLocator = new Locator::CommandLocator(QLatin1String("Perforce"), prefix, prefix);
    addAutoReleasedObject(m_commandLocator);

con's avatar
con committed
262
    //register actions
263
    Core::ActionManager *am = Core::ICore::instance()->actionManager();
con's avatar
con committed
264

265
    Core::ActionContainer *mtools =
con's avatar
con committed
266
267
        am->actionContainer(Core::Constants::M_TOOLS);

268
    Core::ActionContainer *mperforce =
hjk's avatar
hjk committed
269
        am->createMenu(Core::Id(CMD_ID_PERFORCE_MENU));
con's avatar
con committed
270
271
    mperforce->menu()->setTitle(tr("&Perforce"));
    mtools->addMenu(mperforce);
272
    m_menuAction = mperforce->menu()->menuAction();
con's avatar
con committed
273

274
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
275
    Core::Context perforcesubmitcontext(Constants::PERFORCESUBMITEDITOR_CONTEXT);
con's avatar
con committed
276

con's avatar
con committed
277
    Core::Command *command;
278
279
280
281
282
283
284

    m_diffFileAction = new Utils::ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_diffFileAction, CMD_ID_DIFF_CURRENT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    command->setDefaultText(tr("Diff Current File"));
    connect(m_diffFileAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
285
    m_commandLocator->appendCommand(command);
286
287
288
289
290
291
292

    m_annotateCurrentAction = new Utils::ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_annotateCurrentAction, CMD_ID_ANNOTATE_CURRENT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    command->setDefaultText(tr("Annotate Current File"));
    connect(m_annotateCurrentAction, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
293
    m_commandLocator->appendCommand(command);
294
295
296
297
298
299
300
301

    m_filelogCurrentAction = new Utils::ParameterAction(tr("Filelog Current File"), tr("Filelog \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_filelogCurrentAction, CMD_ID_FILELOG_CURRENT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+F")));
    command->setDefaultText(tr("Filelog Current File"));
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this, SLOT(filelogCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
302
    m_commandLocator->appendCommand(command);
303
304

    mperforce->addAction(createSeparator(this, am, "Perforce.Sep.Edit", globalcontext));
con's avatar
con committed
305

306
    m_editAction = new Utils::ParameterAction(tr("Edit"), tr("Edit \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
307
    command = am->registerAction(m_editAction, CMD_ID_EDIT, globalcontext);
con's avatar
con committed
308
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
309
310
311
312
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+E")));
    command->setDefaultText(tr("Edit File"));
    connect(m_editAction, SIGNAL(triggered()), this, SLOT(openCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
313
    m_commandLocator->appendCommand(command);
con's avatar
con committed
314

315
    m_addAction = new Utils::ParameterAction(tr("Add"), tr("Add \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
316
    command = am->registerAction(m_addAction, CMD_ID_ADD, globalcontext);
con's avatar
con committed
317
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
318
319
320
321
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+A")));
    command->setDefaultText(tr("Add File"));
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
322
    m_commandLocator->appendCommand(command);
con's avatar
con committed
323

324
    m_deleteAction = new Utils::ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
325
    command = am->registerAction(m_deleteAction, CMD_ID_DELETE_FILE, globalcontext);
con's avatar
con committed
326
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
327
    command->setDefaultText(tr("Delete File"));
328
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
con's avatar
con committed
329
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
330
    m_commandLocator->appendCommand(command);
con's avatar
con committed
331

332
333
    m_revertFileAction = new Utils::ParameterAction(tr("Revert"), tr("Revert \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_revertFileAction, CMD_ID_REVERT, globalcontext);
con's avatar
con committed
334
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
335
336
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+R")));
    command->setDefaultText(tr("Revert File"));
337
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
338
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
339
    m_commandLocator->appendCommand(command);
con's avatar
con committed
340

341
    mperforce->addAction(createSeparator(this, am, "Perforce.Sep.Project", globalcontext));
con's avatar
con committed
342

343
    const QString diffProjectDefaultText = tr("Diff Current Project/Session");
344
    m_diffProjectAction = new Utils::ParameterAction(diffProjectDefaultText, tr("Diff Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
345
    command = am->registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT, globalcontext);
con's avatar
con committed
346
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
347
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+D")));
348
    command->setDefaultText(diffProjectDefaultText);
con's avatar
con committed
349
350
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
351
    m_commandLocator->appendCommand(command);
con's avatar
con committed
352

Friedemann Kleint's avatar
Friedemann Kleint committed
353
    m_logProjectAction = new Utils::ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
354
355
356
357
    command = am->registerAction(m_logProjectAction, CMD_ID_PROJECTLOG, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
358
    m_commandLocator->appendCommand(command);
359

360
361
362
    m_submitProjectAction = new Utils::ParameterAction(tr("Submit Project"), tr("Submit Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_submitProjectAction, CMD_ID_SUBMIT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
363
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+S")));
364
    connect(m_submitProjectAction, SIGNAL(triggered()), this, SLOT(startSubmitProject()));
con's avatar
con committed
365
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
366
    m_commandLocator->appendCommand(command);
con's avatar
con committed
367

368
    const QString updateProjectDefaultText = tr("Update Current Project");
369
    m_updateProjectAction = new Utils::ParameterAction(updateProjectDefaultText, tr("Update Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
370
371
372
373
374
    command = am->registerAction(m_updateProjectAction, CMD_ID_UPDATE_PROJECT, globalcontext);
    command->setDefaultText(updateProjectDefaultText);
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
375
    m_commandLocator->appendCommand(command);
376

377
378
379
380
381
    m_revertUnchangedAction = new Utils::ParameterAction(tr("Revert Unchanged"), tr("Revert Unchanged Files of Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_revertUnchangedAction, CMD_ID_REVERT_UNCHANGED_PROJECT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertUnchangedAction, SIGNAL(triggered()), this, SLOT(revertUnchangedCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
382
    m_commandLocator->appendCommand(command);
383

384
385
386
387
388
    m_revertProjectAction = new Utils::ParameterAction(tr("Revert Project"), tr("Revert Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
    command = am->registerAction(m_revertProjectAction, CMD_ID_REVERT_PROJECT, globalcontext);
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertProjectAction, SIGNAL(triggered()), this, SLOT(revertCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
389
    m_commandLocator->appendCommand(command);
390

391
392
393
394
395
    mperforce->addAction(createSeparator(this, am, "Perforce.Sep.Repository", globalcontext));

    m_diffAllAction = new QAction(tr("Diff Opened Files"), this);
    command = am->registerAction(m_diffAllAction, CMD_ID_DIFF_ALL, globalcontext);
    connect(m_diffAllAction, SIGNAL(triggered()), this, SLOT(diffAllOpened()));
396
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
397
    m_commandLocator->appendCommand(command);
398

399
400
401
402
403
    m_openedAction = new QAction(tr("Opened"), this);
    command = am->registerAction(m_openedAction, CMD_ID_OPENED, globalcontext);
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+O")));
    connect(m_openedAction, SIGNAL(triggered()), this, SLOT(printOpenedFileList()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
404
    m_commandLocator->appendCommand(command);
405
406
407
408
409

    m_logRepositoryAction = new QAction(tr("Repository Log"), this);
    command = am->registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
410
    m_commandLocator->appendCommand(command);
411
412
413
414
415

    m_pendingAction = new QAction(tr("Pending Changes..."), this);
    command = am->registerAction(m_pendingAction, CMD_ID_PENDING_CHANGES, globalcontext);
    connect(m_pendingAction, SIGNAL(triggered()), this, SLOT(printPendingChanges()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
416
    m_commandLocator->appendCommand(command);
417
418
419
420

    m_updateAllAction = new QAction(tr("Update All"), this);
    command = am->registerAction(m_updateAllAction, CMD_ID_UPDATEALL, globalcontext);
    connect(m_updateAllAction, SIGNAL(triggered()), this, SLOT(updateAll()));
con's avatar
con committed
421
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
422
    m_commandLocator->appendCommand(command);
con's avatar
con committed
423

424
425
    mperforce->addAction(createSeparator(this, am, "Perforce.Sep.Dialogs", globalcontext));

con's avatar
con committed
426
    m_describeAction = new QAction(tr("Describe..."), this);
427
    command = am->registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
con's avatar
con committed
428
429
430
431
    connect(m_describeAction, SIGNAL(triggered()), this, SLOT(describeChange()));
    mperforce->addAction(command);

    m_annotateAction = new QAction(tr("Annotate..."), this);
432
    command = am->registerAction(m_annotateAction, CMD_ID_ANNOTATE, globalcontext);
con's avatar
con committed
433
434
435
436
    connect(m_annotateAction, SIGNAL(triggered()), this, SLOT(annotate()));
    mperforce->addAction(command);

    m_filelogAction = new QAction(tr("Filelog..."), this);
437
    command = am->registerAction(m_filelogAction, CMD_ID_FILELOG, globalcontext);
con's avatar
con committed
438
439
440
    connect(m_filelogAction, SIGNAL(triggered()), this, SLOT(filelog()));
    mperforce->addAction(command);

441
    m_submitCurrentLogAction = new QAction(VCSBase::VCSBaseSubmitEditor::submitIcon(), tr("Submit"), this);
con's avatar
con committed
442
    command = am->registerAction(m_submitCurrentLogAction, Constants::SUBMIT_CURRENT, perforcesubmitcontext);
443
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
444
445
    connect(m_submitCurrentLogAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

446
    m_diffSelectedFiles = new QAction(VCSBase::VCSBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
con's avatar
con committed
447
448
449
450
451
452
453
454
455
456
457
458
459
    command = am->registerAction(m_diffSelectedFiles, Constants::DIFF_SELECTED, perforcesubmitcontext);

    m_undoAction = new QAction(tr("&Undo"), this);
    command = am->registerAction(m_undoAction, Core::Constants::UNDO, perforcesubmitcontext);

    m_redoAction = new QAction(tr("&Redo"), this);
    command = am->registerAction(m_redoAction, Core::Constants::REDO, perforcesubmitcontext);

    return true;
}

void PerforcePlugin::extensionsInitialized()
{
460
    VCSBase::VCSBasePlugin::extensionsInitialized();
461
    getTopLevel();
con's avatar
con committed
462
463
464
465
}

void PerforcePlugin::openCurrentFile()
{
466
467
468
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
    vcsOpen(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
469
470
471
472
}

void PerforcePlugin::addCurrentFile()
{
473
474
475
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
476
477
478
479
}

void PerforcePlugin::revertCurrentFile()
{
480
481
482
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)

483
    QTextCodec *codec = VCSBase::VCSBaseEditorWidget::getCodec(state.currentFile());
con's avatar
con committed
484
    QStringList args;
485
486
487
488
    args << QLatin1String("diff") << QLatin1String("-sa") << state.relativeCurrentFile();
    PerforceResponse result = runP4Cmd(state.currentFileTopLevel(), args,
                                       RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow,
                                       QStringList(), QByteArray(), codec);
con's avatar
con committed
489
490
    if (result.error)
        return;
491
492
493
    // "foo.cpp - file(s) not opened on this client."
    if (result.stdOut.isEmpty() || result.stdOut.contains(QLatin1String(" - ")))
        return;
con's avatar
con committed
494

495
496
497
498
499
    const bool doNotRevert = QMessageBox::warning(0, tr("p4 revert"),
                             tr("The file has been changed. Do you want to revert it?"),
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No;
    if (doNotRevert)
        return;
con's avatar
con committed
500

501
502
503
504
505
    Core::FileChangeBlocker fcb(state.currentFile());
    args.clear();
    args << QLatin1String("revert") << state.relativeCurrentFile();
    PerforceResponse result2 = runP4Cmd(state.currentFileTopLevel(), args,
                                        CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
506
    if (!result2.error)
507
        perforceVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
508
509
510
511
}

void PerforcePlugin::diffCurrentFile()
{
512
513
514
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
    p4Diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
515
516
517
518
}

void PerforcePlugin::diffCurrentProject()
{
519
520
521
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)
    p4Diff(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
con's avatar
con committed
522
523
524
525
}

void PerforcePlugin::diffAllOpened()
{
526
    p4Diff(m_settings.topLevel(), QStringList());
con's avatar
con committed
527
528
}

529
void PerforcePlugin::updateCurrentProject()
530
{
531
532
533
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)
    updateCheckout(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
534
535
}

536
537
538
539
540
541
void PerforcePlugin::updateAll()
{
    updateCheckout(m_settings.topLevel());
}

void PerforcePlugin::revertCurrentProject()
542
{
543
544
545
546
547
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)

    const QString msg = tr("Do you want to revert all changes to the project \"%1\"?").arg(state.currentProjectName());
    if (QMessageBox::warning(0, tr("p4 revert"), msg, QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
548
        return;
549
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
550
551
}

552
void PerforcePlugin::revertUnchangedCurrentProject()
553
{
554
555
556
557
    // revert -a.
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true);
558
559
}

560
561
562
563
564
565
566
567
568
569
570
571
bool PerforcePlugin::revertProject(const QString &workingDir, const QStringList &pathArgs, bool unchangedOnly)
{
    QStringList args(QLatin1String("revert"));
    if (unchangedOnly)
        args.push_back(QLatin1String("-a"));
    args.append(pathArgs);
    const PerforceResponse resp = runP4Cmd(workingDir, args,
                                           RunFullySynchronous|CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
    return !resp.error;
}

void PerforcePlugin::updateCheckout(const QString &workingDir, const QStringList &dirs)
572
573
574
{
    QStringList args(QLatin1String("sync"));
    args.append(dirs);
575
576
577
578
579
580
581
    const PerforceResponse resp = runP4Cmd(workingDir, args,
                                           CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
    if (dirs.empty()) {
        if (!workingDir.isEmpty())
            perforceVersionControl()->emitRepositoryChanged(workingDir);
    } else {
        const QChar slash = QLatin1Char('/');
582
        foreach(const QString &dir, dirs)
583
584
            perforceVersionControl()->emitRepositoryChanged(workingDir + slash + dir);
    }
585
586
}

con's avatar
con committed
587
588
void PerforcePlugin::printOpenedFileList()
{
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
    const PerforceResponse perforceResponse
            = runP4Cmd(m_settings.topLevel(), QStringList(QLatin1String("opened")),
                       CommandToWindow|StdErrToWindow|ErrorToWindow);
    if (perforceResponse.error || perforceResponse.stdOut.isEmpty())
        return;
    // reformat "//depot/file.cpp#1 - description" into "file.cpp # - description"
    // for context menu opening to work. This produces absolute paths, then.
    VCSBase::VCSBaseOutputWindow *outWin = VCSBase::VCSBaseOutputWindow::instance();
    QString errorMessage;
    QString mapped;
    const QChar delimiter = QLatin1Char('#');
    foreach (const QString &line, perforceResponse.stdOut.split(QLatin1Char('\n'))) {
        mapped.clear();
        const int delimiterPos = line.indexOf(delimiter);
        if (delimiterPos > 0)
            mapped = fileNameFromPerforceName(line.left(delimiterPos), true, &errorMessage);
        if (mapped.isEmpty()) {
            outWin->appendSilently(line);
        } else {
            outWin->appendSilently(mapped + QLatin1Char(' ') + line.mid(delimiterPos));
        }
    }
    outWin->popup();
con's avatar
con committed
612
613
}

614
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
615
{
616

617
    if (VCSBase::VCSBaseSubmitEditor::raiseSubmitEditor())
con's avatar
con committed
618
619
        return;

620
    if (isCommitEditorOpen()) {
621
        VCSBase::VCSBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
622
623
624
        return;
    }

625
626
627
628
629
630
631
632
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)

    // Revert all unchanged files.
    if (!revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true))
        return;
    // Start a change
    QStringList args;
con's avatar
con committed
633

634
635
636
    args << QLatin1String("change") << QLatin1String("-o");
    PerforceResponse result = runP4Cmd(state.currentProjectTopLevel(), args,
                                       RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
637
    if (result.error) {
638
        cleanCommitMessageFile();
con's avatar
con committed
639
640
641
        return;
    }

642
643
644
645
646
647
648
649
650
    Utils::TempFileSaver saver;
    saver.setAutoRemove(false);
    saver.write(result.stdOut.toAscii());
    if (!saver.finalize()) {
        VCSBase::VCSBaseOutputWindow::instance()->appendError(saver.errorString());
        cleanCommitMessageFile();
        return;
    }
    m_commitMessageFileName = saver.fileName();
con's avatar
con committed
651

652
653
654
655
656
657
    args.clear();
    args << QLatin1String("fstat");
    args.append(perforceRelativeProjectDirectory(state));
    PerforceResponse fstatResult = runP4Cmd(state.currentProjectTopLevel(), args,
                                            RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
    if (fstatResult.error) {
658
        cleanCommitMessageFile();
con's avatar
con committed
659
660
661
        return;
    }

662
    QStringList fstatLines = fstatResult.stdOut.split(QLatin1Char('\n'));
con's avatar
con committed
663
    QStringList depotFileNames;
664
    foreach (const QString &line, fstatLines) {
665
        if (line.startsWith(QLatin1String("... depotFile")))
con's avatar
con committed
666
667
668
            depotFileNames.append(line.mid(14));
    }
    if (depotFileNames.isEmpty()) {
669
        VCSBase::VCSBaseOutputWindow::instance()->appendWarning(tr("Project has no files"));
670
        cleanCommitMessageFile();
con's avatar
con committed
671
672
673
        return;
    }

674
    openPerforceSubmitEditor(m_commitMessageFileName, depotFileNames);
con's avatar
con committed
675
676
677
678
}

Core::IEditor *PerforcePlugin::openPerforceSubmitEditor(const QString &fileName, const QStringList &depotFileNames)
{
679
    Core::EditorManager *editorManager = Core::EditorManager::instance();
680
681
    Core::IEditor *editor = editorManager->openEditor(fileName, Constants::PERFORCE_SUBMIT_EDITOR_ID,
                                                      Core::EditorManager::ModeSwitch);
dt's avatar
dt committed
682
    PerforceSubmitEditor *submitEditor = static_cast<PerforceSubmitEditor*>(editor);
con's avatar
con committed
683
    submitEditor->restrictToProjectFiles(depotFileNames);
684
    submitEditor->registerActions(m_undoAction, m_redoAction, m_submitCurrentLogAction, m_diffSelectedFiles);
685
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotSubmitDiff(QStringList)));
686
    submitEditor->setCheckScriptWorkingDirectory(m_commitWorkingDirectory);
con's avatar
con committed
687
688
689
690
691
692
    return editor;
}

void PerforcePlugin::printPendingChanges()
{
    qApp->setOverrideCursor(Qt::WaitCursor);
693
    PendingChangesDialog dia(pendingChangesData(), Core::ICore::instance()->mainWindow());
con's avatar
con committed
694
695
    qApp->restoreOverrideCursor();
    if (dia.exec() == QDialog::Accepted) {
696
697
698
        const int i = dia.changeNumber();
        QStringList args(QLatin1String("submit"));
        args << QLatin1String("-c") << QString::number(i);
699
700
        runP4Cmd(m_settings.topLevel(), args,
                 CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
701
702
703
704
705
706
707
708
709
710
711
712
    }
}

void PerforcePlugin::describeChange()
{
    ChangeNumberDialog dia;
    if (dia.exec() == QDialog::Accepted && dia.number() > 0)
        describe(QString(), QString::number(dia.number()));
}

void PerforcePlugin::annotateCurrentFile()
{
713
714
715
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
    annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
716
717
718
719
}

void PerforcePlugin::annotate()
{
720
721
722
723
724
    const QString file = QFileDialog::getOpenFileName(0, tr("p4 annotate"));
    if (!file.isEmpty()) {
        const QFileInfo fi(file);
        annotate(fi.absolutePath(), fi.fileName());
    }
con's avatar
con committed
725
726
}

727
void PerforcePlugin::vcsAnnotate(const QString &file, const QString &revision, int lineNumber)
728
729
730
731
732
733
734
735
736
{
    const QFileInfo fi(file);
    annotate(fi.absolutePath(), fi.fileName(), revision, lineNumber);
}

void PerforcePlugin::annotate(const QString &workingDir,
                              const QString &fileName,
                              const QString &changeList /* = QString() */,
                              int lineNumber /* = -1 */)
con's avatar
con committed
737
{
738
    const QStringList files = QStringList(fileName);
739
740
741
    QTextCodec *codec = VCSBase::VCSBaseEditorWidget::getCodec(workingDir, files);
    const QString id = VCSBase::VCSBaseEditorWidget::getTitleId(workingDir, files, changeList);
    const QString source = VCSBase::VCSBaseEditorWidget::getSource(workingDir, files);
con's avatar
con committed
742
    QStringList args;
743
744
745
746
747
748
    args << QLatin1String("annotate") << QLatin1String("-cqi");
    if (changeList.isEmpty()) {
        args << fileName;
    } else {
        args << (fileName + QLatin1Char('@') + changeList);
    }
749
750
751
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
con's avatar
con committed
752
    if (!result.error) {
753
        if (lineNumber < 1)
754
            lineNumber = VCSBase::VCSBaseEditorWidget::lineNumberOfCurrentEditor();
con's avatar
con committed
755
        const QFileInfo fi(fileName);
756
        Core::IEditor *ed = showOutputInEditor(tr("p4 annotate %1").arg(id),
757
758
                                               result.stdOut, VCSBase::AnnotateOutput,
                                               source, codec);
759
        VCSBase::VCSBaseEditorWidget::gotoLineOfEditor(ed, lineNumber);
con's avatar
con committed
760
761
762
763
764
    }
}

void PerforcePlugin::filelogCurrentFile()
{
765
766
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
767
    filelog(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()), true);
con's avatar
con committed
768
769
770
771
}

void PerforcePlugin::filelog()
{
772
773
774
775
776
    const QString file = QFileDialog::getOpenFileName(0, tr("p4 filelog"));
    if (!file.isEmpty()) {
        const QFileInfo fi(file);
        filelog(fi.absolutePath(), QStringList(fi.fileName()));
    }
con's avatar
con committed
777
778
}

779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
void PerforcePlugin::logProject()
{
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasProject(), return)
    filelog(state.currentProjectTopLevel(), perforceRelativeFileArguments(state.relativeCurrentProject()));
}

void PerforcePlugin::logRepository()
{
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)
    filelog(state.topLevel(), perforceRelativeFileArguments(QStringList()));
}

void PerforcePlugin::filelog(const QString &workingDir, const QStringList &fileNames,
                             bool enableAnnotationContextMenu)
con's avatar
con committed
795
{
796
797
    const QString id = VCSBase::VCSBaseEditorWidget::getTitleId(workingDir, fileNames);
    QTextCodec *codec = VCSBase::VCSBaseEditorWidget::getCodec(workingDir, fileNames);
con's avatar
con committed
798
    QStringList args;
799
    args << QLatin1String("filelog") << QLatin1String("-li");
800
801
    if (m_settings.logCount() > 0)
        args << QLatin1String("-m") << QString::number(m_settings.logCount());
802
803
804
805
    args.append(fileNames);
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
806
    if (!result.error) {
807
        const QString source = VCSBase::VCSBaseEditorWidget::getSource(workingDir, fileNames);
808
809
810
        Core::IEditor *editor = showOutputInEditor(tr("p4 filelog %1").arg(id), result.stdOut,
                                VCSBase::LogOutput, source, codec);
        if (enableAnnotationContextMenu)
811
            VCSBase::VCSBaseEditorWidget::getVcsBaseEditor(editor)->setFileLogAnnotateEnabled(true);
812
    }
con's avatar
con committed
813
814
}

815
void PerforcePlugin::updateActions(VCSBase::VCSBasePlugin::ActionState as)
con's avatar
con committed
816
{
Friedemann Kleint's avatar
Friedemann Kleint committed
817
818
819
820
821
822
823
    if (!enableMenuAction(as, m_menuAction)) {
        m_commandLocator->setEnabled(false);
        return;        
    }
    const bool hasTopLevel = currentState().hasTopLevel();
    m_commandLocator->setEnabled(hasTopLevel);
    m_logRepositoryAction->setEnabled(hasTopLevel);
824

825
826
827
828
829
830
831
832
833
834
    const QString fileName = currentState().currentFileName();
    m_editAction->setParameter(fileName);
    m_addAction->setParameter(fileName);
    m_deleteAction->setParameter(fileName);
    m_revertFileAction->setParameter(fileName);
    m_diffFileAction->setParameter(fileName);
    m_annotateCurrentAction->setParameter(fileName);
    m_filelogCurrentAction->setParameter(fileName);

    const QString projectName = currentState().currentProjectName();
835
    m_logProjectAction->setParameter(projectName);
836
837
838
839
840
    m_updateProjectAction->setParameter(projectName);
    m_diffProjectAction->setParameter(projectName);
    m_submitProjectAction->setParameter(projectName);
    m_revertProjectAction->setParameter(projectName);
    m_revertUnchangedAction->setParameter(projectName);
841

con's avatar
con committed
842
843
844
845
846
847
    m_diffAllAction->setEnabled(true);
    m_openedAction->setEnabled(true);
    m_describeAction->setEnabled(true);
    m_annotateAction->setEnabled(true);
    m_filelogAction->setEnabled(true);
    m_pendingAction->setEnabled(true);
848
    m_updateAllAction->setEnabled(true);
con's avatar
con committed
849
850
}

851
852
853
854
855
856
857
858
859
860
861
862
863
864
bool PerforcePlugin::managesDirectory(const QString &directory, QString *topLevel /* = 0 */)
{
    const bool rc = managesDirectoryFstat(directory);
    if (topLevel) {
        if (rc) {
            *topLevel = m_settings.topLevelSymLinkTarget();
        } else {
            topLevel->clear();
        }
    }
    return rc;
}

bool PerforcePlugin::managesDirectoryFstat(const QString &directory)
con's avatar
con committed
865
{
866
    if (!m_settings.isValid())
867
        return false;
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
    // Cached?
    const ManagedDirectoryCache::const_iterator cit = m_managedDirectoryCache.constFind(directory);
    if (cit != m_managedDirectoryCache.constEnd())
        return cit.value();
    // Determine value and insert into cache
    bool managed = false;
    do {
        // Quick check: Must be at or below top level and not "../../other_path"
        const QStringList relativeDirArgs = m_settings.relativeToTopLevelArguments(directory);
        if (!relativeDirArgs.empty() && relativeDirArgs.front().startsWith(QLatin1String("..")))
            break;
        // Is it actually managed by perforce?
        QStringList args;
        args << QLatin1String("fstat") << QLatin1String("-m1") << perforceRelativeFileArguments(relativeDirArgs);
        const PerforceResponse result = runP4Cmd(m_settings.topLevel(), args,
                                                 RunFullySynchronous);
        managed = result.stdOut.contains("depotFile") || result.stdErr.contains("... - no such file(s)");
    } while(false);

    m_managedDirectoryCache.insert(directory, managed);
    return managed;
con's avatar
con committed
889
890
}

891
bool PerforcePlugin::vcsOpen(const QString &workingDir, const QString &fileName)
con's avatar
con committed
892
{
893
894
895
896
897
    if (Perforce::Constants::debug)
        qDebug() << "PerforcePlugin::vcsOpen" << workingDir << fileName;
    QStringList args;
    args << QLatin1String("edit") << QDir::toNativeSeparators(fileName);
    const PerforceResponse result = runP4Cmd(workingDir, args,
898
                                       CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
899
900
901
    return !result.error;
}

902
bool PerforcePlugin::vcsAdd(const QString &workingDir, const QString &fileName)
con's avatar
con committed
903
{
904
905
906
    QStringList args;
    args << QLatin1String("add") << fileName;
    const PerforceResponse result = runP4Cmd(workingDir, args,
907
                                       CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
908
909
910
    return !result.error;
}

911
bool PerforcePlugin::vcsDelete(const QString &workingDir, const QString &fileName)
con's avatar
con committed
912
{
913
914
915
916

    QStringList args;
    args << QLatin1String("revert") << fileName;
    const PerforceResponse revertResult = runP4Cmd(workingDir, args,
917
                                       CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
918
919
920
921
922
923
    if (revertResult.error)
        return false;
    args.clear();
    args << QLatin1String("delete") << fileName;
    const PerforceResponse deleteResult = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
924
925
926
927
928
929
930
    // TODO need to carefully parse the actual messages from perforce
    // or do a fstat before to decide what to do

    // Different states are:
    // File is in depot and unopened => p4 delete %
    // File is in depot and opened => p4 revert %, p4 delete %
    // File is not in depot => p4 revert %
931
    return !deleteResult.error;
con's avatar
con committed
932
933
}

dt's avatar
dt committed
934
935
936
937
938
939
bool PerforcePlugin::vcsMove(const QString &workingDir, const QString &from, const QString &to)
{
    // TODO verify this works
    QStringList args;
    args << QLatin1String("edit") << from;
    const PerforceResponse editResult = runP4Cmd(workingDir, args,
940
                                                 RunFullySynchronous|CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
dt's avatar
dt committed
941
942
943
944
945
    if (editResult.error)
        return false;
    args.clear();
    args << QLatin1String("move") << from << to;
    const PerforceResponse moveResult = runP4Cmd(workingDir, args,
946
                                                 RunFullySynchronous|CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
dt's avatar
dt committed
947
948
949
    return !moveResult.error;
}

950
// Write extra args to temporary file
951
952
953
QSharedPointer<Utils::TempFileSaver>
        PerforcePlugin::createTemporaryArgumentFile(const QStringList &extraArgs,
                                                    QString *errorString) const
con's avatar
con committed
954
{
955
    if (extraArgs.isEmpty())
956
        return QSharedPointer<Utils::TempFileSaver>();
957
958
959
960
961
962
    // create pattern
    if (m_tempFilePattern.isEmpty()) {
        m_tempFilePattern = QDir::tempPath();
        if (!m_tempFilePattern.endsWith(QDir::separator()))
            m_tempFilePattern += QDir::separator();
        m_tempFilePattern += QLatin1String("qtc_p4_XXXXXX.args");
con's avatar
con committed
963
    }
964
    QSharedPointer<Utils::TempFileSaver> rc(new Utils::TempFileSaver(m_tempFilePattern));
965
966
967
968
969
    rc->setAutoRemove(true);
    const int last = extraArgs.size() - 1;
    for (int i = 0; i <= last; i++) {
        rc->write(extraArgs.at(i).toLocal8Bit());
        if (i != last)
970
            rc->write("\n", 1);
971
    }
972
973
    if (!rc->finalize(errorString))
        return QSharedPointer<Utils::TempFileSaver>();
974
975
    return rc;
}
con's avatar
con committed
976

977
978
979
980
981
982
983
// Run messages

static inline QString msgNotStarted(const QString &cmd)
{
    return PerforcePlugin::tr("Could not start perforce '%1'. Please check your settings in the preferences.").arg(cmd);
}

Friedemann Kleint's avatar
Friedemann Kleint committed
984
static inline QString msgTimeout(int timeOut)
985
{
Friedemann Kleint's avatar
Friedemann Kleint committed
986
    return PerforcePlugin::tr("Perforce did not respond within timeout limit (%1 ms).").arg(timeOut );
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}

static inline QString msgCrash()
{
    return PerforcePlugin::tr("The process terminated abnormally.");
}

static inline QString msgExitCode(int ex)
{