diff --git a/obs/CMakeLists.txt b/obs/CMakeLists.txt index ea9a706e5..596d19a4b 100644 --- a/obs/CMakeLists.txt +++ b/obs/CMakeLists.txt @@ -39,6 +39,10 @@ endif() include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") +find_package(Libcurl REQUIRED) +include_directories(${LIBCURL_INCLUDE_DIRS}) +add_definitions(${LIBCURL_DEFINITIONS}) + if(WIN32) set(obs_PLATFORM_SOURCES platform-windows.cpp @@ -119,6 +123,7 @@ set(obs_SOURCES crash-report.cpp hotkey-edit.cpp source-label.cpp + remote-text.cpp qt-wrappers.cpp) set(obs_HEADERS @@ -157,6 +162,7 @@ set(obs_HEADERS crash-report.hpp hotkey-edit.hpp source-label.hpp + remote-text.hpp qt-wrappers.hpp) set(obs_UI @@ -201,6 +207,7 @@ target_link_libraries(obs libff Qt5::Widgets Qt5::Network + ${LIBCURL_LIBRARIES} ${obs_PLATFORM_LIBRARIES}) define_graphic_modules(obs) diff --git a/obs/remote-text.cpp b/obs/remote-text.cpp new file mode 100644 index 000000000..a7fcc5d4f --- /dev/null +++ b/obs/remote-text.cpp @@ -0,0 +1,88 @@ +/****************************************************************************** + Copyright (C) 2015 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include +#include "obs-app.hpp" +#include "qt-wrappers.hpp" +#include "remote-text.hpp" + +using namespace std; + +static size_t string_write(char *ptr, size_t size, size_t nmemb, string &str) +{ + size_t total = size * nmemb; + if (total) + str.append(ptr, total); + + return total; +} + +void RemoteTextThread::run() +{ + char error[CURL_ERROR_SIZE]; + CURLcode code; + + string versionString("User-Agent: obs-basic "); + versionString += App()->GetVersionString(); + + string contentTypeString; + if (!contentType.empty()) { + contentTypeString += "Content-Type: "; + contentTypeString += contentType; + } + + auto curl_deleter = [] (CURL *curl) {curl_easy_cleanup(curl);}; + using Curl = unique_ptr; + + Curl curl{curl_easy_init(), curl_deleter}; + if (curl) { + struct curl_slist *header = nullptr; + string str; + + header = curl_slist_append(header, + versionString.c_str()); + + if (!contentTypeString.empty()) { + header = curl_slist_append(header, + contentTypeString.c_str()); + } + + curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, + header); + curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, + error); + curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, + string_write); + curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, + &str); + + if (!postData.empty()) { + curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, + postData.c_str()); + } + + code = curl_easy_perform(curl.get()); + if (code != CURLE_OK) { + emit Result(QString(), QT_UTF8(error)); + } else { + emit Result(QT_UTF8(str.c_str()), QString()); + } + + curl_slist_free_all(header); + } +} diff --git a/obs/remote-text.hpp b/obs/remote-text.hpp new file mode 100644 index 000000000..e0e226819 --- /dev/null +++ b/obs/remote-text.hpp @@ -0,0 +1,42 @@ +/****************************************************************************** + Copyright (C) 2015 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include +#include + +class RemoteTextThread : public QThread { + Q_OBJECT + + std::string url; + std::string contentType; + std::string postData; + + void run() override; + +signals: + void Result(const QString &text, const QString &error); + +public: + inline RemoteTextThread( + std::string url_, + std::string contentType_ = std::string(), + std::string postData_ = std::string()) + : url(url_), contentType(contentType_), postData(postData_) + {} +};