mercurialplugin.cpp 26.7 KB
Newer Older
dt's avatar
dt committed
1 2 3 4 5 6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Brian McGillion
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
dt's avatar
dt committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
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.
dt's avatar
dt committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
dt's avatar
dt committed
30 31 32
**
**************************************************************************/

33 34 35 36 37 38 39 40 41 42 43 44 45
#include "mercurialplugin.h"
#include "optionspage.h"
#include "constants.h"
#include "mercurialclient.h"
#include "mercurialcontrol.h"
#include "mercurialeditor.h"
#include "revertdialog.h"
#include "srcdestdialog.h"
#include "commiteditor.h"
#include "clonewizard.h"
#include "mercurialsettings.h"

#include <coreplugin/actionmanager/actionmanager.h>
46 47
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
48
#include <coreplugin/id.h>
49
#include <coreplugin/vcsmanager.h>
50 51 52 53 54
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/editormanager/editormanager.h>

Friedemann Kleint's avatar
Friedemann Kleint committed
55 56
#include <locator/commandlocator.h>

57
#include <utils/parameteraction.h>
58
#include <utils/qtcassert.h>
59 60 61 62

#include <vcsbase/basevcseditorfactory.h>
#include <vcsbase/basevcssubmiteditorfactory.h>
#include <vcsbase/vcsbaseeditor.h>
63
#include <vcsbase/vcsbaseoutputwindow.h>
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#include <QtCore/QtPlugin>
#include <QtGui/QAction>
#include <QtGui/QMenu>
#include <QtGui/QMainWindow>
#include <QtCore/QtDebug>
#include <QtCore/QtGlobal>
#include <QtCore/QDir>
#include <QtGui/QDialog>
#include <QtGui/QFileDialog>
#include <QtCore/QTemporaryFile>


using namespace Mercurial::Internal;
using namespace Mercurial;

static const VCSBase::VCSBaseEditorParameters editorParameters[] = {
{
    VCSBase::RegularCommandOutput, //type
83 84
    Constants::COMMANDLOG_ID, // id
    Constants::COMMANDLOG_DISPLAY_NAME, // display name
85 86 87 88 89
    Constants::COMMANDLOG, // context
    Constants::COMMANDAPP, // mime type
    Constants::COMMANDEXT}, //extension

{   VCSBase::LogOutput,
90 91
    Constants::FILELOG_ID,
    Constants::FILELOG_DISPLAY_NAME,
92 93 94 95 96
    Constants::FILELOG,
    Constants::LOGAPP,
    Constants::LOGEXT},

{    VCSBase::AnnotateOutput,
97 98
     Constants::ANNOTATELOG_ID,
     Constants::ANNOTATELOG_DISPLAY_NAME,
99 100 101 102 103
     Constants::ANNOTATELOG,
     Constants::ANNOTATEAPP,
     Constants::ANNOTATEEXT},

{   VCSBase::DiffOutput,
104 105
    Constants::DIFFLOG_ID,
    Constants::DIFFLOG_DISPLAY_NAME,
106 107 108 109 110 111 112
    Constants::DIFFLOG,
    Constants::DIFFAPP,
    Constants::DIFFEXT}
};

static const VCSBase::VCSBaseSubmitEditorParameters submitEditorParameters = {
    Constants::COMMITMIMETYPE,
113 114 115
    Constants::COMMIT_ID,
    Constants::COMMIT_DISPLAY_NAME,
    Constants::COMMIT_ID
116 117 118 119 120 121
};

// Utility to find a parameter set by type
static inline const VCSBase::VCSBaseEditorParameters *findType(int ie)
{
    const VCSBase::EditorContentType et = static_cast<VCSBase::EditorContentType>(ie);
122
    return  VCSBase::VCSBaseEditorWidget::findType(editorParameters,
123 124 125 126 127
                                             sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters), et);
}

MercurialPlugin *MercurialPlugin::m_instance = 0;

128
MercurialPlugin::MercurialPlugin() :
129
        VCSBase::VCSBasePlugin(QLatin1String(Constants::COMMIT_ID)),
130
        optionsPage(0),
131
        m_client(0),
Friedemann Kleint's avatar
Friedemann Kleint committed
132 133
        core(0),
        m_commandLocator(0),
134
        changeLog(0),
135 136
        m_addAction(0),
        m_deleteAction(0),
137
        m_createRepositoryAction(0),
138
        m_menuAction(0)
139 140 141 142 143 144
{
    m_instance = this;
}

MercurialPlugin::~MercurialPlugin()
{
145 146 147
    if (m_client) {
        delete m_client;
        m_client = 0;
148 149 150 151 152 153 154
    }

    deleteCommitLog();

    m_instance = 0;
}

155
bool MercurialPlugin::initialize(const QStringList & /* arguments */, QString * /*error_message */)
156 157 158
{
    typedef VCSBase::VCSEditorFactory<MercurialEditor> MercurialEditorFactory;

159
    m_client = new MercurialClient(&mercurialSettings);
160
    initializeVcs(new MercurialControl(m_client));
161

162 163 164 165 166
    core = Core::ICore::instance();
    actionManager = core->actionManager();

    optionsPage = new OptionsPage();
    addAutoReleasedObject(optionsPage);
167
    mercurialSettings.readSettings(core->settings());
168

169
    connect(optionsPage, SIGNAL(settingsChanged()), m_client, SLOT(handleSettingsChanged()));
170

171
    connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant)));
172 173 174 175

    static const char *describeSlot = SLOT(view(QString,QString));
    const int editorCount = sizeof(editorParameters)/sizeof(VCSBase::VCSBaseEditorParameters);
    for (int i = 0; i < editorCount; i++)
176
        addAutoReleasedObject(new MercurialEditorFactory(editorParameters + i, m_client, describeSlot));
177 178 179 180 181

    addAutoReleasedObject(new VCSBase::VCSSubmitEditorFactory<CommitEditor>(&submitEditorParameters));

    addAutoReleasedObject(new CloneWizard);

Friedemann Kleint's avatar
Friedemann Kleint committed
182 183 184 185
    const QString prefix = QLatin1String("hg");
    m_commandLocator = new Locator::CommandLocator(QLatin1String("Mercurial"), prefix, prefix);
    addAutoReleasedObject(m_commandLocator);

186 187 188 189 190 191 192
    createMenu();

    createSubmitEditorActions();

    return true;
}

193
const MercurialSettings &MercurialPlugin::settings() const
194 195 196 197
{
    return mercurialSettings;
}

198 199 200 201
void MercurialPlugin::setSettings(const MercurialSettings &settings)
{
    if (settings != mercurialSettings) {
        mercurialSettings = settings;
202
        static_cast<MercurialControl *>(versionControl())->emitConfigurationChanged();
203 204 205
    }
}

206 207
QStringList MercurialPlugin::standardArguments() const
{
208
    return mercurialSettings.standardArguments();
209 210
}

211 212
void MercurialPlugin::createMenu()
{
213
    Core::Context context(Core::Constants::C_GLOBAL);
214 215

    // Create menu item for Mercurial
hjk's avatar
hjk committed
216
    mercurialContainer = actionManager->createMenu(Core::Id("Mercurial.MercurialMenu"));
217 218 219 220
    QMenu *menu = mercurialContainer->menu();
    menu->setTitle(tr("Mercurial"));

    createFileActions(context);
221
    createSeparator(context, Core::Id("Mercurial.FileDirSeperator"));
222
    createDirectoryActions(context);
223
    createSeparator(context, Core::Id("Mercurial.DirRepoSeperator"));
224
    createRepositoryActions(context);
225
    createSeparator(context, Core::Id("Mercurial.Repository Management"));
226
    createRepositoryManagementActions(context);
227
    createSeparator(context, Core::Id("Mercurial.LessUsedfunctionality"));
228 229 230
    createLessUsedActions(context);

    // Request the Tools menu and add the Mercurial menu to it
hjk's avatar
hjk committed
231
    Core::ActionContainer *toolsMenu = actionManager->actionContainer(Core::Id(Core::Constants::M_TOOLS));
232
    toolsMenu->addMenu(mercurialContainer);
233
    m_menuAction = mercurialContainer->menu()->menuAction();
234 235
}

236
void MercurialPlugin::createFileActions(const Core::Context &context)
237 238 239
{
    Core::Command *command;

240
    annotateFile = new Utils::ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
241
    command = actionManager->registerAction(annotateFile, Core::Id(Constants::ANNOTATE), context);
242 243 244
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(annotateFile, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
245
    m_commandLocator->appendCommand(command);
246

247
    diffFile = new Utils::ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
248
    command = actionManager->registerAction(diffFile, Core::Id(Constants::DIFF), context);
249
    command->setAttribute(Core::Command::CA_UpdateText);
250
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+D")));
251 252
    connect(diffFile, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
253
    m_commandLocator->appendCommand(command);
254

255
    logFile = new Utils::ParameterAction(tr("Log Current File"), tr("Log \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
256
    command = actionManager->registerAction(logFile, Core::Id(Constants::LOG), context);
257
    command->setAttribute(Core::Command::CA_UpdateText);
258
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+L")));
259 260
    connect(logFile, SIGNAL(triggered()), this, SLOT(logCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
261
    m_commandLocator->appendCommand(command);
262

263
    statusFile = new Utils::ParameterAction(tr("Status Current File"), tr("Status \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
264
    command = actionManager->registerAction(statusFile, Core::Id(Constants::STATUS), context);
265
    command->setAttribute(Core::Command::CA_UpdateText);
266
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+S")));
267 268
    connect(statusFile, SIGNAL(triggered()), this, SLOT(statusCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
269
    m_commandLocator->appendCommand(command);
270

271
    createSeparator(context, Core::Id("Mercurial.FileDirSeperator1"));
272 273

    m_addAction = new Utils::ParameterAction(tr("Add"), tr("Add \"%1\""), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
274
    command = actionManager->registerAction(m_addAction, Core::Id(Constants::ADD), context);
275 276 277
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
278
    m_commandLocator->appendCommand(command);
279 280

    m_deleteAction = new Utils::ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
281
    command = actionManager->registerAction(m_deleteAction, Core::Id(Constants::DELETE), context);
282 283 284
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
285
    m_commandLocator->appendCommand(command);
286 287

    revertFile = new Utils::ParameterAction(tr("Revert Current File..."), tr("Revert \"%1\"..."), Utils::ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
288
    command = actionManager->registerAction(revertFile, Core::Id(Constants::REVERT), context);
289 290 291
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(revertFile, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
292
    m_commandLocator->appendCommand(command);
293 294 295 296 297 298
}

void MercurialPlugin::addCurrentFile()
{
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
cerf's avatar
cerf committed
299
    m_client->synchronousAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
300 301 302 303
}

void MercurialPlugin::annotateCurrentFile()
{
304 305
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
306
    m_client->annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
307 308 309 310
}

void MercurialPlugin::diffCurrentFile()
{
311 312
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
313
    m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
314 315 316 317
}

void MercurialPlugin::logCurrentFile()
{
318 319
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
320
    m_client->log(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()),
321
                  QStringList(), true);
322 323 324 325
}

void MercurialPlugin::revertCurrentFile()
{
326 327 328
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)

329 330 331
    RevertDialog reverter;
    if (reverter.exec() != QDialog::Accepted)
        return;
332
    m_client->revertFile(state.currentFileTopLevel(), state.relativeCurrentFile(), reverter.revision());
333 334 335 336
}

void MercurialPlugin::statusCurrentFile()
{
337 338
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasFile(), return)
339
    m_client->status(state.currentFileTopLevel(), state.relativeCurrentFile());
340 341
}

342
void MercurialPlugin::createDirectoryActions(const Core::Context &context)
343 344 345 346 347
{
    QAction *action;
    Core::Command *command;

    action = new QAction(tr("Diff"), this);
348
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
349
    command = actionManager->registerAction(action, Core::Id(Constants::DIFFMULTI), context);
350 351
    connect(action, SIGNAL(triggered()), this, SLOT(diffRepository()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
352
    m_commandLocator->appendCommand(command);
353 354

    action = new QAction(tr("Log"), this);
355
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
356
    command = actionManager->registerAction(action, Core::Id(Constants::LOGMULTI), context);
357 358
    connect(action, SIGNAL(triggered()), this, SLOT(logRepository()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
359
    m_commandLocator->appendCommand(command);
360

361
    action = new QAction(tr("Revert..."), this);
362
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
363
    command = actionManager->registerAction(action, Core::Id(Constants::REVERTMULTI), context);
364 365
    connect(action, SIGNAL(triggered()), this, SLOT(revertMulti()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
366
    m_commandLocator->appendCommand(command);
367 368

    action = new QAction(tr("Status"), this);
369
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
370
    command = actionManager->registerAction(action, Core::Id(Constants::STATUSMULTI), context);
371 372
    connect(action, SIGNAL(triggered()), this, SLOT(statusMulti()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
373
    m_commandLocator->appendCommand(command);
374 375
}

Friedemann Kleint's avatar
Friedemann Kleint committed
376

377 378
void MercurialPlugin::diffRepository()
{
379 380
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)
381
    m_client->diff(state.topLevel());
382 383 384 385
}

void MercurialPlugin::logRepository()
{
386 387
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)
388
    m_client->log(state.topLevel());
389 390 391 392
}

void MercurialPlugin::revertMulti()
{
393 394 395
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

396 397 398
    RevertDialog reverter;
    if (reverter.exec() != QDialog::Accepted)
        return;
cerf's avatar
cerf committed
399
    m_client->revertAll(state.topLevel(), reverter.revision());
400 401 402 403
}

void MercurialPlugin::statusMulti()
{
404 405 406
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

407
    m_client->status(state.topLevel());
408 409
}

410
void MercurialPlugin::createRepositoryActions(const Core::Context &context)
411
{
412
    QAction *action = new QAction(tr("Pull..."), this);
413
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
414
    Core::Command *command = actionManager->registerAction(action, Core::Id(Constants::PULL), context);
415 416
    connect(action, SIGNAL(triggered()), this, SLOT(pull()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
417
    m_commandLocator->appendCommand(command);
418

419
    action = new QAction(tr("Push..."), this);
420
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
421
    command = actionManager->registerAction(action, Core::Id(Constants::PUSH), context);
422 423
    connect(action, SIGNAL(triggered()), this, SLOT(push()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
424
    m_commandLocator->appendCommand(command);
425

426
    action = new QAction(tr("Update..."), this);
427
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
428
    command = actionManager->registerAction(action, Core::Id(Constants::UPDATE), context);
429 430
    connect(action, SIGNAL(triggered()), this, SLOT(update()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
431
    m_commandLocator->appendCommand(command);
432

433
    action = new QAction(tr("Import..."), this);
434
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
435
    command = actionManager->registerAction(action, Core::Id(Constants::IMPORT), context);
436 437
    connect(action, SIGNAL(triggered()), this, SLOT(import()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
438
    m_commandLocator->appendCommand(command);
439

440
    action = new QAction(tr("Incoming..."), this);
441
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
442
    command = actionManager->registerAction(action, Core::Id(Constants::INCOMING), context);
443 444
    connect(action, SIGNAL(triggered()), this, SLOT(incoming()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
445
    m_commandLocator->appendCommand(command);
446

447
    action = new QAction(tr("Outgoing..."), this);
448
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
449
    command = actionManager->registerAction(action, Core::Id(Constants::OUTGOING), context);
450 451
    connect(action, SIGNAL(triggered()), this, SLOT(outgoing()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
452
    m_commandLocator->appendCommand(command);
453

454
    action = new QAction(tr("Commit..."), this);
455
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
456
    command = actionManager->registerAction(action, Core::Id(Constants::COMMIT), context);
457
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+C")));
458 459
    connect(action, SIGNAL(triggered()), this, SLOT(commit()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
460
    m_commandLocator->appendCommand(command);
461 462

    m_createRepositoryAction = new QAction(tr("Create Repository..."), this);
hjk's avatar
hjk committed
463
    command = actionManager->registerAction(m_createRepositoryAction, Core::Id(Constants::CREATE_REPOSITORY), context);
464 465
    connect(m_createRepositoryAction, SIGNAL(triggered()), this, SLOT(createRepository()));
    mercurialContainer->addAction(command);
466 467 468 469
}

void MercurialPlugin::pull()
{
470 471 472
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

473
    SrcDestDialog dialog;
474
    dialog.setWindowTitle(tr("Pull Source"));
475 476
    if (dialog.exec() != QDialog::Accepted)
        return;
cerf's avatar
cerf committed
477
    m_client->synchronousPull(state.topLevel(), dialog.getRepositoryString());
478 479 480 481
}

void MercurialPlugin::push()
{
482 483 484
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

485
    SrcDestDialog dialog;
486
    dialog.setWindowTitle(tr("Push Destination"));
487 488
    if (dialog.exec() != QDialog::Accepted)
        return;
cerf's avatar
cerf committed
489
    m_client->synchronousPush(state.topLevel(), dialog.getRepositoryString());
490 491 492 493
}

void MercurialPlugin::update()
{
494 495 496
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

497
    RevertDialog updateDialog;
498
    updateDialog.setWindowTitle(tr("Update"));
499 500
    if (updateDialog.exec() != QDialog::Accepted)
        return;
501
    m_client->update(state.topLevel(), updateDialog.revision());
502 503 504 505
}

void MercurialPlugin::import()
{
506 507 508
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

509 510 511 512 513 514 515 516
    QFileDialog importDialog;
    importDialog.setFileMode(QFileDialog::ExistingFiles);
    importDialog.setViewMode(QFileDialog::Detail);

    if (importDialog.exec() != QDialog::Accepted)
        return;

    const QStringList fileNames = importDialog.selectedFiles();
517
    m_client->import(state.topLevel(), fileNames);
518 519 520 521
}

void MercurialPlugin::incoming()
{
522 523 524
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

525
    SrcDestDialog dialog;
526
    dialog.setWindowTitle(tr("Incoming Source"));
527 528
    if (dialog.exec() != QDialog::Accepted)
        return;
529
    m_client->incoming(state.topLevel(), dialog.getRepositoryString());
530 531 532 533
}

void MercurialPlugin::outgoing()
{
534 535
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)
536
    m_client->outgoing(state.topLevel());
537 538 539 540
}

void MercurialPlugin::createSubmitEditorActions()
{
541
    Core::Context context(Constants::COMMIT_ID);
542 543 544
    Core::Command *command;

    editorCommit = new QAction(VCSBase::VCSBaseSubmitEditor::submitIcon(), tr("Commit"), this);
hjk's avatar
hjk committed
545
    command = actionManager->registerAction(editorCommit, Core::Id(Constants::COMMIT), context);
546
    command->setAttribute(Core::Command::CA_UpdateText);
547 548
    connect(editorCommit, SIGNAL(triggered()), this, SLOT(commitFromEditor()));

549
    editorDiff = new QAction(VCSBase::VCSBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
hjk's avatar
hjk committed
550
    command = actionManager->registerAction(editorDiff, Core::Id(Constants::DIFFEDITOR), context);
551 552

    editorUndo = new QAction(tr("&Undo"), this);
hjk's avatar
hjk committed
553
    command = actionManager->registerAction(editorUndo, Core::Id(Core::Constants::UNDO), context);
554 555

    editorRedo = new QAction(tr("&Redo"), this);
hjk's avatar
hjk committed
556
    command = actionManager->registerAction(editorRedo, Core::Id(Core::Constants::REDO), context);
557 558 559 560 561 562 563
}

void MercurialPlugin::commit()
{
    if (VCSBase::VCSBaseSubmitEditor::raiseSubmitEditor())
        return;

564 565 566 567 568
    const VCSBase::VCSBasePluginState state = currentState();
    QTC_ASSERT(state.hasTopLevel(), return)

    m_submitRepository = state.topLevel();

569 570
    connect(m_client, SIGNAL(parsedStatus(QList<VCSBase::VCSBaseClient::StatusItem>)),
            this, SLOT(showCommitWidget(QList<VCSBase::VCSBaseClient::StatusItem>)));
571
    m_client->emitParsedStatus(m_submitRepository);
572 573
}

574
void MercurialPlugin::showCommitWidget(const QList<VCSBase::VCSBaseClient::StatusItem> &status)
575
{
576 577

    VCSBase::VCSBaseOutputWindow *outputWindow = VCSBase::VCSBaseOutputWindow::instance();
578
    //Once we receive our data release the connection so it can be reused elsewhere
579 580
    disconnect(m_client, SIGNAL(parsedStatus(QList<VCSBase::VCSBaseClient::StatusItem>)),
               this, SLOT(showCommitWidget(QList<VCSBase::VCSBaseClient::StatusItem>)));
581 582

    if (status.isEmpty()) {
583
        outputWindow->appendError(tr("There are no changes to commit."));
584 585 586 587 588
        return;
    }

    deleteCommitLog();

589 590 591 592 593 594
    // Open commit log
    QString changeLogPattern = QDir::tempPath();
    if (!changeLogPattern.endsWith(QLatin1Char('/')))
        changeLogPattern += QLatin1Char('/');
    changeLogPattern += QLatin1String("qtcreator-hg-XXXXXX.msg");
    changeLog = new QTemporaryFile(changeLogPattern,  this);
595
    if (!changeLog->open()) {
596
        outputWindow->appendError(tr("Unable to generate a temporary file for the commit editor."));
597 598 599 600
        return;
    }

    Core::IEditor *editor = core->editorManager()->openEditor(changeLog->fileName(),
601 602
                                                              QLatin1String(Constants::COMMIT_ID),
                                                              Core::EditorManager::ModeSwitch);
603
    if (!editor) {
604
        outputWindow->appendError(tr("Unable to create an editor for the commit."));
605 606 607
        return;
    }

608 609
    QTC_ASSERT(qobject_cast<CommitEditor *>(editor), return)
    CommitEditor *commitEditor = static_cast<CommitEditor *>(editor);
610

611 612
    const QString msg = tr("Commit changes for \"%1\".").
                        arg(QDir::toNativeSeparators(m_submitRepository));
613
    commitEditor->setDisplayName(msg);
614

615
    QString branch = m_client->branchQuerySync(m_submitRepository);
616

617
    commitEditor->setFields(m_submitRepository, branch, mercurialSettings.userName(),
618
                            mercurialSettings.email(), status);
619 620

    commitEditor->registerActions(editorUndo, editorRedo, editorCommit, editorDiff);
621 622
    connect(commitEditor, SIGNAL(diffSelectedFiles(QStringList)),
            this, SLOT(diffFromEditorSelected(QStringList)));
623
    commitEditor->setCheckScriptWorkingDirectory(m_submitRepository);
624 625 626 627
}

void MercurialPlugin::diffFromEditorSelected(const QStringList &files)
{
628
    m_client->diff(m_submitRepository, files);
629 630 631 632 633 634 635 636 637 638 639
}

void MercurialPlugin::commitFromEditor()
{
    if (!changeLog)
        return;

    //use the same functionality than if the user closes the file without completing the commit
    core->editorManager()->closeEditors(core->editorManager()->editorsForFileName(changeLog->fileName()));
}

640
bool MercurialPlugin::submitEditorAboutToClose(VCSBase::VCSBaseSubmitEditor *submitEditor)
641
{
642
    if (!changeLog)
643
        return true;
644 645
    Core::IFile *editorFile = submitEditor->file();
    CommitEditor *commitEditor = qobject_cast<CommitEditor *>(submitEditor);
646 647 648
    if (!editorFile || !commitEditor)
        return true;

649
    bool dummyPrompt = mercurialSettings.prompt();
650
    const VCSBase::VCSBaseSubmitEditor::PromptSubmitResult response =
Leena Miettinen's avatar
Leena Miettinen committed
651
            commitEditor->promptSubmit(tr("Close Commit Editor"), tr("Do you want to commit the changes?"),
652
                                       tr("Message check failed. Do you want to proceed?"),
653
                                       &dummyPrompt, mercurialSettings.prompt());
654 655 656 657 658 659 660 661 662 663 664 665 666 667

    switch (response) {
    case VCSBase::VCSBaseSubmitEditor::SubmitCanceled:
        return false;
    case VCSBase::VCSBaseSubmitEditor::SubmitDiscarded:
        deleteCommitLog();
        return true;
    default:
        break;
    }

    const QStringList files = commitEditor->checkedFiles();
    if (!files.empty()) {
        //save the commit message
668 669
        if (!core->fileManager()->saveFile(editorFile))
            return false;
670

671 672 673
        QStringList extraOptions;
        if (!commitEditor->committerInfo().isEmpty())
            extraOptions << QLatin1String("-u") << commitEditor->committerInfo();
cerf's avatar
cerf committed
674 675
        m_client->commit(m_submitRepository, files, editorFile->fileName(),
                         extraOptions);
676 677 678
    }
    return true;
}
cerf's avatar
cerf committed
679

680 681 682 683 684 685 686 687
void MercurialPlugin::deleteCommitLog()
{
    if (changeLog) {
        delete changeLog;
        changeLog = 0;
    }
}

688
void MercurialPlugin::createRepositoryManagementActions(const Core::Context &context)
689 690 691 692 693 694 695 696 697 698 699
{
    //TODO create menu for these options
    Q_UNUSED(context);
    return;
    //    QAction *action = new QAction(tr("Branch"), this);
    //    actionList.append(action);
    //    Core::Command *command = actionManager->registerAction(action, Constants::BRANCH, context);
    //    //    connect(action, SIGNAL(triggered()), this, SLOT(branch()));
    //    mercurialContainer->addAction(command);
}

700
void MercurialPlugin::createLessUsedActions(const Core::Context &context)
701 702 703 704 705 706
{
    //TODO create menue for these options
    Q_UNUSED(context);
    return;
}

hjk's avatar
hjk committed
707
void MercurialPlugin::createSeparator(const Core::Context &context, const Core::Id &id)
708 709 710 711 712 713
{
    QAction *action = new QAction(this);
    action->setSeparator(true);
    mercurialContainer->addAction(actionManager->registerAction(action, id, context));
}

714
void MercurialPlugin::updateActions(VCSBase::VCSBasePlugin::ActionState as)
715
{
716 717
    if (!enableMenuAction(as, m_menuAction)) {
        m_commandLocator->setEnabled(false);
718
        return;
719
    }
720 721
    const QString filename = currentState().currentFileName();
    const bool repoEnabled = currentState().hasTopLevel();
722
    m_commandLocator->setEnabled(repoEnabled);
723 724 725 726

    annotateFile->setParameter(filename);
    diffFile->setParameter(filename);
    logFile->setParameter(filename);
727 728
    m_addAction->setParameter(filename);
    m_deleteAction->setParameter(filename);
729 730 731
    revertFile->setParameter(filename);
    statusFile->setParameter(filename);

732 733
    foreach (QAction *repoAction, m_repositoryActionList)
        repoAction->setEnabled(repoEnabled);
734 735 736
}

Q_EXPORT_PLUGIN(MercurialPlugin)