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