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,12 +90,17 @@ namespace DevTools
void Initialize() void Initialize()
{ {
QString chromePath = settings.value(CHROME_LOCATION).toString(); QString chromePath = settings.value(CHROME_LOCATION).toString();
wchar_t programFiles[MAX_PATH + 100] = {}; if (chromePath.isEmpty())
if (chromePath.isEmpty()) for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
{ {
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles); for (auto [_, process] : GetAllProcesses())
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe"); if (process && process->find(L"\\chrome.exe") != std::string::npos) chromePath = S(process.value());
if (std::filesystem::exists(programFiles)) chromePath = S(programFiles); wchar_t programFiles[MAX_PATH + 100] = {};
for (auto folder : { CSIDL_PROGRAM_FILESX86, CSIDL_PROGRAM_FILES, CSIDL_LOCAL_APPDATA })
{
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, programFiles);
wcscat_s(programFiles, L"/Google/Chrome/Application/chrome.exe");
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());