mercurialplugin.cpp 26.6 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
29
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@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/uniqueidmanager.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;

cerf's avatar
cerf committed
159
    m_client = new MercurialClient(mercurialSettings);
160
    VCSBase::VCSBasePlugin::initialize(new MercurialControl(m_client));
161

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

    optionsPage = new OptionsPage();
    addAutoReleasedObject(optionsPage);
cerf's avatar
cerf committed
167
    mercurialSettings.readSettings(core->settings(), QLatin1String("Mercurial"));
168

169
    connect(optionsPage, SIGNAL(settingsChanged()), m_client, SLOT(settingsChanged()));
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()), true);
321
322
323
324
}

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

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

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

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

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

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

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

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

Friedemann Kleint's avatar
Friedemann Kleint committed
375

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    m_submitRepository = state.topLevel();

568
    connect(m_client, SIGNAL(parsedStatus(QList<QPair<QString,QString> >)),
569
            this, SLOT(showCommitWidget(QList<QPair<QString,QString> >)));
570
    m_client->statusWithSignal(m_submitRepository);
571
572
573
574
}

void MercurialPlugin::showCommitWidget(const QList<QPair<QString, QString> > &status)
{
575
576

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

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

    deleteCommitLog();

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

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

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

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

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

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

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

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

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()));
}

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

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

    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
667
        core->fileManager()->saveFile(editorFile);
668

cerf's avatar
cerf committed
669
670
671
672
        QHash<int, QVariant> extraOptions;
        extraOptions[MercurialClient::AuthorCommitOptionId] = commitEditor->committerInfo();
        m_client->commit(m_submitRepository, files, editorFile->fileName(),
                         extraOptions);
673
674
675
    }
    return true;
}
cerf's avatar
cerf committed
676

677
678
679
680
681
682
683
684
void MercurialPlugin::deleteCommitLog()
{
    if (changeLog) {
        delete changeLog;
        changeLog = 0;
    }
}

685
void MercurialPlugin::createRepositoryManagementActions(const Core::Context &context)
686
687
688
689
690
691
692
693
694
695
696
{
    //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);
}

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

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

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

    annotateFile->setParameter(filename);
    diffFile->setParameter(filename);
    logFile->setParameter(filename);
724
725
    m_addAction->setParameter(filename);
    m_deleteAction->setParameter(filename);
726
727
728
    revertFile->setParameter(filename);
    statusFile->setParameter(filename);

729
730
    foreach (QAction *repoAction, m_repositoryActionList)
        repoAction->setEnabled(repoEnabled);
731
732
733
}

Q_EXPORT_PLUGIN(MercurialPlugin)