]> repos.lx-s.de Git - winmute.git/commitdiff
Detect already connected networks on settings change
authorAlexander Steinhöfer <kontakt@lx-s.de>
Thu, 14 Apr 2022 06:27:35 +0000 (08:27 +0200)
committerAlexander Steinhöfer <kontakt@lx-s.de>
Thu, 14 Apr 2022 06:27:35 +0000 (08:27 +0200)
WinMute/AboutDlg.cpp
WinMute/TrayIcon.cpp
WinMute/TrayIcon.h
WinMute/WMSettings.cpp
WinMute/WiFiDetector.h
WinMute/WifiDetector.cpp
WinMute/WinMute.cpp
WinMute/WinMute.rc

index 27b919b9c7ea253d2d32780bcdab196aa41825e8..81fab64be296765d8257025c92f558d3f11009ef 100644 (file)
@@ -196,12 +196,7 @@ static bool GetWinMuteVersion(tstring& versNumber)
                reinterpret_cast<LPVOID*>(&pvi),
                &pviLen)) {
             TCHAR buf[30];
-
-#ifdef _UNICODE
-            swprintf_s(
-#else
-            sprintf_s(
-#endif
+            StringCchPrintf(
                buf,
                ARRAY_SIZE(buf),
                _T("%d.%d.%d.%d"),
index 443ea60c900b84590e503b8f1fb5e790137e935b..d6f5cb054246f258c6c186a80cd200e6e86d5845 100644 (file)
@@ -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_;
index 7304306883c34b24f81399bb8d4be7e8c2c39b70..0fbd9edc754eaf2aaa5e43253f9942fbd88f1483 100644 (file)
@@ -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<TrayIconPopup> popupQueue_;
+
+   bool initialized_;
    bool iconVisible_;
    UINT trayID_;
    HICON hIcon_;
index 9b3349d779305cf271d395b4ff55229e1f91ace6..ca7d655861354045f42ef5dd4ec786875c83ebb7 100644 (file)
@@ -383,11 +383,7 @@ bool WMSettings::StoreWifiNetworks(std::vector<tstring>& 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_,
index 82e752411cdcbb34363e25bb27715df35a921091..4435fe6f6ffa30dfa78349e2bbadebb1b8ae8063 100644 (file)
@@ -49,11 +49,15 @@ public:
    void Unload();
    void SetNetworkList(const std::vector<tstring> 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<tstring> networks_;
index 2652ab3dd136c386bf604fac3e53866701ea0aec..448207bd9f5e6bc586b8b4ca8265279446f8c404 100644 (file)
@@ -95,6 +95,48 @@ void WifiDetector::SetNetworkList(const std::vector<tstring> 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<LPARAM>(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<WLAN_CONNECTION_NOTIFICATION_DATA*>(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<LPARAM>((*it).c_str()));
-      } else if (!isMuteList_ && it == std::end(networks_)) {
-         SendMessage(hNotifyWnd_, WM_WIFISTATUSCHANGED, connected, NULL);
+                      reinterpret_cast<LPARAM>(wiFiName));
       }
    }
 }
index 5fe5c3de2261480da3e775ceb760adee2a0ef405..be6ec13be8bf954973cf03d3d4a5a446c469d90b 100644 (file)
@@ -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<TCHAR*>(lParam);
-               
+               TCHAR* wifiName = reinterpret_cast<TCHAR*>(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);
          }
index 4a112c43abf71c4bde41fe2135a6f8b8a5e65904..93848469fa36e57087df954ee69299230eb464ab 100644 (file)
@@ -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"