+/*
+ 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"
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];
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) {
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) {
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;
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);
+ }
}
}
}
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;
}
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;
}
}
- 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
networks.push_back(dataBuf);
}
}
- if (networks.size() > 1) {
- std::sort(std::begin(networks), std::end(networks));
- }
+ NormalizeNetworkList(networks);
return networks;
}
--- /dev/null
+/*
+ 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_;
+};
--- /dev/null
+/*
+ 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;
+}
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
<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" />
<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">
<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
#include <unordered_map>
#include <vector>
#include <cstdarg>
+#include <iterator>
#include <windows.h>
#include <windowsx.h>
#include "MuteControl.h"
#include "WinMute.h"
#include "VersionHelper.h"
+#include "WiFiDetector.h"
// Utility
void PrintWindowsError(LPCWSTR lpszFunction, DWORD lastError = -1);