From 83e8c2ecdeba601dc057db6587c6ca5bde79d31b Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 3 Nov 2018 23:26:27 -0400 Subject: [PATCH] reorganize --- extensions/CMakeLists.txt | 10 +++--- .../{ => bingtranslate}/bingtranslate.cpp | 31 ++++++++++++++----- .../{ => copyclipboard}/copyclipboard.cpp | 4 +-- .../{ => extranewlines}/extranewlines.cpp | 2 +- .../{ => removerepeat}/removerepeat.cpp | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) rename extensions/{ => bingtranslate}/bingtranslate.cpp (79%) rename extensions/{ => copyclipboard}/copyclipboard.cpp (79%) rename extensions/{ => extranewlines}/extranewlines.cpp (86%) rename extensions/{ => removerepeat}/removerepeat.cpp (98%) diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index 08e4295..b9aed40 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -4,9 +4,9 @@ find_qt5(Core Widgets) cmake_policy(SET CMP0037 OLD) -add_library(Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp) -add_library(Copy\ to\ Clipboard SHARED copyclipboard.cpp extensionimpl.cpp) -add_library(Bing\ Translate SHARED bingtranslate.cpp extensionimpl.cpp) -add_library(Extra\ Newlines SHARED extranewlines.cpp extensionimpl.cpp) +add_library(Bing\ Translate SHARED bingtranslate/bingtranslate.cpp extensionimpl.cpp) +add_library(Copy\ to\ Clipboard SHARED copyclipboard/copyclipboard.cpp extensionimpl.cpp) +add_library(Extra\ Newlines SHARED extranewlines/extranewlines.cpp extensionimpl.cpp) +add_library(Remove\ Repetition SHARED removerepeat/removerepeat.cpp extensionimpl.cpp) -target_link_libraries(Bing\ Translate winhttp.lib Qt5::Widgets) +target_link_libraries(Bing\ Translate winhttp Qt5::Widgets) diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate/bingtranslate.cpp similarity index 79% rename from extensions/bingtranslate.cpp rename to extensions/bingtranslate/bingtranslate.cpp index 3a10fe3..a1580c1 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate/bingtranslate.cpp @@ -1,5 +1,8 @@ -#include "extension.h" +#include "../extension.h" #include +#include +#include +#include #include #include #include @@ -81,13 +84,13 @@ std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstr static HINTERNET internet = NULL; if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); - char buffer[10000] = {}; - WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer, 10000, NULL, NULL); + char utf8[10000] = {}; + WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, utf8, 10000, NULL, NULL); text.clear(); - for (int i = 0; buffer[i];) + for (int i = 0; utf8[i];) { wchar_t utf8char[3] = {}; - swprintf_s<3>(utf8char, L"%02X", (int)(unsigned char)buffer[i++]); + swprintf_s<3>(utf8char, L"%02X", (unsigned)utf8[i++]); text += L"%" + std::wstring(utf8char); } @@ -123,10 +126,22 @@ std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstr bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - if (sentenceInfo["hook address"] == -1 || sentenceInfo["current select"] != 1) return false; + if (sentenceInfo["hook address"] == -1) return false; - std::wstring translation; - std::wstring translateFrom; + { + static std::mutex m; + static std::vector requestTimes; + std::lock_guard l(m); + requestTimes.push_back(GetTickCount()); + requestTimes.erase(std::remove_if(requestTimes.begin(), requestTimes.end(), [&](DWORD requestTime) { return GetTickCount() - requestTime > 60 * 1000; }), requestTimes.end()); + if (!sentenceInfo["current select"] && requestTimes.size() > 30) + { + sentence += L"\r\nToo many translation requests: refuse to make more."; + return true; + } + } + + std::wstring translation, translateFrom; Translate(sentence, translateFrom, translateTo); translation = Translate(sentence, translateFrom, translateTo); for (auto& c : translation) if (c == L'\\') c = 0x200b; diff --git a/extensions/copyclipboard.cpp b/extensions/copyclipboard/copyclipboard.cpp similarity index 79% rename from extensions/copyclipboard.cpp rename to extensions/copyclipboard/copyclipboard.cpp index 9b344f8..ee421a3 100644 --- a/extensions/copyclipboard.cpp +++ b/extensions/copyclipboard/copyclipboard.cpp @@ -1,8 +1,8 @@ -#include "extension.h" +#include "../extension.h" bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - if (sentenceInfo["current select"] == 1 && sentenceInfo["hook address"] != -1) + if (sentenceInfo["current select"] && sentenceInfo["hook address"] != -1) { HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t)); memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t)); diff --git a/extensions/extranewlines.cpp b/extensions/extranewlines/extranewlines.cpp similarity index 86% rename from extensions/extranewlines.cpp rename to extensions/extranewlines/extranewlines.cpp index d0fb344..8e0555d 100644 --- a/extensions/extranewlines.cpp +++ b/extensions/extranewlines/extranewlines.cpp @@ -1,4 +1,4 @@ -#include "extension.h" +#include "../extension.h" bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { diff --git a/extensions/removerepeat.cpp b/extensions/removerepeat/removerepeat.cpp similarity index 98% rename from extensions/removerepeat.cpp rename to extensions/removerepeat/removerepeat.cpp index 544649b..0def122 100644 --- a/extensions/removerepeat.cpp +++ b/extensions/removerepeat/removerepeat.cpp @@ -1,4 +1,4 @@ -#include "extension.h" +#include "../extension.h" #include #include