perforceplugin.cpp 64.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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>
Friedemann Kleint's avatar
Friedemann Kleint committed
51
#include <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 63 64 65 66 67
#include <QtPlugin>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QSettings>
#include <QTextCodec>
hjk's avatar
hjk committed
68

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

hjk's avatar
hjk committed
75
static const VcsBase::VcsBaseEditorParameters editorParameters[] = {
con's avatar
con committed
76
{
Orgad Shaneh's avatar
Orgad Shaneh committed
77
    VcsBase::LogOutput,
78
    Perforce::Constants::PERFORCE_LOG_EDITOR_ID,
79
    Perforce::Constants::PERFORCE_LOG_EDITOR_DISPLAY_NAME,
80
    Perforce::Constants::PERFORCE_LOG_EDITOR_CONTEXT,
Orgad Shaneh's avatar
Orgad Shaneh committed
81
    "text/vnd.qtcreator.p4.log"},
hjk's avatar
hjk committed
82
{    VcsBase::AnnotateOutput,
83
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_ID,
84
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_DISPLAY_NAME,
85
     Perforce::Constants::PERFORCE_ANNOTATION_EDITOR_CONTEXT,
Orgad Shaneh's avatar
Orgad Shaneh committed
86
    "text/vnd.qtcreator.p4.annotation"},
hjk's avatar
hjk committed
87
{   VcsBase::DiffOutput,
88
    Perforce::Constants::PERFORCE_DIFF_EDITOR_ID,
89
    Perforce::Constants::PERFORCE_DIFF_EDITOR_DISPLAY_NAME,
90
    Perforce::Constants::PERFORCE_DIFF_EDITOR_CONTEXT,
91
    "text/x-patch"}
con's avatar
con committed
92 93 94
};

// Utility to find a parameter set by type
hjk's avatar
hjk committed
95
static inline const VcsBase::VcsBaseEditorParameters *findType(int ie)
con's avatar
con committed
96
{
hjk's avatar
hjk committed
97 98
    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
99 100 101 102
}

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

106 107 108 109 110 111
// 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("..."));
112
    QTC_ASSERT(args.size() == 1, return QStringList());
113 114 115 116 117
    QStringList p4Args = args;
    p4Args.front() += QLatin1String("/...");
    return p4Args;
}

hjk's avatar
hjk committed
118
static inline QStringList perforceRelativeProjectDirectory(const VcsBase::VcsBasePluginState &s)
119 120 121 122 123 124 125 126 127 128
{
    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;
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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
154 155 156 157 158

////
// PerforcePlugin
////

159 160 161 162 163 164 165 166 167
namespace Perforce {
namespace Internal {

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

con's avatar
con committed
168 169 170
PerforcePlugin *PerforcePlugin::m_perforcePluginInstance = NULL;

PerforcePlugin::PerforcePlugin() :
Friedemann Kleint's avatar
Friedemann Kleint committed
171
    m_commandLocator(0),
con's avatar
con committed
172 173 174 175
    m_editAction(0),
    m_addAction(0),
    m_deleteAction(0),
    m_openedAction(0),
176 177
    m_revertFileAction(0),
    m_diffFileAction(0),
con's avatar
con committed
178
    m_diffProjectAction(0),
179
    m_updateProjectAction(0),
180 181
    m_revertProjectAction(0),
    m_revertUnchangedAction(0),
con's avatar
con committed
182
    m_diffAllAction(0),
183
    m_submitProjectAction(0),
con's avatar
con committed
184 185 186 187 188 189
    m_pendingAction(0),
    m_describeAction(0),
    m_annotateCurrentAction(0),
    m_annotateAction(0),
    m_filelogCurrentAction(0),
    m_filelogAction(0),
190 191
    m_logProjectAction(0),
    m_logRepositoryAction(0),
192
    m_submitCurrentLogAction(0),
193
    m_updateAllAction(0),
194 195 196
    m_submitActionTriggered(false),
    m_diffSelectedFiles(0),
    m_undoAction(0),
197
    m_redoAction(0)
con's avatar
con committed
198 199 200
{
}

hjk's avatar
hjk committed
201
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
202
    Perforce::Constants::SUBMIT_MIMETYPE,
203
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_ID,
204
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME,
205 206
    Perforce::Constants::PERFORCESUBMITEDITOR_CONTEXT,
    VcsBase::VcsBaseSubmitEditorParameters::DiffFiles
con's avatar
con committed
207 208
};

209
bool PerforcePlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
con's avatar
con committed
210
{
hjk's avatar
hjk committed
211 212
    typedef VcsBase::VcsEditorFactory<PerforceEditor> PerforceEditorFactory;
    typedef VcsBase::VcsSubmitEditorFactory<PerforceSubmitEditor> PerforceSubmitEditorFactory;
con's avatar
con committed
213

214
    initializeVcs(new PerforceVersionControl(this));
215

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

220
    m_settings.fromSettings(Core::ICore::settings());
con's avatar
con committed
221

222
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
223 224

    // Editor factories
225
    addAutoReleasedObject(new PerforceSubmitEditorFactory(&submitParameters));
con's avatar
con committed
226 227

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

Friedemann Kleint's avatar
Friedemann Kleint committed
232
    const QString prefix = QLatin1String("p4");
233
    m_commandLocator = new Locator::CommandLocator("Perforce", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
234 235
    addAutoReleasedObject(m_commandLocator);

236
    Core::ActionContainer *mtools =
Eike Ziller's avatar
Eike Ziller committed
237
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
238

239
    Core::ActionContainer *mperforce =
Eike Ziller's avatar
Eike Ziller committed
240
        Core::ActionManager::createMenu(Core::Id(CMD_ID_PERFORCE_MENU));
con's avatar
con committed
241 242
    mperforce->menu()->setTitle(tr("&Perforce"));
    mtools->addMenu(mperforce);
243
    m_menuAction = mperforce->menu()->menuAction();
con's avatar
con committed
244

245
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
246
    Core::Context perforcesubmitcontext(Constants::PERFORCESUBMITEDITOR_CONTEXT);
con's avatar
con committed
247

con's avatar
con committed
248
    Core::Command *command;
249 250

    m_diffFileAction = new Utils::ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
251
    command = Core::ActionManager::registerAction(m_diffFileAction, CMD_ID_DIFF_CURRENT, globalcontext);
252
    command->setAttribute(Core::Command::CA_UpdateText);
253
    command->setDescription(tr("Diff Current File"));
254 255
    connect(m_diffFileAction, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
256
    m_commandLocator->appendCommand(command);
257 258

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

    m_filelogCurrentAction = new Utils::ParameterAction(tr("Filelog Current File"), tr("Filelog \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
267
    command = Core::ActionManager::registerAction(m_filelogCurrentAction, CMD_ID_FILELOG_CURRENT, globalcontext);
268
    command->setAttribute(Core::Command::CA_UpdateText);
269
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+F") : tr("Alt+P,Alt+F")));
270
    command->setDescription(tr("Filelog Current File"));
271 272
    connect(m_filelogCurrentAction, SIGNAL(triggered()), this, SLOT(filelogCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
273
    m_commandLocator->appendCommand(command);
274

275
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
276

277
    m_editAction = new Utils::ParameterAction(tr("Edit"), tr("Edit \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
278
    command = Core::ActionManager::registerAction(m_editAction, CMD_ID_EDIT, globalcontext);
con's avatar
con committed
279
    command->setAttribute(Core::Command::CA_UpdateText);
280
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+E") : tr("Alt+P,Alt+E")));
281
    command->setDescription(tr("Edit File"));
con's avatar
con committed
282 283
    connect(m_editAction, SIGNAL(triggered()), this, SLOT(openCurrentFile()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
284
    m_commandLocator->appendCommand(command);
con's avatar
con committed
285

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

295
    m_deleteAction = new Utils::ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
296
    command = Core::ActionManager::registerAction(m_deleteAction, CMD_ID_DELETE_FILE, globalcontext);
con's avatar
con committed
297
    command->setAttribute(Core::Command::CA_UpdateText);
298
    command->setDescription(tr("Delete File"));
299
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
con's avatar
con committed
300
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
301
    m_commandLocator->appendCommand(command);
con's avatar
con committed
302

303
    m_revertFileAction = new Utils::ParameterAction(tr("Revert"), tr("Revert \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
304
    command = Core::ActionManager::registerAction(m_revertFileAction, CMD_ID_REVERT, globalcontext);
con's avatar
con committed
305
    command->setAttribute(Core::Command::CA_UpdateText);
306
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+R") : tr("Alt+P,Alt+R")));
307
    command->setDescription(tr("Revert File"));
308
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
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
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
313

314
    const QString diffProjectDefaultText = tr("Diff Current Project/Session");
315
    m_diffProjectAction = new Utils::ParameterAction(diffProjectDefaultText, tr("Diff Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
Eike Ziller's avatar
Eike Ziller committed
316
    command = Core::ActionManager::registerAction(m_diffProjectAction, CMD_ID_DIFF_PROJECT, globalcontext);
con's avatar
con committed
317
    command->setAttribute(Core::Command::CA_UpdateText);
318
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+D") : tr("Alt+P,Alt+D")));
319
    command->setDescription(diffProjectDefaultText);
con's avatar
con committed
320 321
    connect(m_diffProjectAction, SIGNAL(triggered()), this, SLOT(diffCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
322
    m_commandLocator->appendCommand(command);
con's avatar
con committed
323

Friedemann Kleint's avatar
Friedemann Kleint committed
324
    m_logProjectAction = new Utils::ParameterAction(tr("Log Project"), tr("Log Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
325
    command = Core::ActionManager::registerAction(m_logProjectAction, CMD_ID_PROJECTLOG, globalcontext);
326 327 328
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_logProjectAction, SIGNAL(triggered()), this, SLOT(logProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
329
    m_commandLocator->appendCommand(command);
330

331
    m_submitProjectAction = new Utils::ParameterAction(tr("Submit Project"), tr("Submit Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
332
    command = Core::ActionManager::registerAction(m_submitProjectAction, CMD_ID_SUBMIT, globalcontext);
333
    command->setAttribute(Core::Command::CA_UpdateText);
334
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+S") : tr("Alt+P,Alt+S")));
335
    connect(m_submitProjectAction, SIGNAL(triggered()), this, SLOT(startSubmitProject()));
con's avatar
con committed
336
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
337
    m_commandLocator->appendCommand(command);
con's avatar
con committed
338

339
    const QString updateProjectDefaultText = tr("Update Current Project");
340
    m_updateProjectAction = new Utils::ParameterAction(updateProjectDefaultText, tr("Update Project \"%1\""), Utils::ParameterAction::AlwaysEnabled, this);
Eike Ziller's avatar
Eike Ziller committed
341
    command = Core::ActionManager::registerAction(m_updateProjectAction, CMD_ID_UPDATE_PROJECT, globalcontext);
342
    command->setDescription(updateProjectDefaultText);
343 344 345
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_updateProjectAction, SIGNAL(triggered()), this, SLOT(updateCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    m_commandLocator->appendCommand(command);
347

348
    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
349
    command = Core::ActionManager::registerAction(m_revertUnchangedAction, CMD_ID_REVERT_UNCHANGED_PROJECT, globalcontext);
350 351 352
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertUnchangedAction, SIGNAL(triggered()), this, SLOT(revertUnchangedCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
353
    m_commandLocator->appendCommand(command);
354

355
    m_revertProjectAction = new Utils::ParameterAction(tr("Revert Project"), tr("Revert Project \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
356
    command = Core::ActionManager::registerAction(m_revertProjectAction, CMD_ID_REVERT_PROJECT, globalcontext);
357 358 359
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_revertProjectAction, SIGNAL(triggered()), this, SLOT(revertCurrentProject()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
360
    m_commandLocator->appendCommand(command);
361

362
    mperforce->addSeparator(globalcontext);
363 364

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

370
    m_openedAction = new QAction(tr("Opened"), this);
Eike Ziller's avatar
Eike Ziller committed
371
    command = Core::ActionManager::registerAction(m_openedAction, CMD_ID_OPENED, globalcontext);
372
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+O") : tr("Alt+P,Alt+O")));
373 374
    connect(m_openedAction, SIGNAL(triggered()), this, SLOT(printOpenedFileList()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
375
    m_commandLocator->appendCommand(command);
376 377

    m_logRepositoryAction = new QAction(tr("Repository Log"), this);
Eike Ziller's avatar
Eike Ziller committed
378
    command = Core::ActionManager::registerAction(m_logRepositoryAction, CMD_ID_REPOSITORYLOG, globalcontext);
379 380
    connect(m_logRepositoryAction, SIGNAL(triggered()), this, SLOT(logRepository()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
381
    m_commandLocator->appendCommand(command);
382 383

    m_pendingAction = new QAction(tr("Pending Changes..."), this);
Eike Ziller's avatar
Eike Ziller committed
384
    command = Core::ActionManager::registerAction(m_pendingAction, CMD_ID_PENDING_CHANGES, globalcontext);
385 386
    connect(m_pendingAction, SIGNAL(triggered()), this, SLOT(printPendingChanges()));
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
387
    m_commandLocator->appendCommand(command);
388 389

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

395
    mperforce->addSeparator(globalcontext);
396

con's avatar
con committed
397
    m_describeAction = new QAction(tr("Describe..."), this);
Eike Ziller's avatar
Eike Ziller committed
398
    command = Core::ActionManager::registerAction(m_describeAction, CMD_ID_DESCRIBE, globalcontext);
con's avatar
con committed
399 400 401 402
    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
403
    command = Core::ActionManager::registerAction(m_annotateAction, CMD_ID_ANNOTATE, globalcontext);
con's avatar
con committed
404 405 406 407
    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
408
    command = Core::ActionManager::registerAction(m_filelogAction, CMD_ID_FILELOG, globalcontext);
con's avatar
con committed
409 410 411
    connect(m_filelogAction, SIGNAL(triggered()), this, SLOT(filelog()));
    mperforce->addAction(command);

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

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

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

    m_redoAction = new QAction(tr("&Redo"), this);
Eike Ziller's avatar
Eike Ziller committed
424
    command = Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, perforcesubmitcontext);
con's avatar
con committed
425 426 427 428 429 430

    return true;
}

void PerforcePlugin::extensionsInitialized()
{
hjk's avatar
hjk committed
431
    VcsBase::VcsBasePlugin::extensionsInitialized();
432
    getTopLevel();
con's avatar
con committed
433 434 435 436
}

void PerforcePlugin::openCurrentFile()
{
hjk's avatar
hjk committed
437
    const VcsBase::VcsBasePluginState state = currentState();
438
    QTC_ASSERT(state.hasFile(), return);
439
    vcsOpen(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
440 441 442 443
}

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

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

hjk's avatar
hjk committed
454
    QTextCodec *codec = VcsBase::VcsBaseEditorWidget::getCodec(state.currentFile());
con's avatar
con committed
455
    QStringList args;
456 457 458 459
    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
460 461
    if (result.error)
        return;
462
    // "foo.cpp - file(s) not opened on this client."
463 464
    // also revert when the output is empty: The file is unchanged but open then.
    if (result.stdOut.contains(QLatin1String(" - ")) || result.stdErr.contains(QLatin1String(" - ")))
465
        return;
con's avatar
con committed
466

467 468 469 470 471
    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);
472 473
    if (doNotRevert)
        return;
con's avatar
con committed
474

475 476 477 478 479
    Core::FileChangeBlocker fcb(state.currentFile());
    args.clear();
    args << QLatin1String("revert") << state.relativeCurrentFile();
    PerforceResponse result2 = runP4Cmd(state.currentFileTopLevel(), args,
                                        CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
480
    if (!result2.error)
481
        perforceVersionControl()->emitFilesChanged(QStringList(state.currentFile()));
con's avatar
con committed
482 483 484 485
}

void PerforcePlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
486
    const VcsBase::VcsBasePluginState state = currentState();
487
    QTC_ASSERT(state.hasFile(), return);
488
    p4Diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
con's avatar
con committed
489 490 491 492
}

void PerforcePlugin::diffCurrentProject()
{
hjk's avatar
hjk committed
493
    const VcsBase::VcsBasePluginState state = currentState();
494
    QTC_ASSERT(state.hasProject(), return);
495
    p4Diff(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
con's avatar
con committed
496 497 498 499
}

void PerforcePlugin::diffAllOpened()
{
500
    p4Diff(m_settings.topLevel(), QStringList());
con's avatar
con committed
501 502
}

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

510 511 512 513 514 515
void PerforcePlugin::updateAll()
{
    updateCheckout(m_settings.topLevel());
}

void PerforcePlugin::revertCurrentProject()
516
{
hjk's avatar
hjk committed
517
    const VcsBase::VcsBasePluginState state = currentState();
518
    QTC_ASSERT(state.hasProject(), return);
519 520 521

    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)
522
        return;
523
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
524 525
}

526
void PerforcePlugin::revertUnchangedCurrentProject()
527
{
528
    // revert -a.
hjk's avatar
hjk committed
529
    const VcsBase::VcsBasePluginState state = currentState();
530
    QTC_ASSERT(state.hasProject(), return);
531
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), true);
532 533
}

534 535 536 537 538 539 540 541 542 543 544 545
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)
546 547 548
{
    QStringList args(QLatin1String("sync"));
    args.append(dirs);
549 550 551 552 553 554 555
    const PerforceResponse resp = runP4Cmd(workingDir, args,
                                           CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
    if (dirs.empty()) {
        if (!workingDir.isEmpty())
            perforceVersionControl()->emitRepositoryChanged(workingDir);
    } else {
        const QChar slash = QLatin1Char('/');
556
        foreach (const QString &dir, dirs)
557 558
            perforceVersionControl()->emitRepositoryChanged(workingDir + slash + dir);
    }
559 560
}

con's avatar
con committed
561 562
void PerforcePlugin::printOpenedFileList()
{
563 564 565 566 567 568 569
    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
570
    VcsBase::VcsBaseOutputWindow *outWin = VcsBase::VcsBaseOutputWindow::instance();
571 572 573 574 575 576 577 578
    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);
579
        if (mapped.isEmpty())
580
            outWin->appendSilently(line);
581
        else
582 583
            outWin->appendSilently(mapped + QLatin1Char(' ') + line.mid(delimiterPos));
    }
584
    outWin->popup(Core::IOutputPane::ModeSwitch | Core::IOutputPane::WithFocus);
con's avatar
con committed
585 586
}

587
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
588
{
589

590
    if (raiseSubmitEditor())
con's avatar
con committed
591 592
        return;

593
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
594
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
595 596 597
        return;
    }

hjk's avatar
hjk committed
598
    const VcsBase::VcsBasePluginState state = currentState();
599
    QTC_ASSERT(state.hasProject(), return);
600 601 602 603 604 605

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

607 608 609
    args << QLatin1String("change") << QLatin1String("-o");
    PerforceResponse result = runP4Cmd(state.currentProjectTopLevel(), args,
                                       RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
610
    if (result.error) {
611
        cleanCommitMessageFile();
con's avatar
con committed
612 613 614
        return;
    }

615 616
    Utils::TempFileSaver saver;
    saver.setAutoRemove(false);
617
    saver.write(result.stdOut.toLatin1());
618
    if (!saver.finalize()) {
hjk's avatar
hjk committed
619
        VcsBase::VcsBaseOutputWindow::instance()->appendError(saver.errorString());
620 621 622 623
        cleanCommitMessageFile();
        return;
    }
    m_commitMessageFileName = saver.fileName();
con's avatar
con committed
624

625 626 627 628 629 630
    args.clear();
    args << QLatin1String("fstat");
    args.append(perforceRelativeProjectDirectory(state));
    PerforceResponse fstatResult = runP4Cmd(state.currentProjectTopLevel(), args,
                                            RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow);
    if (fstatResult.error) {
631
        cleanCommitMessageFile();
con's avatar
con committed
632 633 634
        return;
    }

635
    QStringList fstatLines = fstatResult.stdOut.split(QLatin1Char('\n'));
con's avatar
con committed
636
    QStringList depotFileNames;
637
    foreach (const QString &line, fstatLines) {
638
        if (line.startsWith(QLatin1String("... depotFile")))
con's avatar
con committed
639 640 641
            depotFileNames.append(line.mid(14));
    }
    if (depotFileNames.isEmpty()) {
hjk's avatar
hjk committed
642
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Project has no files"));
643
        cleanCommitMessageFile();
con's avatar
con committed
644 645 646
        return;
    }

647
    openPerforceSubmitEditor(m_commitMessageFileName, depotFileNames);
con's avatar
con committed
648 649 650 651
}

Core::IEditor *PerforcePlugin::openPerforceSubmitEditor(const QString &fileName, const QStringList &depotFileNames)
{
hjk's avatar
hjk committed
652
    Core::IEditor *editor = Core::EditorManager::openEditor(fileName, Constants::PERFORCE_SUBMIT_EDITOR_ID,
653
                                                      Core::EditorManager::ModeSwitch);
dt's avatar
dt committed
654
    PerforceSubmitEditor *submitEditor = static_cast<PerforceSubmitEditor*>(editor);
655
    setSubmitEditor(submitEditor);
con's avatar
con committed
656
    submitEditor->restrictToProjectFiles(depotFileNames);
657
    submitEditor->registerActions(m_undoAction, m_redoAction, m_submitCurrentLogAction, m_diffSelectedFiles);
658
    connect(submitEditor, SIGNAL(diffSelectedFiles(QStringList)), this, SLOT(slotSubmitDiff(QStringList)));
659
    submitEditor->setCheckScriptWorkingDirectory(m_commitWorkingDirectory);
con's avatar
con committed
660 661 662 663 664 665
    return editor;
}

void PerforcePlugin::printPendingChanges()
{
    qApp->setOverrideCursor(Qt::WaitCursor);
hjk's avatar
hjk committed
666
    PendingChangesDialog dia(pendingChangesData(), Core::ICore::mainWindow());
con's avatar
con committed
667 668
    qApp->restoreOverrideCursor();
    if (dia.exec() == QDialog::Accepted) {
669 670 671
        const int i = dia.changeNumber();
        QStringList args(QLatin1String("submit"));
        args << QLatin1String("-c") << QString::number(i);
672 673
        runP4Cmd(m_settings.topLevel(), args,
                 CommandToWindow|StdOutToWindow|StdErrToWindow|ErrorToWindow);
con's avatar
con committed
674 675 676 677 678 679 680 681 682 683 684 685
    }
}

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
686
    const VcsBase::VcsBasePluginState state = currentState();
687
    QTC_ASSERT(state.hasFile(), return);
688
    annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
689 690 691 692
}

void PerforcePlugin::annotate()
{
693 694 695 696 697
    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
698 699
}

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

void