Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

DlgSecureRule.cpp

Go to the documentation of this file.
00001 //
00002 // DlgSecureRule.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "Security.h"
00026 #include "Network.h"
00027 #include "Skin.h"
00028 #include "DlgSecureRule.h"
00029 
00030 #ifdef _DEBUG
00031 #define new DEBUG_NEW
00032 #undef THIS_FILE
00033 static char THIS_FILE[] = __FILE__;
00034 #endif
00035 
00036 BEGIN_MESSAGE_MAP(CSecureRuleDlg, CSkinDialog)
00037         //{{AFX_MSG_MAP(CSecureRuleDlg)
00038         ON_CBN_SELCHANGE(IDC_RULE_EXPIRE, OnSelChangeRuleExpire)
00039         ON_CBN_SELCHANGE(IDC_RULE_TYPE, OnSelChangeRuleType)
00040         //}}AFX_MSG_MAP
00041 END_MESSAGE_MAP()
00042 
00043 
00045 // CSecureRuleDlg dialog
00046 
00047 CSecureRuleDlg::CSecureRuleDlg(CWnd* pParent, CSecureRule* pRule) : CSkinDialog(CSecureRuleDlg::IDD, pParent)
00048 {
00049         //{{AFX_DATA_INIT(CSecureRuleDlg)
00050         m_nExpireD = 0;
00051         m_nExpireH = 0;
00052         m_nExpireM = 0;
00053         m_nAction = -1;
00054         m_nExpire = -1;
00055         m_sComment = _T("");
00056         m_nType = -1;
00057         m_sContent = _T("");
00058         m_nMatch = -1;
00059         //}}AFX_DATA_INIT
00060         m_pRule = pRule;
00061         m_bNew  = FALSE;
00062 }
00063 
00064 CSecureRuleDlg::~CSecureRuleDlg()
00065 {
00066         if ( m_pRule && m_bNew ) delete m_pRule;
00067 }
00068 
00069 void CSecureRuleDlg::DoDataExchange(CDataExchange* pDX)
00070 {
00071         CSkinDialog::DoDataExchange(pDX);
00072         //{{AFX_DATA_MAP(CSecureRuleDlg)
00073         DDX_Control(pDX, IDC_RULE_CONTENT, m_wndContent);
00074         DDX_Control(pDX, IDC_GROUP_CONTENT, m_wndGroupContent);
00075         DDX_Control(pDX, IDC_GROUP_NETWORK, m_wndGroupNetwork);
00076         DDX_Control(pDX, IDC_EXPIRE_M, m_wndExpireM);
00077         DDX_Control(pDX, IDC_EXPIRE_H, m_wndExpireH);
00078         DDX_Control(pDX, IDC_EXPIRE_D, m_wndExpireD);
00079         DDX_Control(pDX, IDC_MASK_4, m_wndMask4);
00080         DDX_Control(pDX, IDC_MASK_3, m_wndMask3);
00081         DDX_Control(pDX, IDC_MASK_2, m_wndMask2);
00082         DDX_Control(pDX, IDC_MASK_1, m_wndMask1);
00083         DDX_Control(pDX, IDC_IP_4, m_wndIP4);
00084         DDX_Control(pDX, IDC_IP_3, m_wndIP3);
00085         DDX_Control(pDX, IDC_IP_2, m_wndIP2);
00086         DDX_Control(pDX, IDC_IP_1, m_wndIP1);
00087         DDX_Text(pDX, IDC_EXPIRE_D, m_nExpireD);
00088         DDX_Text(pDX, IDC_EXPIRE_H, m_nExpireH);
00089         DDX_Text(pDX, IDC_EXPIRE_M, m_nExpireM);
00090         DDX_CBIndex(pDX, IDC_RULE_ACTION, m_nAction);
00091         DDX_CBIndex(pDX, IDC_RULE_EXPIRE, m_nExpire);
00092         DDX_Text(pDX, IDC_RULE_COMMENT, m_sComment);
00093         DDX_CBIndex(pDX, IDC_RULE_TYPE, m_nType);
00094         DDX_Text(pDX, IDC_RULE_CONTENT, m_sContent);
00095         DDX_Radio(pDX, IDC_RULE_MATCH_0, m_nMatch);
00096         //}}AFX_DATA_MAP
00097 }
00098 
00100 // CSecureRuleDlg message handlers
00101 
00102 BOOL CSecureRuleDlg::OnInitDialog()
00103 {
00104         CSkinDialog::OnInitDialog();
00105 
00106         SkinMe( _T("CSecureRuleDlg"), IDR_SECURITYFRAME );
00107 
00108         CSingleLock pLock( &Network.m_pSection, TRUE );
00109 
00110         m_bNew = ! Security.Check( m_pRule );
00111         if ( m_bNew ) m_pRule = new CSecureRule();
00112 
00113         m_nType         = m_pRule->m_nType;
00114         m_sComment      = m_pRule->m_sComment;
00115         m_nAction       = m_pRule->m_nAction;
00116         m_nExpire       = min( m_pRule->m_nExpire, DWORD(2) );
00117         m_nMatch        = 0;
00118 
00119         if ( m_pRule->m_nType == CSecureRule::srAddress )
00120         {
00121                 CEdit* pwIP[4]          = { &m_wndIP1, &m_wndIP2, &m_wndIP3, &m_wndIP4 };
00122                 CEdit* pwMask[4]        = { &m_wndMask1, &m_wndMask2, &m_wndMask3, &m_wndMask4 };
00123 
00124                 for ( int nByte = 0 ; nByte < 4 ; nByte++ )
00125                 {
00126                         CString strItem;
00127 
00128                         strItem.Format( _T("%lu"), m_pRule->m_nMask[ nByte ] );
00129                         pwMask[ nByte ]->SetWindowText( strItem );
00130 
00131                         if ( m_pRule->m_nMask[ nByte ] == 0 )
00132                         {
00133                                 pwIP[ nByte ]->SetWindowText( _T("*") );
00134                         }
00135                         else
00136                         {
00137                                 strItem.Format( _T("%lu"), m_pRule->m_nIP[ nByte ] );
00138                                 pwIP[ nByte ]->SetWindowText( strItem );
00139                         }
00140                 }
00141         }
00142         else if ( m_pRule->m_nType == CSecureRule::srContent )
00143         {
00144                 m_sContent = m_pRule->GetContentWords();
00145                 m_nMatch = m_pRule->m_nIP[0] == 1;
00146         }
00147 
00148         if ( m_nExpire == 2 )
00149         {
00150                 DWORD nTime = m_pRule->m_nExpire - time( NULL );
00151                 m_nExpireD = nTime / 86400;
00152                 m_nExpireH = ( nTime % 86400 ) / 3600;
00153                 m_nExpireM = ( nTime % 3600 ) / 60;
00154         }
00155 
00156         pLock.Unlock();
00157 
00158         UpdateData( FALSE );
00159 
00160         OnSelChangeRuleExpire();
00161         OnSelChangeRuleType();
00162 
00163         return FALSE;
00164 }
00165 
00166 void CSecureRuleDlg::ShowGroup(CWnd* pWnd, BOOL bShow)
00167 {
00168         while ( pWnd )
00169         {
00170                 pWnd->ShowWindow( bShow ? SW_SHOW : SW_HIDE );
00171                 pWnd = pWnd->GetNextWindow();
00172 
00173                 if ( pWnd->GetStyle() & WS_GROUP )
00174                 {
00175                         if ( pWnd->GetDlgCtrlID() != IDC_RULE_MATCH_0 ) break;
00176                 }
00177         }
00178 }
00179 
00180 void CSecureRuleDlg::OnSelChangeRuleType()
00181 {
00182         UpdateData();
00183 
00184         ShowGroup( &m_wndGroupNetwork, m_nType == CSecureRule::srAddress );
00185         ShowGroup( &m_wndGroupContent, m_nType == CSecureRule::srContent );
00186 
00187         switch ( m_nType )
00188         {
00189         case CSecureRule::srAddress:
00190                 m_wndIP1.SetFocus();
00191                 m_wndIP1.SetSel( 0, -1 );
00192                 break;
00193         case CSecureRule::srContent:
00194                 m_wndContent.SetFocus();
00195                 break;
00196         }
00197 }
00198 
00199 void CSecureRuleDlg::OnSelChangeRuleExpire()
00200 {
00201         UpdateData();
00202         m_wndExpireD.EnableWindow( m_nExpire == 2 );
00203         m_wndExpireH.EnableWindow( m_nExpire == 2 );
00204         m_wndExpireM.EnableWindow( m_nExpire == 2 );
00205 }
00206 
00207 BOOL CSecureRuleDlg::PreTranslateMessage(MSG* pMsg)
00208 {
00209         if ( pMsg->message == WM_CHAR )
00210         {
00211                 CEdit* pwIP[4]          = { &m_wndIP1, &m_wndIP2, &m_wndIP3, &m_wndIP4 };
00212                 CEdit* pwMask[4]        = { &m_wndMask1, &m_wndMask2, &m_wndMask3, &m_wndMask4 };
00213                 CWnd* pFocus            = GetFocus();
00214 
00215                 for ( int nByte = 0 ; nByte < 4 && pFocus ; nByte++ )
00216                 {
00217                         if ( pFocus == pwIP[ nByte ] || pFocus == pwMask[ nByte ] )
00218                         {
00219                                 if ( pMsg->wParam == '.' )
00220                                 {
00221                                         if ( nByte == 3 ) return TRUE;
00222 
00223                                         if ( pFocus == pwIP[ nByte ] )
00224                                         {
00225                                                 pwIP[ nByte + 1 ]->SetFocus();
00226                                                 pwIP[ nByte + 1 ]->SetSel( 0, -1 );
00227                                         }
00228                                         else
00229                                         {
00230                                                 pwMask[ nByte + 1 ]->SetFocus();
00231                                                 pwMask[ nByte + 1 ]->SetSel( 0, -1 );
00232                                         }
00233 
00234                                         return TRUE;
00235                                 }
00236                                 else if ( pMsg->wParam == '*' )
00237                                 {
00238                                         if ( pFocus != pwIP[ nByte ] ) return TRUE;
00239                                         pwIP[ nByte ]->SetWindowText( _T("*") );
00240                                         pwMask[ nByte ]->SetWindowText( _T("0") );
00241                                         pwIP[ nByte ]->SetSel( 0, 1 );
00242                                         pwMask[ nByte ]->SetSel( 0, 1 );
00243                                         return TRUE;
00244                                 }
00245                                 else if ( pMsg->wParam >= 32 && ! _istdigit( (TCHAR)pMsg->wParam ) )
00246                                 {
00247                                         return TRUE;
00248                                 }
00249 
00250                                 break;
00251                         }
00252                 }
00253         }
00254 
00255         return CSkinDialog::PreTranslateMessage(pMsg);
00256 }
00257 
00258 void CSecureRuleDlg::OnOK()
00259 {
00260         UpdateData( TRUE );
00261 
00262         CSingleLock pLock( &Network.m_pSection, TRUE );
00263 
00264         if ( ! m_bNew && ! Security.Check( m_pRule ) ) m_pRule = new CSecureRule();
00265 
00266         m_pRule->m_nType        = m_nType;
00267         m_pRule->m_sComment     = m_sComment;
00268         m_pRule->m_nAction      = m_nAction;
00269         m_pRule->m_nExpire      = m_nExpire;
00270 
00271         if ( m_nExpire == 2 )
00272         {
00273                 m_pRule->m_nExpire      = time( NULL ) + m_nExpireD * 86400
00274                                                         + m_nExpireH * 3600 + m_nExpireM * 60;
00275         }
00276 
00277         if ( m_pRule->m_nType == CSecureRule::srAddress )
00278         {
00279                 CEdit* pwIP[4]          = { &m_wndIP1, &m_wndIP2, &m_wndIP3, &m_wndIP4 };
00280                 CEdit* pwMask[4]        = { &m_wndMask1, &m_wndMask2, &m_wndMask3, &m_wndMask4 };
00281 
00282                 for ( int nByte = 0 ; nByte < 4 ; nByte++ )
00283                 {
00284                         CString strItem;
00285                         int nValue = 0;
00286 
00287                         pwIP[ nByte ]->GetWindowText( strItem );
00288                         if ( _stscanf( strItem, _T("%lu"), &nValue ) != 1 ) nValue = 0;
00289                         m_pRule->m_nIP[ nByte ] = (BYTE)max( 0, min( 255, nValue ) );
00290 
00291                         pwMask[ nByte ]->GetWindowText( strItem );
00292                         if ( _stscanf( strItem, _T("%lu"), &nValue ) != 1 ) nValue = 0;
00293                         m_pRule->m_nMask[ nByte ] = (BYTE)max( 0, min( 255, nValue ) );
00294                 }
00295         }
00296         else if ( m_pRule->m_nType == CSecureRule::srContent )
00297         {
00298                 m_pRule->SetContentWords( m_sContent );
00299                 m_pRule->m_nIP[0] = m_nMatch;
00300         }
00301 
00302         Security.Add( m_pRule );
00303         m_pRule = NULL;
00304 
00305         pLock.Unlock();
00306 
00307         CSkinDialog::OnOK();
00308 }
00309 

Generated on Thu Dec 15 10:39:39 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2