From 0fce5791122782316c93128b9fa3ab7413a690b4 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 15 Sep 2017 01:15:34 -0700 Subject: [PATCH] Fix (quick)connect page. --- Lite/Address.cpp | 13 +++++++++++ Lite/Address.h | 5 ++-- Lite/AddressDlg.cpp | 37 ++++++++++++++++++++++++++++-- Lite/AddressDlg.h | 4 +++- Lite/ConnectPage.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++- Lite/ConnectPage.h | 4 ++++ Lite/ListDlg.cpp | 13 ++--------- Lite/MainFrm.cpp | 13 ++++------- Lite/TermView.cpp | 4 ++-- Lite/Websocket.cpp | 7 +++--- 10 files changed, 122 insertions(+), 32 deletions(-) diff --git a/Lite/Address.cpp b/Lite/Address.cpp index 513be59..2811c5f 100644 --- a/Lite/Address.cpp +++ b/Lite/Address.cpp @@ -40,4 +40,17 @@ CString CAddress::Description() const s.Format(_T("[CAddress protocol=%s server=%s port=%d path=%s url=%s]"), protocol_, server_, port_, path_, url_); return s; +} + +unsigned short CAddress::DefaultPort() const +{ + const CString& protocol = Protocol(); + if (protocol == "telnet" || protocol == "bbs") + return 23; + else if (protocol == "http" || protocol == "ws") + return 80; + else if (protocol == "https" || protocol == "wss") + return 443; + else + return 0; } \ No newline at end of file diff --git a/Lite/Address.h b/Lite/Address.h index c41e2e8..b45bf11 100644 --- a/Lite/Address.h +++ b/Lite/Address.h @@ -17,10 +17,11 @@ class CAddress const CString& URL() const { return url_; } const CString& Protocol() const { assert(valid_); return protocol_; } const CString& Server() const { assert(valid_); return server_; } - unsigned short Port(unsigned short default_port = 0) const { + unsigned short Port() const { assert(valid_); - return port_ ? port_ : default_port; + return port_ ? port_ : DefaultPort(); } + unsigned short DefaultPort() const; const CString& Path() const { assert(valid_); return path_; } CString Description() const; diff --git a/Lite/AddressDlg.cpp b/Lite/AddressDlg.cpp index 8d54d1a..653634e 100644 --- a/Lite/AddressDlg.cpp +++ b/Lite/AddressDlg.cpp @@ -2,6 +2,7 @@ // #include "stdafx.h" +#include "Address.h" #include "AddressDlg.h" #include "AppConfig.h" #include "MainFrm.h" @@ -24,9 +25,23 @@ CAddressDlg::CAddressDlg(CWnd* pParent /*=NULL*/) //}}AFX_DATA_INIT } +CString CAddressDlg::GetFormattedAddress() const +{ + // XXX: Duplicate code. + CAddress addr(address); + if (addr.IsValid()) + return addr.URL(); + if (port == 23 || port <= 0) + return address; + CString buf; + buf.Format("%s:%d", LPCTSTR(address), port); + return buf; +} + BEGIN_MESSAGE_MAP(CAddressDlg, CDialog) //{{AFX_MSG_MAP(CAddressDlg) - // NOTE: the ClassWizard will add message map macros here + ON_CBN_EDITCHANGE(IDC_ADDRESS, &CAddressDlg::OnAddressChanged) + ON_CBN_SELCHANGE(IDC_ADDRESS, &CAddressDlg::OnAddressChanged) //}}AFX_MSG_MAP END_MESSAGE_MAP() @@ -74,8 +89,26 @@ void CAddressDlg::DoDataExchange(CDataExchange* pDX) { //{{AFX_DATA_MAP(CAddressDlg) DDX_Text(pDX, IDC_PORT, port); - DDV_MinMaxInt(pDX, port, 1, 65535); + DDV_MinMaxInt(pDX, port, 0, 65535); DDX_Text(pDX, IDC_NAME, name); //}}AFX_DATA_MAP CDialog::DoDataExchange(pDX); } + +void CAddressDlg::OnAddressChanged() +{ + // XXX: Duplicate code. + CEdit* port_field = (CEdit*)GetDlgItem(IDC_PORT); + + CString addr_text; + GetDlgItemText(IDC_ADDRESS, addr_text); + + CAddress addr(addr_text); + port_field->EnableWindow(!addr.IsValid()); + if (addr.IsValid()) + { + CString port_text; + port_text.Format("%d", addr.Port()); + port_field->SetWindowText(port_text); + } +} \ No newline at end of file diff --git a/Lite/AddressDlg.h b/Lite/AddressDlg.h index f5c9906..1d26a41 100644 --- a/Lite/AddressDlg.h +++ b/Lite/AddressDlg.h @@ -22,6 +22,8 @@ class CAddressDlg : public CDialog BOOL OnInitDialog(); CAddressDlg(CWnd* pParent = NULL); // standard constructor + CString GetFormattedAddress() const; + // Dialog Data //{{AFX_DATA(CAddressDlg) enum { IDD = IDD_QUICKCONNECT }; @@ -42,7 +44,7 @@ class CAddressDlg : public CDialog // Generated message map functions //{{AFX_MSG(CAddressDlg) - // NOTE: the ClassWizard will add member functions here + afx_msg void OnAddressChanged(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; diff --git a/Lite/ConnectPage.cpp b/Lite/ConnectPage.cpp index 535a0ee..88f8cd0 100644 --- a/Lite/ConnectPage.cpp +++ b/Lite/ConnectPage.cpp @@ -3,6 +3,7 @@ #include "stdafx.h" #include "pcman.h" +#include "Address.h" #include "ConnectPage.h" #ifdef _DEBUG @@ -26,14 +27,44 @@ CConnectPage::~CConnectPage() { } +void CConnectPage::InitWithAddress(const CString& addr) +{ + address = addr; + port = 0; + + CAddress a(address); + if (!a.IsValid()) + { + port = 23; + int pos = address.ReverseFind(':'); + if (pos != -1) + { + port = (unsigned short)atoi(address.Mid(pos + 1)); + address = address.Left(pos); + } + } +} + +CString CConnectPage::GetFormattedAddress() const +{ + CAddress addr(address); + if (addr.IsValid()) + return addr.URL(); + if (port == 23 || port <= 0) + return address; + CString buf; + buf.Format("%s:%d", LPCTSTR(address), port); + return buf; +} + void CConnectPage::DoDataExchange(CDataExchange* pDX) { CPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CConnectPage) DDX_CBString(pDX, IDC_ADDRESS, address); DDX_Text(pDX, IDC_NAME, name); - DDV_MinMaxInt(pDX, port, 1, 65535); DDX_Text(pDX, IDC_PORT, port); + DDV_MinMaxInt(pDX, port, 0, 65535); //}}AFX_DATA_MAP if (name.IsEmpty()) name = address; @@ -44,6 +75,8 @@ void CConnectPage::DoDataExchange(CDataExchange* pDX) BEGIN_MESSAGE_MAP(CConnectPage, CPropertyPage) //{{AFX_MSG_MAP(CConnectPage) + ON_CBN_EDITCHANGE(IDC_ADDRESS, &CConnectPage::OnAddressChanged) + ON_CBN_SELCHANGE(IDC_ADDRESS, &CConnectPage::OnAddressChanged) //}}AFX_MSG_MAP END_MESSAGE_MAP() @@ -67,5 +100,24 @@ BOOL CConnectPage::OnInitDialog() AppConfig.history.GetNext(pos); } + OnAddressChanged(); + return TRUE; } + +void CConnectPage::OnAddressChanged() +{ + CEdit* port_field = (CEdit*) GetDlgItem(IDC_PORT); + + CString addr_text; + GetDlgItemText(IDC_ADDRESS, addr_text); + + CAddress addr(addr_text); + port_field->EnableWindow(!addr.IsValid()); + if (addr.IsValid()) + { + CString port_text; + port_text.Format("%d", addr.Port()); + port_field->SetWindowText(port_text); + } +} \ No newline at end of file diff --git a/Lite/ConnectPage.h b/Lite/ConnectPage.h index 1159768..9b82210 100644 --- a/Lite/ConnectPage.h +++ b/Lite/ConnectPage.h @@ -21,6 +21,9 @@ class CConnectPage : public CPropertyPage CConnectPage(); ~CConnectPage(); + void InitWithAddress(const CString& addr); + CString GetFormattedAddress() const; + // Dialog Data //{{AFX_DATA(CConnectPage) enum { IDD = IDD_CONNECT }; @@ -43,6 +46,7 @@ class CConnectPage : public CPropertyPage // Generated message map functions //{{AFX_MSG(CConnectPage) afx_msg BOOL OnInitDialog(); + afx_msg void OnAddressChanged(); //}}AFX_MSG DECLARE_MESSAGE_MAP() diff --git a/Lite/ListDlg.cpp b/Lite/ListDlg.cpp index 1d76d5b..ce1d20d 100644 --- a/Lite/ListDlg.cpp +++ b/Lite/ListDlg.cpp @@ -387,14 +387,7 @@ void CListDlg::OnEditSite() CConnectPage page0; CString name = page0.name = str.Left(pos); - page0.address = str.Mid(pos + SEPARATOR_LEN); - page0.port = 23; - pos = page0.address.ReverseFind(':'); - if (pos != -1) - { - page0.port = (unsigned short)atoi(page0.address.Mid(pos + 1)); - page0.address = page0.address.Left(pos); - } + page0.InitWithAddress(str.Mid(pos + SEPARATOR_LEN)); CPropertySheet dlg(IDS_SITE_SETTINGS); CSitePage page1; @@ -416,9 +409,7 @@ void CListDlg::OnEditSite() { sites.changed = TRUE; - CString text = page0.name + SEPARATOR + page0.address; - if (page0.port != 23 && page0.port > 0) - text.Format("%s%s%s:%d", LPCTSTR(page0.name), SEPARATOR, LPCTSTR(page0.address), page0.port); + CString text = page0.name + SEPARATOR + page0.GetFormattedAddress(); sites.SetItemText(item, text); path1 = CSiteSettings::GetFilePath(path1); diff --git a/Lite/MainFrm.cpp b/Lite/MainFrm.cpp index 3fa4d48..4cafc1d 100644 --- a/Lite/MainFrm.cpp +++ b/Lite/MainFrm.cpp @@ -271,16 +271,11 @@ CString NameFromAddress(const CAddress& address) const CString& protocol = address.Protocol(); unsigned short default_port; - if (protocol == "telnet" || protocol == "bbs") - default_port = 23; - else if (protocol == "ws") - default_port = 80; - else if (protocol == "wss") - default_port = 443; - else + if (protocol != "telnet" && protocol != "bbs" && + protocol != "ws" && protocol != "wss") return address.URL(); // Non-BBS. Show full URL. - if (address.Port(default_port) != default_port) + if (address.Port() != address.DefaultPort()) { CString s; s.Format(_T("%s:%d"), address.Server(), address.Port()); @@ -3872,7 +3867,7 @@ void CMainFrame::OnNewConn() return; } - CAddress address = ParseAddress(dlg.address); + CAddress address = ParseAddress(dlg.GetFormattedAddress()); if (!address.IsValid()) return; diff --git a/Lite/TermView.cpp b/Lite/TermView.cpp index 935821c..489bf23 100644 --- a/Lite/TermView.cpp +++ b/Lite/TermView.cpp @@ -1384,7 +1384,7 @@ LRESULT CTermView::OnDNSLookupEnd(WPARAM found, LPARAM lparam) sockaddr.sin_family = AF_INET; // XXX: This path only serves telnet, but we might server others as well later. // Don't hard code default port 23. - sockaddr.sin_port = htons((u_short)new_telnet->address.Port(23)); + sockaddr.sin_port = htons((u_short)new_telnet->address.Port()); // sockaddr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr; if (found) @@ -2063,7 +2063,7 @@ void CTermView::ConnectSocket(CTelnetConn *new_telnet) if (protocol == "telnet" || protocol == "bbs") { - ConnectTcp(new_telnet, address.Server(), address.Port(23)); + ConnectTcp(new_telnet, address.Server(), address.Port()); } else if (protocol == "ws" || protocol == "wss") diff --git a/Lite/Websocket.cpp b/Lite/Websocket.cpp index fcc6aa1..0c12bca 100644 --- a/Lite/Websocket.cpp +++ b/Lite/Websocket.cpp @@ -223,12 +223,11 @@ bool CWebsocket::Connect() cli_info.address = address_.Server(); cli_info.ssl_connection = (address_.Protocol() == "wss"); - unsigned short default_port = cli_info.ssl_connection ? 443 : 80; - cli_info.port = address_.Port(default_port); + cli_info.port = address_.Port(); cli_info.path = address_.Path(); - if (cli_info.port != default_port) - host_header_.Format(_T(":%d"), address_.Port()); + if (cli_info.port != address_.DefaultPort()) + host_header_.Format(_T(":%d"), cli_info.port); host_header_ = address_.Server() + host_header_; cli_info.host = host_header_; cli_info.origin = "app://pcman";