1#include "AddRemoteDlg.h"
2#include "ui_AddSubmoduleDlg.h"
3
4#include <GitRemote.h>
5#include <GitQlientStyles.h>
6
7#include <QMessageBox>
8
9AddRemoteDlg::AddRemoteDlg(const QSharedPointer<GitBase> &git, QWidget *parent)
10 : QDialog(parent)
11 , ui(new Ui::AddSubmoduleDlg)
12 , mGit(git)
13{
14 setStyleSheet(GitQlientStyles::getStyles());
15
16 ui->setupUi(this);
17
18 setWindowTitle("Add remote repository");
19
20 connect(ui->lePath, &QLineEdit::returnPressed, this, &AddRemoteDlg::accept);
21 connect(ui->leUrl, &QLineEdit::returnPressed, this, &AddRemoteDlg::accept);
22 connect(ui->leUrl, &QLineEdit::editingFinished, this, &AddRemoteDlg::proposeName);
23 connect(ui->pbAccept, &QPushButton::clicked, this, &AddRemoteDlg::accept);
24 connect(ui->pbCancel, &QPushButton::clicked, this, &QDialog::reject);
25}
26
27AddRemoteDlg::~AddRemoteDlg()
28{
29 delete ui;
30}
31
32void AddRemoteDlg::accept()
33{
34 const auto remoteName = ui->lePath->text();
35 const auto remoteUrl = ui->leUrl->text();
36
37 QScopedPointer<GitRemote> git(new GitRemote(mGit));
38
39 if (remoteName.isEmpty() || remoteUrl.isEmpty())
40 {
41 QMessageBox::warning(
42 this, tr("Invalid fields"),
43 tr("The information provided is incorrect. Please fix the URL and/or the name and submit again."));
44 }
45 else if (const auto ret = git->addRemote(remoteUrl, remoteName); ret.success)
46 {
47 git->fetch();
48
49 QDialog::accept();
50 }
51}
52
53void AddRemoteDlg::proposeName()
54{
55 auto url = ui->leUrl->text();
56 QString proposedName;
57
58 if (url.startsWith("https"))
59 {
60 url.remove("https://");
61 const auto fields = url.split("/");
62
63 if (fields.count() > 1)
64 proposedName = fields.at(1);
65 }
66 else if (url.contains("@"))
67 {
68 const auto fields = url.split(":");
69
70 if (fields.count() > 0)
71 proposedName = fields.constFirst();
72 }
73
74 ui->lePath->setText(proposedName);
75}
76