1 /*
2     SPDX-FileCopyrightText: 2010 Sebastian Doerner <sebastian@sebastian-doerner.de>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "pulldialog.h"
8 #include "gitwrapper.h"
9 
10 #include <KConfigGroup>
11 #include <KLocalizedString>
12 
13 #include <QComboBox>
14 #include <QDialogButtonBox>
15 #include <QGroupBox>
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QVBoxLayout>
19 #include <QPushButton>
20 
PullDialog(QWidget * parent)21 PullDialog::PullDialog(QWidget* parent):
22     QDialog(parent, Qt::Dialog)
23 {
24     this->setWindowTitle(xi18nc("@title:window", "<application>Git</application> Pull"));
25     m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
26     QWidget *mainWidget = new QWidget(this);
27     QVBoxLayout *mainLayout = new QVBoxLayout;
28     this->setLayout(mainLayout);
29     mainLayout->addWidget(mainWidget);
30     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
31     okButton->setDefault(true);
32     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
33     this->connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
34     this->connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
35     okButton->setText(i18nc("@action:button", "Pull"));
36 
37     QWidget * boxWidget = new QWidget(this);
38     QVBoxLayout * boxLayout = new QVBoxLayout(boxWidget);
39     mainLayout->addWidget(boxWidget);
40 
41     QGroupBox * sourceGroupBox = new QGroupBox(boxWidget);
42     mainLayout->addWidget(sourceGroupBox);
43     boxLayout->addWidget(sourceGroupBox);
44     sourceGroupBox->setTitle(i18nc("@title:group The source to pull from", "Source"));
45     QHBoxLayout * sourceHBox = new QHBoxLayout(sourceGroupBox);
46     sourceGroupBox->setLayout(sourceHBox);
47 
48     mainLayout->addWidget(m_buttonBox);
49 
50     QLabel * remoteLabel = new QLabel(i18nc("@label:listbox a git remote", "Remote:"), sourceGroupBox);
51     sourceHBox->addWidget(remoteLabel);
52     m_remoteComboBox = new QComboBox(sourceGroupBox);
53     sourceHBox->addWidget(m_remoteComboBox);
54 
55     QLabel * remoteBranchLabel = new QLabel(i18nc("@label:listbox", "Remote branch:"), sourceGroupBox);
56     sourceHBox->addWidget(remoteBranchLabel);
57     m_remoteBranchComboBox = new QComboBox(sourceGroupBox);
58     sourceHBox->addWidget(m_remoteBranchComboBox);
59 
60     //populate UI
61     GitWrapper * gitWrapper = GitWrapper::instance();
62 
63     //get sources
64     m_remoteComboBox->addItems(gitWrapper->pullRemotes());
65 
66     //get branch names
67     int currentBranchIndex;
68     const QStringList branches = gitWrapper->branches(&currentBranchIndex);
69 
70     for (const QString& branch : branches) {
71         if (branch.startsWith(QLatin1String("remotes/"))) {
72             const QString remote = branch.section('/', 1, 1);
73             const QString name = branch.section('/', 2);
74             m_remoteBranches[remote] << name;
75         }
76     }
77     remoteSelectionChanged(m_remoteComboBox->currentText());
78     if (currentBranchIndex >= 0) {
79         const int index = m_remoteBranchComboBox->findText(branches.at(currentBranchIndex));
80         if (index != -1) {
81             m_remoteBranchComboBox->setCurrentIndex(index);
82         }
83     }
84 
85     //Signals
86     connect(m_remoteComboBox, SIGNAL(currentIndexChanged(QString)),
87             this, SLOT(remoteSelectionChanged(QString)));
88 }
89 
source() const90 QString PullDialog::source() const
91 {
92     return m_remoteComboBox->currentText();
93 }
94 
remoteBranch() const95 QString PullDialog::remoteBranch() const
96 {
97     return m_remoteBranchComboBox->currentText();
98 }
99 
remoteSelectionChanged(const QString & newRemote)100 void PullDialog::remoteSelectionChanged(const QString& newRemote)
101 {
102     m_remoteBranchComboBox->clear();
103     m_remoteBranchComboBox->addItems(m_remoteBranches.value(newRemote));
104     QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok);
105     okButton->setEnabled(m_remoteBranchComboBox->count() > 0);
106 }
107 
108