27#include <com/sun/star/beans/XPropertySet.hpp>
28#include <com/sun/star/container/XNameAccess.hpp>
29#include <com/sun/star/frame/XModel.hpp>
30#include <com/sun/star/frame/XStorable.hpp>
31#include <com/sun/star/lang/XMultiServiceFactory.hpp>
32#include <com/sun/star/sdb/DatabaseContext.hpp>
33#include <com/sun/star/sdb/SQLContext.hpp>
34#include <com/sun/star/sdb/XCompletedConnection.hpp>
35#include <com/sun/star/sdb/XDatabaseRegistrations.hpp>
36#include <com/sun/star/sdb/XDocumentDataSource.hpp>
37#include <com/sun/star/sdbc/XConnection.hpp>
38#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
39#include <com/sun/star/task/InteractionHandler.hpp>
40#include <com/sun/star/uri/UriReferenceFactory.hpp>
41#include <com/sun/star/uri/VndSunStarPkgUrlReferenceFactory.hpp>
77 using namespace ::
utl;
92 Reference<XDatabaseContext> xContext = DatabaseContext::create(_rxContext);
99 const Reference< XComponentContext >& _rxContext,
const OUString& _rName,
100 Reference< XPropertySet >& _rxNewDataSource )
106 DBG_ASSERT( !xContext->hasByName( _rName ),
"lcl_implCreateAndInsert: name already used!" );
110 Reference< XPropertySet > xNewDataSource;
112 xNewDataSource.set( xContext->createInstance(), UNO_QUERY );
113 DBG_ASSERT( xNewDataSource.is(),
"lcl_implCreateAndInsert: could not create a new data source!" );
117 DBG_ASSERT( xContext.is(),
"lcl_implCreateAndInsert: missing an interface on the context (XNamingService)!" );
121 _rxNewDataSource = xNewDataSource;
128 const Reference< XComponentContext >& _rxORB,
const OUString& _rName,
129 const char* _pInitialAsciiURL )
135 Reference< XPropertySet > xNewDataSource;
140 if (xNewDataSource.is())
142 xNewDataSource->setPropertyValue(
144 Any( OUString::createFromAscii( _pInitialAsciiURL ) )
153 "caught an exception while creating the data source!");
160 const Reference< XComponentContext >& _rxORB,
const OUString& _sName,
161 const OUString& _sURL )
163 OSL_ENSURE( !_sName.isEmpty(),
"lcl_registerDataSource: invalid name!" );
164 OSL_ENSURE( !_sURL.isEmpty(),
"lcl_registerDataSource: invalid URL!" );
167 Reference< XDatabaseContext > xRegistrations( DatabaseContext::create(_rxORB) );
168 if ( xRegistrations->hasRegisteredDatabase( _sName ) )
169 xRegistrations->changeDatabaseLocation( _sName, _sURL );
171 xRegistrations->registerDatabaseLocation( _sName, _sURL );
181 Reference< XComponentContext >
xORB;
202 Sequence< OUString > aDSNames =
m_pImpl->xContext->getElementNames();
203 const OUString* pDSNames = aDSNames.getConstArray();
204 const OUString* pDSNamesEnd = pDSNames + aDSNames.getLength();
206 for ( ;pDSNames != pDSNamesEnd; ++pDSNames )
207 m_pImpl->aDataSourceNames.insert( *pDSNames );
221 OUString sCheck( _rDataSourceName );
222 StringBag::const_iterator aPos =
m_pImpl->aDataSourceNames.find( sCheck );
224 sal_Int32 nPostfix = 1;
225 while ( (
m_pImpl->aDataSourceNames.end() != aPos ) && ( nPostfix < 65535 ) )
227 sCheck = _rDataSourceName + OUString::number( nPostfix++ );
229 aPos =
m_pImpl->aDataSourceNames.find( sCheck );
232 _rDataSourceName = sCheck;
238 _rNames =
m_pImpl->aDataSourceNames;
284 Reference< XComponentContext >
xORB;
305 if(
this != &_rSource )
314 m_pImpl = std::move(_rSource.m_pImpl);
334 Reference< XDocumentDataSource > xDocAccess(
m_pImpl->xDataSource, UNO_QUERY );
335 Reference< XStorable > xStorable;
336 if ( xDocAccess.is() )
337 xStorable.set(xDocAccess->getDatabaseDocument(), css::uno::UNO_QUERY);
338 OSL_ENSURE( xStorable.is(),
"DataSource is no XStorable!" );
339 if ( xStorable.is() )
343 OUString aOwnURL = lcl_getOwnURL(pObjectShell);
347 xStorable->storeAsURL(
m_pImpl->sName,Sequence<PropertyValue>());
352 OUString aStreamRelPath =
"EmbeddedDatabase";
354 auto xUri = css::uri::UriReferenceFactory::create(xContext)->parse(aOwnURL);
356 xUri = css::uri::VndSunStarPkgUrlReferenceFactory::create(xContext)->createVndSunStarPkgUrlReference(xUri);
358 OUString
const sTmpName = xUri->getUriReference() +
"/" + aStreamRelPath;
359 assert(pObjectShell);
360 uno::Reference<embed::XStorage> xStorage = pObjectShell->
GetStorage();
363 {
"TargetStorage",
uno::Any(xStorage)},
364 {
"StreamRelPath",
uno::Any(aStreamRelPath)},
367 xStorable->storeAsURL(sTmpName, aSequence);
372 uno::Reference<lang::XMultiServiceFactory>
xFactory(pObjectShell->
GetModel(), uno::UNO_QUERY);
373 uno::Reference<beans::XPropertySet> xPropertySet(
xFactory->createInstance(
"com.sun.star.document.Settings"), uno::UNO_QUERY);
374 xPropertySet->setPropertyValue(
"EmbeddedDatabaseName",
uno::Any(aStreamRelPath));
381 "caught an exception while creating the data source!");
399 "caught an exception while creating the data source!");
406 if (
m_pImpl->xDataSource.get() == _rxDS.get())
432 "caught an exception while creating the data source!");
462 return aTables.find( _rTableName ) != aTables.end();
471 OSL_FAIL(
"ODataSource::getTableNames: not connected!" );
478 Reference< XTablesSupplier > xSuppTables(
m_pImpl->xConnection.getTyped(), UNO_QUERY );
479 Reference< XNameAccess > xTables;
480 if ( xSuppTables.is( ) )
481 xTables = xSuppTables->getTables();
482 DBG_ASSERT( xTables.is(),
"ODataSource::getTableNames: could not retrieve the tables container!" );
485 Sequence< OUString > aTableNames;
487 aTableNames = xTables->getElementNames( );
490 const OUString* pTableNames = aTableNames.getConstArray();
491 const OUString* pTableNamesEnd = pTableNames + aTableNames.getLength();
492 for (;pTableNames < pTableNamesEnd; ++pTableNames)
493 m_pImpl->aTables.insert( *pTableNames );
513 Reference< XInteractionHandler > xInteractions;
516 xInteractions = InteractionHandler::createWithParent(
m_pImpl->xORB,
nullptr);
524 if (!xInteractions.is())
526 if ( _pMessageParent )
534 Reference< XConnection > xConnection;
537 Reference< XCompletedConnection > xComplConn(
m_pImpl->xDataSource, UNO_QUERY );
538 DBG_ASSERT( xComplConn.is(),
"ODataSource::connect: missing the XCompletedConnection interface on the data source!" );
539 if ( xComplConn.is() )
540 xConnection = xComplConn->connectWithCompletion( xInteractions );
542 catch(
const SQLContext& e ) { aError <<= e; }
543 catch(
const SQLWarning& e ) { aError <<= e; }
544 catch(
const SQLException& e ) { aError <<= e; }
552 if ( aError.hasValue() && _pMessageParent )
556 SQLException aException;
557 aError >>= aException;
558 if ( aException.Message.isEmpty() )
561 SQLContext aDetailedError;
564 aDetailedError.NextException = aError;
566 xInteractions->handle(
new OInteractionRequest(
Any( aDetailedError ) ) );
571 xInteractions->handle(
new OInteractionRequest(
Any( aException ) ) );
577 "caught an exception while trying to display the error!");
581 if ( !xConnection.is() )
586 m_pImpl->xConnection.reset( xConnection );
602 return m_pImpl->xConnection.is();
613 return m_pImpl ?
m_pImpl->xDataSource : Reference< XPropertySet >();
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
const INetURLObject & GetURLObject() const
SfxMedium * GetMedium() const
css::uno::Reference< css::frame::XModel3 > GetModel() const
css::uno::Reference< css::embed::XStorage > const & GetStorage()
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
virtual SfxObjectShell * GetObjectShell() override
ODataSource createNewEvolutionGroupwise(const OUString &_rName)
creates a new Evolution GROUPWISE data source
ODataSource createNewEvolutionLdap(const OUString &_rName)
creates a new Evolution LDAP data source
ODataSourceContext(const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
ODataSource createNewKab(const OUString &_rName)
creates a new KDE address book data source
ODataSource createNewMacab(const OUString &_rName)
creates a new macOS address book data source
std::unique_ptr< ODataSourceContextImpl > m_pImpl
void disambiguate(OUString &_rDataSourceName)
disambiguates the given name by appending successive numbers
void getDataSourceNames(StringBag &_rNames) const
retrieves the names of all data sources
ODataSource createNewOther(const OUString &_rName)
creates a new Other data source; tdf117101: Spreadsheet by default
ODataSource createNewEvolution(const OUString &_rName)
creates a new Evolution local data source
ODataSource createNewThunderbird(const OUString &_rName)
creates a new Thunderbird data source
a non-UNO wrapper for a data source
void store(const AddressSettings &rSettings)
stores the database file
bool connect(weld::Window *_pMessageParent)
connects to the data source represented by this object
void remove()
removes the data source represented by the object from the data source context
bool isValid() const
checks whether or not the object represents a valid data source
void setDataSource(const css::uno::Reference< css::beans::XPropertySet > &_rxDS, const OUString &_sName)
set a new data source.
void disconnect()
disconnects from the data source (i.e. disposes the UNO connection hold internally)
std::unique_ptr< ODataSourceImpl > m_pImpl
bool rename(const OUString &_rName)
renames the data source
const StringBag & getTableNames() const
retrieves the tables names from the connection
ODataSource(const css::uno::Reference< css::uno::XComponentContext > &_rxORB)
constructs an object which is initially invalid
bool hasTable(const OUString &_rTableName) const
determines whether a given table exists
void registerDataSource(const OUString &_sRegisteredDataSourceName)
register the data source under the given name in the configuration
ODataSource & operator=(const ODataSource &_rSource)
copy assignment
css::uno::Reference< css::beans::XPropertySet > getDataSource() const
return the internal data source object
OUString getName() const
returns the name of the data source
bool isConnected() const
returns <TRUE> if the object has a valid connection, obtained from its data source
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
static ODataSource lcl_implCreateAndSetURL(const Reference< XComponentContext > &_rxORB, const OUString &_rName, const char *_pInitialAsciiURL)
creates and inserts a data source, and sets its URL property to the string given
static void lcl_implCreateAndInsert(const Reference< XComponentContext > &_rxContext, const OUString &_rName, Reference< XPropertySet > &_rxNewDataSource)
creates a new data source and inserts it into the context
static Reference< XDatabaseContext > lcl_getDataSourceContext(const Reference< XComponentContext > &_rxContext)
std::set< OUString > StringBag
static void lcl_registerDataSource(const Reference< XComponentContext > &_rxORB, const OUString &_sName, const OUString &_sURL)
Reference< XComponentContext > getProcessComponentContext()
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
OUString ModuleRes(TranslateId pId)
void VCL_DLLPUBLIC ShowServiceNotAvailableError(weld::Widget *pParent, std::u16string_view rServiceName, bool bError)
Reference< XNameAccess > xContext
StringBag aDataSourceNames
the UNO data source context
Reference< XComponentContext > xORB
ODataSourceContextImpl & operator=(const ODataSourceContextImpl &)=delete
ODataSourceContextImpl(const ODataSourceContextImpl &)=delete
ODataSourceContextImpl(const Reference< XComponentContext > &_rxORB)
for quicker name checks (without the UNO overhead)
ODataSourceImpl(const Reference< XComponentContext > &_rxORB)
Reference< XPropertySet > xDataSource
the service factory
Reference< XComponentContext > xORB
::utl::SharedUNOComponent< XConnection > xConnection
the UNO data source