1 | #include "gitremotedialog.h" |
2 | #include "ui_gitremotedialog.h" |
3 | #include "gitmanager.h" |
4 | #include "../iconsmanager.h" |
5 | #include "../widgets/infomessagebox.h" |
6 | |
7 | GitRemoteDialog::GitRemoteDialog(const QString& folder, QWidget *parent) : |
8 | QDialog(parent), |
9 | ui(new Ui::GitRemoteDialog), |
10 | mFolder(folder), |
11 | mChooseMode(false) |
12 | { |
13 | ui->setupUi(this); |
14 | GitManager manager; |
15 | mRemotes = manager.listRemotes(folder); |
16 | ui->lstRemotes->addItems(mRemotes); |
17 | connect(pIconsManager, &IconsManager::actionIconsUpdated, |
18 | this, &GitRemoteDialog::onUpdateIcons); |
19 | ui->btnRemove->setEnabled(false); |
20 | ui->pnlProcess->setVisible(false); |
21 | ui->grpDetail->setEnabled(false); |
22 | connect(ui->lstRemotes->selectionModel(), |
23 | &QItemSelectionModel::selectionChanged, |
24 | this, |
25 | &GitRemoteDialog::onRemotesSelectionChanged); |
26 | } |
27 | |
28 | GitRemoteDialog::~GitRemoteDialog() |
29 | { |
30 | delete ui; |
31 | } |
32 | |
33 | QString GitRemoteDialog::chooseRemote() |
34 | { |
35 | mChooseMode = true; |
36 | ui->btnClose->setText(tr("Ok" )); |
37 | |
38 | if (exec()==QDialog::Accepted) { |
39 | if (ui->lstRemotes->selectedItems().count()>0) |
40 | return ui->lstRemotes->selectedItems()[0]->text(); |
41 | } |
42 | return "" ; |
43 | } |
44 | |
45 | void GitRemoteDialog::onUpdateIcons() |
46 | { |
47 | ui->btnAdd->setIcon(pIconsManager->getIcon(IconsManager::ACTION_MISC_ADD)); |
48 | ui->btnRemove->setIcon(pIconsManager->getIcon(IconsManager::ACTION_MISC_REMOVE)); |
49 | } |
50 | |
51 | void GitRemoteDialog::onRemotesSelectionChanged() |
52 | { |
53 | bool enabled=(ui->lstRemotes->selectedItems().count()>0); |
54 | ui->btnRemove->setEnabled(enabled); |
55 | ui->pnlProcess->setVisible(enabled); |
56 | ui->grpDetail->setEnabled(enabled); |
57 | if (enabled) { |
58 | QString remoteName = ui->lstRemotes->selectedItems()[0]->text(); |
59 | GitManager manager; |
60 | QString remoteURL = manager.getRemoteURL(mFolder,remoteName); |
61 | ui->txtName->setText(remoteName); |
62 | ui->txtURL->setText(remoteURL); |
63 | ui->btnProcess->setText(tr("Update" )); |
64 | } |
65 | } |
66 | |
67 | void GitRemoteDialog::checkDetails() |
68 | { |
69 | if (ui->txtURL->text().isEmpty()) { |
70 | ui->btnProcess->setEnabled(false); |
71 | return; |
72 | } |
73 | |
74 | if (ui->txtName->text().isEmpty()) { |
75 | ui->btnProcess->setEnabled(false); |
76 | return; |
77 | } |
78 | |
79 | if (ui->btnProcess->text() == tr("Add" )) { |
80 | ui->btnProcess->setEnabled(!mRemotes.contains(ui->txtName->text())); |
81 | } else { |
82 | if (ui->lstRemotes->selectedItems().count()>0) { |
83 | QString remoteName = ui->lstRemotes->selectedItems()[0]->text(); |
84 | ui->btnProcess->setEnabled(ui->txtName->text()==remoteName |
85 | || !mRemotes.contains(ui->txtName->text()) ); |
86 | } else |
87 | ui->btnProcess->setEnabled(false); |
88 | } |
89 | } |
90 | |
91 | void GitRemoteDialog::on_btnAdd_clicked() |
92 | { |
93 | ui->grpDetail->setEnabled(true); |
94 | ui->pnlProcess->setVisible(true); |
95 | ui->btnProcess->setText(tr("Add" )); |
96 | ui->btnRemove->setEnabled(false); |
97 | if (ui->lstRemotes->count()==0) { |
98 | ui->txtName->setText("origin" ); |
99 | ui->txtURL->setFocus(); |
100 | } else |
101 | ui->txtName->setFocus(); |
102 | } |
103 | |
104 | void GitRemoteDialog::on_btnRemove_clicked() |
105 | { |
106 | if (ui->lstRemotes->selectedItems().count()>0) { |
107 | QString remoteName = ui->lstRemotes->selectedItems()[0]->text(); |
108 | GitManager manager; |
109 | QString output; |
110 | if (!manager.removeRemote(mFolder,remoteName,output)) { |
111 | InfoMessageBox infoBox; |
112 | infoBox.showMessage(output); |
113 | } else { |
114 | refresh(); |
115 | } |
116 | } |
117 | } |
118 | |
119 | void GitRemoteDialog::refresh() |
120 | { |
121 | ui->txtName->setText("" ); |
122 | ui->txtURL->setText("" ); |
123 | ui->lstRemotes->clear(); |
124 | GitManager manager; |
125 | mRemotes = manager.listRemotes(mFolder); |
126 | ui->lstRemotes->addItems(mRemotes); |
127 | ui->btnRemove->setEnabled(false); |
128 | ui->pnlProcess->setVisible(false); |
129 | ui->grpDetail->setEnabled(false); |
130 | } |
131 | |
132 | |
133 | void GitRemoteDialog::on_btnProcess_clicked() |
134 | { |
135 | if (ui->btnProcess->text() == tr("Add" )) { |
136 | // add remote |
137 | QString remoteName = ui->txtName->text(); |
138 | QString remoteURL = ui->txtURL->text(); |
139 | GitManager manager; |
140 | QString output; |
141 | if (!manager.addRemote(mFolder, remoteName, remoteURL, output)) { |
142 | InfoMessageBox infoBox; |
143 | infoBox.showMessage(output); |
144 | } else { |
145 | refresh(); |
146 | } |
147 | } else { |
148 | // update remote |
149 | if (ui->lstRemotes->selectedItems().count()<=0) |
150 | return; |
151 | QString oldName = ui->lstRemotes->selectedItems()[0]->text(); |
152 | QString newName = ui->txtName->text(); |
153 | QString url = ui->txtURL->text(); |
154 | GitManager manager; |
155 | QString output; |
156 | if (!manager.setRemoteURL(mFolder,oldName,url,output)) { |
157 | InfoMessageBox infoBox; |
158 | infoBox.showMessage(output); |
159 | return; |
160 | } |
161 | if (oldName != newName) { |
162 | if (!manager.setRemoteURL(mFolder,oldName,url,output)) { |
163 | InfoMessageBox infoBox; |
164 | infoBox.showMessage(output); |
165 | return; |
166 | } |
167 | } |
168 | refresh(); |
169 | } |
170 | } |
171 | |
172 | |
173 | void GitRemoteDialog::on_txtName_textChanged(const QString &/*arg1*/) |
174 | { |
175 | checkDetails(); |
176 | } |
177 | |
178 | |
179 | void GitRemoteDialog::on_txtURL_textChanged(const QString & /*arg1*/) |
180 | { |
181 | checkDetails(); |
182 | } |
183 | |
184 | |
185 | void GitRemoteDialog::on_btnClose_clicked() |
186 | { |
187 | accept(); |
188 | } |
189 | |
190 | |