diff --git a/CMakeLists.txt b/CMakeLists.txt index 3222876ff..22eb08937 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,6 +274,7 @@ set(SUBSURFACE_CORE_LIB_SRCS configuredivecomputer.cpp configuredivecomputerthreads.cpp divesitehelpers.cpp + checkcloudconnection.cpp windowtitleupdate.cpp ${SUBSURFACE_PRINTING_SRCS} ${PLATFORM_SRC} diff --git a/checkcloudconnection.cpp b/checkcloudconnection.cpp new file mode 100644 index 000000000..32adc0383 --- /dev/null +++ b/checkcloudconnection.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +#include "pref.h" +#include "helpers.h" + +#include "checkcloudconnection.h" + + +CheckCloudConnection::CheckCloudConnection(QObject *parent) : QObject(parent) +{ + +} + +#define TEAPOT "/make-latte?number-of-shots=3" +#define HTTP_I_AM_A_TEAPOT 418 +#define MILK "Linus does not like non-fat milk" +bool CheckCloudConnection::checkServer() +{ + QTimer timer; + timer.setSingleShot(true); + QEventLoop loop; + QNetworkRequest request; + request.setRawHeader("Accept", "text/plain"); + request.setRawHeader("User-Agent", getUserAgent().toUtf8()); + request.setUrl(QString(prefs.cloud_base_url) + TEAPOT); + QNetworkAccessManager *mgr = new QNetworkAccessManager(); + QNetworkReply *reply = mgr->get(request); + connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + timer.start(2000); // wait two seconds + loop.exec(); + if (timer.isActive()) { + // didn't time out, did we get the right response? + timer.stop(); + if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == HTTP_I_AM_A_TEAPOT && + reply->readAll() == QByteArray(MILK)) { + reply->deleteLater(); + mgr->deleteLater(); + return true; + } + // qDebug() << "did not get expected response - server unreachable" << + // reply->error() << reply->errorString() << + // reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << + // reply->readAll(); + } else { + disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + reply->abort(); + } + reply->deleteLater(); + mgr->deleteLater(); + return false; +} + +// helper to be used from C code +extern "C" bool canReachCloudServer() +{ + return CheckCloudConnection::checkServer(); +} diff --git a/checkcloudconnection.h b/checkcloudconnection.h new file mode 100644 index 000000000..6c85203ac --- /dev/null +++ b/checkcloudconnection.h @@ -0,0 +1,15 @@ +#ifndef CHECKCLOUDCONNECTION_H +#define CHECKCLOUDCONNECTION_H + +#include + +#include "checkcloudconnection.h" + +class CheckCloudConnection : public QObject { + Q_OBJECT +public: + CheckCloudConnection(QObject *parent = 0); + static bool checkServer(); +}; + +#endif // CHECKCLOUDCONNECTION_H diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 665bcf43a..969025d9a 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -1007,48 +1007,3 @@ void CloudStorageAuthenticate::sslErrors(QList errorList) qDebug() << err.errorString(); } } - -CheckCloudConnection::CheckCloudConnection(QObject *parent) -{ - -} - -#define TEAPOT "/make-latte?number-of-shots=3" -#define HTTP_I_AM_A_TEAPOT 418 -#define MILK "Linus does not like non-fat milk" -bool CheckCloudConnection::checkServer() -{ - QTimer timer; - timer.setSingleShot(true); - QEventLoop loop; - QNetworkRequest request; - request.setRawHeader("Accept", "text/plain"); - request.setRawHeader("User-Agent", getUserAgent().toUtf8()); - request.setUrl(QString(prefs.cloud_base_url) + TEAPOT); - QNetworkAccessManager *mgr = new QNetworkAccessManager(); - QNetworkReply *reply = mgr->get(request); - connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); - connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - timer.start(2000); // wait two seconds - loop.exec(); - if (timer.isActive()) { - // didn't time out, did we get the right response? - timer.stop(); - if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == HTTP_I_AM_A_TEAPOT && - reply->readAll() == QByteArray(MILK)) { - reply->deleteLater(); - mgr->deleteLater(); - return true; - } - // qDebug() << "did not get expected response - server unreachable" << - // reply->error() << reply->errorString() << - // reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() << - // reply->readAll(); - } else { - disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - reply->abort(); - } - reply->deleteLater(); - mgr->deleteLater(); - return false; -} diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index 43d1a8add..e4866c529 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -129,13 +129,6 @@ private: }; -class CheckCloudConnection : public QObject { - Q_OBJECT -public: - explicit CheckCloudConnection(QObject *parent = 0); - static bool checkServer(); -}; - #ifdef __cplusplus extern "C" { #endif diff --git a/qthelper.cpp b/qthelper.cpp index 350692cdb..20e4e2c1a 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -33,7 +33,6 @@ #include #include #include "divepicturewidget.h" -#include "subsurfacewebservices.h" #include "mainwindow.h" #include @@ -1068,11 +1067,6 @@ extern "C" bool getProxyString(char **buffer) return false; } -extern "C" bool canReachCloudServer() -{ - return CheckCloudConnection::checkServer(); -} - extern "C" void subsurface_mkdir(const char *dir) { QDir directory;