1 /***************************************************************************
2                           filemanager.cpp  -  description
3                              -------------------
4     begin                : sept 2011
5     copyright            : (C) 2011 by Jaime Robles
6     email                : jaime@robles.es
7  ***************************************************************************/
8 
9 /*****************************************************************************
10  * This file is part of KLog.                                                *
11  *                                                                           *
12  *    KLog is free software: you can redistribute it and/or modify           *
13  *    it under the terms of the GNU General Public License as published by   *
14  *    the Free Software Foundation, either version 3 of the License, or      *
15  *    (at your option) any later version.                                    *
16  *                                                                           *
17  *    KLog is distributed in the hope that it will be useful,                *
18  *    but WITHOUT ANY WARRANTY; without even the implied warranty of         *
19  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
20  *    GNU General Public License for more details.                           *
21  *                                                                           *
22  *    You should have received a copy of the GNU General Public License      *
23  *    along with KLog.  If not, see <https://www.gnu.org/licenses/>.         *
24  *                                                                           *
25  *****************************************************************************/
26 #include "filemanager.h"
27 //#include <QDebug>
28 
29 
FileManager(DataProxy_SQLite * dp)30 FileManager::FileManager(DataProxy_SQLite *dp)
31 {
32        //qDebug() << "FileManager::FileManager()-1" << QT_ENDL;
33     constrid = 1;
34     dataProxy = dp;
35     dbCreated = false;
36     rstTXDefault  = false;
37     rstRXDefault = false;
38     ignoreUnknownAlways = false;
39     noMoreQso = false;
40     defaultStationCallsign = QString();
41     duplicatedQSOSlotInSecs = 0;
42 
43     util = new Utilities;
44     //qso = new QSO;
45     klogVersion = util->getVersion();
46     db = new DataBase(Q_FUNC_INFO, klogVersion, util->getKLogDBFile());
47 
48     usePreviousStationCallsignAnswerAlways = false;
49     world = new World(dataProxy, Q_FUNC_INFO);
50     awards = new Awards(dataProxy, Q_FUNC_INFO);
51     hashLogs.clear();
52     //qDebug() << "FileManager::FileManager()-1  - END" << QT_ENDL;
53 }
54 
55 
FileManager(DataProxy_SQLite * dp,const QString & _klogDir,const QString & _softVersion)56 FileManager::FileManager(DataProxy_SQLite *dp, const QString &_klogDir, const QString &_softVersion)
57 //FileManager::FileManager(const QString &_klogDir, const QString &_softVersion, DataBase _db)
58 {
59        //qDebug() << "FileManager::FileManager()-3: Dir(2)" << _klogDir << QT_ENDL;
60     constrid = 2;
61     dataProxy = dp;
62     util = new Utilities;
63     util->setVersion(klogVersion);
64     defaultStationCallsign = QString();
65     dbCreated = false;
66     rstTXDefault  = false;
67     rstRXDefault = false;
68     duplicatedQSOSlotInSecs = 0;
69     db = new DataBase(Q_FUNC_INFO, klogVersion, util->getKLogDBFile());
70 
71     klogVersion = _softVersion;
72     //dataProxyPrepared = new DataProxy_SQLite(klogVersion);
73 
74     klogDir = _klogDir;
75     ignoreUnknownAlways = false;
76     usePreviousStationCallsignAnswerAlways = false;
77     world = new World(dataProxy, klogDir, Q_FUNC_INFO);
78     awards = new Awards(dataProxy, Q_FUNC_INFO);
79 
80     noMoreQso = false;
81     hashLogs.clear();
82        //qDebug() << "FileManager::FileManager()-3: Dir(2) - END"  << QT_ENDL;
83 }
84 
~FileManager()85 FileManager::~FileManager(){}
86 
setDuplicatedQSOSlot(const int _secs)87 void FileManager::setDuplicatedQSOSlot (const int _secs)
88 {
89     if (_secs >= 0)
90     {
91         duplicatedQSOSlotInSecs = _secs;
92     }
93 }
94 
checkADIFValidFormat(const QStringList & _qs)95 bool FileManager::checkADIFValidFormat(const QStringList &_qs)
96 {
97     QStringList qs = _qs;
98 
99     if (qs.size()!= 2)
100     {
101            //qDebug() << "FileManager::checkADIFValidFormat-0 (not two) " << QT_ENDL;
102         return false;
103     }
104 
105     QString q0 = qs.at(0);
106     QString q1 = qs.at(1);
107 
108       //qDebug() << "FileManager::checkADIFValidFormat: " << qs.at(0) << QT_ENDL;
109       //qDebug() << "FileManager::checkADIFValidFormat: " << qs.at(1) << QT_ENDL;
110 
111     int len = 0;
112     int i = (qs.at(0)).count(":");
113 
114     if (i == 2)
115     { // DATE:8:D / 20141020
116         len = (q0.section(':', 1, 1)).toInt();
117     }
118     else if (i == 1)
119     { // DATE:8 / 20141020
120         len = (q0.section(':', 1, 1)).toInt();
121     }
122     else
123     {
124            //qDebug() << "FileManager::checkADIFValidFormat-1 " << QT_ENDL;
125         return false;
126     }
127 
128     //i = (qs.at(0)).indexOf(":");
129     //i = (qs.at(0)).length() - i -1;
130 
131        //qDebug() << "i = " << QString::number(i) << "/" << qs.at(0) << QT_ENDL;
132 
133     //len = ( (qs.at(0)).right(i)).toInt();
134        //qDebug() << "len = " << QString::number(len) << QT_ENDL;
135 
136     if ( (q1).length() != len )
137     {
138            //qDebug() << "FileManager::checkADIFValidFormat-2: " << (qs.at(1)) << " - " << QString::number((qs.at(1)).length()) << "/" << QString::number(len) << QT_ENDL;
139         return false;
140     }
141 
142     if (len <= 0)
143     {
144            //qDebug() << "FileManager::checkADIFValidFormat-3 " << QT_ENDL;
145         return false;
146     }
147 
148        //qDebug() << "FileManager::checkADIFValidFormat-4 (true)" << QT_ENDL;
149 
150     return true;
151 }
152 
adifLogExport(const QString & _fileName,const int _logN)153 bool FileManager::adifLogExport(const QString& _fileName, const int _logN)
154 {
155     //qDebug() << "FileManager::adifLogExport" << _fileName << QT_ENDL;
156 
157     return adifLogExportToFile(_fileName, _logN, false, false, false);
158 }
159 
showError(const QString & _txt)160 void FileManager::showError (const QString &_txt)
161 {
162     //qDebug() << Q_FUNC_INFO << ": "  << _txt << QT_ENDL;
163 
164     QMessageBox msgBox;
165     msgBox.setWindowTitle(tr("KLog - Error"));
166     msgBox.setIcon(QMessageBox::Warning);
167 
168     QString aux = QString(_txt );
169     msgBox.setText(aux);
170     msgBox.setStandardButtons(QMessageBox::Ok);
171     msgBox.exec();
172 }
173 
adifLogExportReturnList(const QString & _fileName,const QString & _callsign,const QDate & _startDate,const QDate & _endDate,const int _logN,const ExportMode _em)174 QList<int> FileManager::adifLogExportReturnList(const QString& _fileName, const QString &_callsign, const QDate &_startDate, const QDate &_endDate, const int _logN, const ExportMode _em)
175 //QList<int> FileManager::adifLogExportReturnList(const QString& _fileName, const QString &_callsign, const QDate &_startDate, const QDate &_endDate, const int _logN, const bool LoTWOnly)
176 {
177     //qDebug() << Q_FUNC_INFO << ": Start)" << _fileName << "/" << _callsign << QT_ENDL;
178     QList<int> qsos;
179     qsos.clear();
180     if ((!util->isValidCall(_callsign)) && (_callsign != "ALL") && (_callsign !="NOT"))
181     {
182          showError(tr("The selected callsign (%1) is not valid, please check it again to export the log.").arg(_callsign));
183          return qsos;
184     }
185     noMoreQso = false;
186     if (_logN != -1)
187     { // We will export data from ALL logs.
188         if (!dataProxy->doesThisLogExist(_logN))
189         {
190             //qDebug() << Q_FUNC_INFO << ": The log does not exist" << QT_ENDL;
191             showError(tr("The selected log does not exist, please check it again."));
192             return qsos;
193         }
194     }
195 
196     QString queryStringCount;
197     QString queryString;
198     QString _queryStation;
199 
200     if (util->isValidCall(_callsign))
201     {
202          _queryStation = QString(" station_callsign ='%1'").arg(_callsign);
203     }
204     else if (_callsign == "ALL")
205     {
206         _queryStation = QString(" station_callsign !='ALL'");
207     }
208     else
209     {
210          _queryStation = QString(" station_callsign =''");
211     }
212 
213     QString _queryDateFrom;
214 
215     if (_startDate.isValid())
216     {
217          _queryDateFrom = QString(" AND qso_date >= '%1'").arg(util->getDateSQLiteStringFromDate(_startDate));
218     }
219     else
220     {
221          _queryDateFrom = QString(" AND qso_date != '1'");
222     }
223 
224     QString _queryDateTo;
225     if (_endDate.isValid())
226     {
227          _queryDateTo = QString(" AND qso_date <= '%1'").arg(util->getDateSQLiteStringFromDate(_endDate.addDays(1)));
228     }
229     else
230     {
231          _queryDateTo = QString();
232     }
233 
234     QString _queryLog;
235     if (_logN == -1)
236     {
237          _queryLog = QString(" AND logNumber = '%1'").arg(_logN);
238     }
239     else
240     {
241          _queryLog = QString();
242     }
243 
244     QFile file(_fileName);
245     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) /* Flawfinder: ignore */
246     {
247          showError(tr("The file %1 can't be opened.").arg(_fileName));
248          return qsos;
249     }
250 
251     QSqlQuery query;
252 
253     if (_em == ModeLotW)
254     {
255        //qDebug() << "FileManager::adifLogExportReturnList: Exporting for LoTW" << QT_ENDL;
256         // LoTW Required fields: call sign, UTC Date, UTC time, Mode, Band
257         // LoTW Optional fields: RX band, Frecuency TX, frecuency RX, Propagation mode, Satellite
258 
259         queryStringCount = QString("SELECT COUNT (id) FROM log WHERE") + _queryStation + QString(" AND lotw_qsl_sent='Q'") + _queryDateFrom + _queryDateTo;
260         queryString = QString("SELECT id, call, freq, bandid, band_rx, freq_rx, modeid, gridsquare, my_gridsquare, qso_date, prop_mode, sat_name, station_callsign FROM log WHERE") + _queryStation + QString(" AND lotw_qsl_sent='Q'") + _queryDateFrom + _queryDateTo;
261     }
262     else if (_em == ModeClubLog)
263     {
264         //qDebug() << "FileManager::adifLogExportReturnList: Exporting for ClubLog" << QT_ENDL;
265         queryStringCount = QString("SELECT COUNT (id) FROM log WHERE") + _queryStation + QString(" AND clublog_qso_upload_status='M'") + _queryDateFrom + _queryDateTo;
266         queryString = QString("SELECT id, call, rst_sent, rst_rcvd, freq, bandid, band_rx, modeid, qso_date, qsl_rcvd, qslrdate, qslsdate, prop_mode, operator, station_callsign, dxcc, qsl_sent, lotw_qsl_rcvd, credit_granted, notes, qso_date_off FROM log WHERE") + _queryStation + QString(" AND clublog_qso_upload_status='M'") + _queryDateFrom + _queryDateTo;
267     }
268     else if (_em == ModeEQSL)
269     {
270         //qDebug() << "FileManager::adifLogExportReturnList: Exporting for eQSL" << QT_ENDL;
271         queryStringCount = QString("SELECT COUNT (id) FROM log WHERE") + _queryStation + QString(" AND eqsl_qsl_sent='Q'") + _queryDateFrom + _queryDateTo;
272         queryString = QString("SELECT id, call, rst_sent, freq, bandid, modeid, qso_date, prop_mode, operator, station_callsign, sat_name, my_cnty, my_gridsquare, my_lat, my_lon FROM log WHERE") + _queryStation + QString(" AND eqsl_qsl_sent='Q'") + _queryDateFrom + _queryDateTo;
273     }
274     else
275     {
276         //qDebug() << "FileManager::adifLogExportReturnList: Exporting normal ADIF" << QT_ENDL;
277         if (_callsign == "ALL")
278         {
279             queryStringCount = QString("SELECT COUNT (id) FROM log");
280             queryString = QString("SELECT * FROM log");
281         }
282         else
283         {
284             queryStringCount = QString("SELECT COUNT (id) FROM log WHERE") + _queryStation  + _queryDateFrom + _queryDateTo + _queryLog;
285             queryString = QString("SELECT * FROM log WHERE") + _queryStation + _queryDateFrom + _queryDateTo + _queryLog;
286         }
287     }
288     //qDebug() << "FileManager::adifLogExportReturnList: Query Count: " << queryStringCount << QT_ENDL;
289     //qDebug() << "FileManager::adifLogExportReturnList: Query data: " << queryString << QT_ENDL;
290     int numberOfQsos = dataProxy->getHowManyQSOInLog(_logN);
291     int i = 0;
292 
293     bool sqlOK = query.exec(queryStringCount);
294    //qDebug() << "FileManager::adifLogExportReturnList: " << query.lastQuery() << QT_ENDL;
295     if (!sqlOK)
296     {
297         //qDebug() << "FileManager::adifLogExportReturnList: Query Error"  << QT_ENDL;
298         emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
299         return qsos;
300     }
301     else
302     {
303         query.next();
304         if (query.isValid())
305         {
306             i = (query.value(0)).toInt();
307             if (i>0)
308             {
309                 numberOfQsos = i;
310                 query.finish();
311             }
312             else if (i == 0)
313             {
314                 showError(tr("There are no QSOs pending to be exported with that station callsign."));
315                 return qsos;
316             }
317         }
318     }
319 
320     QTextStream out(&file);
321 
322     int step = util->getProgresStepForDialog(numberOfQsos);
323    //qDebug() << "FileManager::adifLogExportReturnList: " << QString::number(step) << QT_ENDL;
324 
325     QProgressDialog progress(tr("Writing ADIF file..."), tr("Abort writing"), 0, numberOfQsos, this);
326     progress.setMaximum(numberOfQsos);
327     progress.setWindowModality(Qt::WindowModal);
328     progress.setValue(0);
329     progress.setWindowTitle(tr("Export"));
330     progress.setAutoClose(true);
331 
332 
333     writeADIFHeader(out, _em, numberOfQsos);
334 
335     i = 0;
336 
337     sqlOK = query.exec(queryString);
338     //qDebug() << "FileManager::adifLogExportReturnList: " << query.lastQuery() << QT_ENDL;
339     if (!sqlOK)
340     {
341         //qDebug() << "FileManager::adifLogExportReturnList: Query Error"  << QT_ENDL;
342         emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
343         return qsos;
344     }
345 
346     QSqlRecord rec = query.record();
347     int nameCol;
348 
349    //qDebug() << "FileManager::adifLogExportReturnList: Entering the While..."  << QT_ENDL;
350 
351     while ( (query.next()) && (!noMoreQso) )
352     {
353         //qDebug() << "FileManager::adifLogExportReturnList: Start of While"  << QT_ENDL;
354         if (query.isValid())
355         {
356             nameCol = rec.indexOf("id");
357             qsos.append((query.value(nameCol)).toInt());
358             writeQuery(query, out, _em, false, false, _logN); // JustMarked = false, onlyRequested = false
359 
360             i++;
361             //qDebug() << "FileManager::adifLogExportReturnList: Start of isValid"  << QT_ENDL;
362         } // END of if (query.isValid())
363         else
364         {
365             //qDebug() << "FileManager::adifLogExportReturnList: Querty NOT isValid"  << QT_ENDL;
366         }
367 
368         //qDebug() << "FileManager::adifLogExportReturnList: Checking if cancelled"  << QT_ENDL;
369 
370         if (( (i % step ) == 0) )
371         { // To update the speed I will only show the progress once each X QSOs
372             //qDebug() << "FileManager::adifLogExportReturnList: ********************************   UPDATING THE MESSAGE! " << QString::number(i)  << QT_ENDL;
373             QString aux = tr("Exporting ADIF file...\n QSO: %1 / %2 ").arg(i).arg(numberOfQsos);
374             progress.setLabelText(aux);
375             progress.setValue(i);
376             //qDebug() << "FileManager::adifLogExportReturnList: ********************************   UPDATING THE MESSAGE: " << aux  << QT_ENDL;
377         }
378 
379         if ( progress.wasCanceled() )
380         {
381             QMessageBox msgBox;
382             msgBox.setWindowTitle(tr("KLog - User cancelled"));
383             QString aux = QString(tr("You have canceled the file export. The file will be removed and no data will be exported.") + "\n" + tr("Do you still want to cancel?"));
384             msgBox.setText(aux);
385             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
386             msgBox.setDefaultButton(QMessageBox::No);
387             int ret = msgBox.exec();
388             switch (ret)
389             {
390                 case QMessageBox::Yes:
391             // Yes was clicked
392                 noMoreQso = true;
393                 qsos.clear();
394                 progress.setValue(numberOfQsos);
395                 return qsos;
396                 case QMessageBox::No:
397                 // No Save was clicked
398                 break;
399                 default:
400                 // should never be reached
401                 break;
402             }
403         }
404     } // END OF WHILE
405 
406     //qDebug() << "FileManager::adifLogExportReturnList: End: " << QString::number(qsos.count()) << QT_ENDL;
407     progress.setValue(numberOfQsos);
408     return qsos;
409 }
410 
adifQSOsExport(const QString & _fileName,QList<int> _qsos)411 bool FileManager::adifQSOsExport(const QString& _fileName, QList<int> _qsos)
412 {
413    //qDebug() << "FileManager::adifQSOsExport: " << _fileName << QT_ENDL;
414     int numberOfQSOs = _qsos.length();
415     if (numberOfQSOs<1)
416     {
417         //TODO: Warn the user NO QSOS TO EXPORT
418        //qDebug() << "FileManager::adifQSOsExport: No QSOs received to be exported" << QT_ENDL;
419     }
420     noMoreQso = false;
421    //qDebug() << "FileManager::adifQSOsExport - 01" << QT_ENDL;
422     QFile file(_fileName);
423     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) /* Flawfinder: ignore */
424         return false;
425    //qDebug() << "FileManager::adifQSOsExport - 02" << QT_ENDL;
426     QTextStream out(&file);
427 
428     QSqlQuery query;
429     QString queryString;
430     queryString = QString("SELECT * FROM log WHERE id IN (");
431     //qDebug() << "FileManager::adifQSOsExport - 10" << QT_ENDL;
432     QString numbers = QString();
433     foreach (int i, _qsos)
434     {
435         //qDebug() << "FileManager::adifQSOsExport - query: " << QString("id = '%1'").arg(i)  << QT_ENDL;
436         numbers = numbers + QString::number(i) ;
437         if (i != _qsos.last())
438         {
439            numbers = numbers + ", ";
440         }
441     }
442     queryString = queryString + numbers + ")";
443     //qDebug() << "FileManager::adifQSOsExport: writing the header" << QT_ENDL;
444     writeADIFHeader(out, ModeADIF, _qsos.length());
445    //qDebug() << "FileManager::adifQSOsExport: writing the body" << QT_ENDL;
446 
447     bool sqlOK = query.exec(queryString);
448     if (!sqlOK)
449     {
450     //qDebug() << "FileManager::adifQSOsExport: query error: " << query.lastQuery() << QT_ENDL;
451      emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
452     }
453    //qDebug() << "FileManager::adifQSOsExport: query: " << query.lastQuery() << QT_ENDL;
454     QProgressDialog progress(tr("Writing ADIF file..."), tr("Abort writing"), 0, numberOfQSOs, this);
455     progress.setMaximum(numberOfQSOs);
456     progress.setWindowModality(Qt::ApplicationModal);
457     progress.setWindowTitle(tr("Export progress"));
458     int currentQso = 0;
459     int step = util->getProgresStepForDialog(numberOfQSOs);
460     while ( (query.next()) && (!noMoreQso) )
461     {
462        //qDebug() << "FileManager::adifLogExportToFile -  Just in the While" << QT_ENDL;
463         if (query.isValid())
464         {
465             //qDebug() << "FileManager::adifLogExportToFile -  Query is Valid" << QT_ENDL;
466              writeQuery(query, out, ModeADIF, false, false, -1);
467         } // Closes the isValid
468         else {
469            //qDebug() << "FileManager::adifLogExportToFile -  Query is NOT Valid" << QT_ENDL;
470         }
471 
472        //qDebug() << "FileManager::adifLogExportToFile -  before showing progress: " << QString::number(currentQso) << QT_ENDL;
473         currentQso++;
474 
475         if (( (currentQso % step ) == 0) )
476         { // To update the speed I will only show the progress once each X QSOs
477            //qDebug() << "FileManager::adifLogExportToFile -  Showing progress: "  << QT_ENDL;
478             QString aux1 = tr("Writing ADIF file...\n QSO: ")  + QString::number(currentQso) + "/" + QString::number(numberOfQSOs);
479             progress.setLabelText(aux1);
480             progress.setValue(currentQso);
481         }
482        //qDebug() << "FileManager::adifLogExportToFile -  after showing progress (current%Step): " << QString::number(currentQso%step) << QT_ENDL;
483         if ( progress.wasCanceled() )
484         {
485             QMessageBox msgBox;
486             msgBox.setWindowTitle(tr("KLog - User cancelled"));
487             QString aux = QString(tr("You have canceled the file export. The file will be removed and no data will be exported.") + "\n" + tr("Do you still want to cancel?"));
488             msgBox.setText(aux);
489             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
490             msgBox.setDefaultButton(QMessageBox::No);
491             int ret = msgBox.exec();
492             switch (ret) {
493               case QMessageBox::Yes:
494                   // Yes was clicked
495                     noMoreQso = true;
496                   break;
497               case QMessageBox::No:
498                     // No Save was clicked
499                   break;
500               default:
501                     // should never be reached
502                   break;
503             }
504         }
505     }
506    //qDebug() << "FileManager::adifQSOsExport - END" << QT_ENDL;
507     return true;
508 }
509 
adifLogExportToFile(const QString & _fileName,const int _logN,bool justMarked,bool _qslRequested,bool _lotw)510 bool FileManager::adifLogExportToFile(const QString& _fileName, const int _logN, bool justMarked, bool _qslRequested , bool _lotw)
511 {
512     //adifLogExportToFile(const QString& _fileName, const int _logN=0, bool justMarked = false, bool _qslRequested = false, bool _lotw=false);
513     // If _logN = 0, then we will export ALL the logs.
514     //qDebug() << "FileManager::adifLogExportToFile: " << _fileName << QT_ENDL;
515     bool exportJustMarkedQSO = justMarked;
516     //bool marked = false;
517     bool exportOnlyQSLRequested = _qslRequested;
518     //bool haveMode = false;
519     noMoreQso = false;
520 
521     QFile file(_fileName);
522     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) /* Flawfinder: ignore */
523         return false;
524 
525     QTextStream out(&file);
526     QString aux1, aux2, queryString, bandst, bandrxst; // bandst & bandrxst are to check if the band is properly defined
527     //int nameCol = 0;
528     QSqlQuery query1;
529     int numberOfQsos = 0;
530     int currentQso = 0;
531     noMoreQso = false;
532     int step = 1;
533     //bool propsat=false; // Just to check if we have added the prop_mode needed by LOTW when SAT QSO
534     //bool bandOK = false; // Just to check if the band is properly defined
535     bool exportAll = false;
536     if (_logN <=0)
537     {
538         exportAll = true;
539     }
540     else
541     {
542         exportAll = false;
543     }
544 
545     bandst = QString();
546     bandrxst = QString();
547 
548     if (exportJustMarkedQSO)
549     {
550         //TODO: Count the marked QSO and adjust the numberOfQsos
551         numberOfQsos = 0;
552         QSqlQuery query;
553 
554         if (exportAll)
555         {
556             queryString = QString("SELECT COUNT(id) marked FROM log");
557         }
558         else
559         {
560             queryString = QString("SELECT COUNT(id) FROM log WHERE lognumber='%1'").arg(_logN);
561         }
562 
563         bool sqlOK = query.exec(queryString);
564         if (!sqlOK)
565         {
566             emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
567         }
568 
569         if (query.next())
570         {
571             if (query.isValid())
572             {
573                 numberOfQsos = query.value(0).toInt();
574             }
575         }
576            //qDebug() << "FileManager::adifLogExportToFile -  numberOfQsos = " << QString::number(numberOfQsos)<< QT_ENDL;
577     }
578     else
579     {
580         if (exportAll)
581         {
582             aux1 = "SELECT count(id) FROM log";
583         }
584         else
585         {
586             aux1 = QString("SELECT count(id) FROM log WHERE lognumber='%1'").arg(_logN);
587         }
588 
589         bool sqlOK = query1.exec(aux1);
590         if (!sqlOK)
591         {
592             emit queryError(Q_FUNC_INFO, query1.lastError().databaseText(), query1.lastError().nativeErrorCode(), query1.lastQuery());
593         }
594         query1.next();
595 
596         if (query1.isValid())
597         {
598             numberOfQsos = (query1.value(0)).toInt();
599         }
600           //qDebug() << "FileManager::adifLogExportToFile -  numberOfQsos = " << QString::number(numberOfQsos)<< QT_ENDL;
601     }
602 
603     //qDebug() << "FileManager::adifLogExportToFile - end get numberOfQsos = " << QString::number(numberOfQsos) << QT_ENDL;
604 
605     step = util->getProgresStepForDialog(numberOfQsos);
606     //qDebug() << "FileManager::adifLogExportToFile END -  Steps = " << QString::number(step) << QT_ENDL;
607     //step = getProgresStepForDialog(numberOfQsos);
608 
609     writeADIFHeader(out, ModeADIF, numberOfQsos);
610     /*
611     out << "ADIF v3.1.1 Export from KLog\nhttps://www.klog.xyz/klog\n<PROGRAMVERSION:" << QString::number(klogVersion.length()) << ">" << klogVersion << "\n<PROGRAMID:4>KLOG " << QT_ENDL;
612     out << "<APP_KLOG_QSOS:" << QString::number((QString::number(numberOfQsos)).length()) << ">" << QString::number(numberOfQsos) << QT_ENDL;
613 
614     QDateTime dateTime = (QDateTime::currentDateTime()).toUTC();
615 
616         out << "<APP_KLOG_LOG_DATE_EXPORT:" << QString::number((dateTime.toString("yyyyMMdd-hhmm")).length()) << ">" << dateTime.toString("yyyyMMdd-hhmm") << QT_ENDL;
617 
618     out << "<EOH>" << QT_ENDL;
619     */
620 
621     if (exportAll)
622     {
623         if (justMarked)
624         {
625             queryString = QString("SELECT * FROM log WHERE marked='X'");
626         }
627         else
628         {
629             queryString = QString("SELECT * FROM log");
630         }
631     }
632     else
633     {
634         if (justMarked)
635         {
636             queryString = QString("SELECT * FROM log WHERE marked='X' AND lognumber='%1'").arg(_logN);
637         }
638         else
639         {
640             queryString = QString("SELECT * FROM log WHERE lognumber='%1'").arg(_logN);
641         }
642     }
643 
644     QSqlQuery query;
645     bool sqlOK = query.exec(queryString);
646     if (!sqlOK)
647     {
648         emit queryError(Q_FUNC_INFO, query.lastError().databaseText(), query.lastError().nativeErrorCode(), query.lastQuery());
649     }
650 
651     QProgressDialog progress(tr("Writing ADIF file..."), tr("Abort writing"), 0, numberOfQsos, this);
652     progress.setMaximum(numberOfQsos);
653     progress.setWindowModality(Qt::ApplicationModal);
654     progress.setWindowTitle(tr("Export progress"));
655 
656     QSqlRecord rec = query.record();
657     QDateTime date;
658 
659     //qDebug() << "FileManager::adifLogExportToFile -  before the While" << QT_ENDL;
660 
661     while ( (query.next()) && (!noMoreQso) )
662     {
663         //qDebug() << "FileManager::adifLogExportToFile -  Just in the While" << QT_ENDL;
664 
665         if (query.isValid())
666         {
667             writeQuery(query, out, ModeADIF, exportJustMarkedQSO, exportOnlyQSLRequested, _logN);
668         } // Closes the isValid
669 
670         //qDebug() << "FileManager::adifLogExportToFile -  before showing progress: " << QString::number(currentQso) << QT_ENDL;
671         currentQso++;
672         if (( (currentQso % step )== 0) )
673         { // To update the speed I will only show the progress once each X QSOs
674             //qDebug() << "FileManager::adifLogExportToFile -  Showing progress: "  << QT_ENDL;
675             aux1 = tr("Writing ADIF file...\n QSO: ")  + QString::number(currentQso) + "/" + QString::number(numberOfQsos);
676             progress.setLabelText(aux1);
677             progress.setValue(currentQso);
678         }
679         //qDebug() << "FileManager::adifLogExportToFile -  after showing progress (current%Step): " << QString::number(currentQso%step) << QT_ENDL;
680 
681         if ( progress.wasCanceled() )
682         {
683             QMessageBox msgBox;
684             msgBox.setWindowTitle(tr("KLog - User cancelled"));
685             QString aux = QString(tr("You have canceled the file export. The file will be removed and no data will be exported.") + "\n" + tr("Do you still want to cancel?"));
686             msgBox.setText(aux);
687             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
688             msgBox.setDefaultButton(QMessageBox::No);
689             int ret = msgBox.exec();
690             switch (ret) {
691               case QMessageBox::Yes:
692                   // Yes was clicked
693                     noMoreQso = true;
694                   break;
695               case QMessageBox::No:
696                     // No Save was clicked
697                   break;
698               default:
699                     // should never be reached
700                   break;
701             }
702         }
703     } //Closes the While
704 
705       //qDebug() << "FileManager::adifLogExportToFile -  after the While" << QT_ENDL;
706     progress.setValue(numberOfQsos);
707 
708     if (noMoreQso)
709     {
710     //TODO: Remove the file (_fileName)
711           //qDebug() << "FileManager::adifLogExportToFile -  noMoreQSO = true" << QT_ENDL;
712         file.remove();
713         return false;
714     }
715     else
716     {
717           //qDebug() << "FileManager::adifLogExportToFile -  noMoreQSO = false" << QT_ENDL;
718         return writeBackupDate();
719     }
720 }
721 
printQs(const QStringList & _line)722 bool FileManager::printQs(const QStringList &_line)
723 //bool FileManager::printQs(const QString &_q, const QStringList _line)
724 {
725     QStringList qs = _line;
726 
727     for (int i = 0; i<qs.size(); i++){
728            //qDebug() << qs.at(i) << " ";
729     }
730 
731     return true;
732 }
733 
734 
adifLogExportMarked(const QString & _fileName)735 bool FileManager::adifLogExportMarked(const QString& _fileName)
736 {
737     //qDebug() << "FileManager::adifLogExportMarked:" << _fileName << QT_ENDL;
738     return adifLogExportToFile(_fileName, 0, true, false, false);
739 }
740 
adifLoTWReadLog2(const QString & fileName,const int logN)741 QList<int> FileManager::adifLoTWReadLog2(const QString& fileName, const int logN)
742 {
743   //qDebug() << "FileManager::adifLoTWReadLog2" << fileName << QT_ENDL;
744    //QSO qso(Q_FUNC_INFO);
745    QString stationCallSign;
746    stationCallSign.clear();
747    bool addNewQSOs = false;
748    bool askedToAddNewQSOs = false;
749    QList<int> _qsos;
750    _qsos.clear();
751    if (!dataProxy->doesThisLogExist(logN))
752    {
753        //qDebug() << "FileManager::adifLoTWReadLog2: ERROR: The log does not exist: " << QString::number(logN) << QT_ENDL;
754        return _qsos;
755    }
756    //qDebug() << "FileManager::adifLoTWReadLog2 - 10"  << QT_ENDL;
757    //QString fn = "blotw.adi";
758    QFile file( fileName );
759    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
760    {
761        //qDebug() << "FileManager::adifLoTWReadLog File not found" << fileName << QT_ENDL;
762        QMessageBox msgBox;
763        msgBox.setIcon(QMessageBox::Warning);
764        msgBox.setWindowTitle(tr("KLog - File not opened"));
765        QString aux = QString(tr("It was not possible to open the file %1 for reading.") ).arg(fileName);
766        msgBox.setText(tr("KLog was not able to read the LoTW file"));
767        msgBox.setInformativeText(aux);
768        msgBox.setStandardButtons(QMessageBox::Ok);
769        msgBox.exec();
770        return _qsos;
771    }
772    //qDebug() << "FileManager::adifLoTWReadLog2 - 20"  << QT_ENDL;
773     int numberOfQsos = 0;
774     QString line = QString();
775     bool hasEOH = false;
776     //qDebug() << "FileManager::adifLoTWReadLog2 - 30"  << QT_ENDL;
777     qint64 pos = file.pos();
778     //while ( !file.atEnd() && !hasEOH )
779     while ( !file.atEnd() )
780     {
781         line = (file.readLine()).toUpper();
782         if (line.contains("OWNCALL:"))
783         {
784             stationCallSign = (line.section(": ", 1, 1)).simplified();
785             if (!util->isValidCall(stationCallSign))
786             {
787                 stationCallSign = QString();
788             }
789         }
790         //qDebug() << "FileManager::adifLoTWReadLog2 - line-1: " << line  << QT_ENDL;
791         numberOfQsos = numberOfQsos + line.count("EOR>");
792         if ((line.count("<EOH>")>0) && (!hasEOH))
793         {
794             //qDebug() << "FileManager::adifLoTWReadLog2 - line-1: EOH FOUND!"   << QT_ENDL;
795             hasEOH = true;
796             pos = file.pos();
797         }
798     }
799     file.seek(pos);
800     int step = util->getProgresStepForDialog(numberOfQsos);
801     QProgressDialog progress(tr("Processing LoTW ADIF file..."), tr("Abort processing"), 0, numberOfQsos, this);
802     progress.setMaximum(numberOfQsos);
803     progress.setWindowModality(Qt::WindowModal);
804     progress.setValue(0);
805     progress.setWindowTitle(tr("LoTW reading"));
806     progress.setAutoClose(true);
807     //qDebug() << "FileManager::adifLoTWReadLog2 - After header while"  << QT_ENDL;
808     noMoreQso = false;
809     QStringList fields;
810     QSO qso;
811     qso.clear();
812     int i = 0;
813     while (!file.atEnd() && !noMoreQso)
814     {
815         fields.clear();
816         line = (file.readLine()).toUpper();
817         if (line.contains("<APP_LOTW_EOF>"))
818         {
819             noMoreQso = true;
820         }
821         fields << line.split("<", QT_SKIP);
822 
823         foreach(QString a, fields)
824         {
825             //qDebug() << "FileManager::adifLoTWReadLog2 - Fields: " << a  << QT_ENDL;
826             QString fullField = "<" + a.trimmed();
827             if (fullField.contains("<EOR>"))
828             {
829                 if (qso.isValid())
830                 {
831                    //qDebug() << "FileManager::adifLoTWReadLog2 VALID QSO: " << QT_ENDL;
832                     qso.setLogId(logN);
833                     if (util->isValidCall(stationCallSign))
834                     {
835                         QString aux = QString("<STATION_CALLSIGN:%1>%2").arg(QString::number(stationCallSign.length())).arg(stationCallSign);
836                         qso.setData(aux);
837                     }
838                     QList<int> dupeQsos;
839                     dupeQsos.clear();
840                     dupeQsos << dataProxy->isThisQSODuplicated(Q_FUNC_INFO, qso.getCall(), qso.getDateTimeOn(), dataProxy->getIdFromBandName(qso.getBand()), dataProxy->getIdFromModeName(qso.getMode()), duplicatedQSOSlotInSecs);
841 
842                     if ((dupeQsos.length()<1) && (!askedToAddNewQSOs) )
843                     {
844                         askedToAddNewQSOs = true;
845                         QMessageBox msgBox;
846                         msgBox.setWindowTitle(tr("KLog - Add new QSOs?"));
847                         QString aux = QString(tr("Do you want to add non existing QSOs to your local log?"));
848                         msgBox.setText(aux);
849                         msgBox.setDetailedText(tr("There are some QSOs in the LoTW log that are not in your local log."));
850                         msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
851                         msgBox.setDefaultButton(QMessageBox::No);
852                         int ret = msgBox.exec();
853                         switch (ret)
854                         {
855                             case QMessageBox::Yes:
856                             addNewQSOs = true;
857                             break;
858                             case QMessageBox::No:
859                             addNewQSOs = false;
860                             break;
861                             default:
862                             // should never be reached
863                             break;
864                         }
865                     }
866                     if ((dupeQsos.length()<1) && (addNewQSOs))
867                     {
868                         //qDebug() << "FileManager::adifLoTWReadLog2 -  New QSO ... adding ..."   << QT_ENDL;
869                         int lastId = dataProxy->addQSO(qso);
870                         if (lastId>0)
871                         {
872                             _qsos.append(lastId);
873                             //qDebug() << "FileManager::adifLoTWReadLog2 -  New QSO ... added ..."   << QT_ENDL;
874                         }
875                     }
876                     else
877                     {
878                         //qDebug() << "FileManager::adifLoTWReadLog2 -  EXisting QSO or not adding, Updating LoTW QSL status to: " <<  qso.getLoTWQSL_RCVD()  << QT_ENDL;
879                         if (dataProxy->setLoTWQSLRec (dupeQsos.at(0), qso.getLoTWQSL_RCVD(), qso.getLoTWQSLRDate()))
880                         {
881                             _qsos.append(dupeQsos.at(0));
882                             //qDebug() << "FileManager::adifLoTWReadLog2: Modified QSO: " << QString::number(dupeQsos.at(0)) << QT_ENDL;
883                         }
884                     }
885                     i++;
886                     qso.clear();
887                 }
888             }
889             else
890             {
891                 qso.setData(fullField);
892                 //qDebug() << Q_FUNC_INFO << ": - " << fullField;
893                 if (fullField.contains ("<CALL:"))
894                 {
895                     //qDebug() << Q_FUNC_INFO << ": Getting the DXCC for a call.";
896                     int _dxcc = dataProxy->getDXCCFromPrefix (qso.getCall ());
897                     //qDebug() << Q_FUNC_INFO << ": DXCC: "  << QString::number(_dxcc);
898                     if (util->isValidDXCC (_dxcc))
899                     {
900                         //qDebug() << Q_FUNC_INFO << ": - Adding a DXCC" ;
901                         qso.setDXCC (_dxcc);
902                     }
903                 }
904             }
905         }
906         if (( (i % step ) == 0) )
907         { // To update the speed I will only show the progress once each X QSOs
908             //qDebug() << "FileManager::adifLoTWReadLog2: ********************************   UPDATING THE MESSAGE! " << QString::number(i)  << QT_ENDL;
909             QString aux = tr("Processing LoTW ADIF file......\n QSO: %1 / %2 ").arg(i).arg(numberOfQsos);
910             progress.setLabelText(aux);
911             progress.setValue(i);
912             //qDebug() << "FileManager::adifLoTWReadLog2: ********************************   UPDATING THE MESSAGE: " << aux  << QT_ENDL;
913         }
914         if ( progress.wasCanceled() )
915         {
916             QMessageBox msgBox;
917             msgBox.setWindowTitle(tr("KLog - User cancelled"));
918             QString aux = QString(tr("You have canceled the LoTW processing. The process will be stopped and your log may not be completely updated.") + "\n" + tr("Do you still want to cancel?"));
919             msgBox.setText(aux);
920             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
921             msgBox.setDefaultButton(QMessageBox::No);
922             int ret = msgBox.exec();
923             switch (ret)
924             {
925                 case QMessageBox::Yes:
926             // Yes was clicked
927                 noMoreQso = true;
928                 _qsos.clear();
929                 progress.setValue(numberOfQsos);
930                 return _qsos;
931                 //return qsos;
932                 case QMessageBox::No:
933                 // No Save was clicked
934                 break;
935                 default:
936                 // should never be reached
937                 break;
938             }
939         }
940     }
941 
942    //qDebug() << "FileManager::adifLoTWReadLog2 - END: " << QString::number(_qsos.length ()) << QT_ENDL;
943    return _qsos;
944 }
945 
adifLoTWReadLog(const QString & tfileName,const int logN)946 QList<int> FileManager::adifLoTWReadLog(const QString& tfileName, const int logN)
947 {
948    //qDebug() << "FileManager::adifLoTWReadLog: " << tfileName << QT_ENDL;
949     QString fileName = tfileName;
950     QList<int> readed;
951     //QElapsedTimer time1 = QTime::currentTime();
952 
953     readed.clear();
954     if (!dataProxy->doesThisLogExist(logN))
955     {
956        //qDebug() << "FileManager::adifLoTWReadLog: ERROR: The log does not exist: " << QString::number(logN) << QT_ENDL;
957         return readed;
958     }
959     QFile file( fileName );
960     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
961     {
962        //qDebug() << "FileManager::adifLoTWReadLog File not found" << fileName << QT_ENDL;
963         QMessageBox msgBox;
964         msgBox.setIcon(QMessageBox::Warning);
965         msgBox.setWindowTitle(tr("KLog - File not opened"));
966         QString aux = QString(tr("It was not possible to open the file %1 for reading.") ).arg(fileName);
967         msgBox.setText(tr("KLog was not able to read the LoTW file"));
968         msgBox.setInformativeText(aux);
969         msgBox.setStandardButtons(QMessageBox::Ok);
970         msgBox.exec();
971         return readed;
972     }
973 
974     //qso->clear();
975 
976     QStringList fields, qsToPass;
977     QStringList modifiedQSOList;    // This is to emit it so after reading the full file, it is possible to show a qtable showing the QSOs that have been modified.
978     modifiedQSOList.clear();
979     bool hasEOH = false;
980     int numberOfQsos = 0;
981     int i = 0;
982     //int numberOfQsosLoWTHeader = 0;
983     QString line = QString();
984     QString auxString = QString();
985     QString aux = QString();
986     qint64 pos; //Position in the file
987     bool inHeader = true;
988     pos = file.pos();
989     fields.clear();
990     qsToPass.clear();
991     QDateTime _dateTime;
992     bool alwaysAdd = false;
993     bool alwaysIgnore = false;
994     QString stationCallsign = QString();
995 
996     //int step = 1;
997 
998     while ( !file.atEnd()   )
999     {
1000         line = (file.readLine()).toUpper();
1001         numberOfQsos = numberOfQsos + line.count("EOR>");
1002         if ((line.count("<EOH>")>0) && (!hasEOH))
1003         {
1004             hasEOH = true;
1005         }
1006     }
1007 
1008     int step = util->getProgresStepForDialog(numberOfQsos);
1009 
1010     //<APP_LOTW_NUMREC:3>847
1011 
1012    //qDebug() << "FileManager::adifLoTWReadLog QSOs found: " << QString::number(numberOfQsos) << QT_ENDL;
1013    //qDebug() << "FileManager::adifLoTWReadLog STEP: " << QString::number(step) << QT_ENDL;
1014 
1015     QProgressDialog progress(tr("Reading LoTW file..."), tr("Abort reading"), 0, numberOfQsos, this);
1016     progress.setWindowModality(Qt::ApplicationModal);
1017     progress.setVisible(true);
1018     progress.setValue(0);
1019     progress.setMaximum(numberOfQsos);
1020 
1021     //step = util->getProgresStepForDialog(numberOfQsos);
1022 
1023     file.seek(pos);
1024 
1025     /*
1026        Optional header information may be included before the actual data in the file.
1027        To include optional header info, the first character of the file must be something other than <.
1028        Any amount of header info of any value except <eoh> may be included. The header info must be
1029        terminated with <eoh>. Any number of characters of any value except < may follow <eoh>.
1030        The first < after <eoh> is the start of the first field of the first data record in the file.
1031     */
1032 
1033    //qDebug() << "FileManager::adifLoTWReadLog: Going to read the HEADER" << QT_ENDL;
1034     //Read HEADER
1035     line = file.readLine().trimmed().toUpper();
1036    //qDebug() << "FileManager::adifLoTWReadLog: " << line << QT_ENDL;
1037 
1038     if ( (!(line.startsWith('<'))) && (inHeader) )
1039     { // The file has a header
1040         if (line.contains("<EOH>")) // To check if the first line contains the EOR but not alone in the line.
1041         {
1042             inHeader = false;
1043         }
1044 
1045         line.clear(); // We should finish the if with real data in "line" or a clear one.
1046         while ( inHeader && hasEOH)
1047         {
1048             line = file.readLine().trimmed().toUpper();
1049             if (line.contains("OWNCALL:"))
1050             {
1051                 stationCallsign = line.section(": ", 1, 1);
1052                 if (!util->isValidCall(stationCallsign))
1053                 {
1054                     stationCallsign = QString();
1055                 }
1056             }
1057             if (line.contains("<EOH>"))
1058             {
1059                 inHeader = false;
1060                 line.clear(); //TODO: Maybe this clearing deletes a line that may have data...
1061             }
1062         }
1063         pos = file.pos();
1064     }
1065     else if (!(line.startsWith('<')))
1066     {   // The file does not have any header.
1067         // Reading the first QSO...
1068         /*
1069             Cases:
1070                1.- One big line with several QSO
1071                2.- One QSO uses several lines.                      - THIS IS USUALLY THE CASE FOR LOTW
1072                3.- Last line of one QSO includes data of the next one
1073         */
1074         inHeader = true;
1075         while (inHeader)
1076         {
1077             pos = file.pos();
1078             line = file.readLine().trimmed().toUpper();
1079             if (  (line.startsWith('<'))  )
1080             {
1081                 inHeader = false;
1082                 line.clear(); //TODO: Maybe this clearing deletes a line that may have data...
1083             }
1084         }
1085     }
1086 
1087     file.seek(pos);
1088     //START reading QSO data...
1089    //qDebug() << "FileManager::adifLoTWReadLog: QSO data reading started..."  << QT_ENDL;
1090     QDate _tdate;
1091     noMoreQso = false;
1092     //time1.start();
1093     while (!noMoreQso )
1094     {
1095         if (!file.atEnd())
1096         {
1097             line.clear();
1098             line.append(file.readLine().trimmed().toUpper());
1099             if (line.contains("<APP_LOTW_EOF>"))
1100             {
1101                 noMoreQso = true;
1102             }
1103             //line.append(file.readLine().toUpper()); // TODO: Check if we should remove extra spaces,tabs and so on...
1104            //qDebug() << "FileManager::adifLoTWReadLog-line:" << line << QT_ENDL;
1105             fields << line.split("<", QT_SKIP);
1106 
1107 
1108             qsToPass.clear();
1109             auxString.clear();
1110             foreach (aux, fields)
1111             {
1112                 aux = aux.trimmed();
1113                 if ( (aux.contains('>')) && (auxString.length() > 0) )
1114                 {
1115                     qsToPass.last() = qsToPass.last() + auxString;
1116                        //qDebug() << "FileManager::adifLoTWReadLog Modified in qsToPass: " << qsToPass.last() << QT_ENDL;
1117                     //qsToPass << aux.trimmed();
1118                    //qDebug() << "FileManager::adifLoTWReadLog Added to qsToPass: " << aux.trimmed() << QT_ENDL;
1119                     auxString.clear();
1120                 }
1121                 else if (( aux.contains('>')) && (auxString.length() <= 0) )
1122                 {
1123                     qsToPass << aux.trimmed();
1124                 }
1125                 else
1126                 {
1127                     auxString = auxString + "-" + aux.trimmed();
1128                    //qDebug() << "FileManager::adifLoTWReadLog auxString: " << auxString << QT_ENDL;
1129                 }
1130                //qDebug() << "FileManager::adifLoTWReadLog fields: " << aux << QT_ENDL;
1131             }
1132 
1133             if (auxString.length()>0)
1134             {
1135                    //qDebug() << "FileManager::adifLoTWReadLog auxString2: " << auxString << QT_ENDL;
1136                 qsToPass.last() = qsToPass.last() + auxString.trimmed();
1137             }
1138              //qDebug() << "FileManager::adifLoTWReadLog END fields" << QT_ENDL;
1139                //qDebug() << "FileManager::adifLoTWReadLog Mod: " << qsToPass.join("/") << QT_ENDL;
1140              //qDebug() << "FileManager::adifLoTWReadLog END2 fields" << QT_ENDL;
1141 
1142             fields = qsToPass;
1143 
1144             if (fields.contains("EOR>")) // We are going to add a QSO to the log... prepare the Query!
1145             {
1146                //qDebug() << "FileManager::adifLoTWReadLog: START of QSO adding"<< QT_ENDL;
1147                 //dataProxy->isThisQSODuplicated()
1148 
1149                 //int getDuplicatedQSOId(const QString &_qrz, const QString &_date, const QString &_time, const int _band, const int _mode);
1150                 QString str, _call, _date, _time, _band, _mode;
1151                 double _freq = 0.0;
1152                 bool haveBand = false;
1153                 QDate _qslrdate = QDate::currentDate();
1154                 QString _satName = QString();
1155 
1156                 bool qsl_rcvd = false;
1157                 QString field, data;
1158                 QStringList clearAdif;
1159                 bool validQSO = false;
1160                 int modifiedQSO = -1;
1161                 foreach (str, fields)
1162                 {
1163                     //field = readAdifField("<"+str).at(0);
1164                     clearAdif.clear();
1165                     clearAdif << readAdifField("<"+str);
1166                   //qDebug() << "FileManager::adifLoTWReadLog: clearAdif length: " << QString::number(clearAdif.length()) << QT_ENDL;
1167                     if (clearAdif.length()==2)
1168                     {
1169                         validQSO = true;
1170                         field = clearAdif.at(0);
1171                         data = clearAdif.at(1);
1172 
1173                       //qDebug() << "FileManager::adifLoTWReadLog: field: " << field << QT_ENDL;
1174                       //qDebug() << "FileManager::adifLoTWReadLog: data: " << data << QT_ENDL;
1175                         if (field == "CALL")
1176                         {
1177                             if (util->isValidCall(data))
1178                             {
1179                                 _call = data;
1180                             }
1181                             else
1182                             {
1183                                 _call = util->getAValidCall(data);
1184                             }
1185                         }
1186                         else if (field == "QSO_DATE")
1187                         {
1188                            //qDebug() << "FileManager::adifLoTWReadLog: field: " << field << QT_ENDL;
1189                            //qDebug() << "FileManager::adifLoTWReadLog: data: " << data << QT_ENDL;
1190 
1191                              _tdate = util->getDateFromADIFDateString(data);
1192                              if (_tdate.isValid())
1193                              {
1194                                  _dateTime.setDate(_tdate);
1195                              }
1196                         }
1197                         else if (field == "TIME_ON")
1198                         {
1199                             //qDebug() << "FileManager::adifLoTWReadLog: field: " << field << QT_ENDL;
1200                             //qDebug() << "FileManager::adifLoTWReadLog: data: " << data << QT_ENDL;
1201                             _time = data;
1202 
1203                             if (util->getTimeFromADIFTimeString(data).isValid())
1204                             {
1205                                 _dateTime.setTime(util->getTimeFromADIFTimeString(data));
1206                             }
1207                         }
1208                         else if (field == "BAND")
1209                         {
1210                             _band  = data;
1211                             haveBand = true;
1212                         }
1213                         else if (field == "MODE")
1214                         {
1215                             _mode = data;
1216                         }
1217                         else if (field == "FREQ") // In MHz with 5 decimal places
1218                         {
1219                             if (haveBand)
1220                             {
1221                                 int bandi = dataProxy->getIdFromBandName(_band);
1222                                 if (dataProxy->getBandIdFromFreq(data.toDouble()) == bandi)
1223                                 {
1224                                     _freq = data.toDouble();
1225                                 }
1226                                 else
1227                                 {
1228                                     _freq = dataProxy->getLowLimitBandFromBandId(bandi);
1229                                     // IF band is defined but not the same than freq, Band wins
1230                                 }
1231                             }
1232                             else
1233                             {
1234                                 _freq = data.toDouble();
1235 
1236                                 int bandi = dataProxy->getBandIdFromFreq(data.toDouble());
1237                                 if (bandi>=0)
1238                                 {
1239                                     _band = dataProxy->getBandNameFromFreq(_freq);
1240                                     haveBand = true;
1241                                 }
1242                              }
1243                         }
1244                         else if (field == "APP_LOTW_RXQSO")
1245                         {
1246                             //_qslsdate = data;
1247                         }
1248                         else if (field == "QSL_RCVD")
1249                         {
1250                             if (data == "Y")
1251                             {
1252                                 qsl_rcvd = true;
1253                             }
1254                         }
1255                         else if (field == "QSLRDATE")
1256                         {
1257                             if ((util->getDateFromADIFDateString(data)).isValid())
1258                             {
1259                                 _qslrdate = util->getDateFromADIFDateString(data);
1260                             }
1261                         }
1262                         else if (field == "SAT_NAME")
1263                         {
1264                             if (dataProxy->getSatelliteName(data).length()>0)
1265                             {
1266                                 _satName = data;
1267                             }
1268                         }
1269                         else
1270                         {
1271                             //Unknown ADIF received.
1272                         }
1273                     }
1274                     else
1275                     {
1276                        //qDebug() << "FileManager::adifLoTWReadLog: NOT VALID ADIF RECEIVED: " << "<" + str << QT_ENDL;
1277                     }
1278                 }
1279                //qDebug() << "FileManager::adifLoTWReadLog: If QSO is valid, we will call the addQSOToList" << "<" + str << QT_ENDL;
1280               //qDebug() << "FileManager::adifLoTWReadLog: New Add String query: " << QT_ENDL;
1281                //dataProxy->addQSO(&qso);
1282                 if (validQSO)
1283                 {
1284                     modifiedQSO = -2; //
1285                     if (qsl_rcvd)
1286                     {
1287                          modifiedQSO = dataProxy->lotwUpdateQSLReception (_call, _dateTime, _band, _mode, _qslrdate);
1288                     }
1289                    //qDebug() << "FileManager::adifLoTWReadLog: QSO Modified:  " << QString::number(modifiedQSO) << QT_ENDL;
1290                     if (modifiedQSO>0)
1291                     {
1292                       //qDebug() << "FileManager::adifLoTWReadLog: QSO Modified:  " << _call << QT_ENDL;
1293                        readed.append(modifiedQSO);
1294                        modifiedQSOList << _call << util->getDateTimeSQLiteStringFromDateTime(_dateTime) << _band << _mode;// << util->getDateSQLiteStringFromDate(_qslrdate);
1295                        emit addQSOToList(modifiedQSOList);
1296                        modifiedQSOList.clear();
1297                     }
1298                     else if ((modifiedQSO == -1 ) && (!qsl_rcvd))
1299                     {
1300                         bool ignoreQSO = false;
1301                       //qDebug() << "FileManager::adifLoTWReadLog: QSO NOT found but confirmed by LoTW - Is it an error or should I add it to the log? " << _call << QT_ENDL;
1302                         if (alwaysAdd)
1303                         {
1304                            //qDebug() << "FileManager::adifLoTWReadLog: ADD THE QSO AUTOMATICALLY!!!"  << QT_ENDL;
1305                             modifiedQSO = dataProxy->addQSOFromLoTW(_call, _dateTime, _mode, _band, _freq, _qslrdate, stationCallsign, logN);
1306                             if (modifiedQSO>0)
1307                             {
1308                                 readed.append(modifiedQSO);
1309                               //qDebug() << "FileManager::adifLoTWReadLog QSO ADDED readed: " << QString::number(readed.length()) << QT_ENDL;
1310                                 modifiedQSOList << _call << util->getDateTimeSQLiteStringFromDateTime(_dateTime) << _band << _mode;// << util->getDateSQLiteStringFromDate(_qslrdate);
1311                                 emit addQSOToList(modifiedQSOList);
1312                                 modifiedQSOList.clear();
1313                               //qDebug() << "FileManager::adifLoTWReadLog: QSO ADDED: " << QString::number(modifiedQSO) << QT_ENDL;
1314                             }
1315                             else
1316                             {
1317                               //qDebug() << "FileManager::adifLoTWReadLog: QSO NOT ADDED: " << QString::number(modifiedQSO) << QT_ENDL;
1318                             }
1319                         }
1320                         else if (alwaysIgnore)
1321                         {
1322                           //qDebug() << "FileManager::adifLoTWReadLog: IGNORE THE QSO AUTOMATICALLY!!!"  << QT_ENDL;
1323                         }
1324                         else
1325                         {
1326                             if (askUserToAddThisQSOToLog(_call, _dateTime, _mode, _band, _freq, _qslrdate))
1327                             {
1328                               //qDebug() << "FileManager::adifLoTWReadLog: ADD THE QSO !!!"  << QT_ENDL;
1329                                 modifiedQSO = dataProxy->addQSOFromLoTW(_call, _dateTime, _mode, _band, _freq, _qslrdate, stationCallsign, logN);
1330                                 if (modifiedQSO>0)
1331                                 {
1332                                     readed.append(modifiedQSO);
1333                                   //qDebug() << "FileManager::adifLoTWReadLog QSO ADDED-2 readed: " << QString::number(readed.length()) << QT_ENDL;
1334                                     modifiedQSOList << _call << util->getDateTimeSQLiteStringFromDateTime(_dateTime) << _band << _mode;// << util->getDateSQLiteStringFromDate(_qslrdate);
1335                                     emit addQSOToList(modifiedQSOList);
1336                                     modifiedQSOList.clear();
1337                                   //qDebug() << "FileManager::adifLoTWReadLog: QSO ADDED-2: " << QString::number(modifiedQSO) << QT_ENDL;
1338                                 }
1339                                 else
1340                                 {
1341                                   //qDebug() << "FileManager::adifLoTWReadLog: QSO NOT ADDED-2: " << QString::number(modifiedQSO) << QT_ENDL;
1342                                 }
1343                             }
1344                             else
1345                             {
1346                               //qDebug() << "FileManager::adifLoTWReadLog: IGNORE THE QSO !!!"  << QT_ENDL;
1347                             }
1348                             if (askUserToUseAlwaysSameAnswer())
1349                             {
1350                                 if (!ignoreQSO)
1351                                 {
1352                                     alwaysAdd = true;
1353                                 }
1354                                 else
1355                                 {
1356                                     alwaysIgnore = true;
1357                                 }
1358                             }
1359                             else
1360                             {
1361                                 alwaysAdd = false;
1362                                 alwaysIgnore = false;
1363                             }
1364                         }
1365                     }
1366                     else
1367                     {
1368                      //qDebug() << "FileManager::adifLoTWReadLog: QSO NOT Modified:  Error: " << QString::number(modifiedQSO) << " - " << _call << QT_ENDL;
1369                     }
1370                 }
1371                 _call.clear();
1372                 _date.clear();
1373                 _time.clear();
1374                 _band.clear();
1375                 _mode.clear();
1376                 _qslrdate = QDate::currentDate();
1377 
1378               //qDebug() << "FileManager::adifLoTWReadLog: END of QSO "<< QT_ENDL;
1379                 fields.clear();
1380                 i++;
1381             } // END of  if (fields.contains("EOR>")) // We are going to add a QSO to the log... !
1382             else
1383             {
1384                 if (file.atEnd())
1385                 {
1386                     noMoreQso = true;
1387                 }
1388                  //qDebug() << "FileManager::adifLoTWReadLog: fields DOES NOT contains EOR>" << QT_ENDL;
1389             }
1390         if (( (i % step ) == 0) )
1391         { // To update the speed I will only show the progress once each X QSOs
1392             //qDebug() << "FileManager::adifLoTWReadLog: MOD 0 - i = " << QString::number(i)  << QT_ENDL;
1393             aux = tr("Importing LoTW ADIF file...") + "\n" + tr(" QSO: ")  + QString::number(i) + "/" + QString::number(numberOfQsos);
1394             //aux = QString(tr("Importing LoTW ADIF file...\n QSO: %1/%2\nImporting speed: %3 QSOs/sec")).arg(i).arg(numberOfQsos).arg(step / (time1.elapsed()/1000));
1395 
1396             //time1.restart();
1397 
1398             progress.setLabelText(aux);
1399             progress.setValue(i);
1400         }
1401         if (i>=numberOfQsos)
1402         {
1403             noMoreQso = true;
1404         }
1405         if ( progress.wasCanceled() )
1406         {
1407             QMessageBox msgBox;
1408             msgBox.setWindowTitle(tr("KLog - User cancelled"));
1409             aux = QString(tr("You have canceled the file import. The file will be removed and no data will be imported.") + "\n" + tr("Do you still want to cancel?"));
1410             msgBox.setText(aux);
1411             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
1412             msgBox.setDefaultButton(QMessageBox::No);
1413             int ret = msgBox.exec();
1414             switch (ret) {
1415               case QMessageBox::Yes:
1416                   // Yes was clicked
1417                     noMoreQso = true;
1418                   break;
1419               case QMessageBox::No:
1420                     // No Save was clicked
1421                   break;
1422               default:
1423                     // should never be reached
1424                   break;
1425             }
1426         }
1427         }
1428     }
1429     progress.setValue(numberOfQsos);
1430   //qDebug() << "FileManager::adifLoTWReadLog - END: " << QString::number(readed.length()) << QT_ENDL;
1431     return readed;
1432 }
1433 
adifReadLog(const QString & tfileName,const int logN)1434 bool FileManager::adifReadLog(const QString& tfileName, const int logN)
1435 {
1436     //qDebug() << "FileManager::adifReadLog:" << tfileName << QT_ENDL;
1437     //QElapsedTimer time1;
1438 
1439     //int n = 0;
1440     //QSqlDatabase db = QSqlDatabase::database();
1441     //int maxLogs = dataProxy->getNumberOfManagedLogs(); // To manage several logs
1442     usePreviousStationCallsignAnswerAlways = false; // This is to ensure that the StationCallsign is used in the process Line function
1443     bool sqlOK = true;
1444     QStringList queries = QStringList();
1445     queries.clear();
1446     QSqlQuery query;
1447 
1448     QStringList fields, qsToPass, qsAux;
1449     QStringList currentQSOfields = QStringList();
1450     fields.clear();
1451     qsToPass.clear();
1452     qsAux.clear();
1453 
1454     QString fieldToAnalyze = QString();
1455     QString fileName = tfileName;
1456     QString line = QString();
1457     QString aux = QString();
1458     QString auxString = QString();
1459     QString _band = QString();
1460 
1461     bool inHeader = true;
1462     bool EOR = false;
1463     noMoreQso = false;
1464     bool isDupeQSO = false;
1465     bool askedToAddDupeQSOs = false;
1466     bool addDupeQSOs = false;
1467     qint64 pos; //Position in the file
1468     int i = 0; //Aunxiliar variable
1469     int numberOfQsos = 0;
1470     int step = 1;
1471     //int errorCode = -1;
1472     //int qsosInTransaction = 0;
1473     bool ignoreErrorCode19 = false;
1474 
1475     QFile file( fileName );
1476 
1477     //bool moreThanOneLog = adifCheckMoreThanOneLog(file);
1478     int howManyLogs = howManyLogsInFile(file);
1479     fillHashLog(file);
1480     //I am creating several logs when importing a file
1481     //We need to fill the hashLog to process then in processLog
1482 
1483     //bool keepLogsInFile = false;
1484 
1485       //qDebug() << "FileManager::adifReadLog: Logs: " << QString::number(howManyLogs) << QT_ENDL;
1486 
1487     if (howManyLogs>1)
1488     {
1489         QMessageBox msgBox;
1490         msgBox.setWindowTitle(tr("KLog - Log selection"));
1491         aux = QString(tr("There is more than one log in this logfile.") + "\n" + tr("All logs will be imported into the current log.") + "\n" + tr("Do you want to continue?"));
1492         msgBox.setText(aux);
1493         msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
1494         msgBox.setDefaultButton(QMessageBox::No);
1495         int ret = msgBox.exec();
1496         switch (ret) {
1497           case QMessageBox::Yes:
1498               // Yes was clicked
1499                    //qDebug() << "FileManager::adifReadLog: clicked YES" << QT_ENDL;
1500                 //keepLogsInFile = true;
1501               break;
1502           case QMessageBox::No:
1503                 // No Save was clicked
1504                    //qDebug() << "FileManager::adifReadLog: clicked NO" << QT_ENDL;
1505                  //keepLogsInFile = false;
1506                  return false;
1507           default:
1508                 // should never be reached
1509                 //keepLogsInFile = false;
1510                 return false;
1511                    //qDebug() << "FileManager::adifReadLog: default" << QT_ENDL;
1512         }
1513     }
1514 
1515     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
1516     {
1517           //qDebug() << "FileManager::adifReadLog File not found" << fileName << QT_ENDL;
1518         return false;
1519     }
1520 
1521     bool hasEOH = false;
1522     pos = file.pos();
1523 
1524     while ( !file.atEnd()   )
1525     {
1526         line = (file.readLine()).toUpper();
1527         numberOfQsos = numberOfQsos + line.count("EOR>");
1528         if ((line.count("<EOH>")>0) && (!hasEOH))
1529         {
1530             hasEOH = true;
1531         }
1532     }
1533        //qDebug() << "FileManager::adifReadLog QSOs found: " << QString::number(numberOfQsos) << QT_ENDL;
1534 
1535     QProgressDialog progress(tr("Reading ADIF file..."), tr("Abort reading"), 0, numberOfQsos, this);
1536     /*progress.setWindowModality(Qt::WindowModal);*/
1537     progress.setWindowModality(Qt::ApplicationModal);
1538     progress.setVisible(true);
1539     progress.setValue(0);
1540     progress.setMaximum(numberOfQsos);
1541 
1542     step = util->getProgresStepForDialog(numberOfQsos);
1543 
1544     //step = getProgresStepForDialog(numberOfQsos);
1545        //qDebug() << "FileManager::adifReadLog (STEP)/Number: " << QString::number(step) << "/" << QString::number(numberOfQsos) << QT_ENDL;
1546        //qDebug() << "FileManager::adifReadLog (number & step: " << QString::number(numberOfQsos % step) << QT_ENDL;
1547 
1548     file.seek(pos);
1549 
1550     /*
1551     Optional header information may be included before the actual data in the file.
1552     To include optional header info, the first character of the file must be something other than <.
1553     Any amount of header info of any value except <eoh> may be included. The header info must be
1554     terminated with <eoh>. Any number of characters of any value except < may follow <eoh>.
1555     The first < after <eoh> is the start of the first field of the first data record in the file.
1556     */
1557 
1558        //qDebug() << "FileManager::adifReadLog: Going to read the HEADER" << QT_ENDL;
1559     //Read HEADER
1560     line = file.readLine().trimmed().toUpper();
1561        //qDebug() << "FileManager::adifReadLog: " << line << QT_ENDL;
1562 
1563     if ( (!(line.startsWith('<'))) && (inHeader) )
1564     { // The file has a header
1565         if (line.contains("<EOH>")) // To check if the first line contains the EOR but not alone in the line.
1566         {
1567             inHeader = false;
1568         }
1569 
1570         line.clear(); // We should finish the if with real data in "line" or a clear one.
1571         while ( inHeader && hasEOH)
1572         {
1573             line = file.readLine().trimmed().toUpper();
1574             if (line.contains("<EOH>"))
1575             {
1576                 inHeader = false;
1577                 line.clear(); //TODO: Maybe this clearing deletes a line that may have data...
1578             }
1579         }
1580         pos = file.pos();
1581     }
1582     else if (!(line.startsWith('<')))
1583     { // The file does not have any header.
1584       // Reading the first QSO...
1585         /*
1586           Cases:
1587             1.- One big line with several QSO
1588             2.- One QSO uses several lines.
1589             3.- Last line of one QSO includes data of the next one
1590         */
1591         inHeader = true;
1592         while (inHeader)
1593         {
1594             pos = file.pos();
1595             line = file.readLine().trimmed().toUpper();
1596             if (  (line.startsWith('<'))  )
1597             {
1598                 inHeader = false;
1599                 line.clear(); //TODO: Maybe this clearing deletes a line that may have data...
1600             }
1601         }
1602     }
1603 
1604     file.seek(pos);
1605 
1606     // START reading QSO data...
1607       //qDebug() << "FileManager::adifReadLog: QSO data reading started..."  << QT_ENDL;
1608 
1609     preparedQuery.prepare( "INSERT INTO log (call, qso_date, bandid, modeid, srx, stx, srx_string, stx_string, qso_date_off, band_rx, rst_sent, rst_rcvd, cqz, ituz, dxcc, address, age, cnty, comment, a_index, ant_az, ant_el, ant_path, arrl_sect, checkcontest, class, contacted_op, contest_id, country, credit_submitted, credit_granted, distance, eq_call, email, eqsl_qslrdate, eqsl_qslsdate, eqsl_qsl_rcvd, eqsl_qsl_sent, force_init, freq, freq_rx, gridsquare, my_gridsquare, iota, iota_island_id, my_iota, my_iota_island_id, k_index, lat, lon, my_lat, my_lon, lotw_qslrdate, lotw_qslsdate, lotw_qsl_rcvd, lotw_qsl_sent, clublog_qso_upload_date, clublog_qso_upload_status, max_bursts, ms_shower, my_antenna, my_city, my_cnty, my_country, my_cq_zone, my_name, name, operator, station_callsign, owner_callsign, my_rig, my_sig, my_sig_info, my_sota_ref, my_state, state, my_street, my_vucc_grids, notes, nr_bursts, nr_pings, pfx, precedence, prop_mode, public_key, qslmsg, qslrdate, qslsdate, qsl_rcvd, qsl_sent, qsl_rcvd_via, qsl_sent_via, qsl_via, qso_complete, qso_random, qth, rx_pwr, tx_pwr, sat_mode, sat_name, sfi, sig, sota_ref, swl, ten_ten, vucc_grids, web, points, multiplier, lognumber) VALUES (:call, :qso_date, :bandid, :modeid, :srx, :stx, :srx_string, :stx_string, :qso_date_off, :band_rx, :rst_sent, :rst_rcvd, :cqz, :ituz, :dxcc, :address, :age, :cnty, :comment, :a_index, :ant_az, :ant_el, :ant_path, :arrl_sect, :checkcontest, :class, :contacted_op, :contest_id, :country, :credit_submitted, :credit_granted, :distance, :eq_call, :email, :eqsl_qslrdate, :eqsl_qslsdate, :eqsl_qsl_rcvd, :eqsl_qsl_sent, :force_init, :freq, :freq_rx, :gridsquare, :my_gridsquare, :iota, :iota_island_id, :my_iota, :my_iota_island_id, :k_index, :lat, :lon, :my_lat, :my_lon, :lotw_qslrdate, :lotw_qslsdate, :lotw_qsl_rcvd, :lotw_qsl_sent, :clublog_qso_upload_date, :clublog_qso_upload_status, :max_bursts, :ms_shower, :my_antenna, :my_city, :my_cnty, :my_country, :my_cq_zone, :my_name, :name, :operator, :station_callsign, :owner_callsign, :my_rig, :my_sig, :my_sig_info, :my_sota_ref, :my_state, :state, :my_street, :my_vucc_grids, :notes, :nr_bursts, :nr_pings, :pfx, :precedence, :prop_mode, :public_key, :qslmsg, :qslrdate, :qslsdate, :qsl_rcvd, :qsl_sent, :qsl_rcvd_via, :qsl_sent_via, :qsl_via, :qso_complete, :qso_random, :qth, :rx_pwr, :tx_pwr, :sat_mode, :sat_name, :sfi, :sig, :sota_ref, :swl, :ten_ten, :vucc_grids, :web, :points, :multiplier, :lognumber)" );
1610 /*
1611     if (db.transaction())
1612     {
1613            //qDebug() << "FileManager::adifReadLog: Transaction Opened"  << QT_ENDL;
1614     }
1615     else
1616     {
1617            //qDebug() << "FileManager::adifReadLog: Transaction NOT Opened"  << QT_ENDL;
1618     }
1619 */
1620     //file.seek(pos);
1621     fields.clear();
1622    // while ( (!file.atEnd() ) && (!noMoreQso) && (sqlOK))
1623     //time1.start();
1624     while ((!noMoreQso) && (sqlOK))
1625     {
1626         if (!file.atEnd())
1627         {
1628             line.clear();
1629             line.append(file.readLine().trimmed().toUpper());
1630             //line.append(file.readLine().toUpper()); // TODO: Check if we should remove extra spaces,tabs and so on...
1631                //qDebug() << "FileManager::adifReadLog-line:" << line << QT_ENDL;
1632             //fields.clear(); //TODO: Check if I should clear fields... I think I should not because I could loose data if a line contains data after an <EOR>
1633              fields << line.split("<", QT_SKIP);
1634         }
1635 
1636         //TODO: Check what happens
1637 
1638            //qDebug() << "FileManager::adifReadLog START fields" << QT_ENDL;
1639         qsToPass.clear();
1640         auxString.clear();
1641         foreach (aux, fields)
1642         {
1643             QString fieldToCheck;
1644             fieldToCheck = "<" + aux;
1645             //QStringList _TEST;
1646             //_TEST.clear();
1647             //_TEST << util->getValidADIFFieldAndData('<'+fields.at(0));
1648             aux = aux.trimmed();
1649             if ( (aux.contains('>')) && (auxString.length() > 0) )
1650             {
1651                 //qsToPass << auxString + aux;
1652                 qsToPass.last() = qsToPass.last() + auxString;
1653                   //qDebug() << "FileManager::adifReadLog Modified in qsToPass: " << qsToPass.last() << QT_ENDL;
1654                 qsToPass << aux.trimmed();
1655                   //qDebug() << "FileManager::adifReadLog Added to qsToPass: " << aux.trimmed() << QT_ENDL;
1656                 auxString.clear();
1657             }
1658             else if (( aux.contains('>')) && (auxString.length() <= 0) )
1659             {
1660                 qsToPass << aux.trimmed();
1661             }
1662             else
1663             {
1664                 auxString = auxString + "-" + aux.trimmed();
1665                   //qDebug() << "FileManager::adifReadLog auxString: " << auxString << QT_ENDL;
1666             }
1667               //qDebug() << "FileManager::adifReadLog fields: " << aux << QT_ENDL;
1668         }
1669 
1670           //qDebug() << "FileManager::adifReadLog-W-1" << QT_ENDL;
1671 
1672         if (auxString.length()>0)
1673         {
1674               //qDebug() << "FileManager::adifReadLog auxString2: " << auxString << QT_ENDL;
1675             qsToPass.last() = qsToPass.last() + auxString.trimmed();
1676         }
1677 
1678            //qDebug() << "FileManager::adifReadLog END fields" << QT_ENDL;
1679            //qDebug() << "FileManager::adifReadLog Mod: " << qsToPass.join("/") << QT_ENDL;
1680            //qDebug() << "FileManager::adifReadLog END2 fields" << QT_ENDL;
1681 
1682         fields = qsToPass;
1683 
1684         if (fields.contains("EOR>")) // We are going to add a QSO to the log... prepare the Query!
1685         {
1686                //qDebug() << "FileManager::adifReadLog: fields contains EOR>" << QT_ENDL;
1687             preparedQuery.bindValue( ":lognumber", logN);
1688 
1689             while ( (!EOR) && (!fields.isEmpty()) )
1690             {
1691                    //qDebug() << "FileManager::adifReadLog-W-2" << QT_ENDL;
1692                 fieldToAnalyze = (fields.takeFirst()).trimmed();
1693 
1694                 if ( fieldToAnalyze.contains("EOR>") )
1695                 {
1696                         //qDebug() << "FileManager::adifReadLog-W-2.1" << QT_ENDL;
1697                     currentQSOfields << fieldToAnalyze;
1698                     isDupeQSO = processQsoReadingADIF(currentQSOfields, logN);
1699                     if (isDupeQSO && (!askedToAddDupeQSOs))
1700                     {
1701                         askedToAddDupeQSOs = true;
1702                         QMessageBox msgBox;
1703                         msgBox.setWindowTitle(tr("KLog - Add new QSOs?"));
1704                         QString aux = QString(tr("Do you want to add dupe QSOs to your local log?"));
1705                         msgBox.setText(aux);
1706                         msgBox.setDetailedText(tr("There are some QSOs in this logfile that may be dupes as they have same call, band & mode and a very close date."));
1707                         msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
1708                         msgBox.setDefaultButton(QMessageBox::No);
1709                         int ret = msgBox.exec();
1710                         switch (ret)
1711                         {
1712                             case QMessageBox::Yes:
1713                             addDupeQSOs = true;
1714                             break;
1715                             case QMessageBox::No:
1716                             addDupeQSOs = false;
1717                             break;
1718                             default:
1719                             // should never be reached
1720                             break;
1721                         }
1722                            //qDebug() << "FileManager::adifReadLog: isDupeQSO = true"  << QT_ENDL;
1723                     }
1724                 }
1725                 else
1726                 {
1727                        //qDebug() << "FileManager::adifReadLog: Not contains EOR"  << QT_ENDL;
1728 
1729                     if ((!fieldToAnalyze.contains('>')) && (currentQSOfields.length()>0))
1730                     {
1731                           //qDebug() << "FileManager::adifReadLog: Contains > & currentsQSOfields.length>0"  << QT_ENDL;
1732                         auxString = currentQSOfields.at(currentQSOfields.length()-1);
1733                         auxString = auxString + "\n" + fieldToAnalyze;
1734                         //currentQSOfields.at(currentQSOfields.length()) = auxString;
1735                         fieldToAnalyze = auxString;
1736                     }
1737                     currentQSOfields << fieldToAnalyze;
1738                     EOR = false;
1739                 }
1740             }
1741             if ((!isDupeQSO) || (addDupeQSOs))
1742             {
1743                 sqlOK = preparedQuery.exec();
1744             }
1745             else
1746             {
1747                 //qDebug() << "FileManager::adifReadLog: DUPE QSO, not adding it" << QT_ENDL;
1748             }
1749 
1750             //qDebug() << "FileManager::adifReadLog: executedQuery1: " << preparedQuery.executedQuery()  << QT_ENDL;
1751             //qDebug() << "FileManager::adifReadLog: executedQuery2: " << preparedQuery.executedQuery()  << QT_ENDL;
1752             //qDebug() << "FileManager::adifReadLog: LastQuery2: " << preparedQuery.lastQuery()  << QT_ENDL;
1753 
1754 
1755             if (( (i % step ) == 0) )
1756             { // To update the speed I will only show the progress once each X QSOs
1757                   //qDebug() << "FileManager::adifReadLog: MOD 0 - i = " << QString::number(i)  << QT_ENDL;
1758                 //aux = QString(tr("Importing LoTW ADIF file...\n QSO: %1/%2\nImporting speed: 0 QSOs/sec")).arg(i).arg(numberOfQsos);
1759                 /*
1760                  *qDebug() << "FileManager::adifReadLog: " << QString::number(step) << QT_ENDL;
1761                 //qDebug() << "FileManager::adifReadLog: " << QString::number(time1.elapsed()) << QT_ENDL;
1762                 //qDebug() << "FileManager::adifReadLog: " << QString::number(time1.elapsed()/1000) << QT_ENDL;
1763                 //qDebug() << "FileManager::adifReadLog: " << QString::number(step / (time1.elapsed()/1000)) << QT_ENDL;
1764 
1765                 if (time1.elapsed()/1000 != 0)
1766                 {
1767                     aux = QString(tr("Importing LoTW ADIF file...\n QSO: %1/%2\nImporting speed: %3 QSOs/sec")).arg(i).arg(numberOfQsos).arg(step / (time1.elapsed()/1000));
1768                 }
1769                 else
1770                 {
1771                     aux = QString(tr("Importing LoTW ADIF file...\n QSO: %1/%2\nImporting speed: 0 QSOs/sec")).arg(i).arg(numberOfQsos);
1772                 }
1773 
1774 
1775                 time1.restart();
1776                 */
1777                 aux = tr("Importing ADIF file...") + "\n" + tr(" QSO: ")  + QString::number(i) + "/" + QString::number(numberOfQsos);
1778 
1779                progress.setLabelText(aux);
1780                progress.setValue(i);
1781             }
1782             else
1783             {
1784                   //qDebug() << "FileManager::adifReadLog: Mod: "<< QString::number(i) << " mod " << QString::number(step) << " = " << QString::number(i % step) << QT_ENDL;
1785             }
1786 
1787             if (sqlOK)
1788             {
1789                 //qDebug() << "FileManager::adifReadLog: (1) in While sqlOK (QSO added) = TRUE"  << QT_ENDL;
1790             }
1791             else
1792             {
1793                 //errorCode = preparedQuery.lastError().nativeErrorCode();
1794                 //qDebug() << "FileManager::adifReadLog: QSO DUPE" << QT_ENDL;
1795 
1796                 //qDebug() << "FileManager::adifReadLog: (1) LastQuery: " << preparedQuery.lastQuery()  << QT_ENDL;
1797                 //qDebug() << "FileManager::adifReadLog: (1) LastError-data: " << preparedQuery.lastError().databaseText()  << QT_ENDL;
1798                 //qDebug() << "FileManager::adifReadLog: (1) LastError-driver: " << preparedQuery.lastError().driverText()  << QT_ENDL;
1799                 //qDebug() << "FileManager::adifReadLog: (1) LastError-n: " << QString::number(preparedQuery.lastError().nativeErrorCode() ) << QT_ENDL;
1800                 if (((preparedQuery.lastError().nativeErrorCode()).toInt() == 19) && (!ignoreErrorCode19))
1801                 { // There are some repeated QSO
1802                     QMessageBox msgBox;
1803                     msgBox.setWindowTitle(tr("KLog - Duplicated QSOs"));
1804                     aux = tr("It seems that there are some duplicated QSOs in the ADIF file you are importing. Do you want to continue? (Duped QSOs will not be imported)");
1805                     msgBox.setText(aux);
1806                     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No);
1807                     msgBox.setDefaultButton(QMessageBox::Yes);
1808                     int ret = msgBox.exec();
1809                     switch (ret) {
1810                       case QMessageBox::Yes:
1811                           // Yes was clicked
1812                                //qDebug() << "FileManager::adifReadLog: (1) clicked YES" << QT_ENDL;
1813                             sqlOK = true;
1814                           break;
1815                     case QMessageBox::YesToAll:
1816                         // Yes was clicked
1817                                //qDebug() << "FileManager::adifReadLog: (1) clicked YES to ALL" << QT_ENDL;
1818                             ignoreErrorCode19 = true;
1819                             sqlOK = true;
1820                         break;
1821                       case QMessageBox::No:
1822                           // No Save was clicked
1823                                //qDebug() << "FileManager::adifReadLog: (1) clicked NO" << QT_ENDL;
1824                              sqlOK = false;
1825                           break;
1826                       default:
1827                           // should never be reached
1828                             sqlOK = true;
1829                                //qDebug() << "FileManager::adifReadLog: (1) default" << QT_ENDL;
1830                           break;
1831                     }
1832                     //;
1833                 }
1834                 else if(((preparedQuery.lastError().nativeErrorCode()).toInt() == 19) && (ignoreErrorCode19))
1835                 {
1836                   sqlOK = true;
1837                     //qDebug() << "FileManager::adifReadLog: errorCode=19 && ignoreErrorCode19" << QT_ENDL;
1838                 }
1839                 else
1840                 {
1841                        //qDebug() << "FileManager::adifReadLog: (2) LastQuery: " << preparedQuery.lastQuery()  << QT_ENDL;
1842                        //qDebug() << "FileManager::adifReadLog: (2) LastError-data: " << preparedQuery.lastError().databaseText()  << QT_ENDL;
1843                        //qDebug() << "FileManager::adifReadLog: (2) LastError-driver: " << preparedQuery.lastError().driverText()  << QT_ENDL;
1844                        //qDebug() << "FileManager::adifReadLog: (2) LastError-n: " << QString::number(preparedQuery.lastError().nativeErrorCode() ) << QT_ENDL;
1845 
1846                     emit queryError( Q_FUNC_INFO, preparedQuery.lastError().databaseText(), preparedQuery.lastError().nativeErrorCode(), preparedQuery.lastQuery());
1847                     noMoreQso = true;
1848                     /*
1849                     QMessageBox msgBox;
1850                     aux = tr("An unexpected error ocurred while importing. Please send this code to the developer for analysis: ");
1851                     msgBox.setText(aux + "FM-1 #" + QString::number(errorCode) );
1852                     msgBox.setStandardButtons(QMessageBox::Ok);
1853                     msgBox.setDefaultButton(QMessageBox::Ok);
1854                     int ret = msgBox.exec();
1855                     switch (ret) {
1856                       case QMessageBox::Ok:
1857                           // Yes was clicked
1858                             //sqlOK = false;
1859                                //qDebug() << "FileManager::adifReadLog: (2) I have just set sqlOK=False (1)" << QT_ENDL;
1860                             noMoreQso = true;
1861                             return;
1862                           break;
1863                       default:
1864                           // should never be reached
1865                             //sqlOK = false;
1866                                //qDebug() << "FileManager::adifReadLog: (2) I have just set sqlOK=False (2)" << QT_ENDL;
1867                           break;
1868                     }
1869                     */
1870                 }
1871             }
1872             if ( progress.wasCanceled() )
1873             {
1874                 QMessageBox msgBox;
1875                 msgBox.setWindowTitle(tr("KLog - User cancelled"));
1876                 aux = QString(tr("You have canceled the file import. The file will be removed and no data will be imported.") + "\n" + tr("Do you still want to cancel?"));
1877                 msgBox.setText(aux);
1878                 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
1879                 msgBox.setDefaultButton(QMessageBox::No);
1880                 int ret = msgBox.exec();
1881                 switch (ret) {
1882                   case QMessageBox::Yes:
1883                       // Yes was clicked
1884                         noMoreQso = true;
1885                       break;
1886                   case QMessageBox::No:
1887                         // No Save was clicked
1888                       break;
1889                   default:
1890                         // should never be reached
1891                       break;
1892                 }
1893             }
1894             currentQSOfields.clear();
1895             queryPreparation(logN); // to clear Values
1896             //progress.setValue(i);
1897             i++;
1898         } // END of  if (fields.contains("EOR>")) // We are going to add a QSO to the log... !
1899         else
1900         {
1901             if (file.atEnd())
1902             {
1903                 noMoreQso = true;
1904             }
1905                //qDebug() << "FileManager::adifReadLog: fields DOES NOT contains EOR>" << QT_ENDL;
1906         }
1907         //TODO: Check how to stop the importing process
1908     } // END OF WHILE
1909 
1910     if (noMoreQso)
1911     {
1912            //qDebug() << "FileManager::adifReadLog: noMoreQso = true" << QT_ENDL;
1913         progress.setValue(numberOfQsos);
1914     }
1915     else
1916     {
1917            //qDebug() << "FileManager::adifReadLog: noMoreQso = false" << QT_ENDL;
1918     }
1919     if (sqlOK)
1920     {
1921            //qDebug() << "FileManager::adifReadLog: sqlOK = true" << QT_ENDL;
1922     }
1923     else
1924     {
1925            //qDebug() << "FileManager::adifReadLog: sqlOK = false" << QT_ENDL;
1926     }
1927 
1928     if (sqlOK)
1929     {
1930         /*
1931         if (db.commit())
1932         {
1933                //qDebug() << "FileManager::adifReadLog: Last commit OK"  << QT_ENDL;
1934         }
1935         else
1936         {
1937             errorCode = preparedQuery.lastError().nativeErrorCode();
1938             QMessageBox msgBox;
1939             aux = tr("An error ocurred while importing. No data will be imported. Please send this code to the developer for analysis: ");
1940             msgBox.setText(aux + "FM-3 #" + QString::number(errorCode));
1941             msgBox.setStandardButtons(QMessageBox::Ok);
1942             msgBox.setDefaultButton(QMessageBox::Ok);
1943 
1944             int ret = msgBox.exec();
1945             switch (ret) {
1946               case QMessageBox::Ok:
1947                   // Yes was clicked
1948                     sqlOK = false;
1949                        //qDebug() << "FileManager::adifReadLog: I have just set sqlOK=False (5)" << QT_ENDL;
1950                   break;
1951               default:
1952                   // should never be reached
1953                     sqlOK = false;
1954                        //qDebug() << "FileManager::adifReadLog: I have just set sqlOK=False (6)" << QT_ENDL;
1955                   break;
1956             }
1957 
1958             if (db.rollback())
1959             {}
1960             else
1961             {
1962               //TODO: Check the error if db.rollback returns false
1963             }
1964         }
1965         */
1966     }
1967     else
1968     {
1969            //qDebug() << "FileManager::adifReadLog: sqlOK = NOK"  << QT_ENDL;
1970     }
1971     progress.setValue(numberOfQsos);
1972        //qDebug() << "FileManager::adifReadLog END "  << QT_ENDL;
1973     return true;
1974 }
1975 
processQsoReadingADIF(const QStringList & _line,const int logNumber)1976 bool FileManager::processQsoReadingADIF(const QStringList &_line, const int logNumber) //, const bool _keepLogsInFile)
1977 //bool FileManager::processQsoReadingADIF(const QStringList _line, const int logNumber, const bool _keepLogsInFile, QHash<int, int> &_logs)
1978 {
1979        //qDebug() << "FileManager::processQsoReadingADIF: log: " << QString::number(logNumber) << QT_ENDL;
1980        //qDebug() << "FileManager::processQsoReadingADIF: log: " << _line.at(0) << QT_ENDL;
1981        //qDebug() << "FileManager::processQsoReadingADIF: " << _line.join("/") << QT_ENDL;
1982 
1983     //QHash<int, int> &hashLogs = _logs;
1984 
1985     //QFile &file = _f;
1986 
1987     //bool keepLogsInF;// = _keepLogsInFile; //TODO: Check if needed or remove it completely. This line is just to remove a warning
1988     int i = -1;
1989     QDate date, dateT;
1990     date = QDate();
1991     dateT = QDate();
1992     QTime time;
1993     QDateTime dateTime, dateTimeOFF;
1994     dateTime = QDateTime();
1995     QStringList qs = _line;
1996     QStringList oneField;
1997     QString field, data;
1998     QSqlQuery query;
1999 
2000     //confirmed = 0; // 0 means worked, 1 means confirmed
2001 
2002     //QString queryString, stringFields, stringData;
2003 
2004     QString aux; // Aux string
2005     QString aux2;
2006     QString qrzCall = QString();
2007     QString submode = QString();
2008     QString defaultRSTTX = util->getDefaultRST(QString());
2009     QString defaultRSTRX = util->getDefaultRST(QString());
2010 
2011     bool isDupeQSO = false;
2012     int modei = -1;
2013     int bandi = -1;
2014     int bandrxi = -1;
2015     bool rstRXr = false;
2016     bool rstTXr = false;
2017 
2018     //KLog does not understand (and will not import) a QSO without these fields
2019     bool haveCall = false;
2020     //QString wrongCall = QString();
2021     bool haveBand = false;
2022     bool bandRXDef = false;
2023     bool haveMode = false;
2024     bool haveSubMode = false;
2025     bool haveTime = false;
2026     bool haveDate = false;
2027     bool haveTimeOff = false;
2028     bool haveDateOff = false;
2029 
2030     bool haveFreqTX = false;
2031     bool haveFreqRX = false;
2032     bool hasStationCall = false;
2033     bool hasLotwQslSent = false;
2034     bool hasEqslQslSent = false;
2035     bool hasQrzQslSent = false;
2036     bool hasClublogQslSent = false;
2037     QString freqTX = QString();
2038 
2039     //bool ret;
2040     //int length = 0;
2041     //int currentLog = logNumber;
2042 
2043        //qDebug() << "FileManager::processQsoReadingADIF" << QString::number(qs.size()) << "/" << QString::number(logNumber) << QT_ENDL;
2044     //TODO: To remove the next line, it was just to measure the time it takes.
2045     ignoreUnknownAlways = true;
2046     QString str;
2047     //preparedQuery.bindValue( ":confirmed", '0' );
2048 
2049        //qDebug() << "FileManager::processQsoReadingADIF: Entering the foreach" << QT_ENDL;
2050     foreach (str, qs)
2051     {
2052            //qDebug() << "FileManager::processQsoReadingADIF: " << str << QT_ENDL;
2053         if ( !( (str.contains(":")) && (str.contains(">")) ) )
2054         {
2055                //qDebug() << "FileManager::processQsoReadingADIF: NOT (contains : and >): " << str << QT_ENDL;
2056         }
2057         else
2058         {
2059                //qDebug() << "FileManager::processQsoReadingADIF: (contains : and >): " << str << QT_ENDL;
2060 
2061             oneField = str.split(">", QT_SKIP);
2062 
2063            //qDebug() << "FileManager::processQsoReadingADIF: (oneField)" << oneField << QT_ENDL;
2064             if (checkADIFValidFormat(oneField))
2065             {
2066                 i = (qs.at(0)).count(":");
2067                 field = (oneField.at(0)).trimmed(); // Needs to be cleared FIELD:4:D
2068                 data = (oneField.at(1)).trimmed();
2069                 data = util->checkAndFixASCIIinADIF(data);
2070                //qDebug() << "FileManager::processQsoReadingADIF: field/data" << field << "/" << data << QT_ENDL;
2071 
2072                 if (i == 2)
2073                 { // DATE:8:D / 20141020
2074                     //length = (field.section(':', 1, 1)).toInt();
2075                     field = field.section(':', 0, 0);
2076                 }
2077                 else if (i == 1)
2078                 { // DATE:8 / 20141020
2079                     //length = (field.section(':', 1, 1)).toInt();
2080                     field = field.section(':', 0, 0);
2081                 }
2082                 else
2083                 {
2084                     //qDebug() << "FileManager::checkADIFValidFormat-1 " << QT_ENDL;
2085                     //return false;
2086                 }
2087 
2088                 //field = oneField.at(0).trimmed();
2089                 //data = oneField.at(1).trimmed();
2090 
2091                 //length = field.indexOf(":");
2092                 //field = field.left(length);
2093                 //qDebug() << "FileManager::processQsoReadingADIF (field/data): " << field << "/" << data << QT_ENDL;
2094 
2095                 if (field == "CALL")
2096                 {
2097                    //qDebug() << "FileManager::processQsoReadingADIF-CALL:" << data << QT_ENDL;
2098                     qrzCall = data;
2099                     haveCall = util->isValidCall(qrzCall);
2100                     if (haveCall)
2101                     {
2102                         //qDebug() << "FileManager::processQsoReadingADIF-CALL: Have CALL!!"  << QT_ENDL;
2103                         preparedQuery.bindValue( ":call", qrzCall );
2104                     }
2105                    //qDebug() << "FileManager::processQsoReadingADIF-CALL-END:" << data << QT_ENDL;
2106                 }
2107                 else if (field == "QSO_DATE")
2108                 {
2109                     //qDebug() << "FileManager::processQsoReadingADIF-QSO_DATE:" << data << QT_ENDL;
2110                     dateT = util->getDateFromADIFDateString(data);
2111 
2112                     if (dateT.isValid())
2113                     {
2114                         dateTime.setDate(dateT);
2115                         haveDate = true;
2116                     }
2117                     else {
2118                        //qDebug() << "FileManager::processQsoReadingADIF QSO_DATE is NOT VALID: " << data << QT_ENDL;
2119                     }
2120                 }
2121                 else if (field == "BAND")
2122                 {
2123                     //preparedQuery.bindValue( ":bandid", data );
2124                     i = dataProxy->getIdFromBandName(data);
2125                     //i = db->getBandIDFromName2(data);
2126                     if (i>=0)
2127                     {
2128                         preparedQuery.bindValue( ":bandid", QString::number(i) );
2129                         haveBand = true;
2130                         bandi = i;
2131                         //qDebug() << "FileManager::processQsoReadingADIF-Band: " << data << "/"  << QString::number(i) << QT_ENDL;
2132                     }
2133                     else
2134                     {
2135                         //qDebug() << "FileManager::processQsoReadingADIF-Band - Wrong band: " << data << "/"  << QString::number(i) << QT_ENDL;
2136                     }
2137                    /*
2138                     queryString = QString("SELECT id FROM band WHERE name ='%1'").arg(data);
2139                     query.exec(queryString);
2140                     query.next();
2141                     if (query.isValid())
2142                     {
2143                         preparedQuery.bindValue( ":bandid", query.value(0).toInt() );
2144                            //qDebug() << "FileManager::bprocessQsoReadingADIF-Band: " << data << QT_ENDL;
2145                     }
2146                     */
2147                 }
2148                 else if (field == "MODE")
2149                 {
2150                     modei = dataProxy->getSubModeIdFromSubMode(data); // get modeid
2151                     if (modei>=0)
2152                     {
2153                         {
2154                             if (!haveSubMode)
2155                             {
2156                                 preparedQuery.bindValue( ":modeid", QString::number(modei) );
2157                                 haveMode = true;
2158                                 haveSubMode = true;
2159                                 submode = dataProxy->getSubModeFromId(modei);
2160                             }
2161                         }
2162                     }
2163                 }
2164                 else if (field == "SUBMODE")
2165                 {
2166                     modei = dataProxy->getSubModeIdFromSubMode(data);
2167                     if (modei>=0)
2168                     {
2169                         preparedQuery.bindValue( ":modeid", QString::number(modei) );
2170                         preparedQuery.bindValue( ":submode", QString::number(modei) );
2171                         haveSubMode = true;
2172                         haveMode=true;
2173                         submode = data;
2174                         //submode = data;
2175                     }
2176                 }
2177                 else if (field == "SRX")
2178                 {
2179                     preparedQuery.bindValue( ":srx", data );
2180                        //qDebug() << "FileManager::bprocessQsoReadingADIF-srx: " << data << QT_ENDL;
2181                 }
2182                 else if (field == "STX")
2183                 {
2184                     preparedQuery.bindValue( ":stx", data );
2185                        //qDebug() << "FileManager::bprocessQsoReadingADIF-stx: " << data << QT_ENDL;
2186                 }
2187                 else if (field == "TIME_ON")
2188                 {
2189                     time = util->getTimeFromADIFTimeString(data);
2190 
2191                     if (time.isValid())
2192                     {
2193                         dateTime.setTime(time);
2194                         haveTime = true;
2195                     }
2196                 }
2197                 else if (field == "QSO_DATE_OFF")
2198                 {
2199                     dateT = util->getDateFromADIFDateString(data);
2200                     if (dateT.isValid())
2201                     {
2202                         dateTimeOFF.setDate(dateT);
2203                         haveDateOff = true;
2204                     }
2205                 }
2206                 else if (field == "BAND_RX")
2207                 {
2208                     i = dataProxy->getIdFromBandName(data);
2209                     //i = db->getBandIDFromName2(data);
2210                     if (i>=0)
2211                     {
2212                         preparedQuery.bindValue( ":band_rx", QString::number(i) );
2213                         bandRXDef = true;
2214                         bandrxi = i;
2215                     }
2216                 }
2217                 else if (field == "TIME_OFF")
2218                 {
2219                     time = util->getTimeFromADIFTimeString(data);
2220                     if (time.isValid())
2221                     {
2222                         dateTimeOFF.setTime(time);
2223                         haveTimeOff = true;
2224                     }
2225                 }
2226                 else if (field == "RST_SENT")
2227                 {
2228                     preparedQuery.bindValue( ":rst_sent", data );
2229                    //qDebug() << "FileManager::bprocessQsoReadingADIF-rst_rsent: " << data << QT_ENDL;
2230                     rstTXr = true;
2231                 }
2232                 else if (field == "RST_RCVD")
2233                 {
2234                    //qDebug() << "FileManager::bprocessQsoReadingADIF-rst_rcvd: " << data << QT_ENDL;
2235                     preparedQuery.bindValue( ":rst_rcvd", data );
2236                     rstRXr = true;
2237                 }
2238                 else if (field == "SRX_STRING")
2239                 {
2240                     preparedQuery.bindValue( ":srx_string", data );
2241                 }
2242                 else if (field == "STX_STRING")
2243                 {
2244                     preparedQuery.bindValue( ":stx_string", data );
2245                 }
2246                 else if (field == "CQZ")
2247                 {
2248                     preparedQuery.bindValue( ":cqz", data );
2249                     //cqz = data.toInt();
2250                 }
2251                 else if (field == "ITUZ")
2252                 {
2253                     preparedQuery.bindValue( ":ituz", data );
2254                     //ituz = data.toInt();
2255                 }
2256                 else if (field == "DXCC")
2257                 {
2258                     //dxcc = data.toInt();
2259                     preparedQuery.bindValue( ":dxcc", data );
2260                 }
2261                 else if (field == "ADDRESS")
2262                 {
2263                     preparedQuery.bindValue( ":address", data );
2264                 }
2265                 else if (field == "AGE")
2266                 {
2267                     preparedQuery.bindValue( ":age", data );
2268                 }
2269                 else if (field == "CNTY")
2270                 {
2271                     preparedQuery.bindValue( ":cnty", data );
2272                 }
2273                 else if (field == "COMMENT")
2274                 {
2275                     preparedQuery.bindValue( ":comment", data );
2276                 }
2277                 else if (field == "A_INDEX")
2278                 {
2279                     preparedQuery.bindValue( ":a_index", data );
2280                 }
2281                 else if (field == "ANT_AZ")
2282                 {
2283                     preparedQuery.bindValue( ":ant_az", data );
2284                 }
2285                 else if (field == "ANT_EL")
2286                 {
2287                     preparedQuery.bindValue( ":ant_el", data );
2288                 }
2289                 else if (field == "ANT_PATH")
2290                 {
2291                     preparedQuery.bindValue( ":ant_path", data );
2292                 }
2293                 else if (field == "ARRL_SECT")
2294                 {
2295                     preparedQuery.bindValue( ":arrl_sect", data );
2296                 }
2297                 else if (field == "AWARD_GRANTED")
2298                 {
2299                     preparedQuery.bindValue( ":award_granted", data );
2300                 }
2301                 else if (field == "AWARD_SUBMITTED")
2302                 {
2303                     preparedQuery.bindValue( ":award_submitted", data );
2304                 }
2305                 else if (field == "CHECKCONTEST")
2306                 {
2307                     preparedQuery.bindValue( ":checkcontest", data );
2308                 }
2309                 else if (field == "CLASS")
2310                 {
2311                     preparedQuery.bindValue( ":class", data );
2312                 }
2313                 else if (field == "CONT")
2314                 {
2315                     preparedQuery.bindValue( ":cont", data );
2316                 }
2317                 else if (field == "CONTACTED_OP")
2318                 {
2319                     preparedQuery.bindValue( ":contacted_op", data );
2320                 }
2321                 else if (field == "CONTEST_ID")
2322                 {
2323                     preparedQuery.bindValue( ":contest_id", data );
2324                 }
2325                 else if (field == "COUNTRY")
2326                 {
2327                     preparedQuery.bindValue( ":country", data );
2328                 }
2329                 else if (field == "CREDIT_SUBMITTED")
2330                 {
2331                     preparedQuery.bindValue( ":credit_submitted", data );
2332                 }
2333                 else if (field == "CREDIT_GRANTED")
2334                 {
2335                     preparedQuery.bindValue( ":credit_granted", data );
2336                 }
2337                 else if (field == "DISTANCE")
2338                 {
2339                     preparedQuery.bindValue( ":distance", data );
2340                 }
2341                 else if (field == "DARC_DOK")
2342                 {
2343                     preparedQuery.bindValue( ":darc_dok", data );
2344                 }
2345                 else if (field == "EQ_CALL")
2346                 {
2347                     preparedQuery.bindValue( ":eq_call", data );
2348                 }
2349                 else if (field == "EMAIL")
2350                 {
2351                     if (data.contains("@") && (data.contains(".")))
2352                     {
2353                        preparedQuery.bindValue( ":email", data );
2354                     }
2355                 }
2356                 else if (field == "EQSL_QSLRDATE")
2357                 {
2358                     dateT = util->getDateFromADIFDateString(data);
2359                     if (dateT.isValid())
2360                     {
2361                        preparedQuery.bindValue( ":eqsl_qslrdate", util->getDateSQLiteStringFromDate(dateT) );
2362                     }
2363                 }
2364                 else if (field == "EQSL_QSLSDATE")
2365                 {
2366                     dateT = util->getDateFromADIFDateString(data);
2367                     if (dateT.isValid())
2368                     {
2369                         preparedQuery.bindValue( ":eqsl_qslsdate", util->getDateSQLiteStringFromDate(dateT) );
2370                     }
2371                 }
2372                 else if (field == "EQSL_QSL_RCVD")
2373                 {
2374                     preparedQuery.bindValue( ":eqsl_qsl_rcvd", data );
2375                 }
2376                 else if (field == "EQSL_QSL_SENT")
2377                 {
2378                     preparedQuery.bindValue( ":eqsl_qsl_sent", data );
2379                     hasEqslQslSent = true;
2380                 }
2381                 else if (field == "FISTS")
2382                 {
2383                     preparedQuery.bindValue( ":fists", data );
2384                 }
2385                 else if (field == "FISTS_CC")
2386                 {
2387                     preparedQuery.bindValue( ":fists_cc", data );
2388                 }
2389                 else if (field == "FORCE_INIT")
2390                 {
2391                     preparedQuery.bindValue( ":force_init", data );
2392                 }
2393                 else if (field == "FREQ")
2394                 {
2395                        //qDebug() << "FileManager::processQsoReadingADIF  -FREQ: " << QString::number(data.toDouble()) << QT_ENDL;
2396                     if (haveBand)
2397                     {
2398                         if (dataProxy->getBandIdFromFreq(data.toDouble()) == bandi)
2399                         //if (db->isThisFreqInBand(db->getBandNameFromNumber(bandi), data))
2400                         {
2401                             preparedQuery.bindValue( ":freq", data);
2402                             haveFreqTX =true;
2403                             freqTX = data;
2404                         }
2405                         else
2406                         {
2407                             // IF band is defined but not the same than freq, Band wins
2408                         }
2409                     }
2410                     else
2411                     {
2412                         preparedQuery.bindValue( ":freq", data);
2413                         haveFreqTX =true;
2414                         freqTX = data;
2415                         i = dataProxy->getBandIdFromFreq(data.toDouble());
2416 
2417                         if (i>=0)
2418                         {
2419                             preparedQuery.bindValue( ":bandid", QString::number(i) );
2420                             haveBand = true;
2421                                //qDebug() << "FileManager::processQsoReadingADIF-Band: " << data << "/"  << QString::number(i) << QT_ENDL;
2422                         }
2423                     }
2424                 }
2425                 else if (field == "FREQ_RX")
2426                 {
2427                     if (bandRXDef)
2428                     {
2429                         if (dataProxy->getBandIdFromFreq(data.toDouble()) == bandrxi)
2430                         {
2431                             preparedQuery.bindValue( ":freq_rx", data);
2432                             haveFreqRX =true;
2433                         }
2434                         else
2435                         {
2436                             // IF band is defined but not the same than freq, Band wins
2437                         }
2438                     }
2439                     else
2440                     {
2441                         preparedQuery.bindValue( ":freq_rx", data);
2442                         haveFreqRX = true;
2443                         i = dataProxy->getBandIdFromFreq(data.toDouble());
2444 
2445                         if (i>=0)
2446                         {
2447                             preparedQuery.bindValue( ":band_rx", QString::number(i) );
2448                             bandRXDef = true;
2449                                //qDebug() << "FileManager::processQsoReadingADIF-Band: " << data << "/"  << QString::number(i) << QT_ENDL;
2450                         }
2451                     }
2452                 }
2453                 else if (field == "GRIDSQUARE")
2454                 {
2455                     preparedQuery.bindValue( ":gridsquare", data );
2456                 }
2457                 else if (field == "GUEST_OP")
2458                 {
2459                     preparedQuery.bindValue( ":guest_op", data );
2460                 }
2461                 else if (field == "HRDLOG_QSO_UPLOAD_DATE")
2462                 {
2463                     dateT = util->getDateFromADIFDateString(data);
2464                     if (dateT.isValid())
2465                     {
2466                         preparedQuery.bindValue( ":hrd_qso_upload_date", util->getDateSQLiteStringFromDate(dateT) );
2467                     }
2468                 }
2469                 else if (field == "HRDLOG_QSO_UPLOAD_STATUS")
2470                 {
2471                     preparedQuery.bindValue( ":hrd_qso_upload_status", data );
2472                 }
2473                 else if (field == "MY_GRIDSQUARE")
2474                 {
2475                     preparedQuery.bindValue( ":my_gridsquare", data );
2476                 }
2477                 else if (field == "MY_ANTENNA")
2478                 {
2479                     //qDebug() << ": MY_ANTENNA: " << data;
2480                     preparedQuery.bindValue( ":my_antenna", data );
2481                 }
2482                 else if (field == "IOTA")
2483                 {
2484                        //qDebug() << "FileManager::processQsoReadingADIF (IOTA): " << data << QT_ENDL;
2485                     data = awards->checkIfValidIOTA(data);
2486                     /*
2487                     if (data.length()==4) //EU-1
2488                     {
2489                         data.insert(3, "00");
2490                     }
2491                     else if (data.length()==5) //EU-01
2492                     {
2493                         data.insert(3, "0");
2494                     }
2495                     */
2496                     if (data.length() == 6)
2497                     {
2498                         preparedQuery.bindValue( ":iota", data );
2499                     }
2500                 }
2501                 else if (field == "IOTA_ISLAND_ID")
2502                 {
2503                     preparedQuery.bindValue( ":iota_island_id", data );
2504                 }
2505                 else if (field == "MY_IOTA")
2506                 {
2507                     /*
2508                     if (data.length()==4) //EU-1
2509                     {
2510                         data.insert(3, "00");
2511                     }
2512                     else if (data.length()==5) //EU-01
2513                     {
2514                         data.insert(3, "0");
2515                     }
2516                     */
2517                     data = awards->checkIfValidIOTA(data);
2518                     if (data.length() == 6)
2519                     {
2520                         preparedQuery.bindValue( ":my_iota", data );
2521                     }
2522                 }
2523                 else if (field == "MY_DXCC")
2524                 {
2525                     preparedQuery.bindValue( ":my_dxcc", data );
2526                 }
2527                 else if (field == "MY_FISTS")
2528                 {
2529                     preparedQuery.bindValue( ":my_fists", data );
2530                 }
2531                 else if (field == "MY_IOTA_ISLAND_ID")
2532                 {
2533                     preparedQuery.bindValue( ":my_iota_island_id", data );
2534                 }
2535                 else if (field == "K_INDEX")
2536                 {
2537                     preparedQuery.bindValue( ":k_index", data );
2538                 }
2539                 else if (field == "LAT")
2540                 {
2541                     preparedQuery.bindValue( ":lat", data );
2542                 }
2543                 else if (field == "LON")
2544                 {
2545                     preparedQuery.bindValue( ":lon", data );
2546                 }
2547                 else if (field == "MY_LAT")
2548                 {
2549                     preparedQuery.bindValue( ":my_lat", data );
2550                 }
2551                 else if (field == "MY_LON")
2552                 {
2553                     preparedQuery.bindValue( ":my_lon", data );
2554                 }
2555                 else if (field == "MY_ITU_ZONE")
2556                 {
2557                     preparedQuery.bindValue( ":my_itu_zone", data );
2558                 }
2559                 else if (field == "MY_POSTAL_CODE")
2560                 {
2561                     preparedQuery.bindValue( ":my_postal_code", data );
2562                 }
2563                 else if (field == "LOTW_QSLRDATE")
2564                 {
2565                     dateT = util->getDateFromADIFDateString(data);
2566                     if (dateT.isValid())
2567                     {
2568                         preparedQuery.bindValue( ":lotw_qslrdate", util->getDateSQLiteStringFromDate(dateT) );
2569                     }
2570                 }
2571                 else if (field == "LOTW_QSLSDATE")
2572                 {
2573                     dateT = util->getDateFromADIFDateString(data);
2574                     if (dateT.isValid())
2575                     {
2576                         preparedQuery.bindValue( ":lotw_qslsdate", util->getDateSQLiteStringFromDate(dateT) );
2577                     }
2578                 }
2579                 else if (field == "LOTW_QSL_RCVD")
2580                 {
2581                     preparedQuery.bindValue( ":lotw_qsl_rcvd", data );
2582                 }
2583                 else if (field == "LOTW_QSL_SENT")
2584                 {
2585                     preparedQuery.bindValue( ":lotw_qsl_sent", data );
2586                     hasLotwQslSent = true;
2587                 }
2588                 else if (field == "CLUBLOG_QSO_UPLOAD_DATE")
2589                 {
2590                     dateT = util->getDateFromADIFDateString(data);
2591                     if (dateT.isValid())
2592                     {
2593                         preparedQuery.bindValue( ":clublog_qso_upload_date", util->getDateSQLiteStringFromDate(dateT) );
2594                         hasClublogQslSent = true;
2595                     }
2596                 }
2597                 else if (field == "CLUBLOG_QSO_UPLOAD_STATUS")
2598                 {
2599                     preparedQuery.bindValue( ":clublog_qso_upload_status", data );
2600                 }
2601                 else if (field == "MAX_BURSTS")
2602                 {
2603                     preparedQuery.bindValue( ":max_bursts", data );
2604                 }
2605                 else if (field == "MS_SHOWER")
2606                 {
2607                     preparedQuery.bindValue( ":ms_shower", data );
2608                 }
2609                 else if (field == "MY_CITY")
2610                 {
2611                     preparedQuery.bindValue( ":my_city", data );
2612                 }
2613                 else if (field == "MY_CNTY")
2614                 {
2615                     preparedQuery.bindValue( ":my_cnty", data );
2616                 }
2617                 else if (field == "MY_COUNTRY")
2618                 {
2619                     preparedQuery.bindValue( ":my_country", data );
2620                 }
2621                 else if (field == "MY_CQ_ZONE")
2622                 {
2623                     preparedQuery.bindValue( ":my_cq_zone", data );
2624                 }
2625                 else if (field == "MY_NAME")
2626                 {
2627                     preparedQuery.bindValue( ":my_name", data );
2628                 }
2629                 else if (field == "NAME")
2630                 {
2631                     preparedQuery.bindValue( ":name", data );
2632                 }
2633                 else if (field == "OPERATOR")
2634                 {
2635                     if (util->isValidCall(data))
2636                     {
2637                         preparedQuery.bindValue( ":operator", data );
2638                     }
2639                 }
2640                 else if (field == "STATION_CALLSIGN")
2641                 {
2642                     if (util->isValidCall(data))
2643                     {
2644                         hasStationCall = true;
2645                         preparedQuery.bindValue( ":station_callsign", data );
2646                     }
2647                 }
2648                 else if (field == "OWNER_CALLSIGN")
2649                 {
2650                     if (util->isValidCall(data))
2651                     {
2652                         preparedQuery.bindValue( ":owner_callsign", data );
2653                     }
2654                 }
2655                 else if (field == "MY_RIG")
2656                 {
2657                     preparedQuery.bindValue( ":my_rig", data );
2658                 }
2659                 else if (field == "MY_SIG")
2660                 {
2661                     preparedQuery.bindValue( ":my_sig", data );
2662                 }
2663                 else if (field == "MY_SIG_INFO")
2664                 {
2665                     preparedQuery.bindValue( ":my_sig_info", data );
2666                 }
2667                 else if (field == "MY_SOTA_REF")
2668                 {
2669                     preparedQuery.bindValue( ":my_sota_ref", data );
2670                 }
2671                 else if (field == "MY_STATE")
2672                 {
2673                     preparedQuery.bindValue( ":my_state", data );
2674                 }
2675                 else if (field == "STATE")
2676                 {
2677                     preparedQuery.bindValue( ":state", data );
2678                 }
2679                 else if (field == "MY_STREET")
2680                 {
2681                     preparedQuery.bindValue( ":my_street", data );
2682                 }
2683                 else if (field == "MY_USACA_COUNTIES")
2684                 {
2685                     preparedQuery.bindValue( ":my_usaca_counties", data );
2686                 }
2687                 else if (field == "MY_VUCC_GRIDS")
2688                 {
2689                     preparedQuery.bindValue( ":my_vucc_grids", data );
2690                 }
2691                 else if (field == "NOTES")
2692                 {
2693                     preparedQuery.bindValue( ":notes", data );
2694                 }
2695                 else if (field == "NR_BURSTS")
2696                 {
2697                     preparedQuery.bindValue( ":nr_bursts", data );
2698                 }
2699                 else if (field == "NR_PINGS")
2700                 {
2701                     preparedQuery.bindValue( ":nr_pings", data );
2702                 }
2703                 else if (field == "PFX")
2704                 {
2705                     preparedQuery.bindValue( ":pfx", data );
2706                 }
2707                 else if (field == "PRECEDENCE")
2708                 {
2709                     preparedQuery.bindValue( ":precedence", data );
2710                 }
2711                 else if (field == "PROP_MODE")
2712                 {
2713                     preparedQuery.bindValue( ":prop_mode", data );
2714                 }
2715                 else if (field == "PUBLIC_KEY")
2716                 {
2717                     preparedQuery.bindValue( ":public_key", data );
2718                 }
2719                 else if (field == "QRZCOM_QSO_UPLOAD_DATE")
2720                 {
2721                     dateT = util->getDateFromADIFDateString(data);
2722                     if (dateT.isValid())
2723                     {
2724                         preparedQuery.bindValue( ":qrzcom_qso_upload_date", util->getDateSQLiteStringFromDate(dateT));
2725                         hasQrzQslSent = true;
2726                     }
2727                 }
2728                 else if (field == "QRZCOM_QSO_UPLOAD_STATUS")
2729                 {
2730                     preparedQuery.bindValue( ":qrzcom_qso_upload_status", data );
2731                 }
2732                 else if (field == "QSLMSG")
2733                 {
2734                     preparedQuery.bindValue( ":qslmsg", data );
2735                 }
2736                 else if (field == "QSLRDATE")
2737                 {
2738                     dateT = util->getDateFromADIFDateString(data);
2739                     if (dateT.isValid())
2740                     {
2741                         preparedQuery.bindValue( ":qslrdate", util->getDateSQLiteStringFromDate(dateT));
2742                     }
2743                 }
2744                 else if (field == "QSLSDATE")
2745                 {
2746                     dateT = util->getDateFromADIFDateString(data);
2747                     if (dateT.isValid())
2748                     {
2749                         preparedQuery.bindValue( ":qslsdate", util->getDateSQLiteStringFromDate(dateT) );
2750                     }
2751                 }
2752                 else if (field == "QSL_RCVD")
2753                 {
2754                     preparedQuery.bindValue( ":qsl_rcvd", data );
2755                     //preparedQuery.bindValue( ":confirmed", '1' );
2756                 }
2757                 else if (field == "QSL_SENT")
2758                 {
2759                     preparedQuery.bindValue( ":qsl_sent", data );
2760                 }
2761                 else if (field == "QSL_RCVD_VIA")
2762                 {
2763                     preparedQuery.bindValue( ":qsl_rcvd_via", data );
2764                 }
2765                 else if (field == "QSL_SENT_VIA")
2766                 {
2767                     preparedQuery.bindValue( ":qsl_sent_via", data );
2768                 }
2769                 else if (field == "QSL_VIA")
2770                 {
2771                        //qDebug() << "FileManager::bprocessQsoReadingADIF-QSL_VIA: " << data << QT_ENDL;
2772                     if (data == "BUREAU") // This comprobation is to "correct" old logs, mainly from KLog
2773                         // comming from older ADIF files
2774                     {
2775                         preparedQuery.bindValue( ":qsl_sent_via", "B" );
2776                     }
2777                     else if ( (data == "B") || (data == "D") || (data == "E") )// M Deprecated value from ADIF 304
2778                     {
2779                        preparedQuery.bindValue( ":qsl_via", data );
2780                     }
2781                     else
2782                     {
2783                         // If values are not valid, are not imported.
2784                         //TODO: Send an error to the user to show that there was an invalid field
2785                     }
2786                 }
2787                 else if (field == "QSO_COMPLETE")
2788                 {
2789                     preparedQuery.bindValue( ":qso_complete", data );
2790                 }
2791                 else if (field == "QSO_RANDOM")
2792                 {
2793                     preparedQuery.bindValue( ":qso_random", data );
2794                 }
2795                 else if (field == "QTH")
2796                 {
2797                     preparedQuery.bindValue( ":qth", data );
2798                 }
2799                 else if (field == "REGION")
2800                 {
2801                     preparedQuery.bindValue( ":region", data );
2802                 }
2803                 else if (field == "RIG")
2804                 {
2805                     preparedQuery.bindValue( ":rig", data );
2806                 }
2807                 else if (field == "RX_PWR")
2808                 {
2809                        //qDebug() << "FileManager::bprocessQsoReadingADIF-rx_pwr: " << data << QT_ENDL;
2810                     preparedQuery.bindValue( ":rx_pwr", data);
2811                 }
2812                 else if (field == "TX_PWR")
2813                 {
2814                        //qDebug() << "FileManager::bprocessQsoReadingADIF-tx_pwr: " << data << QT_ENDL;
2815                     preparedQuery.bindValue( ":tx_pwr", data);
2816                 }
2817                 else if (field == "SAT_MODE")
2818                 {
2819                     preparedQuery.bindValue( ":sat_mode", data );
2820                 }
2821                 else if (field == "SAT_NAME")
2822                 {
2823                     preparedQuery.bindValue( ":sat_name", data );
2824                 }
2825                 else if (field == "SFI")
2826                 {
2827                     preparedQuery.bindValue( ":sfi", data );
2828                 }
2829                 else if (field == "SIG")
2830                 {
2831                     preparedQuery.bindValue( ":sig", data );
2832                 }
2833                 else if (field == "SIG_INFO")
2834                 {
2835                     preparedQuery.bindValue( ":sig_info", data );
2836                 }
2837                 else if (field == "SILENT_KEY")
2838                 {
2839                     preparedQuery.bindValue( ":silent_key", data );
2840                 }
2841                 else if (field == "SKCC")
2842                 {
2843                     preparedQuery.bindValue( ":skcc", data );
2844                 }
2845                 else if (field == "SOTA_REF")
2846                 {
2847                     preparedQuery.bindValue( ":sota_ref", data );
2848                 }
2849                 else if (field == "SWL")
2850                 {
2851                     preparedQuery.bindValue( ":swl", data );
2852                 }
2853                 else if (field == "TEN_TEN")
2854                 {
2855                     preparedQuery.bindValue( ":ten_ten", data );
2856                 }
2857                 else if (field == "UKSMG")
2858                 {
2859                     preparedQuery.bindValue( ":uksmg", data );
2860                 }
2861                 else if (field == "USACA_COUNTIES")
2862                 {
2863                     preparedQuery.bindValue( ":usaca_counties", data );
2864                 }
2865                 else if (field == "VE_PROV")
2866                 {
2867                     preparedQuery.bindValue( ":ve_prov", data );
2868                 }
2869                 else if (field == "VUCC_GRIDS")
2870                 {
2871                     preparedQuery.bindValue( ":vucc_grids", data );
2872                 }
2873                 else if (field == "WEB")
2874                 {
2875                     preparedQuery.bindValue( ":web", data );
2876                 }
2877                 else if (field == "APP_KLOG_POINTS") //Importing own ADIF fields
2878                 {
2879                     preparedQuery.bindValue( ":points", data );
2880                 }
2881                 else if (field == "APP_KLOG_MULTIPLIER") //Importing own ADIF fields
2882                 {
2883                     preparedQuery.bindValue( ":multiplier", data );
2884                 }
2885                 else if (field == "APP_KLOG_TRX") //Importing own ADIF fields
2886                 {
2887                     preparedQuery.bindValue( ":transmiterid", data );
2888                 }
2889                 else if (field == "APP_KLOG_LOGN") //Lognumber in a multiple-log file
2890                 {
2891                     //TODO: Think about how to import a file with different logs
2892                     //isThisQSODuplicated(const QString &_qrz, const QString &_date, const QString &_time, const int _band, const int _mode)
2893                 }
2894                 else if (field == "APP_N1MM_POINTS") //Importing from N1MM
2895                 {
2896                     preparedQuery.bindValue( ":points", data );
2897                 }
2898                 else
2899                 {
2900                 }
2901             }
2902             else
2903             {
2904                    //qDebug() << "FileManager::processQsoReadingADIF (field) CheckAdif FALSE: " << field << QT_ENDL;
2905             }
2906         }
2907     }
2908     if ( haveCall && haveDate && haveTime && haveBand && haveMode)
2909     {
2910         QList<int> _dupeQSOs;
2911         _dupeQSOs.clear();
2912         _dupeQSOs << dataProxy->isThisQSODuplicated(Q_FUNC_INFO, qrzCall, dateTime, bandi, modei, duplicatedQSOSlotInSecs);
2913         if (_dupeQSOs.length()>0)
2914         {
2915             isDupeQSO = true;
2916         }
2917         //QList<int> DataProxy_SQLite::isThisQSODuplicated(const QString &_qrz, const QDateTime &_dateTime, const int _band, const int _mode, const int _secs)
2918     }
2919 
2920     if ( (haveDate) && (haveTime))
2921     {
2922         preparedQuery.bindValue( ":qso_date", util->getDateTimeSQLiteStringFromDateTime(dateTime));
2923         //preparedQuery.bindValue( ":time_on", dateTime.toString("hhmmss"));
2924     }
2925 
2926     if ( (haveDateOff) && (haveTimeOff))
2927     {
2928         preparedQuery.bindValue( ":qso_date_off", util->getDateTimeSQLiteStringFromDateTime(dateTimeOFF));
2929     }
2930     if ((haveBand) && (!haveFreqTX))
2931     {
2932         preparedQuery.bindValue( ":freq",  dataProxy->getFreqFromBandId(bandi));
2933     }
2934     if ((bandRXDef) && (!haveFreqRX))
2935     {
2936         preparedQuery.bindValue( ":freq_rx",  dataProxy->getFreqFromBandId(bandrxi));
2937     }
2938     if ((haveFreqTX) && (!haveFreqRX))
2939     {
2940         preparedQuery.bindValue( ":freq_rx",  freqTX);
2941     }
2942     if (!haveCall)
2943     {
2944         QString text = util->getAValidCall(qrzCall);
2945         if (!(util->isValidCall(text)))
2946         {
2947             haveCall = true;
2948             qrzCall = text;
2949         }
2950         else
2951         {
2952             qrzCall = QString();
2953             haveCall = false;
2954         }
2955     }
2956     if (!(haveBand  && haveMode && haveTime && haveDate ))
2957     {
2958           //qDebug() << "FileManager::processQsoReadingADIF (Don't have all mandatory fields): " << QT_ENDL;
2959         aux2 = tr ("This QSO is not including the minimum data to consider a QSO as valid!") + "\n\n\n" + tr("Please edit the ADIF file and make sure that it include at least:") + "\n\nCALL, QSO_DATE, TIME_ON, BAND "+ tr("and") +" MODE.\n\n" + tr("This QSO had:") + "\n";
2960         if (!haveBand)
2961         {
2962             aux2 = aux2 + tr(" - The band missing and the following call: ") + qrzCall + ".\n";
2963         }
2964         if (!haveMode)
2965         {
2966             aux2 = aux2 + tr(" - The mode missing and the following call: ") + qrzCall + ".\n";
2967         }
2968         if (!haveDate)
2969         {
2970             aux2 = aux2 + tr(" - The date missing and the following call: ") + qrzCall + ".\n";
2971         }
2972         if (!haveTime)
2973         {
2974             aux2 = aux2 + tr(" - The time missing and the following call: ") + qrzCall + ".\n";
2975         }
2976         aux2 = aux2 + "\n\n" + tr("Do you want to continue with the current file?");
2977         //qDebug() << "FileManager::processQsoReadingADIF - Missing fields: " << aux2 << QT_ENDL;
2978 
2979         QMessageBox msgBox;
2980         msgBox.setWindowTitle(tr("KLog: Not all required data found!"));
2981         msgBox.setText(aux2);
2982         msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::Cancel);
2983         msgBox.setDefaultButton(QMessageBox::Cancel);
2984         msgBox.setIcon(QMessageBox::Warning);
2985         int ret = msgBox.exec();
2986         switch (ret)
2987         {
2988             case QMessageBox::Yes:
2989               // Yes was clicked
2990             break;
2991             case QMessageBox::Cancel:
2992                 noMoreQso = true;
2993             break;
2994             default:
2995               // should never be reached
2996             break;
2997         }
2998     }
2999 
3000     if ((!rstTXr) && (!rstTXDefault))
3001     {
3002         //submode = dataProxy->getSubModeFromId(i);
3003         defaultRSTTX = util->getDefaultRST(submode);
3004         QMessageBox msgBox;
3005         aux = tr("Some QSOs of this log, (i.e.: %1) seems to lack RST-TX information.").arg(qrzCall)  + "\n\n" + tr("Click on Yes to add a default %1 for mode %2 to all QSOs with a similar problem.").arg(defaultRSTTX).arg(submode) + "\n\n" + tr("If you select NO, maybe the QSO will not be imported.");
3006         msgBox.setWindowTitle(tr("KLog: No RST TX found!"));
3007         msgBox.setText(aux);
3008         msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
3009         msgBox.setDefaultButton(QMessageBox::Yes);
3010         msgBox.setIcon(QMessageBox::Warning);
3011         int ret = msgBox.exec();
3012         switch (ret) {
3013           case QMessageBox::Yes:
3014               // Yes was clicked
3015                 rstTXDefault = true;
3016               break;
3017           default:
3018               // should never be reached
3019                 rstTXDefault = false;
3020               break;
3021         }
3022     }
3023     if ((!rstRXr) && (!rstRXDefault))
3024     {
3025         defaultRSTRX = util->getDefaultRST(submode);
3026         QMessageBox msgBox;
3027         //aux = tr("Some QSOs of this log, (i.e.: %1) seems to lack RST-RX information.").arg(qrzCall) + "\n\n" + tr("Click on Yes to add a default 59 to all QSO with a similar problem.") + "\n\n" + tr("If you select NO, the QSO may not be imported.");
3028         aux = tr("Some QSOs of this log, (i.e.: %1) seems to lack RST-RX information.").arg(qrzCall)  + "\n\n" + tr("Click on Yes to add a default %1 for mode %2 to all QSOs with a similar problem.").arg(defaultRSTRX).arg(submode) + "\n\n" + tr("If you select NO, maybe the QSO will not be imported.");
3029         msgBox.setText(aux);
3030         msgBox.setWindowTitle(tr("KLog: No RST RX found!"));
3031         msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
3032         msgBox.setDefaultButton(QMessageBox::Yes);
3033         msgBox.setIcon(QMessageBox::Warning);
3034         int ret = msgBox.exec();
3035         switch (ret) {
3036           case QMessageBox::Yes:
3037               // Yes was clicked
3038                 rstRXDefault = true;
3039               break;
3040           default:
3041               // should never be reached
3042                 rstRXDefault = false;
3043               break;
3044         }
3045     }
3046 
3047     if ((!rstTXr) && (rstTXDefault))
3048     {
3049         preparedQuery.bindValue( ":rst_sent", defaultRSTTX );
3050     }
3051 
3052     if ((!rstRXr) && (rstRXDefault))
3053     {
3054         preparedQuery.bindValue( ":rst_rcvd", defaultRSTRX );
3055     }
3056 
3057     if ((!hasStationCall)  )
3058     {
3059         if (!usePreviousStationCallsignAnswerAlways)
3060         {
3061             QMessageBox msgBox;
3062             if(getStationCallsignFromUser(qrzCall, dateTime.date()))
3063             {
3064                 hasStationCall = true;
3065             }
3066             else
3067             {
3068                 msgBox.setWindowTitle(tr("KLog - No Station callsign entered."));
3069                 msgBox.setIcon(QMessageBox::Warning);
3070                 msgBox.setText("KLog - No Station callsign entered.");
3071                 msgBox.setInformativeText("Not entering a valid Station Callsign may create problems in some ocasions, like when uploading data to LoTW.\n\n Do you want to try again?");
3072                 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
3073                 msgBox.setDefaultButton(QMessageBox::Yes);
3074                 int ret = msgBox.exec();
3075                 if (ret == QMessageBox::Yes)
3076                 {
3077                     if (!getStationCallsignFromUser(qrzCall, dateTime.date()))
3078                     {
3079                         msgBox.setWindowTitle(tr("KLog - No Station callsign entered."));
3080                         msgBox.setIcon(QMessageBox::Warning);
3081                         msgBox.setText("KLog - No Station callsign entered.");
3082                         msgBox.setInformativeText("No valid station callsign to add to the QSO.");
3083                         msgBox.setStandardButtons(QMessageBox::Ok);
3084                         msgBox.setDefaultButton(QMessageBox::Ok);
3085                         msgBox.exec();
3086                     }
3087                     else
3088                     {
3089                         hasStationCall = true;
3090                     }
3091                 }
3092             }
3093 
3094             msgBox.setWindowTitle(tr("KLog - Apply to all QSOs in this log?"));
3095             msgBox.setIcon(QMessageBox::Question);
3096             msgBox.setText("KLog - Do you want to use the same answer for all the QSOs in this log?");
3097             msgBox.setInformativeText("KLog can use the same answer that you provided to all the QSOs in this logfile when importing it.");
3098             msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
3099             msgBox.setDefaultButton(QMessageBox::Yes);
3100             int ret = msgBox.exec();
3101             if (ret == QMessageBox::Yes)
3102             {
3103                 usePreviousStationCallsignAnswerAlways = true;
3104             }
3105             else
3106             {
3107                 usePreviousStationCallsignAnswerAlways = false;
3108             }
3109         }
3110            //qDebug() << "FileManager::processQsoReadingADIF defaultStationCallsign: " << defaultStationCallsign << QT_ENDL;
3111 
3112         if ((hasStationCall) || (util->isValidCall(defaultStationCallsign)))
3113         {
3114             preparedQuery.bindValue( ":station_callsign", defaultStationCallsign );
3115         }
3116         if (!hasLotwQslSent)
3117         {
3118             preparedQuery.bindValue( ":lotw_qsl_sent","Q");
3119         }
3120 
3121         if (!hasEqslQslSent)
3122         {
3123             preparedQuery.bindValue( ":eqsl_qsl_sent","Q");
3124         }
3125 
3126         if (!hasClublogQslSent)
3127         {
3128             preparedQuery.bindValue( ":clublog_qso_upload_status","M");
3129         }
3130 
3131         if (!hasQrzQslSent)
3132         {
3133             preparedQuery.bindValue( ":qrzcom_qso_upload_status","M");
3134         }
3135     }
3136 
3137     preparedQuery.bindValue( ":lognumber", QString::number(logNumber));
3138     return isDupeQSO;
3139 }
3140 
getStationCallsignFromUser(const QString & _qrzDX,const QDate & _dt)3141 bool FileManager::getStationCallsignFromUser(const QString &_qrzDX, const QDate &_dt)
3142 {
3143     //qDebug() << "FileManager::getStationCallsignFromUser: " << _qrzDX << "/" << util->getDateSQLiteStringFromDate(_dt) << "-" << QT_ENDL;
3144     bool ok;
3145     QString text;
3146     QString aux;
3147     QString _date = QString();
3148     if (_dt.isValid())
3149     {
3150         _date = ", on "+  util->getDateSQLiteStringFromDate(_dt) ;
3151     }
3152 
3153     if (util->isValidCall(_qrzDX))
3154     {
3155         aux = tr("KLog has found one QSO without the Station Callsign defined.\n\nEnter the Station Callsign that was used to do this QSO with %1 on %2:").arg(_qrzDX).arg(_date);
3156         text = QInputDialog::getText(this, tr("KLog - QSO without Station Callsign"),
3157                                             aux, QLineEdit::Normal, "", &ok);
3158     }
3159     else
3160     {
3161         text = QInputDialog::getText(this, tr("KLog - QSO without Station Callsign"),
3162                                                    tr("KLog has found one QSO without the Station Callsign defined.\n\nEnter the Station Callsign that was used to do this QSO on %1:").arg(_date), QLineEdit::Normal,
3163                                                    "", &ok);
3164     }
3165 
3166     if (ok && util->isValidCall(text))
3167     {
3168         defaultStationCallsign = text.toUpper();
3169         return true;
3170     }
3171     else
3172     {
3173         return false;
3174     }
3175 }
3176 
queryPreparation(const int _logN)3177 void FileManager::queryPreparation(const int _logN)
3178 {
3179        //qDebug() << "FileManager::queryPreparation log: " << QString::number(_logN) << QT_ENDL;
3180     //Prepares the query, ALL fields to default except lognumber, as may change...
3181 
3182     QStringList columns;
3183     columns.clear();
3184 
3185     columns << dataProxy->getColumnNamesFromTableLog();
3186     if (columns.size()<2)
3187     {
3188            //qDebug() << "FileManager::queryPreparation: <2 "  << QT_ENDL;
3189         return;
3190     }
3191 
3192     for (int i=0;i<columns.size();i++)
3193     {
3194         if (columns.at(i) == "lognumber")
3195         {
3196             preparedQuery.bindValue( ":lognumber", QString::number(_logN));
3197         }
3198         else if (columns.at(i) == "rx_pwr")
3199         {
3200             preparedQuery.bindValue( ":rx_pwr", QString::number(0.0));
3201         }
3202         else if (columns.at(i) == "tx_pwr")
3203         {
3204             preparedQuery.bindValue( ":tx_pwr", QString::number(0.0));
3205         }
3206 
3207         else
3208         {
3209             preparedQuery.bindValue(":" + columns.at(i), "");
3210         }
3211     }
3212 }
3213 
adifReqQSLExport(const QString & _fileName)3214 bool FileManager::adifReqQSLExport(const QString& _fileName)
3215 {
3216     //qDebug() << "FileManager::adifReqQSLExport" << _fileName << QT_ENDL;
3217     return adifLogExportToFile(_fileName, 0, false, true, false);
3218 }
3219 
modifySetupFile(const QString & _filename,const QString & _field,const QString & _value)3220 bool FileManager::modifySetupFile(const QString& _filename, const QString &_field, const QString &_value)
3221 {
3222       //qDebug() << "FileManager::modifySetupFile" << QT_ENDL;
3223 
3224 
3225     QFile file(_filename);
3226     if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){ /* Flawfinder: ignore */
3227           //qDebug() << "FileManager::modifySetupFile File not found" << _filename << QT_ENDL;
3228         return false;
3229     }
3230 
3231     QTemporaryFile tmp;
3232     if (!tmp.open()) { /* Flawfinder: ignore */
3233              //qDebug() << "FileManager::modifySetupFile- Temp file not opened" << QT_ENDL;
3234            return false;
3235     }
3236 
3237     QString line = QString();
3238     QTextStream in(&file);
3239     QTextStream out(&tmp);
3240     qint64 pos1 = in.pos();
3241     qint64 pos2 = out.pos();
3242     bool modified = false;
3243 
3244     out << in.readAll();
3245 
3246     in.seek(pos1);
3247     out.seek(pos2);
3248 
3249     while (!out.atEnd())
3250     {
3251         line = out.readLine();
3252            //qDebug() << "FileManager::modifySetupFile- Temp file: " << line << QT_ENDL;
3253         if (line.startsWith(_field))
3254         {
3255             in << _field << "=" << _value << ";" << QT_ENDL;
3256             modified = true;
3257         }
3258         else
3259         {
3260             in << line << QT_ENDL;
3261         }
3262     }
3263     if (!modified)
3264     {// If the data is not found, we will add it to the end.
3265         in << _field << "=" << _value << ";" << QT_ENDL;
3266     }
3267     return true;
3268 }
3269 
howManyLogsInFile(QFile & _f)3270 int FileManager::howManyLogsInFile(QFile& _f)
3271 {
3272       //qDebug() << "FileManager::howManyLogsInFile:" << QT_ENDL;
3273     QFile &file = _f;
3274     //qint64 pos; //Position in the file
3275     QString line = QString();
3276     QStringList fields = QStringList();
3277     QStringList logs = QStringList();
3278     QString aux = QString();
3279     QStringList data = QStringList();
3280     bool atLeastOneLog = false;
3281 
3282     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
3283     {
3284            //qDebug() << "FileManager::howManyLogsInFile File not found" << QT_ENDL;
3285         return false;
3286     }
3287 
3288     //pos = file.pos();
3289 
3290     while ( !file.atEnd()   )
3291     {
3292         //line = file.readLine();
3293 
3294         line.clear();
3295         line.append(file.readLine().trimmed().toUpper());
3296 
3297         if (line.contains("APP_KLOG_LOGN"))
3298         {   fields.clear();
3299             fields << line.split("<", QT_SKIP);
3300 
3301             foreach (aux, fields)
3302             {
3303                 aux = aux.trimmed();
3304                 if (aux.contains("APP_KLOG_LOGN"))
3305                 {
3306                     data.clear();
3307                     data << aux.split('>');
3308                       //qDebug() << "FileManager::howManyLogsInFile: data.0: " << data.at(0) << QT_ENDL;
3309                       //qDebug() << "FileManager::howManyLogsInFile: data.1: " << data.at(1) << QT_ENDL;
3310                     if (!logs.contains(data.at(1)))
3311                     {
3312                         logs.append(data.at(1));
3313                     }
3314                 }
3315             }
3316         }
3317         else if (line.contains("EOR"))
3318         {
3319             atLeastOneLog = true;
3320         }
3321         else
3322         {
3323         }
3324     }
3325        //qDebug() << "FileManager::howManyLogsInFile: JUST ONE!" << aux << QT_ENDL;
3326     file.close();
3327 
3328     if ( (logs.size()<2) && atLeastOneLog )
3329     {
3330         return 1;
3331     }
3332     return logs.size();
3333 }
3334 
fillHashLog(QFile & _f)3335 bool FileManager::fillHashLog(QFile &_f)
3336 {
3337        //qDebug() << "FileManager::fillHashLog:" << QT_ENDL;
3338    //hashLogs
3339     //1.- Run the log and fill hashLogs
3340     //2.- Run the file and continue the log series to be able to translate the files log into our own log
3341     hashLogs.clear();
3342     QStringList qs;
3343     qs.clear();
3344     qs << dataProxy->getListOfManagedLogs();
3345     int i = 0;
3346     for (i=0;i<qs.size();i++)
3347     {
3348            //qDebug() << "FileManager::fillHashLog: - log: " << QString::number(qs.at(i).toInt()) << QT_ENDL;
3349         hashLogs.insert(i+1, (qs.at(i)).toInt() );
3350     }
3351 
3352     for (i = 0; i < hashLogs.size(); ++i)
3353     {
3354            //qDebug() << "FileManager::fillHashLog: - hashLogs: " << QString::number(hashLogs[i]) << QT_ENDL;
3355     }
3356 
3357     qs.clear();
3358     qs << getListOfLogsInFile(_f);
3359 
3360     int j = i;
3361 
3362     for (int i=0; i<qs.size();i++)
3363     {
3364         hashLogs.insert(j+i, (qs.at(i)).toInt());
3365     }
3366 
3367     for (i=0; i<hashLogs.size();i++)
3368     {
3369            //qDebug() << "FileManager::fillHashLog: (" << QString::number(hashLogs.value(i)) << "/" << QString::number(i) << ")" << QT_ENDL;
3370     }
3371     return true;
3372 }
3373 
getListOfLogsInFile(QFile & _f)3374 QStringList FileManager::getListOfLogsInFile(QFile& _f)
3375 {
3376     // Returns the logs of the file + 10.000 (So, log 1 is returned as 10001, 2 as 10.002 and so on)
3377        //qDebug() << "FileManager::getListOfLogsInFile:" << QT_ENDL;
3378     QFile &file = _f;
3379     //qint64 pos; //Position in the file
3380     QString line = QString();
3381     QStringList fields = QStringList();
3382     QStringList logs = QStringList();
3383     QString aux = QString();
3384     QStringList data = QStringList();
3385     //bool atLeastOneLog = false;
3386 
3387     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) /* Flawfinder: ignore */
3388     {
3389            //qDebug() << "FileManager::howManyLogsInFile File not found" << QT_ENDL;
3390         return QStringList();
3391     }
3392 
3393     //pos = file.pos();
3394 
3395     while ( !file.atEnd()   )
3396     {
3397         //line = file.readLine();
3398         line.clear();
3399         line.append(file.readLine().trimmed().toUpper());
3400 
3401         if (line.contains("APP_KLOG_LOGN"))
3402         {   fields.clear();
3403             fields << line.split("<", QT_SKIP);
3404 
3405             foreach (aux, fields)
3406             {
3407                 aux = aux.trimmed();
3408                 if (aux.contains("APP_KLOG_LOGN"))
3409                 {
3410                     data.clear();
3411                     data << aux.split('>');
3412                        //qDebug() << "FileManager::howManyLogsInFile: data.0: " << data.at(0) << QT_ENDL;
3413                        //qDebug() << "FileManager::howManyLogsInFile: data.1: " << data.at(1) << QT_ENDL;
3414                     if (!logs.contains(QString::number(data.at(1).toInt()+10000)))
3415                     {
3416                         logs.append(QString::number(data.at(1).toInt()+10000));
3417                     }
3418                 }
3419             }
3420         }
3421         else if (line.contains("EOR"))
3422         {
3423             //atLeastOneLog = true;
3424         }
3425     }
3426 
3427     file.close();
3428     return logs;
3429 }
3430 
setVersion(const QString & _version)3431 void FileManager::setVersion(const QString &_version)
3432 {
3433     util->setVersion(_version);
3434 }
3435 
readAdifField(const QString & _field)3436 QStringList FileManager::readAdifField(const QString &_field)
3437 {
3438     // This function receives a QString with an ADIF field and returns a QStringList with the following format:
3439     // ADIF-tag, value
3440     // If the QString is not an ADIF tag, it returns a clear QStringList.
3441     // We are expecting the ADIF format:
3442     // <F:L:T>D
3443     // <Field:Length:Type>Data
3444 
3445     //qDebug() << "FileManager::readAdifField: " << _field << QT_ENDL;
3446     QStringList result;
3447     result.clear();
3448 
3449     if (_field == "<EOR>")
3450     {
3451         //qDebug() << "FileManager::readAdifField: EOR found!!" << QT_ENDL;
3452         result << "EOR" << "EOR";
3453         return result;
3454     }
3455 
3456     if (!((_field.startsWith("<")) && (_field.contains(":")) && (_field.contains(">"))))
3457     {
3458         //qDebug() << "FileManager::readAdifField: NOT (contains : and >): " << _field << QT_ENDL;
3459         return QStringList();
3460     }
3461 
3462     // Now we have the data in the result[1]
3463     result = _field.split(">", QT_SKIP); // Remove the first '<' and split in field & data
3464     QString data = result.at(1);
3465     QStringList fieldList;
3466     fieldList.clear();
3467     fieldList << (result.at(0)).split(':'); // we may have 1 or 2 depending on the format of the ADIF field.
3468     int iAux = fieldList.length();
3469       //qDebug() << "FileManager::readAdifField: iAux: " << QString::number(iAux) << QT_ENDL;
3470     int dataLength;
3471     QString field;
3472     QString fieldType;
3473       //qDebug() << "FileManager::readAdifField: analyzing..."  << QT_ENDL;
3474 
3475     if (iAux == 2)
3476     {
3477         field = fieldList.at(0);
3478         dataLength = (fieldList.at(1)).toInt();
3479         fieldType = QString();
3480     }
3481     else if (iAux == 3)
3482     {
3483         field = fieldList.at(0);
3484         dataLength = (fieldList.at(1)).toInt();
3485         fieldType = fieldList.at(2);
3486     }
3487     else
3488     { // Not valid ADIF
3489          //qDebug() << "FileManager::readAdifField: iAux != 1, 2" << QT_ENDL;
3490         return QStringList();
3491     }
3492     field.remove('<');
3493     data = data.left(dataLength);
3494       //qDebug() << "FileManager::readAdifField: field: " << field << QT_ENDL;
3495       //qDebug() << "FileManager::readAdifField: dataLength: " << QString::number(dataLength) << QT_ENDL;
3496       //qDebug() << "FileManager::readAdifField: data: " << data << QT_ENDL;
3497       //qDebug() << "FileManager::readAdifField: fieldType: " << fieldType << QT_ENDL;
3498 
3499     if (data.length() != dataLength)
3500     {
3501         //qDebug() << "FileManager::readAdifField: data.length != dataLength: " << QString::number(data.length()) << "/" << QString::number(dataLength) << QT_ENDL;
3502         return QStringList();
3503     }
3504     result.clear();
3505     result << field << data;
3506     //qDebug() << "FileManager::readAdifField: OK: " << field << "/" << data << QT_ENDL;
3507     return result;
3508 }
3509 
prepareStringLog()3510 QString FileManager::prepareStringLog()
3511 {
3512     //qDebug() << "FileManager::prepareStringLog: " << QT_ENDL;
3513     QStringList columns;
3514     columns.clear();
3515 
3516     columns << dataProxy->getColumnNamesFromTableLog();
3517     if (columns.size()<2)
3518     {
3519            //qDebug() << "FileManager::prepareStringLog: <2 "  << QT_ENDL;
3520         return QString();
3521     }
3522 
3523     QString queryFields, queryValues;
3524     queryFields.clear();
3525     queryValues.clear();
3526 
3527     for (int i=0;i<columns.size();i++)
3528     {
3529         queryFields = queryFields + columns.at(i) + ", ";
3530         queryValues = queryValues + ":" + columns.at(i) + ", ";
3531     }
3532     queryFields = queryFields.left(queryFields.size() -2);
3533     queryValues = queryValues.left(queryValues.size() -2);
3534 
3535     QString string = "INSERT INTO log ("  + queryFields + ") VALUES (" + queryValues + ")";
3536        //qDebug() << "FileManager::prepareStringLog: " << string << QT_ENDL;
3537     return string;
3538 }
3539 
getDateTimeOfLastBackup()3540 QDateTime FileManager::getDateTimeOfLastBackup()
3541 {
3542       //qDebug() << "FileManager::getDateTimeOfLastBackup: " << (QDateTime::currentDateTime()).toString("yyyyMMdd-hhmmss")<< QT_ENDL;
3543       //qDebug() << "FileManager::getDateTimeOfLastBackup: " << util->getCfgFile() << QT_ENDL;
3544     QFile file (util->getCfgFile());
3545     QString line;
3546     QStringList fields;
3547     fields.clear();
3548     QDateTime _dataTime = QDateTime();
3549 
3550     if (file.open (QIODevice::ReadOnly)) /* Flawfinder: ignore */
3551     {
3552         while ( !file.atEnd()   )
3553         {
3554             line.clear();
3555             line.append(file.readLine().trimmed().toUpper());
3556             if (line.contains("LATESTBACKUP"))
3557             {
3558                 fields << line.split("=", QT_SKIP);
3559                 line = fields.at(1);
3560                 line.truncate(15);
3561                 return _dataTime.fromString(line, "yyyyMMdd-hhmmss");
3562             }
3563         }
3564     }
3565     return QDateTime();
3566 }
3567 
writeBackupDate()3568 bool FileManager::writeBackupDate()
3569 {
3570       //qDebug() << "FileManager::writeBackupDate: current: " << (QDateTime::currentDateTime()).toString("yyyyMMdd-hhmmss") << QT_ENDL;
3571       //qDebug() << "FileManager::writeBackupDate: current: " << (getDateTimeOfLastBackup()).toString("yyyyMMdd-hhmmss") << QT_ENDL;
3572 
3573     QFile file (util->getCfgFile());
3574     QString line, lineTemp;
3575     //QStringList fields;
3576     //fields.clear();
3577     QDateTime _dataTime = QDateTime();
3578 
3579     QStringList completeFile;
3580     completeFile.clear();
3581 
3582     if(file.open(QIODevice::ReadWrite | QIODevice::Text)) /* Flawfinder: ignore */
3583     {
3584         QString s;
3585         QTextStream t(&file);
3586         while(!t.atEnd())
3587         {
3588             QString line = t.readLine();
3589             if ( !(line.toUpper()).contains("LATESTBACKUP")  )
3590             {
3591                 s.append(line + "\n");
3592             }
3593         }
3594         s.append("LatestBackup=" + (QDateTime::currentDateTime()).toString("yyyyMMdd-hhmmss") + ";\n" );
3595         file.resize(0);
3596         t << s;
3597         file.close();
3598     }
3599     return true;
3600 }
3601 
3602 
3603 
setStationCallSign(const QString & _st)3604 void FileManager::setStationCallSign(const QString& _st)
3605 {
3606       //qDebug() << "FileManager::setStationCallSign: " << _st << QT_ENDL;
3607     if (util->isValidCall(_st))
3608     {
3609           //qDebug() << "FileManager::setStationCallSign: True" << QT_ENDL;
3610         defaultStationCallsign = _st;
3611           //qDebug() << "FileManager::setStationCallSign: " << defaultStationCallsign << QT_ENDL;
3612     }
3613     else
3614     {
3615           //qDebug() << "FileManager::setStationCallSign: FALSE" << QT_ENDL;
3616     }
3617 
3618    //qDebug() << "FileManager::setStationCallSign: -" << defaultStationCallsign << "-END" << QT_ENDL;
3619 }
3620 
3621 
askUserToUseAlwaysSameAnswer()3622 bool FileManager::askUserToUseAlwaysSameAnswer()
3623 {
3624     //qDebug() << "FileManager::askUserToUseAlwaysSameAnswer: "  << QT_ENDL;
3625 
3626     QMessageBox msgBox;
3627     msgBox.setIcon(QMessageBox::Warning);
3628     msgBox.setWindowTitle(tr("KLog - Don't ask again"));
3629 
3630     msgBox.setText(tr("Do you want to reuse your answer?"));
3631     msgBox.setInformativeText(tr("KLog will use automatically your previous answer for any other similar ocurrence, if any, without asking you again."));
3632     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
3633     msgBox.setDefaultButton(QMessageBox::No);
3634     int ret = msgBox.exec();
3635     switch (ret) {
3636     case QMessageBox::Yes:
3637           // Yes was clicked
3638            return true;
3639     case QMessageBox::No:
3640             // No Save was clicked
3641         return false;
3642     default:
3643             // should never be reached
3644         return false;
3645     }
3646 
3647     //qDebug() << "FileManager::askUserToUseAlwaysSameAnswer: - END" << QT_ENDL;
3648 }
3649 
askUserToAddThisQSOToLog(const QString & _call,const QDateTime _datetime,const QString & _mode,const QString & _band,const double _freq,const QDate _qslrdate)3650 bool FileManager::askUserToAddThisQSOToLog(const QString &_call, const QDateTime _datetime, const QString &_mode, const QString &_band, const double _freq, const QDate _qslrdate)
3651 {
3652     //qDebug() << "FileManager::askUserToAddThisQSOToLog: " << _call << QT_ENDL;
3653     QString qsoData = QString(tr("<ul><li>Date/Time:</i> %1</li><li>Callsign: %2</li><li>Band: %3</li><li>Mode: %4</li></ul>")).arg(util->getDateTimeSQLiteStringFromDateTime(_datetime)).arg(_call).arg(_band).arg(_mode);
3654     QMessageBox msgBox;
3655     msgBox.setTextFormat(Qt::RichText);
3656     msgBox.setIcon(QMessageBox::Warning);
3657     msgBox.setWindowTitle(tr("KLog - QSO not found"));
3658 
3659     msgBox.setText(tr("Do you want to add this QSO to the log?:\n\n") + qsoData);
3660     msgBox.setInformativeText(tr("We have found a QSO coming from LoTW that is not in your local log.\n\nDo you want KLog to add this QSO to the log?"));
3661     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
3662     msgBox.setDefaultButton(QMessageBox::No);
3663     int ret = msgBox.exec();
3664     switch (ret) {
3665     case QMessageBox::Yes:
3666           // Yes was clicked
3667            return true;
3668     case QMessageBox::No:
3669             // No Save was clicked
3670         return false;
3671     default:
3672             // should never be reached
3673         return false;
3674     }
3675 
3676     //qDebug() << "FileManager::askUserToAddThisQSOToLog: - END" << QT_ENDL;
3677 }
3678 
showInvalidCallMessage(const QString & _call)3679 bool FileManager::showInvalidCallMessage(const QString &_call){
3680     QMessageBox msgBox;
3681     msgBox.setIcon(QMessageBox::Warning);
3682     msgBox.setWindowTitle(tr("KLog - Invalid call detected"));
3683 
3684     QString aux = _call;
3685 
3686     if (aux.length()<1)
3687     {
3688         aux = QString(tr("An empty callsign has been detected. Do you want to export this QSO anyway (click on Yes) or remove the field from the exported log file?"));
3689     }
3690     else
3691     {
3692         aux = QString(tr("An invalid callsign has been detected %1. Do you want to export this callsign anyway (click on Yes) or remove the call from the exported log file?").arg(aux));
3693     }
3694     msgBox.setText(aux);
3695     msgBox.setInformativeText(tr("Exporting wrong calls may create problems in the applications you are potentially importing this logfile to. It may, however, be a good callsign that is wrongly identified by KLog as not valid. You can, however, edit the ADIF file once the export process is finished."));
3696     msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
3697     msgBox.setDefaultButton(QMessageBox::No);
3698     int ret = msgBox.exec();
3699     switch (ret) {
3700     case QMessageBox::Yes:
3701           // Yes was clicked
3702            return true;
3703     case QMessageBox::No:
3704             // No Save was clicked
3705         return false;
3706     default:
3707             // should never be reached
3708         return false;
3709     }
3710 }
3711 
writeADIFHeader(QTextStream & out,const ExportMode _em,const int _numberOfQsos)3712 void FileManager::writeADIFHeader(QTextStream &out, const ExportMode _em, const int _numberOfQsos)
3713 {
3714     if (_em == ModeEQSL)
3715     {
3716         out << "ADIF v3.1.0 Export from KLog\n<PROGRAMID:4>KLog " << QT_ENDL;
3717     }
3718     else
3719     {
3720         out << "ADIF v3.1.0 Export from KLog\nhttps://www.klog.xyz/klog\n<PROGRAMVERSION:" << QString::number(klogVersion.length()) << ">" << klogVersion << "\n<PROGRAMID:4>KLOG " << QT_ENDL;
3721         out << "<APP_KLOG_QSOS:" << QString::number((QString::number(_numberOfQsos)).length()) << ">" << QString::number(_numberOfQsos) << QT_ENDL;
3722         out << "<APP_KLOG_LOG_DATE_EXPORT:" << QString::number((QDateTime::currentDateTime().toString("yyyyMMdd-hhmm")).length()) << ">" << QDateTime::currentDateTime().toString("yyyyMMdd-hhmm") << QT_ENDL;
3723     }
3724     out << "<EOH>" << QT_ENDL;
3725 }
3726 
writeQuery(QSqlQuery query,QTextStream & out,const ExportMode _em,const bool _justMarked,const bool _onlyRequested,const int _logN)3727 void FileManager::writeQuery(QSqlQuery query, QTextStream &out, const ExportMode _em, const bool _justMarked, const bool _onlyRequested, const int _logN )
3728 {
3729    //qDebug() << "FileManager::writeQuery: " <<  query.lastQuery() << QT_ENDL;
3730     int nameCol;
3731     QString aux;
3732     bool propsat = false;    // Reset the QSO in case it is a Satellite QSO
3733     QSqlRecord rec = query.record();
3734 
3735     if (_justMarked)
3736     {
3737         nameCol = rec.indexOf("marked");
3738         aux = (query.value(nameCol)).toString();
3739         if (aux != "X")
3740         {
3741             return;
3742         }
3743     }
3744 
3745     if (_onlyRequested)
3746     {
3747         nameCol = rec.indexOf("qsl_sent");
3748         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3749         if ( aux !="R" )
3750         {
3751             return;
3752         }
3753     }
3754 
3755     nameCol = rec.indexOf("call");
3756     if (nameCol>=0)
3757     {
3758         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3759        //qDebug() << "FileManager::writeQuery: " << QString::number(nameCol) << "/" << aux << QT_ENDL;
3760         if (util->isValidCall(aux))
3761         {
3762             out << "<CALL:" << QString::number(aux.length()) << ">" << aux << " ";
3763         }
3764         else
3765         {
3766             if (showInvalidCallMessage(aux))
3767             {
3768                  out << "<CALL:" << QString::number(aux.length()) << ">" << aux << " ";
3769             }
3770         }
3771     }
3772     nameCol = rec.indexOf("qso_date");
3773     QDateTime tDateTime;
3774     if (nameCol>=0)
3775     {
3776         aux = (query.value(nameCol)).toString();
3777         tDateTime = util->getDateTimeFromSQLiteString(aux);
3778         if (tDateTime.isValid())
3779         {
3780             aux = util->getADIFDateFromQDateTime(tDateTime);
3781             out << "<QSO_DATE:" << aux.length() << ">" << aux  << " ";
3782             aux = util->getADIFTimeFromQDateTime(tDateTime);
3783             if (_em == ModeEQSL)
3784             {
3785                 aux.chop(2);
3786                 out << "<TIME_ON:" << aux.length() << ">" << aux  << " ";
3787             }
3788             else
3789             {
3790                 out << "<TIME_ON:" << aux.length() << ">" << aux  << " ";
3791             }
3792         }
3793     }
3794 
3795     nameCol = rec.indexOf("bandid");
3796     QString bandst = QString();
3797     if (nameCol>=0)
3798     {
3799         aux = (query.value(nameCol)).toString();
3800         //qDebug() << "FileManager::writeQuery-Band-1: "  << aux << QT_ENDL;
3801         aux = util->checkAndFixASCIIinADIF(aux);
3802         //qDebug() << "FileManager::writeQuery-Band-2: "  << aux << QT_ENDL;
3803         aux = dataProxy->getNameFromBandId(aux.toInt());
3804 
3805         if (dataProxy->getIdFromBandName(aux)>=0)
3806         {
3807          //out << "<BAND:" << QString::number(aux.length()) << ">" << aux  << " ";
3808          bandst = aux;
3809         }
3810     }
3811     nameCol = rec.indexOf("freq");
3812     if (nameCol>=0)
3813     {
3814         aux = (query.value(nameCol)).toString();
3815         //qDebug() << "FileManager::adifLogExportReturnList FREQ1: "  << aux << QT_ENDL;
3816         aux = util->checkAndFixASCIIinADIF(aux);
3817         double freqTX = aux.toDouble();
3818         if (freqTX > 0.0)
3819         {
3820             //TODO: Check if the Band is correctly defined. BAND Wins and freq is lost if not correct
3821             if (dataProxy->getBandIdFromFreq(freqTX) != dataProxy->getIdFromBandName(bandst))
3822             {
3823                 bandst = dataProxy->getBandNameFromFreq(freqTX);
3824             }
3825             if (_em != ModeEQSL)
3826             {
3827                 out << "<FREQ:" << QString::number(aux.length()) << ">" << aux  << " ";
3828             }
3829         }
3830         if (dataProxy->getIdFromBandName(bandst)>0)
3831         {
3832             out << "<BAND:" << QString::number(bandst.length()) << ">" << bandst  << " ";
3833         }
3834     }
3835     // Now the BAND RX
3836     nameCol = rec.indexOf("band_rx");
3837     QString bandrxst = QString();
3838     if (nameCol>=0)
3839     {
3840         aux = (query.value(nameCol)).toString();
3841         aux = util->checkAndFixASCIIinADIF(aux);
3842         aux = dataProxy->getNameFromBandId(aux.toInt());
3843 
3844         if (dataProxy->getIdFromBandName(aux)>=0)
3845         {
3846             bandrxst = aux;
3847         }
3848     }
3849     nameCol = rec.indexOf("freq_rx");
3850     if (nameCol>=0)
3851     {
3852         aux = (query.value(nameCol)).toString();
3853         aux = util->checkAndFixASCIIinADIF(aux);
3854         double freqRX = aux.toDouble();
3855         if (freqRX > 0.0)
3856         {
3857         //TODO: Check if the Band is correctly defined. BAND Wins and freq is lost if not correct
3858             if (dataProxy->getBandIdFromFreq(freqRX) != dataProxy->getIdFromBandName(bandrxst))
3859             {
3860                 bandrxst = dataProxy->getBandNameFromFreq(freqRX);
3861             }
3862             out << "<FREQ_RX:" << QString::number(aux.length()) << ">" << aux  << " ";
3863         }
3864         if (dataProxy->getIdFromBandName(bandrxst)>0)
3865         {
3866             out << "<BAND_RX:" << QString::number(bandrxst.length()) << ">" << bandrxst  << " ";
3867         }
3868         // END of Band RX
3869     }
3870     nameCol = rec.indexOf("modeid");
3871     if (nameCol>=0)
3872     {
3873         aux = (query.value(nameCol)).toString();
3874         aux = util->checkAndFixASCIIinADIF(aux);
3875         // get SubModeId to check if it is the same or not from modeid
3876         QString aux2 = dataProxy->getSubModeFromId(aux.toInt());
3877         //aux = db->getModeNameFromID2(aux.toInt());
3878         aux = dataProxy->getNameFromSubMode(aux2);
3879         if ((aux.length()>1) && (dataProxy->getIdFromModeName(aux)>=0))
3880         {
3881             out << "<MODE:" << QString::number(aux.length()) << ">" << aux  << " ";
3882         }
3883         if ((aux != aux2) && (aux.length()>1) && (dataProxy->getSubModeIdFromSubMode(aux2)>=0) )
3884         {
3885             out << "<SUBMODE:" << QString::number(aux2.length()) << ">" << aux2  << " ";
3886         }
3887     }
3888     nameCol = rec.indexOf("prop_mode");
3889     if (nameCol>=0)
3890     {
3891         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3892         if ((aux.length())>1)
3893         {
3894             out << "<PROP_MODE:" << QString::number(aux.length()) << ">" << aux  << " ";
3895             if (aux == "SAT")
3896             {
3897                 propsat = true;
3898             }
3899         }
3900         //qDebug() << "FileManager::writeQuery: PROP_MODE"  << QT_ENDL;
3901     }
3902     nameCol = rec.indexOf("sat_name");
3903     if (nameCol>=0)
3904     {
3905         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3906         if ((aux.length())>0)
3907         {
3908             out << "<SAT_NAME:" << QString::number(aux.length()) << ">" << aux  << " ";
3909             if (!propsat && (_em == ModeLotW))
3910             {
3911                 out << "<PROP_MODE:3>SAT ";
3912                 propsat = false;
3913             }
3914         }
3915     //qDebug() << "FileManager::writeQuery: SAT_NAME"  << QT_ENDL;
3916     }
3917     nameCol = rec.indexOf("gridsquare");
3918     if (nameCol>=0)
3919     {
3920         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3921         if ((aux.length())>0)
3922         {
3923             out << "<GRIDSQUARE:" << QString::number(aux.length()) << ">" << aux  << " ";
3924         }
3925     }
3926     nameCol = rec.indexOf("my_gridsquare");
3927     if (nameCol>=0)
3928     {
3929         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3930         if ((aux.length())>0)
3931         {
3932             out << "<MY_GRIDSQUARE:" << QString::number(aux.length()) << ">" << aux  << " ";
3933         }
3934     }
3935     nameCol = rec.indexOf("station_callsign");
3936     if ((nameCol>=0) && (_em != ModeEQSL))
3937     {
3938         aux = (query.value(nameCol)).toString();
3939         //qDebug() << "FileManager::writeQuery: StationCallSign: " << aux  << QT_ENDL;
3940         if ((util->isValidCall(aux)))
3941         { // User selected one station callsign from the log
3942             out << "<STATION_CALLSIGN:" << QString::number(aux.length()) << ">" << aux  << " ";
3943         }
3944     }
3945     nameCol = rec.indexOf("qso_date_off");
3946     if (nameCol>=0)
3947     {
3948         aux = (query.value(nameCol)).toString();
3949         aux = util->checkAndFixASCIIinADIF(aux);
3950         tDateTime = util->getDateTimeFromSQLiteString(aux);
3951 
3952         if (tDateTime.isValid())
3953         {
3954             aux = util->getADIFDateFromQDateTime(tDateTime);
3955             out << "<QSO_DATE_OFF:" << QString::number(aux.length()) << ">" << aux  << " ";
3956             aux = util->getADIFTimeFromQDateTime(tDateTime);
3957             out << "<TIME_OFF:" << QString::number(aux.length()) << ">" << aux  << " ";
3958         }
3959     }
3960     nameCol = rec.indexOf("srx");
3961     if (nameCol>=0)
3962     {
3963         aux = (query.value(nameCol)).toString();
3964         aux = util->checkAndFixASCIIinADIF(aux);
3965         if ((aux.length())>0)
3966         {
3967             out << "<SRX:" << QString::number(aux.length()) << ">" << aux  << " ";
3968         }
3969     }
3970     nameCol = rec.indexOf("srx_string");
3971     if (nameCol>=0)
3972     {
3973         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3974         if ((aux.length())>0)
3975         {
3976             out << "<SRX_STRING:" << QString::number(aux.length()) << ">" << aux  << " ";
3977         }
3978     }
3979     nameCol = rec.indexOf("stx");
3980     if (nameCol>=0)
3981     {
3982         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3983         if ((aux.length())>0)
3984         {
3985             out << "<STX:" << QString::number(aux.length()) << ">" << aux  << " ";
3986         }
3987     }
3988     nameCol = rec.indexOf("stx_string");
3989     if (nameCol>=0)
3990     {
3991         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
3992         if ((aux.length())>0)
3993         {
3994             out << "<STX_STRING:" << QString::number(aux.length()) << ">" << aux  << " ";
3995         }
3996     }
3997     //qDebug() << "FileManager::writeQuery - 100" << QT_ENDL;
3998     nameCol = rec.indexOf("cqz");
3999     if (nameCol>=0)
4000     {
4001         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4002         if ( ((aux.length())>0) && (0 < aux.toInt()) && (aux.toInt() < CQZones+1) )
4003         {
4004             out << "<CQZ:" << QString::number(aux.length()) << ">" << aux  << " ";
4005         }
4006     }
4007     nameCol = rec.indexOf("ituz");
4008     if (nameCol>=0)
4009     {
4010         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4011         if ( ((aux.length())>0) && (0 < aux.toInt()) && (aux.toInt() < ITUZones+1) )
4012         {
4013             out << "<ITUZ:" << QString::number(aux.length()) << ">" << aux  << " ";
4014         }
4015         //qDebug() << "FileManager::writeQuery: DXCC - Now..."  << QT_ENDL;
4016     }
4017     nameCol = rec.indexOf("dxcc");
4018     if (nameCol>=0)
4019     {
4020         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4021         if ((aux.length())>0)
4022         {
4023             out << "<DXCC:" << QString::number(aux.length()) << ">" << aux  << " ";
4024             //qDebug() << "FileManager::writeQuery: DXCC " << aux << QT_ENDL;
4025         }
4026         //qDebug() << "FileManager::writeQuery: DXCC - Exported!"  << QT_ENDL;
4027     }
4028     nameCol = rec.indexOf("address");
4029     if (nameCol>=0)
4030     {
4031         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4032         if ((aux.length())>0)
4033         {
4034             out << "<ADDRESS:" << QString::number(aux.length()) << ">" << aux  << " ";
4035         }
4036     }
4037     nameCol = rec.indexOf("age");
4038     if (nameCol>=0)
4039     {
4040         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4041         if ((aux.length())>0)
4042         {
4043             out << "<AGE:" << QString::number(aux.length()) << ">" << aux  << " ";
4044         }
4045     }
4046     nameCol = rec.indexOf("cnty");
4047     if (nameCol>=0)
4048     {
4049         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4050         if ((aux.length())>0)
4051         {
4052             out << "<CNTY:" << QString::number(aux.length()) << ">" << aux  << " ";
4053         }
4054     }
4055     nameCol = rec.indexOf("comment");
4056     if (nameCol>=0)
4057     {
4058         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4059         if ((aux.length())>0)
4060         {
4061             out << "<COMMENT:" << QString::number(aux.length()) << ">" << aux  << " ";
4062         }
4063         //qDebug() << "FileManager::writeQuery - 200" << QT_ENDL;
4064     }
4065     nameCol = rec.indexOf("a_index");
4066     if (nameCol>=0)
4067     {
4068         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4069         if ((aux.length())>0)
4070         {
4071             out << "<A_INDEX:" << QString::number(aux.length()) << ">" << aux  << " ";
4072         }
4073     }
4074     nameCol = rec.indexOf("ant_az");
4075     if (nameCol>=0)
4076     {
4077         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4078         if ((aux.length())>0)
4079         {
4080             out << "<ANT_AZ:" << QString::number(aux.length()) << ">" << aux  << " ";
4081         }
4082     }
4083     nameCol = rec.indexOf("ant_el");
4084     if (nameCol>=0)
4085     {
4086         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4087         if ((aux.length())>0)
4088         {
4089             out << "<ANT_EL:" << QString::number(aux.length()) << ">" << aux  << " ";
4090         }
4091     }
4092     nameCol = rec.indexOf("ant_path");
4093     if (nameCol>=0)
4094     {
4095         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4096         if ((aux.length())>0)
4097         {
4098             out << "<ANT_PATH:" << QString::number(aux.length()) << ">" << aux  << " ";
4099         }
4100     }
4101     nameCol = rec.indexOf("arrl_sect");
4102     if (nameCol>=0)
4103     {
4104         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4105         if ((aux.length())>0)
4106         {
4107             out << "<ARRL_SECT:" << QString::number(aux.length()) << ">" << aux  << " ";
4108         }
4109     }
4110     nameCol = rec.indexOf("checkcontest");
4111     if (nameCol>=0)
4112     {
4113         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4114         if ((aux.length())>0)
4115         {
4116             out << "<CHECKCONTEST:" << QString::number(aux.length()) << ">" << aux  << " ";
4117         }
4118     }
4119     //qDebug() << "FileManager::writeQuery - 30" << QT_ENDL;
4120     nameCol = rec.indexOf("class");
4121     if (nameCol>=0)
4122     {
4123         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4124         if ((aux.length())>0)
4125         {
4126             out << "<CLASS:" << QString::number(aux.length()) << ">" << aux  << " ";
4127         }
4128     }
4129     nameCol = rec.indexOf("cont");
4130     if (nameCol>=0)
4131     {
4132         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4133         if ((aux.length())>0)
4134         {
4135             out << "<CONT:" << QString::number(aux.length()) << ">" << aux  << " ";
4136         }
4137     }
4138     nameCol = rec.indexOf("contacted_op");
4139     if (nameCol>=0)
4140     {
4141         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4142         if (util->isValidCall(aux))
4143         {
4144             out << "<CONTACTED_OP:" << QString::number(aux.length()) << ">" << aux  << " ";
4145         }
4146     }
4147     nameCol = rec.indexOf("contest_id");
4148     if (nameCol>=0)
4149     {
4150         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4151         if ((aux.length())>0)
4152         {
4153             out << "<CONTEST_ID:" << QString::number(aux.length()) << ">" << aux  << " ";
4154         }
4155     }
4156     nameCol = rec.indexOf("points");
4157     if (nameCol>=0)
4158     {
4159         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4160         if ((aux.length())>0)
4161         {
4162             out << "<APP_KLOG_POINTS:" << QString::number(aux.length()) << ">" << aux  << " ";
4163         }
4164     }
4165     nameCol = rec.indexOf("multiplier");
4166     if (nameCol>=0)
4167     {
4168         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4169         if ((aux.length())>0)
4170         {
4171             out << "<APP_KLOG_MULTIPLIER:" << QString::number(aux.length()) << ">" << aux  << " ";
4172         }
4173         //qDebug() << "FileManager::writeQuery - 40" << QT_ENDL;
4174     }
4175     nameCol = rec.indexOf("transmiterid");
4176     if (nameCol>=0)
4177     {
4178         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4179         if ((aux.length())>0)
4180         {
4181             out << "<APP_KLOG_TRX:" << QString::number(aux.length()) << ">" << aux  << " ";
4182         }
4183     }
4184     nameCol = rec.indexOf("country");
4185     if (nameCol>=0)
4186     {
4187         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4188         if ((aux.length())>0)
4189         {
4190             out << "<COUNTRY:" << QString::number(aux.length()) << ">" << aux  << " ";
4191         }
4192     }
4193     nameCol = rec.indexOf("credit_submitted");
4194     if (nameCol>=0)
4195     {
4196         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4197         if ((aux.length())>0)
4198         {
4199             out << "<CREDIT_SUBMITTED:" << QString::number(aux.length()) << ">" << aux  << " ";
4200         }
4201     }
4202     nameCol = rec.indexOf("credit_granted");
4203     if (nameCol>=0)
4204     {
4205         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4206         if ((aux.length())>0)
4207         {
4208             out << "<CREDIT_GRANTED:" << QString::number(aux.length()) << ">" << aux  << " ";
4209         }
4210     }
4211     nameCol = rec.indexOf("distance");
4212     if (nameCol>=0)
4213     {
4214         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4215         if ((aux.length())>0)
4216         {
4217             out << "<DISTANCE:" << QString::number(aux.length()) << ">" << aux  << " ";
4218         }
4219     }
4220     nameCol = rec.indexOf("darc_dok");
4221     if (nameCol>=0)
4222     {
4223         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4224         if ((aux.length())>0)
4225         {
4226             out << "<DARC_DOK:" << QString::number(aux.length()) << ">" << aux  << " ";
4227         }
4228     }
4229     nameCol = rec.indexOf("eq_call");
4230     if (nameCol>=0)
4231     {
4232         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4233         if (util->isValidCall(aux))
4234         {
4235             out << "<EQ_CALL:" << QString::number(aux.length()) << ">" << aux  << " ";
4236         }
4237     }
4238     nameCol = rec.indexOf("email");
4239     if (nameCol>=0)
4240     {
4241         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4242         if ((aux.length())>0)
4243         {
4244             if (aux.contains("@") && (aux.contains(".")))
4245             {
4246                 out << "<EMAIL:" << QString::number(aux.length()) << ">" << aux  << " ";
4247             }
4248         }
4249     }
4250     nameCol = rec.indexOf("eqsl_qslrdate");
4251     QDate date;
4252     if (nameCol>=0)
4253     {
4254             aux = (query.value(nameCol)).toString();
4255             date = util->getDateFromSQliteString(aux);
4256             if (date.isValid())
4257             {
4258                 aux = util->getADIFDateFromQDate(date);
4259                 out << "<EQSL_QSLRDATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4260             }
4261     }
4262     nameCol = rec.indexOf("eqsl_qslsdate");
4263     if (nameCol>=0)
4264     {
4265         aux = (query.value(nameCol)).toString();
4266         date = util->getDateFromSQliteString(aux);
4267         if (date.isValid())
4268         {
4269             aux = util->getADIFDateFromQDate(date);
4270             out << "<EQSL_QSLSDATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4271         }
4272     }
4273     nameCol = rec.indexOf("eqsl_qsl_rcvd");
4274     if (nameCol>=0)
4275     {
4276         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4277         if (  ((aux.length())==1) && (aux!="N") )
4278         {
4279             out << "<EQSL_QSL_RCVD:" << QString::number(aux.length()) << ">" << aux  << " ";
4280         }
4281     }
4282     nameCol = rec.indexOf("eqsl_qsl_sent");
4283     if (nameCol>=0)
4284     {
4285         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4286         if (  ((aux.length())==1) && (aux!="N") )
4287         {
4288             out << "<EQSL_QSL_SENT:" << QString::number(aux.length()) << ">" << aux  << " ";
4289         }
4290     }
4291     nameCol = rec.indexOf("fists");
4292     if (nameCol>=0)
4293     {
4294         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4295         if ((aux.length())>0)
4296         {
4297             out << "<FISTS:" << QString::number(aux.length()) << ">" << aux  << " ";
4298         }
4299     }
4300     nameCol = rec.indexOf("fists_cc");
4301     if (nameCol>=0)
4302     {
4303         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4304         if ((aux.length())>0)
4305         {
4306             out << "<FISTS_CC:" << QString::number(aux.length()) << ">" << aux  << " ";
4307         }
4308     }
4309     nameCol = rec.indexOf("force_init");
4310     if (nameCol>=0)
4311     {
4312         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4313         if ((aux.length())>0)
4314         {
4315             out << "<FORCE_INIT:" << QString::number(aux.length()) << ">" << aux  << " ";
4316         }
4317     }
4318     nameCol = rec.indexOf("guest_op");
4319     if (nameCol>=0)
4320     {
4321         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4322         if ((aux.length())>0)
4323         {
4324             out << "<GUEST_OP:" << QString::number(aux.length()) << ">" << aux  << " ";
4325         }
4326     }
4327     nameCol = rec.indexOf("hrdlog_qso_upload_date");
4328     if (nameCol>=0)
4329     {
4330         aux = (query.value(nameCol)).toString();
4331         date = util->getDateFromSQliteString(aux);
4332         if (date.isValid())
4333         {
4334             aux = util->getADIFDateFromQDate(date);
4335             out << "<HRDLOG_QSO_UPLOAD_DATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4336         }
4337     }
4338     nameCol = rec.indexOf("hrdlog_qso_upload_status");
4339     if (nameCol>=0)
4340     {
4341         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4342         if ( ((aux.length())==1)  && ((aux!="Y") || (aux!="N") || (aux!="M")) )
4343         {
4344             out << "<HRDLOG_QSO_UPLOAD_STATUS:" << QString::number(aux.length()) << ">" << aux  << " ";
4345         }
4346     }
4347     nameCol = rec.indexOf("my_antenna");
4348     //qDebug() << Q_FUNC_INFO << ": my_antenna-0";
4349     if (nameCol>=0)
4350     {
4351         //qDebug() << Q_FUNC_INFO << ": my_antenna-1";
4352         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4353         //qDebug() << Q_FUNC_INFO << ": my_antenna: " << aux;
4354         if ((aux.length())>0)
4355         {
4356             //qDebug() << Q_FUNC_INFO << ": my_antenna ... printing...";
4357             out << "<MY_ANTENNA:" << QString::number(aux.length()) << ">" << aux  << " ";
4358         }
4359     }
4360     //qDebug() << Q_FUNC_INFO << ": my_antenna-99";
4361     nameCol = rec.indexOf("my_dxcc");
4362     if (nameCol>=0)
4363     {
4364         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4365         if ((aux.length())>0)
4366         {
4367             out << "<MY_DXCC:" << QString::number(aux.length()) << ">" << aux  << " ";
4368         }
4369     }
4370     nameCol = rec.indexOf("my_fists");
4371     if (nameCol>=0)
4372     {
4373         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4374         if ((aux.length())>0)
4375         {
4376             out << "<MY_FISTS:" << QString::number(aux.length()) << ">" << aux  << " ";
4377         }
4378     }
4379     nameCol = rec.indexOf("iota");
4380     if (nameCol>=0)
4381     {
4382         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4383         //qDebug() << "FileManager::writeQuery (IOTA): " << aux << QT_ENDL;
4384         if (((aux.length())>=4) && ((aux.length())<=6))
4385         {
4386             out << "<IOTA:" << QString::number(aux.length()) << ">" << aux  << " ";
4387         }
4388     }
4389     nameCol = rec.indexOf("iota_island_id");
4390     if (nameCol>=0)
4391     {
4392         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4393         //qDebug() << "FileManager::writeQuery (IOTA_ID): " << aux << QT_ENDL;
4394         if ((aux.length())>0)
4395         {
4396             out << "<IOTA_ISLAND_ID:" << QString::number(aux.length()) << ">" << aux  << " ";
4397         }
4398     }
4399     nameCol = rec.indexOf("my_iota");
4400     if (nameCol>=0)
4401     {
4402         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4403         if (((aux.length())>=4) && ((aux.length())<=6))
4404         {
4405             out << "<MY_IOTA:" << QString::number(aux.length()) << ">" << aux  << " ";
4406         }
4407     }
4408     nameCol = rec.indexOf("my_iota_island_id");
4409     if (nameCol>=0)
4410     {
4411         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4412         if ((aux.length())>0)
4413         {
4414             out << "<MY_IOTA_ISLAND_ID:" << QString::number(aux.length()) << ">" << aux  << " ";
4415         }
4416     }
4417     nameCol = rec.indexOf("k_index");
4418     if (nameCol>=0)
4419     {
4420         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4421         if ((aux.length())>0)
4422         {
4423             out << "<K_INDEX:" << QString::number(aux.length()) << ">" << aux  << " ";
4424         }
4425     }
4426     nameCol = rec.indexOf("my_itu_zone");
4427     if (nameCol>=0)
4428     {
4429         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4430         if ((aux.length())>0)
4431         {
4432             out << "<MY_ITU_ZONE:" << QString::number(aux.length()) << ">" << aux  << " ";
4433         }
4434     }
4435     nameCol = rec.indexOf("lat");
4436     if (nameCol>=0)
4437     {
4438         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4439         if ((aux.length())>0)
4440         {
4441             out << "<LAT:" << QString::number(aux.length()) << ">" << aux  << " ";
4442         }
4443     }
4444     nameCol = rec.indexOf("lon");
4445     if (nameCol>=0)
4446     {
4447         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4448         if ((aux.length())>0)
4449         {
4450             out << "<LON:" << QString::number(aux.length()) << ">" << aux  << " ";
4451         }
4452      }
4453     nameCol = rec.indexOf("my_lat");
4454     if (nameCol>=0)
4455     {
4456         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4457         if ((aux.length())>0)
4458         {
4459             out << "<MY_LAT:" << QString::number(aux.length()) << ">" << aux  << " ";
4460         }
4461      }
4462     nameCol = rec.indexOf("my_lon");
4463     if (nameCol>=0)
4464     {
4465         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4466         if ((aux.length())>0)
4467         {
4468             out << "<MY_LON:" << QString::number(aux.length()) << ">" << aux  << " ";
4469         }
4470      }
4471     nameCol = rec.indexOf("lotw_qslrdate");
4472     if (nameCol>=0)
4473     {
4474         aux = (query.value(nameCol)).toString();
4475         date = util->getDateFromSQliteString(aux);
4476         if (date.isValid())
4477         {
4478             aux = util->getADIFDateFromQDate(date);
4479             out << "<LOTW_QSLRDATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4480         }
4481     }
4482     nameCol = rec.indexOf("lotw_qslsdate");
4483     if (nameCol>=0)
4484     {
4485         aux = (query.value(nameCol)).toString();
4486         date = util->getDateFromSQliteString(aux);
4487         if (date.isValid())
4488         {
4489             aux = util->getADIFDateFromQDate(date);
4490             out << "<LOTW_QSLSDATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4491         }
4492     }
4493     nameCol = rec.indexOf("lotw_qsl_rcvd");
4494     if (nameCol>=0)
4495     {
4496         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4497         if ( ((aux.length())==1) && (aux!="N") )
4498         {
4499             out << "<LOTW_QSL_RCVD:" << QString::number(aux.length()) << ">" << aux  << " ";
4500         }
4501     }
4502     nameCol = rec.indexOf("lotw_qsl_sent");
4503     if (nameCol>=0)
4504     {
4505         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4506         if ( ((aux.length())==1)  && (aux!="N") )
4507         {
4508             out << "<LOTW_QSL_SENT:" << QString::number(aux.length()) << ">" << aux  << " ";
4509         }
4510     }
4511     nameCol = rec.indexOf("clublog_qso_upload_date");
4512     if (nameCol>=0)
4513     {
4514         aux = (query.value(nameCol)).toString();
4515         date = util->getDateFromSQliteString(aux);
4516         if (date.isValid())
4517         {
4518             aux = util->getADIFDateFromQDate(date);
4519             out << "<CLUBLOG_QSO_UPLOAD_DATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4520         }
4521     }
4522     nameCol = rec.indexOf("clublog_qso_upload_status");
4523     if (nameCol>=0)
4524     {
4525         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4526         if ( ((aux.length())==1)  && ((aux!="Y") || (aux!="N") || (aux!="M")) )
4527         {
4528             out << "<CLUBLOG_QSO_UPLOAD_STATUS:" << QString::number(aux.length()) << ">" << aux  << " ";
4529         }
4530     }
4531     nameCol = rec.indexOf("qrzcom_qso_upload_date");
4532     if (nameCol>=0)
4533     {
4534         aux = (query.value(nameCol)).toString();
4535         date = util->getDateFromSQliteString(aux);
4536         if (date.isValid())
4537         {
4538             aux = util->getADIFDateFromQDate(date);
4539             out << "<QRZCOM_QSO_UPLOAD_DATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4540         }
4541     }
4542     nameCol = rec.indexOf("qrzcom_qso_upload_status");
4543     if (nameCol>=0)
4544     {
4545         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4546         if ( ((aux.length())==1)  && ((aux!="Y") || (aux!="N") || (aux!="M")) )
4547         {
4548             out << "<QRZCOM_QSO_UPLOAD_STATUS:" << QString::number(aux.length()) << ">" << aux  << " ";
4549         }
4550     }
4551     nameCol = rec.indexOf("max_bursts");
4552     if (nameCol>=0)
4553     {
4554         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4555         if ((aux.length())>0)
4556         {
4557             out << "<MAX_BURSTS:" << QString::number(aux.length()) << ">" << aux  << " ";
4558         }
4559     }
4560     nameCol = rec.indexOf("ms_shower");
4561     if (nameCol>=0)
4562     {
4563         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4564         if ((aux.length())>0)
4565         {
4566             out << "<MS_SHOWER:" << QString::number(aux.length()) << ">" << aux  << " ";
4567         }
4568     }
4569     nameCol = rec.indexOf("my_city");
4570     if (nameCol>=0)
4571     {
4572         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4573         if ((aux.length())>0)
4574         {
4575             out << "<MY_CITY:" << QString::number(aux.length()) << ">" << aux  << " ";
4576         }
4577     }
4578     nameCol = rec.indexOf("my_cnty");
4579     if (nameCol>=0)
4580     {
4581         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4582         if ((aux.length())>0)
4583         {
4584             out << "<MY_CNTY:" << QString::number(aux.length()) << ">" << aux  << " ";
4585         }
4586     }
4587     nameCol = rec.indexOf("my_country");
4588     if (nameCol>=0)
4589     {
4590         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4591         if ((aux.length())>0)
4592         {
4593             out << "<MY_COUNTRY:" << QString::number(aux.length()) << ">" << aux  << " ";
4594         }
4595     }
4596     nameCol = rec.indexOf("my_cq_zone");
4597     if (nameCol>=0)
4598     {
4599         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4600         if ((aux.toInt()>0) && (aux.toInt()<41))
4601         {
4602             out << "<MY_CQ_ZONE:" << QString::number(aux.length()) << ">" << aux  << " ";
4603         }
4604     }
4605     nameCol = rec.indexOf("my_name");
4606     if (nameCol>=0)
4607     {
4608         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4609         if ((aux.length())>0)
4610         {
4611             out << "<MY_NAME:" << QString::number(aux.length()) << ">" << aux  << " ";
4612         }
4613     }
4614     nameCol = rec.indexOf("name");
4615     if (nameCol>=0)
4616     {
4617         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4618         if ((aux.length())>0)
4619         {
4620             out << "<NAME:" << QString::number(aux.length()) << ">" << aux  << " ";
4621         }
4622     }
4623     nameCol = rec.indexOf("operator");
4624     if (nameCol>=0)
4625     {
4626         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4627         if (util->isValidCall(aux))
4628         {
4629             out << "<OPERATOR:" << QString::number(aux.length()) << ">" << aux  << " ";
4630         }
4631     }
4632     nameCol = rec.indexOf("owner_callsign");
4633     if (nameCol>=0)
4634     {
4635         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4636         if (util->isValidCall(aux))
4637         {
4638             out << "<OWNER_CALLSIGN:" << QString::number(aux.length()) << ">" << aux  << " ";
4639         }
4640     }
4641     nameCol = rec.indexOf("my_postal_code");
4642     if (nameCol>=0)
4643     {
4644         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4645         if ((aux.length())>0)
4646         {
4647             out << "<MY_POSTAL_CODE:" << QString::number(aux.length()) << ">" << aux  << " ";
4648         }
4649     }
4650     nameCol = rec.indexOf("my_rig");
4651     if (nameCol>=0)
4652     {
4653         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4654         if ((aux.length())>0)
4655         {
4656             out << "<MY_RIG:" << QString::number(aux.length()) << ">" << aux  << " ";
4657         }
4658     }
4659     nameCol = rec.indexOf("my_sig");
4660     if (nameCol>=0)
4661     {
4662         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4663         if ((aux.length())>0)
4664         {
4665             out << "<MY_SIG:" << QString::number(aux.length()) << ">" << aux  << " ";
4666         }
4667     }
4668     nameCol = rec.indexOf("my_sota_ref");
4669     if (nameCol>=0)
4670     {
4671         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4672         if ((aux.length())>0)
4673         {
4674             out << "<MY_SOTA_REF:" << QString::number(aux.length()) << ">" << aux  << " ";
4675         }
4676     }
4677     nameCol = rec.indexOf("my_postal_code");
4678     if (nameCol>=0)
4679     {
4680         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4681         if ((aux.length())>0)
4682         {
4683             out << "<MY_POSTAL_CODE:" << QString::number(aux.length()) << ">" << aux  << " ";
4684         }
4685     }
4686     nameCol = rec.indexOf("my_state");
4687     if (nameCol>=0)
4688     {
4689         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4690         if ((aux.length())>0)
4691         {
4692             out << "<MY_STATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4693         }
4694     }
4695     nameCol = rec.indexOf("my_street");
4696     if (nameCol>=0)
4697     {
4698         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4699         if ((aux.length())>0)
4700         {
4701             out << "<MY_STREET:" << QString::number(aux.length()) << ">" << aux  << " ";
4702         }
4703     }
4704     nameCol = rec.indexOf("notes");
4705     if (nameCol>=0)
4706     {
4707         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4708         if ((aux.length())>0)
4709         {
4710             aux.replace("\n", "---");
4711             out << "<NOTES:" << QString::number(aux.length()) << ">" << aux  << " ";
4712         }
4713     }
4714     nameCol = rec.indexOf("nr_bursts");
4715     if (nameCol>=0)
4716     {
4717         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4718         if ((aux.length())>0)
4719         {
4720             out << "<NR_BURSTS:" << QString::number(aux.length()) << ">" << aux  << " ";
4721         }
4722     }
4723     nameCol = rec.indexOf("nr_pings");
4724     if (nameCol>=0)
4725     {
4726         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4727         if ((aux.length())>0)
4728         {
4729             out << "<NR_PINGS:" << QString::number(aux.length()) << ">" << aux  << " ";
4730         }
4731     }
4732     nameCol = rec.indexOf("pfx");
4733     if (nameCol>=0)
4734     {
4735         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4736         if ((aux.length())>0)
4737         {
4738             out << "<PFX:" << QString::number(aux.length()) << ">" << aux  << " ";
4739         }
4740     }
4741     nameCol = rec.indexOf("precedence");
4742     if (nameCol>=0)
4743     {
4744         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4745         if ((aux.length())>0)
4746         {
4747             out << "<PRECEDENCE:" << QString::number(aux.length()) << ">" << aux  << " ";
4748         }
4749     }
4750     nameCol = rec.indexOf("public_key");
4751     if (nameCol>=0)
4752     {
4753         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4754         if ((aux.length())>0){
4755             out << "<PUBLIC_KEY:" << QString::number(aux.length()) << ">" << aux  << " ";
4756         }
4757     }
4758     nameCol = rec.indexOf("qslmsg");
4759     if (nameCol>=0)
4760     {
4761         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4762         if ((aux.length())>0){
4763             out << "<QSLMSG:" << QString::number(aux.length()) << ">" << aux  << " ";
4764         }
4765     }
4766     nameCol = rec.indexOf("qslrdate");
4767     if (nameCol>=0)
4768     {
4769         aux = (query.value(nameCol)).toString();
4770         date = util->getDateFromSQliteString(aux);
4771         if (date.isValid())
4772         {
4773             aux = util->getADIFDateFromQDate(date);
4774             out << "<QSLRDATE:" << QString::number(aux.length()) << ">" << aux << " ";
4775         }
4776     }
4777     nameCol = rec.indexOf("qslsdate");
4778     if (nameCol>=0)
4779     {
4780         aux = (query.value(nameCol)).toString();
4781         date = util->getDateFromSQliteString(aux);
4782         if (date.isValid())
4783         {
4784             aux = util->getADIFDateFromQDate(date);
4785             out << "<QSLSDATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4786         }
4787     }
4788     nameCol = rec.indexOf("qsl_rcvd");
4789     if (nameCol>=0)
4790     {
4791         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4792         if (((aux.length())==1) && (aux!="N") )
4793         {
4794             out << "<QSL_RCVD:" << QString::number(aux.length()) << ">" << aux  << " ";
4795 
4796             nameCol = rec.indexOf("qsl_rcvd_via");
4797             aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4798             if ((aux.length())==1)
4799             {
4800                 out << "<QSL_RCVD_VIA:" << QString::number(aux.length()) << ">" << aux  << " ";
4801             }
4802         }
4803     }
4804     nameCol = rec.indexOf("qsl_sent");
4805     if (nameCol>=0)
4806     {
4807         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4808         if (((aux.length())==1) && (aux!="N") )
4809         {
4810             out << "<QSL_SENT:" << QString::number(aux.length()) << ">" << aux  << " ";
4811             nameCol = rec.indexOf("qsl_sent_via");
4812             aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4813             if ((aux.length())==1)
4814             {
4815                 out << "<QSL_SENT_VIA:" << QString::number(aux.length()) << ">" << aux  << " ";
4816             }
4817         }
4818     }
4819     nameCol = rec.indexOf("qsl_via");
4820     if (nameCol>=0)
4821     {
4822         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4823         if ((aux.length())>0){
4824             out << "<QSL_VIA:" << QString::number(aux.length()) << ">" << aux  << " ";
4825         }
4826     }
4827     nameCol = rec.indexOf("qso_complete");
4828     if (nameCol>=0)
4829     {
4830         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4831         if ((aux.length())>0)
4832         {
4833             out << "<QSO_COMPLETE:" << QString::number(aux.length()) << ">" << aux  << " ";
4834         }
4835     }
4836     nameCol = rec.indexOf("qso_random");
4837     if (nameCol>=0)
4838     {
4839         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4840         if ((aux.length())>0)
4841         {
4842             out << "<QSO_RANDOM:" << QString::number(aux.length()) << ">" << aux  << " ";
4843         }
4844     }
4845     nameCol = rec.indexOf("qth");
4846     if (nameCol>=0)
4847     {
4848         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4849         if ((aux.length())>0){
4850             out << "<QTH:" << QString::number(aux.length()) << ">" << aux  << " ";
4851         }
4852     }
4853     nameCol = rec.indexOf("rst_sent");
4854     if (nameCol>=0)
4855     {
4856         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4857         if ((aux.length())>0){
4858             out << "<RST_SENT:" << QString::number(aux.length()) << ">" << aux  << " ";
4859         }
4860     }
4861     nameCol = rec.indexOf("rst_rcvd");
4862     if (nameCol>=0)
4863     {
4864         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4865         if ((aux.length())>0){
4866             out << "<RST_RCVD:" << QString::number(aux.length()) << ">" << aux  << " ";
4867         }
4868     }
4869     nameCol = rec.indexOf("region");
4870     if (nameCol>=0)
4871     {
4872         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4873         if ((aux.length())>0){
4874             out << "<REGION:" << QString::number(aux.length()) << ">" << aux  << " ";
4875         }
4876     }
4877     nameCol = rec.indexOf("rig");
4878     if (nameCol>=0)
4879     {
4880         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4881         if ((aux.length())>0){
4882             out << "<RIG:" << QString::number(aux.length()) << ">" << aux  << " ";
4883         }
4884     }
4885     nameCol = rec.indexOf("rx_pwr");
4886     if (nameCol>=0)
4887     {
4888         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4889         if (((aux.length())>0) && (aux.toDouble()>0) )
4890         {
4891             out << "<RX_PWR:" << QString::number(aux.length()) << ">" << aux  << " ";
4892         }
4893     }
4894     nameCol = rec.indexOf("tx_pwr");
4895     if (nameCol>=0)
4896     {
4897         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4898         if ( ((aux.length())>0) && (aux.toDouble()>0))
4899         {
4900             out << "<TX_PWR:" << QString::number(aux.length()) << ">" << aux  << " ";
4901         }
4902     }
4903     nameCol = rec.indexOf("sat_mode");
4904     if (nameCol>=0)
4905     {
4906         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4907         if ((aux.length())>0)
4908         {
4909             out << "<SAT_MODE:" << QString::number(aux.length()) << ">" << aux  << " ";
4910         }
4911     }
4912     nameCol = rec.indexOf("sfi");
4913     if (nameCol>=0)
4914     {
4915         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4916         if ((aux.length())>0){
4917             out << "<SFI:" << QString::number(aux.length()) << ">" << aux  << " ";
4918         }
4919     }
4920     nameCol = rec.indexOf("sig");
4921     if (nameCol>=0)
4922     {
4923         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4924         if ((aux.length())>0){
4925             out << "<SIG:" << QString::number(aux.length()) << ">" << aux  << " ";
4926         }
4927     }
4928     nameCol = rec.indexOf("sig_info");
4929     if (nameCol>=0)
4930     {
4931         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4932         if ((aux.length())>0){
4933             out << "<SIG_INFO:" << QString::number(aux.length()) << ">" << aux  << " ";
4934         }
4935     }
4936     nameCol = rec.indexOf("silent_key");
4937     if (nameCol>=0)
4938     {
4939         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4940         if ((aux.length())>0){
4941             out << "<SILENT_KEY:" << QString::number(aux.length()) << ">" << aux  << " ";
4942         }
4943     }
4944     nameCol = rec.indexOf("skcc");
4945     if (nameCol>=0)
4946     {
4947         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4948         if ((aux.length())>0){
4949             out << "<SKCC:" << QString::number(aux.length()) << ">" << aux  << " ";
4950         }
4951     }
4952     nameCol = rec.indexOf("sota_ref");
4953     if (nameCol>=0)
4954     {
4955         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4956         if ((aux.length())>0){
4957             out << "<SOTA_REF:" << QString::number(aux.length()) << ">" << aux  << " ";
4958         }
4959     }
4960     nameCol = rec.indexOf("state");
4961     if (nameCol>=0)
4962     {
4963         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4964         if ((aux.length())>0){
4965             out << "<STATE:" << QString::number(aux.length()) << ">" << aux  << " ";
4966         }
4967     }
4968     nameCol = rec.indexOf("swl");
4969     if (nameCol>=0)
4970     {
4971         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4972         if ((aux.length())>0){
4973             out << "<SWL:" << QString::number(aux.length()) << ">" << aux  << " ";
4974         }
4975     }
4976     nameCol = rec.indexOf("ten_ten");
4977     if (nameCol>=0)
4978     {
4979         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4980         if ((aux.length())>0){
4981             out << "<TEN_TEN:" << QString::number(aux.length()) << ">" << aux  << " ";
4982         }
4983     }
4984     nameCol = rec.indexOf("ten_ten");
4985     if (nameCol>=0)
4986     {
4987         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4988         if ((aux.length())>0){
4989             out << "<TEN_TEN:" << QString::number(aux.length()) << ">" << aux  << " ";
4990         }
4991     }
4992     nameCol = rec.indexOf("uksmg");
4993     if (nameCol>=0)
4994     {
4995         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
4996         if ((aux.length())>0){
4997             out << "<UKSMG:" << QString::number(aux.length()) << ">" << aux  << " ";
4998         }
4999     }
5000     nameCol = rec.indexOf("ve_prov");
5001     if (nameCol>=0)
5002     {
5003         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5004         if ((aux.length())>0){
5005             out << "<VE_PROV:" << QString::number(aux.length()) << ">" << aux  << " ";
5006         }
5007     }
5008     nameCol = rec.indexOf("my_usaca_counties");
5009     if (nameCol>=0)
5010     {
5011         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5012         if ((aux.length())>0){
5013             out << "<MY_USACA_COUNTIES:" << QString::number(aux.length()) << ">" << aux  << " ";
5014         }
5015     }
5016     nameCol = rec.indexOf("usaca_counties");
5017     if (nameCol>=0)
5018     {
5019         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5020         if ((aux.length())>0){
5021             out << "<USACA_COUNTIES:" << QString::number(aux.length()) << ">" << aux  << " ";
5022         }
5023     }
5024     nameCol = rec.indexOf("vucc_grids");
5025     if (nameCol>=0)
5026     {
5027         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5028         if ((aux.length())>0){
5029             out << "<VUCC_GRIDS:" << QString::number(aux.length()) << ">" << aux  << " ";
5030         }
5031     }
5032     nameCol = rec.indexOf("my_vucc_grids");
5033     if (nameCol>=0)
5034     {
5035         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5036         if ((aux.length())>0){
5037             out << "<MY_VUCC_GRIDS:" << QString::number(aux.length()) << ">" << aux  << " ";
5038         }
5039     }
5040     nameCol = rec.indexOf("web");
5041     if (nameCol>=0)
5042     {
5043         aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5044         if ((aux.length())>0){
5045             out << "<WEB:" << QString::number(aux.length()) << ">" << aux  << " ";
5046         }
5047     }
5048     if (_logN == -1)
5049     {
5050         nameCol = rec.indexOf("lognumber");
5051         if (nameCol>=0)
5052         {
5053             aux = (query.value(nameCol)).toString(); aux = util->checkAndFixASCIIinADIF(aux);
5054             if ((aux.length())>0)
5055             {
5056                 out << "<APP_KLOG_LOGN:" << QString::number(aux.length()) << ">" << aux  << " ";
5057             }
5058         }
5059     }
5060     out << "<EOR>" << QT_ENDL;
5061 }
5062 
5063 
5064 
5065 
5066 
5067 
5068 
5069 
5070 
5071 
5072 
5073 
5074 
5075 
5076