From 6595601466e29064559c50ff911978002d8e3a18 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20Steinh=C3=B6fer?= Date: Thu, 14 Apr 2022 08:27:35 +0200 Subject: [PATCH] Detect already connected networks on settings change --- WinMute/AboutDlg.cpp | 7 +---- WinMute/TrayIcon.cpp | 15 +++++++++++ WinMute/TrayIcon.h | 8 ++++++ WinMute/WMSettings.cpp | 6 +---- WinMute/WiFiDetector.h | 4 +++ WinMute/WifiDetector.cpp | 55 ++++++++++++++++++++++++++++++++++++---- WinMute/WinMute.cpp | 36 +++++++++++++------------- WinMute/WinMute.rc | 12 ++++----- 8 files changed, 103 insertions(+), 40 deletions(-) diff --git a/WinMute/AboutDlg.cpp b/WinMute/AboutDlg.cpp index 27b919b..81fab64 100644 --- a/WinMute/AboutDlg.cpp +++ b/WinMute/AboutDlg.cpp @@ -196,12 +196,7 @@ static bool GetWinMuteVersion(tstring& versNumber) reinterpret_cast(&pvi), &pviLen)) { TCHAR buf[30]; - -#ifdef _UNICODE - swprintf_s( -#else - sprintf_s( -#endif + StringCchPrintf( buf, ARRAY_SIZE(buf), _T("%d.%d.%d.%d"), diff --git a/WinMute/TrayIcon.cpp b/WinMute/TrayIcon.cpp index 443ea60..d6f5cb0 100644 --- a/WinMute/TrayIcon.cpp +++ b/WinMute/TrayIcon.cpp @@ -40,6 +40,7 @@ TrayIcon::TrayIcon() : TrayIcon::TrayIcon(HWND hWnd, UINT trayID, HICON hIcon, const tstring& tooltip, bool show) : + initialized_(false), iconVisible_(false) { Init(hWnd, trayID, hIcon, tooltip, show); @@ -61,6 +62,13 @@ void TrayIcon::Init(HWND hWnd, UINT trayID, HICON hIcon, if (show) { Show(); } + initialized_ = true; + if (popupQueue_.size() > 0) { + for (const auto& p : popupQueue_) { + ShowPopup(p.title, p.text); + } + popupQueue_.clear(); + } } TrayIcon::~TrayIcon() @@ -125,6 +133,13 @@ void TrayIcon::ChangeText(const tstring& tooltip) void TrayIcon::ShowPopup(const tstring& title, const tstring& text) const { + if (!initialized_) { + TrayIconPopup popup; + popup.title = title; + popup.text = text; + popupQueue_.push_back(popup); + return; + } NOTIFYICONDATA tnid; tnid.cbSize = sizeof(NOTIFYICONDATA); tnid.hWnd = hWnd_; diff --git a/WinMute/TrayIcon.h b/WinMute/TrayIcon.h index 7304306..0fbd9ed 100644 --- a/WinMute/TrayIcon.h +++ b/WinMute/TrayIcon.h @@ -37,6 +37,11 @@ POSSIBILITY OF SUCH DAMAGE. #define WM_TRAYICON (WM_APP + 700) +struct TrayIconPopup { + tstring title; + tstring text; +}; + class TrayIcon { public: TrayIcon(); @@ -58,6 +63,9 @@ private: bool RemoveNotifyIcon(); bool ChangeText(); + mutable std::vector popupQueue_; + + bool initialized_; bool iconVisible_; UINT trayID_; HICON hIcon_; diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index 9b3349d..ca7d655 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -383,11 +383,7 @@ bool WMSettings::StoreWifiNetworks(std::vector& networks) for (size_t i = 0; i < networks.size(); ++i) { TCHAR valueName[10]; -#ifdef _UNICODE - swprintf(valueName, ARRAY_SIZE(valueName), _T("WiFi %03zu"), i + 1); -#else - sprintf_s(valueName, "WiFi %03zu", i + 1); -#endif + StringCchPrintfW(valueName, ARRAY_SIZE(valueName), _T("WiFi %03zu"), i + 1); const tstring& v = networks[i]; DWORD regError = RegSetValueEx( hWifiKey_, diff --git a/WinMute/WiFiDetector.h b/WinMute/WiFiDetector.h index 82e7524..4435fe6 100644 --- a/WinMute/WiFiDetector.h +++ b/WinMute/WiFiDetector.h @@ -49,11 +49,15 @@ public: void Unload(); void SetNetworkList(const std::vector networks, bool isMuteList); + void CheckNetwork(); void WlanNotificationCallback(PWLAN_NOTIFICATION_DATA notifyData); private: HWND hNotifyWnd_; HANDLE wlanHandle_; + // If "true" then networks_ contains all networks where the workstation should + // be muted. If false, then networks_ contains all networks where the ws should + // not be muted. bool isMuteList_; bool initialized_; std::vector networks_; diff --git a/WinMute/WifiDetector.cpp b/WinMute/WifiDetector.cpp index 2652ab3..448207b 100644 --- a/WinMute/WifiDetector.cpp +++ b/WinMute/WifiDetector.cpp @@ -95,6 +95,48 @@ void WifiDetector::SetNetworkList(const std::vector networks, bool isMu isMuteList_ = isMuteList; } +void WifiDetector::CheckNetwork() +{ + PWLAN_INTERFACE_INFO_LIST ifList; + DWORD wlanErr = WlanEnumInterfaces(wlanHandle_, NULL, &ifList); + if (wlanErr != ERROR_SUCCESS) { + PrintWindowsError(_T("WlanEnumInterfaces"), wlanErr); + } else { + for (; ifList->dwIndex < ifList->dwNumberOfItems; ++ifList->dwIndex) { + PWLAN_AVAILABLE_NETWORK_LIST availList; + wlanErr = WlanGetAvailableNetworkList( + wlanHandle_, + &ifList->InterfaceInfo[ifList->dwIndex].InterfaceGuid, + 0, + NULL, + &availList); + if (wlanErr != ERROR_SUCCESS) { + PrintWindowsError(_T("WlanGetAvailableNetworkList"), wlanErr); + } else { + for (; availList->dwIndex < availList->dwNumberOfItems; ++availList->dwIndex) { + PWLAN_AVAILABLE_NETWORK net = &availList->Network[availList->dwIndex]; + if (net->dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) { + const auto it = std::find(std::begin(networks_), + std::end(networks_), + net->strProfileName); + if (isMuteList_ && it != std::end(networks_) + || !isMuteList_ && it == std::end(networks_)) { + size_t profileNameLen = lstrlen(net->strProfileName); + TCHAR* wiFiName = new TCHAR[profileNameLen + 1]; + StringCchCopy(wiFiName, profileNameLen + 1, net->strProfileName); + SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, 1, + reinterpret_cast(wiFiName)); + break; + } + } + } + WlanFreeMemory(availList); + } + } + WlanFreeMemory(ifList); + } +} + void WifiDetector::WlanNotificationCallback(PWLAN_NOTIFICATION_DATA notifyData) { if (notifyData->NotificationSource != WLAN_NOTIFICATION_SOURCE_ACM) { @@ -103,15 +145,18 @@ void WifiDetector::WlanNotificationCallback(PWLAN_NOTIFICATION_DATA notifyData) if (notifyData->NotificationCode == wlan_notification_acm_connection_complete || notifyData->NotificationCode == wlan_notification_acm_disconnected) { bool connected = notifyData->NotificationCode == wlan_notification_acm_connection_complete; - WLAN_CONNECTION_NOTIFICATION_DATA *wcnd = + WLAN_CONNECTION_NOTIFICATION_DATA * wcnd = reinterpret_cast(notifyData->pData); const auto it = std::find(std::begin(networks_), std::end(networks_), wcnd->strProfileName); - if (isMuteList_ && it != std::end(networks_)) { + + if (isMuteList_ && it != std::end(networks_) + || !isMuteList_ && it == std::end(networks_)) { + size_t profileNameLen = lstrlen(wcnd->strProfileName); + TCHAR* wiFiName = new TCHAR[profileNameLen + 1]; + StringCchCopy(wiFiName, profileNameLen + 1, wcnd->strProfileName); SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, connected, - reinterpret_cast((*it).c_str())); - } else if (!isMuteList_ && it == std::end(networks_)) { - SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, connected, NULL); + reinterpret_cast(wiFiName)); } } } diff --git a/WinMute/WinMute.cpp b/WinMute/WinMute.cpp index 5fe5c3d..be6ec13 100644 --- a/WinMute/WinMute.cpp +++ b/WinMute/WinMute.cpp @@ -331,9 +331,17 @@ bool WinMute::LoadSettings() muteConfig_.muteOnWlan = settings_.QueryValue(SettingsKey::MUTE_ON_WLAN); if (muteConfig_.muteOnWlan) { - wifiDetector_.Init(hWnd_); - bool isMuteList = !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST); - wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList); + if (!wifiDetector_.Init(hWnd_)) { + TaskDialog(nullptr, nullptr, PROGRAM_NAME, + _T("WLAN Detection"), + _T("Failed to initialize WLAN detection. ") + _T("This feature will be unavailable for this session"), + TDCBF_OK_BUTTON, TD_ERROR_ICON, nullptr); + } else { + bool isMuteList = !settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST); + wifiDetector_.SetNetworkList(settings_.GetWifiNetworks(), isMuteList); + wifiDetector_.CheckNetwork(); + } } else { wifiDetector_.Unload(); } @@ -573,28 +581,20 @@ LRESULT CALLBACK WinMute::WindowProc( if (wParam == 1) { // Connected if (settings_.QueryValue(SettingsKey::NOTIFICATIONS_ENABLED)) { TCHAR msgBuf[260] = { 0 }; - const TCHAR* wifiName = reinterpret_cast(lParam); - + TCHAR* wifiName = reinterpret_cast(lParam); if (settings_.QueryValue(SettingsKey::MUTE_ON_WLAN_ALLOWLIST)) { - StringCchCopy( - msgBuf, - ARRAY_SIZE(msgBuf), - _T("Current WLAN network is not on allowed list for AutoMute")); + StringCchPrintfW( + msgBuf, ARRAY_SIZE(msgBuf), + _T("WLAN \"%s\"network is not on allowed list.\n"), + wifiName); } else { -#ifdef _UNICODE - swprintf( + StringCchPrintfW( msgBuf, ARRAY_SIZE(msgBuf), _T("WLAN network \"%s\" is configured for AutoMute."), wifiName); -#else - sprintf_s( - msgBuf, - "WLAN network \"%s\" is configured for AutoMute.", - wifiName); -#endif } trayIcon_.ShowPopup(_T("Workstation muted"), msgBuf); - + delete [] wifiName; } muteCtrl_.SetMute(true); } diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index 4a112c4..9384846 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -359,8 +359,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 1,9,9,2 - PRODUCTVERSION 1,9,9,2 + FILEVERSION 1,9,9,3 + PRODUCTVERSION 1,9,9,3 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -377,23 +377,23 @@ BEGIN BEGIN VALUE "CompanyName", "LX-Systems" VALUE "FileDescription", "WinMute" - VALUE "FileVersion", "1.9.9.2" + VALUE "FileVersion", "1.9.9.3" VALUE "InternalName", "WinMute.exe" VALUE "LegalCopyright", "Copyright (C) 2022, Alexander Steinhöfer" VALUE "OriginalFilename", "WinMute.exe" VALUE "ProductName", "WinMute" - VALUE "ProductVersion", "1.9.9.2" + VALUE "ProductVersion", "1.9.9.3" END BLOCK "040704b0" BEGIN VALUE "CompanyName", "LX-Systems" VALUE "FileDescription", "WinMute" - VALUE "FileVersion", "1.9.9.2" + VALUE "FileVersion", "1.9.9.3" VALUE "InternalName", "WinMute.exe" VALUE "LegalCopyright", "Copyright (C) 2022, Alexander Steinhöfer" VALUE "OriginalFilename", "WinMute.exe" VALUE "ProductName", "WinMute" - VALUE "ProductVersion", "1.9.9.2" + VALUE "ProductVersion", "1.9.9.3" END END BLOCK "VarFileInfo" -- 2.43.0