"Fossies" - the Fresh Open Source Software Archive

Member "ldapexplorertool-2.0.1/src/mainframe.cc" (28 Nov 2006, 24113 Bytes) of package /linux/privat/old/ldapexplorertool-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mainframe.cc" see the Fossies "Dox" file reference documentation.

    1 #ifdef __GNUG__
    2     #pragma implementation "mainframe.h"
    3 #endif
    4 
    5 
    6 #include "wx/wxprec.h"
    7 
    8 #ifdef __BORLANDC__
    9     #pragma hdrstop
   10 #endif
   11 
   12 #ifndef WX_PRECOMP
   13     #include "wx/wx.h"
   14 #endif
   15 
   16 #include <wx/xrc/xmlres.h>
   17 #include <wx/config.h>
   18 #include <wx/file.h>
   19 
   20 
   21 #include "config_fct.h"
   22 #include "mainframe.h"
   23 #include "configurationdlg.h"
   24 #include "newentrydlg.h"
   25 #include "schemadlg.h"
   26 #include "attribute_add_dlg.h"
   27 #include "config_manage_dlg.h"
   28 #include "searchdlg.h"
   29 
   30 
   31 
   32 BEGIN_EVENT_TABLE( MainFrame, wxFrame )
   33     EVT_MENU(   XRCID( "file_quit" ), MainFrame::OnQuit )
   34     EVT_MENU(   XRCID( "file_open_lastconfig" ), MainFrame::OnOpenLast )
   35     EVT_MENU(   XRCID( "file_open" ), MainFrame::OnOpen )
   36     EVT_MENU(   XRCID( "new_configuration" ), MainFrame::OnNewConnection )
   37     EVT_MENU(   XRCID( "edit_configuration" ), MainFrame::OnEditConnection )
   38     EVT_MENU(   XRCID( "open_configuration" ), MainFrame::OnOpenConnection )
   39     EVT_MENU(   XRCID( "delete_configuration" ), MainFrame::OnDeleteConnection )
   40     EVT_MENU(   XRCID( "schema_view" ), MainFrame::OnViewSchema )
   41 
   42     EVT_MENU(   XRCID( "left_popup_add" ), MainFrame::OnAddEntry )
   43     
   44     EVT_MENU(   XRCID( "left_popup_copy" ), MainFrame::OnCopyEntry )
   45     EVT_MENU(   XRCID( "left_popup_rename" ), MainFrame::OnRenameEntry )
   46     EVT_MENU(   XRCID( "left_popup_delete" ), MainFrame::OnDeleteEntry )
   47     EVT_MENU(   XRCID( "left_popup_search" ), MainFrame::OnSearch )
   48     EVT_MENU(   XRCID( "right_popup_add_att" ), MainFrame::OnAddAttribute )
   49     EVT_MENU(   XRCID( "left_popup_del_att" ), MainFrame::OnDeleteAttribute )
   50     EVT_MENU(   XRCID( "right_popup_add_value" ), MainFrame::OnAddValue )
   51     EVT_MENU(   XRCID( "right_popup_add_binvalue" ), MainFrame::OnAddBinValue )
   52     EVT_MENU(   XRCID( "right_popup_mod_value" ), MainFrame::OnModifyValue )
   53     EVT_MENU(   XRCID( "right_popup_del_value" ), MainFrame::OnDeleteValue )
   54     EVT_TREE_SEL_CHANGED(XRCID( "left_tree_ctrl" ), MainFrame::OnLeftSelectionChanged ) 
   55     EVT_TREE_SEL_CHANGED(XRCID( "right_tree_ctrl" ), MainFrame::OnRightSelectionChanged ) 
   56     EVT_TREE_ITEM_ACTIVATED(XRCID( "left_tree_ctrl" ), MainFrame::OnLeftSelectionChanged )
   57     EVT_TREE_ITEM_RIGHT_CLICK( XRCID( "left_tree_ctrl" ), MainFrame::OnLeft_PopupMenu )
   58     EVT_TREE_ITEM_RIGHT_CLICK( XRCID( "right_tree_ctrl" ), MainFrame::OnRight_PopupMenu )    
   59 END_EVENT_TABLE()
   60 
   61 MainFrame::MainFrame()
   62 {
   63     wxXmlResource::Get()->LoadFrame( this, NULL, wxT("main_frame") );
   64     left_tree_ctrl = (wxTreeCtrl*)FindWindowById( XRCID("left_tree_ctrl"), this) ;
   65     right_tree_ctrl = (wxTreeCtrl*)FindWindowById( XRCID("right_tree_ctrl"), this) ;
   66 }
   67 MainFrame::~MainFrame()
   68 {
   69 }
   70 
   71 void MainFrame::OnQuit( wxCommandEvent& WXUNUSED( p_Event ) )
   72 {
   73     Close(true);
   74 }
   75 
   76 
   77 void MainFrame::OnViewSchema( wxCommandEvent& WXUNUSED( p_Event ) )
   78 {
   79     int Ret;
   80     SchemaDlg Dlg( this, m_Ldap ) ;
   81     
   82     Ret = Dlg.ShowModal( );
   83     if( Ret == wxID_OK )
   84     {
   85     }
   86 }
   87 
   88 void MainFrame::OnOpen( wxCommandEvent& WXUNUSED( p_Event ) )
   89 {
   90     Config_Manage_Dlg Dlg;
   91     if( Dlg.ShowModal() == wxID_OK )
   92     {   
   93         wxConfig::Get()->Write( wxT( "/lastconfiguration" ), Dlg.ConfigName );
   94         OnConnect( Dlg.ConfigName );
   95     }
   96 }
   97 void MainFrame::OnOpenLast( wxCommandEvent& WXUNUSED( p_Event ) )
   98 {
   99     wxString ConfigName ;
  100     
  101     ConfigName = wxConfig::Get()->Read( wxT( "/lastconfiguration" ), wxT( "" ) );
  102     if( ConfigName.Len() == 0 )
  103     {
  104         ::wxMessageBox( _( "No connection defined" ), _("Error"), wxOK| wxICON_ERROR );
  105         return ;
  106     }
  107     OnConnect( ConfigName );
  108 }
  109 void MainFrame::OnNewConnection( wxCommandEvent& WXUNUSED( p_Event ) )
  110 {
  111     int Ret;
  112     ConfigurationDlg Dlg(this) ;
  113     
  114     Ret = Dlg.ShowModal();
  115     if( Ret == wxID_OK )
  116     {
  117         wxConfig::Get()->Write( wxT( "/lastconfiguration" ), Dlg.Name );
  118         OnConnect( Dlg.Name );
  119     }
  120 }
  121 
  122 void MainFrame::OnDeleteConnection( wxCommandEvent& WXUNUSED( p_Event ) )
  123 {
  124     //wxArrayString Configurations;
  125     wxString    *Configurations;
  126     int     nConf;
  127     long            Index ;
  128     int             Ret ;
  129     
  130 
  131     GetConfigurations( Configurations, nConf ); 
  132 
  133     wxSingleChoiceDialog Dlg( this, _( "Choose a configuration to delete" ), _( "Configurations" ), 
  134     nConf, Configurations );
  135     
  136     Ret = Dlg.ShowModal();
  137     
  138     delete []Configurations;
  139     
  140     if( Ret == wxID_CANCEL )
  141     {
  142         return ;
  143     }
  144     wxString Config = wxString::Format( wxT( "/configurations/%s" ), Dlg.GetStringSelection().c_str() ) ;
  145 
  146     wxConfig::Get()->DeleteGroup( Config );
  147 }
  148 
  149 void MainFrame::OnOpenConnection( wxCommandEvent& WXUNUSED( p_Event ) )
  150 {
  151     //wxArrayString Configurations;
  152     wxString    *Configurations;
  153     int     nConf;
  154     long            Index ;
  155     int             Ret ;
  156     
  157 
  158     GetConfigurations( Configurations, nConf ); 
  159 
  160     wxSingleChoiceDialog Dlg( this, _( "Choose a configuration to open" ), _( "Configurations" ), 
  161     nConf, Configurations );
  162     
  163     Ret = Dlg.ShowModal();
  164     
  165     delete []Configurations;
  166     
  167     if( Ret == wxID_CANCEL )
  168     {
  169         return ;
  170     }
  171     wxConfig::Get()->Write( wxT( "/lastconfiguration" ), Dlg.GetStringSelection() );
  172     OnConnect( Dlg.GetStringSelection() );
  173 }
  174 
  175 void MainFrame::OnEditConnection( wxCommandEvent& WXUNUSED( p_Event ) )
  176 {
  177     //wxArrayString Configurations;
  178     wxString    *Configurations;
  179     int     nConf;
  180     
  181     int             Ret ;
  182     
  183     
  184     GetConfigurations( Configurations, nConf );
  185     
  186     wxSingleChoiceDialog Dlg( this, _( "Choose a configuration to open" ), _( "Configurations" ), 
  187     nConf, Configurations );
  188     
  189     Ret = Dlg.ShowModal();
  190     
  191     delete []Configurations;
  192     
  193     if( Ret == wxID_CANCEL )
  194     {
  195         return ;
  196     }
  197     
  198     ConfigurationDlg DlgConf( this, Dlg.GetStringSelection() );
  199     
  200     Ret = DlgConf.ShowModal();
  201     if( Ret == wxID_OK )
  202     {
  203         wxConfig::Get()->Write( wxT( "/lastconfiguration" ), DlgConf.Name );
  204         OnConnect( DlgConf.Name );
  205     }
  206 }
  207 
  208 void MainFrame::OnLeftSelectionChanged( wxTreeEvent& p_Event )
  209 {
  210     wxTreeItemId Id = p_Event.GetItem() ;
  211     if( Id > 0 )
  212     {
  213         LDAPItemData* data = ( LDAPItemData* )left_tree_ctrl->GetItemData( p_Event.GetItem() ) ;
  214 
  215         Right_Display( data->m_Value );
  216         if( left_tree_ctrl->GetChildrenCount( Id, false ) == 0 )
  217         {
  218             Left_Display( Id, data->m_Value );
  219         }
  220     }
  221 }
  222 
  223 void MainFrame::OnRightSelectionChanged( wxTreeEvent& WXUNUSED( p_Event ) )
  224 {
  225 }
  226 
  227 void MainFrame::OnRight_PopupMenu( wxTreeEvent& p_Event )
  228 {
  229     wxTreeItemId Id = right_tree_ctrl->GetSelection() ;
  230     if( !Id.IsOk() )
  231         return ;
  232     wxTreeItemId RootId = right_tree_ctrl->GetRootItem();
  233     
  234     wxMenu *Popup = new wxMenu( _( "Entry Attributes" ) );
  235     if( Popup == NULL )
  236         return ;
  237     if( Id == RootId )
  238     {
  239         Popup->Append( XRCID( "right_popup_add_att" ), _( "Add Attribute..." ) );
  240     }
  241     else if( right_tree_ctrl->GetItemParent( Id ) == RootId )
  242     {
  243         Popup->Append( XRCID( "left_popup_del_att" ), _( "Delete Attribute..." ) );
  244         Popup->AppendSeparator();
  245         Popup->Append( XRCID( "right_popup_add_value" ), _( "Add value..." ) );
  246         Popup->Append( XRCID( "right_popup_add_binvalue" ), _( "Add binary value..." ) );
  247     }
  248     else
  249     {
  250         Popup->Append( XRCID( "right_popup_mod_value" ), _( "Modifiy value..." ) );     
  251         Popup->Append( XRCID( "right_popup_del_value" ), _( "Delete value" ) );     
  252         
  253     }
  254     PopupMenu( Popup, wxDefaultPosition );
  255 }
  256 
  257 
  258 
  259 void MainFrame::OnLeft_PopupMenu( wxTreeEvent& p_Event )
  260 {
  261     wxTreeItemId Id = p_Event.GetItem() ;
  262     if( !Id.IsOk() )
  263         return ;
  264     wxTreeItemId RootId = left_tree_ctrl->GetRootItem();
  265 
  266     wxMenu *Popup = new wxMenu( _( "Entry" ) );
  267     if( Popup == NULL )
  268         return ;
  269 
  270     Popup->Append( XRCID( "left_popup_add" ), _( "Add..." ) );
  271     Popup->Append( XRCID( "left_popup_copy" ), _( "Copy to..." ) );
  272     Popup->Append( XRCID( "left_popup_rename" ), _( "Rename..." ) );
  273     Popup->Append( XRCID( "left_popup_delete" ), _( "Delete..." ) );
  274     Popup->AppendSeparator();
  275     Popup->Append( XRCID( "left_popup_search" ), _( "Search..." ) );        
  276     
  277     PopupMenu( Popup, wxDefaultPosition );
  278 }
  279 
  280 void MainFrame::OnConnect( const wxString& p_Config )
  281 {
  282     CnxConfiguration Config ;
  283     u_long          Version;
  284     u_long          Port;
  285     u_long          SSLPort;
  286     int             Err;
  287     bool            UseSSL = false ;
  288     bool            UseTLS = false ;
  289     int             Ret ;
  290 
  291     left_tree_ctrl->DeleteAllItems();
  292     right_tree_ctrl->DeleteAllItems();
  293     
  294 
  295     m_ConfigName = p_Config ;       
  296     ReadConfiguration( p_Config, Config );
  297     
  298     Config.port.ToULong( &Port );
  299     Config.sslport.ToULong( &SSLPort );
  300     Config.version.ToULong( &Version );
  301     
  302     
  303     if( Config.usessl == _("Yes") )
  304     {
  305         UseSSL = true ;
  306         Port = SSLPort;
  307     }
  308     else if( Config.usetls == _("Yes") )
  309     {
  310         UseTLS = true ;
  311         
  312     }
  313     
  314     if( UseSSL || UseTLS )
  315     {
  316         Ret = m_Ldap.SetCACert( Config.cacert );
  317         if( Ret != LDAP_SUCCESS )
  318         {
  319             ::wxMessageBox( m_Ldap.GetErrorStr(), _("Set CA Cert" ) );
  320         }
  321         if( Config.certfile.Len() > 0 && Config.keyfile.Len() > 0 )
  322         {
  323             Ret = m_Ldap.SetClientCert( Config.certfile );
  324             Ret = m_Ldap.SetClientKey( Config.keyfile );
  325         }
  326     }
  327     Ret = m_Ldap.Init( Config.server, Port );
  328     if( Ret != LDAP_SUCCESS )
  329     {
  330         ::wxMessageBox( wxString::Format( wxT( "LDAP open:%s" ), m_Ldap.GetErrorStr().c_str() ) );
  331         return;
  332     }
  333     if( m_Ldap.SetVersion( Version ) != LDAP_SUCCESS )
  334     {
  335         ::wxMessageBox( wxString::Format( wxT( "LDAP SetVersion:%s" ), m_Ldap.GetErrorStr().c_str() ) );
  336         return;
  337     }
  338     
  339     if( UseTLS )
  340     {   
  341         Ret = m_Ldap.StartTLS( );
  342         if( Ret != LDAP_SUCCESS )
  343         {
  344             ::wxMessageBox( wxString::Format( wxT( "LDAP StartTLS:%s" ), m_Ldap.GetErrorStr().c_str() ) );
  345             return;
  346         }
  347     }
  348     if( UseSSL )
  349     {
  350         int SSLArg = LDAP_OPT_X_TLS_HARD ;
  351         Ret = m_Ldap.SetOption( LDAP_OPT_X_TLS, &SSLArg );
  352         if( Ret != LDAP_SUCCESS )
  353         {
  354             ::wxMessageBox( m_Ldap.GetErrorStr().c_str(), _( "SetOption:LDAP_OPT_X_TLS" )  );
  355             return;
  356         }
  357     }
  358 
  359     unsigned long SizeLimit ;
  360     unsigned long TimeLimit ;
  361     
  362     Config.sizelimit.ToULong( &SizeLimit );
  363     Config.timelimit.ToULong( &TimeLimit );
  364     
  365     if( Config.sizelimit.Len() > 0 )
  366     {
  367         Err = m_Ldap.SetSizeLimit( SizeLimit );
  368     }
  369     
  370     if( Config.timelimit.Len() > 0 )
  371     {
  372         Err = m_Ldap.SetTimeLimit( TimeLimit );
  373     }
  374     
  375     if( Config.referals == _( "Yes" ) )
  376     {
  377         Err = m_Ldap.Referals( true ) ;
  378     }
  379     else
  380     {
  381         Err = m_Ldap.Referals( false ) ;
  382     }
  383     
  384     if( Config.anonymous_login == wxT( "1" ) )
  385     {
  386         Err = m_Ldap.SimpleBind();
  387     }
  388     else
  389     {
  390         if( Config.storepwd )
  391         {
  392             Err = m_Ldap.SimpleBind( Config.userdn, Config.password );
  393         }
  394         else
  395         {
  396             //wxPasswordEntryDialog Dlg( this, wxT("Enter user password") );
  397             wxTextEntryDialog Dlg( this, wxT("Enter user password"),wxT("Enter password"), wxT(""), wxOK|wxCANCEL|wxTE_PASSWORD );
  398             Dlg.ShowModal();
  399             Err = m_Ldap.SimpleBind( Config.userdn, Dlg.GetValue());
  400         }
  401     }
  402     
  403     if( Err != LDAP_SUCCESS )
  404     {
  405         ::wxMessageBox( wxString::Format( wxT( "LDAP Bind:%s" ), m_Ldap.GetErrorStr().c_str() ) );
  406         return;
  407     }
  408     
  409     m_Ldap.SetConfig( m_ConfigName );
  410     if( Config.guessbinary.CmpNoCase( _("Yes" ) ) == 0 )
  411         m_Ldap.GuessBinary( true );
  412     else
  413         m_Ldap.GuessBinary( false );
  414     
  415     m_Ldap.GetSchema( Config.basedn );
  416     
  417     wxTreeItemId RootID = left_tree_ctrl->AddRoot( wxString::Format( _("Root-%s"), Config.basedn.c_str() ) );
  418     left_tree_ctrl->SetItemData( RootID, new LDAPItemData( Config.basedn ) );
  419     Left_Display( RootID, Config.basedn );
  420 
  421 }
  422 
  423 void MainFrame::Left_Display( wxTreeItemId p_ParentId, const wxString& p_Url )
  424 {
  425     int Id;
  426     int Ret;
  427     
  428     
  429     Ret = m_Ldap.Search( Id, p_Url, LDAP_SCOPE_ONELEVEL, wxT( "objectclass=*" ), 
  430     NULL, false, LF_NONE );
  431     if( Ret != LDAP_SUCCESS )
  432     {
  433         return ;
  434     }
  435     
  436 
  437     int NbEntries = m_Ldap.GetEntryCount( Id ) ;
  438     for( int i = 0; i < NbEntries; i++ )
  439     {
  440         LdapEntry Entry ;
  441         if( !m_Ldap.GetEntry( Id, i, Entry ) )
  442         {
  443             ::wxMessageBox( m_Ldap.GetErrorStr(), _("Error"), wxOK|wxICON_ERROR );
  444         }
  445         
  446         wxTreeItemId NewId = left_tree_ctrl->AppendItem( p_ParentId, Entry.GetEndOfDn() );
  447         left_tree_ctrl->SetItemData( NewId, new LDAPItemData( Entry.GetDn() ) );
  448     }
  449     m_Ldap.CloseSearch( Id );
  450 }
  451 
  452 
  453 void MainFrame::Right_Display( wxString p_Url )
  454 {
  455     int Id;
  456     int Ret;
  457     
  458     right_tree_ctrl->DeleteAllItems();
  459     
  460     Ret = m_Ldap.Search( Id, p_Url, LDAP_SCOPE_BASE, wxT( "objectclass=*" ), 
  461     NULL, true, LF_ALL_OPERATIONAL );
  462     if( Ret != LDAP_SUCCESS )
  463     {
  464         ::wxMessageBox( wxString::Format( wxT( "Search:%s" ),m_Ldap.GetErrorStr().c_str() ), _("Error"), wxOK|wxICON_ERROR );
  465         return ;
  466     }
  467     
  468 
  469     LdapEntry Entry ;
  470     if( !m_Ldap.GetEntry( Id, 0, Entry ) )
  471     {
  472         ::wxMessageBox( m_Ldap.GetErrorStr(), _("Error"), wxOK|wxICON_ERROR );
  473     }
  474     wxTreeItemId NewId = right_tree_ctrl->AddRoot( Entry.GetDn() );
  475     right_tree_ctrl->SetItemData( NewId, new LDAPItemData( Entry.GetDn() ) );
  476     
  477     long Nb = Entry.CountAttributes();
  478     for( long i = 0; i < Nb ; i++ )
  479     {
  480         LdapAttribute* Attribute = Entry.GetAttribute( i );
  481         
  482         wxTreeItemId ItemId = right_tree_ctrl->AppendItem( NewId, Attribute->GetName() );
  483         long NbVals = Attribute->CountValues();
  484         
  485         for( long j = 0; j < NbVals; j++ )
  486         {
  487             wxTreeItemId AttValId = right_tree_ctrl->AppendItem( ItemId, Attribute->GetValue( j ) );
  488             if( Attribute->IsBinary() )
  489             {
  490                 right_tree_ctrl->SetItemData( AttValId, 
  491                 new ValueItemData( BINARY_VALUES, ber_bvdup( Attribute->GetBinaryValue( j ) ) ) );
  492             }
  493             else
  494             {
  495                 right_tree_ctrl->SetItemData( AttValId, new ValueItemData( STRING_VALUES ) );
  496             }
  497         }
  498     }
  499     m_Ldap.CloseSearch( Id );   
  500 }
  501 /*
  502 void MainFrame::GetConfigurations( wxArrayString& p_List )
  503 {
  504     wxString        ConfigurationName ;
  505     long            Index ;
  506     bool            bRet ;
  507 
  508     wxConfig::Get()->SetPath( wxT( "/configurations" ) );
  509     
  510     bRet = wxConfig::Get()->GetFirstGroup( ConfigurationName, Index );
  511     while( bRet )
  512     {
  513         p_List.Add( ConfigurationName );
  514         bRet = wxConfig::Get()->GetNextGroup( ConfigurationName, Index );
  515     }
  516     
  517 }
  518 */
  519 void MainFrame::OnAddEntry( wxCommandEvent& WXUNUSED( p_Event ) )
  520 {
  521     wxTreeItemId Id ;
  522     wxTreeItemId NewId ;
  523     int     Ret;
  524     
  525     Id = left_tree_ctrl->GetSelection();
  526     if( !Id.IsOk() )
  527         return ;
  528     LDAPItemData* Data = (LDAPItemData*)left_tree_ctrl->GetItemData( Id ) ;
  529     
  530     NewEntryDlg Dlg( this, Data->m_Value, m_Ldap );
  531 
  532         
  533     Ret = Dlg.ShowModal();
  534     if( Ret == wxID_OK )
  535     {
  536         NewId = left_tree_ctrl->AppendItem( Id, Dlg.m_NewRdn );
  537         left_tree_ctrl->SetItemData( NewId, new LDAPItemData( Dlg.m_NewDn ) );
  538     }
  539 }
  540 
  541 void MainFrame::OnCopyEntry( wxCommandEvent& WXUNUSED( p_Event ) )
  542 {
  543     wxTreeItemId    Id ;
  544     int             SId;
  545     int             Ret;
  546     int             Choice ;
  547     int             Scope ;
  548     wxString        NewRDn;
  549     LdapEntry       Entry;
  550     wxArrayString   Choices;
  551     
  552     Choices.Add( _( "Selected entry" ) );
  553     Choices.Add( _( "Selected entry sub-level" ) );
  554     Choices.Add( _( "Selected entry and all sub-levels" ) );
  555 
  556     Id = left_tree_ctrl->GetSelection();
  557     if( !Id.IsOk() )
  558         return ;
  559 
  560     Choice = ::wxGetSingleChoiceIndex( _( "Select the entries you wish to copy" ), 
  561     _( "Copy To..." ), Choices );
  562     if( Choice == -1 )
  563         return ;
  564         
  565     switch( Choice )
  566     {
  567         case 0:
  568             Scope = LDAP_SCOPE_BASE ;
  569         break;
  570         case 1:
  571             Scope = LDAP_SCOPE_ONELEVEL ;
  572         break;
  573         case 2:
  574             Scope = LDAP_SCOPE_SUBTREE;
  575         break;
  576     }
  577     LDAPItemData* Data = (LDAPItemData*)left_tree_ctrl->GetItemData( Id ) ;
  578     
  579     Ret = m_Ldap.Search( SId, Data->m_Value, Scope, wxT( "objectClass=*" ), NULL,
  580     true, LF_ALL );
  581     if( Ret != LDAP_SUCCESS )
  582     {
  583         ::wxMessageBox( m_Ldap.GetErrorStr(), Data->m_Value );
  584         return;
  585     }
  586     
  587     wxString NewDn = ::wxGetTextFromUser( _( "Enter the new parent DN" ), Data->m_Value, wxT( "" ) );
  588     if( NewDn == wxT( "" ) )
  589         return ;
  590 
  591     wxString OldDn;
  592     if( Choice == 1 )
  593     {
  594         OldDn = Data->m_Value;
  595     }
  596     else
  597     {
  598         OldDn = Data->m_Value.AfterFirst( ',' );
  599     }
  600     Ret = m_Ldap.CopyTo( SId, NewDn, OldDn );
  601     if( Ret != LDAP_SUCCESS )
  602     {
  603         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  604     }
  605     
  606     m_Ldap.CloseSearch( SId );
  607 }
  608 
  609 void MainFrame::OnRenameEntry( wxCommandEvent& WXUNUSED( p_Event ) )
  610 {
  611     wxTreeItemId    Id ;
  612     int             Ret;
  613     wxString        NewRDn ;
  614     Id = left_tree_ctrl->GetSelection();
  615     if( !Id.IsOk() )
  616         return ;
  617         
  618     LDAPItemData* Data = (LDAPItemData*)left_tree_ctrl->GetItemData( Id ) ;
  619 
  620     NewRDn = ::wxGetTextFromUser( _("Enter the new RDN of the entry" ), Data->m_Value ) ;
  621     if( NewRDn.Len() == 0 )
  622         return;
  623         
  624     Ret = m_Ldap.Rename( Data->m_Value, NewRDn );
  625     if( Ret != LDAP_SUCCESS )
  626     {
  627         ::wxMessageBox( m_Ldap.GetErrorStr(), Data->m_Value ) ;
  628     }
  629     else
  630     {
  631         
  632         int Pos = Data->m_Value.Find( ',' );
  633         if ( Pos == -1 )
  634         {
  635             Data->m_Value = NewRDn ;
  636             ::wxMessageBox ( wxT( "test1" ) );
  637         }
  638         else
  639         {
  640             Data->m_Value = (Data->m_Value.Right( ( Data->m_Value).Length() - Pos )).Prepend(NewRDn);
  641         }
  642         left_tree_ctrl->SetItemData( Id, Data );
  643         left_tree_ctrl->SetItemText( Id, NewRDn );
  644     }
  645 }
  646 
  647 void MainFrame::OnSearch( wxCommandEvent& WXUNUSED( p_Event ) )
  648 {
  649 
  650     wxTreeItemId Id = left_tree_ctrl->GetSelection();
  651     if( !Id.IsOk() )
  652         return ;
  653         
  654     
  655     LDAPItemData* Data = (LDAPItemData*)left_tree_ctrl->GetItemData( Id ) ;
  656     
  657     SearchDlg Dlg( this, &m_Ldap, Data->m_Value );
  658     
  659     if( Dlg.ShowModal() == wxID_OK )
  660     {
  661         left_tree_ctrl->DeleteAllItems();
  662         right_tree_ctrl->DeleteAllItems();
  663 
  664         wxTreeItemId RootID = left_tree_ctrl->AddRoot( Data->m_Value );
  665         left_tree_ctrl->SetItemData( RootID, new LDAPItemData( Data->m_Value ) );
  666         int NbEntries = m_Ldap.GetEntryCount( Dlg.Id );
  667         for( int i = 0; i < NbEntries; i++ )
  668         {
  669             LdapEntry Entry ;
  670             if( !m_Ldap.GetEntry( Dlg.Id, i, Entry ) )
  671             {
  672                 ::wxMessageBox( m_Ldap.GetErrorStr(), _("Error"), wxOK|wxICON_ERROR );
  673             }
  674             
  675             wxTreeItemId NewId = left_tree_ctrl->AppendItem( RootID, Entry.GetDn() );
  676             left_tree_ctrl->SetItemData( NewId, new LDAPItemData( Entry.GetDn() ) );
  677         }
  678         m_Ldap.CloseSearch( Dlg.Id );
  679     }
  680 }
  681 
  682 void MainFrame::OnDeleteEntry( wxCommandEvent& WXUNUSED( p_Event ) )
  683 {
  684     LdapEntry Entry ;
  685     wxTreeItemId    Id ;
  686     int             Ret;
  687     int             SId;
  688         
  689     Id = left_tree_ctrl->GetSelection();
  690     if( !Id.IsOk() )
  691         return ;
  692         
  693     LDAPItemData* Data = (LDAPItemData*)left_tree_ctrl->GetItemData( Id ) ;
  694     
  695     if( ::wxMessageBox( _("Do you really want to delete this entry and all its sub-entries?"), Data->m_Value, wxYES_NO ) == wxNO )
  696     {
  697         return;
  698     }
  699     
  700     Ret = m_Ldap.Search( SId, Data->m_Value, LDAP_SCOPE_SUBTREE, wxT( "objectClass=*" ), NULL,
  701     true, LF_NONE );
  702     if( Ret != LDAP_SUCCESS )
  703     {
  704         ::wxMessageBox( m_Ldap.GetErrorStr(), Data->m_Value );
  705         return;
  706     }
  707         
  708     
  709     int nEntries = m_Ldap.GetEntryCount( SId );
  710     
  711     for( long i = nEntries - 1; i >= 0; i-- )
  712     {
  713         m_Ldap.GetEntry( SId, i, Entry );
  714         Ret = m_Ldap.DeleteEntry( Entry );
  715         if( Ret != LDAP_SUCCESS )
  716         {
  717             ::wxMessageBox( m_Ldap.GetErrorStr(), Data->m_Value ) ;
  718             break;
  719         }
  720     }
  721     if( Ret == LDAP_SUCCESS )
  722     {
  723         left_tree_ctrl->Delete( Id );
  724     }
  725 }
  726 
  727 void MainFrame::OnAddAttribute( wxCommandEvent& WXUNUSED( p_Event ) )
  728 {
  729     int Id;
  730     int Ret;
  731     wxTreeItemId RootId;
  732     LDAPItemData *Data ;
  733     LdapEntry   Entry, NewEntry ;
  734     LdapAttribute *Attribute ;
  735     wxString ObjectClass;
  736     
  737     RootId = right_tree_ctrl->GetRootItem();
  738     Data = (LDAPItemData*)right_tree_ctrl->GetItemData( RootId );
  739     
  740     Ret = m_Ldap.Search( Id, Data->m_Value, LDAP_SCOPE_BASE, wxT( "objectclass=*" ), 
  741     NULL, true, LF_ALL_OPERATIONAL );
  742     if( Ret != LDAP_SUCCESS )
  743     {
  744         ::wxMessageBox( m_Ldap.GetErrorStr(), Data->m_Value );
  745         return;
  746     }
  747     
  748     Ret = m_Ldap.GetEntry( Id, 0, Entry );
  749     
  750     Attribute = Entry.GetAttribute( wxT( "objectclass" ), true );
  751     ObjectClass = Attribute->GetValue( 0 ) ;
  752     m_Ldap.CloseSearch( Id );
  753     
  754     Attribute_Add_Dlg Dlg( this, Data->m_Value, ObjectClass, m_Ldap );
  755     
  756     Ret = Dlg.ShowModal();
  757     if( Ret == wxID_OK )
  758     {
  759         Right_Display( Data->m_Value );
  760     }
  761     
  762 }
  763 
  764 void MainFrame::OnDeleteAttribute( wxCommandEvent& WXUNUSED( p_Event ) )
  765 {
  766     wxTreeItemId Id = right_tree_ctrl->GetSelection(); 
  767     wxTreeItemId RootId = right_tree_ctrl->GetRootItem();
  768     if( !Id.IsOk() )
  769         return ;
  770     
  771     wxString Attribute = right_tree_ctrl->GetItemText( Id );
  772     if( ::wxMessageBox( _("Do you really want to delete this attribute?"), Attribute, wxYES_NO ) == wxYES )
  773     {
  774         //LdapEntry Entry ;
  775         LDAPItemData* Data = ( LDAPItemData* )right_tree_ctrl->GetItemData( RootId ) ;
  776         /*
  777         Entry.SetDn( Data->m_Value );
  778         Entry.AddValue( Attribute, "" );
  779         */
  780         int Err = m_Ldap.DeleteAttribute( Data->m_Value, Attribute );
  781         if( Err != LDAP_SUCCESS )
  782         {
  783             ::wxMessageBox( m_Ldap.GetErrorStr(), Attribute );
  784         }
  785         else
  786         {
  787             right_tree_ctrl->Delete( Id );
  788         }
  789     }   
  790     
  791     
  792     
  793 }
  794 
  795 void MainFrame::OnAddValue( wxCommandEvent& WXUNUSED( p_Event ) )
  796 {
  797     wxTreeItemId    Id ;
  798     wxTreeItemId    RootId ;
  799     wxTreeItemId    ParentId ;
  800     LDAPItemData*   data;
  801     wxString        Value ;
  802     wxString        Attribute ;
  803     int             Err ;
  804     
  805     Id = right_tree_ctrl->GetSelection();
  806     if( !Id.IsOk() )
  807         return ;
  808 
  809     ParentId = right_tree_ctrl->GetItemParent( Id ) ;
  810     RootId = right_tree_ctrl->GetRootItem();
  811     
  812     if( ParentId != RootId )
  813     {
  814         Id = ParentId;
  815     }
  816     Attribute = right_tree_ctrl->GetItemText( Id ) ;
  817     data = ( LDAPItemData* )right_tree_ctrl->GetItemData( RootId ) ;
  818     
  819     
  820     Value = ::wxGetTextFromUser( _( "Enter a value for the item" ), Attribute , wxT( "" ) );
  821     if( Value.Len() == 0 )
  822     {
  823         return ;
  824     }
  825     
  826     Err = m_Ldap.AddAttributeValue( data->m_Value, Attribute, Value );
  827     if( Err != LDAP_SUCCESS )
  828     {
  829         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  830     }
  831     else
  832     {
  833         Right_Display( data->m_Value );
  834     }
  835             
  836 }
  837 void MainFrame::OnAddBinValue( wxCommandEvent& WXUNUSED( p_Event ) )
  838 {
  839     wxTreeItemId    Id ;
  840     wxTreeItemId    RootId ;
  841     wxTreeItemId    ParentId ;
  842     LDAPItemData*   data;
  843     wxString        Attribute ;
  844     int             Err ;
  845     
  846     Id = right_tree_ctrl->GetSelection();
  847     if( !Id.IsOk() )
  848         return ;
  849 
  850     ParentId = right_tree_ctrl->GetItemParent( Id ) ;
  851     RootId = right_tree_ctrl->GetRootItem();
  852     
  853     if( ParentId != RootId )
  854     {
  855         Id = ParentId;
  856     }
  857     Attribute = right_tree_ctrl->GetItemText( Id ) ;
  858     data = ( LDAPItemData* )right_tree_ctrl->GetItemData( RootId ) ;
  859     
  860     
  861     wxString Filename = wxFileSelector( _( "Choose a file to open" ));
  862     if ( Filename.empty() )
  863     {
  864         return ;
  865     }
  866 
  867     wxFile File( Filename );
  868     if( !File.IsOpened() )
  869     {
  870         ::wxMessageBox( _( "File could not be opened" ), Filename );
  871         return;
  872     }
  873     
  874     berval  Ber ;
  875     
  876     Ber.bv_len = File.Length() ;
  877     Ber.bv_val = new char[ Ber.bv_len ] ;
  878     File.Read( Ber.bv_val, Ber.bv_len );
  879 
  880     
  881     Err = m_Ldap.AddAttributeValue( data->m_Value, Attribute, Ber );
  882     
  883     delete []Ber.bv_val ;
  884     if( Err != LDAP_SUCCESS )
  885     {
  886         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  887     }
  888     else
  889     {
  890         Right_Display( data->m_Value );
  891     }
  892             
  893 }
  894 
  895 void MainFrame::OnModifyValue( wxCommandEvent& WXUNUSED( p_Event ) )
  896 {
  897     wxTreeItemId    Id ;
  898     wxTreeItemId    RootId ;
  899     wxTreeItemId    ParentId ;
  900     LDAPItemData*   data;
  901     wxString        OldValue ;
  902     wxString        NewValue;
  903     wxString        Attribute ;
  904     int             Err ;
  905     
  906     Id = right_tree_ctrl->GetSelection();
  907     if( !Id.IsOk() )
  908         return ;
  909 
  910     ParentId = right_tree_ctrl->GetItemParent( Id ) ;
  911     RootId = right_tree_ctrl->GetRootItem();
  912     
  913     if( ParentId == RootId )
  914     {
  915         return ;
  916     }
  917     Attribute = right_tree_ctrl->GetItemText( ParentId ) ;
  918     data = ( LDAPItemData* )right_tree_ctrl->GetItemData( RootId ) ;
  919     OldValue = right_tree_ctrl->GetItemText( Id );
  920     
  921     /*
  922     OldEntry.SetDn( data->m_Value );
  923     NewEntry.SetDn( data->m_Value );
  924     */
  925     
  926     NewValue = ::wxGetTextFromUser( _( "Enter a value for the item" ), Attribute , wxT( "" ) );
  927     if( NewValue.Len() == 0 )
  928     {
  929         return ;
  930     }
  931     
  932     /*
  933     OldEntry.AddValue( Attribute, OldValue );
  934     NewEntry.AddValue( Attribute, NewValue );
  935     */
  936     Err = m_Ldap.AddAttributeValue( data->m_Value, Attribute, NewValue );
  937     //Err = m_Ldap.AddAttributeValue( NewEntry );
  938     if( Err != LDAP_SUCCESS )
  939     {
  940         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  941         return ;
  942     }
  943     Err = m_Ldap.DeleteAttributeValue( data->m_Value, Attribute, OldValue );
  944     if( Err != LDAP_SUCCESS )
  945     {
  946         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  947     }
  948     Right_Display( data->m_Value );
  949 }
  950 
  951 void MainFrame::OnDeleteValue( wxCommandEvent& WXUNUSED( p_Event ) )
  952 {
  953     wxTreeItemId    Id ;
  954     wxTreeItemId    ParentId;
  955     wxTreeItemId    RootId ;
  956     LDAPItemData*   data;
  957     ValueItemData*  valuedata ;
  958     LdapEntry       Entry ;
  959     wxString        Value ;
  960     wxString        Attribute ;
  961     int             Err ;
  962     
  963     Id = right_tree_ctrl->GetSelection();
  964     if( !Id.IsOk() )
  965         return ;
  966     
  967     RootId = right_tree_ctrl->GetRootItem();
  968     ParentId = right_tree_ctrl->GetItemParent( Id ) ;
  969     if( ParentId == RootId )
  970     {
  971         return ;
  972     }
  973     
  974     Attribute = right_tree_ctrl->GetItemText( ParentId ) ;
  975     Value = right_tree_ctrl->GetItemText( Id ) ;
  976     
  977     data = ( LDAPItemData* )right_tree_ctrl->GetItemData( RootId ) ;
  978     
  979     valuedata = ( ValueItemData* )right_tree_ctrl->GetItemData( Id ) ;
  980     
  981     if( valuedata != NULL && valuedata->m_Type == BINARY_VALUES )
  982     {
  983         Err = m_Ldap.DeleteAttributeValue( data->m_Value, Attribute, *( valuedata->m_BerVal ) );
  984     }
  985     else
  986     {
  987         Err = m_Ldap.DeleteAttributeValue( data->m_Value, Attribute, Value );
  988     }
  989     if( Err != LDAP_SUCCESS )
  990     {
  991         ::wxMessageBox( m_Ldap.GetErrorStr(), _( "Error" ) );
  992     }
  993     else
  994     {
  995         Right_Display( data->m_Value );
  996     }
  997     
  998 }