📄 rulelist.cpp
字号:
// RuleList.cpp : implementation file
//
#include "stdafx.h"
#include "nospam.h"
#include "RuleList.h"
#include "nospamdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRuleList dialog
CRuleList::CRuleList(CByteArray* pRuleTypes, CStringArray* pRulePatterns, CDWordArray* pRuleDate, CWnd* pParent /*=NULL*/)
: CDialog(CRuleList::IDD, pParent),
m_listMargin(0, 0, 0, 0),
m_pRuleTypes(pRuleTypes),
m_pRulePatterns(pRulePatterns),
m_pRuleDate(pRuleDate)
{
//{{AFX_DATA_INIT(CRuleList)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CRuleList::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRuleList)
DDX_Control(pDX, IDC_RULELIST, m_ruleList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRuleList, CDialog)
//{{AFX_MSG_MAP(CRuleList)
ON_WM_SIZE()
ON_NOTIFY(HDN_ITEMCLICK, 0, OnItemclickRulelist)
ON_BN_CLICKED(IDC_ADDRULE, OnAddrule)
ON_BN_CLICKED(IDC_DELRULE, OnDelrule)
ON_BN_CLICKED(IDC_EDITRULE, OnEditrule)
ON_NOTIFY(NM_DBLCLK, IDC_RULELIST, OnDblclkRulelist)
ON_BN_CLICKED(IDC_SHRINK, OnShrink)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRuleList message handlers
static const char* cszMatchWhat[]= {
"Email", "Name", "Subject"
};
BOOL CRuleList::OnInitDialog()
{
CDialog::OnInitDialog();
// caculate margin
CRect cliRect, listRect;
GetClientRect(cliRect);
m_ruleList.GetWindowRect(listRect);
ScreenToClient(listRect);
m_listMargin = listRect - (LPCRECT)cliRect;
// init columns
m_ruleList.InsertColumn(0, "Match", LVCFMT_LEFT, 60);
m_ruleList.InsertColumn(1, "Pattern", LVCFMT_RIGHT, 160);
m_ruleList.InsertColumn(2, "Last Active", LVCFMT_RIGHT, 80);
for (int i = 0; i < m_pRuleTypes->GetSize(); ++i)
{
if (m_pRuleTypes->GetAt(i) >= 0 && m_pRuleTypes->GetAt(i) <= 2)
{
m_ruleList.InsertItem(i, cszMatchWhat[m_pRuleTypes->GetAt(i)]);
m_ruleList.SetItemText(i, 1, m_pRulePatterns->GetAt(i));
m_ruleList.SetItemText(i, 2, DWord2DateString(m_pRuleDate->GetAt(i)));
m_ruleList.SetItemData(i, i);
}
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CRuleList::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if (m_listMargin.top != 0)
{
CRect cliRect, listRect;
GetClientRect(cliRect);
listRect.top = cliRect.top + m_listMargin.top;
listRect.bottom = cliRect.bottom + m_listMargin.bottom;
listRect.left = cliRect.left + m_listMargin.left;
listRect.right = cliRect.right + m_listMargin.right;
m_ruleList.MoveWindow(listRect);
}
}
static int CALLBACK SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
return ((CNoSpamDlg*)AfxGetMainWnd())->SortRule(lParam1, lParam2, lParamSort);
}
void CRuleList::OnItemclickRulelist(NMHDR* pNMHDR, LRESULT* pResult)
{
//HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
NMLISTVIEW *pLV = (NMLISTVIEW *) pNMHDR;
m_ruleList.SortItems(SortFunc, pLV->iItem);
*pResult = 0;
}
void CRuleList::OnAddrule()
{
if (((CNoSpamDlg*)AfxGetMainWnd())->DlgAddRule(NULL, NULL, NULL))
{
int i = m_pRuleTypes->GetUpperBound();
m_ruleList.InsertItem(i, cszMatchWhat[m_pRuleTypes->GetAt(i)]);
m_ruleList.SetItemText(i, 1, m_pRulePatterns->GetAt(i));
m_ruleList.SetItemText(i, 2, DWord2DateString(m_pRuleDate->GetAt(i)));
m_ruleList.SetItemData(i, i);
}
}
void CRuleList::OnDelrule()
{
int idxSel = m_ruleList.GetSelectionMark();
if (idxSel >= 0)
{
int idxMsg = m_ruleList.GetItemData(idxSel);
if (idxMsg >= 0)
{
m_pRuleTypes->RemoveAt(idxMsg);
m_pRulePatterns->RemoveAt(idxMsg);
m_pRuleDate->RemoveAt(idxMsg);
((CNoSpamDlg*)AfxGetMainWnd())->SaveRules();
m_ruleList.DeleteItem(idxSel);
// adjust all item data
for (int i = 0; i < m_ruleList.GetItemCount(); ++i)
{
int itemData = m_ruleList.GetItemData(i);
if (itemData > idxMsg) {
m_ruleList.SetItemData(i, itemData-1);
}
}
}
}
}
void CRuleList::OnEditrule()
{
int idxSel = m_ruleList.GetSelectionMark();
if (idxSel >= 0)
{
int idxMsg = m_ruleList.GetItemData(idxSel);
if (idxMsg >= 0)
{
if (((CNoSpamDlg*)AfxGetMainWnd())->DlgEditRule(idxMsg))
{
m_ruleList.SetItemText(idxSel, 0, cszMatchWhat[m_pRuleTypes->GetAt(idxMsg)]);
m_ruleList.SetItemText(idxSel, 1, m_pRulePatterns->GetAt(idxMsg));
m_ruleList.SetItemText(idxSel, 2, DWord2DateString(m_pRuleDate->GetAt(idxMsg)));
}
}
}
}
LPCSTR CRuleList::DWord2DateString(DWORD dw)
{
static CString tmp;
tmp.Format(
"%02d/%02d/%04d",
CNoSpamDlg::DWord2Month(dw),
CNoSpamDlg::DWord2Day(dw),
CNoSpamDlg::DWord2Year(dw)
);
return tmp;
}
void CRuleList::OnDblclkRulelist(NMHDR* pNMHDR, LRESULT* pResult)
{
OnEditrule();
*pResult = 0;
}
void CRuleList::OnShrink()
{
if (AfxMessageBox(
"'Shrinking' will delete those rules 'covered' by other rules, continue?",
MB_OKCANCEL | MB_ICONQUESTION, 0))
{
CWaitCursor waitCursor;
BOOL bChanged = FALSE;
for (int i = m_pRuleTypes->GetUpperBound(); i > 0; --i)
{
for (int j = m_pRuleTypes->GetUpperBound(); j >= 0; --j)
{
if (j != i && m_pRuleTypes->GetAt(j) == m_pRuleTypes->GetAt(i))
{
CNoSpamDlg* pMainDlg = (CNoSpamDlg*)AfxGetMainWnd();
if (pMainDlg->PatternMatch(m_pRulePatterns->GetAt(i), m_pRulePatterns->GetAt(j)))
{
m_pRuleTypes->RemoveAt(i);
m_pRulePatterns->RemoveAt(i);
m_pRuleDate->RemoveAt(i);
bChanged = TRUE;
break;
}
}
}
}
if (bChanged) {
((CNoSpamDlg*)AfxGetMainWnd())->SaveRules();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -