File indexing completed on 2024-04-21 05:40:58

0001 /*
0002     SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "pulldialog.h"
0008 #include "gitwrapper.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 
0013 #include <QComboBox>
0014 #include <QDialogButtonBox>
0015 #include <QGroupBox>
0016 #include <QHBoxLayout>
0017 #include <QLabel>
0018 #include <QVBoxLayout>
0019 #include <QPushButton>
0020 
0021 PullDialog::PullDialog(QWidget* parent):
0022     QDialog(parent, Qt::Dialog)
0023 {
0024     this->setWindowTitle(xi18nc("@title:window", "<application>Git</application> Pull"));
0025     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
0026     QWidget *mainWidget = new QWidget(this);
0027     QVBoxLayout *mainLayout = new QVBoxLayout;
0028     this->setLayout(mainLayout);
0029     mainLayout->addWidget(mainWidget);
0030     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
0031     okButton->setDefault(true);
0032     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0033     this->connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0034     this->connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0035     okButton->setText(i18nc("@action:button", "Pull"));
0036 
0037     QWidget * boxWidget = new QWidget(this);
0038     QVBoxLayout * boxLayout = new QVBoxLayout(boxWidget);
0039     mainLayout->addWidget(boxWidget);
0040 
0041     QGroupBox * sourceGroupBox = new QGroupBox(boxWidget);
0042     mainLayout->addWidget(sourceGroupBox);
0043     boxLayout->addWidget(sourceGroupBox);
0044     sourceGroupBox->setTitle(i18nc("@title:group The source to pull from", "Source"));
0045     QHBoxLayout * sourceHBox = new QHBoxLayout(sourceGroupBox);
0046     sourceGroupBox->setLayout(sourceHBox);
0047 
0048     mainLayout->addWidget(m_buttonBox);
0049 
0050     QLabel * remoteLabel = new QLabel(i18nc("@label:listbox a git remote", "Remote:"), sourceGroupBox);
0051     sourceHBox->addWidget(remoteLabel);
0052     m_remoteComboBox = new QComboBox(sourceGroupBox);
0053     sourceHBox->addWidget(m_remoteComboBox);
0054 
0055     QLabel * remoteBranchLabel = new QLabel(i18nc("@label:listbox", "Remote branch:"), sourceGroupBox);
0056     sourceHBox->addWidget(remoteBranchLabel);
0057     m_remoteBranchComboBox = new QComboBox(sourceGroupBox);
0058     sourceHBox->addWidget(m_remoteBranchComboBox);
0059 
0060     //populate UI
0061     GitWrapper * gitWrapper = GitWrapper::instance();
0062 
0063     //get sources
0064     m_remoteComboBox->addItems(gitWrapper->pullRemotes());
0065 
0066     //get branch names
0067     int currentBranchIndex;
0068     const QStringList branches = gitWrapper->branches(&currentBranchIndex);
0069 
0070     for (const QString& branch : branches) {
0071         if (branch.startsWith(QLatin1String("remotes/"))) {
0072             const QString remote = branch.section(QLatin1Char('/'), 1, 1);
0073             const QString name = branch.section(QLatin1Char('/'), 2);
0074             m_remoteBranches[remote] << name;
0075         }
0076     }
0077     remoteSelectionChanged(m_remoteComboBox->currentText());
0078     if (currentBranchIndex >= 0) {
0079         const int index = m_remoteBranchComboBox->findText(branches.at(currentBranchIndex));
0080         if (index != -1) {
0081             m_remoteBranchComboBox->setCurrentIndex(index);
0082         }
0083     }
0084 
0085     //Signals
0086     connect(m_remoteComboBox, SIGNAL(currentIndexChanged(QString)),
0087             this, SLOT(remoteSelectionChanged(QString)));
0088 }
0089 
0090 QString PullDialog::source() const
0091 {
0092     return m_remoteComboBox->currentText();
0093 }
0094 
0095 QString PullDialog::remoteBranch() const
0096 {
0097     return m_remoteBranchComboBox->currentText();
0098 }
0099 
0100 void PullDialog::remoteSelectionChanged(const QString& newRemote)
0101 {
0102     m_remoteBranchComboBox->clear();
0103     m_remoteBranchComboBox->addItems(m_remoteBranches.value(newRemote));
0104     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
0105     okButton->setEnabled(m_remoteBranchComboBox->count() > 0);
0106 }
0107 
0108 
0109 #include "moc_pulldialog.cpp"