perforceplugin.cpp 63.8 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
// Ensure adding "..." to relative paths which is p4's convention
// for the current directory
108
static inline QString perforceRelativeFileArguments(const QString &args)
109
110
{
    if (args.isEmpty())
111
112
        return QLatin1String("...");
    return args + QLatin1String("/...");
113
114
}

hjk's avatar
hjk committed
115
static inline QStringList perforceRelativeProjectDirectory(const VcsBase::VcsBasePluginState &s)
116
{
117
    return QStringList(perforceRelativeFileArguments(s.relativeCurrentProject()));
118
119
120
121
122
123
124
125
}

// 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;
126
127
}

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

////
// PerforcePlugin
////

155
156
157
158
159
160
161
162
163
namespace Perforce {
namespace Internal {

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

Tobias Hunger's avatar
Tobias Hunger committed
164
PerforcePlugin *PerforcePlugin::m_instance = NULL;
con's avatar
con committed
165
166

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

hjk's avatar
hjk committed
197
static const VcsBase::VcsBaseSubmitEditorParameters submitParameters = {
con's avatar
con committed
198
    Perforce::Constants::SUBMIT_MIMETYPE,
199
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_ID,
200
    Perforce::Constants::PERFORCE_SUBMIT_EDITOR_DISPLAY_NAME,
201
202
    Perforce::Constants::PERFORCESUBMITEDITOR_CONTEXT,
    VcsBase::VcsBaseSubmitEditorParameters::DiffFiles
con's avatar
con committed
203
204
};

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

210
    initializeVcs(new PerforceVersionControl(this));
211

hjk's avatar
hjk committed
212
    if (!Core::MimeDatabase::addMimeTypes(QLatin1String(":/trolltech.perforce/Perforce.mimetypes.xml"), errorMessage))
con's avatar
con committed
213
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
214
    m_instance = this;
con's avatar
con committed
215

216
    m_settings.fromSettings(Core::ICore::settings());
con's avatar
con committed
217

218
    addAutoReleasedObject(new SettingsPage);
con's avatar
con committed
219
220

    // Editor factories
221
    addAutoReleasedObject(new PerforceSubmitEditorFactory(&submitParameters));
con's avatar
con committed
222
223

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

Friedemann Kleint's avatar
Friedemann Kleint committed
228
    const QString prefix = QLatin1String("p4");
229
    m_commandLocator = new Locator::CommandLocator("Perforce", prefix, prefix);
Friedemann Kleint's avatar
Friedemann Kleint committed
230
231
    addAutoReleasedObject(m_commandLocator);

232
    Core::ActionContainer *mtools =
Eike Ziller's avatar
Eike Ziller committed
233
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
234

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

241
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
242
    Core::Context perforcesubmitcontext(Constants::PERFORCESUBMITEDITOR_CONTEXT);
con's avatar
con committed
243

con's avatar
con committed
244
    Core::Command *command;
245
246

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

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

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

271
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
272

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

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

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

299
    m_revertFileAction = new Utils::ParameterAction(tr("Revert"), tr("Revert \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
Eike Ziller's avatar
Eike Ziller committed
300
    command = Core::ActionManager::registerAction(m_revertFileAction, CMD_ID_REVERT, globalcontext);
con's avatar
con committed
301
    command->setAttribute(Core::Command::CA_UpdateText);
302
    command->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+P,Meta+R") : tr("Alt+P,Alt+R")));
303
    command->setDescription(tr("Revert File"));
304
    connect(m_revertFileAction, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
con's avatar
con committed
305
    mperforce->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
306
    m_commandLocator->appendCommand(command);
con's avatar
con committed
307

308
    mperforce->addSeparator(globalcontext);
con's avatar
con committed
309

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

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

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

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

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

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

358
    mperforce->addSeparator(globalcontext);
359
360

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

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

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

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

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

391
    mperforce->addSeparator(globalcontext);
392

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

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

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

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

    m_redoAction = new QAction(tr("&Redo"), this);
Eike Ziller's avatar
Eike Ziller committed
420
    command = Core::ActionManager::registerAction(m_redoAction, Core::Constants::REDO, perforcesubmitcontext);
con's avatar
con committed
421
422
423
424
425
426

    return true;
}

void PerforcePlugin::extensionsInitialized()
{
hjk's avatar
hjk committed
427
    VcsBase::VcsBasePlugin::extensionsInitialized();
428
    getTopLevel();
con's avatar
con committed
429
430
431
432
}

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

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

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

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

463
464
465
466
467
    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);
468
469
    if (doNotRevert)
        return;
con's avatar
con committed
470

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

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

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

void PerforcePlugin::diffAllOpened()
{
496
    p4Diff(m_settings.topLevel(), QStringList());
con's avatar
con committed
497
498
}

499
void PerforcePlugin::updateCurrentProject()
500
{
hjk's avatar
hjk committed
501
    const VcsBase::VcsBasePluginState state = currentState();
502
    QTC_ASSERT(state.hasProject(), return);
503
    updateCheckout(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state));
504
505
}

506
507
508
509
510
511
void PerforcePlugin::updateAll()
{
    updateCheckout(m_settings.topLevel());
}

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

    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)
518
        return;
519
    revertProject(state.currentProjectTopLevel(), perforceRelativeProjectDirectory(state), false);
520
521
}

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

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

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

583
void PerforcePlugin::startSubmitProject()
con's avatar
con committed
584
{
585

586
    if (raiseSubmitEditor())
con's avatar
con committed
587
588
        return;

589
    if (isCommitEditorOpen()) {
hjk's avatar
hjk committed
590
        VcsBase::VcsBaseOutputWindow::instance()->appendWarning(tr("Another submit is currently executed."));
con's avatar
con committed
591
592
593
        return;
    }

hjk's avatar
hjk committed
594
    const VcsBase::VcsBasePluginState state = currentState();
595
    QTC_ASSERT(state.hasProject(), return);
596
597
598
599
600
601

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

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

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

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

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

643
    openPerforceSubmitEditor(m_commitMessageFileName, depotFileNames);
con's avatar
con committed
644
645
646
647
}

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

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

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
681
    const VcsBase::VcsBasePluginState state = currentState();
682
    QTC_ASSERT(state.hasFile(), return);
683
    annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
con's avatar
con committed
684
685
686
687
}

void PerforcePlugin::annotate()
{
688
689
690
691
692
    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
693
694
}

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

void PerforcePlugin::filelogCurrentFile()
{
hjk's avatar
hjk committed
732
    const VcsBase::VcsBasePluginState state = currentState();
733
    QTC_ASSERT(state.hasFile(), return);
734
    filelog(state.currentFileTopLevel(), state.relativeCurrentFile(), true);
con's avatar
con committed
735
736
737
738
}

void PerforcePlugin::filelog()
{
739
740
741
    const QString file = QFileDialog::getOpenFileName(0, tr("p4 filelog"));
    if (!file.isEmpty()) {
        const QFileInfo fi(file);
742
        filelog(fi.absolutePath(), fi.fileName());
743
    }
con's avatar
con committed
744
745
}

746
747
void PerforcePlugin::logProject()
{
hjk's avatar
hjk committed
748
    const VcsBase::VcsBasePluginState state = currentState();
749
    QTC_ASSERT(state.hasProject(), return);
750
751
752
753
754
    filelog(state.currentProjectTopLevel(), perforceRelativeFileArguments(state.relativeCurrentProject()));
}

void PerforcePlugin::logRepository()
{
hjk's avatar
hjk committed
755
    const VcsBase::VcsBasePluginState state = currentState();
756
    QTC_ASSERT(state.hasTopLevel(), return);
757
    filelog(state.topLevel(), perforceRelativeFileArguments(QString()));
758
759
}

760
void PerforcePlugin::filelog(const QString &workingDir, const QString &fileName,
761
                             bool enableAnnotationContextMenu)
con's avatar
con committed
762
{
763
764
    const QString id = VcsBase::VcsBaseEditorWidget::getTitleId(workingDir, QStringList(fileName));
    QTextCodec *codec = VcsBase::VcsBaseEditorWidget::getCodec(workingDir, QStringList(fileName));
con's avatar
con committed
765
    QStringList args;
766
    args << QLatin1String("filelog") << QLatin1String("-li");
767
768
    if (m_settings.logCount() > 0)
        args << QLatin1String("-m") << QString::number(m_settings.logCount());
769
770
    if (!fileName.isEmpty())
        args.append(fileName);
771
772
773
    const PerforceResponse result = runP4Cmd(workingDir, args,
                                             CommandToWindow|StdErrToWindow|ErrorToWindow,
                                             QStringList(), QByteArray(), codec);
774
    if (!result.error) {
775
        const QString source = VcsBase::VcsBaseEditorWidget::getSource(workingDir, fileName);
776
        Core::IEditor *editor = showOutputInEditor(tr("p4 filelog %1").arg(id), result.stdOut,
hjk's avatar
hjk committed
777
                                VcsBase::LogOutput, source, codec);
778
        if (enableAnnotationContextMenu)
hjk's avatar
hjk committed
779
            VcsBase::VcsBaseEditorWidget::getVcsBaseEditor(editor)->setFileLogAnnotateEnabled(