From f14878b05ee885f1747feab256011b4da72ae778 Mon Sep 17 00:00:00 2001 From: Erik Prabowo Kamal Date: Fri, 23 Aug 2019 15:36:18 +0800 Subject: Started to use cURL --- kelakon.pro | 11 ++++++++++- main.cxx | 10 ++++++++++ networkworker.cxx | 16 ++++++++++++++++ networkworker.hxx | 17 +++++++++++++++++ rtclient.c | 33 +++++++++++++++++++++++++++++++++ rtclient.h | 15 +++++++++++++++ 6 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 networkworker.cxx create mode 100644 networkworker.hxx create mode 100644 rtclient.c create mode 100644 rtclient.h diff --git a/kelakon.pro b/kelakon.pro index fb0d403..e0e0d89 100644 --- a/kelakon.pro +++ b/kelakon.pro @@ -1,6 +1,15 @@ QT += quickcontrols2 +HEADERS += \ + rtclient.h \ + networkworker.hxx + SOURCES += \ - main.cxx + rtclient.c \ + networkworker.cxx \ + main.cxx RESOURCES += kelakon.qrc + +LIBS += \ + -lcurl diff --git a/main.cxx b/main.cxx index 94f8599..0108135 100644 --- a/main.cxx +++ b/main.cxx @@ -1,11 +1,21 @@ #include #include +#include +#include "networkworker.hxx" int main(int argc, char* argv[]) { QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; + + Kelakon::NetworkWorker worker{}; + QThread thread; + worker.moveToThread(&thread); + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + thread.start(); + return app.exec(); } diff --git a/networkworker.cxx b/networkworker.cxx new file mode 100644 index 0000000..928e4e8 --- /dev/null +++ b/networkworker.cxx @@ -0,0 +1,16 @@ +#include "rtclient.h" +#include "networkworker.hxx" + +namespace Kelakon { + + NetworkWorker::NetworkWorker(QObject* parent) + : QObject{parent} + { + rtclient_init(); + } + + NetworkWorker::~NetworkWorker() + { + rtclient_cleanup(); + } +} diff --git a/networkworker.hxx b/networkworker.hxx new file mode 100644 index 0000000..c08743f --- /dev/null +++ b/networkworker.hxx @@ -0,0 +1,17 @@ +#ifndef NETWORKWORKER_HXX +#define NETWORKWORKER_HXX + +#include + +namespace Kelakon { + + class NetworkWorker : public QObject + { + Q_OBJECT + public: + explicit NetworkWorker(QObject* parent = Q_NULLPTR); + virtual ~NetworkWorker(); + }; +} + +#endif // NETWORKWORKER_HXX diff --git a/rtclient.c b/rtclient.c new file mode 100644 index 0000000..1be0579 --- /dev/null +++ b/rtclient.c @@ -0,0 +1,33 @@ +#ifdef DEBUG +#ifdef ANDROID +#include +#else +#include +#endif // ANDROID +#endif // DEBUG +#include +#include +#include "rtclient.h" + +static CURL *handle = NULL; + +bool rtclient_init() +{ + curl_global_init(CURL_GLOBAL_SSL); + handle = curl_easy_init(); + if (handle) { + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); +#ifdef DEBUG + curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); +#endif + } + + return (bool)handle; +} + +void rtclient_cleanup() +{ + if (handle) + curl_easy_cleanup(handle); + curl_global_cleanup(); +} diff --git a/rtclient.h b/rtclient.h new file mode 100644 index 0000000..53b8a51 --- /dev/null +++ b/rtclient.h @@ -0,0 +1,15 @@ +#ifndef RTCLIENT_H +#define RTCLIENT_H + +#ifdef __cplusplus +extern "C" { +#endif + + bool rtclient_init(); + void rtclient_cleanup(); + +#ifdef __cplusplus +} +#endif + +#endif // RTCLIENT_H -- cgit v1.2.3