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

📄 trdfiledlg.cpp

📁 SIP 用户登陆用的对话框
💻 CPP
字号:
// TRdFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TRdFile.h"
#include "TRdFileDlg.h"
#include <winsock2.h>
#include "rcfile.h"
#include <stdio.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTRdFileDlg dialog

CTRdFileDlg::CTRdFileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTRdFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTRdFileDlg)
	m_check_autoreg = FALSE;
	m_check_default = FALSE;
	m_check_savepassword = FALSE;
	m_edit_password = _T("");
	m_edit_port = _T("5060");
	m_edit_serveraddr = _T("");
	m_edit_username = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTRdFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTRdFileDlg)
	DDX_Control(pDX, IDC_COMBO_USERURI, m_combo_useruri);
	DDX_Control(pDX, IDC_COMBO_PORTTYPE, m_combo_porttype);
	DDX_Control(pDX, IDC_COMBO_AUTHENTICATE, m_combo_authenticate);
	DDX_Check(pDX, IDC_CHECK_AUTOREG, m_check_autoreg);
	DDX_Check(pDX, IDC_CHECK_DEFAULT, m_check_default);
	DDX_Check(pDX, IDC_CHECK_SAVEPASSWORD, m_check_savepassword);
	DDX_Text(pDX, IDC_PASSWORD, m_edit_password);
	DDV_MaxChars(pDX, m_edit_password, 10);
	DDX_Text(pDX, IDC_PORT, m_edit_port);
	DDV_MaxChars(pDX, m_edit_port, 5);
	DDX_Text(pDX, IDC_SERVERADRR, m_edit_serveraddr);
	DDV_MaxChars(pDX, m_edit_serveraddr, 20);
	DDX_Text(pDX, IDC_USERNAME, m_edit_username);
	DDV_MaxChars(pDX, m_edit_username, 20);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTRdFileDlg, CDialog)
//{{AFX_MSG_MAP(CTRdFileDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SETTING_OK, OnSettingOk)
ON_BN_CLICKED(IDC_SETTING_CLEAR, OnSettingClear)
ON_BN_CLICKED(IDC_SETTING_ADD, OnSettingAdd)
ON_BN_CLICKED(IDC_SETTING_DEL, OnSettingDel)
ON_BN_CLICKED(IDC_SETTING_RENEW, OnSettingRenew)
ON_BN_CLICKED(IDC_SETTING_CANCEL, OnSettingCancel)
ON_WM_CLOSE()
ON_CBN_SELCHANGE(IDC_COMBO_USERURI, OnSelchangeComboUseruri)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTRdFileDlg message handlers

BOOL CTRdFileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_count = 0;
	m_combo_porttype.SetCurSel(2);
	m_combo_authenticate.SetCurSel(1);
	
	for(int i=0;i<CONFIG_SIZE;i++)
	{
		memset(&config[i],0,sizeof(config_t)); // 为开辟的缓冲区设置0字符
        config[i].checkstate = 0x00; // 此时保存密码的按钮被选中
	}
	loadconfigfile("config.txt");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTRdFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTRdFileDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTRdFileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTRdFileDlg::OnSettingOk() 
{
	// TODO: Add your control notification handler code here
	FILE *fp;
	if((fp = fopen("config.txt","w")) == NULL)
	{
		return;
	}else
	{
		for(int i = 0;i<m_count;i++)
			if(fwrite(&config[i],sizeof(config[i]),1,fp)!=1) 
			{
				return;
			}
			fclose(fp);
	}
	//	CDialog::OnOK();
}

void CTRdFileDlg::GetHostNameIP()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD(2,0);
	if(WSAStartup(wVersionRequested,&wsaData)==0)
	{
		if(gethostname(hostname,sizeof(hostname))==0)
		{
			if((hostinfo = gethostbyname(hostname))!=NULL)
			{
				m_ip = inet_ntoa(*(struct in_addr*)*hostinfo->h_addr_list);
			}
		}
	}
	WSACleanup();
}

void CTRdFileDlg::OnSettingClear() 
{
	// TODO: Add your control notification handler code here
	m_edit_username = "";
	m_edit_password = "";
    m_edit_serveraddr = "";
    m_edit_port = "";
	m_check_autoreg = FALSE;
	m_check_default = FALSE;
	m_check_savepassword = FALSE;
	UpdateData(FALSE);
}

void CTRdFileDlg::OnSettingAdd() // 当账号已经存在则
{
	if(m_count>CONFIG_SIZE)
	{
		AfxMessageBox("用户数目已满!");
		return;
	}
    UpdateData(TRUE);
	if(m_edit_username == "")
	{
		AfxMessageBox("用户帐号不能为空!");
		return;
	}else if(m_edit_serveraddr == "")
	{
		AfxMessageBox("服务器地址不能为空!");
		return;
	}else
	{
		for(int i = 0 ; i<m_count ; i++)
		{
			if(config[i].username == m_edit_username)
			{
				AfxMessageBox("该帐户已存在,请点击更新按钮!");
				return;
			}
		}
	}
	
	strcpy(config[m_count].username,m_edit_username.GetBuffer(0));
	if(m_check_savepassword)strcpy(config[m_count].passwd,m_edit_password.GetBuffer(0));
	strcpy(config[m_count].seraddr,m_edit_serveraddr.GetBuffer(0));
	strcpy(config[m_count].serport,m_edit_port.GetBuffer(0));
	
	{
		if(m_check_autoreg)config[m_count].checkstate = config[m_count].checkstate|CONFIG_AUTO_REGISTER;
		if(m_check_default)config[m_count].checkstate = config[m_count].checkstate|CONFIG_DEFAULT_LOGIN;
        if(m_check_savepassword)config[m_count].checkstate = config[m_count].checkstate|CONFIG_SAVE_PASSWD;
	}
	
    m_edit_username.ReleaseBuffer();
    if(m_check_savepassword)m_edit_password.ReleaseBuffer();
    m_edit_serveraddr.ReleaseBuffer();
    m_edit_port.ReleaseBuffer();
	CString logincount = m_edit_username + "@" + m_edit_serveraddr;
    m_combo_useruri.InsertString(m_count,logincount);
    m_combo_useruri.SetCurSel(m_count);
	m_count++;  // 添加时m_count要加1,更新时m_count不进行加1
}

void CTRdFileDlg::OnSettingDel()
{
	// TODO: Add your control notification handler code here
	if(m_count<=0)
	{
		AfxMessageBox("无用户帐号!");
		return;
	}
	int pos = m_combo_useruri.GetCurSel();
	m_combo_useruri.DeleteString(pos);
	m_count--;
	for(int i = pos; i<m_count; i++)
	{
        strcpy(config[i].passwd,config[i+1].passwd);
		strcpy(config[i].seraddr,config[i+1].seraddr);
        strcpy(config[i].serport,config[i+1].serport);
		strcpy(config[i].username,config[i+1].username);
		config[i].checkstate = config[i+1].checkstate;
	}
	
	if(m_count == 0)
	{
		m_combo_useruri.SetCurSel(0);
		OnSettingClear(); 
	}else
	{
		m_combo_useruri.SetCurSel(0);
		int pos0 = 0;
		strcpy(m_edit_username.GetBuffer(0),config[pos0].username);
		strcpy(m_edit_password.GetBuffer(0),config[pos0].passwd);
		strcpy(m_edit_serveraddr.GetBuffer(0),config[pos0].seraddr);
		strcpy(m_edit_port.GetBuffer(0),config[pos0].serport);
		m_edit_username.ReleaseBuffer();
		m_edit_password.ReleaseBuffer();
		m_edit_serveraddr.ReleaseBuffer();
		m_edit_port.ReleaseBuffer();
		UpdateData(FALSE);
	}
}

void CTRdFileDlg::OnSettingRenew()
{
	// TODO: Add your control notification handler code here
	int i;
	if(m_count>CONFIG_SIZE)
	{
		AfxMessageBox("用户数目已满!");
		return;
	}
    UpdateData(TRUE);
	if(m_edit_username == "")
	{
		AfxMessageBox("用户帐号不能为空!");
		return;
	}else if(m_edit_serveraddr == "")
	{
		AfxMessageBox("服务器地址不能为空!");
		return;
	}else if(m_edit_port == "")
	{
		AfxMessageBox("端口地址不能为空!");
		return;
	}

	for(i =0 ; i<m_count ; i++)
	{
		if(config[i].username == m_edit_username)
		{
			strcpy(config[i].username,m_edit_username.GetBuffer(0));
			if(m_check_savepassword)strcpy(config[i].passwd,m_edit_password.GetBuffer(0));
			else strcpy(config[i].passwd,"");
			strcpy(config[i].seraddr,m_edit_serveraddr.GetBuffer(0));
			strcpy(config[i].serport,m_edit_port.GetBuffer(0));
			m_edit_username.ReleaseBuffer();
			if(m_check_savepassword)m_edit_password.ReleaseBuffer();
			m_edit_serveraddr.ReleaseBuffer();
			m_edit_port.ReleaseBuffer();
			{
                config[i].checkstate = 0x00;
				if(m_check_autoreg)config[i].checkstate = config[i].checkstate|CONFIG_AUTO_REGISTER;
				if(m_check_default)config[i].checkstate = config[i].checkstate|CONFIG_DEFAULT_LOGIN;
				if(m_check_savepassword)config[i].checkstate = config[i].checkstate|CONFIG_SAVE_PASSWD;
			}
			int pos = m_combo_useruri.FindString(0,m_edit_username);
			CString logincount = m_edit_username + "@" + m_edit_serveraddr;
			m_combo_useruri.DeleteString(pos);
			m_combo_useruri.InsertString(pos,logincount);
			m_combo_useruri.SetCurSel(pos);
			break;
		}
	}
}

void CTRdFileDlg::OnSettingCancel()
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CTRdFileDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnClose();
}


void CTRdFileDlg::OnSelchangeComboUseruri() 
{
	int pos = m_combo_useruri.GetCurSel();
    m_edit_username.Format("%s",config[pos].username);
    m_edit_password.Format("%s",config[pos].passwd);
    m_edit_serveraddr.Format("%s",config[pos].seraddr);
    m_edit_port.Format("%s",config[pos].serport);
    
	switch (config[pos].checkstate)
	{
	case 0x00:{m_check_savepassword = FALSE;m_check_autoreg = FALSE;m_check_default = FALSE;}break;
	case 0x01:{m_check_savepassword = FALSE;m_check_autoreg = FALSE;m_check_default =  TRUE;}break;
	case 0x02:{m_check_savepassword = FALSE;m_check_autoreg =  TRUE;m_check_default = FALSE;}break;
	case 0x03:{m_check_savepassword = FALSE;m_check_autoreg =  TRUE;m_check_default =  TRUE;}break;
	case 0x04:{m_check_savepassword =  TRUE;m_check_autoreg = FALSE;m_check_default = FALSE;}break;
	case 0x05:{m_check_savepassword =  TRUE;m_check_autoreg = FALSE;m_check_default =  TRUE;}break;
	case 0x06:{m_check_savepassword =  TRUE;m_check_autoreg =  TRUE;m_check_default = FALSE;}break;
	case 0x07:{m_check_savepassword =  TRUE;m_check_autoreg =  TRUE;m_check_default =  TRUE;}break;
	default:break;
	}
	UpdateData(FALSE);
}

int CTRdFileDlg::loadconfigfile(char *filename)
{
	FILE *fp;
	char temp[40];
	char tempusername[40];
	char tempseraddr[40];
	if((fp = fopen("config.txt","r")) == NULL)
	{
		return -1;
	}else
	{
		for(int i = 0;i<CONFIG_SIZE;i++)
		{
			if(!feof(fp)) // 文件没结束
			{
				if(fread(&config[i],sizeof(config_t),1,fp)==1) 
				{
					strcpy(tempusername,config[i].username);
					strcpy(tempseraddr,config[i].seraddr);
					strcpy(temp,strcat(strcat(tempusername,"@"),tempseraddr));
					m_combo_useruri.InsertString(i,temp);
					m_count = i+1;
				}
			}else if(feof(fp))  // 文件结束
			{
				fclose(fp);
				InitReflash();
				return 0;
			}
		}
	}
	fclose(fp);
	return 0;
}

int CTRdFileDlg::InitReflash()
{
	int pos = 0;
    m_edit_username.Format("%s",config[pos].username);
    m_edit_password.Format("%s",config[pos].passwd);
    m_edit_serveraddr.Format("%s",config[pos].seraddr);
    m_edit_port.Format("%s",config[pos].serport);
  
	switch (config[pos].checkstate)
	{
	case 0x00:{m_check_savepassword = FALSE;m_check_autoreg = FALSE;m_check_default = FALSE;}break;
	case 0x01:{m_check_savepassword = FALSE;m_check_autoreg = FALSE;m_check_default =  TRUE;}break;
	case 0x02:{m_check_savepassword = FALSE;m_check_autoreg =  TRUE;m_check_default = FALSE;}break;
	case 0x03:{m_check_savepassword = FALSE;m_check_autoreg =  TRUE;m_check_default =  TRUE;}break;
	case 0x04:{m_check_savepassword =  TRUE;m_check_autoreg = FALSE;m_check_default = FALSE;}break;
	case 0x05:{m_check_savepassword =  TRUE;m_check_autoreg = FALSE;m_check_default =  TRUE;}break;
	case 0x06:{m_check_savepassword =  TRUE;m_check_autoreg =  TRUE;m_check_default = FALSE;}break;
	case 0x07:{m_check_savepassword =  TRUE;m_check_autoreg =  TRUE;m_check_default =  TRUE;}break;
	default:break;
	}
	m_combo_useruri.SetCurSel(0);
	UpdateData(FALSE);
	return 0;
}

⌨️ 快捷键说明

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