| 1 | #include "CreateRepoDlg.h" |
| 2 | #include "ui_CreateRepoDlg.h" |
| 3 | |
| 4 | #include <GitBase.h> |
| 5 | #include <GitConfig.h> |
| 6 | #include <GitQlientSettings.h> |
| 7 | #include <GitQlientStyles.h> |
| 8 | |
| 9 | #include <QFileDialog> |
| 10 | #include <QLogger.h> |
| 11 | #include <QMessageBox> |
| 12 | |
| 13 | using namespace QLogger; |
| 14 | |
| 15 | CreateRepoDlg::CreateRepoDlg(CreateRepoDlgType type, QSharedPointer<GitConfig> git, QWidget *parent) |
| 16 | : QDialog(parent) |
| 17 | , ui(new Ui::CreateRepoDlg) |
| 18 | , mType(type) |
| 19 | , mGit(git) |
| 20 | { |
| 21 | setStyleSheet(GitQlientStyles::getStyles()); |
| 22 | |
| 23 | ui->setupUi(this); |
| 24 | |
| 25 | if (mType == CreateRepoDlgType::INIT) |
| 26 | ui->leURL->setHidden(true); |
| 27 | |
| 28 | const auto operation = mType == CreateRepoDlgType::INIT ? QString("init" ) : QString("clone" ); |
| 29 | const auto checkText = ui->chbOpen->text().arg(operation); |
| 30 | ui->chbOpen->setText(checkText); |
| 31 | |
| 32 | GitQlientSettings settings; |
| 33 | const auto defaultLocation = settings.globalValue("DefaultCloneLocation" , QString()).toString(); |
| 34 | |
| 35 | if (!defaultLocation.isEmpty()) |
| 36 | ui->lePath->setText(defaultLocation); |
| 37 | |
| 38 | setWindowTitle(QString(tr("%1 repository" )) |
| 39 | .arg(mType == CreateRepoDlgType::INIT ? QString(tr("Initialize" )) : QString(tr("Clone" )))); |
| 40 | |
| 41 | connect(ui->leURL, &QLineEdit::returnPressed, this, &CreateRepoDlg::accept); |
| 42 | connect(ui->leURL, &QLineEdit::textChanged, this, &CreateRepoDlg::addDefaultName); |
| 43 | connect(ui->pbBrowse, &QPushButton::clicked, this, &CreateRepoDlg::selectFolder); |
| 44 | connect(ui->lePath, &QLineEdit::returnPressed, this, &CreateRepoDlg::accept); |
| 45 | connect(ui->leRepoName, &QLineEdit::returnPressed, this, &CreateRepoDlg::accept); |
| 46 | connect(ui->pbAccept, &QPushButton::clicked, this, &CreateRepoDlg::accept); |
| 47 | connect(ui->pbCancel, &QPushButton::clicked, this, &QDialog::reject); |
| 48 | connect(ui->cbGitUser, &CheckBox::clicked, this, &CreateRepoDlg::showGitControls); |
| 49 | |
| 50 | showGitControls(); |
| 51 | } |
| 52 | |
| 53 | CreateRepoDlg::~CreateRepoDlg() |
| 54 | { |
| 55 | delete ui; |
| 56 | } |
| 57 | |
| 58 | void CreateRepoDlg::selectFolder() |
| 59 | { |
| 60 | const QString dirName(QFileDialog::getExistingDirectory(this, "Choose the directory of a Git project" )); |
| 61 | |
| 62 | if (!dirName.isEmpty()) |
| 63 | { |
| 64 | QDir d(dirName); |
| 65 | |
| 66 | ui->lePath->setText(d.absolutePath()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void CreateRepoDlg::addDefaultName(const QString &url) |
| 71 | { |
| 72 | static const QString extension(".git" ); |
| 73 | if (url.endsWith(extension)) |
| 74 | { |
| 75 | const auto lastDashIndex = url.lastIndexOf("/" ); |
| 76 | const auto projectName = url.mid(lastDashIndex + 1, url.size() - lastDashIndex - extension.size() - 1); |
| 77 | |
| 78 | ui->leRepoName->setText(projectName); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void CreateRepoDlg::showGitControls() |
| 83 | { |
| 84 | const auto checkedState = ui->cbGitUser->isChecked(); |
| 85 | |
| 86 | ui->leGitName->setVisible(checkedState); |
| 87 | ui->leGitEmail->setVisible(checkedState); |
| 88 | } |
| 89 | |
| 90 | void CreateRepoDlg::accept() |
| 91 | { |
| 92 | auto path = ui->lePath->text().trimmed(); |
| 93 | auto repoName = ui->leRepoName->text().trimmed(); |
| 94 | |
| 95 | if (path.isEmpty() || repoName.isEmpty()) |
| 96 | { |
| 97 | const auto msg = QString(tr("You need to provider a repository name and a clone directory." )); |
| 98 | |
| 99 | QMessageBox::critical(this, tr("No Repo name provided" ), msg); |
| 100 | |
| 101 | QLog_Error("UI" , msg); |
| 102 | } |
| 103 | |
| 104 | if (!path.isEmpty() && !repoName.isEmpty()) |
| 105 | { |
| 106 | repoName.replace(" " , "\\ " ); |
| 107 | const auto fullPath = path.append("/" ).append(repoName); |
| 108 | |
| 109 | QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); |
| 110 | |
| 111 | GitExecResult ret; |
| 112 | QString actionApplied; |
| 113 | |
| 114 | if (mType == CreateRepoDlgType::CLONE) |
| 115 | { |
| 116 | const auto url = ui->leURL->text().trimmed(); |
| 117 | |
| 118 | if (!url.isEmpty()) |
| 119 | { |
| 120 | actionApplied = "clone" ; |
| 121 | |
| 122 | QDir dir(fullPath); |
| 123 | |
| 124 | if (!dir.exists()) |
| 125 | dir.mkpath(fullPath); |
| 126 | |
| 127 | ret = mGit->clone(url, fullPath); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | const auto msg = QString(tr("You need to provider a URL to clone a repository." )); |
| 132 | |
| 133 | QMessageBox::critical(this, tr("Nor URL provided" ), msg); |
| 134 | |
| 135 | QLog_Error("UI" , msg); |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | else if (mType == CreateRepoDlgType::INIT) |
| 140 | { |
| 141 | actionApplied = "init" ; |
| 142 | ret = mGit->initRepo(fullPath); |
| 143 | } |
| 144 | |
| 145 | QApplication::restoreOverrideCursor(); |
| 146 | |
| 147 | if (ret.success) |
| 148 | { |
| 149 | if (ui->chbDefaultDir->isChecked()) |
| 150 | { |
| 151 | GitQlientSettings settings; |
| 152 | settings.setGlobalValue("DefaultCloneLocation" , ui->lePath->text()); |
| 153 | } |
| 154 | |
| 155 | if (ui->cbGitUser->isChecked()) |
| 156 | mGit->setLocalUserInfo({ ui->leGitName->text().trimmed(), ui->leGitEmail->text().trimmed() }); |
| 157 | |
| 158 | if (ui->chbOpen->isChecked()) |
| 159 | emit signalOpenWhenFinish(fullPath); |
| 160 | |
| 161 | QDialog::accept(); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | QMessageBox::critical(this, tr("Error when %1" ).arg(actionApplied), ret.output); |
| 166 | |
| 167 | QLog_Error("UI" , ret.output); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |