File indexing completed on 2024-04-21 05:41:03

0001 /*
0002     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "updatedialog.h"
0008 #include "hgwrapper.h"
0009 
0010 #include <QLabel>
0011 #include <QGroupBox>
0012 #include <QHBoxLayout>
0013 #include <QVBoxLayout>
0014 #include <QCheckBox>
0015 #include <KComboBox>
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 
0019 
0020 HgUpdateDialog::HgUpdateDialog(QWidget *parent):
0021     DialogBase(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, parent)
0022 {
0023     // dialog properties
0024     this->setWindowTitle(xi18nc("@title:window",
0025                 "<application>Hg</application> Update"));
0026 
0027     this->okButton()->setText(xi18nc("@action:button", "Update"));
0028 
0029     // UI 
0030     QGroupBox *selectGroup = new QGroupBox(i18n("New working directory"));
0031     QVBoxLayout *selectLayout = new QVBoxLayout;
0032     m_selectType = new KComboBox;
0033     m_selectFinal = new KComboBox;
0034     m_selectType->addItem(i18n("Branch"));
0035     m_selectType->addItem(i18n("Tag"));
0036     m_selectType->addItem(i18n("Changeset/Revision"));
0037     selectLayout->addWidget(m_selectType);
0038     selectLayout->addWidget(m_selectFinal);
0039     selectGroup->setLayout(selectLayout);
0040 
0041     QGroupBox *infoGroup = new QGroupBox(i18n("Current Parent"));
0042     QVBoxLayout *infoLayout = new QVBoxLayout;
0043     m_currentInfo = new QLabel;
0044     infoLayout->addWidget(m_currentInfo);
0045     infoGroup->setLayout(infoLayout);
0046 
0047     QGroupBox *optionGroup = new QGroupBox(i18n("Options"));
0048     QVBoxLayout *optionLayout = new QVBoxLayout;
0049     m_discardChanges = new QCheckBox(i18n("Discard uncommitted changes"));
0050     m_discardChanges->setCheckState(Qt::Unchecked);
0051     optionLayout->addWidget(m_discardChanges);
0052     optionGroup->setLayout(optionLayout);
0053 
0054     QVBoxLayout *mainLayout = new QVBoxLayout;
0055     mainLayout->addWidget(infoGroup);
0056     mainLayout->addWidget(selectGroup);
0057     mainLayout->addWidget(optionGroup);
0058 
0059     slotUpdateDialog(0);
0060     layout()->insertLayout(0, mainLayout);
0061 
0062     // connections
0063     connect(m_selectType, SIGNAL(currentIndexChanged(int)), this,
0064             SLOT(slotUpdateDialog(int)));
0065 }
0066 
0067 void HgUpdateDialog::slotUpdateDialog(int index)
0068 {
0069     HgWrapper *hgWrapper = HgWrapper::instance();
0070     m_selectFinal->clear();
0071     if (index == 0) {
0072         m_updateTo = ToBranch;
0073         m_selectFinal->setEditable(false);
0074         m_selectFinal->addItems(hgWrapper->getBranches());
0075     }
0076     else if (index == 1) {
0077         m_updateTo = ToTag;
0078         m_selectFinal->setEditable(false);
0079         m_selectFinal->addItems(hgWrapper->getTags());
0080     }
0081     else if (index == 2) {
0082         m_updateTo = ToRevision;
0083         m_selectFinal->setEditable(true);
0084     }
0085     m_selectFinal->setFocus();
0086 
0087     /// get parents of current working directory
0088     /// more precise information using 'hg summary'
0089     /// but no proper way to retrieve needed data
0090     QString output;
0091     QStringList args;
0092     args << QLatin1String("--template");
0093     args << QLatin1String("{rev}:{node|short} ({branch})\n");
0094     hgWrapper->executeCommand(QLatin1String("parents"), args, output);
0095     output.replace(QLatin1String("\n"), QLatin1String("<br/>"));
0096     if (output.contains(QLatin1String("()"))) {
0097         output.replace(QLatin1String("()"), QLatin1String("(default)"));
0098     }
0099     m_currentInfo->setText(output);
0100 }
0101 
0102 void HgUpdateDialog::done(int r)
0103 {
0104     if (r == QDialog::Accepted) {
0105         QStringList args;
0106         // Should we discard uncommitted changes
0107         if (m_discardChanges->checkState() == Qt::Checked) {
0108             args << QStringLiteral("-C");
0109         }
0110         else {
0111             args << QStringLiteral("-c");
0112         }
0113         if (m_updateTo == ToRevision) {
0114             args << QStringLiteral("-r");
0115         }
0116 
0117         // update to
0118         args << m_selectFinal->currentText();
0119 
0120         // execute mercurial command
0121         HgWrapper *hgw = HgWrapper::instance();
0122         if (hgw->executeCommandTillFinished(QStringLiteral("update"), args)) {
0123             QDialog::done(r);
0124         }
0125         else {
0126             KMessageBox::error(this, i18n("Some error occurred! "
0127                         "\nMaybe there are uncommitted changes."));
0128         }
0129     }
0130     else {
0131         QDialog::done(r);
0132     }
0133 }
0134 
0135 
0136 #include "moc_updatedialog.cpp"