| 1 |
#include "xmltoolsinterface.h" |
| 2 |
#include "ui_xmltoolsinterface.h" |
| 3 |
|
| 4 |
XmlToolsInterface::XmlToolsInterface(QWidget *parent) : |
| 5 |
QMainWindow(parent), |
| 6 |
ui(new Ui::XmlToolsInterface), |
| 7 |
xmlProcessor() |
| 8 |
{ |
| 9 |
ui->setupUi(this); |
| 10 |
this->setAttribute(Qt::WA_DeleteOnClose, true); //destroy itself once finished. |
| 11 |
this->xmlProcessor = new XmlProcessor(UtilVago::getAppPath(), &this->listToProccess); |
| 12 |
|
| 13 |
// setup the correct input options for the current selection |
| 14 |
on_rbFilterRelativeElements_clicked(); |
| 15 |
on_cbFilterParentElement_toggled(ui->cbFilterParentElement->isChecked()); |
| 16 |
on_cbFilterAttributeName_toggled(ui->cbFilterAttributeName->isChecked()); |
| 17 |
on_cbXmlToolsOperation_currentIndexChanged(ui->cbXmlToolsOperation->currentText()); |
| 18 |
|
| 19 |
connect(this->xmlProcessor, SIGNAL(resultConversion(QString,int)), this, SLOT(TXmlToolsResult(QString,int))); |
| 20 |
} |
| 21 |
|
| 22 |
XmlToolsInterface::~XmlToolsInterface() |
| 23 |
{ |
| 24 |
delete xmlProcessor; |
| 25 |
delete ui; |
| 26 |
} |
| 27 |
|
| 28 |
void XmlToolsInterface::dropEvent(QDropEvent* event) |
| 29 |
{ |
| 30 |
const QMimeData* mimeData = event->mimeData(); |
| 31 |
|
| 32 |
event->acceptProposedAction(); |
| 33 |
|
| 34 |
// Set the input file. This file type was already validated by the dragEnterEvent at this point |
| 35 |
ui->leInputInputFiles->setText(mimeData->urls().at(0).toLocalFile()); |
| 36 |
} |
| 37 |
|
| 38 |
void XmlToolsInterface::dragEnterEvent(QDragEnterEvent *event){ |
| 39 |
const QMimeData* mimeData = event->mimeData(); |
| 40 |
|
| 41 |
if (mimeData->hasUrls()) |
| 42 |
{ |
| 43 |
if(mimeData->urls().size() == 1 && QFileInfo(mimeData->urls().at(0).toLocalFile()).suffix().toUpper() == "XML"){ |
| 44 |
event->accept(); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void XmlToolsInterface::on_rbFilterRelativeElements_clicked() |
| 50 |
{ |
| 51 |
ui->leFilterXPathExpression->setEnabled(false); |
| 52 |
ui->leFilterElement->setEnabled(true); |
| 53 |
ui->leFilterParentElement->setEnabled(true); |
| 54 |
ui->lbFilterElement->setEnabled(true); |
| 55 |
ui->cbFilterParentElement->setEnabled(true); |
| 56 |
ui->leFilterAttributeName->setEnabled(true); |
| 57 |
ui->leFilterAttributeValue->setEnabled(true); |
| 58 |
ui->lbFilterAttributeValue->setEnabled(true); |
| 59 |
ui->cbFilterAttributeName->setEnabled(true); |
| 60 |
|
| 61 |
on_cbFilterParentElement_toggled(ui->cbFilterParentElement->isChecked()); |
| 62 |
on_cbFilterAttributeName_toggled(ui->cbFilterAttributeName->isChecked()); |
| 63 |
|
| 64 |
setCommand(); |
| 65 |
} |
| 66 |
|
| 67 |
void XmlToolsInterface::on_rbFilterXPathExpression_clicked() |
| 68 |
{ |
| 69 |
ui->leFilterElement->setEnabled(false); |
| 70 |
ui->leFilterParentElement->setEnabled(false); |
| 71 |
ui->lbFilterElement->setEnabled(false); |
| 72 |
ui->cbFilterParentElement->setEnabled(false); |
| 73 |
ui->leFilterAttributeName->setEnabled(false); |
| 74 |
ui->leFilterAttributeValue->setEnabled(false); |
| 75 |
ui->lbFilterAttributeValue->setEnabled(false); |
| 76 |
ui->cbFilterAttributeName->setEnabled(false); |
| 77 |
ui->leFilterXPathExpression->setEnabled(true); |
| 78 |
|
| 79 |
setCommand(); |
| 80 |
} |
| 81 |
|
| 82 |
void XmlToolsInterface::on_cbXmlToolsOperation_currentIndexChanged(const QString &arg1) |
| 83 |
{ |
| 84 |
ui->leInputCurrentValues->setEnabled(true); |
| 85 |
ui->leInputNewValues->setEnabled(true); |
| 86 |
ui->leInputDiffOldNewValue->setEnabled(true); |
| 87 |
ui->leInputPositions->setEnabled(true); |
| 88 |
|
| 89 |
if(arg1 == "Add Values"){ |
| 90 |
ui->leInputCurrentValues->setEnabled(false); |
| 91 |
ui->leInputDiffOldNewValue->setEnabled(false); |
| 92 |
ui->leInputPositions->setEnabled(false); |
| 93 |
} |
| 94 |
else if(arg1 == "Remove Values"){ |
| 95 |
ui->leInputNewValues->setEnabled(false); |
| 96 |
ui->leInputDiffOldNewValue->setEnabled(false); |
| 97 |
ui->leInputPositions->setEnabled(false); |
| 98 |
} |
| 99 |
else if(arg1 == "Replace Single Value"){ |
| 100 |
ui->leInputDiffOldNewValue->setEnabled(false); |
| 101 |
ui->leInputPositions->setEnabled(false); |
| 102 |
} |
| 103 |
else if(arg1 == "Replace All Values"){ |
| 104 |
ui->leInputCurrentValues->setEnabled(false); |
| 105 |
ui->leInputDiffOldNewValue->setEnabled(false); |
| 106 |
} |
| 107 |
else if(arg1 == "Update Elements"){ |
| 108 |
ui->leInputCurrentValues->setEnabled(false); |
| 109 |
ui->leInputNewValues->setEnabled(false); |
| 110 |
ui->leInputPositions->setEnabled(false); |
| 111 |
} |
| 112 |
else if(arg1 == "Invert Elements"){ |
| 113 |
ui->leInputCurrentValues->setEnabled(false); |
| 114 |
ui->leInputNewValues->setEnabled(false); |
| 115 |
ui->leInputDiffOldNewValue->setEnabled(false); |
| 116 |
ui->leInputPositions->setEnabled(false); |
| 117 |
} |
| 118 |
|
| 119 |
setCommand(); |
| 120 |
} |
| 121 |
|
| 122 |
void XmlToolsInterface::on_cbFilterParentElement_toggled(bool checked) |
| 123 |
{ |
| 124 |
ui->leFilterParentElement->setEnabled(checked); |
| 125 |
} |
| 126 |
|
| 127 |
void XmlToolsInterface::on_cbFilterAttributeName_toggled(bool checked) |
| 128 |
{ |
| 129 |
ui->leFilterAttributeName->setEnabled(checked); |
| 130 |
ui->leFilterAttributeValue->setEnabled(checked); |
| 131 |
} |
| 132 |
|
| 133 |
void XmlToolsInterface::on_pbInputBrowse_clicked() |
| 134 |
{ |
| 135 |
QString result = QFileDialog::getOpenFileName(this,"Choose the XML file...","./" , "XML Files (*.xml)"); |
| 136 |
|
| 137 |
if(!result.isEmpty()){ |
| 138 |
ui->leInputInputFiles->setText(result); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void XmlToolsInterface::on_pbPreviewOperation_clicked() |
| 143 |
{ |
| 144 |
if(!validateInput()){ |
| 145 |
return; |
| 146 |
} |
| 147 |
this->listToProccess.clear(); |
| 148 |
|
| 149 |
// Copy the target file to temporary location and aply to it the command |
| 150 |
|
| 151 |
QString currentFileLocation = ui->leInputInputFiles->text(); |
| 152 |
QString previewFileLocation=GlobalVars::VagoTemporaryDir+"/"+QFileInfo(currentFileLocation).fileName(); |
| 153 |
|
| 154 |
QFile oldFile(previewFileLocation); |
| 155 |
|
| 156 |
// Create temp folder if it doesn't exist |
| 157 |
if(!QDir(GlobalVars::VagoTemporaryDir).exists()){ |
| 158 |
QDir().mkdir(GlobalVars::VagoTemporaryDir); |
| 159 |
} |
| 160 |
|
| 161 |
if(oldFile.exists()){ |
| 162 |
if(!oldFile.remove()){ |
| 163 |
UtilVago::showAndLogErrorPopUpLogButton("Couldn't remove old temporary file to preview XML patch! Existing file:\n" |
| 164 |
+ previewFileLocation |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if(!QFile::copy(currentFileLocation, previewFileLocation)){ |
| 170 |
UtilVago::showAndLogErrorPopUpLogButton("Couldn't create temporary file to preview the XML patch!\nFrom: " + |
| 171 |
currentFileLocation + |
| 172 |
"\nTo: " + previewFileLocation |
| 173 |
); |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
this->listToProccess.append(buildCommand(previewFileLocation).remove(0,9)); // 0,9 removes XmlTools from the beginning |
| 178 |
this->previewInProgress = true; |
| 179 |
|
| 180 |
this->xmlProcessor->start(); |
| 181 |
this->xmlProcessor->wait(); // wait for the XML to be processed |
| 182 |
|
| 183 |
XmlToolsInterfaceCommandPreview *previewWindow = new XmlToolsInterfaceCommandPreview(currentFileLocation, previewFileLocation, this); |
| 184 |
previewWindow->show(); |
| 185 |
} |
| 186 |
|
| 187 |
void XmlToolsInterface::on_pbApplyOperation_clicked() |
| 188 |
{ |
| 189 |
if(!validateInput()){ |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
this->listToProccess.clear(); |
| 194 |
this->listToProccess.append(ui->leOperationCommandGenCommand->text().remove(0,9)); // 0,9 removes XmlTools from the beginning |
| 195 |
this->xmlProcessor->start(); |
| 196 |
} |
| 197 |
|
| 198 |
// return true if valid, false otherwise |
| 199 |
bool XmlToolsInterface::validateInput() |
| 200 |
{ |
| 201 |
|
| 202 |
if(ui->leInputInputFiles->text().trimmed().isEmpty()){ |
| 203 |
Util::Dialogs::showError("You must provide an Input File!"); |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
if(ui->rbFilterRelativeElements->isChecked() && ui->leFilterElement->text().trimmed().isEmpty()){ |
| 208 |
Util::Dialogs::showError("With Relative Elements checked you must provide a Element Name!"); |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
if(ui->cbFilterParentElement->isChecked() && ui->leFilterParentElement->text().trimmed().isEmpty()){ |
| 213 |
Util::Dialogs::showError("Parent Element is checked but none was provided!"); |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
if(ui->cbFilterAttributeName->isChecked()){ |
| 218 |
if(ui->leFilterAttributeName->text().trimmed().isEmpty()) |
| 219 |
{ |
| 220 |
Util::Dialogs::showError("Attribute Name is checked but none was provided!"); |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if(ui->leFilterAttributeValue->text().trimmed().isEmpty()) |
| 225 |
{ |
| 226 |
Util::Dialogs::showError("With Attribute Name checked you must provide a Attribute Value!"); |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if(ui->rbFilterXPathExpression->isChecked() && ui->leFilterXPathExpression->text().trimmed().isEmpty()) |
| 232 |
{ |
| 233 |
Util::Dialogs::showError("X-Path Expression is checked but none was provided!"); |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if(ui->cbXmlToolsOperation->currentText() == "Add Values" && ui->leInputNewValues->text().isEmpty()){ |
| 238 |
Util::Dialogs::showError(R"|(With "Add Values" operation selected you must provide the "New Value(s)" to be added.)|"); |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
if(ui->cbXmlToolsOperation->currentText() == "Remove Values" && ui->leInputCurrentValues->text().isEmpty()){ |
| 243 |
Util::Dialogs::showError(R"|(With "Remove Value" operation selected you must provide the "Current Value(s)" to be removed.)|"); |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
void XmlToolsInterface::TXmlToolsResult(QString result, int numErrors){ |
| 251 |
|
| 252 |
if(!this->previewInProgress){ |
| 253 |
QApplication::alert(this); //Show a notification if window is not active (only when not previewing) |
| 254 |
} |
| 255 |
|
| 256 |
if(numErrors!=0){ |
| 257 |
QString sNumErrors=QString::number(numErrors); |
| 258 |
if(numErrors>1){ |
| 259 |
UtilVago::showErrorPopUpLogButton(result+"\n This is the last of "+sNumErrors+" errors."); |
| 260 |
} |
| 261 |
else{ |
| 262 |
UtilVago::showErrorPopUpLogButton(result); |
| 263 |
} |
| 264 |
} |
| 265 |
else if(numErrors == 0){ |
| 266 |
// if there's a preview in progress don't display the message below |
| 267 |
if(!this->previewInProgress){ |
| 268 |
Util::Dialogs::showInfo("File(s) processed with sucess!"); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
this->previewInProgress = false; |
| 273 |
} |
| 274 |
|
| 275 |
void XmlToolsInterface::setCommand(){ |
| 276 |
ui->leOperationCommandGenCommand->setText(buildCommand()); |
| 277 |
} |
| 278 |
|
| 279 |
QString XmlToolsInterface::buildCommand(const QString &alternativeFileLocation){ |
| 280 |
QString currCommand; |
| 281 |
|
| 282 |
QString currOperation = ui->cbXmlToolsOperation->currentText(); |
| 283 |
|
| 284 |
if(currOperation == "Add Values"){ |
| 285 |
currCommand += "--add-values "; |
| 286 |
} |
| 287 |
else if(currOperation == "Remove Values"){ |
| 288 |
currCommand += "--remove-values "; |
| 289 |
} |
| 290 |
else if(currOperation == "Replace Single Value"){ |
| 291 |
currCommand += "--replace-value "; |
| 292 |
} |
| 293 |
else if(currOperation == "Replace All Values"){ |
| 294 |
currCommand += "--replace-all-values "; |
| 295 |
} |
| 296 |
else if(currOperation == "Update Elements"){ |
| 297 |
currCommand += "--update-elements "; |
| 298 |
} |
| 299 |
else if(currOperation == "Invert Elements"){ |
| 300 |
currCommand += "--invert-elements "; |
| 301 |
} |
| 302 |
|
| 303 |
if(ui->leInputNewValues->isEnabled()){ |
| 304 |
currCommand += "--new-val " + Util::String::insertQuotes(ui->leInputNewValues->text()) + " "; |
| 305 |
} |
| 306 |
|
| 307 |
if(ui->leInputCurrentValues->isEnabled()){ |
| 308 |
currCommand += "--current-val " + Util::String::insertQuotes(ui->leInputCurrentValues->text()) + " "; |
| 309 |
} |
| 310 |
|
| 311 |
if(ui->leInputPositions->isEnabled() && !ui->leInputPositions->text().trimmed().isEmpty()){ |
| 312 |
currCommand += "--positions " + Util::String::insertQuotes(ui->leInputPositions->text()) + " "; |
| 313 |
} |
| 314 |
|
| 315 |
if(ui->leInputDiffOldNewValue->isEnabled()){ |
| 316 |
currCommand += "--diff-old-new-val " + Util::String::insertQuotes(ui->leInputDiffOldNewValue->text()) + " "; |
| 317 |
} |
| 318 |
|
| 319 |
if(ui->rbFilterRelativeElements->isChecked()){ |
| 320 |
if(ui->leFilterElement->isEnabled()){ |
| 321 |
currCommand += "--element-name " + Util::String::insertQuotes(ui->leFilterElement->text()) + " "; |
| 322 |
} |
| 323 |
if(ui->leFilterParentElement->isEnabled()){ |
| 324 |
currCommand += "--parent-element-name " + Util::String::insertQuotes(ui->leFilterParentElement->text()) + " "; |
| 325 |
} |
| 326 |
if(ui->leFilterAttributeName->isEnabled()){ |
| 327 |
currCommand += "--attribute-name " + Util::String::insertQuotes(ui->leFilterAttributeName->text()) + " "; |
| 328 |
currCommand += "--attribute-value " + Util::String::insertQuotes(ui->leFilterAttributeValue->text()) + " "; |
| 329 |
} |
| 330 |
} |
| 331 |
else{ |
| 332 |
if(ui->leFilterXPathExpression->isEnabled()){ |
| 333 |
currCommand += "--xpath-expression " + Util::String::insertQuotes(ui->leFilterXPathExpression->text()) + " "; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if(alternativeFileLocation.isEmpty()){ |
| 338 |
currCommand += "--files " + Util::String::insertQuotes(ui->leInputInputFiles->text()); |
| 339 |
} |
| 340 |
else{ |
| 341 |
currCommand += "--files " + Util::String::insertQuotes(alternativeFileLocation); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
if(ui->cbOptionsNoBackups->isChecked()){ |
| 346 |
currCommand += " --no-backups"; |
| 347 |
} |
| 348 |
|
| 349 |
return "XmlTools " + currCommand; |
| 350 |
} |
| 351 |
|
| 352 |
void XmlToolsInterface::on_pbOperationCommandCopyToClipboard_clicked() |
| 353 |
{ |
| 354 |
QApplication::clipboard()->setText(ui->leOperationCommandGenCommand->text()); |
| 355 |
} |
| 356 |
|
| 357 |
void XmlToolsInterface::on_leInputInputFiles_textChanged(const QString &arg1) |
| 358 |
{ |
| 359 |
// If it contains a wildcard we are not able to guarantee that it is editing only one file, in this case we can't preview the result |
| 360 |
if(arg1.contains("*") || arg1.contains("?")){ |
| 361 |
ui->pbPreviewOperation->setEnabled(false); |
| 362 |
} |
| 363 |
else{ |
| 364 |
ui->pbPreviewOperation->setEnabled(true); |
| 365 |
} |
| 366 |
|
| 367 |
setCommand(); |
| 368 |
} |
| 369 |
|
| 370 |
void XmlToolsInterface::on_leFilterElement_textChanged(const QString &) |
| 371 |
{ |
| 372 |
setCommand(); |
| 373 |
} |
| 374 |
|
| 375 |
void XmlToolsInterface::on_leFilterParentElement_textChanged(const QString &) |
| 376 |
{ |
| 377 |
setCommand(); |
| 378 |
} |
| 379 |
|
| 380 |
void XmlToolsInterface::on_leFilterAttributeName_textChanged(const QString &) |
| 381 |
{ |
| 382 |
setCommand(); |
| 383 |
} |
| 384 |
|
| 385 |
void XmlToolsInterface::on_leFilterAttributeValue_textChanged(const QString &) |
| 386 |
{ |
| 387 |
setCommand(); |
| 388 |
} |
| 389 |
|
| 390 |
void XmlToolsInterface::on_leInputCurrentValues_textChanged(const QString &) |
| 391 |
{ |
| 392 |
setCommand(); |
| 393 |
} |
| 394 |
|
| 395 |
void XmlToolsInterface::on_leInputNewValues_textChanged(const QString &) |
| 396 |
{ |
| 397 |
setCommand(); |
| 398 |
} |
| 399 |
|
| 400 |
void XmlToolsInterface::on_leInputPositions_textChanged(const QString &) |
| 401 |
{ |
| 402 |
setCommand(); |
| 403 |
} |
| 404 |
|
| 405 |
void XmlToolsInterface::on_leInputDiffOldNewValue_textChanged(const QString &) |
| 406 |
{ |
| 407 |
setCommand(); |
| 408 |
} |
| 409 |
|
| 410 |
void XmlToolsInterface::on_cbOptionsNoBackups_toggled(bool) |
| 411 |
{ |
| 412 |
setCommand(); |
| 413 |
} |