ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/s10k/Vago/xmlToolsInterface/xmltoolsinterface.cpp
Revision: 1059
Committed: Sun Oct 30 16:01:17 2016 UTC (8 years, 11 months ago) by s10k
Content type: text/x-c++src
Original Path: Vago/trunk/Vago/xmlToolsInterface/xmltoolsinterface.cpp
File size: 14413 byte(s)
Log Message:
Vago 1.2 - fixes for MacOS

File Contents

# Content
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 // Create temp folder if it doesn't exist
158 if(!QDir(GlobalVars::VagoTemporaryDir).exists()){
159 QDir().mkdir(GlobalVars::VagoTemporaryDir);
160 }
161
162 if(oldFile.exists()){
163 if(!oldFile.remove()){
164 UtilVago::showAndLogErrorPopUpLogButton(this->myLogger,
165 "Couldn't remove old temporary file to preview XML patch! Existing file:\n"
166 + previewFileLocation
167 );
168 }
169 }
170
171 if(!QFile::copy(currentFileLocation, previewFileLocation)){
172 UtilVago::showAndLogErrorPopUpLogButton(this->myLogger,
173 "Couldn't create temporary file to preview the XML patch!\nFrom: " +
174 currentFileLocation +
175 "\nTo: " + previewFileLocation
176 );
177 return;
178 }
179
180 this->listToProccess.append(buildCommand(previewFileLocation).remove(0,9)); // 0,9 removes XmlTools from the beginning
181 this->previewInProgress = true;
182
183 this->xmlProcessor->start();
184 this->xmlProcessor->wait(); // wait for the XML to be processed
185
186 XmlToolsInterfaceCommandPreview *previewWindow = new XmlToolsInterfaceCommandPreview(currentFileLocation, previewFileLocation, this);
187 previewWindow->show();
188 }
189
190 void XmlToolsInterface::on_pbApplyOperation_clicked()
191 {
192 if(!validateInput()){
193 return;
194 }
195
196 this->listToProccess.clear();
197 this->listToProccess.append(ui->leOperationCommandGenCommand->text().remove(0,9)); // 0,9 removes XmlTools from the beginning
198 this->xmlProcessor->start();
199 }
200
201 // return true if valid, false otherwise
202 bool XmlToolsInterface::validateInput()
203 {
204
205 if(ui->leInputInputFiles->text().trimmed().isEmpty()){
206 Util::showErrorPopUp("You must provide an Input File!");
207 return false;
208 }
209
210 if(ui->rbFilterRelativeElements->isChecked() && ui->leFilterElement->text().trimmed().isEmpty()){
211 Util::showErrorPopUp("With Relative Elements checked you must provide a Element Name!");
212 return false;
213 }
214
215 if(ui->cbFilterParentElement->isChecked() && ui->leFilterParentElement->text().trimmed().isEmpty()){
216 Util::showErrorPopUp("Parent Element is checked but none was provided!");
217 return false;
218 }
219
220 if(ui->cbFilterAttributeName->isChecked()){
221 if(ui->leFilterAttributeName->text().trimmed().isEmpty())
222 {
223 Util::showErrorPopUp("Attribute Name is checked but none was provided!");
224 return false;
225 }
226
227 if(ui->leFilterAttributeValue->text().trimmed().isEmpty())
228 {
229 Util::showErrorPopUp("With Attribute Name checked you must provide a Attribute Value!");
230 return false;
231 }
232 }
233
234 if(ui->rbFilterXPathExpression->isChecked() && ui->leFilterXPathExpression->text().trimmed().isEmpty())
235 {
236 Util::showErrorPopUp("X-Path Expression is checked but none was provided!");
237 return false;
238 }
239
240 if(ui->cbXmlToolsOperation->currentText() == "Add Values" && ui->leInputNewValues->text().isEmpty()){
241 Util::showErrorPopUp(R"|(With "Add Values" operation selected you must provide the "New Value(s)" to be added.)|");
242 return false;
243 }
244
245 if(ui->cbXmlToolsOperation->currentText() == "Remove Values" && ui->leInputCurrentValues->text().isEmpty()){
246 Util::showErrorPopUp(R"|(With "Remove Value" operation selected you must provide the "Current Value(s)" to be removed.)|");
247 return false;
248 }
249
250 return true;
251 }
252
253 void XmlToolsInterface::TXmlToolsResult(QString result, int numErrors){
254
255 if(!this->previewInProgress){
256 QApplication::alert(this); //Show a notification if window is not active (only when not previewing)
257 }
258
259 if(numErrors!=0){
260 QString sNumErrors=QString::number(numErrors);
261 if(numErrors>1){
262 UtilVago::showErrorPopUpLogButton(result+"\n This is the last of "+sNumErrors+" errors.");
263 }
264 else{
265 UtilVago::showErrorPopUpLogButton(result);
266 }
267 }
268 else if(numErrors == 0){
269 // if there's a preview in progress don't display the message below
270 if(!this->previewInProgress){
271 Util::showPopUp("File(s) processed with sucess!");
272 }
273 }
274
275 this->previewInProgress = false;
276 }
277
278 void XmlToolsInterface::setCommand(){
279 ui->leOperationCommandGenCommand->setText(buildCommand());
280 }
281
282 QString XmlToolsInterface::buildCommand(const QString &alternativeFileLocation){
283 QString currCommand;
284
285 QString currOperation = ui->cbXmlToolsOperation->currentText();
286
287 if(currOperation == "Add Values"){
288 currCommand += "--add-values ";
289 }
290 else if(currOperation == "Remove Values"){
291 currCommand += "--remove-values ";
292 }
293 else if(currOperation == "Replace Single Value"){
294 currCommand += "--replace-value ";
295 }
296 else if(currOperation == "Replace All Values"){
297 currCommand += "--replace-all-values ";
298 }
299 else if(currOperation == "Update Elements"){
300 currCommand += "--update-elements ";
301 }
302 else if(currOperation == "Invert Elements"){
303 currCommand += "--invert-elements ";
304 }
305
306 if(ui->leInputNewValues->isEnabled()){
307 currCommand += "--new-val " + Util::insertQuotes(ui->leInputNewValues->text()) + " ";
308 }
309
310 if(ui->leInputCurrentValues->isEnabled()){
311 currCommand += "--current-val " + Util::insertQuotes(ui->leInputCurrentValues->text()) + " ";
312 }
313
314 if(ui->leInputPositions->isEnabled() && !ui->leInputPositions->text().trimmed().isEmpty()){
315 currCommand += "--positions " + Util::insertQuotes(ui->leInputPositions->text()) + " ";
316 }
317
318 if(ui->leInputDiffOldNewValue->isEnabled()){
319 currCommand += "--diff-old-new-val " + Util::insertQuotes(ui->leInputDiffOldNewValue->text()) + " ";
320 }
321
322 if(ui->rbFilterRelativeElements->isChecked()){
323 if(ui->leFilterElement->isEnabled()){
324 currCommand += "--element-name " + Util::insertQuotes(ui->leFilterElement->text()) + " ";
325 }
326 if(ui->leFilterParentElement->isEnabled()){
327 currCommand += "--parent-element-name " + Util::insertQuotes(ui->leFilterParentElement->text()) + " ";
328 }
329 if(ui->leFilterAttributeName->isEnabled()){
330 currCommand += "--attribute-name " + Util::insertQuotes(ui->leFilterAttributeName->text()) + " ";
331 currCommand += "--attribute-value " + Util::insertQuotes(ui->leFilterAttributeValue->text()) + " ";
332 }
333 }
334 else{
335 if(ui->leFilterXPathExpression->isEnabled()){
336 currCommand += "--xpath-expression " + Util::insertQuotes(ui->leFilterXPathExpression->text()) + " ";
337 }
338 }
339
340 if(alternativeFileLocation.isEmpty()){
341 currCommand += "--files " + Util::insertQuotes(ui->leInputInputFiles->text());
342 }
343 else{
344 currCommand += "--files " + Util::insertQuotes(alternativeFileLocation);
345 }
346
347
348 if(ui->cbOptionsNoBackups->isChecked()){
349 currCommand += " --no-backups";
350 }
351
352 return "XmlTools " + currCommand;
353 }
354
355 void XmlToolsInterface::on_pbOperationCommandCopyToClipboard_clicked()
356 {
357 QApplication::clipboard()->setText(ui->leOperationCommandGenCommand->text());
358 }
359
360 void XmlToolsInterface::on_leInputInputFiles_textChanged(const QString &arg1)
361 {
362 // 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
363 if(arg1.contains("*") || arg1.contains("?")){
364 ui->pbPreviewOperation->setEnabled(false);
365 }
366 else{
367 ui->pbPreviewOperation->setEnabled(true);
368 }
369
370 setCommand();
371 }
372
373 void XmlToolsInterface::on_leFilterElement_textChanged(const QString &)
374 {
375 setCommand();
376 }
377
378 void XmlToolsInterface::on_leFilterParentElement_textChanged(const QString &)
379 {
380 setCommand();
381 }
382
383 void XmlToolsInterface::on_leFilterAttributeName_textChanged(const QString &)
384 {
385 setCommand();
386 }
387
388 void XmlToolsInterface::on_leFilterAttributeValue_textChanged(const QString &)
389 {
390 setCommand();
391 }
392
393 void XmlToolsInterface::on_leInputCurrentValues_textChanged(const QString &)
394 {
395 setCommand();
396 }
397
398 void XmlToolsInterface::on_leInputNewValues_textChanged(const QString &)
399 {
400 setCommand();
401 }
402
403 void XmlToolsInterface::on_leInputPositions_textChanged(const QString &)
404 {
405 setCommand();
406 }
407
408 void XmlToolsInterface::on_leInputDiffOldNewValue_textChanged(const QString &)
409 {
410 setCommand();
411 }
412
413 void XmlToolsInterface::on_cbOptionsNoBackups_toggled(bool)
414 {
415 setCommand();
416 }