www.gusucode.com > VC++编写的SQL服务端和客户端源码程序 > VC++编写的SQL服务端和客户端源码程序\code\Server\miniSQL.cpp

    //Download by http://www.NewXing.com
// miniSQL.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "miniSQL.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "miniSQLDoc.h"
#include "miniSQLView.h"
//#include "MSLDoc.h"
#include "MSLView.h"
#include "SelDoc.h"
#include "SelView.h"
#include "LogDlg.h"
#include "UserDoc.h"
#include "UserView.h"
#include "CodeDlg.h"
#include "HelpDoc.h"
#include "HelpView.h"
#include "ServerDoc.h"
#include "ServerView.h"

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

/////////////////////////////////////////////////////////////////////////////
// Global function
void Message( LPCTSTR text )
{
	CMainFrame* pMainFrm = ( CMainFrame* )AfxGetMainWnd();
	pMainFrm->m_TabBar.InsertText( text );
}

/////////////////////////////////////////////////////////////////////////////
// CMiniSQLApp

BEGIN_MESSAGE_MAP(CMiniSQLApp, CWinApp)
	//{{AFX_MSG_MAP(CMiniSQLApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_REFRESH_FILE_TREE, OnRefreshFileTree)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_RE_LOGON, OnReLogon)
	ON_COMMAND(ID_USER_MANAGE, OnUserManage)
	ON_COMMAND(ID_CHANGECODE, OnChangecode)
	ON_COMMAND(ID_HELP_ME, OnHelpMe)
	ON_COMMAND(ID_CONNECT, OnConnect)
	ON_COMMAND(ID_DISCONNECT, OnDisconnect)
	ON_UPDATE_COMMAND_UI(ID_DISCONNECT, OnUpdateDisconnect)
	ON_UPDATE_COMMAND_UI(ID_USER_MANAGE, OnUpdateUserManage)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMiniSQLApp construction

CMiniSQLApp::CMiniSQLApp()
{
	dir[ 0 ] = '\0';
	::GetCurrentDirectory(1024, dir);
	m_bConnection = FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMiniSQLApp object

CMiniSQLApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMiniSQLApp initialization

BOOL CMiniSQLApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		::Message( "Error initiating socket!\n" );
		return FALSE;
	}

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("miniSQL Server"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_MINISQTYPE,
		RUNTIME_CLASS(CMiniSQLDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CMiniSQLView));
	AddDocTemplate(pDocTemplate);

	// Custom MDIDocument
	// add CMSLDoc and CMSLView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_MSLTYPE,
		RUNTIME_CLASS(CMSLDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CMSLView)) );

	// add CSelDoc and CSelView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_SELTYPE,
		RUNTIME_CLASS(CSelDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CSelView)) );

	// add CUserDoc and CUserView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_USERTYPE,
		RUNTIME_CLASS(CUserDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CUserView)) );

	// add CHelpDoc and CHelpView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_HELPTYPE,
		RUNTIME_CLASS(CHelpDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CHelpView)) );

	// add CServerDoc and CServerView into the new document
	AddDocTemplate(
		new CMultiDocTemplate(
		IDR_CONTYPE,
		RUNTIME_CLASS(CServerDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CServerView)) );

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	Logon();

	return TRUE;
}


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

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CHyperLink	m_hyperLink;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}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)
	DDX_Control(pDX, IDC_URL_MAIL, m_hyperLink);
	//}}AFX_DATA_MAP
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_hyperLink.SetURL(_T("mailto:luxiaochun_1111@163.com"));

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

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

// App command to run the dialog
void CMiniSQLApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMiniSQLApp message handlers


void CMiniSQLApp::OnRefreshFileTree() 
{
	CMainFrame* pMainFrame = ( CMainFrame* )m_pMainWnd;
	
	pMainFrame->m_FileBar.PreInsertTree();
}

void CMiniSQLApp::OnFileNew() 
{
	POSITION curTemplatePos = GetFirstDocTemplatePosition();

	while( curTemplatePos != NULL )
	{
		CDocTemplate* curTemplate =
			GetNextDocTemplate( curTemplatePos );
		CString str;
		curTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "MiniSQ" ) )
		{
			curTemplate->OpenDocumentFile(NULL);
			return;
		}
	}
}

CDocTemplate* CMiniSQLApp::OnSelect()
{
	POSITION curTemplatePos = GetFirstDocTemplatePosition();
	CDocTemplate* curTemplate;

	while( curTemplatePos != NULL )
	{
		curTemplate = GetNextDocTemplate( curTemplatePos );
		CString str;
		curTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "SEL" ) )
		{
			curTemplate->OpenDocumentFile(NULL);
			return curTemplate;
		}
	}

	return curTemplate;
}

CMSLDoc* CMiniSQLApp::IsDocExist( LPCTSTR title )
{
	POSITION Pos = GetFirstDocTemplatePosition();
	CDocTemplate* pTemplate;
	while( Pos )
	{
		pTemplate = GetNextDocTemplate( Pos );
		CString str;
		pTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "MSL" ) )
			break;
	}

	Pos = pTemplate->GetFirstDocPosition();
	while( Pos )
	{
		CMSLDoc* pDoc = (CMSLDoc* )pTemplate->GetNextDoc( Pos );
		if( pDoc->GetTitle() == title )
			return pDoc;
	}

	return NULL;
}

void CMiniSQLApp::Logon()
{
	POSITION Pos = GetFirstDocTemplatePosition();
	CDocTemplate* pTemplate;
	while( Pos )
	{
		pTemplate = GetNextDocTemplate( Pos );
		CString str;
		pTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "USER" ) )
			break;
	}
	Pos = pTemplate->GetFirstDocPosition();
	while( Pos )
	{
		CMSLDoc* pDoc = (CMSLDoc* )pTemplate->GetNextDoc( Pos );
		pDoc->OnCloseDocument();
	}

	Pos = GetFirstDocTemplatePosition();
	while( Pos )
	{
		pTemplate = GetNextDocTemplate( Pos );
		CString str;
		pTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "HELP" ) )
			break;
	}
	Pos = pTemplate->GetFirstDocPosition();
	while( Pos )
	{
		CHelpDoc* pDoc = ( CHelpDoc* )pTemplate->GetNextDoc( Pos );
		POSITION pos = pDoc->GetFirstViewPosition();
		pDoc->DoSave( pDoc->GetPathName() );
		while( pos )
		{
			CHelpView* pView = ( CHelpView* )pDoc->GetNextView( pos );
			pView->SetReadOnly( TRUE );
		}
	}

	CLogDlg* pDlg = new CLogDlg();

	if( pDlg->DoModal() == IDOK )
	{
		if( m_LogType = pDlg->GetType() )
		{
			m_LogName = pDlg->m_user;
			return;
		}
		else
		{
			AfxMessageBox("不存在这个用户或密码错误!");
			Logon();
		}
	}
	else
		Exit();
}

void CMiniSQLApp::OnReLogon() 
{
	Logon();
}

void CMiniSQLApp::OnUserManage()
{
	if( !(m_LogType & LOG_ADMIN) )
	{
		AfxMessageBox( "You'ar not authorized to access user file\nand grant authorization." );
		return;
	}
	POSITION curTemplatePos = GetFirstDocTemplatePosition();

	while( curTemplatePos != NULL )
	{
		CDocTemplate* curTemplate =
			GetNextDocTemplate( curTemplatePos );
		CString str;
		curTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "USER" ) )
		{
			CString path( dir );
			path += "\\user.dat";
			curTemplate->OpenDocumentFile( path );
			return;
		}
	}
}

void CMiniSQLApp::OnChangecode() 
{
	if( theApp.m_LogName == _T("admin") ||
		theApp.m_LogName == _T("superuser") ||
		theApp.m_LogName == _T("user") )
	{
		AfxMessageBox("这是默认帐户,无法修改密码!");
		return;
	}

	// close all the open document associated with "user.dat"
	POSITION Pos = GetFirstDocTemplatePosition();
	CDocTemplate* pTemplate;
	while( Pos )
	{
		pTemplate = GetNextDocTemplate( Pos );
		CString str;
		pTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "USER" ) )
			break;
	}

	Pos = pTemplate->GetFirstDocPosition();
	while( Pos )
	{
		CMSLDoc* pDoc = (CMSLDoc* )pTemplate->GetNextDoc( Pos );
		pDoc->OnCloseDocument();
	}

	// load user profiles
	CString path( dir );
	path += "\\user.dat";
	CFile File( path, CFile::modeRead );
	CArchive ar( &File, CArchive::load );
	int count;
	CList< CUser, CUser& > UserList;

	ar>>count;
	for( int i = 0; i < count; i++ )
	{
		CUser temp;
		temp.Serialize( ar );
		UserList.AddTail( temp );
	}
	char c;
	ar>>c;
	if( c != '\0' )
		throw Error( ERROR_FILE_CORRUPTED, 0, _T("") );
	File.Close();

	// change code
	POSITION pos = UserList.GetHeadPosition();
	CUser* user;
	while( pos )
	{
		user = &UserList.GetNext( pos );
		if( user->m_user == m_LogName )
			break;
	}

	CCodeDlg dlg;
	if( dlg.DoModal() == IDOK )
	{
		if( dlg.m_oldcode != user->m_code )
		{
			AfxMessageBox( "旧密码输入错误,请重输!" );
			OnChangecode();
			return;
		}

		if( dlg.m_newcode != dlg.m_recode )
		{
			AfxMessageBox( "两处密码不相符,请重输!" );
			OnChangecode();
			return;
		}

		if( !dlg.m_newcode.GetLength() )
		{
			AfxMessageBox( "新密码不能为空,请重输!" );
			OnChangecode();
			return;
		}

		user->m_code = dlg.m_newcode;

		// save the new user profile
		TRY
		{
			CFile::Remove( path );
		}
		CATCH( CFileException, e )
		{
			throw Error( ERROR_CHANGE_CODE, 0, _T("") );
		}
		END_CATCH

		CFile new_file( path, CFile::modeCreate | CFile::modeWrite );
		CArchive ar1( &new_file, CArchive::store );
		ar1<<count;
		for( i = 0; i < count; i++ )
		{
			CUser temp = UserList.GetAt( UserList.FindIndex( i ) );
			temp.Serialize( ar1 );
		}
		ar1<<'\0';

		// successful
		AfxMessageBox( "密码已经修改成功!" );
	}	
}

void CMiniSQLApp::OnHelpMe() 
{
	CString path( theApp.dir );
	path += "\\help.hlp";
	OpenDocumentFile( path );
}

void CMiniSQLApp::OnConnect() 
{
	POSITION pos = GetFirstDocTemplatePosition();
	CDocTemplate* pTemplate;
	while( pos )
	{
		pTemplate = GetNextDocTemplate( pos );
		CString str;
		pTemplate->GetDocString( str, CDocTemplate::docName );
		if( str == _T( "LOG" ) )
			break;
	}

	if( m_bConnection )
	{
		if( AfxMessageBox( "已经建立了SOCKET服务,\n想取消现有的服务吗?",
		MB_YESNO | MB_ICONQUESTION ) == IDYES )
		{
			pos = pTemplate->GetFirstDocPosition();
			if( pos )
			{
				CServerDoc* pDoc = ( CServerDoc* )pTemplate->GetNextDoc( pos );
				pDoc->DoSave( pDoc->GetPathName() );
				pDoc->OnCloseDocument();
			}
		}
		else
			return;
	}

	m_bConnection = TRUE;
	pTemplate->OpenDocumentFile( NULL );
}

void CMiniSQLApp::OnDisconnect() 
{
	if( AfxMessageBox( "已经建立了SOCKET服务,\n想取消现有的服务吗?",
		MB_YESNO | MB_ICONQUESTION ) == IDYES )
	{
		POSITION pos = GetFirstDocTemplatePosition();
		CDocTemplate* pTemplate;
		while( pos )
		{
			pTemplate = GetNextDocTemplate( pos );
			CString str;
			pTemplate->GetDocString( str, CDocTemplate::docName );
			if( str == _T( "LOG" ) )
				break;
		}

		pos = pTemplate->GetFirstDocPosition();
		CServerDoc* pDoc = ( CServerDoc* )pTemplate->GetNextDoc( pos );
		pDoc->DoSave( pDoc->GetPathName() );
		pDoc->OnCloseDocument();
	}
}

void CMiniSQLApp::OnUpdateDisconnect(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_bConnection );
}

void CMiniSQLApp::OnUpdateUserManage(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable( m_LogType & LOG_ADMIN );
}