File indexing completed on 2024-04-28 09:36:07

0001 /*
0002     SPDX-FileCopyrightText: 2011 Vishesh Yadav <vishesh3y@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "bundledialog.h"
0008 #include "commitinfowidget.h"
0009 #include "pathselector.h"
0010 #include "fileviewhgpluginsettings.h"
0011 #include "hgwrapper.h"
0012 
0013 #include <QWidget>
0014 #include <QGroupBox>
0015 #include <QCheckBox>
0016 #include <QGridLayout>
0017 #include <QVBoxLayout>
0018 #include <QListWidgetItem>
0019 #include <QLabel>
0020 #include <QTextCodec>
0021 #include <QProcess>
0022 #include <QFileDialog>
0023 #include <QLineEdit>
0024 #include <KMessageBox>
0025 #include <KLocalizedString>
0026 
0027 HgBundleDialog::HgBundleDialog(QWidget *parent) :
0028     DialogBase(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, parent)
0029 {
0030     this->setWindowTitle(xi18nc("@title:window",
0031                 "<application>Hg</application> Bundle"));
0032     okButton()->setText(xi18nc("@action:button", "Bundle"));
0033 
0034     // Load saved settings
0035     FileViewHgPluginSettings *settings = FileViewHgPluginSettings::self();
0036     this->resize(QSize(settings->bundleDialogWidth(),
0037                                settings->bundleDialogHeight()));
0038     //
0039     setupUI();
0040 
0041     // connections
0042     connect(this, SIGNAL(finished(int)), this, SLOT(saveGeometry()));
0043     connect(m_selectCommitButton, &QAbstractButton::clicked,
0044             this, &HgBundleDialog::slotSelectChangeset);
0045     connect(m_allChangesets, &QCheckBox::stateChanged,
0046             this, &HgBundleDialog::slotAllChangesCheckToggled);
0047 }
0048 
0049 void HgBundleDialog::setupUI()
0050 {
0051     QVBoxLayout *mainLayout = new QVBoxLayout;
0052 
0053     // main 
0054     m_pathSelect = new HgPathSelector;
0055     m_baseRevision = new QLineEdit;
0056     m_selectCommitButton = new QPushButton(xi18nc("@label:button",
0057                                 "Select Changeset"));
0058     QLabel *baseRevisionLabel = new QLabel(xi18nc("@label",
0059                 "Base Revision (optional): "));
0060     m_allChangesets = new QCheckBox(xi18nc("@label",
0061                             "Bundle all changesets in repository."));
0062 
0063     QGridLayout *bodyLayout = new QGridLayout;
0064     bodyLayout->addWidget(m_pathSelect, 0, 0, 2, 0);
0065     bodyLayout->addWidget(baseRevisionLabel, 2, 0);
0066     bodyLayout->addWidget(m_baseRevision, 2, 1);
0067     bodyLayout->addWidget(m_selectCommitButton, 2, 2);
0068     bodyLayout->addWidget(m_allChangesets, 3, 0, 2, 0);
0069 
0070     m_mainGroup = new QGroupBox;
0071     m_mainGroup->setLayout(bodyLayout);
0072 
0073     mainLayout->addWidget(m_mainGroup);
0074 
0075     // options
0076     m_optionGroup = new QGroupBox(xi18nc("@label:group", "Options"));
0077     m_optForce = new QCheckBox(xi18nc("@label:checkbox",
0078                                      "Run even when the destination is "
0079                                      "unrelated (force)"));
0080     m_optInsecure = new QCheckBox(xi18nc("@label:checkbox",
0081                              "Do not verify server certificate"));
0082     
0083     QVBoxLayout *optionLayout = new QVBoxLayout;
0084     optionLayout->addWidget(m_optForce);
0085     optionLayout->addWidget(m_optInsecure);
0086     m_optionGroup->setLayout(optionLayout);
0087 
0088     mainLayout->addWidget(m_optionGroup);
0089     //end options
0090 
0091     layout()->insertLayout(0, mainLayout);
0092 }
0093 
0094 void HgBundleDialog::done(int r)
0095 {
0096     if (r == QDialog::Accepted) {
0097         QString result = QFileDialog::getSaveFileName(this);
0098         if (result.length() > 0) {
0099             createBundle(result);
0100             QDialog::done(r);
0101         }
0102     }
0103     else {
0104         QDialog::done(r);
0105     }
0106 }
0107 
0108 void HgBundleDialog::createBundle(const QString &fileName)
0109 {
0110     HgWrapper *hgw = HgWrapper::instance();
0111     QStringList args;
0112     
0113     if (m_allChangesets->checkState() == Qt::Checked) {
0114         args << QLatin1String("--all");
0115     }
0116     else {
0117         if (m_baseRevision->text().trimmed().length() > 0) {
0118             args << QLatin1String("--base");
0119             args << m_baseRevision->text().trimmed();
0120         }
0121     }
0122 
0123     if (m_optForce->checkState() == Qt::Checked) {
0124         args << QLatin1String("--force");
0125     }
0126     if (m_optInsecure->checkState() == Qt::Checked) {
0127         args << QLatin1String("--insecure");
0128     }
0129     
0130     args << fileName;
0131     args << m_pathSelect->remote();
0132 
0133     hgw->executeCommand(QLatin1String("bundle"), args);
0134 }
0135 
0136 void HgBundleDialog::saveGeometry()
0137 {
0138     FileViewHgPluginSettings *settings = FileViewHgPluginSettings::self();
0139     settings->setBundleDialogHeight(this->height());
0140     settings->setBundleDialogWidth(this->width());
0141     settings->save();
0142 }
0143 
0144 void HgBundleDialog::loadCommits()
0145 {
0146     HgWrapper *hgWrapper = HgWrapper::instance();
0147 
0148     // update heads list
0149     QProcess process;
0150     process.setWorkingDirectory(hgWrapper->getBaseDir());
0151 
0152     QStringList args;
0153     args << QLatin1String("log");
0154     args << QLatin1String("--template");
0155     args << QLatin1String("{rev}\n{node|short}\n{branch}\n"
0156                           "{author}\n{desc|firstline}\n");
0157 
0158     process.start(QLatin1String("hg"), args);
0159     process.waitForFinished();
0160     m_commitInfo->clear();
0161 
0162     const int FINAL = 5;
0163     char buffer[FINAL][1024];
0164     int count = 0;
0165     while (process.readLine(buffer[count], sizeof(buffer[count])) > 0) {
0166         if (count == FINAL - 1) {
0167             QString rev = QTextCodec::codecForLocale()->toUnicode(buffer[0]).trimmed();
0168             QString changeset = QTextCodec::codecForLocale()->toUnicode(buffer[1]).trimmed();
0169             QString branch = QTextCodec::codecForLocale()->toUnicode(buffer[2]).trimmed();
0170             QString author = QTextCodec::codecForLocale()->toUnicode(buffer[3]).trimmed();
0171             QString log = QTextCodec::codecForLocale()->toUnicode(buffer[4]).trimmed();
0172 
0173             QListWidgetItem *item = new QListWidgetItem;
0174             item->setData(Qt::DisplayRole, changeset);
0175             item->setData(Qt::UserRole + 1, rev);
0176             item->setData(Qt::UserRole + 2, branch);
0177             item->setData(Qt::UserRole + 3, author);
0178             item->setData(Qt::UserRole + 4, log);
0179             m_commitInfo->addItem(item);
0180         }
0181         count = (count + 1)%FINAL;
0182     }
0183 }
0184 
0185 void HgBundleDialog::slotSelectChangeset()
0186 {
0187     DialogBase diag(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0188     diag.setWindowTitle(xi18nc("@title:window",
0189                 "Select Changeset"));
0190     diag.okButton()->setText(xi18nc("@action:button", "Select"));
0191 
0192     diag.setMinimumWidth(700);
0193     
0194     m_commitInfo = new HgCommitInfoWidget;
0195     loadCommits();
0196     diag.layout()->insertWidget(0, m_commitInfo);
0197     
0198     if (diag.exec() == QDialog::Accepted) {
0199         m_baseRevision->setText(m_commitInfo->selectedChangeset());
0200     }
0201 }
0202 
0203 void HgBundleDialog::slotAllChangesCheckToggled(int state)
0204 {
0205     if (state == Qt::Checked) {
0206         m_selectCommitButton->setEnabled(false);
0207         m_baseRevision->setEnabled(false);
0208     }
0209     else {
0210         m_selectCommitButton->setEnabled(true);
0211         m_baseRevision->setEnabled(true);
0212     }
0213 }
0214 
0215 
0216 
0217 #include "moc_bundledialog.cpp"