source: s10k/Vago/xmlToolsInterface/xmltoolsinterface.cpp@ 1134

Last change on this file since 1134 was 1093, checked in by s10k, 7 years ago

Vago 1.4

File size: 14.0 KB
RevLine 
[1058]1#include "xmltoolsinterface.h"
2#include "ui_xmltoolsinterface.h"
3
[1093]4XmlToolsInterface::XmlToolsInterface(QWidget *parent) :
[1058]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.
[1093]11 this->xmlProcessor = new XmlProcessor(UtilVago::getAppPath(), &this->listToProccess);
[1058]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
22XmlToolsInterface::~XmlToolsInterface()
23{
24 delete xmlProcessor;
25 delete ui;
26}
27
28void 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
38void 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
49void 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
67void 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
82void 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
122void XmlToolsInterface::on_cbFilterParentElement_toggled(bool checked)
123{
124 ui->leFilterParentElement->setEnabled(checked);
125}
126
127void XmlToolsInterface::on_cbFilterAttributeName_toggled(bool checked)
128{
129 ui->leFilterAttributeName->setEnabled(checked);
130 ui->leFilterAttributeValue->setEnabled(checked);
131}
132
133void 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
142void 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
[1059]156 // Create temp folder if it doesn't exist
157 if(!QDir(GlobalVars::VagoTemporaryDir).exists()){
158 QDir().mkdir(GlobalVars::VagoTemporaryDir);
159 }
160
[1058]161 if(oldFile.exists()){
162 if(!oldFile.remove()){
[1093]163 UtilVago::showAndLogErrorPopUpLogButton("Couldn't remove old temporary file to preview XML patch! Existing file:\n"
[1058]164 + previewFileLocation
165 );
166 }
167 }
168
169 if(!QFile::copy(currentFileLocation, previewFileLocation)){
[1093]170 UtilVago::showAndLogErrorPopUpLogButton("Couldn't create temporary file to preview the XML patch!\nFrom: " +
[1058]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
187void 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
199bool XmlToolsInterface::validateInput()
200{
201
202 if(ui->leInputInputFiles->text().trimmed().isEmpty()){
[1093]203 Util::Dialogs::showError("You must provide an Input File!");
[1058]204 return false;
205 }
206
207 if(ui->rbFilterRelativeElements->isChecked() && ui->leFilterElement->text().trimmed().isEmpty()){
[1093]208 Util::Dialogs::showError("With Relative Elements checked you must provide a Element Name!");
[1058]209 return false;
210 }
211
212 if(ui->cbFilterParentElement->isChecked() && ui->leFilterParentElement->text().trimmed().isEmpty()){
[1093]213 Util::Dialogs::showError("Parent Element is checked but none was provided!");
[1058]214 return false;
215 }
216
217 if(ui->cbFilterAttributeName->isChecked()){
218 if(ui->leFilterAttributeName->text().trimmed().isEmpty())
219 {
[1093]220 Util::Dialogs::showError("Attribute Name is checked but none was provided!");
[1058]221 return false;
222 }
223
224 if(ui->leFilterAttributeValue->text().trimmed().isEmpty())
225 {
[1093]226 Util::Dialogs::showError("With Attribute Name checked you must provide a Attribute Value!");
[1058]227 return false;
228 }
229 }
230
231 if(ui->rbFilterXPathExpression->isChecked() && ui->leFilterXPathExpression->text().trimmed().isEmpty())
232 {
[1093]233 Util::Dialogs::showError("X-Path Expression is checked but none was provided!");
[1058]234 return false;
235 }
236
237 if(ui->cbXmlToolsOperation->currentText() == "Add Values" && ui->leInputNewValues->text().isEmpty()){
[1093]238 Util::Dialogs::showError(R"|(With "Add Values" operation selected you must provide the "New Value(s)" to be added.)|");
[1058]239 return false;
240 }
241
242 if(ui->cbXmlToolsOperation->currentText() == "Remove Values" && ui->leInputCurrentValues->text().isEmpty()){
[1093]243 Util::Dialogs::showError(R"|(With "Remove Value" operation selected you must provide the "Current Value(s)" to be removed.)|");
[1058]244 return false;
245 }
246
247 return true;
248}
249
250void 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){
[1093]268 Util::Dialogs::showInfo("File(s) processed with sucess!");
[1058]269 }
270 }
271
272 this->previewInProgress = false;
273}
274
275void XmlToolsInterface::setCommand(){
276 ui->leOperationCommandGenCommand->setText(buildCommand());
277}
278
279QString 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()){
[1093]304 currCommand += "--new-val " + Util::String::insertQuotes(ui->leInputNewValues->text()) + " ";
[1058]305 }
306
307 if(ui->leInputCurrentValues->isEnabled()){
[1093]308 currCommand += "--current-val " + Util::String::insertQuotes(ui->leInputCurrentValues->text()) + " ";
[1058]309 }
310
311 if(ui->leInputPositions->isEnabled() && !ui->leInputPositions->text().trimmed().isEmpty()){
[1093]312 currCommand += "--positions " + Util::String::insertQuotes(ui->leInputPositions->text()) + " ";
[1058]313 }
314
315 if(ui->leInputDiffOldNewValue->isEnabled()){
[1093]316 currCommand += "--diff-old-new-val " + Util::String::insertQuotes(ui->leInputDiffOldNewValue->text()) + " ";
[1058]317 }
318
319 if(ui->rbFilterRelativeElements->isChecked()){
320 if(ui->leFilterElement->isEnabled()){
[1093]321 currCommand += "--element-name " + Util::String::insertQuotes(ui->leFilterElement->text()) + " ";
[1058]322 }
323 if(ui->leFilterParentElement->isEnabled()){
[1093]324 currCommand += "--parent-element-name " + Util::String::insertQuotes(ui->leFilterParentElement->text()) + " ";
[1058]325 }
326 if(ui->leFilterAttributeName->isEnabled()){
[1093]327 currCommand += "--attribute-name " + Util::String::insertQuotes(ui->leFilterAttributeName->text()) + " ";
328 currCommand += "--attribute-value " + Util::String::insertQuotes(ui->leFilterAttributeValue->text()) + " ";
[1058]329 }
330 }
331 else{
332 if(ui->leFilterXPathExpression->isEnabled()){
[1093]333 currCommand += "--xpath-expression " + Util::String::insertQuotes(ui->leFilterXPathExpression->text()) + " ";
[1058]334 }
335 }
336
337 if(alternativeFileLocation.isEmpty()){
[1093]338 currCommand += "--files " + Util::String::insertQuotes(ui->leInputInputFiles->text());
[1058]339 }
340 else{
[1093]341 currCommand += "--files " + Util::String::insertQuotes(alternativeFileLocation);
[1058]342 }
343
344
345 if(ui->cbOptionsNoBackups->isChecked()){
346 currCommand += " --no-backups";
347 }
348
349 return "XmlTools " + currCommand;
350}
351
352void XmlToolsInterface::on_pbOperationCommandCopyToClipboard_clicked()
353{
354 QApplication::clipboard()->setText(ui->leOperationCommandGenCommand->text());
355}
356
357void 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
370void XmlToolsInterface::on_leFilterElement_textChanged(const QString &)
371{
372 setCommand();
373}
374
375void XmlToolsInterface::on_leFilterParentElement_textChanged(const QString &)
376{
377 setCommand();
378}
379
380void XmlToolsInterface::on_leFilterAttributeName_textChanged(const QString &)
381{
382 setCommand();
383}
384
385void XmlToolsInterface::on_leFilterAttributeValue_textChanged(const QString &)
386{
387 setCommand();
388}
389
390void XmlToolsInterface::on_leInputCurrentValues_textChanged(const QString &)
391{
392 setCommand();
393}
394
395void XmlToolsInterface::on_leInputNewValues_textChanged(const QString &)
396{
397 setCommand();
398}
399
400void XmlToolsInterface::on_leInputPositions_textChanged(const QString &)
401{
402 setCommand();
403}
404
405void XmlToolsInterface::on_leInputDiffOldNewValue_textChanged(const QString &)
406{
407 setCommand();
408}
409
410void XmlToolsInterface::on_cbOptionsNoBackups_toggled(bool)
411{
412 setCommand();
413}
Note: See TracBrowser for help on using the repository browser.