summaryrefslogtreecommitdiff
path: root/DisplayManual.cpp
diff options
context:
space:
mode:
authorhibby <d@vehibberd.com>2018-02-16 15:17:14 +0000
committerhibby <d@vehibberd.com>2018-02-16 15:17:14 +0000
commit37f727ac0746fd85504aa46c1f023c09b3eff33a (patch)
treec8a7ce04545040a8f161ba279528730bd9a3e6ab /DisplayManual.cpp
parentd63dd8d081a6fcd0715cd13f26df59fb831769d2 (diff)
New upstream version 1.8.0+repackupstream/1.8.0+repack
Diffstat (limited to 'DisplayManual.cpp')
-rw-r--r--DisplayManual.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/DisplayManual.cpp b/DisplayManual.cpp
new file mode 100644
index 00000000..155c2200
--- /dev/null
+++ b/DisplayManual.cpp
@@ -0,0 +1,157 @@
+#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)
+ {
+ if (QNetworkAccessManager::Accessible != qnam_->networkAccessible ()) {
+ // try and recover network access for QNAM
+ qnam_->setNetworkAccessible (QNetworkAccessManager::Accessible);
+ }
+
+ // 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 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});
+}
+