-
Notifications
You must be signed in to change notification settings - Fork 15
/
projectproperties.cpp
275 lines (229 loc) · 11.1 KB
/
projectproperties.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
*
Copyright (C) 2017-2019 Fábio Bento (fabiobento512)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "projectproperties.h"
#include "ui_projectproperties.h"
ProjectProperties::ProjectProperties(QWidget *parent, FRequestTreeWidgetProjectItem * const projectItem) :
QDialog(parent),
ui(new Ui::ProjectProperties),
projectItem(projectItem)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose,true); //destroy itself once finished.
fillInterface();
if(this->projectItem->authData != nullptr){
fillAuthenticationData(*this->projectItem->authData);
}
if(this->currentPasswordSalt.isEmpty()){
// Unix time as sha256
this->currentPasswordSalt = QCryptographicHash::hash(QByteArray().setNum(QDateTime::currentDateTime().toSecsSinceEpoch()), QCryptographicHash::Sha256).toHex();
}
setRequestAuthenticationNote();
ui->lbProjectPropertiesNote->setText(
"<b>Note:</b> Request authentication works by selecting one of your requests as the one for the authentication.<br/><br/>"
"If you have a website you can find the request that you need by checking in your browser the one that authenticates you "
"(normally you could find it in a \"network\" tab), "
"then just replicate that request in FRequest and select it for the authentication here.<br/><br/>"
"Currently FRequest can only use a single request for the authentication.<br/><br/>"
"You should add in your authentication request the FRequest placeholders for the username and password, they are respectively "
"{{FREQUEST_AUTH_USERNAME}} and {{FREQUEST_AUTH_PASSWORD}}.<br/><br/>"
"You can add these placeholders either in the body of the request or in the headers. "
"FRequest will replace them automatically when authenticating by the username and password that you input above."
);
}
void ProjectProperties::fillInterface(){
ui->leProjectName->setText(this->projectItem->projectName);
ui->leProjectMainUrl->setText(this->projectItem->projectMainUrl);
// Get all requests name to fill request combobox
for(int i=0; i < this->projectItem->childCount(); i++){
FRequestTreeWidgetRequestItem* currentRequest = FRequestTreeWidgetRequestItem::fromQTreeWidgetItem(this->projectItem->child(i));
ui->cbRequestForAuthentication->addItem(getComboBoxNameForRequest(currentRequest), QVariant::fromValue(currentRequest));
}
ui->cbIdentCharacter->setCurrentText(UtilFRequest::getIdentCharacterString(this->projectItem->saveIdentCharacter));
for (int i = 0; i < this->projectItem->globalHeaders.size(); i++) {
const UtilFRequest::HttpHeader &currGlobalHeader = this->projectItem->globalHeaders.at(i);
Util::TableWidget::addRow(ui->twGlobalHeaderKeyValue, QStringList() << currGlobalHeader.name << currGlobalHeader.value);
}
}
ProjectProperties::~ProjectProperties()
{
delete ui;
}
void ProjectProperties::on_cbRequestType_currentIndexChanged(const QString &arg1)
{
ui->cbRequestForAuthentication->setEnabled(false);
if(arg1 == "Request Authentication"){
ui->cbRequestForAuthentication->setEnabled(true);
}
setRequestAuthenticationNote();
}
// Need to override to do the verification
// http://stackoverflow.com/questions/3261676/how-to-make-qdialogbuttonbox-not-close-its-parent-qdialog
void ProjectProperties::accept (){
// Validations
if(Util::Validation::checkEmptySpaces(
QStringList() <<
ui->leProjectName->text() <<
ui->leProjectMainUrl->text()
)){
Util::Dialogs::showError("Please fill the project name and main url.");
return;
}
if(ui->cbUseAuthentication->isChecked() && Util::Validation::checkEmptySpaces(
QStringList() <<
ui->leUsername->text()
)){
Util::Dialogs::showError("Please fill the authorization username and authorization password.");
return;
}
this->projectItem->projectName = ui->leProjectName->text();
this->projectItem->setText(0, this->projectItem->projectName); // todo replace column with enum
this->projectItem->projectMainUrl = ui->leProjectMainUrl->text();
if(ui->cbUseAuthentication->isChecked()){
switch(FRequestAuthentication::getAuthenticationTypeByString(ui->cbRequestType->currentText())){
case FRequestAuthentication::AuthenticationType::REQUEST_AUTHENTICATION:
{
this->projectItem->authData = std::make_shared<RequestAuthentication>(
ui->rbSaveProjectAuthToConfigurationFile->isChecked(),
ui->cbRetryLoginIfError401->isChecked(),
ui->leUsername->text(),
this->currentPasswordSalt,
ui->lePassword->text(),
qvariant_cast<FRequestTreeWidgetRequestItem*>(ui->cbRequestForAuthentication->itemData(ui->cbRequestForAuthentication->currentIndex()))->itemContent.uuid
);
break;
}
case FRequestAuthentication::AuthenticationType::BASIC_AUTHENTICATION:
{
this->projectItem->authData = std::make_shared<BasicAuthentication>(BasicAuthentication(ui->rbSaveProjectAuthToConfigurationFile->isChecked(),
ui->cbRetryLoginIfError401->isChecked(),
ui->leUsername->text(),
this->currentPasswordSalt,
ui->lePassword->text()
));
break;
}
default:
{
QString errorMessage = "Authentication type unknown: '" + ui->cbRequestType->currentText() + "'. Program can't proceed.";
Util::Dialogs::showError(errorMessage);
LOG_FATAL << errorMessage;
exit(1);
}
}
}
else{
this->projectItem->authData = nullptr; // clear the old authentication data
}
UtilFRequest::IdentCharacter identCharacter = UtilFRequest::getIdentCharacterByString(ui->cbIdentCharacter->currentText());
switch(identCharacter){
case UtilFRequest::IdentCharacter::SPACE:
case UtilFRequest::IdentCharacter::TAB:
{
this->projectItem->saveIdentCharacter = identCharacter;
break;
}
default:
{
QString errorMessage = "Ident character unknown: '" + ui->cbIdentCharacter->currentText() + "'. Program can't proceed.";
Util::Dialogs::showError(errorMessage);
LOG_FATAL << errorMessage;
exit(1);
}
}
// Save global headers
this->projectItem->globalHeaders.clear();
for (int i = 0; i < ui->twGlobalHeaderKeyValue->rowCount(); i++) {
UtilFRequest::HttpHeader currentHeader;
currentHeader.name = ui->twGlobalHeaderKeyValue->item(i, 0)->text();
currentHeader.value = ui->twGlobalHeaderKeyValue->item(i, 1)->text();
this->projectItem->globalHeaders.append(currentHeader);
}
QDialog::accept();
emit signalSaveProjectProperties();
}
void ProjectProperties::on_cbUseAuthentication_toggled(bool checked)
{
ui->gbAuthentication->setEnabled(checked);
}
void ProjectProperties::on_tbGlobalHeaderKeyValueAdd_clicked()
{
Util::TableWidget::addRow(ui->twGlobalHeaderKeyValue, QStringList() << "" << "");
}
void ProjectProperties::on_tbGlobalHeaderKeyValueRemove_clicked()
{
int size = Util::TableWidget::getSelectedRows(ui->twGlobalHeaderKeyValue).size();
if(size==0){
Util::Dialogs::showInfo("Select a row first!");
return;
}
if(Util::Dialogs::showQuestion(this, "Are you sure you want to remove all selected rows?")){
for(int i=0; i < size; i++){
ui->twGlobalHeaderKeyValue->removeRow(Util::TableWidget::getSelectedRows(ui->twGlobalHeaderKeyValue).at(size-i-1).row());
}
Util::Dialogs::showInfo("Key-Value rows deleted");
}
}
void ProjectProperties::fillAuthenticationData(FRequestAuthentication &auth){
ui->cbUseAuthentication->setChecked(true);
if(auth.saveAuthToConfigFile){
ui->rbSaveProjectAuthToConfigurationFile->setChecked(true);
}
else{
ui->rbSaveProjectAuthToProjectFile->setChecked(true);
}
ui->cbRequestType->setCurrentText(FRequestAuthentication::getAuthenticationString(auth.type));
switch(auth.type){
case FRequestAuthentication::AuthenticationType::REQUEST_AUTHENTICATION:
{
// https://stackoverflow.com/a/43572369/1499019
RequestAuthentication &concreteAuth = static_cast<RequestAuthentication&>(auth);
ui->leUsername->setText(concreteAuth.username);
ui->lePassword->setText(concreteAuth.password);
ui->cbRequestForAuthentication->setCurrentIndex(
ui->cbRequestForAuthentication->findData(QVariant::fromValue(this->projectItem->getChildRequestByUuid(concreteAuth.requestForAuthenticationUuid)))
);
this->currentPasswordSalt = concreteAuth.passwordSalt;
break;
}
case FRequestAuthentication::AuthenticationType::BASIC_AUTHENTICATION:
{
BasicAuthentication &concreteAuth = static_cast<BasicAuthentication&>(auth);
ui->leUsername->setText(concreteAuth.username);
ui->lePassword->setText(concreteAuth.password);
this->currentPasswordSalt = concreteAuth.passwordSalt;
break;
}
default:
{
QString errorMessage = "Invalid authentication type " + QString::number(static_cast<int>(auth.type)) + "'. Program can't proceed.";
Util::Dialogs::showError(errorMessage);
LOG_FATAL << errorMessage;
exit(1);
}
}
ui->cbRetryLoginIfError401->setChecked(auth.retryLoginIfError401);
}
QString ProjectProperties::getComboBoxNameForRequest(const FRequestTreeWidgetRequestItem* const currentRequest){
return currentRequest->itemContent.name + " (" + UtilFRequest::getRequestTypeString(currentRequest->itemContent.requestType) + ")";
}
void ProjectProperties::setRequestAuthenticationNote(){
if(ui->cbRequestType->currentText() == "Request Authentication"){
ui->saProjectPropertiesNote->setVisible(true);
}
else{
ui->saProjectPropertiesNote->setVisible(false);
}
}