mercurialplugin.cpp 25.8 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
**
7
** Contact: Nokia Corporation (qt-info@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
29
** 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/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

#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;
hjk's avatar
hjk committed
79
80
using namespace VcsBase;
using namespace Utils;
81

hjk's avatar
hjk committed
82
static const VcsBaseEditorParameters editorParameters[] = {
83
{
hjk's avatar
hjk committed
84
    RegularCommandOutput, //type
85
86
    Constants::COMMANDLOG_ID, // id
    Constants::COMMANDLOG_DISPLAY_NAME, // display name
87
88
89
90
    Constants::COMMANDLOG, // context
    Constants::COMMANDAPP, // mime type
    Constants::COMMANDEXT}, //extension

hjk's avatar
hjk committed
91
{   LogOutput,
92
93
    Constants::FILELOG_ID,
    Constants::FILELOG_DISPLAY_NAME,
94
95
96
97
    Constants::FILELOG,
    Constants::LOGAPP,
    Constants::LOGEXT},

hjk's avatar
hjk committed
98
99
100
101
102
103
{   AnnotateOutput,
    Constants::ANNOTATELOG_ID,
    Constants::ANNOTATELOG_DISPLAY_NAME,
    Constants::ANNOTATELOG,
    Constants::ANNOTATEAPP,
    Constants::ANNOTATEEXT},
104

hjk's avatar
hjk committed
105
{   DiffOutput,
106
107
    Constants::DIFFLOG_ID,
    Constants::DIFFLOG_DISPLAY_NAME,
108
109
110
111
112
    Constants::DIFFLOG,
    Constants::DIFFAPP,
    Constants::DIFFEXT}
};

hjk's avatar
hjk committed
113
static const VcsBaseSubmitEditorParameters submitEditorParameters = {
114
    Constants::COMMITMIMETYPE,
115
116
117
    Constants::COMMIT_ID,
    Constants::COMMIT_DISPLAY_NAME,
    Constants::COMMIT_ID
118
119
120
121
};

MercurialPlugin *MercurialPlugin::m_instance = 0;

122
MercurialPlugin::MercurialPlugin() :
hjk's avatar
hjk committed
123
        VcsBasePlugin(QLatin1String(Constants::COMMIT_ID)),
124
        optionsPage(0),
125
        m_client(0),
Friedemann Kleint's avatar
Friedemann Kleint committed
126
127
        core(0),
        m_commandLocator(0),
128
        changeLog(0),
129
130
        m_addAction(0),
        m_deleteAction(0),
131
        m_createRepositoryAction(0),
132
        m_menuAction(0)
133
134
135
136
137
138
{
    m_instance = this;
}

MercurialPlugin::~MercurialPlugin()
{
139
140
141
    if (m_client) {
        delete m_client;
        m_client = 0;
142
143
144
145
146
147
148
    }

    deleteCommitLog();

    m_instance = 0;
}

hjk's avatar
hjk committed
149
bool MercurialPlugin::initialize(const QStringList & /* arguments */, QString * /*errorMessage */)
150
{
hjk's avatar
hjk committed
151
    typedef VcsEditorFactory<MercurialEditor> MercurialEditorFactory;
152

153
    m_client = new MercurialClient(&mercurialSettings);
154
    initializeVcs(new MercurialControl(m_client));
155

156
157
158
159
160
    core = Core::ICore::instance();
    actionManager = core->actionManager();

    optionsPage = new OptionsPage();
    addAutoReleasedObject(optionsPage);
161
    mercurialSettings.readSettings(core->settings());
162

163
    connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant)));
164
165

    static const char *describeSlot = SLOT(view(QString,QString));
hjk's avatar
hjk committed
166
    const int editorCount = sizeof(editorParameters)/sizeof(editorParameters[0]);
167
    for (int i = 0; i < editorCount; i++)
168
        addAutoReleasedObject(new MercurialEditorFactory(editorParameters + i, m_client, describeSlot));
169

hjk's avatar
hjk committed
170
    addAutoReleasedObject(new VcsSubmitEditorFactory<CommitEditor>(&submitEditorParameters));
171
172
173

    addAutoReleasedObject(new CloneWizard);

Friedemann Kleint's avatar
Friedemann Kleint committed
174
175
176
177
    const QString prefix = QLatin1String("hg");
    m_commandLocator = new Locator::CommandLocator(QLatin1String("Mercurial"), prefix, prefix);
    addAutoReleasedObject(m_commandLocator);

178
179
180
181
182
183
184
    createMenu();

    createSubmitEditorActions();

    return true;
}

185
const MercurialSettings &MercurialPlugin::settings() const
186
187
188
189
{
    return mercurialSettings;
}

190
191
192
193
void MercurialPlugin::setSettings(const MercurialSettings &settings)
{
    if (settings != mercurialSettings) {
        mercurialSettings = settings;
194
        static_cast<MercurialControl *>(versionControl())->emitConfigurationChanged();
195
196
197
    }
}

198
199
void MercurialPlugin::createMenu()
{
200
    Core::Context context(Core::Constants::C_GLOBAL);
201
202

    // Create menu item for Mercurial
hjk's avatar
hjk committed
203
    mercurialContainer = actionManager->createMenu(Core::Id("Mercurial.MercurialMenu"));
204
205
206
207
    QMenu *menu = mercurialContainer->menu();
    menu->setTitle(tr("Mercurial"));

    createFileActions(context);
208
    createSeparator(context, Core::Id("Mercurial.FileDirSeperator"));
209
    createDirectoryActions(context);
210
    createSeparator(context, Core::Id("Mercurial.DirRepoSeperator"));
211
    createRepositoryActions(context);
212
    createSeparator(context, Core::Id("Mercurial.Repository Management"));
213
    createRepositoryManagementActions(context);
214
    createSeparator(context, Core::Id("Mercurial.LessUsedfunctionality"));
215
216
217
    createLessUsedActions(context);

    // Request the Tools menu and add the Mercurial menu to it
hjk's avatar
hjk committed
218
    Core::ActionContainer *toolsMenu = actionManager->actionContainer(Core::Id(Core::Constants::M_TOOLS));
219
    toolsMenu->addMenu(mercurialContainer);
220
    m_menuAction = mercurialContainer->menu()->menuAction();
221
222
}

223
void MercurialPlugin::createFileActions(const Core::Context &context)
224
225
226
{
    Core::Command *command;

hjk's avatar
hjk committed
227
    annotateFile = new ParameterAction(tr("Annotate Current File"), tr("Annotate \"%1\""), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
228
    command = actionManager->registerAction(annotateFile, Core::Id(Constants::ANNOTATE), context);
229
230
231
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(annotateFile, SIGNAL(triggered()), this, SLOT(annotateCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
232
    m_commandLocator->appendCommand(command);
233

hjk's avatar
hjk committed
234
    diffFile = new ParameterAction(tr("Diff Current File"), tr("Diff \"%1\""), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
235
    command = actionManager->registerAction(diffFile, Core::Id(Constants::DIFF), context);
236
    command->setAttribute(Core::Command::CA_UpdateText);
237
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+D")));
238
239
    connect(diffFile, SIGNAL(triggered()), this, SLOT(diffCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
240
    m_commandLocator->appendCommand(command);
241

hjk's avatar
hjk committed
242
    logFile = new ParameterAction(tr("Log Current File"), tr("Log \"%1\""), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
243
    command = actionManager->registerAction(logFile, Core::Id(Constants::LOG), context);
244
    command->setAttribute(Core::Command::CA_UpdateText);
245
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+L")));
246
247
    connect(logFile, SIGNAL(triggered()), this, SLOT(logCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
248
    m_commandLocator->appendCommand(command);
249

hjk's avatar
hjk committed
250
    statusFile = new ParameterAction(tr("Status Current File"), tr("Status \"%1\""), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
251
    command = actionManager->registerAction(statusFile, Core::Id(Constants::STATUS), context);
252
    command->setAttribute(Core::Command::CA_UpdateText);
253
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+S")));
254
255
    connect(statusFile, SIGNAL(triggered()), this, SLOT(statusCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
256
    m_commandLocator->appendCommand(command);
257

258
    createSeparator(context, Core::Id("Mercurial.FileDirSeperator1"));
259

hjk's avatar
hjk committed
260
    m_addAction = new ParameterAction(tr("Add"), tr("Add \"%1\""), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
261
    command = actionManager->registerAction(m_addAction, Core::Id(Constants::ADD), context);
262
263
264
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_addAction, SIGNAL(triggered()), this, SLOT(addCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
265
    m_commandLocator->appendCommand(command);
266

hjk's avatar
hjk committed
267
    m_deleteAction = new ParameterAction(tr("Delete..."), tr("Delete \"%1\"..."), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
268
    command = actionManager->registerAction(m_deleteAction, Core::Id(Constants::DELETE), context);
269
270
271
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(promptToDeleteCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
272
    m_commandLocator->appendCommand(command);
273

hjk's avatar
hjk committed
274
    revertFile = new ParameterAction(tr("Revert Current File..."), tr("Revert \"%1\"..."), ParameterAction::EnabledWithParameter, this);
hjk's avatar
hjk committed
275
    command = actionManager->registerAction(revertFile, Core::Id(Constants::REVERT), context);
276
277
278
    command->setAttribute(Core::Command::CA_UpdateText);
    connect(revertFile, SIGNAL(triggered()), this, SLOT(revertCurrentFile()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
279
    m_commandLocator->appendCommand(command);
280
281
282
283
}

void MercurialPlugin::addCurrentFile()
{
hjk's avatar
hjk committed
284
    const VcsBasePluginState state = currentState();
285
    QTC_ASSERT(state.hasFile(), return)
cerf's avatar
cerf committed
286
    m_client->synchronousAdd(state.currentFileTopLevel(), state.relativeCurrentFile());
287
288
289
290
}

void MercurialPlugin::annotateCurrentFile()
{
hjk's avatar
hjk committed
291
    const VcsBasePluginState state = currentState();
292
    QTC_ASSERT(state.hasFile(), return)
293
    m_client->annotate(state.currentFileTopLevel(), state.relativeCurrentFile());
294
295
296
297
}

void MercurialPlugin::diffCurrentFile()
{
hjk's avatar
hjk committed
298
    const VcsBasePluginState state = currentState();
299
    QTC_ASSERT(state.hasFile(), return)
300
    m_client->diff(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()));
301
302
303
304
}

void MercurialPlugin::logCurrentFile()
{
hjk's avatar
hjk committed
305
    const VcsBasePluginState state = currentState();
306
    QTC_ASSERT(state.hasFile(), return)
307
    m_client->log(state.currentFileTopLevel(), QStringList(state.relativeCurrentFile()),
308
                  QStringList(), true);
309
310
311
312
}

void MercurialPlugin::revertCurrentFile()
{
hjk's avatar
hjk committed
313
    const VcsBasePluginState state = currentState();
314
315
    QTC_ASSERT(state.hasFile(), return)

316
317
318
    RevertDialog reverter;
    if (reverter.exec() != QDialog::Accepted)
        return;
319
    m_client->revertFile(state.currentFileTopLevel(), state.relativeCurrentFile(), reverter.revision());
320
321
322
323
}

void MercurialPlugin::statusCurrentFile()
{
hjk's avatar
hjk committed
324
    const VcsBasePluginState state = currentState();
325
    QTC_ASSERT(state.hasFile(), return)
326
    m_client->status(state.currentFileTopLevel(), state.relativeCurrentFile());
327
328
}

329
void MercurialPlugin::createDirectoryActions(const Core::Context &context)
330
331
332
333
334
{
    QAction *action;
    Core::Command *command;

    action = new QAction(tr("Diff"), this);
335
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
336
    command = actionManager->registerAction(action, Core::Id(Constants::DIFFMULTI), context);
337
338
    connect(action, SIGNAL(triggered()), this, SLOT(diffRepository()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
339
    m_commandLocator->appendCommand(command);
340
341

    action = new QAction(tr("Log"), this);
342
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
343
    command = actionManager->registerAction(action, Core::Id(Constants::LOGMULTI), context);
344
345
    connect(action, SIGNAL(triggered()), this, SLOT(logRepository()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
346
    m_commandLocator->appendCommand(command);
347

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

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

Friedemann Kleint's avatar
Friedemann Kleint committed
363

364
365
void MercurialPlugin::diffRepository()
{
hjk's avatar
hjk committed
366
    const VcsBasePluginState state = currentState();
367
    QTC_ASSERT(state.hasTopLevel(), return)
368
    m_client->diff(state.topLevel());
369
370
371
372
}

void MercurialPlugin::logRepository()
{
hjk's avatar
hjk committed
373
    const VcsBasePluginState state = currentState();
374
    QTC_ASSERT(state.hasTopLevel(), return)
375
    m_client->log(state.topLevel());
376
377
378
379
}

void MercurialPlugin::revertMulti()
{
hjk's avatar
hjk committed
380
    const VcsBasePluginState state = currentState();
381
382
    QTC_ASSERT(state.hasTopLevel(), return)

383
384
385
    RevertDialog reverter;
    if (reverter.exec() != QDialog::Accepted)
        return;
cerf's avatar
cerf committed
386
    m_client->revertAll(state.topLevel(), reverter.revision());
387
388
389
390
}

void MercurialPlugin::statusMulti()
{
hjk's avatar
hjk committed
391
    const VcsBasePluginState state = currentState();
392
393
    QTC_ASSERT(state.hasTopLevel(), return)

394
    m_client->status(state.topLevel());
395
396
}

397
void MercurialPlugin::createRepositoryActions(const Core::Context &context)
398
{
399
    QAction *action = new QAction(tr("Pull..."), this);
400
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
401
    Core::Command *command = actionManager->registerAction(action, Core::Id(Constants::PULL), context);
402
403
    connect(action, SIGNAL(triggered()), this, SLOT(pull()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
404
    m_commandLocator->appendCommand(command);
405

406
    action = new QAction(tr("Push..."), this);
407
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
408
    command = actionManager->registerAction(action, Core::Id(Constants::PUSH), context);
409
410
    connect(action, SIGNAL(triggered()), this, SLOT(push()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
411
    m_commandLocator->appendCommand(command);
412

413
    action = new QAction(tr("Update..."), this);
414
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
415
    command = actionManager->registerAction(action, Core::Id(Constants::UPDATE), context);
416
417
    connect(action, SIGNAL(triggered()), this, SLOT(update()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
418
    m_commandLocator->appendCommand(command);
419

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

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

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

441
    action = new QAction(tr("Commit..."), this);
442
    m_repositoryActionList.append(action);
hjk's avatar
hjk committed
443
    command = actionManager->registerAction(action, Core::Id(Constants::COMMIT), context);
444
    command->setDefaultKeySequence(QKeySequence(tr("Alt+H,Alt+C")));
445
446
    connect(action, SIGNAL(triggered()), this, SLOT(commit()));
    mercurialContainer->addAction(command);
Friedemann Kleint's avatar
Friedemann Kleint committed
447
    m_commandLocator->appendCommand(command);
448
449

    m_createRepositoryAction = new QAction(tr("Create Repository..."), this);
hjk's avatar
hjk committed
450
    command = actionManager->registerAction(m_createRepositoryAction, Core::Id(Constants::CREATE_REPOSITORY), context);
451
452
    connect(m_createRepositoryAction, SIGNAL(triggered()), this, SLOT(createRepository()));
    mercurialContainer->addAction(command);
453
454
455
456
}

void MercurialPlugin::pull()
{
hjk's avatar
hjk committed
457
    const VcsBasePluginState state = currentState();
458
459
    QTC_ASSERT(state.hasTopLevel(), return)

460
    SrcDestDialog dialog;
461
    dialog.setWindowTitle(tr("Pull Source"));
462
463
    if (dialog.exec() != QDialog::Accepted)
        return;
cerf's avatar
cerf committed
464
    m_client->synchronousPull(state.topLevel(), dialog.getRepositoryString());
465
466
467
468
}

void MercurialPlugin::push()
{
hjk's avatar
hjk committed
469
    const VcsBasePluginState state = currentState();
470
471
    QTC_ASSERT(state.hasTopLevel(), return)

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

void MercurialPlugin::update()
{
hjk's avatar
hjk committed
481
    const VcsBasePluginState state = currentState();
482
483
    QTC_ASSERT(state.hasTopLevel(), return)

484
    RevertDialog updateDialog;
485
    updateDialog.setWindowTitle(tr("Update"));
486
487
    if (updateDialog.exec() != QDialog::Accepted)
        return;
488
    m_client->update(state.topLevel(), updateDialog.revision());
489
490
491
492
}

void MercurialPlugin::import()
{
hjk's avatar
hjk committed
493
    const VcsBasePluginState state = currentState();
494
495
    QTC_ASSERT(state.hasTopLevel(), return)

496
497
498
499
500
501
502
503
    QFileDialog importDialog;
    importDialog.setFileMode(QFileDialog::ExistingFiles);
    importDialog.setViewMode(QFileDialog::Detail);

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

    const QStringList fileNames = importDialog.selectedFiles();
504
    m_client->import(state.topLevel(), fileNames);
505
506
507
508
}

void MercurialPlugin::incoming()
{
hjk's avatar
hjk committed
509
    const VcsBasePluginState state = currentState();
510
511
    QTC_ASSERT(state.hasTopLevel(), return)

512
    SrcDestDialog dialog;
513
    dialog.setWindowTitle(tr("Incoming Source"));
514
515
    if (dialog.exec() != QDialog::Accepted)
        return;
516
    m_client->incoming(state.topLevel(), dialog.getRepositoryString());
517
518
519
520
}

void MercurialPlugin::outgoing()
{
hjk's avatar
hjk committed
521
    const VcsBasePluginState state = currentState();
522
    QTC_ASSERT(state.hasTopLevel(), return)
523
    m_client->outgoing(state.topLevel());
524
525
526
527
}

void MercurialPlugin::createSubmitEditorActions()
{
528
    Core::Context context(Constants::COMMIT_ID);
529
530
    Core::Command *command;

hjk's avatar
hjk committed
531
    editorCommit = new QAction(VcsBaseSubmitEditor::submitIcon(), tr("Commit"), this);
hjk's avatar
hjk committed
532
    command = actionManager->registerAction(editorCommit, Core::Id(Constants::COMMIT), context);
533
    command->setAttribute(Core::Command::CA_UpdateText);
534
535
    connect(editorCommit, SIGNAL(triggered()), this, SLOT(commitFromEditor()));

hjk's avatar
hjk committed
536
    editorDiff = new QAction(VcsBaseSubmitEditor::diffIcon(), tr("Diff &Selected Files"), this);
hjk's avatar
hjk committed
537
    command = actionManager->registerAction(editorDiff, Core::Id(Constants::DIFFEDITOR), context);
538
539

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

    editorRedo = new QAction(tr("&Redo"), this);
hjk's avatar
hjk committed
543
    command = actionManager->registerAction(editorRedo, Core::Id(Core::Constants::REDO), context);
544
545
546
547
}

void MercurialPlugin::commit()
{
hjk's avatar
hjk committed
548
    if (VcsBaseSubmitEditor::raiseSubmitEditor())
549
550
        return;

hjk's avatar
hjk committed
551
    const VcsBasePluginState state = currentState();
552
553
554
555
    QTC_ASSERT(state.hasTopLevel(), return)

    m_submitRepository = state.topLevel();

hjk's avatar
hjk committed
556
557
    connect(m_client, SIGNAL(parsedStatus(QList<VcsBaseClient::StatusItem>)),
            this, SLOT(showCommitWidget(QList<VcsBaseClient::StatusItem>)));
558
    m_client->emitParsedStatus(m_submitRepository);
559
560
}

hjk's avatar
hjk committed
561
void MercurialPlugin::showCommitWidget(const QList<VcsBaseClient::StatusItem> &status)
562
{
563

hjk's avatar
hjk committed
564
    VcsBaseOutputWindow *outputWindow = VcsBaseOutputWindow::instance();
565
    //Once we receive our data release the connection so it can be reused elsewhere
hjk's avatar
hjk committed
566
567
    disconnect(m_client, SIGNAL(parsedStatus(QList<VcsBaseClient::StatusItem>)),
               this, SLOT(showCommitWidget(QList<VcsBaseClient::StatusItem>)));
568
569

    if (status.isEmpty()) {
570
        outputWindow->appendError(tr("There are no changes to commit."));
571
572
573
574
575
        return;
    }

    deleteCommitLog();

576
577
578
579
580
581
    // Open commit log
    QString changeLogPattern = QDir::tempPath();
    if (!changeLogPattern.endsWith(QLatin1Char('/')))
        changeLogPattern += QLatin1Char('/');
    changeLogPattern += QLatin1String("qtcreator-hg-XXXXXX.msg");
    changeLog = new QTemporaryFile(changeLogPattern,  this);
582
    if (!changeLog->open()) {
583
        outputWindow->appendError(tr("Unable to generate a temporary file for the commit editor."));
584
585
586
587
        return;
    }

    Core::IEditor *editor = core->editorManager()->openEditor(changeLog->fileName(),
hjk's avatar
hjk committed
588
                                                              Constants::COMMIT_ID,
589
                                                              Core::EditorManager::ModeSwitch);
590
    if (!editor) {
591
        outputWindow->appendError(tr("Unable to create an editor for the commit."));
592
593
594
        return;
    }

595
596
    QTC_ASSERT(qobject_cast<CommitEditor *>(editor), return)
    CommitEditor *commitEditor = static_cast<CommitEditor *>(editor);
597

598
599
600
601
602
    commitEditor->registerActions(editorUndo, editorRedo, editorCommit, editorDiff);
    connect(commitEditor, SIGNAL(diffSelectedFiles(QStringList)),
            this, SLOT(diffFromEditorSelected(QStringList)));
    commitEditor->setCheckScriptWorkingDirectory(m_submitRepository);

603
604
    const QString msg = tr("Commit changes for \"%1\".").
                        arg(QDir::toNativeSeparators(m_submitRepository));
605
    commitEditor->setDisplayName(msg);
606

607
    QString branch = m_client->branchQuerySync(m_submitRepository);
608
609
610
    commitEditor->setFields(m_submitRepository, branch,
                            mercurialSettings.stringValue(MercurialSettings::userNameKey),
                            mercurialSettings.stringValue(MercurialSettings::userEmailKey), status);
611
612
613
614
}

void MercurialPlugin::diffFromEditorSelected(const QStringList &files)
{
615
    m_client->diff(m_submitRepository, files);
616
617
618
619
620
621
622
623
624
625
626
}

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

hjk's avatar
hjk committed
627
bool MercurialPlugin::submitEditorAboutToClose(VcsBaseSubmitEditor *submitEditor)
628
{
629
    if (!changeLog)
630
        return true;
631
632
    Core::IFile *editorFile = submitEditor->file();
    CommitEditor *commitEditor = qobject_cast<CommitEditor *>(submitEditor);
633
634
635
    if (!editorFile || !commitEditor)
        return true;

636
    bool dummyPrompt = mercurialSettings.boolValue(MercurialSettings::promptOnSubmitKey);
hjk's avatar
hjk committed
637
    const VcsBaseSubmitEditor::PromptSubmitResult response =
Leena Miettinen's avatar
Leena Miettinen committed
638
            commitEditor->promptSubmit(tr("Close Commit Editor"), tr("Do you want to commit the changes?"),
639
                                       tr("Message check failed. Do you want to proceed?"),
640
                                       &dummyPrompt, dummyPrompt);
641
642

    switch (response) {
hjk's avatar
hjk committed
643
    case VcsBaseSubmitEditor::SubmitCanceled:
644
        return false;
hjk's avatar
hjk committed
645
    case VcsBaseSubmitEditor::SubmitDiscarded:
646
647
648
649
650
651
652
653
654
        deleteCommitLog();
        return true;
    default:
        break;
    }

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

658
659
660
        QStringList extraOptions;
        if (!commitEditor->committerInfo().isEmpty())
            extraOptions << QLatin1String("-u") << commitEditor->committerInfo();
cerf's avatar
cerf committed
661
662
        m_client->commit(m_submitRepository, files, editorFile->fileName(),
                         extraOptions);
663
664
665
    }
    return true;
}
cerf's avatar
cerf committed
666

667
668
669
670
671
672
673
674
void MercurialPlugin::deleteCommitLog()
{
    if (changeLog) {
        delete changeLog;
        changeLog = 0;
    }
}

675
void MercurialPlugin::createRepositoryManagementActions(const Core::Context &context)
676
677
678
679
680
681
682
683
684
685
686
{
    //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);
}

687
void MercurialPlugin::createLessUsedActions(const Core::Context &context)
688
689
690
691
692
693
{
    //TODO create menue for these options
    Q_UNUSED(context);
    return;
}

hjk's avatar
hjk committed
694
void MercurialPlugin::createSeparator(const Core::Context &context, const Core::Id &id)
695
696
697
698
699
700
{
    QAction *action = new QAction(this);
    action->setSeparator(true);
    mercurialContainer->addAction(actionManager->registerAction(action, id, context));
}

hjk's avatar
hjk committed
701
void MercurialPlugin::updateActions(VcsBasePlugin::ActionState as)
702
{
703
704
    if (!enableMenuAction(as, m_menuAction)) {
        m_commandLocator->setEnabled(false);
705
        return;
706
    }
707
708
    const QString filename = currentState().currentFileName();
    const bool repoEnabled = currentState().hasTopLevel();
709
    m_commandLocator->setEnabled(repoEnabled);
710
711
712
713

    annotateFile->setParameter(filename);
    diffFile->setParameter(filename);
    logFile->setParameter(filename);
714
715
    m_addAction->setParameter(filename);
    m_deleteAction->setParameter(filename);
716
717
718
    revertFile->setParameter(filename);
    statusFile->setParameter(filename);

719
720
    foreach (QAction *repoAction, m_repositoryActionList)
        repoAction->setEnabled(repoEnabled);
721
722
723
}

Q_EXPORT_PLUGIN(MercurialPlugin)