home *** CD-ROM | disk | FTP | other *** search
- // SecurityDlg.cpp : implementation file
- //
-
-
- #include "stdafx.h"
- #include "Security.h"
- #include "SecurDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CSecurityDlg dialog
-
- CSecurityDlg::CSecurityDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CSecurityDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CSecurityDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CSecurityDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CSecurityDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CSecurityDlg, CDialog)
- //{{AFX_MSG_MAP(CSecurityDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BROWSESYSDB, OnBrowse)
- ON_BN_CLICKED(IDC_OPENDB, OnOpenDB)
- ON_BN_CLICKED(IDC_OPENBROWSEDB, OnBrowseDB)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CSecurityDlg message handlers
-
- BOOL CSecurityDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CEdit *pDBName = (CEdit *)GetDlgItem(IDC_SYSDB);
-
- // 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
-
- //Get existing System DB
- GetSystemDB();
- pDBName->SetWindowText(m_strSystemDB);
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- // 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 CSecurityDlg::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 CSecurityDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- //DAO demo functions
-
- void CSecurityDlg::OnBrowse()
- {
- CFileDialog* pcBrowse; //Need a file open dialog
- CEdit *pDBName = (CEdit *)GetDlgItem(IDC_SYSDB);
-
- //Put up the open file dialog
- pcBrowse = (CFileDialog*)new CFileDialog(TRUE,_T("mda"),NULL,NULL,_T("Jet System Files | *.mda ||"), NULL) ;
-
- pcBrowse->DoModal();
-
- //Set the newly selected system DB filename in the text box
- pDBName->SetWindowText(pcBrowse->GetPathName());
-
- delete pcBrowse;
- }
-
- //Retrieve the existing SystemDB filename
- void CSecurityDlg::GetSystemDB()
- {
- CdbDBEngine dbEng;
-
- try
- {
- m_strSystemDB = dbEng.GetSystemDB();
- }
-
- catch (CdbException dbError)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbError.m_hr),
- (LPCTSTR) exError.GetDescription());
- AfxMessageBox(szBuf);
- }
-
- }
-
- //Here's the meat of the DAO code. This opens the designated database and
- //System DB files and displays the users and groups
-
- void CSecurityDlg::OnOpenDB()
- {
- CComboBox *pGroups = (CComboBox *)GetDlgItem(IDC_GROUPS);
- CComboBox *pUsers = (CComboBox *)GetDlgItem(IDC_USERS);
- CdbDBEngine dbEngine;
- CdbDatabase dbDatabase;
- CdbWorkspace dbMySpace;
- CString strDBName;
- CString strUser;
- CString strPassword;
- LONG lGroups;
- LONG lUsers;
- LONG lIndex;
-
- //Get the database name
- (GetDlgItem(IDC_OPENDATABASE))->GetWindowText(strDBName);
-
- //Get the system DB name
- (GetDlgItem(IDC_SYSDB))->GetWindowText(m_strSystemDB);
-
- //Get the user name
- (GetDlgItem(IDC_USER))->GetWindowText(strUser);
-
- //Get the password
- (GetDlgItem(IDC_PASSWORD))->GetWindowText(strPassword);
-
- //Reset the opened flag
- m_bOpened = FALSE;
- pGroups->ResetContent();
-
- try
- {
- //Set the user and password in the default workspace
- dbEngine.SetDefaultUser(strUser);
- dbEngine.SetDefaultPassword(strPassword);
-
- dbEngine.SetSystemDB(m_strSystemDB);
-
- //Create a New Workspace
- dbMySpace = dbEngine.CreateWorkspace(_T("MySpace"), strUser, strPassword );
-
- //Open the database Exclusive/Read-only
- dbDatabase = dbMySpace.OpenDatabase(strDBName, TRUE, TRUE);
- m_bOpened = TRUE;
-
- //Fill in the Groups
- lGroups = dbMySpace.Groups.GetCount();
- for (lIndex = 0L; lIndex < lGroups; lIndex++)
- {
- pGroups->AddString(dbMySpace.Groups[lIndex].GetName());
- }
-
- //Fill in the Users
- lUsers = dbMySpace.Users.GetCount();
- for (lIndex = 0L; lIndex < lUsers; lIndex++)
- {
- pUsers->AddString(dbMySpace.Users[lIndex].GetName());
- }
- }
-
- catch (CdbException dbError)
- {
- CdbLastOLEError exError;
- TCHAR szBuf[256];
-
- //Get Error description
- CString strDesc = (LPCTSTR)exError.GetDescription();
-
- wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbError.m_hr),
- strDesc);
- AfxMessageBox(szBuf);
- }
-
- dbMySpace.Close();
-
- }
-
- void CSecurityDlg::OnBrowseDB()
- {
- CFileDialog* pcBrowse; //Need a file open dialog
- CEdit *pDBName = (CEdit *)GetDlgItem(IDC_OPENDATABASE);
-
- //Put up the open file dialog
- pcBrowse = (CFileDialog*)new CFileDialog(TRUE,_T("mdb"),NULL,NULL,_T("Jet Databases | *.mdb ||"), NULL) ;
-
- pcBrowse->DoModal();
-
- //Set the newly selected DB filename in the text box
- pDBName->SetWindowText(pcBrowse->GetPathName());
-
- delete pcBrowse;
- }
-