]> repos.lx-s.de Git - winmute.git/commitdiff
Prevent duplicate network names
authorAlexander Steinhöfer <kontakt@lx-s.de>
Wed, 13 Apr 2022 17:34:35 +0000 (19:34 +0200)
committerAlexander Steinhöfer <kontakt@lx-s.de>
Wed, 13 Apr 2022 17:34:35 +0000 (19:34 +0200)
WinMute/QuietHours.h
WinMute/Settings_WifiDlg.cpp
WinMute/WMSettings.cpp
WinMute/WiFiDetector.h [new file with mode: 0644]
WinMute/WifiDetector.cpp [new file with mode: 0644]
WinMute/WinMute.rc
WinMute/WinMute.vcxproj
WinMute/WinMute.vcxproj.filters
WinMute/common.h

index fcedf099c77a7f931d7026d197184dffec254303..be6d047968705377232adfb7aa5dca5bcb1ee133 100644 (file)
@@ -1,3 +1,36 @@
+/*
+ WinMute
+           Copyright (c) 2022, Alexander Steinhoefer
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+
+    * Neither the name of the author nor the names of its contributors may
+      be used to endorse or promote products derived from this software
+      without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
 #pragma once
 
 #include "common.h"
index f0db2a7a534bcbdb8eb6ba184beb2edcd800a6d2..a71f16d2f00dd48a3adb157a39b8a08493755bcb 100644 (file)
@@ -81,7 +81,7 @@ INT_PTR CALLBACK Settings_GeneralWifiAddDlgProc(HWND hDlg, UINT msg, WPARAM wPar
             ebt.cbStruct = sizeof(ebt);
             ebt.pszText = _T("Please enter a SSID/WIFI name");
             ebt.pszTitle = _T("SSID Name");
-            ebt.ttiIcon = TTI_WARNING_LARGE;
+            ebt.ttiIcon = TTI_INFO;
             Edit_ShowBalloonTip(hSsid, &ebt);
          } else {
             TCHAR ssidBuf[SSID_MAX_LEN + 1];
@@ -107,6 +107,21 @@ INT_PTR CALLBACK Settings_GeneralWifiAddDlgProc(HWND hDlg, UINT msg, WPARAM wPar
    return FALSE;
 }
 
+static std::vector<tstring> ExportSsidListItems(HWND hList)
+{
+   std::vector<tstring> items;
+   DWORD itemCount = ListBox_GetCount(hList);
+   for (DWORD i = 0; i < itemCount; ++i) {
+      TCHAR textBuf[SSID_MAX_LEN + 1] = { 0 };
+      DWORD textLen = ListBox_GetTextLen(hList, i);
+      if (textLen < ARRAY_SIZE(textBuf)) {
+         ListBox_GetText(hList, i, textBuf);
+         items.push_back(textBuf);
+      }
+   }
+   return items;
+}
+
 INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
 {
    switch (msg) {
@@ -152,12 +167,15 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
                hDlg,
                Settings_GeneralWifiAddDlgProc,
                reinterpret_cast<LPARAM>(&wifiData)) == 0) {
-            ListBox_AddString(
-               GetDlgItem(hDlg, IDC_WIFI_LIST),
-               wifiData.ssidName.c_str());
-            HWND hRemoveAll = GetDlgItem(hDlg, IDC_WIFI_REMOVEALL);
-            if (!IsWindowEnabled(hRemoveAll)) {
-               Button_Enable(hRemoveAll, TRUE);
+            std::vector<tstring> networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST));
+            if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) {
+               ListBox_AddString(
+                  GetDlgItem(hDlg, IDC_WIFI_LIST),
+                  wifiData.ssidName.c_str());
+               HWND hRemoveAll = GetDlgItem(hDlg, IDC_WIFI_REMOVEALL);
+               if (!IsWindowEnabled(hRemoveAll)) {
+                  Button_Enable(hRemoveAll, TRUE);
+               }
             }
          }
       } else if (LOWORD(wParam) == IDC_WIFI_EDIT) {
@@ -167,7 +185,7 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
             int len = ListBox_GetTextLen(hList, sel);
             TCHAR * textBuf = NULL;
             if (len != LB_ERR) {
-               if ((textBuf = new TCHAR[len + 1]) != NULL) {
+               if ((textBuf = new TCHAR[static_cast<size_t>(len) + 1]) != NULL) {
                   ListBox_GetText(hList, sel, textBuf);
 
                   WiFiData wifiData;
@@ -180,8 +198,13 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
                         hDlg,
                         Settings_GeneralWifiAddDlgProc,
                         reinterpret_cast<LPARAM>(&wifiData)) == 0) {
-                     ListBox_InsertString(hList, sel, wifiData.ssidName.c_str());
-                     ListBox_DeleteString(hList, sel + 1);
+                     std::vector<tstring> networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST));
+                     if (std::find(begin(networks), end(networks), wifiData.ssidName) == end(networks)) {
+                        ListBox_InsertString(hList, sel, wifiData.ssidName.c_str());
+                        ListBox_DeleteString(hList, sel + 1);
+                     } else {
+                        ListBox_DeleteString(hList, sel);
+                     }
                   }
                }
             }
@@ -206,18 +229,8 @@ INT_PTR CALLBACK Settings_GeneralWifiDlgProc(HWND hDlg, UINT msg, WPARAM wParam,
       return 0;
    }
    case WM_SAVESETTINGS: {
-      HWND hList = GetDlgItem(hDlg, IDC_WIFI_LIST);
       WMSettings* settings = reinterpret_cast<WMSettings*>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
-      DWORD items = ListBox_GetCount(hList);
-      std::vector<tstring> networks;
-      for (DWORD i = 0; i < items; ++i) {
-         TCHAR textBuf[SSID_MAX_LEN + 1];
-         DWORD textLen = ListBox_GetTextLen(hDlg, i);
-         if (textLen < ARRAY_SIZE(textBuf)) {
-            ListBox_GetText(hList, i, textBuf);
-            networks.push_back(textBuf);
-         }
-      }
+      std::vector<tstring> networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST));
       settings->StoreWifiNetworks(networks);
       return 0;
    }
index e6bd2325a05e53ab303b859fb56b4018fbeb8066..c502222dccf5e438ef8d3ba3038cef6e3c2e382f 100644 (file)
@@ -131,6 +131,16 @@ static DWORD GetDefaultSetting(SettingsKey key)
    return 0;
 }
 
+
+static void NormalizeNetworkList(std::vector<tstring>& networks)
+{
+   if (networks.size() > 1) {
+      std::sort(std::begin(networks), std::end(networks));
+      auto it = std::unique(std::begin(networks), std::end(networks));
+      networks.resize(std::distance(std::begin(networks), it));
+   }
+}
+
 static bool ReadStringFromRegistry(HKEY hKey, const TCHAR* subKey, tstring& val)
 {
    bool success = false;
@@ -359,9 +369,8 @@ bool WMSettings::StoreWifiNetworks(std::vector<tstring>& networks)
       }
    }
 
-   if (networks.size() > 1) {
-      std::sort(std::begin(networks), std::end(networks));
-   }
+   NormalizeNetworkList(networks);
+
    for (size_t i = 0; i < networks.size(); ++i) {
       TCHAR valueName[10];
 #ifdef _UNICODE
@@ -413,8 +422,6 @@ std::vector<tstring> WMSettings::GetWifiNetworks() const
          networks.push_back(dataBuf);
       }
    }
-   if (networks.size() > 1) {
-      std::sort(std::begin(networks), std::end(networks));
-   }
+   NormalizeNetworkList(networks);
    return networks;
 }
diff --git a/WinMute/WiFiDetector.h b/WinMute/WiFiDetector.h
new file mode 100644 (file)
index 0000000..c91fede
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ WinMute
+           Copyright (c) 2022, Alexander Steinhoefer
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+
+    * Neither the name of the author nor the names of its contributors may
+      be used to endorse or promote products derived from this software
+      without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#pragma once
+
+#include "common.h"
+
+class WifiDetector {
+public:
+   WifiDetector();
+   ~WifiDetector();
+   WifiDetector(const WifiDetector&) = delete;
+   WifiDetector& operator=(const WifiDetector&) = delete;
+
+   bool Init(HWND hNotifyWnd);
+   void SetNetworkList(const std::vector<tstring> networks);
+
+private:
+   HWND hNotifyWnd_;
+   HANDLE wlanHandle_;
+   bool initialized_;
+   std::vector<tstring> networks_;
+};
diff --git a/WinMute/WifiDetector.cpp b/WinMute/WifiDetector.cpp
new file mode 100644 (file)
index 0000000..fb020a9
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ WinMute
+           Copyright (c) 2022, Alexander Steinhoefer
+
+-----------------------------------------------------------------------------
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice,
+      this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+
+    * Neither the name of the author nor the names of its contributors may
+      be used to endorse or promote products derived from this software
+      without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+-----------------------------------------------------------------------------
+*/
+
+#pragma once
+
+#include "common.h"
+
+WifiDetector::WifiDetector()
+   : hNotifyWnd_(NULL), wlanHandle_(NULL), initialized_(false)
+{
+}
+
+WifiDetector::~WifiDetector()
+{
+}
+
+bool WifiDetector::Init(HWND hNotifyWnd)
+{
+   hNotifyWnd_ = hNotifyWnd;
+   return true;
+}
+
+void WifiDetector::SetNetworkList(const std::vector<tstring> networks)
+{
+   networks_ = networks;
+}
index 3b18ff0df7fb4d017346a911da0e28d02c5df923..e53c9f8e4dabfadfc4f35ea539a2408ef1937fc5 100644 (file)
@@ -147,11 +147,11 @@ STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
 FONT 8, "MS Shell Dlg", 400, 0, 0x1
 BEGIN
     LTEXT           "WinMute can detect when you are connecting to a particular WiFi Network and mute your PC when it finishes connecting. \nHere you can manage SSIDs of networks that should trigger muting:",IDC_STATIC,7,7,228,35
-    LISTBOX         IDC_WIFI_LIST,7,51,170,123,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
-    PUSHBUTTON      "Add",IDC_WIFI_ADD,179,51,56,16
-    PUSHBUTTON      "Edit",IDC_WIFI_EDIT,179,70,56,16
-    PUSHBUTTON      "Remove",IDC_WIFI_REMOVE,179,89,56,16
-    PUSHBUTTON      "Remove all",IDC_WIFI_REMOVEALL,179,109,56,16
+    LISTBOX         IDC_WIFI_LIST,7,40,170,134,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
+    PUSHBUTTON      "Add",IDC_WIFI_ADD,179,40,56,16
+    PUSHBUTTON      "Edit",IDC_WIFI_EDIT,179,59,56,16
+    PUSHBUTTON      "Remove",IDC_WIFI_REMOVE,179,78,56,16
+    PUSHBUTTON      "Remove all",IDC_WIFI_REMOVEALL,179,98,56,16
     LTEXT           "Note: This feature is currently experimental",IDC_STATIC,7,182,171,15
 END
 
index 5b9f673bb43d2561252936c753c4d5d722fdbe83..473bcd5db497d8342759e780b7047f913741c475 100644 (file)
   <ItemGroup>
     <ClInclude Include="BaseTypes.h" />
     <ClInclude Include="common.h" />
+    <ClInclude Include="WiFiDetector.h" />
     <ClInclude Include="WMLog.h" />
     <ClInclude Include="MMNotificationClient.h" />
     <ClInclude Include="MuteControl.h" />
     </ClCompile>
     <ClCompile Include="Settings_MuteDlg.cpp" />
     <ClCompile Include="Settings_WifiDlg.cpp" />
+    <ClCompile Include="WifiDetector.cpp" />
     <ClCompile Include="WMLog.cpp" />
     <ClCompile Include="MMNotificationClient.cpp" />
     <ClCompile Include="MuteControl.cpp" />
index ddb2cc9a3abf19d47252cc678671b3aafc7dd579..f2904ac8c453cfd57c4997610b5963688a86875f 100644 (file)
@@ -51,6 +51,9 @@
     <Filter Include="Source Files\UI\Dialogs\Settings">
       <UniqueIdentifier>{c4de9bea-a833-43ac-8021-6e5cf7d048e8}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Source Files\Controllers\WiFi">
+      <UniqueIdentifier>{a804b525-296b-455b-a1bc-d8fdbf5a9c3d}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="WinAudio.h">
@@ -95,6 +98,9 @@
     <ClInclude Include="BaseTypes.h">
       <Filter>Source Files</Filter>
     </ClInclude>
+    <ClInclude Include="WiFiDetector.h">
+      <Filter>Source Files\Controllers\WiFi</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="WinMute.rc">
     <ClCompile Include="Settings_WifiDlg.cpp">
       <Filter>Source Files\UI\Dialogs\Settings</Filter>
     </ClCompile>
+    <ClCompile Include="WifiDetector.cpp">
+      <Filter>Source Files\Controllers\WiFi</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>
\ No newline at end of file
index 3150072c102d049d2491056efc11955439f4e810..761381087f1b71ed010ee09bcaa282ae52803f6c 100644 (file)
@@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include <unordered_map>
 #include <vector>
 #include <cstdarg>
+#include <iterator>
 
 #include <windows.h>
 #include <windowsx.h>
@@ -88,6 +89,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "MuteControl.h"
 #include "WinMute.h"
 #include "VersionHelper.h"
+#include "WiFiDetector.h"
 
 // Utility
 void PrintWindowsError(LPCWSTR lpszFunction, DWORD lastError = -1);