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
|
#include "LotWUsers.hpp"
#include <exception>
#include <utility>
#include <QHash>
#include <QString>
#include <QDate>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QFileInfo>
#include <QPointer>
#include <QSaveFile>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrentRun>
#include "qt_helpers.hpp"
#include "Logger.hpp"
#include "FileDownload.hpp"
#include "pimpl_impl.hpp"
#include "moc_LotWUsers.cpp"
namespace
{
// Dictionary mapping call sign to date of last upload to LotW
using dictionary = QHash<QString, QDate>;
struct load_result
{
dictionary users;
QString error;
};
// Upload times and any additional fields are not needed for date lookups.
load_result load_dictionary (QString const& lotw_csv_file)
{
load_result result;
try
{
QFile f {lotw_csv_file};
if (!f.open (QFile::ReadOnly | QFile::Text))
{
result.error = QObject::tr ("Failed to open LotW users CSV file: '%1'").arg (f.fileName ());
return result;
}
QTextStream s {&f};
for (auto l = s.readLine (); !l.isNull (); l = s.readLine ())
{
auto const pos = l.indexOf (',');
if (pos <= 0) continue;
auto const date_end = l.indexOf (',', pos + 1);
auto const date_field = date_end < 0 ? l.mid (pos + 1) : l.mid (pos + 1, date_end - pos - 1);
auto const call = l.left (pos).trimmed ();
auto const date = QDate::fromString (date_field.trimmed (), "yyyy-MM-dd");
if (!call.isEmpty () && date.isValid ()) result.users[call] = date;
}
if (f.error () != QFileDevice::NoError)
{
result.error = QObject::tr ("Failed to read LotW users CSV file: '%1'").arg (f.fileName ());
}
}
catch (std::exception const& e)
{
result.error = QString::fromUtf8 (e.what ());
}
return result;
}
}
class LotWUsers::impl final
: public QObject
{
Q_OBJECT
public:
impl (LotWUsers * self, QNetworkAccessManager * network_manager)
: QObject {self}
, self_ {self}
, network_manager_ {network_manager}
, url_valid_ {false}
, redirect_count_ {0}
, age_constraint_ {365}
, connected_ {false}
{
lotw_downloader_.setParent (this);
connect (&loader_watcher_, &QFutureWatcher<load_result>::finished, this, [this] {
loading_ = false;
if (!pending_file_.isEmpty ())
{
if (active_generation_ == load_generation_)
{
auto result = loader_watcher_.result ();
if (result.error.isEmpty ())
{
// Keep the cached snapshot available while the downloaded file is parsed.
last_uploaded_ = std::move (result.users);
}
}
auto const next_file = std::exchange (pending_file_, QString {});
start_load (next_file);
return;
}
if (active_generation_ != load_generation_) return;
auto result = loader_watcher_.result ();
auto const should_finish = !download_active_;
QPointer<LotWUsers> owner {self_};
if (result.error.isEmpty ())
{
last_uploaded_ = std::move (result.users);
LOG_INFO (QString {"LotWUsers: Loaded %1 records from %2"}.arg (last_uploaded_.size ()).arg (active_file_));
Q_EMIT owner->progress (QString {"Loaded %1 records from LotW."}.arg (last_uploaded_.size ()));
}
else
{
Q_EMIT owner->LotW_users_error (result.error);
}
if (owner && should_finish) Q_EMIT owner->load_finished ();
});
}
void load (QString const& url, bool fetch, bool forced_fetch)
{
auto csv_file_name = csv_file_.fileName ();
auto exists = QFileInfo::exists (csv_file_name);
auto const downloading = fetch && (!exists || forced_fetch);
auto const preserve_cache_load = downloading && exists && loading_ && active_file_ == csv_file_name
&& active_generation_ == load_generation_;
if (!preserve_cache_load) ++load_generation_;
pending_file_.clear ();
download_active_ = false;
abort (); // abort any active download
if (downloading)
{
current_url_.setUrl(url);
if (current_url_.isValid() && !QSslSocket::supportsSsl())
{
current_url_.setScheme("http");
}
redirect_count_ = 0;
Q_EMIT self_->progress (QString("Starting download from %1").arg(url));
lotw_downloader_.configure(network_manager_,
url,
csv_file_name,
"WSJT-X LotW User Downloader");
if (!connected_)
{
connect(&lotw_downloader_, &FileDownload::complete, this, [this] (QString const& file_name) {
download_active_ = false;
request_load (file_name);
});
connect(&lotw_downloader_, &FileDownload::download_error, this, [this] (QString const& msg) {
LOG_INFO(QString{"LotWUsers: Error downloading LotW file: %1"}.arg(msg));
Q_EMIT self_->LotW_users_error (msg);
});
connect(&lotw_downloader_, &FileDownload::load_finished, this, [this] {
if (!download_active_) return;
download_active_ = false;
if (!loading_) Q_EMIT self_->load_finished ();
});
connect( &lotw_downloader_, &FileDownload::progress, [this] (QString const& msg) {
Q_EMIT self_->progress (msg);
});
connected_ = true;
}
download_active_ = true;
lotw_downloader_.start_download();
}
else
{
if (exists)
{
request_load (csv_file_name);
}
}
}
void abort ()
{
lotw_downloader_.abort();
}
void request_load (QString const& file_name)
{
if (loading_)
{
pending_file_ = file_name;
}
else
{
start_load (file_name);
}
}
void start_load (QString const& file_name)
{
loading_ = true;
active_generation_ = load_generation_;
active_file_ = file_name;
loader_watcher_.setFuture (QtConcurrent::run ([file_name] { return load_dictionary (file_name); }));
}
LotWUsers * self_;
QNetworkAccessManager * network_manager_;
QSaveFile csv_file_;
bool url_valid_;
QUrl current_url_; // may be a redirect
int redirect_count_;
QPointer<QNetworkReply> reply_;
QFutureWatcher<load_result> loader_watcher_ {this};
bool loading_ {false};
bool download_active_ {false};
quint64 load_generation_ {0};
quint64 active_generation_ {0};
QString active_file_;
QString pending_file_;
dictionary last_uploaded_;
qint64 age_constraint_; // days
FileDownload lotw_downloader_;
bool connected_;
};
#include "LotWUsers.moc"
LotWUsers::LotWUsers (QNetworkAccessManager * network_manager, QObject * parent)
: QObject {parent}
, m_ {this, network_manager}
{
}
LotWUsers::~LotWUsers ()
{
}
void LotWUsers::set_local_file_path (QString const& path)
{
m_->csv_file_.setFileName (path);
}
void LotWUsers::load (QString const& url, bool fetch, bool force_download)
{
m_->load (url, fetch, force_download);
}
void LotWUsers::set_age_constraint (qint64 uploaded_since_days)
{
m_->age_constraint_ = uploaded_since_days;
}
bool LotWUsers::user (QString const& call) const
{
if (m_->last_uploaded_.size ())
{
auto p = m_->last_uploaded_.constFind (call);
if (p != m_->last_uploaded_.end ())
{
auto const age = p.value ().daysTo (QDateTime::currentDateTimeUtc ().date ());
return p.value ().isValid () && age >= 0 && age <= m_->age_constraint_;
}
}
return false;
}
|