Back to index

lightning-sunbird  0.9+nobinonly
winldap.cpp
Go to the documentation of this file.
00001 // winldap.cpp : Defines the class behaviors for the application.
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "ldap.h"
00006 #include "winldap.h"
00007 
00008 #include "MainFrm.h"
00009 #include "LdapDoc.h"
00010 #include "LdapView.h"
00011 #include "ConnDlg.h"
00012 #include "SrchDlg.h"
00013 
00014 #ifdef _DEBUG
00015 #ifdef _WIN32
00016 #define new DEBUG_NEW
00017 #endif
00018 #undef THIS_FILE
00019 static char THIS_FILE[] = __FILE__;
00020 #endif
00021 
00023 // LdapApp
00024 
00025 BEGIN_MESSAGE_MAP(LdapApp, CWinApp)
00026        //{{AFX_MSG_MAP(LdapApp)
00027        ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
00028        ON_COMMAND(ID_FILE_CONNECT, OnFileConnect)
00029        ON_UPDATE_COMMAND_UI(ID_FILE_CONNECT, OnUpdateFileConnect)
00030        ON_COMMAND(ID_FILE_DISCONNECT, OnFileDisconnect)
00031        ON_UPDATE_COMMAND_UI(ID_FILE_DISCONNECT, OnUpdateFileDisconnect)
00032        ON_COMMAND(ID_FILE_SEARCH, OnFileSearch)
00033        ON_UPDATE_COMMAND_UI(ID_FILE_SEARCH, OnUpdateFileSearch)
00034        //}}AFX_MSG_MAP
00035        // Standard file based document commands
00036        ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
00037        ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
00038 END_MESSAGE_MAP()
00039 
00041 // LdapApp construction
00042 
00043 LdapApp::LdapApp()
00044 {
00045        m_ld = NULL;
00046        m_connected = FALSE;
00047 }
00048 
00050 // The one and only LdapApp object
00051 
00052 LdapApp theApp;
00053 
00055 // LdapApp initialization
00056 
00057 BOOL LdapApp::InitInstance()
00058 {
00059        // Standard initialization
00060 #ifdef _WIN32
00061 #ifdef _AFXDLL
00062        Enable3dControls();                // Call this when using MFC in a shared DLL
00063 #else
00064        Enable3dControlsStatic();   // Call this when linking to MFC statically
00065 #endif
00066 #endif
00067 
00068        LoadStdProfileSettings();  // Load standard INI file options (including MRU)
00069 
00070        // Register document templates
00071 
00072        CSingleDocTemplate* pDocTemplate;
00073        pDocTemplate = new CSingleDocTemplate(
00074               IDR_MAINFRAME,
00075               RUNTIME_CLASS(LdapDoc),
00076               RUNTIME_CLASS(CMainFrame),       // main SDI frame window
00077               RUNTIME_CLASS(LdapView));
00078        AddDocTemplate(pDocTemplate);
00079 
00080 #ifdef _WIN32
00081        // Parse command line for standard shell commands, DDE, file open
00082        CCommandLineInfo cmdInfo;
00083        ParseCommandLine(cmdInfo);
00084 
00085        // Dispatch commands specified on the command line
00086        if (!ProcessShellCommand(cmdInfo))
00087               return FALSE;
00088 #else
00089        // create a new (empty) document
00090        OnFileNew();
00091 
00092        if (m_lpCmdLine[0] != '\0')
00093        {
00094        }
00095 #endif
00096 
00097        m_dirHost = GetProfileString( "Connection", "host", "localhost" );
00098        m_dirPort = GetProfileInt( "Connection", "port", 389 );
00099        m_searchBase = GetProfileString( "Search", "base", "dc=example,dc=com" );
00100 
00101        m_scope = GetProfileInt( "Search", "scope", LDAP_SCOPE_SUBTREE );
00102        m_searchFilter = GetProfileString( "Search", "filter", "objectclass=*" );
00103 
00104        return TRUE;
00105 }
00106 
00108 // CAboutDlg dialog used for App About
00109 
00110 class CAboutDlg : public CDialog
00111 {
00112 public:
00113        CAboutDlg();
00114 
00115 // Dialog Data
00116        //{{AFX_DATA(CAboutDlg)
00117        enum { IDD = IDD_ABOUTBOX };
00118        //}}AFX_DATA
00119 
00120        // ClassWizard generated virtual function overrides
00121        //{{AFX_VIRTUAL(CAboutDlg)
00122        protected:
00123        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
00124        //}}AFX_VIRTUAL
00125 
00126 // Implementation
00127 protected:
00128        //{{AFX_MSG(CAboutDlg)
00129               // No message handlers
00130        //}}AFX_MSG
00131        DECLARE_MESSAGE_MAP()
00132 };
00133 
00134 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
00135 {
00136        //{{AFX_DATA_INIT(CAboutDlg)
00137        //}}AFX_DATA_INIT
00138 }
00139 
00140 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
00141 {
00142        CDialog::DoDataExchange(pDX);
00143        //{{AFX_DATA_MAP(CAboutDlg)
00144        //}}AFX_DATA_MAP
00145 }
00146 
00147 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
00148        //{{AFX_MSG_MAP(CAboutDlg)
00149               // No message handlers
00150        //}}AFX_MSG_MAP
00151 END_MESSAGE_MAP()
00152 
00153 // App command to run the dialog
00154 void LdapApp::OnAppAbout()
00155 {
00156        CAboutDlg aboutDlg;
00157        aboutDlg.DoModal();
00158 }
00159 
00161 // LdapApp commands
00162 
00163 // Initialize an anonymous connection to a Directory Server
00164 void LdapApp::OnFileConnect() 
00165 {
00166        ConnDlg dlg;
00167        dlg.m_dirHost = m_dirHost;
00168        dlg.m_dirPort = m_dirPort;
00169        if ( IDOK == dlg.DoModal() )
00170        {
00171               m_dirHost = dlg.m_dirHost;
00172               m_dirPort = dlg.m_dirPort;
00173               m_ld = ldap_init( m_dirHost, m_dirPort );
00174               if ( NULL != m_ld )
00175               {
00176                      // Bind as anonymous
00177                      if ( ldap_bind_s( m_ld, "", "", LDAP_AUTH_SIMPLE )
00178                                    != LDAP_SUCCESS )
00179                      {
00180                             AfxMessageBox( "Error binding!" );
00181                             return;
00182                      }
00183               }
00184               else
00185               {
00186                      AfxMessageBox( "Error connecting!" );
00187                      return;
00188               }
00189               m_connected = TRUE;
00190               LdapView *view = (LdapView *)((CFrameWnd *)AfxGetMainWnd())->GetActiveView();
00191               if ( view )
00192               {
00193                      CString title;
00194                      title.Format( "Host %s, port %d", m_dirHost, m_dirPort );
00195                      view->GetDocument()->SetTitle( title );
00196               }
00197        }
00198 }
00199 
00200 void LdapApp::OnUpdateFileConnect(CCmdUI* pCmdUI) 
00201 {
00202        pCmdUI->Enable( !IsConnected() );
00203 }
00204 
00205 // Disconnect (unbind from) a Directory Server
00206 void LdapApp::OnFileDisconnect() 
00207 {
00208        ldap_unbind( m_ld );
00209        m_ld = NULL;
00210        m_connected = FALSE;
00211        LdapView *view = (LdapView *)((CFrameWnd *)AfxGetMainWnd())->GetActiveView();
00212        if ( view )
00213        {
00214               view->GetDocument()->SetTitle( "" );
00215        }
00216 }
00217 
00218 void LdapApp::OnUpdateFileDisconnect(CCmdUI* pCmdUI) 
00219 {
00220        pCmdUI->Enable( IsConnected() );
00221 }
00222 
00223 // Search for and report distinguished names (no attributes)
00224 void LdapApp::OnFileSearch() 
00225 {
00226        SearchDlg dlg;
00227        dlg.m_searchBase = m_searchBase;
00228        dlg.SetScope( m_scope );
00229        dlg.m_searchFilter = m_searchFilter;
00230        // Get desired search parameters
00231        if ( IDOK == dlg.DoModal() )
00232        {
00233               m_searchBase = dlg.m_searchBase;
00234               m_searchFilter = dlg.m_searchFilter;
00235               m_scope = dlg.GetScope();
00236               // Clear the result list
00237               LdapView *view = (LdapView *)((CFrameWnd *)AfxGetMainWnd())->GetActiveView();
00238               if ( view )
00239                      view->ClearLines();
00240               char *attrs[2];
00241               // "dn" is a pseudo-attribute; it is always returned anyway, but not as
00242               // an attribute
00243               attrs[0] = "dn";
00244               attrs[1] = NULL;
00245               if ( ldap_search( m_ld, m_searchBase, m_scope, m_searchFilter,
00246                             attrs, FALSE ) == -1 )
00247               {
00248                      AfxMessageBox( "Failed to start asynchronous search" );
00249                      return;
00250               }
00251               LDAPMessage *res;
00252               int rc;
00253               // Fetch all results as they become available
00254               while ( (rc = ldap_result( m_ld, LDAP_RES_ANY, 0, NULL, &res ))
00255                      == LDAP_RES_SEARCH_ENTRY )
00256               {
00257                      LDAPMessage *e = ldap_first_entry( m_ld, res );
00258                      // Get the distinguished name and show it
00259                      char *dn = ldap_get_dn( m_ld, e );
00260                      if ( view )
00261                             view->AddLine( dn, dn );
00262                      ldap_memfree( dn );
00263                      ldap_msgfree( res );
00264                      // Let the view be updated
00265 #ifdef _WIN32
00266                      Sleep( 0 );
00267 #else
00268                      Yield();
00269 #endif
00270               }
00271               if ( rc == -1 )
00272               {
00273                      AfxMessageBox( "Error on ldap_result" );
00274                      return;
00275            }
00276               else if (( rc = ldap_result2error( m_ld, res, 0 )) != LDAP_SUCCESS )
00277               {
00278                      char *errString = ldap_err2string( rc );
00279                      AfxMessageBox( errString );
00280            }
00281               ldap_msgfree( res );
00282        }
00283 }
00284 
00285 void LdapApp::OnUpdateFileSearch(CCmdUI* pCmdUI) 
00286 {
00287        pCmdUI->Enable( IsConnected() );
00288 }
00289 
00290 int LdapApp::ExitInstance() 
00291 {
00292        WriteProfileString( "Connection", "host", m_dirHost );
00293        WriteProfileInt( "Connection", "port", m_dirPort );
00294        WriteProfileString( "Search", "base", m_searchBase );
00295        WriteProfileInt( "Search", "scope", m_scope );
00296        WriteProfileString( "Search", "filter", m_searchFilter );
00297        
00298        return CWinApp::ExitInstance();
00299 }