reorganize

This commit is contained in:
Akash Mozumdar 2018-11-03 23:26:27 -04:00
parent 0160578c2d
commit 83e8c2ecde
5 changed files with 32 additions and 17 deletions

View File

@ -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)

View File

@ -1,5 +1,8 @@
#include "extension.h"
#include "../extension.h"
#include <winhttp.h>
#include <vector>
#include <mutex>
#include <algorithm>
#include <regex>
#include <QInputDialog>
#include <QTimer>
@ -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<DWORD> 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;

View File

@ -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));

View File

@ -1,4 +1,4 @@
#include "extension.h"
#include "../extension.h"
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{

View File

@ -1,4 +1,4 @@
#include "extension.h"
#include "../extension.h"
#include <set>
#include <mutex>