improve chrome locating and ignore empty translation

This commit is contained in:
Akash Mozumdar 2022-01-29 14:20:57 -07:00
parent c9240d9e68
commit ef55ef0c61
2 changed files with 12 additions and 6 deletions

View File

@ -1,4 +1,5 @@
#include "devtools.h" #include "devtools.h"
#include "module.h"
#include <ppltasks.h> #include <ppltasks.h>
#include <ShlObj.h> #include <ShlObj.h>
#include <QWebSocket> #include <QWebSocket>
@ -89,13 +90,18 @@ namespace DevTools
void Initialize() void Initialize()
{ {
QString chromePath = settings.value(CHROME_LOCATION).toString(); QString chromePath = settings.value(CHROME_LOCATION).toString();
if (chromePath.isEmpty())
{
for (auto [_, process] : GetAllProcesses())
if (process && process->find(L"\\chrome.exe") != std::string::npos) chromePath = S(process.value());
wchar_t programFiles[MAX_PATH + 100] = {}; wchar_t programFiles[MAX_PATH + 100] = {};
if (chromePath.isEmpty()) for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA }) for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
{ {
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles); SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles);
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe"); wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe");
if (std::filesystem::exists(programFiles)) chromePath = S(programFiles); if (std::filesystem::exists(programFiles)) chromePath = S(programFiles);
} }
}
auto chromePathEdit = new QLineEdit(chromePath); auto chromePathEdit = new QLineEdit(chromePath);
static struct : QObject static struct : QObject
{ {

View File

@ -192,7 +192,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
if (useCache) if (useCache)
{ {
auto translationCache = ::translationCache.Acquire(); auto translationCache = ::translationCache.Acquire();
if (auto it = translationCache->find(sentence); it != translationCache->end()) translation = it->second + L"\x200b"; // dumb hack to not try to translate if stored empty translation if (auto it = translationCache->find(sentence); it != translationCache->end()) translation = it->second;
} }
if (translation.empty() && (!translateSelectedOnly || sentenceInfo["current select"])) if (translation.empty() && (!translateSelectedOnly || sentenceInfo["current select"]))
if (rateLimiter.Request() || !useRateLimiter || (!rateLimitSelected && sentenceInfo["current select"])) std::tie(cache, translation) = Translate(sentence, tlp.Copy()); if (rateLimiter.Request() || !useRateLimiter || (!rateLimitSelected && sentenceInfo["current select"])) std::tie(cache, translation) = Translate(sentence, tlp.Copy());