From 41ae1ce5bec8ba343750345972b26770129d81f8 Mon Sep 17 00:00:00 2001 From: George Hazan <ghazan@miranda.im> Date: Mon, 24 Jan 2022 15:01:41 +0300 Subject: fixes #3006 again + massive code cleaning --- plugins/Ping/src/options.cpp | 629 +++++++++++++++++++--------------------- plugins/Ping/src/ping.cpp | 11 +- plugins/Ping/src/pinggraph.cpp | 20 +- plugins/Ping/src/pinglist.cpp | 91 +++--- plugins/Ping/src/pinglist.h | 47 ++- plugins/Ping/src/pingthread.cpp | 65 +++-- plugins/Ping/src/pingthread.h | 2 +- plugins/Ping/src/stdafx.h | 31 -- plugins/Ping/src/utils.cpp | 108 ++++--- 9 files changed, 472 insertions(+), 532 deletions(-) (limited to 'plugins/Ping/src') diff --git a/plugins/Ping/src/options.cpp b/plugins/Ping/src/options.cpp index 953b72dc0a..549300c979 100644 --- a/plugins/Ping/src/options.cpp +++ b/plugins/Ping/src/options.cpp @@ -2,165 +2,12 @@ PingOptions options; -// main ping options -static INT_PTR CALLBACK DlgProcOpts(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) -{ - switch (msg) { - case WM_INITDIALOG: - TranslateDialogDefault(hwndDlg); - - if (ServiceExists(MS_CLIST_FRAMES_ADDFRAME)) - EnableWindow(GetDlgItem(hwndDlg, IDC_CHK_ATTACH), FALSE); - - CheckDlgButton(hwndDlg, IDC_CHK_ATTACH, options.attach_to_clist ? BST_CHECKED : BST_UNCHECKED); - - SetDlgItemInt(hwndDlg, IDC_PPM, options.ping_period, FALSE); - SetDlgItemInt(hwndDlg, IDC_PT, options.ping_timeout, FALSE); - CheckDlgButton(hwndDlg, IDC_CHECKPOPUP, options.show_popup ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwndDlg, IDC_CHECKPOPUP2, options.show_popup2 ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwndDlg, IDC_CHK_BLOCK, options.block_reps ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwndDlg, IDC_CHK_LOG, options.logging ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwndDlg, IDC_CHK_LOGCSV, options.log_csv ? BST_CHECKED : BST_UNCHECKED); - CheckDlgButton(hwndDlg, IDC_CHK_NOTESTICON, options.no_test_icon ? BST_CHECKED : BST_UNCHECKED); - - SendDlgItemMessage(hwndDlg, IDC_SP_INDENT, UDM_SETRANGE, 0, (LPARAM)MAKELONG(500, 0)); - SendDlgItemMessage(hwndDlg, IDC_SP_INDENT, UDM_SETPOS, 0, options.indent); - SendDlgItemMessage(hwndDlg, IDC_SP_ROWHEIGHT, UDM_SETRANGE, 0, (LPARAM)MAKELONG(500, 6)); - SendDlgItemMessage(hwndDlg, IDC_SP_ROWHEIGHT, UDM_SETPOS, 0, options.row_height); - - SetDlgItemInt(hwndDlg, IDC_RPT, options.retries, FALSE); - - SetDlgItemText(hwndDlg, IDC_ED_FILENAME, options.log_filename); - if (!options.logging) { - EnableWindow(GetDlgItem(hwndDlg, IDC_ED_FILENAME), FALSE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_LOGBROWSE), FALSE); - EnableWindow(GetDlgItem(hwndDlg, IDC_CHK_LOGCSV), FALSE); - } - return TRUE; - - case WM_COMMAND: - if (HIWORD(wParam) == EN_CHANGE && (HWND)lParam == GetFocus()) { - switch (LOWORD(wParam)) { - case IDC_PPM: - case IDC_PT: - case IDC_ED_FILENAME: - case IDC_RPT: - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - break; - } - - if (HIWORD(wParam) == CBN_SELCHANGE) { - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - break; - } - - if (HIWORD(wParam) == BN_CLICKED) { - switch (LOWORD(wParam)) { - case IDC_CHK_LOG: - EnableWindow(GetDlgItem(hwndDlg, IDC_ED_FILENAME), IsDlgButtonChecked(hwndDlg, IDC_CHK_LOG)); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_LOGBROWSE), IsDlgButtonChecked(hwndDlg, IDC_CHK_LOG)); - EnableWindow(GetDlgItem(hwndDlg, IDC_CHK_LOGCSV), IsDlgButtonChecked(hwndDlg, IDC_CHK_LOG)); - // drop through - case IDC_CHK_LOGCSV: - case IDC_CHECKPOPUP: - case IDC_CHECKPOPUP2: - case IDC_CHK_BLOCK: - case IDC_CHK_MINMAX: - case IDC_CHK_NOTESTICON: - case IDC_CHK_ATTACH: - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - break; - case IDC_BTN_VIEWLOG: - CallService(MODULENAME "/ViewLogData", 0, 0); - break; - case IDC_BTN_LOGBROWSE: - { - wchar_t filter[MAX_PATH]; - mir_snwprintf(filter, L"%s%c*.txt%c%s%c*.*%c", TranslateT("Text Files (*.txt)"), 0, 0, TranslateT("All Files"), 0, 0); - OPENFILENAME ofn = { 0 }; - ofn.lStructSize = sizeof(ofn); - ofn.lpstrFile = options.log_filename; - ofn.hwndOwner = hwndDlg; - ofn.nMaxFile = _countof(options.log_filename); - ofn.lpstrTitle = TranslateT("Open log file"); - ofn.lpstrFilter = filter; - ofn.nFilterIndex = 1; - ofn.lpstrFileTitle = nullptr; - ofn.nMaxFileTitle = 0; - ofn.lpstrInitialDir = nullptr; - ofn.Flags = OFN_PATHMUSTEXIST; - - if (GetOpenFileName(&ofn) == TRUE) { - SetDlgItemText(hwndDlg, IDC_ED_FILENAME, ofn.lpstrFile); - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - } - break; - } - break; - } - break; - - case WM_NOTIFY: - if (((LPNMHDR)lParam)->code == UDN_DELTAPOS) { - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - if (((LPNMHDR)lParam)->code == PSN_APPLY) { - - BOOL trans_success; - - uint32_t new_ping_period = GetDlgItemInt(hwndDlg, IDC_PPM, &trans_success, FALSE); - if (trans_success) { - options.ping_period = new_ping_period; - } - uint32_t new_ping_timeout = GetDlgItemInt(hwndDlg, IDC_PT, &trans_success, FALSE); - if (trans_success) { - options.ping_timeout = new_ping_timeout; - } - options.show_popup = IsDlgButtonChecked(hwndDlg, IDC_CHECKPOPUP) == BST_CHECKED; - options.show_popup2 = IsDlgButtonChecked(hwndDlg, IDC_CHECKPOPUP2) == BST_CHECKED; - options.block_reps = IsDlgButtonChecked(hwndDlg, IDC_CHK_BLOCK) == BST_CHECKED; - options.logging = IsDlgButtonChecked(hwndDlg, IDC_CHK_LOG) == BST_CHECKED; - options.log_csv = IsDlgButtonChecked(hwndDlg, IDC_CHK_LOGCSV) == BST_CHECKED; - GetDlgItemText(hwndDlg, IDC_ED_FILENAME, options.log_filename, _countof(options.log_filename)); - - options.no_test_icon = IsDlgButtonChecked(hwndDlg, IDC_CHK_NOTESTICON) == BST_CHECKED; - - options.indent = SendDlgItemMessage(hwndDlg, IDC_SP_INDENT, UDM_GETPOS, 0, 0); - options.row_height = SendDlgItemMessage(hwndDlg, IDC_SP_ROWHEIGHT, UDM_GETPOS, 0, 0); - - uint32_t new_retries = GetDlgItemInt(hwndDlg, IDC_RPT, &trans_success, FALSE); - if (trans_success) { - options.retries = new_retries; - } - - bool new_attach = (IsDlgButtonChecked(hwndDlg, IDC_CHK_ATTACH) == BST_CHECKED); - if (!ServiceExists(MS_CLIST_FRAMES_ADDFRAME) && options.attach_to_clist != new_attach) - AttachToClist(new_attach); - - options.attach_to_clist = new_attach; - - SaveOptions(); - - RefreshWindow(0, 0); - - Log(L"options changed"); - - if (hWakeEvent) - SetEvent(hWakeEvent); - return TRUE; - } - break; - } - - return FALSE; -} +///////////////////////////////////////////////////////////////////////////////////////// +// host edit PINGLIST temp_list; PINGADDRESS add_edit_addr; -// host edit INT_PTR CALLBACK DlgProcDestEdit(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) { switch (msg) { @@ -207,7 +54,7 @@ INT_PTR CALLBACK DlgProcDestEdit(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) // ? doesn't work? ? SetFocus(GetDlgItem(hwndDlg, IDC_ED_DESTLAB)); return FALSE; - + case WM_COMMAND: if (HIWORD(wParam) == LBN_SELCHANGE && LOWORD(wParam) == IDC_COMBO_DESTPROTO) { int sel = SendDlgItemMessage(hwndDlg, IDC_COMBO_DESTPROTO, CB_GETCURSEL, 0, 0); @@ -222,7 +69,7 @@ INT_PTR CALLBACK DlgProcDestEdit(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) case IDC_CHK_DESTTCP: EnableWindow(GetDlgItem(hwndDlg, IDC_ED_DESTPORT), IsDlgButtonChecked(hwndDlg, IDC_CHK_DESTTCP)); break; - + case IDOK: GetDlgItemText(hwndDlg, IDC_ED_DESTADDR, add_edit_addr.pszName, _countof(add_edit_addr.pszName)); GetDlgItemText(hwndDlg, IDC_ED_DESTLAB, add_edit_addr.pszLabel, _countof(add_edit_addr.pszLabel)); @@ -254,9 +101,9 @@ INT_PTR CALLBACK DlgProcDestEdit(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM) else add_edit_addr.port = -1; EndDialog(hwndDlg, IDOK); - RefreshWindow(0, 0); + RefreshWindow(); break; - + case IDCANCEL: EndDialog(hwndDlg, IDCANCEL); break; @@ -279,203 +126,324 @@ bool Edit(HWND hwnd, PINGADDRESS &addr) return false; } -// ping hosts list window -static INT_PTR CALLBACK DlgProcOpts2(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) +///////////////////////////////////////////////////////////////////////////////////////// +// main ping options + +class CMainOptsDlg : public CDlgBase { - switch (msg) { - case WM_INITDIALOG: - TranslateDialogDefault(hwndDlg); - { - mir_cslock lck(data_list_cs); - temp_list = data_list; + CCtrlCheck chkLog; + CCtrlButton btnViewLog, btnBrowse; + +public: + CMainOptsDlg() : + CDlgBase(g_plugin, IDD_DIALOG1), + chkLog(this, IDC_CHK_LOG), + btnBrowse(this, IDC_BTN_LOGBROWSE), + btnViewLog(this, IDC_BTN_VIEWLOG) + { + chkLog.OnChange = Callback(this, &CMainOptsDlg::onChange_Log); + + btnBrowse.OnClick = Callback(this, &CMainOptsDlg::onClick_Browse); + btnViewLog.OnClick = Callback(this, &CMainOptsDlg::onClick_ViewLog); + } - for (pinglist_it i = temp_list.begin(); i != temp_list.end(); ++i) { - int index = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)i->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, index, (LPARAM) & (*i)); - } - } - return TRUE; + bool OnInitDialog() override + { + if (ServiceExists(MS_CLIST_FRAMES_ADDFRAME)) + EnableWindow(GetDlgItem(m_hwnd, IDC_CHK_ATTACH), FALSE); - case WM_COMMAND: - if (HIWORD(wParam) == LBN_SELCHANGE && LOWORD(wParam) == IDC_LST_DEST) { - int sel = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCURSEL, 0, 0); - if (sel != LB_ERR) { - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTREM), TRUE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTEDIT), TRUE); - - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), (sel > 0)); - int count = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCOUNT, 0, 0); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), (sel < count - 1)); - } + CheckDlgButton(m_hwnd, IDC_CHK_ATTACH, options.attach_to_clist ? BST_CHECKED : BST_UNCHECKED); + + SetDlgItemInt(m_hwnd, IDC_PPM, options.ping_period, FALSE); + SetDlgItemInt(m_hwnd, IDC_PT, options.ping_timeout, FALSE); + CheckDlgButton(m_hwnd, IDC_CHECKPOPUP, options.show_popup ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(m_hwnd, IDC_CHECKPOPUP2, options.show_popup2 ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(m_hwnd, IDC_CHK_BLOCK, options.block_reps ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(m_hwnd, IDC_CHK_LOG, options.logging ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(m_hwnd, IDC_CHK_LOGCSV, options.log_csv ? BST_CHECKED : BST_UNCHECKED); + CheckDlgButton(m_hwnd, IDC_CHK_NOTESTICON, options.no_test_icon ? BST_CHECKED : BST_UNCHECKED); + + SendDlgItemMessage(m_hwnd, IDC_SP_INDENT, UDM_SETRANGE, 0, (LPARAM)MAKELONG(500, 0)); + SendDlgItemMessage(m_hwnd, IDC_SP_INDENT, UDM_SETPOS, 0, options.indent); + SendDlgItemMessage(m_hwnd, IDC_SP_ROWHEIGHT, UDM_SETRANGE, 0, (LPARAM)MAKELONG(500, 6)); + SendDlgItemMessage(m_hwnd, IDC_SP_ROWHEIGHT, UDM_SETPOS, 0, options.row_height); + + SetDlgItemInt(m_hwnd, IDC_RPT, options.retries, FALSE); + + SetDlgItemText(m_hwnd, IDC_ED_FILENAME, options.log_filename); + if (!options.logging) { + EnableWindow(GetDlgItem(m_hwnd, IDC_ED_FILENAME), FALSE); + EnableWindow(GetDlgItem(m_hwnd, IDC_BTN_LOGBROWSE), FALSE); + EnableWindow(GetDlgItem(m_hwnd, IDC_CHK_LOGCSV), FALSE); } + return true; + } - if (HIWORD(wParam) == BN_CLICKED) { - switch (LOWORD(wParam)) { - case IDC_BTN_DESTEDIT: - { - int sel = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCURSEL, 0, 0); - if (sel != LB_ERR) { - PINGADDRESS *item = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel, 0); - PINGADDRESS temp = *item; - if (Edit(hwndDlg, temp)) { - *item = temp; - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)sel, 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)sel, (LPARAM)item->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)sel, (LPARAM)item); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETCURSEL, (WPARAM)sel, 0); - - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTREM), TRUE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTEDIT), TRUE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), sel > 0); - int count = SendDlgItemMessage(hwndDlg, IDC_BTN_DESTDOWN, LB_GETCOUNT, 0, 0); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), (sel < count - 1)); - - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - } - } - break; + bool OnApply() override + { + BOOL trans_success; + uint32_t new_ping_period = GetDlgItemInt(m_hwnd, IDC_PPM, &trans_success, FALSE); + if (trans_success) + options.ping_period = new_ping_period; - case IDC_BTN_DESTADD: - memset(&add_edit_addr, 0, sizeof(add_edit_addr)); - add_edit_addr.cbSize = sizeof(add_edit_addr); - add_edit_addr.port = -1; - add_edit_addr.set_status = ID_STATUS_ONLINE; - add_edit_addr.get_status = ID_STATUS_OFFLINE; - add_edit_addr.status = PS_NOTRESPONDING; - add_edit_addr.item_id = 0; - add_edit_addr.index = (int)temp_list.size(); + uint32_t new_ping_timeout = GetDlgItemInt(m_hwnd, IDC_PT, &trans_success, FALSE); + if (trans_success) + options.ping_timeout = new_ping_timeout; - if (DialogBox(g_plugin.getInst(), MAKEINTRESOURCE(IDD_DIALOG3), hwndDlg, DlgProcDestEdit) == IDOK) { - temp_list.push_back(add_edit_addr); + options.show_popup = IsDlgButtonChecked(m_hwnd, IDC_CHECKPOPUP) == BST_CHECKED; + options.show_popup2 = IsDlgButtonChecked(m_hwnd, IDC_CHECKPOPUP2) == BST_CHECKED; + options.block_reps = IsDlgButtonChecked(m_hwnd, IDC_CHK_BLOCK) == BST_CHECKED; + options.logging = IsDlgButtonChecked(m_hwnd, IDC_CHK_LOG) == BST_CHECKED; + options.log_csv = IsDlgButtonChecked(m_hwnd, IDC_CHK_LOGCSV) == BST_CHECKED; + GetDlgItemText(m_hwnd, IDC_ED_FILENAME, options.log_filename, _countof(options.log_filename)); - int index = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)-1, (LPARAM)add_edit_addr.pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETCURSEL, (WPARAM)index, 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)index, (LPARAM) & (temp_list.back())); + options.no_test_icon = IsDlgButtonChecked(m_hwnd, IDC_CHK_NOTESTICON) == BST_CHECKED; - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTREM), TRUE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTEDIT), TRUE); + options.indent = SendDlgItemMessage(m_hwnd, IDC_SP_INDENT, UDM_GETPOS, 0, 0); + options.row_height = SendDlgItemMessage(m_hwnd, IDC_SP_ROWHEIGHT, UDM_GETPOS, 0, 0); - int sel = (int)temp_list.size() - 1; - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), (sel > 0)); - int count = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCOUNT, 0, 0); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), (sel < count - 1)); + uint32_t new_retries = GetDlgItemInt(m_hwnd, IDC_RPT, &trans_success, FALSE); + if (trans_success) + options.retries = new_retries; - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - break; + bool new_attach = (IsDlgButtonChecked(m_hwnd, IDC_CHK_ATTACH) == BST_CHECKED); + if (!ServiceExists(MS_CLIST_FRAMES_ADDFRAME) && options.attach_to_clist != new_attach) + AttachToClist(new_attach); - case IDC_BTN_DESTREM: - { - int sel = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCURSEL, 0, 0); - if (sel != LB_ERR) { - PINGADDRESS *item = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel, 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)sel, 0); - temp_list.remove(*item); - } + options.attach_to_clist = new_attach; - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTREM), FALSE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTEDIT), FALSE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), FALSE); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), FALSE); + SaveOptions(); - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - RefreshWindow(0, 0); - } - break; + RefreshWindow(); - case IDC_BTN_DESTDOWN: - { - int sel2 = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCURSEL, 0, 0); - if (sel2 != LB_ERR) { - PINGADDRESS *item = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel2, 0), - *item2 = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel2 + 1, 0); - if (item && item2) { - add_edit_addr = *item; - *item = *item2; - *item2 = add_edit_addr; - - // keep indexes the same, as they're used for sorting the binary tree - int index = item->index, index2 = item2->index; - item->index = index2; - item2->index = index; - - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)sel2, 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)sel2, (LPARAM)item->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)sel2, (LPARAM)item); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)(sel2 + 1), 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)(sel2 + 1), (LPARAM)item2->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)(sel2 + 1), (LPARAM)item2); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETCURSEL, (WPARAM)(sel2 + 1), 0); - - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), (sel2 + 1 > 0)); - int count = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCOUNT, 0, 0); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), (sel2 + 1 < count - 1)); - - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - } - } - break; + Log(L"options changed"); - case IDC_BTN_DESTUP: - { - int sel2 = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCURSEL, 0, 0); - if (sel2 != LB_ERR) { - PINGADDRESS *item = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel2, 0), - *item2 = (PINGADDRESS *)SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETITEMDATA, sel2 - 1, 0); + if (hWakeEvent) + SetEvent(hWakeEvent); + return true; + } - if (item && item2) { - add_edit_addr = *item; - *item = *item2; - *item2 = add_edit_addr; + void onChange_Log(CCtrlButton *) + { + bool bEnabled = IsDlgButtonChecked(m_hwnd, IDC_CHK_LOG); + EnableWindow(GetDlgItem(m_hwnd, IDC_ED_FILENAME), bEnabled); + EnableWindow(GetDlgItem(m_hwnd, IDC_BTN_LOGBROWSE), bEnabled); + EnableWindow(GetDlgItem(m_hwnd, IDC_CHK_LOGCSV), bEnabled); + } - // keep indexes the same, as they're used for sorting the binary tree - int index = item->index, index2 = item2->index; - item->index = index2; - item2->index = index; + void onClick_ViewLog(CCtrlButton *) + { + CallService(MODULENAME "/ViewLogData", 0, 0); + } - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)sel2, 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)sel2, (LPARAM)item->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)sel2, (LPARAM)item); + void onClick_Browse(CCtrlButton *) + { + wchar_t filter[MAX_PATH]; + mir_snwprintf(filter, L"%s%c*.txt%c%s%c*.*%c", TranslateT("Text Files (*.txt)"), 0, 0, TranslateT("All Files"), 0, 0); + OPENFILENAME ofn = { 0 }; + ofn.lStructSize = sizeof(ofn); + ofn.lpstrFile = options.log_filename; + ofn.hwndOwner = m_hwnd; + ofn.nMaxFile = _countof(options.log_filename); + ofn.lpstrTitle = TranslateT("Open log file"); + ofn.lpstrFilter = filter; + ofn.nFilterIndex = 1; + ofn.lpstrFileTitle = nullptr; + ofn.nMaxFileTitle = 0; + ofn.lpstrInitialDir = nullptr; + ofn.Flags = OFN_PATHMUSTEXIST; + + if (GetOpenFileName(&ofn) == TRUE) { + SetDlgItemText(m_hwnd, IDC_ED_FILENAME, ofn.lpstrFile); + SendMessage(GetParent(m_hwnd), PSM_CHANGED, 0, 0); + } + } +}; - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_DELETESTRING, (WPARAM)(sel2 - 1), 0); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_INSERTSTRING, (WPARAM)(sel2 - 1), (LPARAM)item2->pszLabel); - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETITEMDATA, (WPARAM)(sel2 - 1), (LPARAM)item2); +///////////////////////////////////////////////////////////////////////////////////////// +// ping hosts list window - SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_SETCURSEL, (WPARAM)(sel2 - 1), 0); +class CHostOptsDlg : public CDlgBase +{ + CCtrlButton btnAdd, btnEdit, btnRemove, btnDown, btnUp; + CCtrlListBox m_list; + +public: + CHostOptsDlg() : + CDlgBase(g_plugin, IDD_DIALOG2), + m_list(this, IDC_LST_DEST), + btnUp(this, IDC_BTN_DESTUP), + btnAdd(this, IDC_BTN_DESTADD), + btnDown(this, IDC_BTN_DESTDOWN), + btnEdit(this, IDC_BTN_DESTEDIT), + btnRemove(this, IDC_BTN_DESTREM) + { + m_list.OnSelChange = Callback(this, &CHostOptsDlg::onSelChange_List); + + btnAdd.OnClick = Callback(this, &CHostOptsDlg::onClick_Add); + btnEdit.OnClick = Callback(this, &CHostOptsDlg::onClick_Edit); + btnRemove.OnClick = Callback(this, &CHostOptsDlg::onClick_Remove); + + btnUp.OnClick = Callback(this, &CHostOptsDlg::onClick_Up); + btnDown.OnClick = Callback(this, &CHostOptsDlg::onClick_Down); + } - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTUP), (sel2 - 1 > 0)); - int count = SendDlgItemMessage(hwndDlg, IDC_LST_DEST, LB_GETCOUNT, 0, 0); - EnableWindow(GetDlgItem(hwndDlg, IDC_BTN_DESTDOWN), (sel2 - 1 < count - 1)); + bool OnInitDialog() override + { + mir_cslock lck(data_list_cs); + temp_list = data_list; - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); - } - } - } + for (auto &it : temp_list) + m_list.InsertString(it.pszLabel, -1, (LPARAM)&it); - break; + return true; + } + + bool OnApply() override + { + SetAndSavePingList(temp_list); + GetPingList(temp_list); + + RefreshWindow(); + + // the following will be affected due to list rebuild event + if (hWakeEvent) + SetEvent(hWakeEvent); + return true; + } + + void onSelChange_List(CCtrlListBox*) + { + int sel = m_list.GetCurSel(); + if (sel != LB_ERR) { + btnEdit.Enable(); + btnRemove.Enable(); + + btnUp.Enable(sel > 0); + btnDown.Enable(sel < m_list.GetCount() - 1); + } + } + + void onClick_Edit(CCtrlButton*) + { + int sel = m_list.GetCurSel(); + if (sel != LB_ERR) { + PINGADDRESS *item = (PINGADDRESS *)m_list.GetItemData(sel); + PINGADDRESS temp = *item; + if (Edit(m_hwnd, temp)) { + *item = temp; + m_list.DeleteString(sel); + m_list.InsertString(item->pszLabel, sel, (LPARAM)item); + m_list.SetCurSel(sel); + + onSelChange_List(0); + NotifyChange(); } } - if (LOWORD(wParam) == IDC_BGCOL - || LOWORD(wParam) == IDC_SP_INDENT || LOWORD(wParam) == IDC_SP_ROWHEIGHT) { - SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0); + } + + void onClick_Add(CCtrlButton *) + { + memset(&add_edit_addr, 0, sizeof(add_edit_addr)); + add_edit_addr.cbSize = sizeof(add_edit_addr); + add_edit_addr.port = -1; + add_edit_addr.set_status = ID_STATUS_ONLINE; + add_edit_addr.get_status = ID_STATUS_OFFLINE; + add_edit_addr.status = PS_NOTRESPONDING; + add_edit_addr.item_id = 0; + add_edit_addr.index = (int)temp_list.size(); + + if (DialogBox(g_plugin.getInst(), MAKEINTRESOURCE(IDD_DIALOG3), m_hwnd, DlgProcDestEdit) == IDOK) { + temp_list.push_back(add_edit_addr); + + int index = m_list.InsertString(add_edit_addr.pszLabel, -1, (LPARAM)&temp_list.back()); + m_list.SetCurSel(index); + + onSelChange_List(0); + NotifyChange(); } - break; - - case WM_NOTIFY: - if (((LPNMHDR)lParam)->code == PSN_APPLY) { - CallService(MODULENAME "/SetAndSavePingList", (WPARAM)&temp_list, 0); - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&temp_list); - // the following will be affected due to list rebuild event - //if(hWakeEvent) SetEvent(hWakeEvent); - return TRUE; + } + + void onClick_Remove(CCtrlButton *) + { + int sel = m_list.GetCurSel(); + if (sel != LB_ERR) { + PINGADDRESS *item = (PINGADDRESS *)m_list.GetItemData(sel); + m_list.DeleteString(sel); + temp_list.remove(*item); } - break; + btnEdit.Disable(); + btnRemove.Disable(); + btnDown.Disable(); + btnUp.Disable(); + + NotifyChange(); + RefreshWindow(); } - return FALSE; -} + + void onClick_Down(CCtrlButton *) + { + int sel2 = m_list.GetCurSel(); + if (sel2 == LB_ERR) + return; + + PINGADDRESS *item = (PINGADDRESS *)m_list.GetItemData(sel2), *item2 = (PINGADDRESS *)m_list.GetItemData(sel2 + 1); + if (item && item2) { + add_edit_addr = *item; + *item = *item2; + *item2 = add_edit_addr; + + // keep indexes the same, as they're used for sorting the binary tree + int index = item->index, index2 = item2->index; + item->index = index2; + item2->index = index; + + m_list.DeleteString(sel2); + m_list.InsertString(item->pszLabel, sel2, (LPARAM)item); + + m_list.DeleteString(sel2 + 1); + m_list.InsertString(item2->pszLabel, sel2 + 1, (LPARAM)item2); + + m_list.SetCurSel(sel2 + 1); + + btnUp.Enable(sel2 + 1 > 0); + btnDown.Enable(sel2 + 1 < m_list.GetCount() - 1); + NotifyChange(); + } + } + + void onClick_Up(CCtrlButton *) + { + int sel2 = SendDlgItemMessage(m_hwnd, IDC_LST_DEST, LB_GETCURSEL, 0, 0); + if (sel2 == LB_ERR) + return; + + PINGADDRESS *item = (PINGADDRESS *)m_list.GetItemData(sel2), *item2 = (PINGADDRESS *)m_list.GetItemData(sel2 - 1); + if (item && item2) { + add_edit_addr = *item; + *item = *item2; + *item2 = add_edit_addr; + + // keep indexes the same, as they're used for sorting the binary tree + int index = item->index, index2 = item2->index; + item->index = index2; + item2->index = index; + + m_list.DeleteString(sel2); + m_list.InsertString(item->pszLabel, sel2, (LPARAM)item); + + m_list.DeleteString(sel2 - 1); + m_list.InsertString(item2->pszLabel, sel2 - 1, (LPARAM)item2); + + m_list.SetCurSel(sel2 - 1); + + btnUp.Enable(sel2 - 1 > 0); + btnDown.Enable(sel2 - 1 < m_list.GetCount() - 1); + NotifyChange(); + } + } +}; int PingOptInit(WPARAM wParam, LPARAM) { @@ -485,13 +453,11 @@ int PingOptInit(WPARAM wParam, LPARAM) odp.szTitle.w = LPGENW("Ping"); odp.szTab.w = LPGENW("Settings"); - odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG1); - odp.pfnDlgProc = DlgProcOpts; + odp.pDialog = new CMainOptsDlg(); g_plugin.addOptions(wParam, &odp); odp.szTab.w = LPGENW("Hosts"); - odp.pszTemplate = MAKEINTRESOURCEA(IDD_DIALOG2); - odp.pfnDlgProc = DlgProcOpts2; + odp.pDialog = new CHostOptsDlg(); g_plugin.addOptions(wParam, &odp); return 0; } @@ -501,7 +467,6 @@ void LoadOptions() options.ping_period = g_plugin.getDword("PingPeriod", DEFAULT_PING_PERIOD); options.ping_timeout = g_plugin.getDword("PingTimeout", DEFAULT_PING_TIMEOUT); - CallService(MODULENAME "/SetPingTimeout", (WPARAM)options.ping_timeout, 0); options.show_popup = (g_plugin.getByte("ShowPopup", DEFAULT_SHOW_POPUP ? 1 : 0) == 1); options.show_popup2 = (g_plugin.getByte("ShowPopup2", DEFAULT_SHOW_POPUP2 ? 1 : 0) == 1); options.block_reps = (g_plugin.getByte("BlockReps", DEFAULT_BLOCK_REPS ? 1 : 0) == 1); @@ -526,7 +491,7 @@ void SaveOptions() { g_plugin.setDword("PingPeriod", options.ping_period); g_plugin.setDword("PingTimeout", options.ping_timeout); - CallService(MODULENAME "/SetPingTimeout", (WPARAM)options.ping_timeout, 0); + g_plugin.setByte("ShowPopup", options.show_popup ? 1 : 0); g_plugin.setByte("ShowPopup2", options.show_popup2 ? 1 : 0); g_plugin.setByte("BlockReps", options.block_reps ? 1 : 0); diff --git a/plugins/Ping/src/ping.cpp b/plugins/Ping/src/ping.cpp index c9a63554a9..30e93614d5 100644 --- a/plugins/Ping/src/ping.cpp +++ b/plugins/Ping/src/ping.cpp @@ -36,13 +36,6 @@ static void CreatePluginServices() CreateServiceFunction(MODULENAME "/DblClick", DblClick); // list - CreateServiceFunction(MODULENAME "/ClearPingList", ClearPingList); - CreateServiceFunction(MODULENAME "/GetPingList", GetPingList); - CreateServiceFunction(MODULENAME "/SetPingList", SetPingList); - CreateServiceFunction(MODULENAME "/SetAndSavePingList", SetAndSavePingList); - CreateServiceFunction(MODULENAME "/LoadPingList", LoadPingList); - CreateServiceFunction(MODULENAME "/SavePingList", SavePingList); - reload_event_handle = CreateHookableEvent(MODULENAME "/ListReload"); //log @@ -95,7 +88,7 @@ static int OnModulesLoaded(WPARAM, LPARAM) InitList(); - CallService(MODULENAME "/LoadPingList", 0, 0); + LoadPingList(); graphs_init(); @@ -139,7 +132,7 @@ int CMPlugin::Load() int CMPlugin::Unload() { - SavePingList(0, 0); + SavePingList(); Log(L"stop"); return 0; diff --git a/plugins/Ping/src/pinggraph.cpp b/plugins/Ping/src/pinggraph.cpp index 68850b73bd..a3130faccd 100644 --- a/plugins/Ping/src/pinggraph.cpp +++ b/plugins/Ping/src/pinggraph.cpp @@ -26,8 +26,8 @@ LRESULT CALLBACK GraphWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPar { bool found = false; mir_cslock lck(data_list_cs); - for (pinglist_it i = data_list.begin(); i != data_list.end(); ++i) { - if (i->item_id == wd->item_id) { + for (auto &it : data_list) { + if (it.item_id == wd->item_id) { wd->list = history_map[wd->item_id]; found = true; break; @@ -284,7 +284,7 @@ INT_PTR ShowGraph(WPARAM wParam, LPARAM lParam) // save window positions, close windows void graphs_cleanup() { - int list_size = GetListSize(0, 0); + int list_size = GetListSize(); char buff[64]; HWND hwnd; @@ -303,15 +303,17 @@ void graphs_cleanup() void graphs_init() { PINGLIST pl; - char buff[64]; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - mir_snprintf(buff, "WindowHandle%d", i->item_id); // clean up from possible crash + GetPingList(pl); + + for (auto &it : pl) { + char buff[64]; + mir_snprintf(buff, "WindowHandle%d", it.item_id); // clean up from possible crash g_plugin.setDword(buff, 0); - mir_snprintf(buff, "WindowWasOpen%d", i->item_id); // restore windows that were open on shutdown + + mir_snprintf(buff, "WindowWasOpen%d", it.item_id); // restore windows that were open on shutdown if (g_plugin.getByte(buff, 0)) { g_plugin.setByte(buff, 0); - ShowGraph((WPARAM)i->item_id, (LPARAM)i->pszLabel); + ShowGraph(it.item_id, (LPARAM)it.pszLabel); } } } diff --git a/plugins/Ping/src/pinglist.cpp b/plugins/Ping/src/pinglist.cpp index d10a51174a..3d62301a8b 100644 --- a/plugins/Ping/src/pinglist.cpp +++ b/plugins/Ping/src/pinglist.cpp @@ -28,26 +28,6 @@ const bool PINGADDRESS::operator<(const PINGADDRESS &b) const return index < b.index; } -// lParam is address of pointer to a std::list<PINGADDRESS> -// copies data into this structure -INT_PTR GetPingList(WPARAM, LPARAM lParam) -{ - PINGLIST *pa = (PINGLIST *)lParam; - - mir_cslock lck(list_cs); - *pa = list_items; - - return 0; -} - -INT_PTR GetListSize(WPARAM, LPARAM) -{ - INT_PTR ret = 0; - mir_cslock lck(list_cs); - ret = list_items.size(); - return ret; -} - void write_ping_address(PINGADDRESS &i) { char buff[16]; @@ -81,9 +61,9 @@ void write_ping_address(PINGADDRESS &i) void write_ping_addresses() { int index = 0; - for (pinglist_it i = list_items.begin(); i != list_items.end(); ++i, index++) { - i->index = index; - write_ping_address(*i); + for (auto &it : list_items) { + it.index = index; + write_ping_address(it); } // mark further destinations in the DB as invalid @@ -177,59 +157,60 @@ void read_ping_addresses() } } -INT_PTR LoadPingList(WPARAM, LPARAM) +///////////////////////////////////////////////////////////////////////////////////////// +// copies data into this structure + +void GetPingList(PINGLIST &list) { mir_cslock lck(list_cs); - read_ping_addresses(); - NotifyEventHooks(reload_event_handle, 0, 0); - return 0; + list = list_items; } -// wParam is zero -// lParam is zero -INT_PTR SavePingList(WPARAM, LPARAM) +int GetListSize() { mir_cslock lck(list_cs); - write_ping_addresses(); - - return 0; + return (int)list_items.size(); } -// wParam is address of a PINGLIST structure to replace the current one -// lParam is zero -INT_PTR SetPingList(WPARAM wParam, LPARAM) +void LoadPingList() { - PINGLIST *pli = (PINGLIST *)wParam; - - mir_cslock lck(list_cs); - list_items = *pli; + { mir_cslock lck(list_cs); + read_ping_addresses(); + } + NotifyEventHooks(reload_event_handle, 0, 0); - - return 0; } -// wParam is address of a PINGLIST structure to replace the current one -// lParam is zero -INT_PTR SetAndSavePingList(WPARAM wParam, LPARAM) +void SavePingList() { - PINGLIST *pli = (PINGLIST *)wParam; - mir_cslock lck(list_cs); - - // set new list - list_items = *pli; write_ping_addresses(); +} + +void SetPingList(const PINGLIST &list) +{ + { mir_cslock lck(list_cs); + list_items = list; + } NotifyEventHooks(reload_event_handle, 0, 0); +} + +void SetAndSavePingList(const PINGLIST &list) +{ + { mir_cslock lck(list_cs); + list_items = list; + write_ping_addresses(); + } - return 0; + NotifyEventHooks(reload_event_handle, 0, 0); } -INT_PTR ClearPingList(WPARAM, LPARAM) +void ClearPingList() { - mir_cslock lck(list_cs); - list_items.clear(); + { mir_cslock lck(list_cs); + list_items.clear(); + } NotifyEventHooks(reload_event_handle, 0, 0); - return 0; } diff --git a/plugins/Ping/src/pinglist.h b/plugins/Ping/src/pinglist.h index b01dcd4a17..0b9bfe15b8 100644 --- a/plugins/Ping/src/pinglist.h +++ b/plugins/Ping/src/pinglist.h @@ -1,18 +1,49 @@ #ifndef _PINGLIST_H #define _PINGLIST_H -INT_PTR LoadPingList(WPARAM wParam, LPARAM lParam); -INT_PTR GetPingList(WPARAM wParam, LPARAM lParam); -INT_PTR SavePingList(WPARAM wParam, LPARAM lParam); -INT_PTR SetPingList(WPARAM wParam, LPARAM lParam); // use when you modified db yourself -INT_PTR SetAndSavePingList(WPARAM wParam, LPARAM lParam); -INT_PTR ClearPingList(WPARAM wParam, LPARAM lParam); -INT_PTR GetListSize(WPARAM wParam, LPARAM lParam); +#define PS_RESPONDING 1 +#define PS_NOTRESPONDING 2 +#define PS_TESTING 3 +#define PS_DISABLED 4 + +#define MAX_PINGADDRESS_STRING_LENGTH 256 + +struct PINGADDRESS +{ + int cbSize; //size in bytes of this structure + uint32_t item_id; + wchar_t pszName[MAX_PINGADDRESS_STRING_LENGTH]; //IP address or domain name + wchar_t pszLabel[MAX_PINGADDRESS_STRING_LENGTH]; + bool responding; + int status; + short round_trip_time; + int miss_count; + int port; // -1 for ICMP, non-zero for TCP + char pszProto[MAX_PINGADDRESS_STRING_LENGTH]; + wchar_t pszCommand[MAX_PATH]; + wchar_t pszParams[MAX_PATH]; + unsigned int get_status; // on success, if status equals this + unsigned int set_status; // set it to this + int index; + + const bool operator==(const PINGADDRESS &b) const; + const bool operator<(const PINGADDRESS &b) const; +}; + +typedef std::list<PINGADDRESS> PINGLIST; +typedef std::list<PINGADDRESS>::iterator pinglist_it; + +void ClearPingList(void); +int GetListSize(void); +void GetPingList(PINGLIST &list); +void LoadPingList(void); +void SavePingList(void); +void SetPingList(const PINGLIST &list); // use when you modified db yourself +void SetAndSavePingList(const PINGLIST &list); // only call with list_cs locked! void write_ping_addresses(); - BOOL changing_clist_handle(); void set_changing_clist_handle(BOOL flag); #endif diff --git a/plugins/Ping/src/pingthread.cpp b/plugins/Ping/src/pingthread.cpp index c4f8ebb6e4..f8ad707c81 100644 --- a/plugins/Ping/src/pingthread.cpp +++ b/plugins/Ping/src/pingthread.cpp @@ -105,20 +105,25 @@ void __cdecl sttCheckStatusThreadProc(void *) { mir_cslock lck(data_list_cs); size_t c = 0; - for (pinglist_it i = data_list.begin(); i != data_list.end() && c <= index; ++i, c++) { + for (auto &it : data_list) { + if (c > index) + break; + if (c == index) { // copy just what we need - i.e. not history, not command - pa.get_status = i->get_status; - pa.item_id = i->item_id; - pa.miss_count = i->miss_count; - pa.port = i->port; - mir_wstrncpy(pa.pszLabel, i->pszLabel, _countof(pa.pszLabel)); - mir_wstrncpy(pa.pszName, i->pszName, _countof(pa.pszName)); - mir_strncpy(pa.pszProto, i->pszProto, _countof(pa.pszProto)); - pa.set_status = i->set_status; - pa.status = i->status; + pa.get_status = it.get_status; + pa.item_id = it.item_id; + pa.miss_count = it.miss_count; + pa.port = it.port; + mir_wstrncpy(pa.pszLabel, it.pszLabel, _countof(pa.pszLabel)); + mir_wstrncpy(pa.pszName, it.pszName, _countof(pa.pszName)); + mir_strncpy(pa.pszProto, it.pszProto, _countof(pa.pszProto)); + pa.set_status = it.set_status; + pa.status = it.status; break; } + + c++; } } @@ -128,9 +133,9 @@ void __cdecl sttCheckStatusThreadProc(void *) if (pa.status != PS_DISABLED) { if (!options.no_test_icon) { mir_cslock lck(data_list_cs); - for (pinglist_it i = data_list.begin(); i != data_list.end(); ++i) - if (i->item_id == pa.item_id) - i->status = PS_TESTING; + for (auto &it : data_list) + if (it.item_id == pa.item_id) + it.status = PS_TESTING; InvalidateRect(list_hwnd, nullptr, FALSE); } @@ -143,17 +148,17 @@ void __cdecl sttCheckStatusThreadProc(void *) { mir_cslock lck(data_list_cs); - for (pinglist_it i = data_list.begin(); i != data_list.end(); ++i) { - if (i->item_id == pa.item_id) { - i->responding = pa.responding; - i->round_trip_time = pa.round_trip_time; - history_entry.first = i->round_trip_time; + for (auto &it : data_list) { + if (it.item_id == pa.item_id) { + it.responding = pa.responding; + it.round_trip_time = pa.round_trip_time; + history_entry.first = it.round_trip_time; history_entry.second = time(0); - history_map[i->item_id].push_back(history_entry); + history_map[it.item_id].push_back(history_entry); // maintain history (-1 represents no response) - while (history_map[i->item_id].size() >= MAX_HISTORY) - //history_map[i->item_id].pop_front(); - history_map[i->item_id].remove(history_map[i->item_id].begin().val()); + while (history_map[it.item_id].size() >= MAX_HISTORY) + //history_map[it.item_id].pop_front(); + history_map[it.item_id].remove(history_map[it.item_id].begin().val()); if (pa.responding) { if (pa.miss_count > 0) @@ -170,9 +175,8 @@ void __cdecl sttCheckStatusThreadProc(void *) pa.status = PS_NOTRESPONDING; } - i->miss_count = pa.miss_count; - i->status = pa.status; - + it.miss_count = pa.miss_count; + it.status = pa.status; break; } } @@ -237,7 +241,7 @@ int FillList(WPARAM, LPARAM) Log(L"ping address list reload"); PINGLIST pl; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); + GetPingList(pl); SendMessage(list_hwnd, WM_SETREDRAW, FALSE, 0); { @@ -622,7 +626,7 @@ LRESULT CALLBACK FrameWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPar if (Edit(hwnd, itemData)) { mir_cslock lck(data_list_cs); *temp = itemData; - CallService(MODULENAME "/SetAndSavePingList", (WPARAM)&data_list, 0); + SetAndSavePingList(data_list); } } } @@ -730,14 +734,13 @@ int ReloadFont(WPARAM, LPARAM) SendMessage(list_hwnd, WM_SETFONT, (WPARAM)hFont, TRUE); bk_col = Colour_GetW(bk_col_id); - RefreshWindow(0, 0); + RefreshWindow(); return 0; } -int RefreshWindow(WPARAM, LPARAM) +int RefreshWindow() { - InvalidateRect(list_hwnd, nullptr, TRUE); - InvalidateRect(hpwnd, nullptr, TRUE); + SendMessageW(hpwnd, WM_SIZE, 0, 0); return 0; } diff --git a/plugins/Ping/src/pingthread.h b/plugins/Ping/src/pingthread.h index 37b50e308c..dc68a25a30 100644 --- a/plugins/Ping/src/pingthread.h +++ b/plugins/Ping/src/pingthread.h @@ -3,7 +3,7 @@ int FillList(WPARAM wParam, LPARAM lParam); -int RefreshWindow(WPARAM wParam, LPARAM lParam); +int RefreshWindow(); void UpdateFrame(); void AttachToClist(bool attach); diff --git a/plugins/Ping/src/stdafx.h b/plugins/Ping/src/stdafx.h index 7dfa5e5b09..180135f0e8 100644 --- a/plugins/Ping/src/stdafx.h +++ b/plugins/Ping/src/stdafx.h @@ -51,8 +51,6 @@ #define DEFAULT_NO_TEST_ICON true #define DEFAULT_ATTACH_TO_CLIST false -#define MAX_PINGADDRESS_STRING_LENGTH 256 - struct CMPlugin : public PLUGIN<CMPlugin> { CMPlugin(); @@ -82,38 +80,9 @@ struct HistPair { }; typedef LinkedList< HistPair > HistoryList; - -#define PS_RESPONDING 1 -#define PS_NOTRESPONDING 2 -#define PS_TESTING 3 -#define PS_DISABLED 4 - -struct PINGADDRESS { - int cbSize; //size in bytes of this structure - uint32_t item_id; - wchar_t pszName[MAX_PINGADDRESS_STRING_LENGTH]; //IP address or domain name - wchar_t pszLabel[MAX_PINGADDRESS_STRING_LENGTH]; - bool responding; - int status; - short round_trip_time; - int miss_count; - int port; // -1 for ICMP, non-zero for TCP - char pszProto[MAX_PINGADDRESS_STRING_LENGTH]; - wchar_t pszCommand[MAX_PATH]; - wchar_t pszParams[MAX_PATH]; - unsigned int get_status; // on success, if status equals this - unsigned int set_status; // set it to this - int index; - - const bool operator==(const PINGADDRESS &b) const; - const bool operator<(const PINGADDRESS &b) const; -}; - #include "options.h" typedef Map<uint32_t, HistoryList> HistoryMap; -typedef std::list<PINGADDRESS> PINGLIST; -typedef std::list<PINGADDRESS>::iterator pinglist_it; extern HNETLIBUSER hNetlibUser; diff --git a/plugins/Ping/src/utils.cpp b/plugins/Ping/src/utils.cpp index 3eedce7b9e..cf263026b5 100644 --- a/plugins/Ping/src/utils.cpp +++ b/plugins/Ping/src/utils.cpp @@ -3,10 +3,9 @@ LRESULT CALLBACK NullWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { - case WM_COMMAND: { + case WM_COMMAND: PUDeletePopup(hWnd); break; - } case WM_CONTEXTMENU: PUDeletePopup(hWnd); @@ -32,7 +31,6 @@ void __stdcall ShowPopup(wchar_t *line1, wchar_t *line2, int) INT_PTR PluginPing(WPARAM, LPARAM lParam) { PINGADDRESS *pa = (PINGADDRESS *)lParam; - if (pa->port == -1) { // ICMP echo if (use_raw_ping) { @@ -61,7 +59,7 @@ INT_PTR PluginPing(WPARAM, LPARAM lParam) conn.timeout = options.ping_timeout; HNETLIBCONN s = Netlib_OpenConnection(hNetlibUser, &conn); - mir_free((void*)conn.szHost); + mir_free((void *)conn.szHost); clock_t end_tcp = clock(); @@ -101,65 +99,66 @@ INT_PTR PluginPing(WPARAM, LPARAM lParam) return 0; } -INT_PTR PingDisableAll(WPARAM, LPARAM) { +INT_PTR PingDisableAll(WPARAM, LPARAM) +{ PINGLIST pl; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - i->status = PS_DISABLED; - i->miss_count = 0; + GetPingList(pl); + + for (auto &it: pl) { + it.status = PS_DISABLED; + it.miss_count = 0; } - CallService(MODULENAME "/SetPingList", (WPARAM)&pl, 0); + SetPingList(pl); return 0; } -INT_PTR PingEnableAll(WPARAM, LPARAM) { +INT_PTR PingEnableAll(WPARAM, LPARAM) +{ PINGLIST pl; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - if (i->status == PS_DISABLED) { - i->status = PS_NOTRESPONDING; - } - } - CallService(MODULENAME "/SetPingList", (WPARAM)&pl, 0); + GetPingList(pl); + + for (auto &it : pl) + if (it.status == PS_DISABLED) + it.status = PS_NOTRESPONDING; + + SetPingList(pl); return 0; } -INT_PTR ToggleEnabled(WPARAM wParam, LPARAM) { - int retval = 0; +INT_PTR ToggleEnabled(WPARAM wParam, LPARAM) +{ PINGLIST pl; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - if (i->item_id == (uint32_t)wParam) { + GetPingList(pl); - if (i->status == PS_DISABLED) - i->status = PS_NOTRESPONDING; + for (auto &it : pl) { + if (it.item_id == (uint32_t)wParam) { + + if (it.status == PS_DISABLED) + it.status = PS_NOTRESPONDING; else { - i->status = PS_DISABLED; - i->miss_count = 0; - retval = 1; + it.status = PS_DISABLED; + it.miss_count = 0; } } } - CallService(MODULENAME "/SetPingList", (WPARAM)&pl, 0); + SetPingList(pl); return 0; } INT_PTR EditContact(WPARAM wParam, LPARAM) { PINGLIST pl; - HWND hwndList = g_clistApi.hwndContactList; + GetPingList(pl); - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - if (i->item_id == (uint32_t)wParam) { - - add_edit_addr = *i; + HWND hwndList = g_clistApi.hwndContactList; + for (auto &it : pl) { + if (it.item_id == (uint32_t)wParam) { + add_edit_addr = it; if (DialogBox(g_plugin.getInst(), MAKEINTRESOURCE(IDD_DIALOG3), hwndList, DlgProcDestEdit) == IDOK) { - - *i = add_edit_addr; - CallService(MODULENAME "/SetAndSavePingList", (WPARAM)&pl, 0); + it = add_edit_addr; + SetAndSavePingList(pl); return 0; } } @@ -167,13 +166,15 @@ INT_PTR EditContact(WPARAM wParam, LPARAM) return 1; } -INT_PTR DblClick(WPARAM wParam, LPARAM) { +INT_PTR DblClick(WPARAM wParam, LPARAM) +{ PINGLIST pl; - CallService(MODULENAME "/GetPingList", 0, (LPARAM)&pl); - for (pinglist_it i = pl.begin(); i != pl.end(); ++i) { - if (i->item_id == (uint32_t)wParam) { - if (mir_wstrlen(i->pszCommand)) { - ShellExecute(nullptr, L"open", i->pszCommand, i->pszParams, nullptr, SW_SHOW); + GetPingList(pl); + + for (auto &it : pl) { + if (it.item_id == (uint32_t)wParam) { + if (mir_wstrlen(it.pszCommand)) { + ShellExecute(nullptr, L"open", it.pszCommand, it.pszParams, nullptr, SW_SHOW); } else { return CallService(MODULENAME "/ToggleEnabled", wParam, 0); @@ -183,8 +184,8 @@ INT_PTR DblClick(WPARAM wParam, LPARAM) { return 0; } - -void import_ping_address(int index, PINGADDRESS &pa) { +void import_ping_address(int index, PINGADDRESS &pa) +{ DBVARIANT dbv; char buf[256]; mir_snprintf(buf, "Address%d", index); @@ -192,16 +193,14 @@ void import_ping_address(int index, PINGADDRESS &pa) { mir_wstrncpy(pa.pszName, dbv.pwszVal, _countof(pa.pszName)); db_free(&dbv); } - else - mir_wstrncpy(pa.pszName, TranslateT("Unknown Address"), _countof(pa.pszName)); + else mir_wstrncpy(pa.pszName, TranslateT("Unknown Address"), _countof(pa.pszName)); mir_snprintf(buf, "Label%d", index); if (!db_get_ws(0, "PingPlug", buf, &dbv)) { mir_wstrncpy(pa.pszLabel, dbv.pwszVal, _countof(pa.pszLabel)); db_free(&dbv); } - else - mir_wstrncpy(pa.pszLabel, TranslateT("Unknown"), _countof(pa.pszLabel)); + else mir_wstrncpy(pa.pszLabel, TranslateT("Unknown"), _countof(pa.pszLabel)); mir_snprintf(buf, "Port%d", index); pa.port = (int)db_get_dw(0, "PingPlug", buf, -1); @@ -215,9 +214,7 @@ void import_ping_address(int index, PINGADDRESS &pa) { mir_snprintf(buf, "Status2%d", index); pa.get_status = db_get_w(0, "PingPlug", buf, ID_STATUS_OFFLINE); } - else - pa.pszProto[0] = '\0'; - + else pa.pszProto[0] = '\0'; pa.responding = false; pa.round_trip_time = 0; @@ -242,8 +239,7 @@ void import_ping_addresses() mir_cslock lck(list_cs); list_items.clear(); - for (int index = 0; index < count; index++) - { + for (int index = 0; index < count; index++) { import_ping_address(index, pa); list_items.push_back(pa); } @@ -266,7 +262,7 @@ int ReloadIcons(WPARAM, LPARAM) hIconTesting = IcoLib_GetIcon("ping_testing"); hIconDisabled = IcoLib_GetIcon("ping_disabled"); - RefreshWindow(0, 0); + RefreshWindow(); return 0; } -- cgit v1.2.3