]> repos.lx-s.de Git - winmute.git/commitdiff
Added WMi18n-class. Added default language table
authorlx-s <lx-s@users.noreply.github.com>
Sun, 8 Oct 2023 09:29:31 +0000 (11:29 +0200)
committerlx-s <lx-s@users.noreply.github.com>
Sun, 8 Oct 2023 09:29:31 +0000 (11:29 +0200)
13 files changed:
Translations/wm_de/resource.h
Translations/wm_de/wm_de.rc
WinMute/Settings_GeneralDlg.cpp
WinMute/WMSettings.cpp
WinMute/WMSettings.h
WinMute/WinMain.cpp
WinMute/WinMute.cpp
WinMute/WinMute.h
WinMute/WinMute.rc
WinMute/WinMute.vcxproj
WinMute/WinMute.vcxproj.filters
WinMute/common.h
WinMute/resource.h

index afeb27ac0973c14a1904ebd0761b635e70b14d1c..93eadb03f38db301a6fcee9ff79bae9945cc912d 100644 (file)
@@ -1,12 +1,14 @@
 //{{NO_DEPENDENCIES}}
 // Microsoft Visual C++ generated include file.
 // Used by wm_de.rc
+//
+#define IDI_LANG_NAME                   101
 
 // Next default values for new objects
 // 
 #ifdef APSTUDIO_INVOKED
 #ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE        101
+#define _APS_NEXT_RESOURCE_VALUE        102
 #define _APS_NEXT_COMMAND_VALUE         40001
 #define _APS_NEXT_CONTROL_VALUE         1001
 #define _APS_NEXT_SYMED_VALUE           101
index 164dec452a6eab8fbdb7c853bc9144c7e6231882..389ad11191902fd8baeccb9d0126c0c57f000249 100644 (file)
Binary files a/Translations/wm_de/wm_de.rc and b/Translations/wm_de/wm_de.rc differ
index 55fbbb182d9e1eeec1f5d23f19e5b14b969fa82e..4b562daa3fcf5b96c463cab33f52815cea975be4 100644 (file)
@@ -33,35 +33,12 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include "common.h"
 
-
-
 namespace fs = std::filesystem;
 
-static std::optional<fs::path> GetTranslationSearchString()
-{
-   wchar_t wmPath[MAX_PATH + 1]{ 0 };
-   if (GetModuleFileNameW(nullptr, wmPath, MAX_PATH) <= 0) {
-      WMLog::GetInstance().WriteWindowsError(L"GetModuleFileNameW", GetLastError());
-      return std::nullopt;
-   }
-   return fs::path{ wmPath } / L"lang" / "*.dll";
-}
-
-static std::vector<std::wstring> GetLanguageList()
-{
-   const auto langPath = GetTranslationSearchString();
-   if (!langPath.has_value()) {
-      return {};
-   }
-   WIN32_FIND_DATAW wfd{ 0 };
-   FindFirstFileExW(
-      langPath->c_str(),
-      FindExInfoBasic,
-      &wfd,
-      FindExSearchNameMatch,
-      nullptr,
-      FIND_FIRST_EX_CASE_SENSITIVE);
-}
+struct LanguageDll {
+   std::wstring langName;
+   std::wstring file;
+};
 
 INT_PTR CALLBACK Settings_GeneralDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
 {
index e616a0b9a55d2e7b47d330cca7afefba5a749ece..aaf4c62e59b41303c43ce707c71afd230bfd2f38 100644 (file)
@@ -119,6 +119,9 @@ static const wchar_t* KeyToStr(SettingsKey key)
    case SettingsKey::NOTIFICATIONS_ENABLED:
       keyStr = L"ShowNotifications";
       break;
+   case SettingsKey::APP_LANGUAGE:
+      keyStr = L"AppLanguage";
+      break;
    }
    return keyStr;
 }
@@ -166,6 +169,8 @@ static DWORD GetDefaultSetting(SettingsKey key)
       return 0;
    case SettingsKey::NOTIFICATIONS_ENABLED:
       return 0;
+   case SettingsKey::APP_LANGUAGE:
+      return 0;
    }
    return 0;
 }
@@ -324,7 +329,6 @@ void WMSettings::Unload() noexcept
    hWifiKey_ = nullptr;
    RegCloseKey(hBluetoothKey_);
    hBluetoothKey_ = nullptr;
-   UnloadLanguage();
 }
 
 HKEY WMSettings::OpenAutostartKey(REGSAM samDesired)
@@ -423,6 +427,17 @@ DWORD WMSettings::QueryValue(SettingsKey key) const
    return value;
 }
 
+std::optional<std::wstring> WMSettings::QueryStrValue(SettingsKey key) const
+{
+   std::wstring val;
+   auto keyStr = KeyToStr(key);
+   assert(keyStr != nullptr);
+   if (!ReadStringFromRegistry(hSettingsKey_, keyStr, val)) {
+      return std::nullopt;
+   }
+   return val;
+}
+
 bool WMSettings::SetValue(SettingsKey key, DWORD value)
 {
    auto keyStr = KeyToStr(key);
@@ -443,6 +458,28 @@ bool WMSettings::SetValue(SettingsKey key, DWORD value)
    return true;
 }
 
+bool WMSettings::SetValue(SettingsKey key, const std::wstring &value)
+{
+   auto keyStr = KeyToStr(key);
+   assert(keyStr != nullptr);
+
+   BYTE *strValue = reinterpret_cast<BYTE *>(const_cast<wchar_t *>(value.c_str()));
+   DWORD strLen = static_cast<DWORD>((value.length() + 1) * sizeof(wchar_t));
+   DWORD regError = RegSetValueExW(
+      hSettingsKey_,
+      keyStr,
+      0,
+      REG_SZ,
+      strValue,
+      strLen);
+   if (regError != ERROR_SUCCESS) {
+      PrintWindowsError(L"RegSetValueExW", regError);
+      return false;
+   }
+
+   return true;
+}
+
 bool WMSettings::StoreWifiNetworks(std::vector<std::wstring>& networks)
 {
    // Clear all stored keys
@@ -730,60 +767,4 @@ std::vector<std::wstring> WMSettings::GetManagedAudioEndpoints() const
    NormalizeStringList(devices);
    return devices;
 }
-// https://learn.microsoft.com/en-us/windows/win32/intl/creating-a-multilingual-user-interface-application
-
-bool WMSettings::SetLanguage(const std::wstring &dllName)
-{
-   if (dllName.empty()) {
-      UnloadLanguage();
-      return true;
-   } else {
-      wchar_t winMutePath[MAX_PATH + 1];
-      GetModuleFileNameW(GetModuleHandleW(nullptr), winMutePath, ARRAY_SIZE(winMutePath));
-      fs::path langPath{winMutePath};
-      langPath /= L"lang";
-      langPath /= dllName;
-      if (!fs::exists(langPath)) {
-         // TODO: Error
-      } else {
-         auto newLangModule = LoadLibraryExW(
-            langPath.c_str(), nullptr,
-            LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_AS_DATAFILE);
-         if (newLangModule == nullptr) {
-            // TODO: Error
-         } else {
-            UnloadLanguage();
-            textModule_ = newLangModule;
-            return true;
-         }
-      }
-   }
-   return false;
-}
-
-void WMSettings::UnloadLanguage()
-{
-   if (textModule_ != nullptr) {
-      FreeLibrary(textModule_);
-      textModule_ = nullptr;
-   }
-}
 
-std::wstring WMSettings::GetString(UINT strId)
-{
-   HMODULE strModule = (textModule_ == nullptr) ? hglobInstance : textModule_;
-   wchar_t strBuffer[2048 + 1]{ 0 };
-   int strLength = LoadStringW(strModule, strId, strBuffer, 0);
-   if (strLength == 0) {
-      return L"";
-   } else if (strLength < 2048) {
-      LoadStringW(strModule, strId, strBuffer, ARRAY_SIZE(strBuffer));
-      return std::wstring(strBuffer, strBuffer + strLength);
-   } else {
-      wchar_t *largeBuffer = new wchar_t[strLength + 1];
-      LoadStringW(strModule, strId, largeBuffer, strLength + 1);
-      std::wstring ret(strBuffer, largeBuffer + strLength);
-      delete[] largeBuffer;
-      return ret;
-   }
-}
index db901fb37c9e4ba769ea339bf40586d8a73c4055..af5b56178986fca83871d01ade6d20e8222d0f6c 100644 (file)
@@ -64,6 +64,7 @@ enum class SettingsKey {
    , QUIETHOURS_END
    , NOTIFICATIONS_ENABLED
    , LOGGING_ENABLED
+   , APP_LANGUAGE
 };
 
 class WMSettings {
@@ -93,8 +94,8 @@ public:
    DWORD QueryValue(SettingsKey key) const;
    bool  SetValue(SettingsKey key, DWORD value);
 
-   bool SetLanguage(const std::wstring &dllName);
-   std::wstring GetString(UINT strId);
+   std::optional<std::wstring> QueryStrValue(SettingsKey key) const;
+   bool SetValue(SettingsKey key, const std::wstring &value);
 
 private:
    HKEY hSettingsKey_;
@@ -102,9 +103,6 @@ private:
    HKEY hBluetoothKey_;
    HKEY hAudioEndpointsKey_;
 
-   HMODULE textModule_;
-
    bool MigrateSettings();
    HKEY OpenAutostartKey(REGSAM samDesired);
-   void UnloadLanguage();
 };
index dd8fb14ccaa6c8843968f680b89f468345e94c2f..6b7b566d90034a0d7d6d260e801ece552946fb34 100644 (file)
@@ -49,16 +49,39 @@ static bool SetWorkingDirectory()
    return false;
 }
 
+static void LoadLanguage(WMSettings &settings, WMi18n &i18n)
+{
+   const auto langModule = settings.QueryStrValue(SettingsKey::APP_LANGUAGE);
+   if (langModule.has_value()) {
+      i18n.LoadLanguage(*langModule);
+   }
+}
+
 int WINAPI wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE,
    _In_ PWSTR,
    _In_ int)
 {
+   WMSettings settings;
+   WMi18n& i18n = WMi18n::GetInstance();
+   if (!settings.Init()) {
+      TaskDialog(nullptr,
+                 nullptr,
+                 PROGRAM_NAME,
+                 L"Failed to initialize settings",
+                 L"Critical error while initializing WinMute",
+                 TDCBF_OK_BUTTON,
+                 TD_ERROR_ICON,
+                 nullptr);
+      return FALSE;
+   }
+
+   LoadLanguage(settings, i18n);
+
    HANDLE hMutex = CreateMutexW(nullptr, TRUE, L"LxSystemsWinMute");
    if (hMutex == nullptr) {
       return FALSE;
-   }
-   if (GetLastError() == ERROR_ALREADY_EXISTS) {
+   } else if (GetLastError() == ERROR_ALREADY_EXISTS) {
       ReleaseMutex(hMutex);
       TaskDialog(nullptr,
          nullptr,
@@ -111,7 +134,7 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance,
    }
 
    MSG msg = { nullptr };
-   WinMute program;
+   WinMute program(settings);
    if (program.Init()) {
       while (GetMessage(&msg, nullptr, 0, 0)) {
          HWND hwnd = GetForegroundWindow();
index 7cad7a99c7e8194cdfcad9ddcba354ad84e10d1b..cda8af364ed9937aa9f881f9608d31cc59a275fd 100644 (file)
@@ -143,11 +143,12 @@ WinMute::MuteConfig::MuteConfig()
 {
 }
 
-WinMute::WinMute() :
+WinMute::WinMute(WMSettings& settings) :
    hWnd_(nullptr),
    hTrayMenu_(nullptr),
    hAppIcon_(nullptr),
-   hTrayIcon_(nullptr)
+   hTrayIcon_(nullptr),
+   settings_(settings)
 {
 }
 
@@ -235,10 +236,6 @@ bool WinMute::Init()
 
    hAppIcon_ = LoadIconW(hglobInstance, MAKEINTRESOURCE(IDI_APP));
 
-   if (!settings_.Init()) {
-      return false;
-   }
-
 #ifdef _DEBUG
    WMLog::GetInstance().SetEnabled(true);
 #else
index 8aa747b6ddc39952c93e889f6e7d171fdc5030f6..514979f8880364fc1fdc83caae1031cb0dfc9e8a 100644 (file)
@@ -39,7 +39,7 @@ class WinAudio;
 
 class WinMute {
 public:
-   WinMute();
+   explicit WinMute(WMSettings& settings);
    WinMute(const WinMute&) = delete;
    WinMute(WinMute&&) = delete;
    WinMute& operator=(const WinMute &) = delete;
@@ -66,7 +66,7 @@ private:
 
    TrayIcon trayIcon_;
    WifiDetector wifiDetector_;
-   WMSettings settings_;
+   WMSettings& settings_;
    MuteControl muteCtrl_;
    QuietHoursTimer quietHours_;
    BluetoothDetector btDetector_;
index 75a56b9536bf14ab2d50e468d5ce87bdd20564d9..e100965cfff32b91534ab68c997d971a7fafabf1 100644 (file)
@@ -524,6 +524,17 @@ BEGIN
     END
 END
 
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// String Table
+//
+
+STRINGTABLE
+BEGIN
+    IDI_LANG_NAME           "English"
+END
+
 #endif    // German (Germany) resources
 /////////////////////////////////////////////////////////////////////////////
 
index 2af7e7bc15e4d1523153329aaeabe392812951ec..9dfba472fb5ee17a58063d689cbf0171f974a025 100644 (file)
     <ClInclude Include="BluetoothDetector.h" />
     <ClInclude Include="common.h" />
     <ClInclude Include="WiFiDetector.h" />
+    <ClInclude Include="WMi18n.h" />
     <ClInclude Include="WMLog.h" />
     <ClInclude Include="MMNotificationClient.h" />
     <ClInclude Include="MuteControl.h" />
     <ClCompile Include="Settings_MuteDlg.cpp" />
     <ClCompile Include="Settings_WifiDlg.cpp" />
     <ClCompile Include="WifiDetector.cpp" />
+    <ClCompile Include="WMi18n.cpp" />
     <ClCompile Include="WMLog.cpp" />
     <ClCompile Include="MMNotificationClient.cpp" />
     <ClCompile Include="MuteControl.cpp" />
index bc4070f97a08daac927e79a6b92a61100c74a183..c12cc05434727bc381fe3d0e1194d54b7a343846 100644 (file)
     <ClInclude Include="BluetoothDetector.h">
       <Filter>Source Files\Controllers\Bluetooth</Filter>
     </ClInclude>
+    <ClInclude Include="WMi18n.h">
+      <Filter>Source Files\Base\Settings</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="WinMute.rc">
     <ClCompile Include="Settings_ManageEndpointsDlg.cpp">
       <Filter>Source Files\UI\Dialogs\Settings</Filter>
     </ClCompile>
+    <ClCompile Include="WMi18n.cpp">
+      <Filter>Source Files\Base\Settings</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>
\ No newline at end of file
index 58261c14b8b93238c4d5c572ca8b1b75f90e70b8..628df310fadacb7f52230aab5d0579fef31791d0 100644 (file)
@@ -72,6 +72,8 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <unordered_map>
 #include <vector>
 
+namespace fs = std::filesystem;
+
 #include <windows.h>
 #include <windowsx.h>
 #include <commctrl.h>
@@ -95,6 +97,7 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include "resource.h"
 
+#include "WMi18n.h"
 #include "WMSettings.h"
 #include "WMLog.h"
 #include "TrayIcon.h"
@@ -106,7 +109,6 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "WinMute.h"
 #include "VersionHelper.h"
 
-namespace fs = std::filesystem;
 
 // Utility
 void PrintWindowsError(const wchar_t *functionName, DWORD lastError = -1);
index 96e36eb122eaf1a81247fa0da65c764f8da66af8..b6fe63eefde4bdcbd1f9accd707fba5738c4bdb1 100644 (file)
@@ -4,6 +4,7 @@
 //
 #define IDI_APP                         5
 #define IDD_ABOUT                       9
+#define IDI_LANG_NAME                   101
 #define IDI_TRAY_BRIGHT                 104
 #define IDI_TRAY_DARK                   105
 #define IDR_TRAYMENU                    106
@@ -86,7 +87,6 @@
 #define IDC_ENDPOINT_REMOVEALL          1048
 #define IDC_ENDPOINT_NAME               1049
 #define IDC_MUTEDELAY                   1050
-#define IDC_COMBO1                      1051
 #define IDC_LANGUAGE                    1051
 #define ID_TRAYMENU_INFO                40001
 #define ID_TRAYMENU_                    40002
 // 
 #ifdef APSTUDIO_INVOKED
 #ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE        139
+#define _APS_NEXT_RESOURCE_VALUE        140
 #define _APS_NEXT_COMMAND_VALUE         40037
 #define _APS_NEXT_CONTROL_VALUE         1052
 #define _APS_NEXT_SYMED_VALUE           101