forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEqsl.cpp
603 lines (488 loc) · 18.5 KB
/
Eqsl.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
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSettings>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QHttpMultiPart>
#include <QSqlRecord>
#include <QDir>
#include <QTemporaryDir>
#include "Eqsl.h"
#include "core/debug.h"
#include "core/CredentialStore.h"
#include "logformat/AdiFormat.h"
#define DOWNLOAD_1ST_PAGE "https://www.eQSL.cc/qslcard/DownloadInBox.cfm"
#define DOWNLOAD_2ND_PAGE "https://www.eQSL.cc/downloadedfiles/"
#define UPLOAD_ADIF_PAGE "https://www.eQSL.cc/qslcard/ImportADIF.cfm"
#define QSL_IMAGE_FILENAME_PAGE "https://www.eQSL.cc/qslcard/GeteQSL.cfm"
#define QSL_IMAGE_DOWNLOAD_PAGE "https://www.eQSL.cc"
MODULE_IDENTIFICATION("qlog.core.eqsl");
extern QTemporaryDir tempDir;
EQSL::EQSL( QObject *parent ):
QObject(parent),
qslStorage(new QSLStorage(this)),
currentReply(nullptr)
{
FCT_IDENTIFICATION;
nam = new QNetworkAccessManager(this);
connect(nam, &QNetworkAccessManager::finished,
this, &EQSL::processReply);
}
EQSL::~EQSL()
{
FCT_IDENTIFICATION;
if ( currentReply )
{
currentReply->abort();
currentReply->deleteLater();
}
nam->deleteLater();
qslStorage->deleteLater();
}
void EQSL::update(const QDate &start_date, bool qso_since, const QString &qthNick)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << start_date << qso_since << qthNick;
QList<QPair<QString, QString>> params;
if ( !qthNick.isEmpty() )
{
params.append(qMakePair(QString("QTHNickname"), qthNick));
}
if ( start_date.isValid() )
{
if ( qso_since )
{
const QString &start = start_date.toString("MM/dd/yyyy");
const QString &stop = QDate::currentDate().addDays(1).toString("MM/dd/yyyy");
params.append(qMakePair(QString("LimitDateLo"), start));
params.append(qMakePair(QString("LimitDateHi"), stop));
}
else
{
//qsl_since
const QString &start = start_date.toString("yyyyMMdd");
params.append(qMakePair(QString("RcvdSince"), start));
}
}
get(params);
}
void EQSL::uploadAdif(const QByteArray &data)
{
FCT_IDENTIFICATION;
QSettings settings;
QString username = settings.value(EQSL::CONFIG_USERNAME_KEY).toString();
QString password = CredentialStore::instance()->getPassword(EQSL::SECURE_STORAGE_KEY,
username);
/* http://www.eqsl.cc/qslcard/ImportADIF.txt */
//qInfo() << qPrintable(data);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType, this);
/* UserName */
QHttpPart userPart;
userPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"eqsl_user\""));
userPart.setBody(username.toUtf8());
/* Password */
QHttpPart passPart;
passPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"eqsl_pswd\""));
passPart.setBody(password.toUtf8());
/* File */
QTemporaryFile file;
file.open();
file.write(data);
file.flush();
QHttpPart filePart;
QString aux = QString("form-data; name=\"Filename\"; filename=\"%1\"").arg(file.fileName());
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(aux));
filePart.setBody(data);
multiPart->append(userPart);
multiPart->append(passPart);
multiPart->append(filePart);
QNetworkRequest request(QUrl(UPLOAD_ADIF_PAGE));
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->post(request, multiPart);
currentReply->setProperty("messageType", QVariant("uploadADIFFile"));
}
void EQSL::getQSLImage(const QSqlRecord &qso)
{
FCT_IDENTIFICATION;
QString inCacheFilename;
if ( isQSLImageInCache(qso, inCacheFilename) )
{
emit QSLImageFound(inCacheFilename);
return;
}
/* QSL image is not in Cache */
qCDebug(runtime) << "QSL is not in Cache";
const QString &username = getUsername();
const QString &password = getPassword();
const QDateTime &time_start = qso.value("start_time").toDateTime().toTimeZone(QTimeZone::utc());
QUrlQuery query;
query.addQueryItem("UserName", username.toUtf8().toPercentEncoding());
query.addQueryItem("Password", password.toUtf8().toPercentEncoding());
query.addQueryItem("CallsignFrom", qso.value("callsign").toString());
query.addQueryItem("QSOYear", time_start.toString("yyyy"));
query.addQueryItem("QSOMonth", time_start.toString("MM"));
query.addQueryItem("QSODay", time_start.toString("dd"));
query.addQueryItem("QSOHour", time_start.toString("hh"));
query.addQueryItem("QSOMinute", time_start.toString("mm"));
query.addQueryItem("QSOBand", qso.value("band").toString());
query.addQueryItem("QSOMode", qso.value("mode").toString());
QUrl url(QSL_IMAGE_FILENAME_PAGE);
url.setQuery(query);
qCDebug(runtime) << url.toString();
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->get(QNetworkRequest(url));
currentReply->setProperty("messageType", QVariant("getQSLImageFileName"));
currentReply->setProperty("onDiskFilename", QVariant(inCacheFilename));
currentReply->setProperty("QSORecordID", QVariant(qso.value("id")));
}
const QString EQSL::getUsername()
{
FCT_IDENTIFICATION;
QSettings settings;
return settings.value(EQSL::CONFIG_USERNAME_KEY).toString().trimmed();
}
const QString EQSL::getPassword()
{
FCT_IDENTIFICATION;
return CredentialStore::instance()->getPassword(EQSL::SECURE_STORAGE_KEY,
getUsername());
}
void EQSL::saveUsernamePassword(const QString &newUsername, const QString &newPassword)
{
FCT_IDENTIFICATION;
QSettings settings;
const QString &oldUsername = getUsername();
if ( oldUsername != newUsername )
{
CredentialStore::instance()->deletePassword(EQSL::SECURE_STORAGE_KEY,
oldUsername);
}
settings.setValue(EQSL::CONFIG_USERNAME_KEY, newUsername);
CredentialStore::instance()->savePassword(EQSL::SECURE_STORAGE_KEY,
newUsername,
newPassword);
}
void EQSL::get(const QList<QPair<QString, QString>> ¶ms)
{
FCT_IDENTIFICATION;
QSettings settings;
const QString &username = getUsername();
const QString &password = getPassword();
QUrlQuery query;
query.setQueryItems(params);
query.addQueryItem("UserName", username.toUtf8().toPercentEncoding());
query.addQueryItem("Password", password.toUtf8().toPercentEncoding());
QUrl url(DOWNLOAD_1ST_PAGE);
url.setQuery(query);
qCDebug(runtime) << url.toString();
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->get(QNetworkRequest(url));
currentReply->setProperty("messageType", QVariant("getADIFFileName"));
}
void EQSL::downloadADIF(const QString &filename)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << filename;
QUrlQuery query;
QUrl url(DOWNLOAD_2ND_PAGE + filename);
url.setQuery(query);
qCDebug(runtime) << url.toString();
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->get(QNetworkRequest(url));
currentReply->setProperty("messageType", QVariant("getADIF"));
}
void EQSL::downloadImage(const QString &URLFilename,
const QString &onDiskFilename,
const qulonglong qsoid)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << URLFilename << onDiskFilename << qsoid;
QUrlQuery query;
QUrl url(QSL_IMAGE_DOWNLOAD_PAGE + URLFilename);
url.setQuery(query);
qCDebug(runtime) << url.toString();
currentReply = nam->get(QNetworkRequest(url));
currentReply->setProperty("messageType", QVariant("downloadQSLImage"));
currentReply->setProperty("onDiskFilename", QVariant(onDiskFilename));
currentReply->setProperty("QSORecordID", qsoid);
}
QString EQSL::QSLImageFilename(const QSqlRecord &qso)
{
FCT_IDENTIFICATION;
/* QSL Fileformat YYYYMMDD_ID_Call_eqsl.jpg */
const QDateTime &time_start = qso.value("start_time").toDateTime().toTimeZone(QTimeZone::utc());
const QString &ret = QString("%1_%2_%3_eqsl.jpg").arg(time_start.toString("yyyyMMdd"),
qso.value("id").toString(),
qso.value("callsign").toString().replace(QRegularExpression(QString::fromUtf8("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"]")),"_"));
qCDebug(runtime) << "EQSL Image Filename: " << ret;
return ret;
}
bool EQSL::isQSLImageInCache(const QSqlRecord &qso, QString &fullPath)
{
FCT_IDENTIFICATION;
bool isFileExists = false;
const QString &expectingFilename = QSLImageFilename(qso);
const QSLObject &eqsl = qslStorage->getQSL(qso, QSLObject::EQSL, expectingFilename);
QFile f(tempDir.path() + QDir::separator() + eqsl.getQSLName());
qCDebug(runtime) << "Using temp file" << f.fileName();
fullPath = f.fileName();
if ( eqsl.getBLOB() != QByteArray()
&& f.open(QFile::WriteOnly) )
{
f.write(eqsl.getBLOB());
f.flush();
f.close();
isFileExists = true;
}
qCDebug(runtime) << isFileExists << " " << fullPath;
return isFileExists;
}
void EQSL::processReply(QNetworkReply* reply)
{
FCT_IDENTIFICATION;
/* always process one requests per class */
currentReply = nullptr;
int replyStatusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if ( reply->error() != QNetworkReply::NoError
|| replyStatusCode < 200
|| replyStatusCode >= 300)
{
qCDebug(runtime) << "eQSL error URL " << reply->request().url().toString();
qCDebug(runtime) << "eQSL error" << reply->errorString();
qCDebug(runtime) << "HTTP Status Code" << replyStatusCode;
if ( reply->error() != QNetworkReply::OperationCanceledError )
{
emit updateFailed(reply->errorString());
emit QSLImageError(reply->errorString());
emit uploadError(reply->errorString());
reply->deleteLater();
}
return;
}
const QString &messageType = reply->property("messageType").toString();
qCDebug(runtime) << "Received Message Type: " << messageType;
/*******************/
/* getADIFFileName */
/*******************/
if ( messageType == "getADIFFileName" )
{
//getting the first page where a ADIF filename is present
QString replyString(reply->readAll());
qCDebug(runtime) << replyString;
if ( replyString.contains("No such Username/Password found")
|| replyString.contains("No such Callsign found") )
{
qCDebug(runtime) << "Incorrect Password or QTHProfile Id";
emit updateFailed(tr("Incorrect Password or QTHProfile Id"));
}
else
{
static QRegularExpression re("/downloadedfiles/(.*.adi)\">.ADI file");
QRegularExpressionMatch match = re.match(replyString);
if ( match.hasMatch() )
{
const QString &filename = match.captured(1);
downloadADIF(filename);
}
else if ( replyString.contains("You have no log entries"))
{
emit updateComplete(QSLMergeStat());
}
else
{
qCInfo(runtime) << "File not found in HTTP reply ";
emit updateFailed(tr("ADIF file not found in eQSL response"));
}
}
}
/***********************/
/* getQSLImageFileName */
/***********************/
else if ( messageType == "getQSLImageFileName" )
{
//getting the first page where an Image filename is present
QString replyString(reply->readAll());
qCDebug(runtime) << replyString;
if ( replyString.contains("No such Username/Password found") )
{
emit QSLImageError(tr("Incorrect Username or password"));
}
else
{
static QRegularExpression re("<img src=\"(.*)\" alt");
QRegularExpressionMatch match = re.match(replyString);
if ( match.hasMatch() )
{
QString filename = match.captured(1);
QString onDiskFilename = reply->property("onDiskFilename").toString();
qulonglong qsoid = reply->property("QSORecordID").toULongLong();
downloadImage(filename, onDiskFilename, qsoid);
}
else
{
static QRegularExpression rError("Error: (.*)");
QRegularExpressionMatch matchError = rError.match(replyString);
if (matchError.hasMatch() )
{
QString msg = matchError.captured(1);
emit QSLImageError(msg);
}
else
{
static QRegularExpression rWarning("Warning: (.*) --");
QRegularExpressionMatch matchWarning = rWarning.match(replyString);
if ( matchWarning.hasMatch() )
{
QString msg = matchWarning.captured(1);
emit QSLImageError(msg);
}
else
{
qCInfo(runtime) << replyString;
emit QSLImageError(tr("Unknown Error"));
}
}
}
}
}
/***********/
/* getADIF */
/***********/
else if ( messageType == "getADIF")
{
qint64 size = reply->size();
qCDebug(runtime) << "Reply size: " << size;
/* Currently, QT returns an incorrect stream position value in Network stream
* when the stream is used in QTextStream. Therefore
* QLog downloads a response, saves it to a temp file and opens
* the file as a stream */
QTemporaryFile tempFile;
if ( ! tempFile.open() )
{
qCDebug(runtime) << "Cannot open temp file";
emit updateFailed(tr("Cannot opet temporary file"));
reply->deleteLater();
return;
}
const QByteArray &data = reply->readAll();
tempFile.write(data);
tempFile.flush();
tempFile.seek(0);
emit updateStarted();
/* see above why QLog uses a temp file */
QTextStream stream(&tempFile);
AdiFormat adi(stream);
connect(&adi, &AdiFormat::importPosition, this, [this, size](qint64 position)
{
if ( size > 0 )
{
double progress = position * 100.0 / size;
emit updateProgress(static_cast<int>(progress));
}
});
connect(&adi, &AdiFormat::QSLMergeFinished, this, [this](QSLMergeStat stats)
{
emit updateComplete(stats);
});
adi.runQSLImport(adi.EQSL);
tempFile.close();
}
/********************/
/* downloadQSLImage */
/********************/
else if ( messageType == "downloadQSLImage")
{
qint64 size = reply->size();
qCDebug(runtime) << "Reply size: " << size;
const QByteArray &data = reply->readAll();
const QString &onDiskFilename = reply->property("onDiskFilename").toString();
qulonglong qsoID = reply->property("QSORecordID").toULongLong();
QFile file(onDiskFilename);
if ( !file.open(QIODevice::WriteOnly))
{
emit QSLImageError(tr("Cannot save the image to file") + " " + onDiskFilename);
return;
}
file.write(data);
file.flush();
file.close();
if ( !qslStorage->add(QSLObject(qsoID, QSLObject::EQSL,
QFileInfo(onDiskFilename).fileName(), data,
QSLObject::RAWBYTES)) )
{
qWarning() << "Cannot save the eQSL image to database cache";
// ??? database is only a cache for images. not needed to inform operator about this ????
}
emit QSLImageFound(onDiskFilename);
}
/******************/
/* uploadADIFFile */
/******************/
else if ( messageType == "uploadADIFFile" )
{
QString replyString(reply->readAll());
qCDebug(runtime) << replyString;
static QRegularExpression rOK("Result: (.*)");
QRegularExpressionMatch matchOK = rOK.match(replyString);
static QRegularExpression rError("Error: (.*)");
QRegularExpressionMatch matchError = rError.match(replyString);
static QRegularExpression rWarning("Warning: (.*)");
QRegularExpressionMatch matchWarning = rWarning.match(replyString);
static QRegularExpression rCaution("Caution: (.*)");
QRegularExpressionMatch matchCaution = rCaution.match(replyString);
QString msg;
if ( matchOK.hasMatch() )
{
msg = matchOK.captured(1);
emit uploadOK(msg);
}
else if (matchError.hasMatch() )
{
msg = matchError.captured(1);
emit uploadError(msg);
}
else if (matchWarning.hasMatch() )
{
msg = matchWarning.captured(1);
emit uploadOK(msg);
}
else if (matchCaution.hasMatch() )
{
msg = matchCaution.captured(1);
emit uploadError(msg);
}
else
{
qCInfo(runtime) << "Unknown Reply ";
qCInfo(runtime) << replyString;
emit uploadError(tr("Unknown Reply from eQSL"));
}
}
reply->deleteLater();
}
void EQSL::abortRequest()
{
FCT_IDENTIFICATION;
if ( currentReply )
{
currentReply->abort();
//currentReply->deleteLater(); // pointer is deleted later in processReply
currentReply = nullptr;
}
}
const QString EQSL::SECURE_STORAGE_KEY = "eQSL";
const QString EQSL::CONFIG_USERNAME_KEY = "eqsl/username";