forked from foldynl/QLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHRDLog.cpp
316 lines (250 loc) · 8.8 KB
/
HRDLog.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
#include <QSettings>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDomDocument>
#include "HRDLog.h"
#include "debug.h"
#include "core/CredentialStore.h"
#include "logformat/AdiFormat.h"
#include "rig/macros.h"
MODULE_IDENTIFICATION("qlog.core.hrdlog");
#define API_LOG_UPLOAD_URL "https://robot.hrdlog.net/NewEntry.aspx"
#define API_ONAIR_URL "https://robot.hrdlog.net/OnAir.aspx"
// http://www.iw1qlh.net/projects/hrdlog/HRDLognet_4.pdf
HRDLog::HRDLog(QObject *parent)
: QObject(parent),
currentReply(nullptr),
cancelUpload(false)
{
FCT_IDENTIFICATION;
nam = new QNetworkAccessManager(this);
connect(nam, &QNetworkAccessManager::finished,
this, &HRDLog::processReply);
}
HRDLog::~HRDLog()
{
FCT_IDENTIFICATION;
nam->deleteLater();
if ( currentReply )
{
currentReply->abort();
currentReply->deleteLater();
}
}
void HRDLog::abortRequest()
{
FCT_IDENTIFICATION;
cancelUpload = true;
if ( currentReply )
{
currentReply->abort();
//currentReply->deleteLater(); // pointer is deleted later in processReply
currentReply = nullptr;
}
}
const QString HRDLog::getRegisteredCallsign()
{
FCT_IDENTIFICATION;
QSettings settings;
return settings.value(HRDLog::CONFIG_CALLSIGN_KEY).toString().trimmed();
}
const QString HRDLog::getUploadCode()
{
FCT_IDENTIFICATION;
QSettings settings;
return CredentialStore::instance()->getPassword(HRDLog::SECURE_STORAGE_KEY,
getRegisteredCallsign());
}
bool HRDLog::getOnAirEnabled()
{
FCT_IDENTIFICATION;
QSettings settings;
return settings.value(HRDLog::CONFIG_ONAIR_ENABLED_KEY, false).toBool();
}
void HRDLog::saveUploadCode(const QString &newUsername, const QString &newPassword)
{
FCT_IDENTIFICATION;
QSettings settings;
QString oldUsername = getRegisteredCallsign();
if ( oldUsername != newUsername )
{
CredentialStore::instance()->deletePassword(HRDLog::SECURE_STORAGE_KEY,
oldUsername);
}
settings.setValue(HRDLog::CONFIG_CALLSIGN_KEY, newUsername);
CredentialStore::instance()->savePassword(HRDLog::SECURE_STORAGE_KEY,
newUsername,
newPassword);
}
void HRDLog::saveOnAirEnabled(bool state)
{
FCT_IDENTIFICATION;
QSettings settings;
settings.setValue(HRDLog::CONFIG_ONAIR_ENABLED_KEY, state);
}
void HRDLog::uploadAdif(const QByteArray &data,
const QVariant &contactID,
bool update)
{
FCT_IDENTIFICATION;
QUrlQuery params;
params.addQueryItem("Callsign", getRegisteredCallsign().toUtf8().toPercentEncoding());
params.addQueryItem("Code", getUploadCode().toUtf8().toPercentEncoding());
params.addQueryItem("App", "QLog");
params.addQueryItem("ADIFData", data.trimmed().toPercentEncoding());
if ( update )
{
params.addQueryItem("ADIFKey", data.trimmed().toPercentEncoding());
params.addQueryItem("Cmd", "UPDATE");
}
QUrl url(API_LOG_UPLOAD_URL);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
qCDebug(runtime) << url;
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->post(request, params.query(QUrl::FullyEncoded).toUtf8());
currentReply->setProperty("messageType", QVariant("uploadQSO"));
currentReply->setProperty("ADIFData", data);
currentReply->setProperty("contactID", contactID);
}
void HRDLog::uploadContact(QSqlRecord record)
{
FCT_IDENTIFICATION;
QByteArray data;
QTextStream stream(&data, QIODevice::ReadWrite);
AdiFormat adi(stream);
adi.exportContact(record);
stream.flush();
cancelUpload = false;
uploadAdif(data.trimmed(),
record.value("id"),
(record.value("hrdlog_qso_upload_status").toString() == "M"));
}
void HRDLog::uploadContacts(const QList<QSqlRecord> &qsos)
{
FCT_IDENTIFICATION;
//qCDebug(function_parameters) << qsos;
/* always process one requests per class */
if ( qsos.isEmpty() )
{
/* Nothing to do */
emit uploadFinished(false);
return;
}
cancelUpload = false;
queuedContacts4Upload = qsos;
uploadContact(queuedContacts4Upload.first());
queuedContacts4Upload.removeFirst();
}
void HRDLog::sendOnAir(double freq, const QString &mode)
{
FCT_IDENTIFICATION;
qCDebug(function_parameters) << freq << mode;
QUrlQuery params;
params.addQueryItem("Callsign", getRegisteredCallsign().toUtf8().toPercentEncoding());
params.addQueryItem("Code", getUploadCode().toUtf8().toPercentEncoding());
params.addQueryItem("App", "QLog");
params.addQueryItem("Frequency", QString::number(static_cast<unsigned long long>(MHz(freq))));
params.addQueryItem("Mode", mode);
params.addQueryItem("Radio", " ");
QUrl url(API_ONAIR_URL);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
qCDebug(runtime) << url;
if ( currentReply )
{
qCWarning(runtime) << "processing a new request but the previous one hasn't been completed yet !!!";
}
currentReply = nam->post(request, params.query(QUrl::FullyEncoded).toUtf8());
currentReply->setProperty("messageType", QVariant("onAir"));
}
void HRDLog::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) << "HDRLog.com error URL " << reply->request().url().toString();
qCDebug(runtime) << "HDRLog.com error" << reply->errorString();
qCDebug(runtime) << "HTTP Status Code" << replyStatusCode;
if ( reply->error() != QNetworkReply::OperationCanceledError )
{
emit uploadError(reply->errorString());
reply->deleteLater();
}
cancelUpload = true;
return;
}
const QString &messageType = reply->property("messageType").toString();
qCDebug(runtime) << "Received Message Type: " << messageType;
const QByteArray &response = reply->readAll();
qCDebug(runtime) << response;
/*************/
/* uploadQSO */
/*************/
if ( messageType == "uploadQSO" )
{
QDomDocument doc;
if ( !doc.setContent(response) )
{
qWarning() << "Failed to parse XML document from HRDLog";
emit uploadError(tr("Response message malformed"));
cancelUpload = true;
}
else
{
QDomElement root = doc.documentElement();
QDomNodeList errorNodes = root.elementsByTagName("error");
if ( !errorNodes.isEmpty() )
{
QDomElement errorElement = errorNodes.at(0).toElement();
QString errorText = errorElement.text();
qCDebug(runtime) << "XML contains an error element:" << errorText;
if ( errorText.contains("Unable to find QSO") )
{
// Try to resend it without UPDATE Flag
uploadAdif(reply->property("ADIFData").toByteArray(),
reply->property("contactID"));
}
else
{
emit uploadError(errorText);
cancelUpload = true;
}
}
else
{
qCDebug(runtime) << "Confirmed Upload for QSO Id " << reply->property("contactID").toInt();
emit uploadedQSO(reply->property("contactID").toInt());
if ( queuedContacts4Upload.isEmpty() )
{
cancelUpload = false;
emit uploadFinished(true);
}
else if ( ! cancelUpload )
{
uploadContact(queuedContacts4Upload.first());
queuedContacts4Upload.removeFirst();
}
}
}
}
else if ( messageType == "onAir" )
{
// Do no handle onAir response - error handling is unclear from spec
}
reply->deleteLater();
}
const QString HRDLog::SECURE_STORAGE_KEY = "HRDLog";
const QString HRDLog::CONFIG_CALLSIGN_KEY = "hrdlog/callsign";
const QString HRDLog::CONFIG_ONAIR_ENABLED_KEY = "hrdlog/onair";