perforceplugin.cpp 64.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@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
29
** Nokia at qt-info@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/id.h>
con's avatar
con committed
48
#include <coreplugin/coreconstants.h>
hjk's avatar
hjk committed
49
#include <coreplugin/editormanager/editormanager.h>
50
#include <coreplugin/documentmanager.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

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

72 73 74 75 76
#include <QAction>
#include <QFileDialog>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
con's avatar
con committed
77

hjk's avatar
hjk committed
78
static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
79
{
hjk's avatar
hjk committed
80
    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
    "application/vnd.nokia.text.scs_commandlog",
    "scslog"},
hjk's avatar
hjk committed
86
{   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
    "application/vnd.nokia.text.scs_filelog",
    "scsfilelog"},
hjk's avatar
hjk committed
92
{    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
    "application/vnd.nokia.text.scs_annotation",
    "scsannotate"},
hjk's avatar
hjk committed
98
{   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
    "text/x-patch","diff"}
};

// Utility to find a parameter set by type
hjk's avatar
hjk committed
106
static inline const VcsBase::VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
107
{
hjk's avatar
hjk committed
108 109
    const VcsBase::EditorContentType et = static_cast<VcsBase::EditorContentType>(ie);
    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
// 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("..."));
123
    QTC_ASSERT(args.size() == 1, return QStringList());
124 125 126 127 128
    QStringList p4Args = args;
    p4Args.front() += QLatin1String("/...");
    return p4Args;
}

hjk's avatar
hjk committed
129
static inline QStringList perforceRelativeProjectDirectory(const VcsBase::VcsBasePluginState &s)
130 131 132 133 134 135 136 137 138 139
{
    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() :
hjk's avatar
hjk committed
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
{
}

hjk's avatar
hjk committed
216
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
217
    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
{
hjk's avatar
hjk committed
235 236
    typedef VcsBase::VcsEditorFactory<PerforceEditor> PerforceEditorFactory;
    typedef VcsBase::VcsSubmitEditorFactory<PerforceSubmitEditor> PerforceSubmitEditorFactory;
con's avatar
con committed
237

238
    initializeVcs(new PerforceVersionControl(this));
239

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

hjk's avatar
hjk committed
244
    if (QSettings *settings = Core::ICore::settings())
con's avatar
con committed
245 246
        m_settings.fromSettings(settings);

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

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

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

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

con's avatar
con committed
261
    //register actions
hjk's avatar
hjk committed
262
    Core::ActionManager *am = Core::ICore::actionManager();
con's avatar
con committed
263

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

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

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

con's avatar
con committed
276
    Core::Command *command;
277 278 279 280

    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);
281
    command->setDescription(tr("Diff Current File"));
282 283
    connect(m_diffFileAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
284
    m_commandLocator->appendCommand(command);
285 286 287 288

    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);
289
    command->setDescription(tr("Annotate Current File"));
290 291
    connect(m_annotateCurrentAction, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
292
    m_commandLocator->appendCommand(command);
293 294 295 296 297

    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")));
298
    command->setDescription(tr("Filelog Current File"));
299 300
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this, SLOT(filelogCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
301
    m_commandLocator->appendCommand(command);
302 303

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

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

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

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

331 332
    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
333
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
334
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+R")));
335
    command->setDescription(tr("Revert File"));
336
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
337
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
338
    m_commandLocator->appendCommand(command);
con's avatar
con committed
339

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

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

Friedemann Kleint's avatar
Friedemann Kleint committed
352
    m_logProjectAction = new Utils::ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
353 354 355 356
    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
357
    m_commandLocator->appendCommand(command);
358

359 360 361
    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
362
    command->setDefaultKeySequence(QKeySequence(tr("Alt+P,Alt+S")));
363
    connect(m_submitProjectAction, SIGNAL(triggered()), this, SLOT(startSubmitProject()));
con's avatar
con committed
364
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
365
    m_commandLocator->appendCommand(command);
con's avatar
con committed
366

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

376 377 378 379 380
    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
381
    m_commandLocator->appendCommand(command);
382

383 384 385 386 387
    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
388
    m_commandLocator->appendCommand(command);
389

390 391 392 393 394
    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()));
395
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
396
    m_commandLocator->appendCommand(command);
397

398 399 400 401 402
    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
403
    m_commandLocator->appendCommand(command);
404 405 406 407 408

    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
409
    m_commandLocator->appendCommand(command);
410 411 412 413 414

    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
415
    m_commandLocator->appendCommand(command);
416 417 418 419

    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
420
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
421
    m_commandLocator->appendCommand(command);
con's avatar
con committed
422

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

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

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

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

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

hjk's avatar
hjk committed
445
    m_diffSelectedFiles = new QAction(VcsBase::VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
con's avatar
con committed
446 447 448 449 450 451 452 453 454 455 456 457 458
    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()
{
hjk's avatar
hjk committed
459
    VcsBase::VcsBasePlugin::extensionsInitialized();
460
    getTopLevel();
con's avatar
con committed
461 462 463 464
}

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

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

void PerforcePlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
479
    const VcsBase::VcsBasePluginState state = currentState();
480
    QTC_ASSERT(state.hasFile(), return);
481

hjk's avatar
hjk committed
482
    QTextCodec *codec = VcsBase::VcsBaseEditorWidget::getCodec(state.currentFile());
con's avatar
con committed
483
    QStringList args;
484 485 486 487
    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
488 489
    if (result.error)
        return;
490
    // "foo.cpp - file(s) not opened on this client."
491 492
    // also revert when the output is empty: The file is unchanged but open then.
    if (result.stdOut.contains(QLatin1String(" - ")) || result.stdErr.contains(QLatin1String(" - ")))
493
        return;
con's avatar
con committed
494

495 496 497 498 499
    bool doNotRevert = false;
    if (!result.stdOut.isEmpty())
        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);
500 501
    if (doNotRevert)
        return;
con's avatar
con committed
502

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

void PerforcePlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
514
    const VcsBase::VcsBasePluginState state = currentState();
515
    QTC_ASSERT(state.hasFile(), return);
516
    p4Diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
517 518 519 520
}

void PerforcePlugin::diffCurrentProject()
{
hjk's avatar
hjk committed
521
    const VcsBase::VcsBasePluginState state = currentState();
522
    QTC_ASSERT(state.hasProject(), return);
523
    p4Diff(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
con's avatar
con committed
524 525 526 527
}

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

531
void PerforcePlugin::updateCurrentProject()
532
{
hjk's avatar
hjk committed
533
    const VcsBase::VcsBasePluginState state = currentState();
534
    QTC_ASSERT(state.hasProject(), return);
535
    updateCheckout(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
536 537
}

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

void PerforcePlugin::revertCurrentProject()
544
{
hjk's avatar
hjk committed
545
    const VcsBase::VcsBasePluginState state = currentState();
546
    QTC_ASSERT(state.hasProject(), return);
547 548 549

    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)
550
        return;
551
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
552 553
}

554
void PerforcePlugin::revertUnchangedCurrentProject()
555
{
556
    // revert -a.
hjk's avatar
hjk committed
557
    const VcsBase::VcsBasePluginState state = currentState();
558
    QTC_ASSERT(state.hasProject(), return);
559
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true);
560 561
}

562 563 564 565 566 567 568 569 570 571 572 573
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)
574 575 576
{
    QStringList args(QLatin1String("sync"));
    args.append(dirs);
577 578 579 580 581 582 583
    const PerforceResponse resp = runP4Cmd(workingDir, args,
                                           CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
    if (dirs.empty()) {
        if (!workingDir.isEmpty())
            perforceVersionControl()->emitRepositoryChanged(workingDir);
    } else {
        const QChar slash = QLatin1Char('/');
584
        foreach(const QString &dir, dirs)
585 586
            perforceVersionControl()->emitRepositoryChanged(workingDir + slash + dir);
    }
587 588
}

con's avatar
con committed
589 590
void PerforcePlugin::printOpenedFileList()
{
591 592 593 594 595 596 597
    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.
hjk's avatar
hjk committed
598
    VcsBase::VcsBaseOutputWindow *outWin = VcsBase::VcsBaseOutputWindow::instance();
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
    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
614 615
}

616
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
617
{
618

hjk's avatar
hjk committed
619
    if (VcsBase::VcsBaseSubmitEditor::raiseSubmitEditor())
con's avatar
con committed
620 621
        return;

622
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
623
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
624 625 626
        return;
    }

hjk's avatar
hjk committed
627
    const VcsBase::VcsBasePluginState state = currentState();
628
    QTC_ASSERT(state.hasProject(), return);
629 630 631 632 633 634

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

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

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

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

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

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

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

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

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

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

void PerforcePlugin::annotate()
{
721 722 723 724 725
    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
726 727
}

728
void PerforcePlugin::vcsAnnotate(const QString &file, const QString &revision, int lineNumber)
729 730 731 732 733 734 735 736 737
{
    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
738
{
739
    const QStringList files = QStringList(fileName);
hjk's avatar
hjk committed
740 741 742
    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
743
    QStringList args;
744 745 746 747 748 749
    args << QLatin1String("annotate") << QLatin1String("-cqi");
    if (changeList.isEmpty()) {
        args << fileName;
    } else {
        args << (fileName + QLatin1Char('@') + changeList);
    }
750 751 752
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
con's avatar
con committed
753
    if (!result.error) {
754
        if (lineNumber < 1)
hjk's avatar
hjk committed
755
            lineNumber = VcsBase::VcsBaseEditorWidget::lineNumberOfCurrentEditor();
con's avatar
con committed
756
        const QFileInfo fi(fileName);
757
        Core::IEditor *ed = showOutputInEditor(tr("p4 annotate %1").arg(id),
hjk's avatar
hjk committed
758
                                               result.stdOut, VcsBase::AnnotateOutput,
759
                                               source, codec);
hjk's avatar
hjk committed
760
        VcsBase::VcsBaseEditorWidget::gotoLineOfEditor(ed, lineNumber);
con's avatar
con committed
761 762 763 764 765
    }
}

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

void PerforcePlugin::filelog()
{
773 774 775 776 777
    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
778 779
}

780 781
void PerforcePlugin::logProject()
{
hjk's avatar
hjk committed
782
    const VcsBase::VcsBasePluginState state = currentState();
783
    QTC_ASSERT(state.hasProject(), return);
784 785 786 787 788
    filelog(state.currentProjectTopLevel(), perforceRelativeFileArguments(state.relativeCurrentProject()));
}

void PerforcePlugin::logRepository()
{
hjk's avatar
hjk committed
789
    const VcsBase::VcsBasePluginState state = currentState();
790
    QTC_ASSERT(state.hasTopLevel(), return);
791 792 793 794 795
    filelog(state.topLevel(), perforceRelativeFileArguments(QStringList()));
}

void PerforcePlugin::filelog(const QString &workingDir, const QStringList &fileNames,
                             bool enableAnnotationContextMenu)
con's avatar
con committed
796
{
hjk's avatar
hjk committed
797 798
    const QString id = VcsBase::VcsBaseEditorWidget::getTitleId(workingDir, fileNames);
    QTextCodec *codec = VcsBase::VcsBaseEditorWidget::getCodec(workingDir, fileNames);
con's avatar
con committed
799
    QStringList args;
800
    args << QLatin1String("filelog") << QLatin1String("-li");
801 802