Back to index

lightning-sunbird  0.9+nobinonly
LdapView.cpp
Go to the documentation of this file.
00001 // LdapView.cpp : implementation of the LdapView class
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "ldap.h"
00006 #include "winldap.h"
00007 
00008 #include "LdapDoc.h"
00009 #include "LdapView.h"
00010 #include "PropDlg.h"
00011 
00012 #ifdef _DEBUG
00013 #ifdef _WIN32
00014 #define new DEBUG_NEW
00015 #endif
00016 #undef THIS_FILE
00017 static char THIS_FILE[] = __FILE__;
00018 #endif
00019 
00021 // LdapView
00022 
00023 IMPLEMENT_DYNCREATE(LdapView, CView)
00024 
00025 BEGIN_MESSAGE_MAP(LdapView, CView)
00026        //{{AFX_MSG_MAP(LdapView)
00027        ON_WM_SIZE()
00028        ON_LBN_DBLCLK(5000,OnListDoubleClick)
00029        //}}AFX_MSG_MAP
00030 END_MESSAGE_MAP()
00031 
00033 // LdapView construction/destruction
00034 
00035 LdapView::LdapView()
00036 {
00037 }
00038 
00039 LdapView::~LdapView()
00040 {
00041 }
00042 
00043 BOOL LdapView::PreCreateWindow(CREATESTRUCT& cs)
00044 {
00045        return CView::PreCreateWindow(cs);
00046 }
00047 
00049 // LdapView drawing
00050 
00051 void LdapView::OnDraw(CDC* pDC)
00052 {
00053        LdapDoc* pDoc = GetDocument();
00054        ASSERT_VALID(pDoc);
00055 }
00056 
00057 void LdapView::OnInitialUpdate()
00058 {
00059        CView::OnInitialUpdate();
00060        // Create a list box the same size as the client area
00061        CRect rect;
00062        GetClientRect( &rect );
00063        m_list.Create( WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_NOTIFY, rect, this, 5000 );
00064 }
00065 
00066 void LdapView::AddLine( LPCSTR line, const char *dn )
00067 {
00068        // Add a string to the list box
00069        int ind = m_list.AddString( line );
00070        if ( NULL != dn )
00071               m_list.SetItemDataPtr( ind, (void *)strdup(dn) );
00072 }
00073 
00074 void LdapView::ClearLines()
00075 {
00076        // Remove all lines from the list box
00077        int nCount = m_list.GetCount();
00078        while ( nCount > 0 )
00079        {
00080               char *str = (char *)m_list.GetItemDataPtr( 0 );
00081               if ( str )
00082                      free( str );
00083               nCount = m_list.DeleteString( 0 );
00084        }
00085 }
00086 
00088 // LdapView diagnostics
00089 
00090 #ifdef _DEBUG
00091 void LdapView::AssertValid() const
00092 {
00093        CView::AssertValid();
00094 }
00095 
00096 void LdapView::Dump(CDumpContext& dc) const
00097 {
00098        CView::Dump(dc);
00099 }
00100 
00101 LdapDoc* LdapView::GetDocument() // non-debug version is inline
00102 {
00103        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(LdapDoc)));
00104        return (LdapDoc*)m_pDocument;
00105 }
00106 #endif //_DEBUG
00107 
00109 // LdapView message handlers
00110 
00111 void LdapView::OnSize(UINT nType, int cx, int cy) 
00112 {
00113        // Make sure the list box is resized along with the View
00114        CView::OnSize(nType, cx, cy);
00115        if ( ::IsWindow( m_list.m_hWnd ) )
00116               m_list.SetWindowPos( &wndTop, 0, 0, cx, cy, SWP_NOZORDER );
00117 }
00118 
00119 // Fetch all attributes of an entry, and display them in a dialog
00120 void LdapView::showProperties( LDAP *ld, char *dn )
00121 {
00122        PropDlg dlg;
00123        if ( ldap_search( ld, dn, LDAP_SCOPE_BASE, "objectclass=*",
00124                             NULL, FALSE ) == -1 )
00125        {
00126               AfxMessageBox( "Failed to start asynchronous search" );
00127               return;
00128        }
00129 
00130        LDAPMessage *res;
00131        int rc;
00132        // Process results as they come in
00133        while ( (rc = ldap_result( ld, LDAP_RES_ANY, 0, NULL, &res ))
00134               == LDAP_RES_SEARCH_ENTRY )
00135        {
00136               LDAPMessage *e = ldap_first_entry( ld, res );
00137               BerElement           *ber;
00138               // Loop over attributes in this entry
00139               for ( char *a = ldap_first_attribute( ld, e, &ber ); a != NULL;
00140                             a = ldap_next_attribute( ld, e, ber ) )
00141               {
00142                      struct berval **bvals;
00143                      if ( (bvals = ldap_get_values_len( ld, e, a )) != NULL )
00144                      {
00145                             dlg.AddLine( a );
00146                             // Loop over values for this attribute
00147                             for ( int i = 0; bvals[i] != NULL; i++ )
00148                             {
00149                                    CString val;
00150                                    val.Format( "    %s", bvals[ i ]->bv_val );
00151                                    dlg.AddLine( val );
00152                             }
00153                             ber_bvecfree( bvals );
00154                      }
00155               }
00156               if ( ber != NULL )
00157                      ber_free( ber, 0 );
00158               ldap_msgfree( res );
00159        }
00160        if ( rc == -1 )
00161        {
00162               AfxMessageBox( "Error on ldap_result" );
00163               return;
00164        }
00165        else if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS )
00166        {
00167               char *errString = ldap_err2string( rc );
00168               AfxMessageBox( errString );
00169        }
00170        ldap_msgfree( res );
00171        // Set the title of the dialog to the distinguished name, and display it
00172        dlg.SetTitle( dn );
00173        dlg.DoModal();
00174 }
00175 
00176 // Catch double-clicks on the list box, and fetch properties for a dn
00177 void LdapView::OnListDoubleClick()
00178 {
00179        int ind = m_list.GetCurSel();
00180        if ( ind >= 0 )
00181        {
00182               char *dn = (char *)m_list.GetItemDataPtr( ind );
00183               if ( NULL != dn )
00184               {
00185                      LDAP *ld = ((LdapApp *)AfxGetApp())->GetConnection();
00186                      if ( NULL != ld )
00187                             showProperties( ld, dn );
00188               }
00189        }
00190 }