From 5187ce45b93e5e841e541eac98da3fa2110fe632 Mon Sep 17 00:00:00 2001 From: Jan-Marek Glogowski Date: Mon, 15 Sep 2014 00:48:59 +0200 Subject: MM: add non-UI LoadAndRegisterDataSource function For the mail merge unit test we need a function to register new database sources without any UI interaction. This refactors and introduces new versions of LoadAndRegisterDataSource, which gets all previously interactive values as arguments. Change-Id: I1601b4112ddc800a0935950133d386ce349b9087 --- sw/inc/dbmgr.hxx | 37 ++++++++- sw/source/uibase/dbui/dbmgr.cxx | 176 +++++++++++++++++++++++++--------------- 2 files changed, 145 insertions(+), 68 deletions(-) diff --git a/sw/inc/dbmgr.hxx b/sw/inc/dbmgr.hxx index 797e64ce7832..b79cda077a2c 100644 --- a/sw/inc/dbmgr.hxx +++ b/sw/inc/dbmgr.hxx @@ -73,6 +73,7 @@ class SwDbtoolsClient; class SwXMailMerge; class SwMailMergeConfigItem; class SwCalc; +class INetURLObject; enum DBManagerOptions { @@ -217,6 +218,16 @@ public: SwDBManager(); ~SwDBManager(); + enum DBConnURITypes { + DBCONN_UNKNOWN = 0, + DBCONN_ODB, + DBCONN_CALC, + DBCONN_DBASE, + DBCONN_FLAT, + DBCONN_MSJET, + DBCONN_MSACE + }; + /// MailMergeEvent source const SwXMailMerge * GetMailMergeEvtSrc() const { return pMergeEvtSrc; } void SetMailMergeEvtSrc( const SwXMailMerge *pSrc ) { pMergeEvtSrc = pSrc; } @@ -328,11 +339,35 @@ public: static ::com::sun::star::uno::Sequence GetExistingDatabaseNames(); + static DBConnURITypes GetDBunoURI(const OUString &rURI, ::com::sun::star::uno::Any &aURLAny); + /** - Loads a data source from file and registers it. Returns the registered name. + Loads a data source from file and registers it. + + This function requires GUI interaction, as it loads the data source from + the filename returned by a file picker and additional settings dialog. + In case of success it returns the registered name, otherwise an empty string. */ static OUString LoadAndRegisterDataSource(); + /** + Loads a data source from file and registers it. + + In case of success it returns the registered name, otherwise an empty string. + Optionally add a prefix to the registered DB name. + */ + static OUString LoadAndRegisterDataSource(const DBConnURITypes type, const ::com::sun::star::uno::Any &rUnoURI, + const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet > *pSettings, + const OUString &rURI, const OUString *pPrefix = 0, const OUString *pDestDir = 0); + /** + Loads a data source from file and registers it. + + Convenience function, which calls GetDBunoURI and has just one mandatory parameter. + In case of success it returns the registered name, otherwise an empty string. + */ + static OUString LoadAndRegisterDataSource(const OUString& rURI, const OUString *pPrefix = 0, const OUString *pDestDir = 0, + const ::com::sun::star::uno::Reference < ::com::sun::star::beans::XPropertySet > *pSettings = 0); + static SwDbtoolsClient& GetDbtoolsClient(); /// has to be called from _FinitUI() static void RemoveDbtoolsClient(); diff --git a/sw/source/uibase/dbui/dbmgr.cxx b/sw/source/uibase/dbui/dbmgr.cxx index 9103a1a0dac9..49a714f2a69e 100644 --- a/sw/source/uibase/dbui/dbmgr.cxx +++ b/sw/source/uibase/dbui/dbmgr.cxx @@ -2391,74 +2391,113 @@ OUString SwDBManager::LoadAndRegisterDataSource() xFltMgr->setCurrentFilter( sFilterAll ) ; OUString sFind; - bool bTextConnection = false; if( ERRCODE_NONE == aDlgHelper.Execute() ) { - OUString sURL = xFP->getFiles().getConstArray()[0]; - //data sources have to be registered depending on their extensions - INetURLObject aURL( sURL ); - OUString sExt( aURL.GetExtension() ); Any aURLAny; + uno::Reference< beans::XPropertySet > aSettings; + const OUString aURI( xFP->getFiles().getConstArray()[0] ); + const DBConnURITypes type = GetDBunoURI( aURI, aURLAny ); + + if( DBCONN_FLAT == type ) + { + Reference xContext( ::comphelper::getProcessComponentContext() ); + uno::Reference < sdb::XTextConnectionSettings > xSettingsDlg = sdb::TextConnectionSettings::create(xContext); + if( xSettingsDlg->execute() ) + aSettings.set( uno::Reference < beans::XPropertySet >( xSettingsDlg, uno::UNO_QUERY_THROW ) ); + } + sFind = LoadAndRegisterDataSource( type, aURLAny, DBCONN_FLAT == type ? &aSettings : 0, aURI ); + } + return sFind; +} + +SwDBManager::DBConnURITypes SwDBManager::GetDBunoURI(const OUString &rURI, Any &aURLAny) +{ + INetURLObject aURL( rURI ); + OUString sExt( aURL.GetExtension() ); + DBConnURITypes type = DBCONN_UNKNOWN; + + if(sExt == "odb") + { + type = DBCONN_ODB; + } + else if(sExt.equalsIgnoreAsciiCase("sxc") + || sExt.equalsIgnoreAsciiCase("ods") + || sExt.equalsIgnoreAsciiCase("xls")) + { + OUString sDBURL("sdbc:calc:"); + sDBURL += aURL.GetMainURL(INetURLObject::NO_DECODE); + aURLAny <<= sDBURL; + type = DBCONN_CALC; + } + else if(sExt.equalsIgnoreAsciiCase("dbf")) + { + aURL.removeSegment(); + aURL.removeFinalSlash(); + OUString sDBURL("sdbc:dbase:"); + sDBURL += aURL.GetMainURL(INetURLObject::NO_DECODE); + aURLAny <<= sDBURL; + type = DBCONN_DBASE; + } + else if(sExt.equalsIgnoreAsciiCase("csv") || sExt.equalsIgnoreAsciiCase("txt")) + { + aURL.removeSegment(); + aURL.removeFinalSlash(); + OUString sDBURL("sdbc:flat:"); + //only the 'path' has to be added + sDBURL += aURL.GetMainURL(INetURLObject::NO_DECODE); + aURLAny <<= sDBURL; + type = DBCONN_FLAT; + } +#ifdef WNT + else if(sExt.equalsIgnoreAsciiCase("mdb")) + { + OUString sDBURL("sdbc:ado:access:PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="); + sDBURL += aURL.PathToFileName(); + aURLAny <<= sDBURL; + type = DBCONN_MSJET; + } + else if(sExt.equalsIgnoreAsciiCase("accdb")) + { + OUString sDBURL("sdbc:ado:PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE="); + sDBURL += aURL.PathToFileName(); + aURLAny <<= sDBURL; + type = DBCONN_MSACE; + } +#endif + return type; +} + +OUString SwDBManager::LoadAndRegisterDataSource(const DBConnURITypes type, const Any &aURLAny, const uno::Reference< beans::XPropertySet > *pSettings, + const OUString &rURI, const OUString *pPrefix, const OUString *pDestDir) +{ + INetURLObject aURL( rURI ); + OUString sExt( aURL.GetExtension() ); Any aTableFilterAny; Any aSuppressVersionsAny; Any aInfoAny; - INetURLObject aTempURL(aURL); bool bStore = true; - if(sExt == "odb") - { + OUString sFind; + Sequence aFilters(1); + + switch (type) { + case DBCONN_UNKNOWN: + case DBCONN_CALC: + break; + case DBCONN_ODB: bStore = false; - } - else if(sExt.equalsIgnoreAsciiCase("sxc") - || sExt.equalsIgnoreAsciiCase("ods") - || sExt.equalsIgnoreAsciiCase("xls")) - { - OUString sDBURL("sdbc:calc:"); - sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE); - aURLAny <<= sDBURL; - } - else if(sExt.equalsIgnoreAsciiCase("dbf")) - { - aTempURL.removeSegment(); - aTempURL.removeFinalSlash(); - OUString sDBURL("sdbc:dbase:"); - sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE); - aURLAny <<= sDBURL; - //set the filter to the file name without extension - Sequence aFilters(1); - aFilters[0] = aURL.getBase(); - aTableFilterAny <<= aFilters; - } - else if(sExt.equalsIgnoreAsciiCase("csv") || sExt.equalsIgnoreAsciiCase("txt")) - { - aTempURL.removeSegment(); - aTempURL.removeFinalSlash(); - OUString sDBURL("sdbc:flat:"); - //only the 'path' has to be added - sDBURL += aTempURL.GetMainURL(INetURLObject::NO_DECODE); - aURLAny <<= sDBURL; - - bTextConnection = true; + break; + case DBCONN_FLAT: + case DBCONN_DBASE: //set the filter to the file name without extension - Sequence aFilters(1); aFilters[0] = aURL.getBase(); aTableFilterAny <<= aFilters; - } -#ifdef WNT - else if(sExt.equalsIgnoreAsciiCase("mdb")) - { - OUString sDBURL("sdbc:ado:access:PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="); - sDBURL += aTempURL.PathToFileName(); - aURLAny <<= sDBURL; - aSuppressVersionsAny <<= makeAny(true); - } - else if(sExt.equalsIgnoreAsciiCase("accdb")) - { - OUString sDBURL("sdbc:ado:PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE="); - sDBURL += aTempURL.PathToFileName(); - aURLAny <<= sDBURL; + break; + case DBCONN_MSJET: + case DBCONN_MSACE: aSuppressVersionsAny <<= makeAny(true); + break; } -#endif + try { Reference xContext( ::comphelper::getProcessComponentContext() ); @@ -2470,6 +2509,8 @@ OUString SwDBManager::LoadAndRegisterDataSource() RTL_TEXTENCODING_UTF8 ); sal_Int32 nExtLen = aURL.GetExtension().getLength(); sNewName = sNewName.replaceAt( sNewName.getLength() - nExtLen - 1, nExtLen + 1, "" ); + if (pPrefix) + sNewName = *pPrefix + sNewName; //find a unique name if sNewName already exists sFind = sNewName; @@ -2484,7 +2525,7 @@ OUString SwDBManager::LoadAndRegisterDataSource() if(!bStore) { //odb-file - Any aDataSource = xDBContext->getByName(aTempURL.GetMainURL(INetURLObject::NO_DECODE)); + Any aDataSource = xDBContext->getByName(aURL.GetMainURL(INetURLObject::NO_DECODE)); aDataSource >>= xNewInstance; } else @@ -2501,19 +2542,13 @@ OUString SwDBManager::LoadAndRegisterDataSource() if(aInfoAny.hasValue()) xDataProperties->setPropertyValue("Info", aInfoAny); - if( bTextConnection ) + if( DBCONN_FLAT == type && pSettings ) { - uno::Reference < sdb::XTextConnectionSettings > xSettingsDlg = sdb::TextConnectionSettings::create(xContext); - if( xSettingsDlg->execute() ) - { uno::Any aSettings = xDataProperties->getPropertyValue( "Settings" ); uno::Reference < beans::XPropertySet > xDSSettings; aSettings >>= xDSSettings; - ::comphelper::copyProperties( - uno::Reference < beans::XPropertySet >( xSettingsDlg, uno::UNO_QUERY_THROW ), - xDSSettings ); + ::comphelper::copyProperties( *pSettings, xDSSettings ); xDSSettings->setPropertyValue( "Extension", uno::makeAny( sExt )); - } } Reference xDS(xNewInstance, UNO_QUERY_THROW); @@ -2521,21 +2556,28 @@ OUString SwDBManager::LoadAndRegisterDataSource() OUString sOutputExt = ".odb"; OUString sTmpName; { - utl::TempFile aTempFile(sNewName, true, &sOutputExt, &sHomePath); + OUString sHomePath(SvtPathOptions().GetWorkPath()); + utl::TempFile aTempFile(sNewName, true, &sOutputExt, pDestDir ? pDestDir : &sHomePath); aTempFile.EnableKillingFile(true); sTmpName = aTempFile.GetURL(); } xStore->storeAsURL(sTmpName, Sequence< PropertyValue >()); } xDBContext->registerObject( sFind, xNewInstance ); - } catch(const Exception&) { + sFind = ""; } - } return sFind; +} +OUString SwDBManager::LoadAndRegisterDataSource(const OUString &rURI, const OUString *pPrefix, const OUString *pDestDir, + const uno::Reference< beans::XPropertySet > *pSettings) +{ + Any aURLAny; + DBConnURITypes type = GetDBunoURI( rURI, aURLAny ); + return LoadAndRegisterDataSource( type, aURLAny, pSettings, rURI, pPrefix, pDestDir ); } void SwDBManager::ExecuteFormLetter( SwWrtShell& rSh, -- cgit