Ver Fonte

UI: Add RemoteTextThread class

The RemoteTextThread class is a QThread that is used to get text
remotely in a separate thread with libcurl.  This is intended to replace
the Qt5Network classes because of their dependency on OpenSSL, which we
can't distribute.
jp9000 há 10 anos atrás
pai
commit
72e3ec7b4c
3 ficheiros alterados com 137 adições e 0 exclusões
  1. 7 0
      obs/CMakeLists.txt
  2. 88 0
      obs/remote-text.cpp
  3. 42 0
      obs/remote-text.hpp

+ 7 - 0
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)

+ 88 - 0
obs/remote-text.cpp

@@ -0,0 +1,88 @@
+/******************************************************************************
+    Copyright (C) 2015 by Hugh Bailey <[email protected]>
+
+    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 <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#include <curl/curl.h>
+#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, decltype(curl_deleter)>;
+
+	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);
+	}
+}

+ 42 - 0
obs/remote-text.hpp

@@ -0,0 +1,42 @@
+/******************************************************************************
+    Copyright (C) 2015 by Hugh Bailey <[email protected]>
+
+    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 <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#pragma once
+
+#include <QThread>
+#include <string>
+
+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_)
+	{}
+};