1 /**************************************************************************
2 *   Copyright (C) 2000-2019 by Johan Maes                                 *
3 *   on4qz@telenet.be                                                      *
4 *   http://users.telenet.be/on4qz                                         *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 *   This program is distributed in the hope that it will be useful,       *
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 *   GNU General Public License for more details.                          *
15 *                                                                         *
16 *   You should have received a copy of the GNU General Public License     *
17 *   along with this program; if not, write to the                         *
18 *   Free Software Foundation, Inc.,                                       *
19 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20 ***************************************************************************/
21 
22 #include "supportfunctions.h"
23 #include "appglobal.h"
24 #include <QDateTime>
25 #include <QDebug>
26 #include <stdarg.h>
27 #include "dirdialog.h"
28 
29 
30 QString lastPath("");
31 
getValue(int & val,QLineEdit * input)32 bool getValue(int &val, QLineEdit* input)
33 {
34 	bool ok;
35 	QString s;
36 	s=input->text();
37 	val=s.toInt(&ok,0); // allow ayutomatic conversion from hex to decimal in the classic C++ way : 0x is hex other are decimal
38 	return ok;
39 }
40 
getValue(double & val,QLineEdit * input)41 bool getValue(double &val, QLineEdit* input)
42 {
43 	bool ok;
44 	QString s;
45 	s=input->text();
46 	val=s.toDouble(&ok);
47 	return ok;
48 }
49 
getValue(int & val,QString input)50 bool getValue(int &val, QString input)
51 {
52 	bool ok;
53 	val=input.toInt(&ok);
54 	return ok;
55 }
getValue(double & val,QString input)56 bool getValue(double &val, QString input)
57 {
58 	bool ok;
59 	val=input.toDouble(&ok);
60 	return ok;
61 }
62 
getValue(bool & val,QCheckBox * input)63 void getValue(bool &val, QCheckBox *input)
64 {
65 	val=input->isChecked();
66 }
67 
getValue(int & val,QSpinBox * input)68 void getValue(int &val, QSpinBox *input)
69 {
70 	val=input->value();
71 }
72 
getValue(uint & val,QSpinBox * input)73 void getValue(uint &val, QSpinBox *input)
74 {
75   val=input->value();
76 }
77 
getValue(double & val,QDoubleSpinBox * input)78 void getValue(double &val, QDoubleSpinBox *input)
79 {
80   val=input->value();
81 }
82 
getValue(QString & s,QLineEdit * input)83 void getValue(QString &s, QLineEdit *input)
84 {
85 	s=input->text();
86 }
87 
getValue(QString & s,QPlainTextEdit * input)88 void getValue(QString &s, QPlainTextEdit *input)
89 {
90   s=input->toPlainText();
91 }
92 
93 
94 
getValue(int & s,QComboBox * input)95 void getValue(int &s, QComboBox *input)
96 {
97 	s=input->currentText().toInt();
98 }
99 
getIndex(int & s,QComboBox * input)100 void getIndex(int &s, QComboBox *input)
101 {
102   s=input->currentIndex();
103 }
104 
getValue(QString & s,QComboBox * input)105 void getValue(QString &s, QComboBox *input)
106 {
107 	s=input->currentText();
108 }
109 
getValue(bool & val,QPushButton * input)110 void getValue(bool &val, QPushButton *input)
111 {
112   val=input->isChecked();
113 }
114 
115 
getValue(int & s,QButtonGroup * input)116 void getValue(int &s, QButtonGroup *input)
117 {
118 	s=input->checkedId();
119 }
120 
getValue(bool & s,QRadioButton * input)121 void getValue(bool &s, QRadioButton *input)
122 {
123 	s=input->isChecked();
124 }
125 
getValue(int & val,QSlider * input)126 void getValue(int &val, QSlider *input)
127 {
128   val=input->value();
129 }
130 
getValue(uint & val,QSlider * input)131 void getValue(uint &val, QSlider *input)
132 {
133   val=input->value();
134 }
135 
setValue(int val,QLineEdit * output)136 void setValue(int val, QLineEdit* output)
137 {
138 	output->setText(QString::number(val));
139 }
140 
setValue(double val,QLineEdit * output)141 void setValue(double val, QLineEdit* output)
142 {
143 	output->setText(QString::number(val));
144 }
145 /**
146 	\brief sets double number in a QlineEdit
147 	\param val  the value to set
148 	\param output pointer to QLineEdit
149 	\param prec the required precision
150 */
151 
setValue(double val,QLineEdit * output,int prec)152 void setValue(double val, QLineEdit* output,int prec)
153 {
154 	output->setText(QString::number(val,'g',prec));
155 }
156 
setValue(bool val,QCheckBox * input)157 void setValue(bool val, QCheckBox *input)
158 {
159 	input->setChecked(val);
160 }
161 
setValue(int val,QSpinBox * input)162 void setValue(int val, QSpinBox *input)
163 {
164 	input->setValue(val);
165 }
166 
setValue(uint val,QSpinBox * input)167 void setValue(uint val, QSpinBox *input)
168 {
169   input->setValue(val);
170 }
171 
setValue(double val,QDoubleSpinBox * input)172 void setValue(double val, QDoubleSpinBox *input)
173 {
174   input->setValue(val);
175 }
176 
setValue(QString s,QLineEdit * input)177 void setValue(QString s, QLineEdit *input)
178 {
179 	input->setText(s);
180 }
181 
setValue(QString s,QPlainTextEdit * input)182 void setValue(QString s, QPlainTextEdit *input)
183 {
184   input->setPlainText(s);
185 }
186 
setValue(int s,QComboBox * input)187 void setValue(int s, QComboBox *input)
188 {
189 	int i;
190 	for(i=0;i<input->count();i++)
191 		{
192 			if(input->itemText(i).toInt()==s)
193 				{
194 					input->setCurrentIndex(i);
195 					return;
196 				}
197 		}
198 	input->setCurrentIndex(0);
199 }
200 
setIndex(int s,QComboBox * input)201 void setIndex(int s, QComboBox *input)
202 {
203   input->setCurrentIndex(s);
204 }
205 
setValue(QString s,QComboBox * input)206 void setValue(QString s, QComboBox *input)
207 {
208 	int i;
209 	for(i=0;i<input->count();i++)
210 		{
211 			if(input->itemText(i)==s)
212 				{
213 					input->setCurrentIndex(i);
214 					return;
215 				}
216 		}
217 	input->setCurrentIndex(0);
218 }
219 
setValue(bool s,QPushButton * input)220 void setValue(bool s, QPushButton *input)
221 {
222   input->setChecked(s);
223 }
224 
setValue(int s,QButtonGroup * input)225 void setValue(int s, QButtonGroup *input)
226 {
227 	input->button(s)->setChecked(true);
228 }
229 
setValue(bool s,QRadioButton * input)230 void setValue(bool s, QRadioButton *input)
231 {
232 	input->setChecked(s);
233 }
234 
setValue(int val,QSlider * input)235 void setValue(int val, QSlider *input)
236 {
237   input->setValue(val);
238 }
239 
240 
browseGetFile(QLineEdit * le,QString deflt,const QString & filter)241 bool browseGetFile(QLineEdit *le,QString deflt, const QString &filter)
242 {
243     dirDialog d((QWidget *)le,"Browse");
244     QString s=d.openFileName(deflt,filter);
245   if (s.isNull()) return false;
246 	if (s.isEmpty()) return false;
247 	le->setText(s);
248 	return true;
249 }
250 
browseSaveFile(QLineEdit * le,QString deflt,const QString & filter)251 bool browseSaveFile(QLineEdit *le,QString deflt,const QString &filter)
252 {
253     dirDialog d((QWidget *)le,"Browse");
254 	QString s=d.saveFileName(deflt,filter,"");
255   if (s.isNull()) return false;
256 	if (s.isEmpty()) return false;
257 	le->setText(s);
258 	return true;
259 }
260 
browseDir(QLineEdit * le,QString deflt)261 bool browseDir(QLineEdit *le,QString deflt)
262 {
263     dirDialog d((QWidget *)le,"Browse");
264     QString s=d.openDirName(deflt);
265   if (s.isNull()) return false;
266 	if (s.isEmpty()) return false;
267 	le->setText(s);
268 	return true;
269 }
270 
271 
272 
273 
deleteFiles(QString dirPath,QString extension)274 void deleteFiles(QString dirPath,QString extension)
275 {
276   int i;
277   QDir dir(dirPath);
278   QStringList filters;
279   QFile fi;
280   filters << extension;
281   dir.setNameFilters(filters);
282   QFileInfoList entries = dir.entryInfoList(filters,QDir::Files|QDir::NoSymLinks);
283   for(i=0;i<entries.count();i++)
284     {
285       fi.setFileName(entries.at(i).absoluteFilePath());
286       fi.remove();
287     }
288 }
289 
trash(QString filename,bool forceDelete)290 bool trash(QString filename,bool forceDelete)
291 {
292   QString tmp;
293   QFile orgFile(filename);
294   QFileInfo modifiedFileInfo(filename);
295   QFileInfo info(filename);
296   QFile infoFile;
297   QFile modifiedFile;
298   QDir trDir;
299   QDir infoDir;
300   QDir filesDir;
301   QString infoTxt;
302   trDir.setPath(getenv("XDG_DATA_HOME"));
303   if (trDir.path().isEmpty())  trDir.setPath(QDir::homePath()+"/.local/share/Trash");
304   infoDir.setPath(trDir.path()+"/info");
305   filesDir.setPath(trDir.path()+"/files");
306   infoFile.setFileName(infoDir.path()+"/"+info.fileName()+".trashinfo");
307   modifiedFile.setFileName(filesDir.path()+"/"+modifiedFileInfo.fileName());
308   int counter=0;
309   do
310     {
311       if(!modifiedFile.exists()) break;
312       counter++;
313       tmp=QString("%1/%2_%3.%4").arg(filesDir.path()).arg(modifiedFileInfo.completeBaseName()).arg(QString::number(counter)).arg(modifiedFileInfo.suffix());
314       modifiedFile.setFileName(tmp);
315       tmp=QString("%1/%2_%3.%4").arg(infoDir.path()).arg(modifiedFileInfo.completeBaseName()).arg(QString::number(counter)).arg(modifiedFileInfo.suffix());
316       infoFile.setFileName(tmp+".trashinfo");
317     }
318   while(1);
319 
320   infoTxt=QString("[Trash Info]\nPath=%1\nDeletionDate=%2")
321       .arg(filename).arg(QDateTime::currentDateTime().toString(Qt::ISODate));
322 
323   if((!trDir.exists()) || (!infoDir.exists()) || (!filesDir.exists()))
324     {
325       errorOut() << "Trash folder or one of its components does not exist";
326       if(forceDelete) orgFile.remove();
327       return false;
328     }
329 
330   if(!infoFile.open(QIODevice::WriteOnly))
331   {
332    errorOut() << QString("Trash folder: can't open %1 for writing").arg(infoFile.fileName());
333     if(forceDelete) orgFile.remove();
334     return false;
335   }
336   infoFile.write(infoTxt.toLatin1().data());
337   infoFile.close();
338   QFile trashFile(info.absoluteFilePath());
339   QString target;
340   target=QString("%1").arg(modifiedFile.fileName());
341   if(!trashFile.rename(filename,target))
342   {
343     errorOut() << QString("Trash folder: can't rename %1 to %2").arg(filename).arg(target);
344     if(forceDelete) orgFile.remove();
345     return false;
346   }
347   return true;
348 }
349 
350 
timingAnalyser()351 timingAnalyser::timingAnalyser()
352 {
353 
354 }
355 
~timingAnalyser()356 timingAnalyser::~timingAnalyser()
357 {
358 }
start()359 void timingAnalyser::start()
360 {
361   tm.start();
362 }
363 
result()364 unsigned long timingAnalyser::result()
365 {
366   return tm.elapsed();
367 }
368 
369 
370 
371 
372 
373 
374