1#include "AddSubtreeDlg.h"
2#include "ui_AddSubtreeDlg.h"
3
4#include <GitQlientStyles.h>
5#include <GitSubtree.h>
6#include <QLogger.h>
7
8#include <QMessageBox>
9
10using namespace QLogger;
11
12AddSubtreeDlg::AddSubtreeDlg(const QSharedPointer<GitBase> &git, QWidget *parent)
13 : QDialog(parent)
14 , ui(new Ui::AddSubtreeDlg)
15 , mGit(git)
16{
17 setStyleSheet(GitQlientStyles::getStyles());
18
19 ui->setupUi(this);
20
21 connect(ui->lePath, &QLineEdit::returnPressed, this, &AddSubtreeDlg::accept);
22 connect(ui->leUrl, &QLineEdit::returnPressed, this, &AddSubtreeDlg::accept);
23 connect(ui->leUrl, &QLineEdit::editingFinished, this, &AddSubtreeDlg::proposeName);
24 connect(ui->pbAccept, &QPushButton::clicked, this, &AddSubtreeDlg::accept);
25 connect(ui->pbCancel, &QPushButton::clicked, this, &QDialog::reject);
26}
27
28AddSubtreeDlg::AddSubtreeDlg(const QString &prefix, const QSharedPointer<GitBase> &git, QWidget *parent)
29 : AddSubtreeDlg(git, parent)
30{
31 disconnect(ui->leUrl, &QLineEdit::editingFinished, this, &AddSubtreeDlg::proposeName);
32
33 ui->lePath->setText(prefix);
34 ui->lePath->setReadOnly(true);
35 ui->chSquash->setVisible(false);
36
37 setWindowTitle(tr("Configure subtree"));
38}
39
40AddSubtreeDlg::AddSubtreeDlg(const QString &prefix, const QString &url, const QString &reference,
41 const QSharedPointer<GitBase> &git, QWidget *parent)
42 : AddSubtreeDlg(prefix, git, parent)
43{
44 disconnect(ui->leUrl, &QLineEdit::editingFinished, this, &AddSubtreeDlg::proposeName);
45
46 ui->leReference->setText(reference);
47 ui->leUrl->setText(url);
48}
49
50AddSubtreeDlg::~AddSubtreeDlg()
51{
52 delete ui;
53}
54
55void AddSubtreeDlg::accept()
56{
57 const auto subtreeName = ui->lePath->text();
58 const auto subtreeUrl = ui->leUrl->text();
59 const auto subtreeRef = ui->leReference->text();
60
61 QScopedPointer<GitSubtree> git(new GitSubtree(mGit));
62
63 if (subtreeName.isEmpty() || subtreeUrl.isEmpty() || subtreeRef.isEmpty())
64 {
65 QMessageBox::warning(
66 this, tr("Invalid fields"),
67 tr("The information provided is incorrect. Please fix the URL and/or the name and submit again."));
68 }
69 else
70 {
71 const auto ret = git->add(subtreeUrl, subtreeRef, subtreeName, ui->chSquash->isChecked());
72
73 if (ret.success)
74 QDialog::accept();
75 else
76 QMessageBox::warning(this, tr("Error when adding a subtree."), ret.output);
77 }
78}
79
80void AddSubtreeDlg::proposeName()
81{
82 auto url = ui->leUrl->text();
83 QString proposedName;
84
85 if (url.startsWith("https"))
86 {
87 url.remove("https://");
88 const auto fields = url.split("/");
89
90 if (fields.count() > 1)
91 {
92 proposedName = fields.at(2);
93 proposedName = proposedName.split(".").constFirst();
94 }
95 }
96 else if (url.contains("@"))
97 {
98 const auto fields = url.split(":");
99
100 if (fields.count() > 0)
101 {
102 proposedName = fields.constLast().split("/").constLast();
103 proposedName = proposedName.split(".").constFirst();
104 }
105 }
106
107 ui->lePath->setText(proposedName);
108}
109