forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogFormat.cpp
1119 lines (934 loc) · 37 KB
/
LogFormat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <QtSql>
#include <QSqlDriver>
#include "LogFormat.h"
#include "AdiFormat.h"
#include "AdxFormat.h"
#include "JsonFormat.h"
#include "CSVFormat.h"
#include "data/Data.h"
#include "core/debug.h"
#include "core/Gridsquare.h"
#include "core/Callsign.h"
#include "data/BandPlan.h"
#include "models/LogbookModel.h"
MODULE_IDENTIFICATION("qlog.logformat.logformat");
LogFormat::LogFormat(QTextStream& stream) :
QObject(nullptr),
stream(stream),
exportedFields("*"),
duplicateQSOFunc(nullptr)
{
FCT_IDENTIFICATION;
this->defaults = nullptr;
}
LogFormat::~LogFormat() {
FCT_IDENTIFICATION;
}
LogFormat* LogFormat::open(QString type, QTextStream& stream) {
FCT_IDENTIFICATION;
qCDebug(function_parameters)<<type;
type = type.toLower();
if (type == "adi") {
return open(LogFormat::ADI, stream);
}
else if (type == "adx") {
return open(LogFormat::ADX, stream);
}
else if (type == "json") {
return open(LogFormat::JSON, stream);
}
else if (type == "csv") {
return open(LogFormat::CSV, stream);
}
else if (type == "cabrillo") {
return open(LogFormat::JSON, stream);
}
else {
return nullptr;
}
}
LogFormat* LogFormat::open(LogFormat::Type type, QTextStream& stream) {
FCT_IDENTIFICATION;
qCDebug(function_parameters)<<type;
switch (type) {
case LogFormat::ADI:
return new AdiFormat(stream);
case LogFormat::ADX:
return new AdxFormat(stream);
case LogFormat::JSON:
return new JsonFormat(stream);
case LogFormat::CSV:
return new CSVFormat(stream);
case LogFormat::CABRILLO:
return nullptr;
default:
return nullptr;
}
}
void LogFormat::setDefaults(QMap<QString, QString>& defaults) {
FCT_IDENTIFICATION;
this->defaults = &defaults;
}
void LogFormat::setFilterDateRange(const QDate &start, const QDate &end)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters)<<start << " " << end;
this->filterStartDate = start;
this->filterEndDate = end;
}
void LogFormat::setFilterMyCallsign(const QString &myCallsing)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << myCallsing;
this->filterMyCallsign = myCallsing;
}
void LogFormat::setFilterMyGridsquare(const QString &myGridsquare)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << myGridsquare;
this->filterMyGridsquare = myGridsquare;
}
void LogFormat::setFilterSentPaperQSL(bool includeNo, bool includeIgnore, bool includeAlreadySent)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << includeNo << includeIgnore << includeAlreadySent;
this->filterSentPaperQSL << "'R'" << "'Q'";
if ( includeNo )
{
this->filterSentPaperQSL << "'N'";
}
if ( includeIgnore )
{
this->filterSentPaperQSL << "'I'";
}
if ( includeAlreadySent )
{
this->filterSentPaperQSL << "'Y'";
}
}
void LogFormat::setFilterSendVia(const QString &value)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << value;
this->filterSendVia = value;
}
QString LogFormat::getWhereClause()
{
FCT_IDENTIFICATION;
whereClause.clear();
whereClause << "1 = 1"; //generic filter
if ( isDateRange() )
{
whereClause << "(start_time BETWEEN :start_date AND :end_date)";
}
if ( !filterMyCallsign.isEmpty() )
{
whereClause << "upper(station_callsign) = upper(:stationCallsign)";
}
if ( !filterMyGridsquare.isEmpty() )
{
whereClause << "upper(my_gridsquare) = upper(:myGridsquare)";
}
if ( !filterSentPaperQSL.isEmpty() )
{
whereClause << QString("upper(qsl_sent) in (%1)").arg(filterSentPaperQSL.join(", "));
}
if ( !filterSendVia.isEmpty() )
{
whereClause << ( ( filterSendVia == " " ) ? "qsl_sent_via is NULL"
: "upper(qsl_sent_via) = upper(:qsl_sent_via)");
}
return whereClause.join(" AND ");
}
void LogFormat::bindWhereClause(QSqlQuery &query)
{
FCT_IDENTIFICATION;
if ( isDateRange() )
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
query.bindValue(":start_date", filterStartDate.startOfDay());
query.bindValue(":end_date", filterEndDate.endOfDay());
#else /* Due to ubuntu 20.04 where qt5.12 is present */
query.bindValue(":start_date", QDateTime(filterStartDate));
query.bindValue(":end_date", QDateTime(filterEndDate));
#endif
}
if ( !filterMyCallsign.isEmpty() )
{
query.bindValue(":stationCallsign", filterMyCallsign);
}
if ( !filterMyGridsquare.isEmpty() )
{
query.bindValue(":myGridsquare", filterMyGridsquare);
}
if ( !filterSendVia.isEmpty() )
{
if ( filterSendVia != " " )
{
query.bindValue(":qsl_sent_via", filterSendVia);
}
}
}
void LogFormat::setExportedFields(const QStringList &fieldsList)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << fieldsList;
exportedFields = fieldsList;
}
void LogFormat::setUpdateDxcc(bool updateDxcc) {
FCT_IDENTIFICATION;
qCDebug(function_parameters)<<updateDxcc;
this->updateDxcc = updateDxcc;
}
void LogFormat::setDuplicateQSOCallback(duplicateQSOBehaviour (*func)(QSqlRecord *, QSqlRecord *))
{
FCT_IDENTIFICATION;
duplicateQSOFunc = func;
}
#define RECORDIDX(a) ( (a) - 1 )
unsigned long LogFormat::runImport(QTextStream& importLogStream,
const StationProfile *defaultStationProfile,
unsigned long *warnings,
unsigned long *errors)
{
FCT_IDENTIFICATION;
this->importStart();
unsigned long count = 0L;
*errors = 0L;
*warnings = 0L;
unsigned long processedRec = 0;
QSqlQuery dupQuery;
QSqlQuery insertQuery;
// It is important to use callsign index here
if ( ! dupQuery.prepare("SELECT * FROM contacts "
"WHERE callsign=upper(:callsign) "
"AND upper(mode)=upper(:mode) "
"AND upper(band)=upper(:band) "
"AND ABS(JULIANDAY(start_time)-JULIANDAY(datetime(:startdate)))*24*60<30") )
{
qWarning() << "cannot prepare Dup statement";
return 0;
}
QSqlDatabase::database().transaction();
QSqlTableModel model;
model.setTable("contacts");
model.removeColumn(model.fieldIndex("id"));
QSqlRecord record = model.record();
duplicateQSOBehaviour dupSetting = LogFormat::ASK_NEXT;
auto setIfEmpty = [&](int column, const QString &value)
{
if ( record.value(RECORDIDX(column)).toString().isEmpty() && !value.isEmpty() )
record.setValue(RECORDIDX(column), value);
};
auto setMyDefaultProfile = [&]()
{
setIfEmpty(LogbookModel::COLUMN_MY_DXCC, QString::number(defaultStationProfile->dxcc));
setIfEmpty(LogbookModel::COLUMN_STATION_CALLSIGN, defaultStationProfile->callsign);
setIfEmpty(LogbookModel::COLUMN_MY_GRIDSQUARE, defaultStationProfile->locator);
setIfEmpty(LogbookModel::COLUMN_MY_NAME, defaultStationProfile->operatorName);
setIfEmpty(LogbookModel::COLUMN_MY_CITY_INTL, defaultStationProfile->qthName);
setIfEmpty(LogbookModel::COLUMN_MY_CITY, Data::removeAccents(defaultStationProfile->qthName));
setIfEmpty(LogbookModel::COLUMN_MY_IOTA, defaultStationProfile->iota);
setIfEmpty(LogbookModel::COLUMN_MY_POTA_REF, defaultStationProfile->pota);
setIfEmpty(LogbookModel::COLUMN_MY_SOTA_REF, defaultStationProfile->sota);
setIfEmpty(LogbookModel::COLUMN_MY_SIG_INTL, defaultStationProfile->sig);
setIfEmpty(LogbookModel::COLUMN_MY_SIG, Data::removeAccents(defaultStationProfile->sig));
setIfEmpty(LogbookModel::COLUMN_MY_SIG_INFO_INTL, defaultStationProfile->sigInfo);
setIfEmpty(LogbookModel::COLUMN_MY_SIG_INFO, Data::removeAccents(defaultStationProfile->sigInfo));
setIfEmpty(LogbookModel::COLUMN_MY_VUCC_GRIDS, defaultStationProfile->vucc);
setIfEmpty(LogbookModel::COLUMN_MY_ITU_ZONE, QString::number(defaultStationProfile->ituz));
setIfEmpty(LogbookModel::COLUMN_MY_CQ_ZONE, QString::number(defaultStationProfile->cqz));
setIfEmpty(LogbookModel::COLUMN_MY_COUNTRY_INTL, defaultStationProfile->country);
setIfEmpty(LogbookModel::COLUMN_MY_COUNTRY, Data::removeAccents(defaultStationProfile->country));
};
auto setMyEntity = [&](const DxccEntity &myEntity)
{
// force overwrite
record.setValue(RECORDIDX(LogbookModel::COLUMN_MY_DXCC), myEntity.dxcc);
record.setValue(RECORDIDX(LogbookModel::COLUMN_MY_COUNTRY), Data::removeAccents(myEntity.country));
record.setValue(RECORDIDX(LogbookModel::COLUMN_MY_COUNTRY_INTL), myEntity.country);
// other DXCC related values are not closely related to DXCC value and could have been filled
// therefore check if it is present or not.
setIfEmpty(LogbookModel::COLUMN_MY_ITU_ZONE, QString::number(myEntity.ituz));
setIfEmpty(LogbookModel::COLUMN_MY_CQ_ZONE, QString::number(myEntity.cqz));
};
auto lookupAndSetMyEntityByCallsign = [&] (const QString& recordMyDXCC)
{
const DxccEntity &myEntity = Data::instance()->lookupDxcc(recordMyDXCC);
if ( myEntity.dxcc == 0 ) // My DXCC not found
{
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Cannot find My DXCC Entity Info"));
}
else
{
setMyEntity(myEntity);
}
};
if ( !insertQuery.prepare( QSqlDatabase::database().driver()->sqlStatement(QSqlDriver::InsertStatement,
"contacts",
record,
true)) )
{
qWarning() << "cannot prepare Insert statement" << insertQuery.lastError();
return 0;
}
while (true)
{
record.clearValues();
if (!this->importNext(record)) break;
processedRec++;
/* Compute the Band if missing
* Band is one of the mandatory fields
*/
if ( record.value(RECORDIDX(LogbookModel::COLUMN_BAND)).toString().isEmpty()
&& !record.value(RECORDIDX(LogbookModel::COLUMN_FREQUENCY)).toString().isEmpty() )
{
double freq = record.value(RECORDIDX(LogbookModel::COLUMN_FREQUENCY)).toDouble();
record.setValue(RECORDIDX(LogbookModel::COLUMN_BAND), BandPlan::freq2Band(freq).name);
}
// needed later
const QVariant &call = record.value(RECORDIDX(LogbookModel::COLUMN_CALL));
const QVariant &mycall = record.value(RECORDIDX(LogbookModel::COLUMN_STATION_CALLSIGN));
const QVariant &band = record.value(RECORDIDX(LogbookModel::COLUMN_BAND));
const QVariant &mode = record.value(RECORDIDX(LogbookModel::COLUMN_MODE));
const QDateTime &start_time = record.value(RECORDIDX(LogbookModel::COLUMN_TIME_ON)).toDateTime();
const QVariant &sota = record.value(RECORDIDX(LogbookModel::COLUMN_SOTA_REF));
const QVariant &mysota = record.value(RECORDIDX(LogbookModel::COLUMN_MY_SOTA_REF));
/* checking matching fields if they are not empty */
if ( !start_time.isValid()
|| call.toString().isEmpty()
|| band.toString().isEmpty()
|| mode.toString().isEmpty())
{
writeImportLog(importLogStream,
ERROR_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("A minimal set of fields not present (start_time, call, band, mode, station_callsign)"));
qWarning() << "Import does not contain minimal set of fields (start_time, call, band, mode, station_callsign)";
qCDebug(runtime) << record;
continue;
}
if ( processedRec % 1000 == 0)
{
emit importPosition(stream.pos());
}
if ( isDateRange() )
{
if (!inDateRange(start_time.date()))
{
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Outside the selected Date Range"));
continue;
}
}
if ( dupSetting != ACCEPT_ALL )
{
dupQuery.bindValue(":callsign", call);
dupQuery.bindValue(":mode", mode);
dupQuery.bindValue(":band", band);
dupQuery.bindValue(":startdate", start_time.toTimeSpec(Qt::UTC).toString("yyyy-MM-dd hh:mm:ss"));
if ( !dupQuery.exec() )
{
qWarning() << "Cannot exect DUP statement";
}
if ( dupQuery.next() )
{
if ( dupSetting == SKIP_ALL)
{
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Duplicate"));
continue;
}
/* Duplicate QSO found */
if ( duplicateQSOFunc )
{
QSqlRecord dupRecord;
dupRecord= dupQuery.record();
dupSetting = duplicateQSOFunc(&record, &dupRecord);
}
switch ( dupSetting )
{
case ACCEPT_ALL:
case ACCEPT_ONE:
case ASK_NEXT:
break;
case SKIP_ONE:
case SKIP_ALL:
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Duplicate"));
continue;
break;
}
}
}
/* Adding information which are important for QLog or QLog knows/compute them */
/************************/
/* Add DXCC Entity Info */
/************************/
int recordDXCCId = record.value(RECORDIDX(LogbookModel::COLUMN_DXCC)).toInt(); // 0 = NAN or not present
// otherwise = DXCC ID
if ( recordDXCCId != 0 || updateDxcc )
{
const DxccEntity &entity = ( updateDxcc ) ? Data::instance()->lookupDxcc(call.toString())
: Data::instance()->lookupDxccID(recordDXCCId);
if ( entity.dxcc == 0 ) // DXCC not found
{
writeImportLog(importLogStream,
(updateDxcc) ? ERROR_SEVERITY : WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Cannot find DXCC Entity Info"));
if ( updateDxcc )
continue;
}
else
{
// force overwrite
record.setValue(RECORDIDX(LogbookModel::COLUMN_DXCC), entity.dxcc);
record.setValue(RECORDIDX(LogbookModel::COLUMN_COUNTRY), Data::removeAccents(entity.country));
record.setValue(RECORDIDX(LogbookModel::COLUMN_COUNTRY_INTL), entity.country);
// other DXCC related values are not closely related to DXCC value and could have been filled
// therefore check if it is present or not.
setIfEmpty(LogbookModel::COLUMN_CONTINENT, entity.cont);
setIfEmpty(LogbookModel::COLUMN_ITUZ, QString::number(entity.ituz));
setIfEmpty(LogbookModel::COLUMN_CQZ, QString::number(entity.cqz));
}
}
else
{
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("DXCC Info is missing"));
}
/************************/
/* Add My Station Info */
/************************/
int recordMyDXCCId = record.value(RECORDIDX(LogbookModel::COLUMN_MY_DXCC)).toInt(); // 0 = NAN or not present
// otherwise = DXCC ID
const QString &myCallString = mycall.toString();
if ( defaultStationProfile )
{
// default is enabled
// Case 1: Both recordMyDXCCId and myCallString are empty
if ( recordMyDXCCId == 0 && myCallString.isEmpty() )
{
setMyDefaultProfile();
}
// Case 2: recordMyDXCCId is empty, myCallString is not
else if ( recordMyDXCCId == 0 )
{
if ( defaultStationProfile->callsign == myCallString )
setMyDefaultProfile();
else
lookupAndSetMyEntityByCallsign(myCallString);
}
// Case 3: myCallString is empty, recordMyDXCCId is not
else if ( myCallString.isEmpty() )
{
if ( defaultStationProfile->dxcc == recordMyDXCCId )
setMyDefaultProfile();
else
{
// no Station Callsign = ERROR
writeImportLog(importLogStream,
ERROR_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("no Station Callsign present"));
continue;
}
}
// Case 4: Both recordMyDXCCId and myCallString are not empty
else
{
if ( defaultStationProfile->callsign == myCallString )
{
if ( defaultStationProfile->dxcc == recordMyDXCCId )
setMyDefaultProfile();
else
{
// no Station Callsign = ERROR
writeImportLog(importLogStream,
ERROR_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("no Station Callsign present"));
continue;
}
}
else
lookupAndSetMyEntityByCallsign(myCallString);
}
}
else
{
// default is disabled
if ( myCallString.isEmpty() )
{
// no Station Callsign = ERROR
writeImportLog(importLogStream,
ERROR_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("no Station Callsign present"));
continue;
}
else
{
const DxccEntity &myEntity = ( recordMyDXCCId != 0 ) ? Data::instance()->lookupDxccID(recordDXCCId)
: Data::instance()->lookupDxcc(myCallString);
if ( myEntity.dxcc == 0 ) // My DXCC not found
{
writeImportLog(importLogStream,
WARNING_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Cannot find My DXCC Entity Info"));
}
else
setMyEntity(myEntity);
}
}
/***********/
/* Add PFX */
/***********/
if ( record.value(RECORDIDX(LogbookModel::COLUMN_PREFIX)).toString().isEmpty() )
{
const QString &pfxRef = Callsign(call.toString()).getWPXPrefix();
if ( !pfxRef.isEmpty() )
{
record.setValue(RECORDIDX(LogbookModel::COLUMN_PREFIX), pfxRef);
}
}
/********************/
/* Compute Distance */
/********************/
const QString &gridsquare = record.value(RECORDIDX(LogbookModel::COLUMN_GRID)).toString();
const QString &my_gridsquare = record.value(RECORDIDX(LogbookModel::COLUMN_MY_GRIDSQUARE)).toString();
if ( !gridsquare.isEmpty()
&& !my_gridsquare.isEmpty()
&& record.value(RECORDIDX(LogbookModel::COLUMN_DISTANCE)).toString().isEmpty() )
{
const Gridsquare grid(gridsquare);
const Gridsquare my_grid(my_gridsquare);
double distance;
if ( my_grid.distanceTo(grid, distance) )
{
record.setValue(RECORDIDX(LogbookModel::COLUMN_DISTANCE), distance);
}
}
/*************************/
/* Compute Alt from SOTA */
/*************************/
if ( record.value(RECORDIDX(LogbookModel::COLUMN_ALTITUDE)).toString().isEmpty()
&& !sota.toString().isEmpty() )
{
const SOTAEntity &sotaInfo = Data::instance()->lookupSOTA(sota.toString());
if ( sotaInfo.summitCode.compare(sota.toString(), Qt::CaseInsensitive)
&& !sotaInfo.summitName.isEmpty() )
{
record.setValue(RECORDIDX(LogbookModel::COLUMN_ALTITUDE),sotaInfo.altm);
}
}
/*******************************/
/* Compute My Alt from My SOTA */
/*******************************/
if ( record.value(RECORDIDX(LogbookModel::COLUMN_MY_ALTITUDE)).toString().isEmpty()
&& !mysota.toString().isEmpty() )
{
const SOTAEntity &sotaInfo = Data::instance()->lookupSOTA(mysota.toString());
if ( sotaInfo.summitCode.compare(sota.toString(), Qt::CaseInsensitive)
&& !sotaInfo.summitName.isEmpty() )
{
record.setValue(RECORDIDX(LogbookModel::COLUMN_MY_ALTITUDE),sotaInfo.altm);
}
}
/******************/
/* PREPARE INSERT */
/******************/
// Bind all values
for ( int i = 0; i < record.count(); i++ )
{
insertQuery.bindValue(i, record.value(i));
}
if ( ! insertQuery.exec() )
{
writeImportLog(importLogStream,
ERROR_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Cannot insert to database") + " - " + insertQuery.lastError().text());
qWarning() << "Cannot insert a record to Contact Table - " << insertQuery.lastError();
qCDebug(runtime) << record;
}
else
{
writeImportLog(importLogStream,
INFO_SEVERITY,
errors,
warnings,
processedRec,
record,
tr("Imported"));
count++;
}
}
emit importPosition(stream.pos());
emit finished(count);
QSqlDatabase::database().commit();
this->importEnd();
return count;
}
#undef RECORDIDX
void LogFormat::runQSLImport(QSLFrom fromService)
{
FCT_IDENTIFICATION;
QSLMergeStat stats = {QStringList(), QStringList(), 0, 0, 0, 0};
this->importStart();
QSqlTableModel model;
model.setTable("contacts");
QSqlRecord QSLRecord = model.record(0);
while ( true )
{
QSLRecord.clearValues();
if ( !this->importNext(QSLRecord) ) break;
stats.qsos_checked++;
if ( stats.qsos_checked % 100 == 0 )
{
emit importPosition(stream.pos());
}
// needed later
const QVariant &call = QSLRecord.value("callsign");
const QVariant &band = QSLRecord.value("band");
const QVariant &mode = QSLRecord.value("mode");
const QVariant &start_time = QSLRecord.value("start_time");
/* checking matching fields if they are not empty */
if ( !start_time.toDateTime().isValid()
|| call.toString().isEmpty()
|| band.toString().isEmpty()
|| mode.toString().isEmpty() )
{
qWarning() << "Import does not contain field start_time or callsign or band or mode ";
qCDebug(runtime) << QSLRecord;
stats.qsos_errors++;
continue;
}
// It is important to use callsign index here
QString matchFilter = QString("callsign=upper('%1') AND upper(mode)=upper('%2') AND upper(band)=upper('%3') AND ABS(JULIANDAY(start_time)-JULIANDAY(datetime('%4')))*24*60<30")
.arg(call.toString(),
mode.toString(),
band.toString(),
start_time.toDateTime().toTimeSpec(Qt::UTC).toString("yyyy-MM-dd hh:mm:ss"));
/* set filter */
model.setFilter(matchFilter);
model.select();
if ( model.rowCount() != 1 )
{
stats.qsos_unmatched++;
stats.unmatchedQSLs.append(call.toString());
continue;
}
/* we have one row for updating */
/* lets update it */
QSqlRecord originalRecord = model.record(0);
switch ( fromService )
{
case LOTW:
{
/* https://lotw.arrl.org/lotw-help/developer-query-qsos-qsls/?lang=en */
if ( !QSLRecord.value("lotw_qsl_rcvd").toString().isEmpty() )
{
if ( QSLRecord.value("qsl_rcvd") != originalRecord.value("lotw_qsl_rcvd")
&& QSLRecord.value("qsl_rcvd").toString() == 'Y' )
{
originalRecord.setValue("lotw_qsl_rcvd", QSLRecord.value("qsl_rcvd"));
originalRecord.setValue("lotw_qslrdate", QSLRecord.value("qsl_rdate"));
Gridsquare dxNewGrid(QSLRecord.value("gridsquare").toString());
if ( dxNewGrid.isValid()
&& ( originalRecord.value("gridsquare").toString().isEmpty()
||
dxNewGrid.getGrid().contains(originalRecord.value("gridsquare").toString()))
)
{
Gridsquare myGrid(originalRecord.value("my_gridsquare").toString());
originalRecord.setValue("gridsquare", dxNewGrid.getGrid());
double distance;
if ( myGrid.distanceTo(dxNewGrid, distance) )
{
originalRecord.setValue("distance", QVariant(distance));
}
}
if ( !QSLRecord.value("credit_granted").toString().isEmpty() )
{
originalRecord.setValue("credit_granted", QSLRecord.value("credit_granted"));
}
if ( !QSLRecord.value("credit_submitted").toString().isEmpty() )
{
originalRecord.setValue("credit_submitted", QSLRecord.value("credit_submitted"));
}
if ( !QSLRecord.value("pfx").toString().isEmpty() )
{
originalRecord.setValue("pfx", QSLRecord.value("pfx"));
}
if ( !QSLRecord.value("iota").toString().isEmpty() )
{
originalRecord.setValue("iota", QSLRecord.value("iota"));
}
if ( !QSLRecord.value("vucc_grids").toString().isEmpty() )
{
originalRecord.setValue("vucc_grids", QSLRecord.value("vucc_grids"));
}
if ( !QSLRecord.value("state").toString().isEmpty() )
{
originalRecord.setValue("state", QSLRecord.value("state"));
}
if ( !QSLRecord.value("cnty").toString().isEmpty() )
{
originalRecord.setValue("cnty", QSLRecord.value("cnty"));
}
originalRecord.setValue("qsl_rcvd_via", "E");
if ( !model.setRecord(0, originalRecord) )
{
qWarning() << "Cannot update a Contact record - " << model.lastError();
qCDebug(runtime) << originalRecord;
}
if ( !model.submitAll() )
{
qWarning() << "Cannot commit changes to Contact Table - " << model.lastError();
}
stats.qsos_updated++;
stats.newQSLs.append(call.toString());
}
}
else
{
qCInfo(runtime) << "Malformed Lotw Record " << QSLRecord;
}
break;
}
case EQSL:
{
/* http://www.eqsl.cc/qslcard/DownloadInBox.txt */
/* CALL
QSO_DATE
TIME_ON
BAND
MODE
SUBMODE (tag only present if non-blank)
PROP_MODE (tag only present if non-blank)
RST_SENT (will be the sender's RST Sent, not yours)
RST_RCVD (we do not capture this in uploads, so will normally be 0 length)
QSL_SENT (always Y)
QSL_SENT_VIA (always E)
QSLMSG (if non-null and containing only valid printable ASCII characters)
QSLMSG_INTL (if non-null and containing international characters - see ADIF V3 specs)
APP_EQSL_SWL (tag only present if sender is SWL and then always Y)
APP_EQSL_AG (tag only present if sender has Authenticity Guaranteed status and then always Y)
GRIDSQUARE (tag only present if non-blank and at least 4 long)
*/
if ( originalRecord.value("eqsl_qsl_rcvd").toString() != 'Y' )
{
originalRecord.setValue("eqsl_qsl_rcvd", QSLRecord.value("qsl_sent"));
originalRecord.setValue("eqsl_qslrdate", QDateTime::currentDateTimeUtc().date().toString("yyyy-MM-dd"));
Gridsquare dxNewGrid(QSLRecord.value("gridsquare").toString());
if ( dxNewGrid.isValid()
&& ( originalRecord.value("gridsquare").toString().isEmpty()
||
dxNewGrid.getGrid().contains(originalRecord.value("gridsquare").toString()))
)
{
Gridsquare myGrid(originalRecord.value("my_gridsquare").toString());
originalRecord.setValue("gridsquare", dxNewGrid.getGrid());
double distance;
if ( myGrid.distanceTo(dxNewGrid, distance) )
{
originalRecord.setValue("distance", QVariant(distance));
}
}
originalRecord.setValue("qslmsg", QSLRecord.value("qslmsg"));
originalRecord.setValue("qslmsg_int", QSLRecord.value("qslmsg_int"));
originalRecord.setValue("qsl_rcvd_via", QSLRecord.value("qsl_sent_via"));
if ( !model.setRecord(0, originalRecord) )
{
qWarning() << "Cannot update a Contact record - " << model.lastError();
qCDebug(runtime) << originalRecord;
}
if ( !model.submitAll() )
{
qWarning() << "Cannot commit changes to Contact Table - " << model.lastError();
}
stats.qsos_updated++;
stats.newQSLs.append(call.toString());
}
break;
}
default:
qCDebug(runtime) << "Uknown QSL import";
}
}
emit importPosition(stream.pos());
this->importEnd();
emit QSLMergeFinished(stats);
}
long LogFormat::runExport()
{
FCT_IDENTIFICATION;
this->exportStart();
QSqlQuery query;
QString queryStmt = QString("SELECT %1 FROM contacts WHERE %2 ORDER BY start_time ASC").arg(exportedFields.join(", "), getWhereClause());
qCDebug(runtime) << queryStmt;
if ( ! query.prepare(queryStmt) )
{
qWarning() << "Cannot prepare select statement";
return 0;
}
bindWhereClause(query);
if ( ! query.exec() )
{
qWarning() << "Cannot execute select statement" << query.lastError();
return 0;
}
long count = 0L;
/* following 3 lines are a workaround - SQLite does not
* return a correct value for QSqlQuery.size
*/