perforceplugin.cpp 63.8 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
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
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
**
29
**************************************************************************/
hjk's avatar
hjk committed
30

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

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

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

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

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

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

// Utility to find a parameter set by type
hjk's avatar
hjk committed
104
static inline const VcsBase::VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
105
{
hjk's avatar
hjk committed
106 107
    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
108 109 110 111 112 113 114
}

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

115 116 117 118 119 120
// 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("..."));
121
    QTC_ASSERT(args.size() == 1, return QStringList());
122 123 124 125 126
    QStringList p4Args = args;
    p4Args.front() += QLatin1String("/...");
    return p4Args;
}

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

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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";
con's avatar
con committed
163 164 165 166 167

////
// PerforcePlugin
////

168 169 170 171 172 173 174 175 176
namespace Perforce {
namespace Internal {

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

con's avatar
con committed
177 178 179
PerforcePlugin *PerforcePlugin::m_perforcePluginInstance = NULL;

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

hjk's avatar
hjk committed
211
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
212
    Perforce::Constants::SUBMIT_MIMETYPE,
213
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_ID,
214
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME,
215
    Perforce::Constants::PERFORCESUBMITEDITOR_CONTEXT
con's avatar
con committed
216 217
};

218
bool PerforcePlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
con's avatar
con committed
219
{
hjk's avatar
hjk committed
220 221
    typedef VcsBase::VcsEditorFactory<PerforceEditor> PerforceEditorFactory;
    typedef VcsBase::VcsSubmitEditorFactory<PerforceSubmitEditor> PerforceSubmitEditorFactory;
con's avatar
con committed
222

223
    initializeVcs(new PerforceVersionControl(this));
224

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

229
    m_settings.fromSettings(Core::ICore::settings());
con's avatar
con committed
230

231
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
232 233

    // Editor factories
234
    addAutoReleasedObject(new PerforceSubmitEditorFactory(&submitParameters));
con's avatar
con committed
235 236

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

Friedemann Kleint's avatar
Friedemann Kleint committed
241 242 243 244
    const QString prefix = QLatin1String("p4");
    m_commandLocator = new Locator::CommandLocator(QLatin1String("Perforce"), prefix, prefix);
    addAutoReleasedObject(m_commandLocator);

245
    Core::ActionContainer *mtools =
Eike Ziller's avatar
Eike Ziller committed
246
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
247

248
    Core::ActionContainer *mperforce =
Eike Ziller's avatar
Eike Ziller committed
249
        Core::ActionManager::createMenu(Core::Id(CMD_ID_PERFORCE_MENU));
con's avatar
con committed
250 251
    mperforce->menu()->setTitle(tr("&Perforce"));
    mtools->addMenu(mperforce);
252
    m_menuAction = mperforce->menu()->menuAction();
con's avatar
con committed
253

254
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
255
    Core::Context perforcesubmitcontext(Constants::PERFORCESUBMITEDITOR_CONTEXT);
con's avatar
con committed
256

con's avatar
con committed
257
    Core::Command *command;
258 259

    m_diffFileAction = new Utils::ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
260
    command = Core::ActionManager::registerAction(m_diffFileAction, CMD_ID_DIFF_CURRENT, globalcontext);
261
    command->setAttribute(Core::Command::CA_UpdateText);
262
    command->setDescription(tr("Diff Current File"));
263 264
    connect(m_diffFileAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
265
    m_commandLocator->appendCommand(command);
266 267

    m_annotateCurrentAction = new Utils::ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
268
    command = Core::ActionManager::registerAction(m_annotateCurrentAction, CMD_ID_ANNOTATE_CURRENT, globalcontext);
269
    command->setAttribute(Core::Command::CA_UpdateText);
270
    command->setDescription(tr("Annotate Current File"));
271 272
    connect(m_annotateCurrentAction, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
273
    m_commandLocator->appendCommand(command);
274 275

    m_filelogCurrentAction = new Utils::ParameterAction(tr("Filelog Current File"), tr("Filelog \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
276
    command = Core::ActionManager::registerAction(m_filelogCurrentAction, CMD_ID_FILELOG_CURRENT, globalcontext);
277
    command->setAttribute(Core::Command::CA_UpdateText);
278
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+F") : tr("Alt+P,Alt+F")));
279
    command->setDescription(tr("Filelog Current File"));
280 281
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this, SLOT(filelogCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
282
    m_commandLocator->appendCommand(command);
283

284
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
285

286
    m_editAction = new Utils::ParameterAction(tr("Edit"), tr("Edit \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
287
    command = Core::ActionManager::registerAction(m_editAction, CMD_ID_EDIT, globalcontext);
con's avatar
con committed
288
    command->setAttribute(Core::Command::CA_UpdateText);
289
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+E") : tr("Alt+P,Alt+E")));
290
    command->setDescription(tr("Edit File"));
con's avatar
con committed
291 292
    connect(m_editAction, SIGNAL(triggered()), this, SLOT(openCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
293
    m_commandLocator->appendCommand(command);
con's avatar
con committed
294

295
    m_addAction = new Utils::ParameterAction(tr("Add"), tr("Add \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
296
    command = Core::ActionManager::registerAction(m_addAction, CMD_ID_ADD, globalcontext);
con's avatar
con committed
297
    command->setAttribute(Core::Command::CA_UpdateText);
298
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+A") : tr("Alt+P,Alt+A")));
299
    command->setDescription(tr("Add File"));
con's avatar
con committed
300 301
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
302
    m_commandLocator->appendCommand(command);
con's avatar
con committed
303

304
    m_deleteAction = new Utils::ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
305
    command = Core::ActionManager::registerAction(m_deleteAction, CMD_ID_DELETE_FILE, globalcontext);
con's avatar
con committed
306
    command->setAttribute(Core::Command::CA_UpdateText);
307
    command->setDescription(tr("Delete File"));
308
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
con's avatar
con committed
309
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
310
    m_commandLocator->appendCommand(command);
con's avatar
con committed
311

312
    m_revertFileAction = new Utils::ParameterAction(tr("Revert"), tr("Revert \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
313
    command = Core::ActionManager::registerAction(m_revertFileAction, CMD_ID_REVERT, globalcontext);
con's avatar
con committed
314
    command->setAttribute(Core::Command::CA_UpdateText);
315
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+R") : tr("Alt+P,Alt+R")));
316
    command->setDescription(tr("Revert File"));
317
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
318
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
319
    m_commandLocator->appendCommand(command);
con's avatar
con committed
320

321
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
322

323
    const QString diffProjectDefaultText = tr("Diff Current Project/Session");
324
    m_diffProjectAction = new Utils::ParameterAction(diffProjectDefaultText, tr("Diff Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
Eike Ziller's avatar
Eike Ziller committed
325
    command = Core::ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT, globalcontext);
con's avatar
con committed
326
    command->setAttribute(Core::Command::CA_UpdateText);
327
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+D") : tr("Alt+P,Alt+D")));
328
    command->setDescription(diffProjectDefaultText);
con's avatar
con committed
329 330
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
331
    m_commandLocator->appendCommand(command);
con's avatar
con committed
332

Friedemann Kleint's avatar
Friedemann Kleint committed
333
    m_logProjectAction = new Utils::ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
334
    command = Core::ActionManager::registerAction(m_logProjectAction, CMD_ID_PROJECTLOG, globalcontext);
335 336 337
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
338
    m_commandLocator->appendCommand(command);
339

340
    m_submitProjectAction = new Utils::ParameterAction(tr("Submit Project"), tr("Submit Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
341
    command = Core::ActionManager::registerAction(m_submitProjectAction, CMD_ID_SUBMIT, globalcontext);
342
    command->setAttribute(Core::Command::CA_UpdateText);
343
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+S") : tr("Alt+P,Alt+S")));
344
    connect(m_submitProjectAction, SIGNAL(triggered()), this, SLOT(startSubmitProject()));
con's avatar
con committed
345
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    m_commandLocator->appendCommand(command);
con's avatar
con committed
347

348
    const QString updateProjectDefaultText = tr("Update Current Project");
349
    m_updateProjectAction = new Utils::ParameterAction(updateProjectDefaultText, tr("Update Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
Eike Ziller's avatar
Eike Ziller committed
350
    command = Core::ActionManager::registerAction(m_updateProjectAction, CMD_ID_UPDATE_PROJECT, globalcontext);
351
    command->setDescription(updateProjectDefaultText);
352 353 354
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
355
    m_commandLocator->appendCommand(command);
356

357
    m_revertUnchangedAction = new Utils::ParameterAction(tr("Revert Unchanged"), tr("Revert Unchanged Files of Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
358
    command = Core::ActionManager::registerAction(m_revertUnchangedAction, CMD_ID_REVERT_UNCHANGED_PROJECT, globalcontext);
359 360 361
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertUnchangedAction, SIGNAL(triggered()), this, SLOT(revertUnchangedCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
362
    m_commandLocator->appendCommand(command);
363

364
    m_revertProjectAction = new Utils::ParameterAction(tr("Revert Project"), tr("Revert Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
365
    command = Core::ActionManager::registerAction(m_revertProjectAction, CMD_ID_REVERT_PROJECT, globalcontext);
366 367 368
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertProjectAction, SIGNAL(triggered()), this, SLOT(revertCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
369
    m_commandLocator->appendCommand(command);
370

371
    mperforce->addSeparator(globalcontext);
372 373

    m_diffAllAction = new QAction(tr("Diff Opened Files"), this);
Eike Ziller's avatar
Eike Ziller committed
374
    command = Core::ActionManager::registerAction(m_diffAllAction, CMD_ID_DIFF_ALL, globalcontext);
375
    connect(m_diffAllAction, SIGNAL(triggered()), this, SLOT(diffAllOpened()));
376
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
377
    m_commandLocator->appendCommand(command);
378

379
    m_openedAction = new QAction(tr("Opened"), this);
Eike Ziller's avatar
Eike Ziller committed
380
    command = Core::ActionManager::registerAction(m_openedAction, CMD_ID_OPENED, globalcontext);
381
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+O") : tr("Alt+P,Alt+O")));
382 383
    connect(m_openedAction, SIGNAL(triggered()), this, SLOT(printOpenedFileList()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
384
    m_commandLocator->appendCommand(command);
385 386

    m_logRepositoryAction = new QAction(tr("Repository Log"), this);
Eike Ziller's avatar
Eike Ziller committed
387
    command = Core::ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
388 389
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
390
    m_commandLocator->appendCommand(command);
391 392

    m_pendingAction = new QAction(tr("Pending Changes..."), this);
Eike Ziller's avatar
Eike Ziller committed
393
    command = Core::ActionManager::registerAction(m_pendingAction, CMD_ID_PENDING_CHANGES, globalcontext);
394 395
    connect(m_pendingAction, SIGNAL(triggered()), this, SLOT(printPendingChanges()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
396
    m_commandLocator->appendCommand(command);
397 398

    m_updateAllAction = new QAction(tr("Update All"), this);
Eike Ziller's avatar
Eike Ziller committed
399
    command = Core::ActionManager::registerAction(m_updateAllAction, CMD_ID_UPDATEALL, globalcontext);
400
    connect(m_updateAllAction, SIGNAL(triggered()), this, SLOT(updateAll()));
con's avatar
con committed
401
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
402
    m_commandLocator->appendCommand(command);
con's avatar
con committed
403

404
    mperforce->addSeparator(globalcontext);
405

con's avatar
con committed
406
    m_describeAction = new QAction(tr("Describe..."), this);
Eike Ziller's avatar
Eike Ziller committed
407
    command = Core::ActionManager::registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
con's avatar
con committed
408 409 410 411
    connect(m_describeAction, SIGNAL(triggered()), this, SLOT(describeChange()));
    mperforce->addAction(command);

    m_annotateAction = new QAction(tr("Annotate..."), this);
Eike Ziller's avatar
Eike Ziller committed
412
    command = Core::ActionManager::registerAction(m_annotateAction, CMD_ID_ANNOTATE, globalcontext);
con's avatar
con committed
413 414 415 416
    connect(m_annotateAction, SIGNAL(triggered()), this, SLOT(annotate()));
    mperforce->addAction(command);

    m_filelogAction = new QAction(tr("Filelog..."), this);
Eike Ziller's avatar
Eike Ziller committed
417
    command = Core::ActionManager::registerAction(m_filelogAction, CMD_ID_FILELOG, globalcontext);
con's avatar
con committed
418 419 420
    connect(m_filelogAction, SIGNAL(triggered()), this, SLOT(filelog()));
    mperforce->addAction(command);

hjk's avatar
hjk committed
421
    m_submitCurrentLogAction = new QAction(VcsBase::VcsBaseSubmitEditor::submitIcon(), tr("Submit"), this);
Eike Ziller's avatar
Eike Ziller committed
422
    command = Core::ActionManager::registerAction(m_submitCurrentLogAction, Constants::SUBMIT_CURRENT, perforcesubmitcontext);
423
    command->setAttribute(Core::Command::CA_UpdateText);
con's avatar
con committed
424 425
    connect(m_submitCurrentLogAction, SIGNAL(triggered()), this, SLOT(submitCurrentLog()));

hjk's avatar
hjk committed
426
    m_diffSelectedFiles = new QAction(VcsBase::VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
Eike Ziller's avatar
Eike Ziller committed
427
    command = Core::ActionManager::registerAction(m_diffSelectedFiles, Constants::DIFF_SELECTED, perforcesubmitcontext);
con's avatar
con committed
428 429

    m_undoAction = new QAction(tr("&Undo"), this);
Eike Ziller's avatar
Eike Ziller committed
430
    command = Core::ActionManager::registerAction(m_undoAction, Core::Constants::UNDO, perforcesubmitcontext);
con's avatar
con committed
431 432

    m_redoAction = new QAction(tr("&Redo"), this);
Eike Ziller's avatar
Eike Ziller committed
433
    command = Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, perforcesubmitcontext);
con's avatar
con committed
434 435 436 437 438 439

    return true;
}

void PerforcePlugin::extensionsInitialized()
{
hjk's avatar
hjk committed
440
    VcsBase::VcsBasePlugin::extensionsInitialized();
441
    getTopLevel();
con's avatar
con committed
442 443 444 445
}

void PerforcePlugin::openCurrentFile()
{
hjk's avatar
hjk committed
446
    const VcsBase::VcsBasePluginState state = currentState();
447
    QTC_ASSERT(state.hasFile(), return);
448
    vcsOpen(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
449 450 451 452
}

void PerforcePlugin::addCurrentFile()
{
hjk's avatar
hjk committed
453
    const VcsBase::VcsBasePluginState state = currentState();
454
    QTC_ASSERT(state.hasFile(), return);
455
    vcsAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
456 457 458 459
}

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

hjk's avatar
hjk committed
463
    QTextCodec *codec = VcsBase::VcsBaseEditorWidget::getCodec(state.currentFile());
con's avatar
con committed
464
    QStringList args;
465 466 467 468
    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
469 470
    if (result.error)
        return;
471
    // "foo.cpp - file(s) not opened on this client."
472 473
    // also revert when the output is empty: The file is unchanged but open then.
    if (result.stdOut.contains(QLatin1String(" - ")) || result.stdErr.contains(QLatin1String(" - ")))
474
        return;
con's avatar
con committed
475

476 477 478 479 480
    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);
481 482
    if (doNotRevert)
        return;
con's avatar
con committed
483

484 485 486 487 488
    Core::FileChangeBlocker fcb(state.currentFile());
    args.clear();
    args << QLatin1String("revert") << state.relativeCurrentFile();
    PerforceResponse result2 = runP4Cmd(state.currentFileTopLevel(), args,
                                        CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
489
    if (!result2.error)
490
        perforceVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
491 492 493 494
}

void PerforcePlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
495
    const VcsBase::VcsBasePluginState state = currentState();
496
    QTC_ASSERT(state.hasFile(), return);
497
    p4Diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
498 499 500 501
}

void PerforcePlugin::diffCurrentProject()
{
hjk's avatar
hjk committed
502
    const VcsBase::VcsBasePluginState state = currentState();
503
    QTC_ASSERT(state.hasProject(), return);
504
    p4Diff(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
con's avatar
con committed
505 506 507 508
}

void PerforcePlugin::diffAllOpened()
{
509
    p4Diff(m_settings.topLevel(), QStringList());
con's avatar
con committed
510 511
}

512
void PerforcePlugin::updateCurrentProject()
513
{
hjk's avatar
hjk committed
514
    const VcsBase::VcsBasePluginState state = currentState();
515
    QTC_ASSERT(state.hasProject(), return);
516
    updateCheckout(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
517 518
}

519 520 521 522 523 524
void PerforcePlugin::updateAll()
{
    updateCheckout(m_settings.topLevel());
}

void PerforcePlugin::revertCurrentProject()
525
{
hjk's avatar
hjk committed
526
    const VcsBase::VcsBasePluginState state = currentState();
527
    QTC_ASSERT(state.hasProject(), return);
528 529 530

    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)
531
        return;
532
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
533 534
}

535
void PerforcePlugin::revertUnchangedCurrentProject()
536
{
537
    // revert -a.
hjk's avatar
hjk committed
538
    const VcsBase::VcsBasePluginState state = currentState();
539
    QTC_ASSERT(state.hasProject(), return);
540
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true);
541 542
}

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

con's avatar
con committed
570 571
void PerforcePlugin::printOpenedFileList()
{
572 573 574 575 576 577 578
    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
579
    VcsBase::VcsBaseOutputWindow *outWin = VcsBase::VcsBaseOutputWindow::instance();
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    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
595 596
}

597
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
598
{
599

hjk's avatar
hjk committed
600
    if (VcsBase::VcsBaseSubmitEditor::raiseSubmitEditor())
con's avatar
con committed
601 602
        return;

603
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
604
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
605 606 607
        return;
    }

hjk's avatar
hjk committed
608
    const VcsBase::VcsBasePluginState state = currentState();
609
    QTC_ASSERT(state.hasProject(), return);
610 611 612 613 614 615

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

617 618 619
    args << QLatin1String("change") << QLatin1String("-o");
    PerforceResponse result = runP4Cmd(state.currentProjectTopLevel(), args,
                                       RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
620
    if (result.error) {
621
        cleanCommitMessageFile();
con's avatar
con committed
622 623 624
        return;
    }

625 626 627 628
    Utils::TempFileSaver saver;
    saver.setAutoRemove(false);
    saver.write(result.stdOut.toAscii());
    if (!saver.finalize()) {
hjk's avatar
hjk committed
629
        VcsBase::VcsBaseOutputWindow::instance()->appendError(saver.errorString());
630 631 632 633
        cleanCommitMessageFile();
        return;
    }
    m_commitMessageFileName = saver.fileName();
con's avatar
con committed
634

635 636 637 638 639 640
    args.clear();
    args << QLatin1String("fstat");
    args.append(perforceRelativeProjectDirectory(state));
    PerforceResponse fstatResult = runP4Cmd(state.currentProjectTopLevel(), args,
                                            RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
    if (fstatResult.error) {
641
        cleanCommitMessageFile();
con's avatar
con committed
642 643 644
        return;
    }

645
    QStringList fstatLines = fstatResult.stdOut.split(QLatin1Char('\n'));
con's avatar
con committed
646
    QStringList depotFileNames;
647
    foreach (const QString &line, fstatLines) {
648
        if (line.startsWith(QLatin1String("... depotFile")))
con's avatar
con committed
649 650 651
            depotFileNames.append(line.mid(14));
    }
    if (depotFileNames.isEmpty()) {
hjk's avatar
hjk committed
652
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Project has no files"));
653
        cleanCommitMessageFile();
con's avatar
con committed
654 655 656
        return;
    }

657
    openPerforceSubmitEditor(m_commitMessageFileName, depotFileNames);
con's avatar
con committed
658 659 660 661
}

Core::IEditor *PerforcePlugin::openPerforceSubmitEditor(const QString &fileName, const QStringList &depotFileNames)
{
hjk's avatar
hjk committed
662
    Core::IEditor *editor = Core::EditorManager::openEditor(fileName, Constants::PERFORCE_SUBMIT_EDITOR_ID,
663
                                                      Core::EditorManager::ModeSwitch);
dt's avatar
dt committed
664
    PerforceSubmitEditor *submitEditor = static_cast<PerforceSubmitEditor*>(editor);
con's avatar
con committed
665
    submitEditor->restrictToProjectFiles(depotFileNames);
666
    submitEditor->registerActions(m_undoAction, m_redoAction, m_submitCurrentLogAction, m_diffSelectedFiles);
667
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotSubmitDiff(QStringList)));
668
    submitEditor->setCheckScriptWorkingDirectory(m_commitWorkingDirectory);
con's avatar
con committed
669 670 671 672 673 674
    return editor;
}

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

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
695
    const VcsBase::VcsBasePluginState state = currentState();
696
    QTC_ASSERT(state.hasFile(), return);
697
    annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
698 699 700 701
}

void PerforcePlugin::annotate()
{
702 703 704 705 706
    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
707 708
}

709
void PerforcePlugin::vcsAnnotate(const QString &file, const QString &revision, int lineNumber)
710 711 712 713 714 715 716 717 718
{
    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
719
{
720
    const QStringList files = QStringList(fileName);
hjk's avatar
hjk committed
721 722 723
    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
724
    QStringList args;
725 726 727 728 729 730
    args << QLatin1String("annotate") << QLatin1String("-cqi");
    if (changeList.isEmpty()) {
        args << fileName;
    } else {
        args << (fileName + QLatin1Char('@') + changeList);
    }
731 732 733
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
con's avatar
con committed
734
    if (!result.error) {
735
        if (lineNumber < 1)
hjk's avatar
hjk committed
736
            lineNumber = VcsBase::VcsBaseEditorWidget::lineNumberOfCurrentEditor();
con's avatar
con committed
737
        const QFileInfo fi(fileName);
738
        Core::IEditor *ed = showOutputInEditor(tr("p4 annotate %1").arg(id),
hjk's avatar
hjk committed
739
                                               result.stdOut, VcsBase::AnnotateOutput,
740
                                               source, codec);
hjk's avatar
hjk committed
741
        VcsBase::VcsBaseEditorWidget::gotoLineOfEditor(ed, lineNumber);
con's avatar
con committed
742 743 744 745 746
    }
}

void PerforcePlugin::filelogCurrentFile()
{
hjk's avatar
hjk committed
747
    const VcsBase::VcsBasePluginState state = currentState();
748
    QTC_ASSERT(state.hasFile(), return);
749
    filelog(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()), true);