home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_DAO.SDK / DISK4 / DAOSDK.1 / securdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-13  |  5.9 KB  |  236 lines

  1. // SecurityDlg.cpp : implementation file
  2. //
  3.  
  4.  
  5. #include "stdafx.h"
  6. #include "Security.h"
  7. #include "SecurDlg.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CSecurityDlg dialog
  17.  
  18. CSecurityDlg::CSecurityDlg(CWnd* pParent /*=NULL*/)
  19.     : CDialog(CSecurityDlg::IDD, pParent)
  20. {
  21.     //{{AFX_DATA_INIT(CSecurityDlg)
  22.         // NOTE: the ClassWizard will add member initialization here
  23.     //}}AFX_DATA_INIT
  24.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  25.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  26. }
  27.  
  28. void CSecurityDlg::DoDataExchange(CDataExchange* pDX)
  29. {
  30.     CDialog::DoDataExchange(pDX);
  31.     //{{AFX_DATA_MAP(CSecurityDlg)
  32.         // NOTE: the ClassWizard will add DDX and DDV calls here
  33.     //}}AFX_DATA_MAP
  34. }
  35.  
  36. BEGIN_MESSAGE_MAP(CSecurityDlg, CDialog)
  37.     //{{AFX_MSG_MAP(CSecurityDlg)
  38.     ON_WM_PAINT()
  39.     ON_WM_QUERYDRAGICON()
  40.     ON_BN_CLICKED(IDC_BROWSESYSDB, OnBrowse)
  41.     ON_BN_CLICKED(IDC_OPENDB, OnOpenDB)
  42.     ON_BN_CLICKED(IDC_OPENBROWSEDB, OnBrowseDB)
  43.     //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45.  
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CSecurityDlg message handlers
  48.  
  49. BOOL CSecurityDlg::OnInitDialog()
  50. {
  51.     CDialog::OnInitDialog();
  52.     CEdit    *pDBName = (CEdit *)GetDlgItem(IDC_SYSDB);
  53.     
  54.     // Set the icon for this dialog.  The framework does this automatically
  55.     //  when the application's main window is not a dialog
  56.     SetIcon(m_hIcon, TRUE);            // Set big icon
  57.     SetIcon(m_hIcon, FALSE);        // Set small icon
  58.     
  59.     //Get existing System DB
  60.     GetSystemDB();
  61.     pDBName->SetWindowText(m_strSystemDB);
  62.     return TRUE;  // return TRUE  unless you set the focus to a control
  63. }
  64.  
  65. // If you add a minimize button to your dialog, you will need the code below
  66. //  to draw the icon.  For MFC applications using the document/view model,
  67. //  this is automatically done for you by the framework.
  68.  
  69. void CSecurityDlg::OnPaint() 
  70. {
  71.     if (IsIconic())
  72.     {
  73.         CPaintDC dc(this); // device context for painting
  74.  
  75.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  76.  
  77.         // Center icon in client rectangle
  78.         int cxIcon = GetSystemMetrics(SM_CXICON);
  79.         int cyIcon = GetSystemMetrics(SM_CYICON);
  80.         CRect rect;
  81.         GetClientRect(&rect);
  82.         int x = (rect.Width() - cxIcon + 1) / 2;
  83.         int y = (rect.Height() - cyIcon + 1) / 2;
  84.  
  85.         // Draw the icon
  86.         dc.DrawIcon(x, y, m_hIcon);
  87.     }
  88.     else
  89.     {
  90.         CDialog::OnPaint();
  91.     }
  92. }
  93.  
  94. // The system calls this to obtain the cursor to display while the user drags
  95. //  the minimized window.
  96. HCURSOR CSecurityDlg::OnQueryDragIcon()
  97. {
  98.     return (HCURSOR) m_hIcon;
  99. }
  100.  
  101. //DAO demo functions
  102.  
  103. void CSecurityDlg::OnBrowse() 
  104. {
  105.     CFileDialog*    pcBrowse;    //Need a file open dialog
  106.     CEdit            *pDBName = (CEdit *)GetDlgItem(IDC_SYSDB);
  107.  
  108.     //Put up the open file dialog
  109.     pcBrowse = (CFileDialog*)new CFileDialog(TRUE,_T("mda"),NULL,NULL,_T("Jet System Files | *.mda ||"), NULL)    ;
  110.  
  111.     pcBrowse->DoModal();
  112.  
  113.     //Set the newly selected system DB filename in the text box
  114.     pDBName->SetWindowText(pcBrowse->GetPathName());
  115.  
  116.     delete pcBrowse;
  117. }
  118.  
  119. //Retrieve the existing SystemDB filename
  120. void CSecurityDlg::GetSystemDB()
  121. {
  122.     CdbDBEngine dbEng;
  123.  
  124.     try
  125.         {
  126.         m_strSystemDB = dbEng.GetSystemDB();
  127.         }
  128.  
  129.     catch (CdbException dbError)
  130.         {
  131.         CdbLastOLEError exError;
  132.         TCHAR szBuf[256];
  133.  
  134.         wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbError.m_hr), 
  135.             (LPCTSTR) exError.GetDescription());
  136.         AfxMessageBox(szBuf);
  137.         }
  138.  
  139. }
  140.  
  141. //Here's the meat of the DAO code. This opens the designated database and
  142. //System DB files and displays the users and groups
  143.  
  144. void CSecurityDlg::OnOpenDB() 
  145. {
  146.     CComboBox        *pGroups = (CComboBox *)GetDlgItem(IDC_GROUPS);
  147.     CComboBox        *pUsers = (CComboBox *)GetDlgItem(IDC_USERS);
  148.     CdbDBEngine        dbEngine;
  149.     CdbDatabase        dbDatabase;
  150.     CdbWorkspace    dbMySpace;
  151.     CString            strDBName;
  152.     CString            strUser;
  153.     CString            strPassword;
  154.     LONG            lGroups;
  155.     LONG            lUsers;
  156.     LONG            lIndex;
  157.  
  158.     //Get the database name
  159.     (GetDlgItem(IDC_OPENDATABASE))->GetWindowText(strDBName);
  160.  
  161.     //Get the system DB name
  162.     (GetDlgItem(IDC_SYSDB))->GetWindowText(m_strSystemDB);
  163.     
  164.     //Get the user name
  165.     (GetDlgItem(IDC_USER))->GetWindowText(strUser);
  166.  
  167.     //Get the password
  168.     (GetDlgItem(IDC_PASSWORD))->GetWindowText(strPassword);
  169.  
  170.     //Reset the opened flag
  171.     m_bOpened = FALSE;
  172.     pGroups->ResetContent();
  173.  
  174.     try
  175.         {
  176.         //Set the user and password in the default workspace
  177.         dbEngine.SetDefaultUser(strUser);
  178.         dbEngine.SetDefaultPassword(strPassword);
  179.  
  180.         dbEngine.SetSystemDB(m_strSystemDB);
  181.  
  182.         //Create a New Workspace
  183.         dbMySpace = dbEngine.CreateWorkspace(_T("MySpace"), strUser, strPassword );
  184.  
  185.         //Open the database Exclusive/Read-only
  186.         dbDatabase = dbMySpace.OpenDatabase(strDBName, TRUE, TRUE);
  187.         m_bOpened = TRUE;
  188.  
  189.         //Fill in the Groups
  190.         lGroups = dbMySpace.Groups.GetCount();
  191.         for (lIndex = 0L; lIndex < lGroups; lIndex++)
  192.             {
  193.             pGroups->AddString(dbMySpace.Groups[lIndex].GetName());
  194.             }
  195.  
  196.         //Fill in the Users
  197.         lUsers = dbMySpace.Users.GetCount();
  198.         for (lIndex = 0L; lIndex < lUsers; lIndex++)
  199.             {
  200.             pUsers->AddString(dbMySpace.Users[lIndex].GetName());
  201.             }
  202.         }
  203.  
  204.     catch (CdbException dbError)
  205.         {
  206.         CdbLastOLEError exError;
  207.         TCHAR szBuf[256];
  208.         
  209.         //Get Error description
  210.         CString strDesc = (LPCTSTR)exError.GetDescription();
  211.  
  212.         wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(dbError.m_hr), 
  213.             strDesc);
  214.         AfxMessageBox(szBuf);
  215.         }
  216.  
  217. dbMySpace.Close();
  218.  
  219. }
  220.  
  221. void CSecurityDlg::OnBrowseDB() 
  222. {
  223.     CFileDialog*    pcBrowse;    //Need a file open dialog
  224.     CEdit            *pDBName = (CEdit *)GetDlgItem(IDC_OPENDATABASE);
  225.  
  226.     //Put up the open file dialog
  227.     pcBrowse = (CFileDialog*)new CFileDialog(TRUE,_T("mdb"),NULL,NULL,_T("Jet Databases | *.mdb ||"), NULL)    ;
  228.  
  229.     pcBrowse->DoModal();
  230.  
  231.     //Set the newly selected DB filename in the text box
  232.     pDBName->SetWindowText(pcBrowse->GetPathName());
  233.  
  234.     delete pcBrowse;
  235. }
  236.