From 493e80e5685c49f5bd838d479e14effee818a773 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Thu, 1 Jul 2021 23:31:36 -0600 Subject: [PATCH] change file name and remove global to fix threading issue --- extensions/translatewrapper.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/extensions/translatewrapper.cpp b/extensions/translatewrapper.cpp index 2406d16..8b95f18 100644 --- a/extensions/translatewrapper.cpp +++ b/extensions/translatewrapper.cpp @@ -2,9 +2,7 @@ #include "extension.h" #include "translatewrapper.h" #include "blockmarkup.h" -#include "network.h" #include -#include #include #include @@ -25,33 +23,35 @@ extern const wchar_t* TOO_MANY_TRANS_REQUESTS; extern const char* TRANSLATION_PROVIDER; extern const char* GET_API_KEY_FROM; extern const QStringList languagesTo, languagesFrom; -extern const std::unordered_map codes; extern bool translateSelectedOnly, rateLimitAll, rateLimitSelected, useCache, useFilter; extern int tokenCount, rateLimitTimespan, maxSentenceSize; std::pair Translate(const std::wstring& text, TranslationParam tlp); -std::string TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache.txt", TRANSLATION_PROVIDER); - QFormLayout* display; Settings settings; namespace { Synchronized tlp; - Synchronized> translationCache; - int savedSize; + Synchronized> translationCache; + int savedSize = 0; + + std::string CacheFile() + { + return FormatString("%s Cache (%S).txt", TRANSLATION_PROVIDER, tlp->translateTo); + } void SaveCache() { std::wstring allTranslations(L"\xfeff"); for (const auto& [sentence, translation] : translationCache.Acquire().contents) allTranslations.append(L"|SENTENCE|").append(sentence).append(L"|TRANSLATION|").append(translation).append(L"|END|\r\n"); - std::ofstream(TRANSLATION_CACHE_FILE, std::ios::binary | std::ios::trunc).write((const char*)allTranslations.c_str(), allTranslations.size() * sizeof(wchar_t)); + std::ofstream(CacheFile(), std::ios::binary | std::ios::trunc).write((const char*)allTranslations.c_str(), allTranslations.size() * sizeof(wchar_t)); savedSize = translationCache->size(); } void LoadCache() { translationCache->clear(); - std::ifstream stream(TRANSLATION_CACHE_FILE, std::ios::binary); + std::ifstream stream(CacheFile(), std::ios::binary); BlockMarkupIterator savedTranslations(stream, Array{ L"|SENTENCE|", L"|TRANSLATION|" }); auto translationCache = ::translationCache.Acquire(); while (auto read = savedTranslations.Next()) @@ -141,9 +141,8 @@ public: private: void SaveTranslateTo(QString language) { - settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language))); if (translationCache->size() > savedSize) SaveCache(); - TRANSLATION_CACHE_FILE = FormatString("%s Translation Cache (%ls).txt", TRANSLATION_PROVIDER, codes.at(S(language))); + settings.setValue(TRANSLATE_TO, S(tlp->translateTo = S(language))); LoadCache(); } void SaveTranslateFrom(QString language)