perforceplugin.cpp 64 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "perforceplugin.h"
hjk's avatar
hjk committed
31

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

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

62
#include <QAction>
63 64 65
#include <QDebug>
#include <QDir>
#include <QFileDialog>
66
#include <QFileInfo>
67 68 69
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
70 71 72 73 74 75 76
#include <QSettings>
#include <QTextCodec>
#include <QtPlugin>

using namespace Core;
using namespace Utils;
using namespace VcsBase;
con's avatar
con committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
namespace Perforce {
namespace Internal {

const char SUBMIT_CURRENT[] = "Perforce.SubmitCurrentLog";
const char DIFF_SELECTED[] = "Perforce.DiffSelectedFilesInLog";
const char SUBMIT_MIMETYPE[] = "text/vnd.qtcreator.p4.submit";

const char PERFORCE_SUBMIT_EDITOR_ID[] = "Perforce.SubmitEditor";
const char PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Perforce.SubmitEditor");
const char PERFORCESUBMITEDITOR_CONTEXT[] = "Perforce Submit Editor";

const char PERFORCE_LOG_EDITOR_ID[] = "Perforce.LogEditor";
const char PERFORCE_LOG_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Perforce Log Editor");
const char PERFORCE_LOG_EDITOR_CONTEXT[] = "Perforce Log Editor";

const char PERFORCE_DIFF_EDITOR_ID[] = "Perforce.DiffEditor";
const char PERFORCE_DIFF_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Perforce Diff Editor");
const char PERFORCE_DIFF_EDITOR_CONTEXT[] = "Perforce Diff Editor";

const char PERFORCE_ANNOTATION_EDITOR_ID[] = "Perforce.AnnotationEditor";
const char PERFORCE_ANNOTATION_EDITOR_DISPLAY_NAME[] = QT_TRANSLATE_NOOP("VCS", "Perforce Annotation Editor");
const char PERFORCE_ANNOTATION_EDITOR_CONTEXT[] = "Perforce Annotation Editor";

const VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
102
{
Orgad Shaneh's avatar
Orgad Shaneh committed
103
    VcsBase::LogOutput,
104 105 106
    PERFORCE_LOG_EDITOR_ID,
    PERFORCE_LOG_EDITOR_DISPLAY_NAME,
    PERFORCE_LOG_EDITOR_CONTEXT,
Orgad Shaneh's avatar
Orgad Shaneh committed
107
    "text/vnd.qtcreator.p4.log"},
hjk's avatar
hjk committed
108
{    VcsBase::AnnotateOutput,
109 110 111
    PERFORCE_ANNOTATION_EDITOR_ID,
    PERFORCE_ANNOTATION_EDITOR_DISPLAY_NAME,
    PERFORCE_ANNOTATION_EDITOR_CONTEXT,
Orgad Shaneh's avatar
Orgad Shaneh committed
112
    "text/vnd.qtcreator.p4.annotation"},
hjk's avatar
hjk committed
113
{   VcsBase::DiffOutput,
114 115 116
    PERFORCE_DIFF_EDITOR_ID,
    PERFORCE_DIFF_EDITOR_DISPLAY_NAME,
    PERFORCE_DIFF_EDITOR_CONTEXT,
117
    "text/x-patch"}
con's avatar
con committed
118 119 120
};

// Utility to find a parameter set by type
121
static inline const VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
122
{
123 124
    const EditorContentType et = static_cast<EditorContentType>(ie);
    return VcsBaseEditorWidget::findType(editorParameters, sizeof(editorParameters)/sizeof(editorParameters[0]), et);
con's avatar
con committed
125 126 127 128
}

static inline QString debugCodec(const QTextCodec *c)
{
129
    return c ? QString::fromLatin1(c->name()) : QString::fromLatin1("Null codec");
con's avatar
con committed
130 131
}

132 133
// Ensure adding "..." to relative paths which is p4's convention
// for the current directory
134
static inline QString perforceRelativeFileArguments(const QString &args)
135 136
{
    if (args.isEmpty())
137 138
        return QLatin1String("...");
    return args + QLatin1String("/...");
139 140
}

141
static inline QStringList perforceRelativeProjectDirectory(const VcsBasePluginState &s)
142
{
143
    return QStringList(perforceRelativeFileArguments(s.relativeCurrentProject()));
144 145 146 147 148 149 150 151
}

// 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;
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
const char CMD_ID_PERFORCE_MENU[] = "Perforce.Menu";
const char CMD_ID_EDIT[] = "Perforce.Edit";
const char CMD_ID_ADD[] = "Perforce.Add";
const char CMD_ID_DELETE_FILE[] = "Perforce.Delete";
const char CMD_ID_OPENED[] = "Perforce.Opened";
const char CMD_ID_PROJECTLOG[] = "Perforce.ProjectLog";
const char CMD_ID_REPOSITORYLOG[] = "Perforce.RepositoryLog";
const char CMD_ID_REVERT[] = "Perforce.Revert";
const char CMD_ID_DIFF_CURRENT[] = "Perforce.DiffCurrent";
const char CMD_ID_DIFF_PROJECT[] = "Perforce.DiffProject";
const char CMD_ID_UPDATE_PROJECT[] = "Perforce.UpdateProject";
const char CMD_ID_REVERT_PROJECT[] = "Perforce.RevertProject";
const char CMD_ID_REVERT_UNCHANGED_PROJECT[] = "Perforce.RevertUnchangedProject";
const char CMD_ID_DIFF_ALL[] = "Perforce.DiffAll";
const char CMD_ID_SUBMIT[] = "Perforce.Submit";
const char CMD_ID_PENDING_CHANGES[] = "Perforce.PendingChanges";
const char CMD_ID_DESCRIBE[] = "Perforce.Describe";
const char CMD_ID_ANNOTATE_CURRENT[] = "Perforce.AnnotateCurrent";
const char CMD_ID_ANNOTATE[] = "Perforce.Annotate";
const char CMD_ID_FILELOG_CURRENT[] = "Perforce.FilelogCurrent";
const char CMD_ID_FILELOG[] = "Perforce.Filelog";
const char CMD_ID_UPDATEALL[] = "Perforce.UpdateAll";
con's avatar
con committed
176 177 178 179 180

////
// PerforcePlugin
////

181 182 183 184 185 186
PerforceResponse::PerforceResponse() :
    error(true),
    exitCode(-1)
{
}

Tobias Hunger's avatar
Tobias Hunger committed
187
PerforcePlugin *PerforcePlugin::m_instance = NULL;
con's avatar
con committed
188 189

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

220 221 222 223 224 225
static const VcsBaseSubmitEditorParameters submitParameters = {
    SUBMIT_MIMETYPE,
    PERFORCE_SUBMIT_EDITOR_ID,
    PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME,
    PERFORCESUBMITEDITOR_CONTEXT,
    VcsBaseSubmitEditorParameters::DiffFiles
con's avatar
con committed
226 227
};

228
bool PerforcePlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
con's avatar
con committed
229
{
230
    typedef VcsSubmitEditorFactory<PerforceSubmitEditor> PerforceSubmitEditorFactory;
con's avatar
con committed
231

232
    initializeVcs(new PerforceVersionControl(this));
233

234
    if (!MimeDatabase::addMimeTypes(QLatin1String(":/trolltech.perforce/Perforce.mimetypes.xml"), errorMessage))
con's avatar
con committed
235
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
236
    m_instance = this;
con's avatar
con committed
237

238
    m_settings.fromSettings(ICore::settings());
con's avatar
con committed
239

240
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
241 242

    // Editor factories
243
    addAutoReleasedObject(new PerforceSubmitEditorFactory(&submitParameters));
con's avatar
con committed
244 245

    static const char *describeSlot = SLOT(describe(QString,QString));
246
    const int editorCount = sizeof(editorParameters) / sizeof(editorParameters[0]);
247
    const auto widgetCreator = []() { return new PerforceEditor; };
248
    for (int i = 0; i < editorCount; i++)
249
        addAutoReleasedObject(new VcsEditorFactory(editorParameters + i, widgetCreator, this, describeSlot));
con's avatar
con committed
250

Friedemann Kleint's avatar
Friedemann Kleint committed
251
    const QString prefix = QLatin1String("p4");
252
    m_commandLocator = new CommandLocator("Perforce", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
253 254
    addAutoReleasedObject(m_commandLocator);

255
    ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
256

257
    ActionContainer *mperforce = ActionManager::createMenu(CMD_ID_PERFORCE_MENU);
con's avatar
con committed
258 259
    mperforce->menu()->setTitle(tr("&Perforce"));
    mtools->addMenu(mperforce);
260
    m_menuAction = mperforce->menu()->menuAction();
con's avatar
con committed
261

262 263
    Context globalcontext(Core::Constants::C_GLOBAL);
    Context perforcesubmitcontext(PERFORCESUBMITEDITOR_CONTEXT);
con's avatar
con committed
264

con's avatar
con committed
265
    Core::Command *command;
266

267 268
    m_diffFileAction = new ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_diffFileAction, CMD_ID_DIFF_CURRENT, globalcontext);
269
    command->setAttribute(Core::Command::CA_UpdateText);
270
    command->setDescription(tr("Diff Current File"));
271 272
    connect(m_diffFileAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
273
    m_commandLocator->appendCommand(command);
274

275 276
    m_annotateCurrentAction = new ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_annotateCurrentAction, CMD_ID_ANNOTATE_CURRENT, globalcontext);
277
    command->setAttribute(Core::Command::CA_UpdateText);
278
    command->setDescription(tr("Annotate Current File"));
279 280
    connect(m_annotateCurrentAction, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
281
    m_commandLocator->appendCommand(command);
282

283 284
    m_filelogCurrentAction = new ParameterAction(tr("Filelog Current File"), tr("Filelog \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_filelogCurrentAction, CMD_ID_FILELOG_CURRENT, globalcontext);
285
    command->setAttribute(Core::Command::CA_UpdateText);
286
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+F") : tr("Alt+P,Alt+F")));
287
    command->setDescription(tr("Filelog Current File"));
288 289
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this, SLOT(filelogCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
290
    m_commandLocator->appendCommand(command);
291

292
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
293

294 295
    m_editAction = new ParameterAction(tr("Edit"), tr("Edit \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_editAction, CMD_ID_EDIT, globalcontext);
con's avatar
con committed
296
    command->setAttribute(Core::Command::CA_UpdateText);
297
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+E") : tr("Alt+P,Alt+E")));
298
    command->setDescription(tr("Edit File"));
con's avatar
con committed
299 300
    connect(m_editAction, SIGNAL(triggered()), this, SLOT(openCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
301
    m_commandLocator->appendCommand(command);
con's avatar
con committed
302

303 304
    m_addAction = new ParameterAction(tr("Add"), tr("Add \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_addAction, CMD_ID_ADD, globalcontext);
con's avatar
con committed
305
    command->setAttribute(Core::Command::CA_UpdateText);
306
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+A") : tr("Alt+P,Alt+A")));
307
    command->setDescription(tr("Add File"));
con's avatar
con committed
308 309
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
310
    m_commandLocator->appendCommand(command);
con's avatar
con committed
311

312 313
    m_deleteAction = new ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_deleteAction, CMD_ID_DELETE_FILE, globalcontext);
con's avatar
con committed
314
    command->setAttribute(Core::Command::CA_UpdateText);
315
    command->setDescription(tr("Delete File"));
316
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
con's avatar
con committed
317
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
318
    m_commandLocator->appendCommand(command);
con's avatar
con committed
319

320 321
    m_revertFileAction = new ParameterAction(tr("Revert"), tr("Revert \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_revertFileAction, CMD_ID_REVERT, globalcontext);
con's avatar
con committed
322
    command->setAttribute(Core::Command::CA_UpdateText);
323
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+R") : tr("Alt+P,Alt+R")));
324
    command->setDescription(tr("Revert File"));
325
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
326
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
327
    m_commandLocator->appendCommand(command);
con's avatar
con committed
328

329
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
330

331
    const QString diffProjectDefaultText = tr("Diff Current Project/Session");
332 333
    m_diffProjectAction = new ParameterAction(diffProjectDefaultText, tr("Diff Project \"%1\""), ParameterAction::AlwaysEnabled, this);
    command = ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT, globalcontext);
con's avatar
con committed
334
    command->setAttribute(Core::Command::CA_UpdateText);
335
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+D") : tr("Alt+P,Alt+D")));
336
    command->setDescription(diffProjectDefaultText);
con's avatar
con committed
337 338
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
339
    m_commandLocator->appendCommand(command);
con's avatar
con committed
340

341 342
    m_logProjectAction = new ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_logProjectAction, CMD_ID_PROJECTLOG, globalcontext);
343 344 345
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    m_commandLocator->appendCommand(command);
347

348 349
    m_submitProjectAction = new ParameterAction(tr("Submit Project"), tr("Submit Project \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_submitProjectAction, CMD_ID_SUBMIT, globalcontext);
350
    command->setAttribute(Core::Command::CA_UpdateText);
351
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+S") : tr("Alt+P,Alt+S")));
352
    connect(m_submitProjectAction, SIGNAL(triggered()), this, SLOT(startSubmitProject()));
con's avatar
con committed
353
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
354
    m_commandLocator->appendCommand(command);
con's avatar
con committed
355

356
    const QString updateProjectDefaultText = tr("Update Current Project");
357 358
    m_updateProjectAction = new ParameterAction(updateProjectDefaultText, tr("Update Project \"%1\""), ParameterAction::AlwaysEnabled, this);
    command = ActionManager::registerAction(m_updateProjectAction, CMD_ID_UPDATE_PROJECT, globalcontext);
359
    command->setDescription(updateProjectDefaultText);
360 361 362
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
363
    m_commandLocator->appendCommand(command);
364

365 366
    m_revertUnchangedAction = new ParameterAction(tr("Revert Unchanged"), tr("Revert Unchanged Files of Project \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_revertUnchangedAction, CMD_ID_REVERT_UNCHANGED_PROJECT, globalcontext);
367 368 369
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertUnchangedAction, SIGNAL(triggered()), this, SLOT(revertUnchangedCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
370
    m_commandLocator->appendCommand(command);
371

372 373
    m_revertProjectAction = new ParameterAction(tr("Revert Project"), tr("Revert Project \"%1\""), ParameterAction::EnabledWithParameter, this);
    command = ActionManager::registerAction(m_revertProjectAction, CMD_ID_REVERT_PROJECT, globalcontext);
374 375 376
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertProjectAction, SIGNAL(triggered()), this, SLOT(revertCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
377
    m_commandLocator->appendCommand(command);
378

379
    mperforce->addSeparator(globalcontext);
380 381

    m_diffAllAction = new QAction(tr("Diff Opened Files"), this);
382
    command = ActionManager::registerAction(m_diffAllAction, CMD_ID_DIFF_ALL, globalcontext);
383
    connect(m_diffAllAction, SIGNAL(triggered()), this, SLOT(diffAllOpened()));
384
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
385
    m_commandLocator->appendCommand(command);
386

387
    m_openedAction = new QAction(tr("Opened"), this);
388 389
    command = ActionManager::registerAction(m_openedAction, CMD_ID_OPENED, globalcontext);
    command->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+P,Meta+O") : tr("Alt+P,Alt+O")));
390 391
    connect(m_openedAction, SIGNAL(triggered()), this, SLOT(printOpenedFileList()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
392
    m_commandLocator->appendCommand(command);
393 394

    m_logRepositoryAction = new QAction(tr("Repository Log"), this);
395
    command = ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
396 397
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
398
    m_commandLocator->appendCommand(command);
399 400

    m_pendingAction = new QAction(tr("Pending Changes..."), this);
401
    command = ActionManager::registerAction(m_pendingAction, CMD_ID_PENDING_CHANGES, globalcontext);
402 403
    connect(m_pendingAction, SIGNAL(triggered()), this, SLOT(printPendingChanges()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
404
    m_commandLocator->appendCommand(command);
405 406

    m_updateAllAction = new QAction(tr("Update All"), this);
407
    command = ActionManager::registerAction(m_updateAllAction, CMD_ID_UPDATEALL, globalcontext);
408
    connect(m_updateAllAction, SIGNAL(triggered()), this, SLOT(updateAll()));
con's avatar
con committed
409
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
410
    m_commandLocator->appendCommand(command);
con's avatar
con committed
411

412
    mperforce->addSeparator(globalcontext);
413

con's avatar
con committed
414
    m_describeAction = new QAction(tr("Describe..."), this);
415
    command = ActionManager::registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
con's avatar
con committed
416 417 418 419
    connect(m_describeAction, SIGNAL(triggered()), this, SLOT(describeChange()));
    mperforce->addAction(command);

    m_annotateAction = new QAction(tr("Annotate..."), this);
420
    command = ActionManager::registerAction(m_annotateAction, CMD_ID_ANNOTATE, globalcontext);
con's avatar
con committed
421 422 423 424
    connect(m_annotateAction, SIGNAL(triggered()), this, SLOT(annotate()));
    mperforce->addAction(command);

    m_filelogAction = new QAction(tr("Filelog..."), this);
425
    command = ActionManager::registerAction(m_filelogAction, CMD_ID_FILELOG, globalcontext);
con's avatar
con committed
426 427 428
    connect(m_filelogAction, SIGNAL(triggered()), this, SLOT(filelog()));
    mperforce->addAction(command);

429 430
    m_submitCurrentLogAction = new QAction(VcsBaseSubmitEditor::submitIcon(), tr("Submit"), this);
    command = ActionManager::registerAction(m_submitCurrentLogAction, SUBMIT_CURRENT, perforcesubmitcontext);
431
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
432 433
    connect(m_submitCurrentLogAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

434 435
    m_diffSelectedFiles = new QAction(VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
    command = ActionManager::registerAction(m_diffSelectedFiles, DIFF_SELECTED, perforcesubmitcontext);
con's avatar
con committed
436 437

    m_undoAction = new QAction(tr("&Undo"), this);
438
    command = ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, perforcesubmitcontext);
con's avatar
con committed
439 440

    m_redoAction = new QAction(tr("&Redo"), this);
441
    command = ActionManager::registerAction(m_redoAction, Core::Constants::REDO, perforcesubmitcontext);
con's avatar
con committed
442 443 444 445 446 447

    return true;
}

void PerforcePlugin::extensionsInitialized()
{
448
    VcsBasePlugin::extensionsInitialized();
449
    getTopLevel();
con's avatar
con committed
450 451 452 453
}

void PerforcePlugin::openCurrentFile()
{
454
    const VcsBasePluginState state = currentState();
455
    QTC_ASSERT(state.hasFile(), return);
456
    vcsOpen(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
457 458 459 460
}

void PerforcePlugin::addCurrentFile()
{
461
    const VcsBasePluginState state = currentState();
462
    QTC_ASSERT(state.hasFile(), return);
463
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
464 465 466 467
}

void PerforcePlugin::revertCurrentFile()
{
468
    const VcsBasePluginState state = currentState();
469
    QTC_ASSERT(state.hasFile(), return);
470

471
    QTextCodec *codec = VcsBaseEditorWidget::getCodec(state.currentFile());
con's avatar
con committed
472
    QStringList args;
473 474 475 476
    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
477 478
    if (result.error)
        return;
479
    // "foo.cpp - file(s) not opened on this client."
480 481
    // also revert when the output is empty: The file is unchanged but open then.
    if (result.stdOut.contains(QLatin1String(" - ")) || result.stdErr.contains(QLatin1String(" - ")))
482
        return;
con's avatar
con committed
483

484 485
    bool doNotRevert = false;
    if (!result.stdOut.isEmpty())
486
        doNotRevert = (QMessageBox::warning(ICore::dialogParent(), tr("p4 revert"),
487 488
                                            tr("The file has been changed. Do you want to revert it?"),
                                            QMessageBox::Yes, QMessageBox::No) == QMessageBox::No);
489 490
    if (doNotRevert)
        return;
con's avatar
con committed
491

492
    FileChangeBlocker fcb(state.currentFile());
493 494 495 496
    args.clear();
    args << QLatin1String("revert") << state.relativeCurrentFile();
    PerforceResponse result2 = runP4Cmd(state.currentFileTopLevel(), args,
                                        CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
497
    if (!result2.error)
498
        perforceVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
499 500 501 502
}

void PerforcePlugin::diffCurrentFile()
{
503
    const VcsBasePluginState state = currentState();
504
    QTC_ASSERT(state.hasFile(), return);
505
    p4Diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
506 507 508 509
}

void PerforcePlugin::diffCurrentProject()
{
510
    const VcsBasePluginState state = currentState();
511
    QTC_ASSERT(state.hasProject(), return);
512
    p4Diff(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
con's avatar
con committed
513 514 515 516
}

void PerforcePlugin::diffAllOpened()
{
517
    p4Diff(m_settings.topLevel(), QStringList());
con's avatar
con committed
518 519
}

520
void PerforcePlugin::updateCurrentProject()
521
{
522
    const VcsBasePluginState state = currentState();
523
    QTC_ASSERT(state.hasProject(), return);
524
    updateCheckout(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
525 526
}

527 528 529 530 531 532
void PerforcePlugin::updateAll()
{
    updateCheckout(m_settings.topLevel());
}

void PerforcePlugin::revertCurrentProject()
533
{
534
    const VcsBasePluginState state = currentState();
535
    QTC_ASSERT(state.hasProject(), return);
536 537

    const QString msg = tr("Do you want to revert all changes to the project \"%1\"?").arg(state.currentProjectName());
538
    if (QMessageBox::warning(ICore::dialogParent(), tr("p4 revert"), msg, QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
539
        return;
540
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
541 542
}

543
void PerforcePlugin::revertUnchangedCurrentProject()
544
{
545
    // revert -a.
546
    const VcsBasePluginState state = currentState();
547
    QTC_ASSERT(state.hasProject(), return);
548
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true);
549 550
}

551 552 553 554 555 556 557 558 559 560 561 562
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)
563 564 565
{
    QStringList args(QLatin1String("sync"));
    args.append(dirs);
566 567 568 569 570 571 572
    const PerforceResponse resp = runP4Cmd(workingDir, args,
                                           CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
    if (dirs.empty()) {
        if (!workingDir.isEmpty())
            perforceVersionControl()->emitRepositoryChanged(workingDir);
    } else {
        const QChar slash = QLatin1Char('/');
573
        foreach (const QString &dir, dirs)
574 575
            perforceVersionControl()->emitRepositoryChanged(workingDir + slash + dir);
    }
576 577
}

con's avatar
con committed
578 579
void PerforcePlugin::printOpenedFileList()
{
580 581 582 583 584 585 586
    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.
587
    VcsBaseOutputWindow *outWin = VcsBaseOutputWindow::instance();
588 589 590 591 592 593 594 595
    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);
596
        if (mapped.isEmpty())
597
            outWin->appendSilently(line);
598
        else
599 600
            outWin->appendSilently(mapped + QLatin1Char(' ') + line.mid(delimiterPos));
    }
601
    outWin->popup(IOutputPane::ModeSwitch | IOutputPane::WithFocus);
con's avatar
con committed
602 603
}

604
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
605
{
606

607
    if (raiseSubmitEditor())
con's avatar
con committed
608 609
        return;

610
    if (isCommitEditorOpen()) {
611
        VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
612 613 614
        return;
    }

615
    const VcsBasePluginState state = currentState();
616
    QTC_ASSERT(state.hasProject(), return);
617 618 619 620 621 622

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

624 625 626
    args << QLatin1String("change") << QLatin1String("-o");
    PerforceResponse result = runP4Cmd(state.currentProjectTopLevel(), args,
                                       RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
627
    if (result.error) {
628
        cleanCommitMessageFile();
con's avatar
con committed
629 630 631
        return;
    }

632
    TempFileSaver saver;
633
    saver.setAutoRemove(false);
634
    saver.write(result.stdOut.toLatin1());
635
    if (!saver.finalize()) {
636
        VcsBaseOutputWindow::instance()->appendError(saver.errorString());
637 638 639 640
        cleanCommitMessageFile();
        return;
    }
    m_commitMessageFileName = saver.fileName();
con's avatar
con committed
641

642 643 644 645 646 647
    args.clear();
    args << QLatin1String("fstat");
    args.append(perforceRelativeProjectDirectory(state));
    PerforceResponse fstatResult = runP4Cmd(state.currentProjectTopLevel(), args,
                                            RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
    if (fstatResult.error) {
648
        cleanCommitMessageFile();
con's avatar
con committed
649 650 651
        return;
    }

652
    QStringList fstatLines = fstatResult.stdOut.split(QLatin1Char('\n'));
con's avatar
con committed
653
    QStringList depotFileNames;
654
    foreach (const QString &line, fstatLines) {
655
        if (line.startsWith(QLatin1String("... depotFile")))
con's avatar
con committed
656 657 658
            depotFileNames.append(line.mid(14));
    }
    if (depotFileNames.isEmpty()) {
659
        VcsBaseOutputWindow::instance()->appendWarning(tr("Project has no files"));
660
        cleanCommitMessageFile();
con's avatar
con committed
661 662 663
        return;
    }

664
    openPerforceSubmitEditor(m_commitMessageFileName, depotFileNames);
con's avatar
con committed
665 666
}

667
IEditor *PerforcePlugin::openPerforceSubmitEditor(const QString &fileName, const QStringList &depotFileNames)
con's avatar
con committed
668
{
669
    IEditor *editor = EditorManager::openEditor(fileName, PERFORCE_SUBMIT_EDITOR_ID);
dt's avatar
dt committed
670
    PerforceSubmitEditor *submitEditor = static_cast<PerforceSubmitEditor*>(editor);
671
    setSubmitEditor(submitEditor);
con's avatar
con committed
672
    submitEditor->restrictToProjectFiles(depotFileNames);
673
    submitEditor->registerActions(m_undoAction, m_redoAction, m_submitCurrentLogAction, m_diffSelectedFiles);
674
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotSubmitDiff(QStringList)));
675
    submitEditor->setCheckScriptWorkingDirectory(m_commitWorkingDirectory);
con's avatar
con committed
676 677 678 679 680 681
    return editor;
}

void PerforcePlugin::printPendingChanges()
{
    qApp->setOverrideCursor(Qt::WaitCursor);
682
    PendingChangesDialog dia(pendingChangesData(), ICore::mainWindow());
con's avatar
con committed
683 684
    qApp->restoreOverrideCursor();
    if (dia.exec() == QDialog::Accepted) {
685 686 687
        const int i = dia.changeNumber();
        QStringList args(QLatin1String("submit"));
        args << QLatin1String("-c") << QString::number(i);
688 689
        runP4Cmd(m_settings.topLevel(), args,
                 CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
690 691 692 693 694 695 696 697 698 699 700 701
    }
}

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

void PerforcePlugin::annotateCurrentFile()
{
702
    const VcsBasePluginState state = currentState();
703
    QTC_ASSERT(state.hasFile(), return);
704
    annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
705 706 707 708
}

void PerforcePlugin::annotate()
{
709
    const QString file = QFileDialog::getOpenFileName(ICore::dialogParent(), tr("p4 annotate"));
710 711 712 713
    if (!file.isEmpty()) {
        const QFileInfo fi(file);
        annotate(fi.absolutePath(), fi.fileName());
    }
con's avatar
con committed
714 715
}

716 717
void PerforcePlugin::vcsAnnotate(const QString &workingDirectory, const QString &file,
                                 const QString &revision, int lineNumber)
718
{
719
    annotate(workingDirectory, file, revision, lineNumber);
720 721 722 723 724 725
}

void PerforcePlugin::annotate(const QString &workingDir,
                              const QString &fileName,
                              const QString &changeList /* = QString() */,
                              int lineNumber /* = -1 */)
con's avatar
con committed
726
{
727
    const QStringList files = QStringList(fileName);
728 729 730
    QTextCodec *codec = VcsBaseEditorWidget::getCodec(workingDir, files);
    const QString id = VcsBaseEditorWidget::getTitleId(workingDir, files, changeList);
    const QString source = VcsBaseEditorWidget::getSource(workingDir, files);
con's avatar
con committed
731
    QStringList args;
732
    args << QLatin1String("annotate") << QLatin1String("-cqi");
733
    if (changeList.isEmpty())
734
        args << fileName;
735
    else
736
        args << (fileName + QLatin1Char('@') + changeList);
737 738 739
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
con's avatar
con committed
740
    if (!result.error) {
741
        if (lineNumber < 1)
742 743 744 745 746
            lineNumber = VcsBaseEditorWidget::lineNumberOfCurrentEditor();
        IEditor *ed = showOutputInEditor(tr("p4 annotate %1").arg(id),
                                         result.stdOut, VcsBase::AnnotateOutput,
                                         source, codec);
        VcsBaseEditorWidget::gotoLineOfEditor(ed, lineNumber);
con's avatar
con committed
747 748 749 750 751
    }
}

void PerforcePlugin::filelogCurrentFile()
{
752
    const VcsBasePluginState state = currentState();
753
    QTC_ASSERT(state.hasFile(), return);
754
    filelog(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
con's avatar
con committed
755 756 757 758
}

void PerforcePlugin::filelog()
{
759
    const QString file = QFileDialog::getOpenFileName(ICore::dialogParent(), tr("p4 filelog"));
760 761
    if (!file.isEmpty()) {
        const QFileInfo fi(file);
762
        filelog(fi.absolutePath(), fi.fileName());
763
    }