22#include <com/sun/star/beans/NamedValue.hpp>
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/document/XExtendedFilterDetection.hpp>
25#include <com/sun/star/embed/ElementModes.hpp>
26#include <com/sun/star/embed/XStorage.hpp>
27#include <com/sun/star/frame/Desktop.hpp>
28#include <com/sun/star/frame/XController2.hpp>
29#include <com/sun/star/frame/XFrame.hpp>
30#include <com/sun/star/frame/XFrameLoader.hpp>
31#include <com/sun/star/frame/XLoadEventListener.hpp>
32#include <com/sun/star/frame/XModel2.hpp>
33#include <com/sun/star/io/XInputStream.hpp>
34#include <com/sun/star/lang/XServiceInfo.hpp>
35#include <com/sun/star/sdb/DatabaseContext.hpp>
36#include <com/sun/star/sdb/XDocumentDataSource.hpp>
37#include <com/sun/star/task/XJobExecutor.hpp>
38#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
39#include <com/sun/star/task/InteractionHandler.hpp>
40#include <com/sun/star/util/URLTransformer.hpp>
41#include <com/sun/star/util/XURLTransformer.hpp>
42#include <com/sun/star/view/XSelectionSupplier.hpp>
43#include <com/sun/star/sdb/application/DatabaseObjectContainer.hpp>
44#include <com/sun/star/sdb/application/NamedDatabaseObject.hpp>
45#include <com/sun/star/frame/XLoadable.hpp>
74using ::com::sun::star::awt::XWindow;
75using ::com::sun::star::sdb::application::NamedDatabaseObject;
82class DBTypeDetection :
public ::cppu::WeakImplHelper< XExtendedFilterDetection, XServiceInfo>
87 explicit DBTypeDetection(
const Reference< XComponentContext >&);
94 virtual OUString SAL_CALL detect( css::uno::Sequence< css::beans::PropertyValue >& Descriptor )
override;
99DBTypeDetection::DBTypeDetection(
const Reference< XComponentContext >& _rxContext)
104OUString SAL_CALL DBTypeDetection::detect( css::uno::Sequence< css::beans::PropertyValue >& Descriptor )
109 bool bStreamFromDescr =
false;
110 OUString sURL = aMedia.getOrDefault(
"URL", OUString() );
112 Reference< XInputStream > xInStream( aMedia.getOrDefault(
"InputStream", Reference< XInputStream >() ) );
113 Reference< XPropertySet > xStorageProperties;
114 if ( xInStream.is() )
116 bStreamFromDescr =
true;
122 OUString sSalvagedURL( aMedia.getOrDefault(
"SalvagedFile", OUString() ) );
124 OUString sFileLocation( sSalvagedURL.isEmpty() ? sURL : sSalvagedURL );
125 if ( !sFileLocation.isEmpty() )
128 sFileLocation, ElementModes::READ,
m_aContext ), UNO_QUERY );
132 if ( xStorageProperties.is() )
136 if ( sMediaType == MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII || sMediaType == MIMETYPE_VND_SUN_XML_BASE_ASCII )
138 if ( bStreamFromDescr && !sURL.startsWith(
"private:stream" ) )
142 aMedia.remove(
"InputStream" );
143 aMedia.remove(
"Stream" );
144 aMedia >>= Descriptor;
147 ::comphelper::disposeComponent(xStorageProperties);
148 if ( xInStream.is() )
149 xInStream->closeInput();
159 ::comphelper::disposeComponent(xStorageProperties);
161 }
catch(Exception&){}
166OUString SAL_CALL DBTypeDetection::getImplementationName()
168 return "org.openoffice.comp.dbflt.DBTypeDetection";
172sal_Bool SAL_CALL DBTypeDetection::supportsService(
const OUString& ServiceName)
178Sequence< OUString > SAL_CALL DBTypeDetection::getSupportedServiceNames()
180 return {
"com.sun.star.document.ExtendedTypeDetection" };
185extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
187 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const& )
189 return cppu::acquire(new ::dbaxml::DBTypeDetection(context));
197class DBContentLoader :
public ::cppu::WeakImplHelper< XFrameLoader, XServiceInfo>
200 const Reference< XComponentContext >
m_aContext;
205 DECL_LINK( OnStartTableWizard,
void*,
void );
207 explicit DBContentLoader(
const Reference< XComponentContext >&);
215 virtual void SAL_CALL load(
const Reference< XFrame > & _rFrame,
const OUString& _rURL,
216 const Sequence< PropertyValue >& _rArgs,
217 const Reference< XLoadEventListener > & _rListener)
override;
218 virtual void SAL_CALL cancel()
override;
221 bool impl_executeNewDatabaseWizard( Reference< XModel >
const & _rxModel,
bool& _bShouldStartTableWizard );
226DBContentLoader::DBContentLoader(
const Reference< XComponentContext >& _rxFactory)
234OUString SAL_CALL DBContentLoader::getImplementationName()
236 return "org.openoffice.comp.dbflt.DBContentLoader2";
240sal_Bool SAL_CALL DBContentLoader::supportsService(
const OUString& ServiceName)
246Sequence< OUString > SAL_CALL DBContentLoader::getSupportedServiceNames()
248 return {
"com.sun.star.frame.FrameLoader" };
254 bool lcl_urlAllowsInteraction(
const Reference<XComponentContext> & _rContext,
const OUString& _rURL )
256 bool bDoesAllow =
false;
259 Reference< XURLTransformer > xTransformer( URLTransformer::create(_rContext) );
261 aURL.Complete = _rURL;
262 xTransformer->parseStrict(
aURL );
263 bDoesAllow =
aURL.Arguments ==
"Interactive";
265 catch(
const Exception& )
267 TOOLS_WARN_EXCEPTION(
"dbaccess",
"lcl_urlAllowsInteraction: caught an exception while analyzing the URL!" );
272 Reference< XWindow > lcl_getTopMostWindow(
const Reference<XComponentContext> & _rxContext )
274 Reference< XWindow > xWindow;
276 Reference < XDesktop2 > xDesktop = Desktop::create(_rxContext);
277 Reference < XFrame > xActiveFrame = xDesktop->getActiveFrame();
278 if ( xActiveFrame.is() )
280 xWindow = xActiveFrame->getContainerWindow();
281 Reference<XFrame>
xFrame = xActiveFrame;
286 xWindow =
xFrame->getContainerWindow();
292bool DBContentLoader::impl_executeNewDatabaseWizard( Reference< XModel >
const & _rxModel,
bool& _bShouldStartTableWizard )
297 {
"InitialSelection",
Any(_rxModel)}
301 Reference< XExecutableDialog > xAdminDialog(
m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.sdb.DatabaseWizardDialog", aWizardArgs,
m_aContext), UNO_QUERY_THROW);
304 if ( RET_OK != xAdminDialog->execute() )
307 Reference<XPropertySet> xProp(xAdminDialog,UNO_QUERY);
308 bool bSuccess =
false;
309 xProp->getPropertyValue(
"OpenDatabase") >>= bSuccess;
310 xProp->getPropertyValue(
"StartTableWizard") >>= _bShouldStartTableWizard;
314void SAL_CALL DBContentLoader::load(
const Reference< XFrame > & rFrame,
const OUString& _rURL,
315 const Sequence< PropertyValue >& rArgs,
316 const Reference< XLoadEventListener > & rListener)
320 bool bPreview = aMediaDesc.getOrDefault(
"Preview",
false );
324 rListener->loadCancelled(
this);
328 Reference< XModel >
xModel = aMediaDesc.getOrDefault(
"Model", Reference< XModel >() );
329 OUString sSalvagedURL = aMediaDesc.getOrDefault(
"SalvagedFile", _rURL );
331 bool bCreateNew =
false;
332 bool bStartTableWizard =
false;
334 bool bSuccess =
true;
342 if ( !aMediaDesc.has(
"InteractionHandler" ) )
344 Reference< XInteractionHandler2 > xHandler( InteractionHandler::createWithParent(
m_aContext,
nullptr) );
345 aMediaDesc.put(
"InteractionHandler", xHandler );
349 Reference< XOfficeDatabaseDocument > xExistentDBDoc;
350 xModel.set( aMediaDesc.getOrDefault(
"Model", xExistentDBDoc ), UNO_QUERY );
351 aMediaDesc.remove(
"Model" );
354 OUString sViewName = aMediaDesc.getOrDefault(
"ViewName", OUString(
"Default" ) );
355 aMediaDesc.remove(
"ViewName" );
358 Reference< XDatabaseContext > xDatabaseContext;
360 sal_Int32 nInitialSelection = -1;
363 xDatabaseContext = DatabaseContext::create(
m_aContext);
366 bCreateNew = sFactoryName.match(_rURL);
368 Reference< XDocumentDataSource > xDocumentDataSource;
369 bool bNewAndInteractive =
false;
372 bNewAndInteractive = lcl_urlAllowsInteraction(
m_aContext, _rURL );
373 xDocumentDataSource.set( xDatabaseContext->createInstance(), UNO_QUERY_THROW );
379 xDocumentDataSource.set( xDatabaseContext->createInstanceWithArguments( aCreationArgs.
getWrappedNamedValues() ), UNO_QUERY_THROW );
382 xModel.set( xDocumentDataSource->getDatabaseDocument(), UNO_QUERY );
384 if ( bCreateNew &&
xModel.is() )
386 if ( bNewAndInteractive )
388 bSuccess = impl_executeNewDatabaseWizard(
xModel, bStartTableWizard );
394 Reference< XLoadable > xLoad(
xModel, UNO_QUERY_THROW );
398 catch(
const Exception& )
405 nInitialSelection = css::sdb::application::DatabaseObjectContainer::TABLES;
411 if ( rListener.is() )
412 rListener->loadCancelled(
this);
422 bool bNeedLoad =
xModel->getURL().isEmpty();
425 aMediaDesc.put(
"FileName", _rURL );
426 Sequence< PropertyValue > aResource( aMediaDesc.getPropertyValues() );
430 Reference< XLoadable > xLoad(
xModel, UNO_QUERY_THROW );
431 xLoad->load( aResource );
435 xModel->attachResource( _rURL, aResource );
437 catch(
const Exception&)
448 Reference< XModel2 > xModel2(
xModel, UNO_QUERY_THROW );
449 Reference< XController2 >
xController( xModel2->createViewController( sViewName, Sequence< PropertyValue >(), rFrame ), UNO_SET_THROW );
456 catch(
const Exception& )
465 if ( rListener.is() )
466 rListener->loadFinished(
this);
468 if ( nInitialSelection != -1 )
470 Reference< css::view::XSelectionSupplier > xDocView(
xModel->getCurrentController(), UNO_QUERY );
473 NamedDatabaseObject aSelection;
474 aSelection.Type = nInitialSelection;
475 xDocView->select(
Any( aSelection ) );
479 if ( bStartTableWizard )
491 if ( rListener.is() )
492 rListener->loadCancelled(
this );
496 ::comphelper::disposeComponent(
xModel);
499void DBContentLoader::cancel()
513 Reference< XJobExecutor > xTableWizard(
m_aContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.wizards.table.CallTableWizard", aWizArgs,
m_aContext), UNO_QUERY);
514 if ( xTableWizard.is() )
515 xTableWizard->trigger(
"start");
526extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
528 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const& )
530 return cppu::acquire(new ::dbaxml::DBContentLoader(context));
constexpr OUStringLiteral sMediaType
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static void RemoveUserEvent(ImplSVEvent *nUserEvent)
OUString GetFactoryEmptyDocumentURL(EFactory eFactory) const
css::uno::Sequence< css::uno::Any > getWrappedNamedValues() const
bool put(const OUString &_rValueName, const VALUE_TYPE &_rValue)
static css::uno::Reference< css::embed::XStorage > GetStorageFromInputStream(const css::uno::Reference< css::io::XInputStream > &xStream, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
static css::uno::Reference< css::embed::XStorage > GetStorageFromURL(const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
const Reference< XComponentContext > m_aContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * org_openoffice_comp_dbflt_DBTypeDetection_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
ImplSVEvent * m_nStartWizard
Reference< XFrameLoader > m_xMySelf
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * org_openoffice_comp_dbflt_DBContentLoader2_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
#define LINK(Instance, Class, Member)
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
IMPL_LINK_NOARG(DBContentLoader, OnStartTableWizard, void *, void)
void ConnectFrameControllerModel(const css::uno::Reference< css::frame::XFrame > &xFrame, const css::uno::Reference< css::frame::XController2 > &xController, const css::uno::Reference< css::frame::XModel > &xModel)
constexpr OUStringLiteral INFO_MEDIATYPE
constexpr OUStringLiteral INFO_POOLURL
Reference< XController > xController
the controller of the sub component. Must not be <NULL>
Reference< XFrame > xFrame
the frame which the component resides in. Must not be <NULL>
Reference< XModel > xModel
the model of the sub component. Might be <NULL>