From 58924c85ce7fced056fdb1b9435fa48c0c6af616 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexander=20Steinh=C3=B6fer?= Date: Wed, 13 Apr 2022 19:34:35 +0200 Subject: [PATCH] Prevent duplicate network names --- WinMute/QuietHours.h | 33 +++++++++++++++++++ WinMute/Settings_WifiDlg.cpp | 55 +++++++++++++++++++------------- WinMute/WMSettings.cpp | 19 +++++++---- WinMute/WiFiDetector.h | 53 +++++++++++++++++++++++++++++++ WinMute/WifiDetector.cpp | 56 +++++++++++++++++++++++++++++++++ WinMute/WinMute.rc | 10 +++--- WinMute/WinMute.vcxproj | 2 ++ WinMute/WinMute.vcxproj.filters | 9 ++++++ WinMute/common.h | 2 ++ 9 files changed, 207 insertions(+), 32 deletions(-) create mode 100644 WinMute/WiFiDetector.h create mode 100644 WinMute/WifiDetector.cpp diff --git a/WinMute/QuietHours.h b/WinMute/QuietHours.h index fcedf09..be6d047 100644 --- a/WinMute/QuietHours.h +++ b/WinMute/QuietHours.h @@ -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" diff --git a/WinMute/Settings_WifiDlg.cpp b/WinMute/Settings_WifiDlg.cpp index f0db2a7..a71f16d 100644 --- a/WinMute/Settings_WifiDlg.cpp +++ b/WinMute/Settings_WifiDlg.cpp @@ -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 ExportSsidListItems(HWND hList) +{ + std::vector 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(&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 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(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(&wifiData)) == 0) { - ListBox_InsertString(hList, sel, wifiData.ssidName.c_str()); - ListBox_DeleteString(hList, sel + 1); + std::vector 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(GetWindowLongPtr(hDlg, GWLP_USERDATA)); - DWORD items = ListBox_GetCount(hList); - std::vector 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 networks = ExportSsidListItems(GetDlgItem(hDlg, IDC_WIFI_LIST)); settings->StoreWifiNetworks(networks); return 0; } diff --git a/WinMute/WMSettings.cpp b/WinMute/WMSettings.cpp index e6bd232..c502222 100644 --- a/WinMute/WMSettings.cpp +++ b/WinMute/WMSettings.cpp @@ -131,6 +131,16 @@ static DWORD GetDefaultSetting(SettingsKey key) return 0; } + +static void NormalizeNetworkList(std::vector& 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& 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 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 index 0000000..c91fede --- /dev/null +++ b/WinMute/WiFiDetector.h @@ -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 networks); + +private: + HWND hNotifyWnd_; + HANDLE wlanHandle_; + bool initialized_; + std::vector networks_; +}; diff --git a/WinMute/WifiDetector.cpp b/WinMute/WifiDetector.cpp new file mode 100644 index 0000000..fb020a9 --- /dev/null +++ b/WinMute/WifiDetector.cpp @@ -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 networks) +{ + networks_ = networks; +} diff --git a/WinMute/WinMute.rc b/WinMute/WinMute.rc index 3b18ff0..e53c9f8 100644 --- a/WinMute/WinMute.rc +++ b/WinMute/WinMute.rc @@ -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 diff --git a/WinMute/WinMute.vcxproj b/WinMute/WinMute.vcxproj index 5b9f673..473bcd5 100644 --- a/WinMute/WinMute.vcxproj +++ b/WinMute/WinMute.vcxproj @@ -192,6 +192,7 @@ + @@ -224,6 +225,7 @@ + diff --git a/WinMute/WinMute.vcxproj.filters b/WinMute/WinMute.vcxproj.filters index ddb2cc9..f2904ac 100644 --- a/WinMute/WinMute.vcxproj.filters +++ b/WinMute/WinMute.vcxproj.filters @@ -51,6 +51,9 @@ {c4de9bea-a833-43ac-8021-6e5cf7d048e8} + + {a804b525-296b-455b-a1bc-d8fdbf5a9c3d} + @@ -95,6 +98,9 @@ Source Files + + Source Files\Controllers\WiFi + @@ -173,5 +179,8 @@ Source Files\UI\Dialogs\Settings + + Source Files\Controllers\WiFi + \ No newline at end of file diff --git a/WinMute/common.h b/WinMute/common.h index 3150072..7613810 100644 --- a/WinMute/common.h +++ b/WinMute/common.h @@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include @@ -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); -- 2.43.0