LCOV - code coverage report
Current view: top level - dbaccess/source/ui/dlg - dbfindex.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 239 0.0 %
Date: 2014-04-14 Functions: 0 26 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "dbfindex.hxx"
      21             : #include <comphelper/processfactory.hxx>
      22             : #include <tools/config.hxx>
      23             : #include <sfx2/app.hxx>
      24             : #include "moduledbu.hxx"
      25             : #include "dbu_dlg.hrc"
      26             : #include <osl/diagnose.h>
      27             : #include <unotools/localfilehelper.hxx>
      28             : #include <tools/urlobj.hxx>
      29             : #include <unotools/pathoptions.hxx>
      30             : #include <ucbhelper/content.hxx>
      31             : #include <svl/filenotation.hxx>
      32             : #include <rtl/strbuf.hxx>
      33             : 
      34             : namespace dbaui
      35             : {
      36             : using namespace ::com::sun::star::uno;
      37             : using namespace ::com::sun::star::ucb;
      38             : using namespace ::svt;
      39             : 
      40           0 : const OString aGroupIdent("dBase III");
      41             : 
      42             : 
      43           0 : ODbaseIndexDialog::ODbaseIndexDialog(Window * pParent, const OUString& aDataSrcName)
      44             :     : ModalDialog(pParent, "DBaseIndexDialog", "dbaccess/ui/dbaseindexdialog.ui")
      45             :     , m_aDSN(aDataSrcName)
      46           0 :     , m_bCaseSensitiv(true)
      47             : {
      48           0 :     get(m_pPB_OK, "ok");
      49           0 :     get(m_pCB_Tables, "table");
      50           0 :     get(m_pIndexes, "frame");
      51           0 :     get(m_pLB_TableIndexes, "tableindex");
      52           0 :     get(m_pLB_FreeIndexes, "freeindex");
      53           0 :     Size aSize(LogicToPixel(Size(76, 98), MAP_APPFONT));
      54           0 :     m_pLB_TableIndexes->set_height_request(aSize.Height());
      55           0 :     m_pLB_TableIndexes->set_width_request(aSize.Width());
      56           0 :     m_pLB_FreeIndexes->set_height_request(aSize.Height());
      57           0 :     m_pLB_FreeIndexes->set_width_request(aSize.Width());
      58           0 :     get(m_pAdd, "add");
      59           0 :     get(m_pAddAll, "addall");
      60           0 :     get(m_pRemove, "remove");
      61           0 :     get(m_pRemoveAll, "removeall");
      62             : 
      63             : 
      64           0 :     m_pCB_Tables->SetSelectHdl( LINK(this, ODbaseIndexDialog, TableSelectHdl) );
      65           0 :     m_pAdd->SetClickHdl( LINK(this, ODbaseIndexDialog, AddClickHdl) );
      66           0 :     m_pRemove->SetClickHdl( LINK(this, ODbaseIndexDialog, RemoveClickHdl) );
      67           0 :     m_pAddAll->SetClickHdl( LINK(this, ODbaseIndexDialog, AddAllClickHdl) );
      68           0 :     m_pRemoveAll->SetClickHdl( LINK(this, ODbaseIndexDialog, RemoveAllClickHdl) );
      69           0 :     m_pPB_OK->SetClickHdl( LINK(this, ODbaseIndexDialog, OKClickHdl) );
      70             : 
      71           0 :     m_pLB_FreeIndexes->SetSelectHdl( LINK(this, ODbaseIndexDialog, OnListEntrySelected) );
      72           0 :     m_pLB_TableIndexes->SetSelectHdl( LINK(this, ODbaseIndexDialog, OnListEntrySelected) );
      73             : 
      74           0 :     m_pCB_Tables->SetDropDownLineCount(8);
      75           0 :     Init();
      76           0 :     SetCtrls();
      77           0 : }
      78             : 
      79           0 : sal_Bool ODbaseIndexDialog::GetTable(const OUString& _rName, TableInfoList::iterator& _rPosition)
      80             : {
      81           0 :     for (   _rPosition = m_aTableInfoList.begin();
      82           0 :             _rPosition != m_aTableInfoList.end();
      83             :             ++_rPosition
      84             :         )
      85             :     {
      86           0 :         if (m_bCaseSensitiv)
      87             :         {
      88           0 :             if (_rPosition->aTableName == _rName)
      89           0 :                 return sal_True;
      90             :         }
      91             :         else
      92             :         {
      93           0 :             if (_rPosition->aTableName.equalsIgnoreAsciiCase(_rName))
      94           0 :                 return sal_True;
      95             :         }
      96             :     }
      97           0 :     return sal_False;
      98             : }
      99             : 
     100           0 : void ODbaseIndexDialog::checkButtons()
     101             : {
     102           0 :     m_pAdd->Enable(0 != m_pLB_FreeIndexes->GetSelectEntryCount());
     103           0 :     m_pAddAll->Enable(0 != m_pLB_FreeIndexes->GetEntryCount());
     104             : 
     105           0 :     m_pRemove->Enable(0 != m_pLB_TableIndexes->GetSelectEntryCount());
     106           0 :     m_pRemoveAll->Enable(0 != m_pLB_TableIndexes->GetEntryCount());
     107           0 : }
     108             : 
     109           0 : OTableIndex ODbaseIndexDialog::implRemoveIndex(const OUString& _rName, TableIndexList& _rList, ListBox& _rDisplay, sal_Bool _bMustExist)
     110             : {
     111           0 :     OTableIndex aReturn;
     112             : 
     113           0 :     sal_Int32 nPos = 0;
     114             : 
     115           0 :     TableIndexList::iterator aSearch;
     116           0 :     for (   aSearch = _rList.begin();
     117           0 :             aSearch != _rList.end();
     118             :             ++aSearch, ++nPos
     119             :         )
     120             :     {
     121           0 :         if ( m_bCaseSensitiv ? aSearch->GetIndexFileName() == _rName : aSearch->GetIndexFileName().equalsIgnoreAsciiCase(_rName) )
     122             :         {
     123           0 :             aReturn = *aSearch;
     124             : 
     125           0 :             _rList.erase(aSearch);
     126           0 :             _rDisplay.RemoveEntry( _rName );
     127             : 
     128             :             // adjust selection if necessary
     129           0 :             if ((sal_uInt32)nPos == _rList.size())
     130           0 :                 _rDisplay.SelectEntryPos((sal_uInt16)nPos-1);
     131             :             else
     132           0 :                 _rDisplay.SelectEntryPos((sal_uInt16)nPos);
     133             : 
     134           0 :             break;
     135             :         }
     136             :     }
     137             : 
     138             :     (void)_bMustExist;
     139             :     OSL_ENSURE(!_bMustExist || (aSearch != _rList.end()), "ODbaseIndexDialog::implRemoveIndex : did not find the index!");
     140           0 :     return aReturn;
     141             : }
     142             : 
     143           0 : void ODbaseIndexDialog::implInsertIndex(const OTableIndex& _rIndex, TableIndexList& _rList, ListBox& _rDisplay)
     144             : {
     145           0 :     _rList.push_front( _rIndex );
     146           0 :     _rDisplay.InsertEntry( _rIndex.GetIndexFileName() );
     147           0 :     _rDisplay.SelectEntryPos(0);
     148           0 : }
     149             : 
     150           0 : OTableIndex ODbaseIndexDialog::RemoveTableIndex( const OUString& _rTableName, const OUString& _rIndexName, sal_Bool _bMustExist )
     151             : {
     152           0 :     OTableIndex aReturn;
     153             : 
     154             :     // does the table exist ?
     155           0 :     TableInfoList::iterator aTablePos;
     156           0 :     if (!GetTable(_rTableName, aTablePos))
     157           0 :         return aReturn;
     158             : 
     159           0 :     return implRemoveIndex(_rIndexName, aTablePos->aIndexList, *m_pLB_TableIndexes, _bMustExist);
     160             : }
     161             : 
     162           0 : void ODbaseIndexDialog::InsertTableIndex( const OUString& _rTableName, const OTableIndex& _rIndex)
     163             : {
     164           0 :     TableInfoList::iterator aTablePos;
     165           0 :     if (!GetTable(_rTableName, aTablePos))
     166           0 :         return;
     167             : 
     168           0 :     implInsertIndex(_rIndex, aTablePos->aIndexList, *m_pLB_TableIndexes);
     169             : }
     170             : 
     171           0 : IMPL_LINK( ODbaseIndexDialog, OKClickHdl, PushButton*, /*pButton*/ )
     172             : {
     173             :     // let all tables write their INF file
     174             : 
     175           0 :     for (   TableInfoList::const_iterator aLoop = m_aTableInfoList.begin();
     176           0 :             aLoop != m_aTableInfoList.end();
     177             :             ++aLoop
     178             :         )
     179           0 :         aLoop->WriteInfFile(m_aDSN);
     180             : 
     181           0 :     EndDialog();
     182           0 :     return 0;
     183             : }
     184             : 
     185           0 : IMPL_LINK( ODbaseIndexDialog, AddClickHdl, PushButton*, /*pButton*/ )
     186             : {
     187           0 :     OUString aSelection = m_pLB_FreeIndexes->GetSelectEntry();
     188           0 :     OUString aTableName = m_pCB_Tables->GetText();
     189           0 :     OTableIndex aIndex = RemoveFreeIndex( aSelection, sal_True );
     190           0 :     InsertTableIndex( aTableName, aIndex );
     191             : 
     192           0 :     checkButtons();
     193           0 :     return 0;
     194             : }
     195             : 
     196           0 : IMPL_LINK( ODbaseIndexDialog, RemoveClickHdl, PushButton*, /*pButton*/ )
     197             : {
     198           0 :     OUString aSelection = m_pLB_TableIndexes->GetSelectEntry();
     199           0 :     OUString aTableName = m_pCB_Tables->GetText();
     200           0 :     OTableIndex aIndex = RemoveTableIndex( aTableName, aSelection, sal_True );
     201           0 :     InsertFreeIndex( aIndex );
     202             : 
     203           0 :     checkButtons();
     204           0 :     return 0;
     205             : }
     206             : 
     207           0 : IMPL_LINK( ODbaseIndexDialog, AddAllClickHdl, PushButton*, /*pButton*/ )
     208             : {
     209           0 :     sal_uInt16 nCnt = m_pLB_FreeIndexes->GetEntryCount();
     210           0 :     OUString aTableName = m_pCB_Tables->GetText();
     211             : 
     212           0 :     for( sal_uInt16 nPos = 0; nPos < nCnt; ++nPos )
     213           0 :         InsertTableIndex( aTableName, RemoveFreeIndex( m_pLB_FreeIndexes->GetEntry(0), sal_True ) );
     214             : 
     215           0 :     checkButtons();
     216           0 :     return 0;
     217             : }
     218             : 
     219           0 : IMPL_LINK( ODbaseIndexDialog, RemoveAllClickHdl, PushButton*, /*pButton*/ )
     220             : {
     221           0 :     sal_uInt16 nCnt = m_pLB_TableIndexes->GetEntryCount();
     222           0 :     OUString aTableName = m_pCB_Tables->GetText();
     223             : 
     224           0 :     for( sal_uInt16 nPos = 0; nPos < nCnt; ++nPos )
     225           0 :         InsertFreeIndex( RemoveTableIndex( aTableName, m_pLB_TableIndexes->GetEntry(0), sal_True ) );
     226             : 
     227           0 :     checkButtons();
     228           0 :     return 0;
     229             : }
     230             : 
     231           0 : IMPL_LINK( ODbaseIndexDialog, OnListEntrySelected, ListBox*, /*NOTINTERESTEDIN*/ )
     232             : {
     233           0 :     checkButtons();
     234           0 :     return 0;
     235             : }
     236             : 
     237           0 : IMPL_LINK( ODbaseIndexDialog, TableSelectHdl, ComboBox*, pComboBox )
     238             : {
     239             :     // search the table
     240           0 :     TableInfoList::iterator aTablePos;
     241           0 :     if (!GetTable(pComboBox->GetText(), aTablePos))
     242           0 :         return 0L;
     243             : 
     244             :     // fill the listbox for the indexes
     245           0 :     m_pLB_TableIndexes->Clear();
     246           0 :     for (   TableIndexList::const_iterator aLoop = aTablePos->aIndexList.begin();
     247           0 :             aLoop != aTablePos->aIndexList.end();
     248             :             ++aLoop
     249             :         )
     250           0 :         m_pLB_TableIndexes->InsertEntry( aLoop->GetIndexFileName() );
     251             : 
     252           0 :     if ( aTablePos->aIndexList.size() )
     253           0 :         m_pLB_TableIndexes->SelectEntryPos(0);
     254             : 
     255           0 :     checkButtons();
     256           0 :     return 0;
     257             : }
     258             : 
     259           0 : void ODbaseIndexDialog::Init()
     260             : {
     261           0 :     m_pPB_OK->Disable();
     262           0 :     m_pIndexes->Disable();
     263             : 
     264             :     // All indices are first added to a list of free indices.
     265             :     // Afterwards, check the index of each table in the Inf-file.
     266             :     // These indices are removed from the list of free indices and
     267             :     // entered in the indexlist of the table.
     268             : 
     269             :     // if the string does not contain a path, cut the string
     270           0 :     INetURLObject aURL;
     271           0 :     aURL.SetSmartProtocol(INET_PROT_FILE);
     272             :     {
     273           0 :         SvtPathOptions aPathOptions;
     274           0 :         m_aDSN = aPathOptions.SubstituteVariable(m_aDSN);
     275             :     }
     276           0 :     aURL.SetSmartURL(m_aDSN);
     277             : 
     278             :     //  String aFileName = aURL.PathToFileName();
     279           0 :     m_aDSN = aURL.GetMainURL(INetURLObject::NO_DECODE);
     280           0 :     ::ucbhelper::Content aFile;
     281           0 :     sal_Bool bFolder=sal_True;
     282             :     try
     283             :     {
     284           0 :         aFile = ::ucbhelper::Content(m_aDSN,Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext());
     285           0 :         bFolder = aFile.isFolder();
     286             :     }
     287           0 :     catch(Exception&)
     288             :     {
     289           0 :         return;
     290             :     }
     291             : 
     292             :     // first assume for all indexes they're free
     293             : 
     294           0 :     Sequence< OUString> aFolderContent( ::utl::LocalFileHelper::GetFolderContents(m_aDSN,bFolder));
     295             : 
     296           0 :     OUString aIndexExt("ndx");
     297           0 :     OUString aTableExt("dbf");
     298             : 
     299           0 :     ::std::vector< OUString > aUsedIndexes;
     300             : 
     301           0 :     const OUString *pBegin = aFolderContent.getConstArray();
     302           0 :     const OUString *pEnd   = pBegin + aFolderContent.getLength();
     303           0 :     aURL.SetSmartProtocol(INET_PROT_FILE);
     304           0 :     for(;pBegin != pEnd;++pBegin)
     305             :     {
     306           0 :         OUString aName;
     307           0 :         ::utl::LocalFileHelper::ConvertURLToPhysicalName(pBegin->getStr(),aName);
     308           0 :         aURL.SetSmartURL(aName);
     309           0 :         OUString aExt = aURL.getExtension();
     310           0 :         if (aExt == aIndexExt)
     311             :         {
     312           0 :             m_aFreeIndexList.push_back( OTableIndex(aURL.getName()) );
     313             :         }
     314           0 :         else if (aExt == aTableExt)
     315             :         {
     316           0 :             m_aTableInfoList.push_back( OTableInfo(aURL.getName()) );
     317           0 :             OTableInfo& rTabInfo = m_aTableInfoList.back();
     318             : 
     319             :             // open the INF file
     320           0 :             aURL.setExtension("inf");
     321           0 :             OFileNotation aTransformer(aURL.GetURLNoPass(), OFileNotation::N_URL);
     322           0 :             Config aInfFile( aTransformer.get(OFileNotation::N_SYSTEM) );
     323           0 :             aInfFile.SetGroup( aGroupIdent );
     324             : 
     325             :             // fill the indexes list
     326           0 :             OString aNDX;
     327           0 :             sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
     328           0 :             OString aKeyName;
     329           0 :             OUString aEntry;
     330             : 
     331           0 :             for( sal_uInt16 nKey = 0; nKey < nKeyCnt; nKey++ )
     332             :             {
     333             :                 // does the key point to an index file ?
     334           0 :                 aKeyName = aInfFile.GetKeyName( nKey );
     335           0 :                 aNDX = aKeyName.copy(0,3);
     336             : 
     337             :                 // yes -> add to the tables index list
     338           0 :                 if (aNDX == "NDX")
     339             :                 {
     340           0 :                     aEntry = OStringToOUString(aInfFile.ReadKey(aKeyName), osl_getThreadTextEncoding());
     341           0 :                     rTabInfo.aIndexList.push_back( OTableIndex( aEntry ) );
     342             : 
     343             :                     // and remove it from the free index list
     344           0 :                     aUsedIndexes.push_back(aEntry);
     345             :                         // do this later below. We may not have encountered the index file, yet, thus we may not
     346             :                         // know the index as beeing free, yet
     347             :                 }
     348           0 :             }
     349             :         }
     350           0 :     }
     351             : 
     352           0 :     for (   ::std::vector< OUString >::const_iterator aUsedIndex = aUsedIndexes.begin();
     353           0 :             aUsedIndex != aUsedIndexes.end();
     354             :             ++aUsedIndex
     355             :         )
     356           0 :         RemoveFreeIndex( *aUsedIndex, sal_False );
     357             : 
     358           0 :     if (m_aTableInfoList.size())
     359             :     {
     360           0 :         m_pPB_OK->Enable();
     361           0 :         m_pIndexes->Enable();
     362             :     }
     363             : 
     364           0 :     checkButtons();
     365             : }
     366             : 
     367           0 : void ODbaseIndexDialog::SetCtrls()
     368             : {
     369             :     // ComboBox tables
     370           0 :     for (   TableInfoList::const_iterator aLoop = m_aTableInfoList.begin();
     371           0 :             aLoop != m_aTableInfoList.end();
     372             :             ++aLoop
     373             :         )
     374           0 :         m_pCB_Tables->InsertEntry( aLoop->aTableName );
     375             : 
     376             :     // put the first dataset into Edit
     377           0 :     if( m_aTableInfoList.size() )
     378             :     {
     379           0 :         const OTableInfo& rTabInfo = m_aTableInfoList.front();
     380           0 :         m_pCB_Tables->SetText( rTabInfo.aTableName );
     381             : 
     382             :         // build ListBox of the table indices
     383           0 :         for (   TableIndexList::const_iterator aIndex = rTabInfo.aIndexList.begin();
     384           0 :                 aIndex != rTabInfo.aIndexList.end();
     385             :                 ++aIndex
     386             :             )
     387           0 :             m_pLB_TableIndexes->InsertEntry( aIndex->GetIndexFileName() );
     388             : 
     389           0 :         if( rTabInfo.aIndexList.size() )
     390           0 :             m_pLB_TableIndexes->SelectEntryPos( 0 );
     391             :     }
     392             : 
     393             :     // ListBox of the free indices
     394           0 :     for (   TableIndexList::const_iterator aFree = m_aFreeIndexList.begin();
     395           0 :             aFree != m_aFreeIndexList.end();
     396             :             ++aFree
     397             :         )
     398           0 :         m_pLB_FreeIndexes->InsertEntry( aFree->GetIndexFileName() );
     399             : 
     400           0 :     if( m_aFreeIndexList.size() )
     401           0 :         m_pLB_FreeIndexes->SelectEntryPos( 0 );
     402             : 
     403           0 :     TableSelectHdl(m_pCB_Tables);
     404           0 :     checkButtons();
     405           0 : }
     406             : 
     407           0 : void OTableInfo::WriteInfFile( const OUString& rDSN ) const
     408             : {
     409             :     // open INF file
     410           0 :     INetURLObject aURL;
     411           0 :     aURL.SetSmartProtocol(INET_PROT_FILE);
     412           0 :     OUString aDsn = rDSN;
     413             :     {
     414           0 :         SvtPathOptions aPathOptions;
     415           0 :         aDsn = aPathOptions.SubstituteVariable(aDsn);
     416             :     }
     417           0 :     aURL.SetSmartURL(aDsn);
     418           0 :     aURL.Append(aTableName);
     419           0 :     aURL.setExtension("inf");
     420             : 
     421           0 :     OFileNotation aTransformer(aURL.GetURLNoPass(), OFileNotation::N_URL);
     422           0 :     Config aInfFile( aTransformer.get(OFileNotation::N_SYSTEM) );
     423           0 :     aInfFile.SetGroup( aGroupIdent );
     424             : 
     425             :     // first, delete all table indices
     426           0 :     OString aNDX;
     427           0 :     sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
     428           0 :     sal_uInt16 nKey = 0;
     429             : 
     430           0 :     while( nKey < nKeyCnt )
     431             :     {
     432             :         // Does the key point to an index file?...
     433           0 :         OString aKeyName = aInfFile.GetKeyName( nKey );
     434           0 :         aNDX = aKeyName.copy(0,3);
     435             : 
     436             :         //...if yes, delete index file, nKey is at subsequent key
     437           0 :         if (aNDX == "NDX")
     438             :         {
     439           0 :             aInfFile.DeleteKey(aKeyName);
     440           0 :             nKeyCnt--;
     441             :         }
     442             :         else
     443           0 :             nKey++;
     444             : 
     445           0 :     }
     446             : 
     447             :     // now add all saved indices
     448           0 :     sal_uInt16 nPos = 0;
     449           0 :     for (   TableIndexList::const_iterator aIndex = aIndexList.begin();
     450           0 :             aIndex != aIndexList.end();
     451             :             ++aIndex, ++nPos
     452             :         )
     453             :     {
     454           0 :         OStringBuffer aKeyName("NDX");
     455           0 :         if( nPos > 0 )  // first index contains no number
     456           0 :             aKeyName.append(static_cast<sal_Int32>(nPos));
     457             :         aInfFile.WriteKey(
     458             :             aKeyName.makeStringAndClear(),
     459             :             OUStringToOString(aIndex->GetIndexFileName(),
     460           0 :                 osl_getThreadTextEncoding()));
     461           0 :     }
     462             : 
     463           0 :     aInfFile.Flush();
     464             : 
     465             :     // if only [dbase] is left in INF-file, delete file
     466           0 :     if(!nPos)
     467             :     {
     468             :         try
     469             :         {
     470           0 :             ::ucbhelper::Content aContent(aURL.GetURLNoPass(),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
     471           0 :             aContent.executeCommand( OUString("delete"),makeAny( sal_Bool( sal_True ) ) );
     472             :         }
     473           0 :         catch (const Exception& e )
     474             :         {
     475             :             (void)e;  // make compiler happy
     476             :             // simply silent this. The strange algorithm here does a lot of
     477             :             // things even if no files at all were created or accessed, so it's
     478             :             // possible that the file we're trying to delete does not even
     479             :             // exist, and this is a valid condition.
     480             :         }
     481           0 :     }
     482           0 : }
     483             : 
     484           0 : } // namespace
     485             : 
     486             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10