Files
libreoffice/dbaccess/source/ui/dlg/dbfindex.cxx

487 lines
15 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-14 17:39:53 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-10-05 09:09:48 +00:00
#include "dbfindex.hxx"
#include <comphelper/processfactory.hxx>
#include <tools/config.hxx>
2000-10-05 09:09:48 +00:00
#include <sfx2/app.hxx>
#include "moduledbu.hxx"
2002-08-19 07:01:32 +00:00
#include "dbu_dlg.hrc"
2011-02-03 00:33:36 +01:00
#include <osl/diagnose.h>
2000-12-14 14:43:32 +00:00
#include <unotools/localfilehelper.hxx>
#include <tools/urlobj.hxx>
#include <unotools/pathoptions.hxx>
2000-12-14 14:43:32 +00:00
#include <ucbhelper/content.hxx>
#include <svl/filenotation.hxx>
#include <rtl/strbuf.hxx>
2000-10-05 09:09:48 +00:00
namespace dbaui
{
2000-12-14 14:43:32 +00:00
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::ucb;
using namespace ::svt;
2000-12-14 14:43:32 +00:00
const OString aGroupIdent("dBase III");
2000-10-05 09:09:48 +00:00
ODbaseIndexDialog::ODbaseIndexDialog(Window * pParent, OUString aDataSrcName)
: ModalDialog(pParent, "DBaseIndexDialog", "dbaccess/ui/dbaseindexdialog.ui")
, m_aDSN(aDataSrcName)
, m_bCaseSensitiv(true)
2000-10-05 09:09:48 +00:00
{
get(m_pPB_OK, "ok");
get(m_pCB_Tables, "table");
get(m_pIndexes, "frame");
get(m_pLB_TableIndexes, "tableindex");
get(m_pLB_FreeIndexes, "freeindex");
Size aSize(LogicToPixel(Size(76, 98), MAP_APPFONT));
m_pLB_TableIndexes->set_height_request(aSize.Height());
m_pLB_TableIndexes->set_width_request(aSize.Width());
m_pLB_FreeIndexes->set_height_request(aSize.Height());
m_pLB_FreeIndexes->set_width_request(aSize.Width());
get(m_pAdd, "add");
get(m_pAddAll, "addall");
get(m_pRemove, "remove");
get(m_pRemoveAll, "removeall");
m_pCB_Tables->SetSelectHdl( LINK(this, ODbaseIndexDialog, TableSelectHdl) );
m_pAdd->SetClickHdl( LINK(this, ODbaseIndexDialog, AddClickHdl) );
m_pRemove->SetClickHdl( LINK(this, ODbaseIndexDialog, RemoveClickHdl) );
m_pAddAll->SetClickHdl( LINK(this, ODbaseIndexDialog, AddAllClickHdl) );
m_pRemoveAll->SetClickHdl( LINK(this, ODbaseIndexDialog, RemoveAllClickHdl) );
m_pPB_OK->SetClickHdl( LINK(this, ODbaseIndexDialog, OKClickHdl) );
m_pLB_FreeIndexes->SetSelectHdl( LINK(this, ODbaseIndexDialog, OnListEntrySelected) );
m_pLB_TableIndexes->SetSelectHdl( LINK(this, ODbaseIndexDialog, OnListEntrySelected) );
m_pCB_Tables->SetDropDownLineCount(8);
2000-10-05 09:09:48 +00:00
Init();
SetCtrls();
}
sal_Bool ODbaseIndexDialog::GetTable(const OUString& _rName, TableInfoList::iterator& _rPosition)
2000-10-05 09:09:48 +00:00
{
for ( _rPosition = m_aTableInfoList.begin();
_rPosition != m_aTableInfoList.end();
++_rPosition
)
{
if (m_bCaseSensitiv)
{
if (_rPosition->aTableName == _rName)
2000-10-05 09:09:48 +00:00
return sal_True;
}
else
{
if (_rPosition->aTableName.equalsIgnoreAsciiCase(_rName))
2000-10-05 09:09:48 +00:00
return sal_True;
}
}
return sal_False;
}
void ODbaseIndexDialog::checkButtons()
{
m_pAdd->Enable(0 != m_pLB_FreeIndexes->GetSelectEntryCount());
m_pAddAll->Enable(0 != m_pLB_FreeIndexes->GetEntryCount());
m_pRemove->Enable(0 != m_pLB_TableIndexes->GetSelectEntryCount());
m_pRemoveAll->Enable(0 != m_pLB_TableIndexes->GetEntryCount());
}
OTableIndex ODbaseIndexDialog::implRemoveIndex(const OUString& _rName, TableIndexList& _rList, ListBox& _rDisplay, sal_Bool _bMustExist)
2000-10-05 09:09:48 +00:00
{
OTableIndex aReturn;
sal_Int32 nPos = 0;
2004-03-12 11:00:15 +00:00
TableIndexList::iterator aSearch;
2004-03-12 11:00:15 +00:00
for ( aSearch = _rList.begin();
2000-10-05 09:09:48 +00:00
aSearch != _rList.end();
++aSearch, ++nPos
)
{
if ( m_bCaseSensitiv ? aSearch->GetIndexFileName() == _rName : aSearch->GetIndexFileName().equalsIgnoreAsciiCase(_rName) )
2000-10-05 09:09:48 +00:00
{
aReturn = *aSearch;
_rList.erase(aSearch);
_rDisplay.RemoveEntry( _rName );
// adjust selection if necessary
if ((sal_uInt32)nPos == _rList.size())
2000-10-05 09:09:48 +00:00
_rDisplay.SelectEntryPos((sal_uInt16)nPos-1);
else
_rDisplay.SelectEntryPos((sal_uInt16)nPos);
break;
}
}
(void)_bMustExist;
2011-02-03 00:33:36 +01:00
OSL_ENSURE(!_bMustExist || (aSearch != _rList.end()), "ODbaseIndexDialog::implRemoveIndex : did not find the index!");
2000-10-05 09:09:48 +00:00
return aReturn;
}
void ODbaseIndexDialog::implInsertIndex(const OTableIndex& _rIndex, TableIndexList& _rList, ListBox& _rDisplay)
{
_rList.push_front( _rIndex );
_rDisplay.InsertEntry( _rIndex.GetIndexFileName() );
_rDisplay.SelectEntryPos(0);
}
OTableIndex ODbaseIndexDialog::RemoveTableIndex( const OUString& _rTableName, const OUString& _rIndexName, sal_Bool _bMustExist )
2000-10-05 09:09:48 +00:00
{
OTableIndex aReturn;
// does the table exist ?
TableInfoList::iterator aTablePos;
2000-10-05 09:09:48 +00:00
if (!GetTable(_rTableName, aTablePos))
return aReturn;
return implRemoveIndex(_rIndexName, aTablePos->aIndexList, *m_pLB_TableIndexes, _bMustExist);
2000-10-05 09:09:48 +00:00
}
void ODbaseIndexDialog::InsertTableIndex( const OUString& _rTableName, const OTableIndex& _rIndex)
2000-10-05 09:09:48 +00:00
{
TableInfoList::iterator aTablePos;
2000-10-05 09:09:48 +00:00
if (!GetTable(_rTableName, aTablePos))
return;
implInsertIndex(_rIndex, aTablePos->aIndexList, *m_pLB_TableIndexes);
2000-10-05 09:09:48 +00:00
}
IMPL_LINK( ODbaseIndexDialog, OKClickHdl, PushButton*, /*pButton*/ )
2000-10-05 09:09:48 +00:00
{
// let all tables write their INF file
for ( TableInfoList::const_iterator aLoop = m_aTableInfoList.begin();
2000-10-05 09:09:48 +00:00
aLoop != m_aTableInfoList.end();
++aLoop
)
aLoop->WriteInfFile(m_aDSN);
EndDialog();
return 0;
}
IMPL_LINK( ODbaseIndexDialog, AddClickHdl, PushButton*, /*pButton*/ )
2000-10-05 09:09:48 +00:00
{
OUString aSelection = m_pLB_FreeIndexes->GetSelectEntry();
OUString aTableName = m_pCB_Tables->GetText();
OTableIndex aIndex = RemoveFreeIndex( aSelection, sal_True );
2000-10-05 09:09:48 +00:00
InsertTableIndex( aTableName, aIndex );
checkButtons();
2000-10-05 09:09:48 +00:00
return 0;
}
IMPL_LINK( ODbaseIndexDialog, RemoveClickHdl, PushButton*, /*pButton*/ )
2000-10-05 09:09:48 +00:00
{
OUString aSelection = m_pLB_TableIndexes->GetSelectEntry();
OUString aTableName = m_pCB_Tables->GetText();
OTableIndex aIndex = RemoveTableIndex( aTableName, aSelection, sal_True );
2000-10-05 09:09:48 +00:00
InsertFreeIndex( aIndex );
checkButtons();
2000-10-05 09:09:48 +00:00
return 0;
}
IMPL_LINK( ODbaseIndexDialog, AddAllClickHdl, PushButton*, /*pButton*/ )
2000-10-05 09:09:48 +00:00
{
sal_uInt16 nCnt = m_pLB_FreeIndexes->GetEntryCount();
OUString aTableName = m_pCB_Tables->GetText();
2000-10-05 09:09:48 +00:00
for( sal_uInt16 nPos = 0; nPos < nCnt; ++nPos )
InsertTableIndex( aTableName, RemoveFreeIndex( m_pLB_FreeIndexes->GetEntry(0), sal_True ) );
2000-10-05 09:09:48 +00:00
checkButtons();
2000-10-05 09:09:48 +00:00
return 0;
}
IMPL_LINK( ODbaseIndexDialog, RemoveAllClickHdl, PushButton*, /*pButton*/ )
2000-10-05 09:09:48 +00:00
{
sal_uInt16 nCnt = m_pLB_TableIndexes->GetEntryCount();
OUString aTableName = m_pCB_Tables->GetText();
2000-10-05 09:09:48 +00:00
for( sal_uInt16 nPos = 0; nPos < nCnt; ++nPos )
InsertFreeIndex( RemoveTableIndex( aTableName, m_pLB_TableIndexes->GetEntry(0), sal_True ) );
2000-10-05 09:09:48 +00:00
checkButtons();
return 0;
}
IMPL_LINK( ODbaseIndexDialog, OnListEntrySelected, ListBox*, /*NOTINTERESTEDIN*/ )
{
checkButtons();
2000-10-05 09:09:48 +00:00
return 0;
}
IMPL_LINK( ODbaseIndexDialog, TableSelectHdl, ComboBox*, pComboBox )
{
// search the table
TableInfoList::iterator aTablePos;
2000-10-05 09:09:48 +00:00
if (!GetTable(pComboBox->GetText(), aTablePos))
return 0L;
// fill the listbox for the indexes
m_pLB_TableIndexes->Clear();
for ( TableIndexList::const_iterator aLoop = aTablePos->aIndexList.begin();
2000-10-05 09:09:48 +00:00
aLoop != aTablePos->aIndexList.end();
++aLoop
)
m_pLB_TableIndexes->InsertEntry( aLoop->GetIndexFileName() );
2000-10-05 09:09:48 +00:00
if ( aTablePos->aIndexList.size() )
m_pLB_TableIndexes->SelectEntryPos(0);
2000-10-05 09:09:48 +00:00
checkButtons();
2000-10-05 09:09:48 +00:00
return 0;
}
void ODbaseIndexDialog::Init()
{
m_pPB_OK->Disable();
m_pIndexes->Disable();
// All indices are first added to a list of free indices.
// Afterwards, check the index of each table in the Inf-file.
// These indices are removed from the list of free indices and
// entered in the indexlist of the table.
2000-10-05 09:09:48 +00:00
// if the string does not contain a path, cut the string
2000-12-14 14:43:32 +00:00
INetURLObject aURL;
aURL.SetSmartProtocol(INET_PROT_FILE);
{
SvtPathOptions aPathOptions;
m_aDSN = aPathOptions.SubstituteVariable(m_aDSN);
}
aURL.SetSmartURL(m_aDSN);
2000-10-05 09:09:48 +00:00
2000-12-14 14:43:32 +00:00
// String aFileName = aURL.PathToFileName();
2001-07-17 06:35:00 +00:00
m_aDSN = aURL.GetMainURL(INetURLObject::NO_DECODE);
::ucbhelper::Content aFile;
2000-12-14 14:43:32 +00:00
sal_Bool bFolder=sal_True;
try
{
aFile = ::ucbhelper::Content(m_aDSN,Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext());
2000-12-14 14:43:32 +00:00
bFolder = aFile.isFolder();
}
2001-06-12 14:42:44 +00:00
catch(Exception&)
2000-12-14 14:43:32 +00:00
{
return;
}
2001-06-12 14:42:44 +00:00
2000-10-05 09:09:48 +00:00
// first assume for all indexes they're free
Sequence< OUString> aFolderContent( ::utl::LocalFileHelper::GetFolderContents(m_aDSN,bFolder));
2000-10-05 09:09:48 +00:00
OUString aIndexExt("ndx");
OUString aTableExt("dbf");
2000-10-05 09:09:48 +00:00
::std::vector< OUString > aUsedIndexes;
const OUString *pBegin = aFolderContent.getConstArray();
const OUString *pEnd = pBegin + aFolderContent.getLength();
aURL.SetSmartProtocol(INET_PROT_FILE);
2000-12-14 14:43:32 +00:00
for(;pBegin != pEnd;++pBegin)
2000-10-05 09:09:48 +00:00
{
OUString aName;
2000-12-14 14:43:32 +00:00
::utl::LocalFileHelper::ConvertURLToPhysicalName(pBegin->getStr(),aName);
aURL.SetSmartURL(aName);
OUString aExt = aURL.getExtension();
if (aExt == aIndexExt)
2000-12-14 14:43:32 +00:00
{
m_aFreeIndexList.push_back( OTableIndex(aURL.getName()) );
}
else if (aExt == aTableExt)
2000-12-14 14:43:32 +00:00
{
m_aTableInfoList.push_back( OTableInfo(aURL.getName()) );
OTableInfo& rTabInfo = m_aTableInfoList.back();
// open the INF file
aURL.setExtension("inf");
OFileNotation aTransformer(aURL.GetURLNoPass(), OFileNotation::N_URL);
Config aInfFile( aTransformer.get(OFileNotation::N_SYSTEM) );
2000-12-14 14:43:32 +00:00
aInfFile.SetGroup( aGroupIdent );
// fill the indexes list
OString aNDX;
2000-12-14 14:43:32 +00:00
sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
OString aKeyName;
OUString aEntry;
2000-12-14 14:43:32 +00:00
for( sal_uInt16 nKey = 0; nKey < nKeyCnt; nKey++ )
{
// does the key point to an index file ?
aKeyName = aInfFile.GetKeyName( nKey );
2011-12-13 23:13:22 +00:00
aNDX = aKeyName.copy(0,3);
2000-10-05 09:09:48 +00:00
2000-12-14 14:43:32 +00:00
// yes -> add to the tables index list
if (aNDX == "NDX")
2000-12-14 14:43:32 +00:00
{
aEntry = OStringToOUString(aInfFile.ReadKey(aKeyName), osl_getThreadTextEncoding());
2000-12-14 14:43:32 +00:00
rTabInfo.aIndexList.push_back( OTableIndex( aEntry ) );
// and remove it from the free index list
aUsedIndexes.push_back(aEntry);
// do this later below. We may not have encountered the index file, yet, thus we may not
// know the index as beeing free, yet
2000-12-14 14:43:32 +00:00
}
}
}
}
for ( ::std::vector< OUString >::const_iterator aUsedIndex = aUsedIndexes.begin();
aUsedIndex != aUsedIndexes.end();
++aUsedIndex
)
RemoveFreeIndex( *aUsedIndex, sal_False );
if (m_aTableInfoList.size())
2000-10-05 09:09:48 +00:00
{
m_pPB_OK->Enable();
m_pIndexes->Enable();
2000-10-05 09:09:48 +00:00
}
checkButtons();
2000-10-05 09:09:48 +00:00
}
void ODbaseIndexDialog::SetCtrls()
{
// ComboBox tables
for ( TableInfoList::const_iterator aLoop = m_aTableInfoList.begin();
2000-10-05 09:09:48 +00:00
aLoop != m_aTableInfoList.end();
++aLoop
)
m_pCB_Tables->InsertEntry( aLoop->aTableName );
2000-10-05 09:09:48 +00:00
// put the first dataset into Edit
2000-10-05 09:09:48 +00:00
if( m_aTableInfoList.size() )
{
const OTableInfo& rTabInfo = m_aTableInfoList.front();
m_pCB_Tables->SetText( rTabInfo.aTableName );
2000-10-05 09:09:48 +00:00
// build ListBox of the table indices
for ( TableIndexList::const_iterator aIndex = rTabInfo.aIndexList.begin();
aIndex != rTabInfo.aIndexList.end();
2000-10-05 09:09:48 +00:00
++aIndex
)
m_pLB_TableIndexes->InsertEntry( aIndex->GetIndexFileName() );
2000-10-05 09:09:48 +00:00
if( rTabInfo.aIndexList.size() )
m_pLB_TableIndexes->SelectEntryPos( 0 );
2000-10-05 09:09:48 +00:00
}
// ListBox of the free indices
for ( TableIndexList::const_iterator aFree = m_aFreeIndexList.begin();
aFree != m_aFreeIndexList.end();
2000-10-05 09:09:48 +00:00
++aFree
)
m_pLB_FreeIndexes->InsertEntry( aFree->GetIndexFileName() );
2000-10-05 09:09:48 +00:00
if( m_aFreeIndexList.size() )
m_pLB_FreeIndexes->SelectEntryPos( 0 );
2000-10-05 09:09:48 +00:00
TableSelectHdl(m_pCB_Tables);
checkButtons();
2000-10-05 09:09:48 +00:00
}
void OTableInfo::WriteInfFile( const OUString& rDSN ) const
2000-10-05 09:09:48 +00:00
{
// open INF file
2000-12-14 14:43:32 +00:00
INetURLObject aURL;
aURL.SetSmartProtocol(INET_PROT_FILE);
OUString aDsn = rDSN;
2000-12-14 14:43:32 +00:00
{
SvtPathOptions aPathOptions;
aDsn = aPathOptions.SubstituteVariable(aDsn);
}
aURL.SetSmartURL(aDsn);
aURL.Append(aTableName);
aURL.setExtension("inf");
2000-12-14 14:43:32 +00:00
OFileNotation aTransformer(aURL.GetURLNoPass(), OFileNotation::N_URL);
Config aInfFile( aTransformer.get(OFileNotation::N_SYSTEM) );
2000-10-05 09:09:48 +00:00
aInfFile.SetGroup( aGroupIdent );
// first, delete all table indices
OString aNDX;
2000-10-05 09:09:48 +00:00
sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
sal_uInt16 nKey = 0;
while( nKey < nKeyCnt )
{
// Does the key point to an index file?...
OString aKeyName = aInfFile.GetKeyName( nKey );
2011-12-13 23:13:22 +00:00
aNDX = aKeyName.copy(0,3);
2000-10-05 09:09:48 +00:00
//...if yes, delete index file, nKey is at subsequent key
if (aNDX == "NDX")
2000-10-05 09:09:48 +00:00
{
aInfFile.DeleteKey(aKeyName);
nKeyCnt--;
}
else
nKey++;
}
// now add all saved indices
2000-10-05 09:09:48 +00:00
sal_uInt16 nPos = 0;
for ( TableIndexList::const_iterator aIndex = aIndexList.begin();
2000-10-05 09:09:48 +00:00
aIndex != aIndexList.end();
++aIndex, ++nPos
)
{
OStringBuffer aKeyName("NDX");
if( nPos > 0 ) // first index contains no number
aKeyName.append(static_cast<sal_Int32>(nPos));
aInfFile.WriteKey(
aKeyName.makeStringAndClear(),
OUStringToOString(aIndex->GetIndexFileName(),
osl_getThreadTextEncoding()));
2000-10-05 09:09:48 +00:00
}
aInfFile.Flush();
// if only [dbase] is left in INF-file, delete file
2000-12-14 14:43:32 +00:00
if(!nPos)
{
try
{
::ucbhelper::Content aContent(aURL.GetURLNoPass(),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
aContent.executeCommand( OUString("delete"),makeAny( sal_Bool( sal_True ) ) );
}
catch (const Exception& e )
{
(void)e; // make compiler happy
// simply silent this. The strange algorithm here does a lot of
// things even if no files at all were created or accessed, so it's
// possible that the file we're trying to delete does not even
// exist, and this is a valid condition.
}
2000-12-14 14:43:32 +00:00
}
2000-10-05 09:09:48 +00:00
}
} // namespace
2000-10-05 09:09:48 +00:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */