forked from jtdx-project/jtdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayManual.cpp
156 lines (139 loc) · 4.53 KB
/
DisplayManual.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
// This source code file was last time modified by Igor UA3DJY on January 25th, 2017
// All changes are shown in the patch file coming together with the full JTDX source code.
#include "DisplayManual.hpp"
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QString>
#include <QDir>
#include <QFileInfo>
#include <QDesktopServices>
#include <QLocale>
#include "revision_utils.hpp"
#include "pimpl_impl.hpp"
namespace
{
class token
: public QObject
{
Q_OBJECT;
public:
token (QUrl const& url, QString const& lang, QString const& name_we, QObject * parent = nullptr)
: QObject {parent}
, url_ {url}
, lang_ {lang}
, name_we_ {name_we}
{
}
QUrl url_;
QString lang_;
QString name_we_;
};
}
class DisplayManual::impl final
: public QObject
{
Q_OBJECT;
public:
impl (QNetworkAccessManager * qnam)
: qnam_ {qnam}
{
connect (qnam_, &QNetworkAccessManager::finished, this, &DisplayManual::impl::reply_finished);
}
void display (QUrl const& url, QString const& name_we)
{
// try and find a localized manual
auto lang = QLocale::system ().name ();
// try for language and country first
// auto file = name_we + '_' + lang + '-' + version () + ".html";
auto file = name_we;
auto target = url.resolved (file);
QNetworkRequest request {target};
request.setRawHeader ("User-Agent", "WSJT-X Manual Checker");
request.setOriginatingObject (new token {url, lang, name_we, this});
auto * reply = qnam_->head (request);
outstanding_requests_ << reply;
}
void reply_finished (QNetworkReply * reply)
{
if (outstanding_requests_.contains (reply))
{
QUrl target;
if (reply->error ())
{
if (auto * tok = qobject_cast<token *> (reply->request ().originatingObject ()))
{
auto pos = tok->lang_.lastIndexOf ('_');
QString file;
if (pos >= 0)
{
tok->lang_.truncate (pos);
file = tok->name_we_ + '_' + tok->lang_ + '-' + version () + ".html";
target = tok->url_.resolved (file);
QNetworkRequest request {target};
request.setRawHeader ("User-Agent", "WSJT-X Manual Checker");
request.setOriginatingObject (tok);
auto * reply = qnam_->head (request);
outstanding_requests_ << reply;
}
else
{
// give up looking and request the default
file = tok->name_we_ + '-' + version () + ".html";
target = tok->url_.resolved (file);
QDesktopServices::openUrl (target);
delete tok;
}
}
}
else
{
// found it
if (auto * tok = qobject_cast<token *> (reply->request ().originatingObject ()))
{
delete tok;
}
QDesktopServices::openUrl (reply->request ().url ());
}
outstanding_requests_.removeOne (reply);
reply->deleteLater ();
}
}
QNetworkAccessManager * qnam_;
QList<QNetworkReply *> outstanding_requests_;
};
#include "DisplayManual.moc"
DisplayManual::DisplayManual (QNetworkAccessManager * qnam, QObject * parent)
: QObject {parent}
, m_ {qnam}
{
}
DisplayManual::~DisplayManual ()
{
}
void DisplayManual::display_html_url (QUrl const& url, QString const& name_we)
{
m_->display (url, name_we);
}
void DisplayManual::display_html_file (QDir const& dir, QString const& name_we)
{
// try and find a localized manual
auto lang = QLocale::system ().name ();
// try for language and country first
auto file = dir.absoluteFilePath (name_we + '_' + lang + '-' + version () + ".html");
if (!QFileInfo::exists (file))
{
// try for language
lang.truncate (lang.lastIndexOf ('_'));
file = dir.absoluteFilePath (name_we + '_' + lang + '-' + version () + ".html");
if (!QFileInfo::exists (file))
{
// use default
file = dir.absoluteFilePath (name_we + '-' + version () + ".html");
}
}
// may fail but browser 404 error is a good as anything
QDesktopServices::openUrl (QUrl {"file:///" + file});
}