File indexing completed on 2025-01-05 05:14:48

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "clonedialog.h"
0008 
0009 #include <KLocalizedString>
0010 #include <QMessageBox>
0011 #include <QPointer>
0012 #include <QSettings>
0013 #include <QStandardPaths>
0014 #include <dialogs/credentialdialog.h>
0015 #include <gitmanager.h>
0016 #include <observers/cloneobserver.h>
0017 #include <observers/fetchobserver.h>
0018 
0019 CloneDialog::CloneDialog(QWidget *parent)
0020     : AppDialog(parent)
0021     , mFixedPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation))
0022     , mCloneObserver{new Git::CloneObserver}
0023 {
0024     setupUi(this);
0025     loadSettings();
0026     connect(buttonBox, &QDialogButtonBox::accepted, this, &CloneDialog::slotAccepted);
0027     connect(lineEditUrl, &QLineEdit::textChanged, this, &CloneDialog::slotUrlChanged);
0028 
0029     connect(mCloneObserver, &Git::CloneObserver::totalObjectsChanged, progressBar, &QProgressBar::setMaximum);
0030     connect(mCloneObserver, &Git::CloneObserver::receivedObjectsChanged, progressBar, &QProgressBar::setValue);
0031     connect(mCloneObserver, &Git::CloneObserver::credentialRequeted, this, &CloneDialog::slotCredentialRequeted);
0032     connect(mCloneObserver, &Git::CloneObserver::message, labelMessage, &QLabel::setText);
0033 
0034     stackedWidget->setCurrentIndex(0);
0035 }
0036 
0037 CloneDialog::~CloneDialog() = default;
0038 
0039 void CloneDialog::loadSettings()
0040 {
0041     QSettings s;
0042     lineEditUrl->setText(s.value(QStringLiteral("lastClonedRepo")).toString());
0043 }
0044 
0045 Git::CloneCommand *CloneDialog::command()
0046 {
0047     auto cmd = new Git::CloneCommand(this);
0048 
0049     cmd->setRepoUrl(lineEditUrl->text());
0050     cmd->setLocalPath(lineEditPath->text());
0051     if (checkBoxBranch->isChecked())
0052         cmd->setBranch(lineEditBranch->text());
0053     if (checkBoxDepth->isChecked())
0054         cmd->setDepth(spinBoxDepth->value());
0055     cmd->setRecursive(checkBoxRecursive->isChecked());
0056     return cmd;
0057 }
0058 
0059 void CloneDialog::setLocalPath(const QString &path)
0060 {
0061     if (path.endsWith(QLatin1Char('/')))
0062         mFixedPath = path.mid(0, path.length() - 1);
0063     else
0064         mFixedPath = path;
0065     lineEditPath->setText(path);
0066     slotUrlChanged(lineEditUrl->text());
0067 }
0068 
0069 void CloneDialog::slotCredentialRequeted(const QString &url, Git::Credential *cred)
0070 {
0071     CredentialDialog d;
0072     d.setUsername(cred->username());
0073     d.setUrl(url);
0074     if (d.exec() == QDialog::Accepted) {
0075         cred->setUsername(d.username());
0076         cred->setPassword(d.password());
0077     }
0078 }
0079 
0080 void CloneDialog::slotUrlChanged(const QString &text)
0081 {
0082     const auto parts = text.split(QLatin1Char('/'));
0083     if (!parts.isEmpty()) {
0084         auto local = parts.last();
0085         if (local.endsWith(QStringLiteral(".git"), Qt::CaseInsensitive))
0086             local = local.mid(0, local.size() - 4);
0087         else
0088             local = local.replace(QStringLiteral("."), QStringLiteral("_"));
0089         lineEditPath->setText(mFixedPath + QLatin1Char('/') + local);
0090     }
0091 }
0092 
0093 void CloneDialog::slotAccepted()
0094 {
0095     QSettings s;
0096     s.setValue(QStringLiteral("lastClonedRepo"), lineEditUrl->text());
0097 
0098     accept();
0099 
0100     // stackedWidget->setCurrentIndex(1);
0101     // QPointer<Git::Manager> git{new Git::Manager};
0102 
0103     // auto ok = git->clone(lineEditUrl->text(), lineEditPath->text(), mCloneObserver);
0104     // if (!ok)
0105     //     QMessageBox::warning(this, tr("Clone"), git->errorMessage());
0106 }
0107 
0108 #include "moc_clonedialog.cpp"