⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taredit.cpp

📁 HP公司的SNMP++的Win32版本源码
💻 CPP
字号:
/*============================================================================
   Copyright (c) 1996
  Hewlett-Packard Company

  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  Permission to use, copy, modify, distribute and/or sell this software 
  and/or its documentation is hereby granted without fee. User agrees 
  to display the above copyright notice and this license notice in all 
  copies of the software and any documentation of the software. User 
  agrees to assume all liability for the use of the software; Hewlett-Packard 
  makes no representations about the suitability of this software for any 
  purpose. It is provided "AS-IS without warranty of any kind,either express 
  or implied. User hereby grants a royalty-free license to any and all 
  derivatives based upon this software code base. 

=============================================================================*/


#include "stdafx.h"
#include "browser.h"
#include "TarEdit.h" 
#include "tarinc.h"
#include "snmp_pp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// TarEdit property page

IMPLEMENT_DYNCREATE(TarEdit, CPropertyPage)

TarEdit::TarEdit() : CPropertyPage(TarEdit::IDD)
{
	//{{AFX_DATA_INIT(TarEdit)
	m_address = _T("");
	m_alias = _T("");
	m_read_community = _T("");
	m_write_community = _T("");
	m_timeout = 50;
	m_retries = 0;
	//}}AFX_DATA_INIT

}

void TarEdit::toggle_controls(int state)
{
     CWnd *ctl;
                                     
	 ctl = GetDlgItem( IDC_AGENTINFO);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_AGENT_ADDR_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_AGENT_ADDR);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_PROTO_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_RADIO_IP);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_RADIO_IPX);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_ALIAS_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_ALIAS);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_TIMEOUT_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_RETRIES_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_MGR_INFO);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_RADIO_V1);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_RADIO_V2C);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_SNMP_INFO);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_GETCOMM_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_READ_COMMUNITY);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_WRITE_COMMUNITY);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_SETCOMM_NAME);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem( IDC_BUTTON_SAVE);
     ctl->EnableWindow( state);

	 ctl = GetDlgItem ( IDC_SLIDER_TIMEOUT);
	 ctl->EnableWindow( state);

	 ctl = GetDlgItem ( IDC_SLIDER_RETRIES);
	 ctl->EnableWindow( state);

};

TarEdit::~TarEdit()
{
}

void TarEdit::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(TarEdit)
	DDX_Text(pDX, IDC_AGENT_ADDR, m_address);
	DDV_MaxChars(pDX, m_address, 40);
	DDX_Text(pDX, IDC_ALIAS, m_alias);
	DDV_MaxChars(pDX, m_alias, 40);
	DDX_Text(pDX, IDC_READ_COMMUNITY, m_read_community);
	DDV_MaxChars(pDX, m_read_community, 80);
	DDX_Text(pDX, IDC_WRITE_COMMUNITY, m_write_community);
	DDV_MaxChars(pDX, m_write_community, 80);
	DDX_Text(pDX, IDC_TIMEOUT, m_timeout);
	DDV_MinMaxUInt(pDX, m_timeout, 50, 500);
	DDX_Text(pDX, IDC_RETRIES, m_retries);
	DDV_MinMaxUInt(pDX, m_retries, 0, 5);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(TarEdit, CPropertyPage)
	//{{AFX_MSG_MAP(TarEdit)
	ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
	ON_LBN_DBLCLK(IDC_LIST_TARGETS, OnDblclkListTargets)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// TarEdit message handlers

BOOL TarEdit::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here

	// disable the controls to begin with
	toggle_controls( FALSE);
	load_target_list();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void TarEdit::OnButtonNew() 
{
	// TODO: Add your control notification handler code here
	
	CString temp;

	// get the defaults
	m_read_community = theApp.GetProfileString( BROWSER_VALUE,READ_COMMUNITY,PUBLIC);
	m_write_community = theApp.GetProfileString( BROWSER_VALUE,WRITE_COMMUNITY,PUBLIC);
	m_timeout = theApp.GetProfileInt( BROWSER_VALUE,TIMEOUT,DEF_TIMEOUT);
	m_retries = theApp.GetProfileInt( BROWSER_VALUE,RETRIES,DEF_RETRIES);
	temp = theApp.GetProfileString( BROWSER_VALUE,PROTOCOL,IP);
	if ( strcmp( temp,IP) == 0)
	  CheckRadioButton( IDC_RADIO_IP,IDC_RADIO_IPX,IDC_RADIO_IP);
	else
	   CheckRadioButton( IDC_RADIO_IP,IDC_RADIO_IPX,IDC_RADIO_IPX);
	temp = theApp.GetProfileString( BROWSER_VALUE,SNMPTYPE,V1);
	if ( strcmp( temp,V1) == 0)
	  CheckRadioButton( IDC_RADIO_V1,IDC_RADIO_V2C,IDC_RADIO_V1);
	else
	  CheckRadioButton( IDC_RADIO_V1,IDC_RADIO_V2C,IDC_RADIO_V2C);

	// set up the slider controls
	CSliderCtrl *slider;

	slider = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_TIMEOUT);
	slider->SetRange(50,500);
	slider->SetPos( m_timeout);

	slider = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_RETRIES);
	slider->SetRange(0,5);
	slider->SetPos( m_retries);

	m_address = "";
	m_alias = "";

	UpdateData( FALSE);


	// enable all controls
	toggle_controls( TRUE);
}

void TarEdit::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
		// determine which  slider bar it is
	CSliderCtrl *slider_retries, *slider_timeouts;

	slider_timeouts = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_TIMEOUT);
	slider_retries = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_RETRIES);

	if ( (CSliderCtrl *) pScrollBar == slider_timeouts)
	{
	   m_timeout = slider_timeouts->GetPos();
	   UpdateData( FALSE);
	}

	if ( (CSliderCtrl *) pScrollBar == slider_retries)
	{
	   m_retries = slider_retries->GetPos();
	   UpdateData( FALSE);
	}

	CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
}


void TarEdit::load_target_list()
{
   CString filename;
   long int nr,i;
   TargetDb_Rec db_rec;
   int status;
   CListBox * target_lb;
   CString target_name;
   CWnd *ctl;


   // get control handle and reset it
   target_lb = ( CListBox *) GetDlgItem( IDC_LIST_TARGETS);
   target_lb->ResetContent();

   // get the db file name from the ini file
   filename = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);
   

   Db target_db;
   target_db.set_attributtes( filename, sizeof( TargetDb_Rec));
   nr = target_db.get_num_recs();

   for (i=1;i<=nr;i++)
   {
      if ((status = target_db.retrieve((long int) (i-1), &db_rec))!= DB_OK)
      {
         MessageBox("Unable to Read Target DB Record", ERR_MSG,MB_ICONSTOP);
         return;
      }

	  if ( strcmp( db_rec.alias,"") ==0)
         target_lb->AddString((char*)  db_rec.key);
	  else
	  {
		target_name = db_rec.alias;
		target_name += " @ ";
		target_name += db_rec.key;
		target_lb->AddString(target_name);
	  }
   }

   if ( nr >= 1)
   {
	 ctl = GetDlgItem( IDC_BUTTON_DELETE);
     ctl->EnableWindow( TRUE);
	 ctl = GetDlgItem( IDC_BUTTON_EDIT);
     ctl->EnableWindow( TRUE);
   }
   else
   {
	 ctl = GetDlgItem( IDC_BUTTON_DELETE);
     ctl->EnableWindow( FALSE);
	 ctl = GetDlgItem( IDC_BUTTON_EDIT);
     ctl->EnableWindow( FALSE);
   }

};

void TarEdit::OnButtonSave() 
{
   // TODO: Add your control notification handler code here
   char buffer[40];
   TargetDb_Rec tdb_rec;

   // verify that all controls have a value
   if ( !UpdateData( TRUE))
      return;

   // check for a read commmunity
   if ( strcmp( m_read_community,"")==0)
   {
      MessageBox("Read Community Not Defined!",ERR_MSG,MB_ICONSTOP);
      return;
   }
   strcpy( buffer, m_read_community);
   strcpy(tdb_rec.read_community, buffer);

   // check for a write community
   if ( strcmp( m_write_community,"")==0)
   {
      MessageBox("Write Community Not Defined!", ERR_MSG,MB_ICONSTOP);
	  return;
   }
   strcpy( buffer, m_write_community);
   strcpy(tdb_rec.write_community, buffer);

   
   // check out the address
   if ( strcmp( m_address,"") == 0)
   {
      MessageBox("Address Not Defined!", ERR_MSG,MB_ICONSTOP);
	  return;
   }
   strcpy( buffer, m_address);
   strcpy(tdb_rec.key, buffer);


   // verify that address is legal ip address
   if ( GetCheckedRadioButton( IDC_RADIO_IP, IDC_RADIO_IPX) == IDC_RADIO_IP)
   {
      char buffer[40];
      strcpy( buffer, m_address);
      IpAddress my_ip( (char *) buffer);
      if ( !my_ip.valid())
      {
         MessageBox("Invalid IP Address!", ERR_MSG,MB_ICONSTOP); 
 	     return;
      }
      tdb_rec.address_type = IP_TYPE; 
   }

   // verify that address is a legal ipx address
   if ( GetCheckedRadioButton( IDC_RADIO_IP, IDC_RADIO_IPX) == IDC_RADIO_IPX)
   {
      strcpy( buffer, m_address);
      IpxAddress my_ipx( (char *) buffer);
	  if ( !my_ipx.valid())
	  {
         MessageBox("Invalid IPX Address!", ERR_MSG,MB_ICONSTOP); 
		 return;
	  } 
      tdb_rec.address_type = IPX_TYPE; 
   }


   
   if ( GetCheckedRadioButton( IDC_RADIO_V1, IDC_RADIO_V2C) == IDC_RADIO_V1)
	  tdb_rec.snmp_type = SNMPV1; 
   else
	  tdb_rec.snmp_type = SNMPV2C;


   // get the db file name from the ini file
   CString filename;

   filename = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);
   

   // write the record to the db
   // if we have duplicates, then confirm update
   Db target_db;
   target_db.set_attributtes( filename, sizeof(TargetDb_Rec));

   tdb_rec.timeout = m_timeout;
   tdb_rec.retries = m_retries;
   strcpy( tdb_rec.alias, m_alias);
   if (target_db.write( &tdb_rec) != DB_OK)
   {
      MessageBox("Unable to Write to Target DataBase", ERR_MSG,MB_ICONSTOP);
      return;
   }        

   load_target_list();

}

void TarEdit::OnButtonEdit() 
{
	// TODO: Add your control notification handler code here
	int index;
    char buffer[80];
    TargetDb_Rec read_rec;
    int status;
	CListBox *target_lb;
	char *ptr,*key;
	CSliderCtrl *slider;


    // get currently selected item
    // if we don't have one then wrn user
    target_lb = ( CListBox *) GetDlgItem( IDC_LIST_TARGETS);
    index = target_lb->GetCurSel();
    if ( index == LB_ERR)
    {
       MessageBox("You Must Select a Target", ERR_MSG,MB_ICONSTOP);
       return;
    }

    if ( (target_lb->GetText( index, buffer)) == LB_ERR)
    {
       MessageBox("Cannot Obtain List Box Selection",ERR_MSG,MB_ICONSTOP);
       return;
    }

	// trim off the @ sign if present
	ptr=buffer;
	key=ptr;
	while (*ptr!=0)
	{
		if ( *ptr == '@')
		   key = ptr+2;
		ptr++;
	}
	    

	// get the db file name from the ini file
    CString filename;
    filename = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);


    // read the record
    Db target_db;
    target_db.set_attributtes( filename, sizeof(TargetDb_Rec));
    strcpy( read_rec.key, key);
    status = target_db.read( (TargetDb_Rec*) &read_rec);
    if ( status != DB_OK)
    {
       MessageBox("Unable to Read DB Record",ERR_MSG,MB_ICONSTOP);
       return;
    }

    // got the record, fill up the controls
    toggle_controls( TRUE);

	m_read_community = read_rec.read_community;
	m_write_community = read_rec.write_community;
	m_address = read_rec.key;
	m_alias = read_rec.alias;
	m_timeout =	read_rec.timeout;
	slider = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_TIMEOUT);
	slider->SetRange(50,500);
	slider->SetPos( m_timeout);
	m_retries = read_rec.retries;
	slider = (CSliderCtrl *) GetDlgItem( IDC_SLIDER_RETRIES);
	slider->SetRange(0,5);
	slider->SetPos( m_retries);
	if ( read_rec.address_type == IP_TYPE)
		CheckRadioButton( IDC_RADIO_IP, IDC_RADIO_IPX, IDC_RADIO_IP);
	else
		CheckRadioButton( IDC_RADIO_IP, IDC_RADIO_IPX, IDC_RADIO_IPX);
	if (read_rec.snmp_type == SNMPV1)
		CheckRadioButton( IDC_RADIO_V1, IDC_RADIO_V2C, IDC_RADIO_V1);
	else
		CheckRadioButton( IDC_RADIO_V1, IDC_RADIO_V2C, IDC_RADIO_V2C);


    UpdateData( FALSE);
	
}



void TarEdit::OnDblclkListTargets() 
{
	// TODO: Add your control notification handler code here
	OnButtonEdit();
}

void TarEdit::OnCancel() 
{
	// TODO: Add your specialized code here and/or call the base class
	CWnd *parent;

	parent = GetParent();
	parent->DestroyWindow();

}

void TarEdit::OnButtonDelete() 
{
	// TODO: Add your control notification handler code here
	CString file_name;
    char buffer[80];
    char message[80];
    int index;
    int status;
    TargetDb_Rec delete_rec;
	CListBox *target_lb;
	char *ptr, *key;

    // get currently selected item
    // if we don't have one then wrn user
    target_lb = ( CListBox *) GetDlgItem( IDC_LIST_TARGETS);
    index = target_lb->GetCurSel();
    if ( index == LB_ERR)
    {
       MessageBox("You Must Select a Target", ERR_MSG,MB_ICONSTOP);
       return;
    }
    if ( (target_lb->GetText( index, buffer)) == LB_ERR)
    {
       MessageBox("Can Not Obtain List Box Selection",ERR_MSG,MB_ICONSTOP);
       return;
    }

    // verify that user really wants to delete
    sprintf( message,  "Do You Really Wish to Delete Target: %s",buffer);
    status = MessageBox( message, ERR_MSG,MB_YESNO);
    if ( status == IDNO)
       return;

	// trim off the @ sign if present
	ptr=buffer;
	key=ptr;
	while (*ptr!=0)
	{
		if ( *ptr == '@')
		   key = ptr+2;
		ptr++;
	}

	file_name = theApp.GetProfileString( BROWSER_VALUE,DB_NAME,DEF_DB_NAME);
    Db dbt;
    dbt.set_attributtes( file_name, sizeof(TargetDb_Rec));

    strcpy( delete_rec.key, key);
    status = dbt.del( delete_rec.key);
    if ( status != DB_OK)
       MessageBox("Unable to Delete Target Record", "SNMP++ Demo Error",MB_ICONSTOP);

    // update the lb
    load_target_list();

	// check to see if currently selected target was deleted
	if ( strcmp( key, m_address) == 0)
    {
		m_address = "";
		m_read_community = "";
		m_write_community = "";
		m_alias = "";
		m_timeout = 0;
		m_retries = 0;
		UpdateData( FALSE);
		toggle_controls( FALSE);
	}

}

void TarEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CPropertyPage::OnKeyDown(nChar, nRepCnt, nFlags);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -