20#include <config_features.h>
21#include <config_fuzzers.h>
28#include <com/sun/star/util/XCloseable.hpp>
29#include <com/sun/star/frame/XComponentLoader.hpp>
30#include <com/sun/star/frame/Desktop.hpp>
31#include <com/sun/star/util/XCloseListener.hpp>
32#include <com/sun/star/beans/XPropertySet.hpp>
33#include <com/sun/star/frame/XTitle.hpp>
34#include <osl/file.hxx>
51#include <com/sun/star/document/XStorageBasedDocument.hpp>
52#include <com/sun/star/script/DocumentDialogLibraryContainer.hpp>
53#include <com/sun/star/script/DocumentScriptLibraryContainer.hpp>
54#include <com/sun/star/document/XEmbeddedScripts.hpp>
55#include <com/sun/star/document/XScriptInvocationContext.hpp>
56#include <com/sun/star/ucb/ContentCreationException.hpp>
57#include <com/sun/star/lang/XMultiServiceFactory.hpp>
72#include <sfx2/strings.hrc>
73#include <sfx2/sfxsids.hrc>
90using ::basic::BasicManagerRepository;
94WeakReference< XInterface > theCurrentComponent;
96#if HAVE_FEATURE_SCRIPTING
99typedef ::std::map< XInterface*, OUString > VBAConstantNameMap;
100VBAConstantNameMap s_aRegisteredVBAConstants;
102OUString lclGetVBAGlobalConstName(
const Reference< XInterface >& rxComponent )
104 OSL_ENSURE( rxComponent.is(),
"lclGetVBAGlobalConstName - missing component" );
106 VBAConstantNameMap::iterator aIt = s_aRegisteredVBAConstants.find( rxComponent.get() );
107 if( aIt != s_aRegisteredVBAConstants.end() )
110 uno::Reference< beans::XPropertySet > xProps( rxComponent, uno::UNO_QUERY );
111 if( xProps.is() )
try
114 xProps->getPropertyValue(
"VBAGlobalConstantName") >>= aConstName;
117 catch (
const uno::Exception&)
125class SfxModelListener_Impl :
public ::cppu::WeakImplHelper< css::util::XCloseListener >
129 explicit SfxModelListener_Impl(
SfxObjectShell* pDoc ) : mpDoc(pDoc) {};
130 virtual void SAL_CALL queryClosing(
const css::lang::EventObject& aEvent,
sal_Bool bDeliverOwnership )
override ;
131 virtual void SAL_CALL notifyClosing(
const css::lang::EventObject& aEvent )
override ;
132 virtual void SAL_CALL disposing(
const css::lang::EventObject& aEvent )
override ;
138void SAL_CALL SfxModelListener_Impl::queryClosing(
const css::lang::EventObject& ,
sal_Bool )
142void SAL_CALL SfxModelListener_Impl::notifyClosing(
const css::lang::EventObject& )
145 mpDoc->Broadcast(
SfxHint(SfxHintId::Deinitializing) );
148void SAL_CALL SfxModelListener_Impl::disposing(
const css::lang::EventObject& _rEvent )
158#if HAVE_FEATURE_SCRIPTING
162 if ( _rEvent.Source.is() )
164 VBAConstantNameMap::iterator aIt = s_aRegisteredVBAConstants.find( _rEvent.Source.get() );
165 if ( aIt != s_aRegisteredVBAConstants.end() )
168 pAppMgr->SetGlobalUNOConstant( aIt->second,
Any( Reference< XInterface >() ) );
169 s_aRegisteredVBAConstants.erase( aIt );
174 if ( !mpDoc->Get_Impl()->bClosing )
181 :rDocShell( _rDocShell )
185 ,nVisualDocumentNumber( USHRT_MAX)
190 ,bIsNamedVisible( false)
191 ,bIsAbortingImport ( false)
192 ,bInPrepareClose( false )
193 ,bPreparedForClose( false )
194 ,bForbidReload( false )
195 ,bBasicInitialized( false )
196 ,bIsPrintJobCancelable( true )
197 ,bOwnsStorage( true )
198 ,bInitialized( false )
199 ,bModelInitialized( false )
200 ,bPreserveVersions( true )
201 ,m_bMacroSignBroken( false )
202 ,m_bNoBasicCapabilities( false )
203 ,m_bDocRecoverySupport( true )
204 ,bQueryLoadTemplate( true )
205 ,bLoadReadonly( false )
206 ,bUseUserData( true )
207 ,bUseThumbnailSave( true )
208 ,bSaveVersionOnClose( false )
209 ,m_bSharedXMLFlag( false )
210 ,m_bAllowShareControlFileClean( true )
211 ,m_bConfigOptionsChecked( false )
212 ,m_bMacroCallsSeenWhileLoading( false )
218 ,bRunningMacro( false )
219 ,bReadOnlyUI( false )
221 ,m_bEnableSetModified( true )
222 ,m_bIsModified( false )
224 ,m_bCreateTempStor( false )
226 ,m_bIncomplEncrWarnShown( false )
227 ,m_nModifyPasswordHash( 0 )
228 ,m_bModifyPasswordEntered( false )
229 ,m_bSavingForSigning( false )
230 ,m_bAllowModifiedBackAfterSigning( false )
234 rArr.push_back( pDoc );
248 , bIsInGenerateThumbnail (false)
249 , mbAvoidRecentDocs(false)
257 if ( !bScriptSupport )
258 pImpl->m_bNoBasicCapabilities =
true;
262 pImpl->m_bDocRecoverySupport =
false;
277 , bIsInGenerateThumbnail(false)
278 , mbAvoidRecentDocs(false)
289 pImpl->pBaseModel.set(
nullptr );
291 pImpl->pReloadTimer.reset();
294 if ( USHRT_MAX !=
pImpl->nVisualDocumentNumber && pSfxApp )
298 pImpl->aBasicManager.reset(
nullptr);
303 pImpl->pBaseModel.set(
nullptr );
309 if (
pImpl->mxObjectContainer )
311 pImpl->mxObjectContainer->CloseEmbeddedObjects();
312 pImpl->mxObjectContainer.reset();
315 if (
pImpl->bOwnsStorage &&
pImpl->m_xDocStorage.is() )
316 pImpl->m_xDocStorage->dispose();
322#if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
331 if ( !
pImpl->aTempName.isEmpty() )
334 osl::FileBase::getFileURLFromSystemPath(
pImpl->aTempName, aTmp );
335 ::utl::UCBContentHelper::Kill( aTmp );
342 pImpl->bIsPrintJobCancelable = bState;
348 return pImpl->bIsPrintJobCancelable;
363 if ( !
pImpl->bClosing )
369 pImpl->bClosing =
true;
370 Reference< util::XCloseable > xCloseable(
GetBaseModel(), UNO_QUERY );
372 if ( xCloseable.is() )
376 xCloseable->close(
true );
380 pImpl->bClosing =
false;
384 if (
pImpl->bClosing )
393 auto it = std::find( rDocs.begin(), rDocs.end(),
this );
394 if ( it != rDocs.end() )
414 if (!aShellID.isEmpty())
417 sal_Int64 nShellID =
reinterpret_cast<sal_Int64
>(pShell);
418 aShellID =
"0x" + OUString::number(nShellID, 16);
426 const std::function<
bool (
const SfxObjectShell* )>& isObjectShell,
435 if ( bOnlyVisible && pSh->IsPreview() && pSh->IsReadOnly() )
438 if ( (!isObjectShell || isObjectShell( pSh)) &&
452 const std::function<
bool (
const SfxObjectShell* )>& isObjectShell,
461 if ( rDocs[
nPos] == &rPrev )
471 if ( (!isObjectShell || isObjectShell( pSh)) &&
488 return pImpl->bInPrepareClose;
509 if(
pImpl->bInPrepareClose ||
pImpl->bPreparedForClose )
511 BoolEnv_Impl aBoolEnv( *
pImpl );
526 if ( pFrm->GetViewShell() )
528 bool bRet = pFrm->GetViewShell()->PrepareClose( bUI );
539 pImpl->bPreparedForClose =
true;
558 const Reference<XTitle> xTitle(*
pImpl->pBaseModel, UNO_QUERY_THROW);
559 const OUString sTitle = xTitle->getTitle ();
572 const SfxPoolItem* ppArgs[] = { &aItem, &aWarnItem,
nullptr };
578 const SfxPoolItem* ppArgs[] = { &aWarnItem,
nullptr };
584 if (
auto pBoolItem =
dynamic_cast< const SfxBoolItem *
>( pPoolItem ) )
585 if ( !pBoolItem->GetValue() )
595 pImpl->bPreparedForClose =
true;
600#if HAVE_FEATURE_SCRIPTING
608 const_cast< SfxObjectShell&
>( _rDocument ).InitBasicManager_Impl();
616 OSL_ENSURE( !Reference< XEmbeddedScripts >( _rDocument.
GetModel(), UNO_QUERY ).is(),
617 "lcl_getBasicManagerForDocument: inconsistency: no Basic, but an XEmbeddedScripts?" );
618 Reference< XModel > xForeignDocument;
619 Reference< XScriptInvocationContext > xContext( _rDocument.
GetModel(), UNO_QUERY );
622 xForeignDocument.set( xContext->getScriptContainer(), UNO_QUERY );
623 OSL_ENSURE( xForeignDocument.is() && xForeignDocument != _rDocument.
GetModel(),
624 "lcl_getBasicManagerForDocument: no Basic, but providing ourself as script container?" );
628 if ( xForeignDocument.is() )
639#if HAVE_FEATURE_SCRIPTING
642 pBasMgr = lcl_getBasicManagerForDocument( *
this );
646 catch (
const css::ucb::ContentCreationException&)
656#if !HAVE_FEATURE_SCRIPTING
659 if (
pImpl->m_bNoBasicCapabilities )
662 if ( !
pImpl->bBasicInitialized )
665 return pImpl->aBasicManager.isValid();
670#if HAVE_FEATURE_SCRIPTING
673 const Reference< XLibraryContainer >&
674 lcl_getOrCreateLibraryContainer(
bool _bScript, Reference< XLibraryContainer >& _rxContainer,
675 const Reference< XModel >& _rxDocument )
677 if ( !_rxContainer.is() )
681 Reference< XStorageBasedDocument > xStorageDoc( _rxDocument, UNO_QUERY );
682 const Reference< XComponentContext > xContext(
683 ::comphelper::getProcessComponentContext() );
684 _rxContainer.set ( _bScript
685 ? DocumentScriptLibraryContainer::create(
686 xContext, xStorageDoc )
687 : DocumentDialogLibraryContainer::create(
688 xContext, xStorageDoc )
691 catch (
const Exception&)
703#if HAVE_FEATURE_SCRIPTING
706 if ( !
pImpl->m_bNoBasicCapabilities )
707 return lcl_getOrCreateLibraryContainer(
false,
pImpl->xDialogLibraries,
GetModel() );
709 BasicManager* pBasMgr = lcl_getBasicManagerForDocument( *
this );
713 catch (
const css::ucb::ContentCreationException&)
718 SAL_WARN(
"sfx.doc",
"SfxObjectShell::GetDialogContainer: falling back to the application - is this really expected here?");
725#if HAVE_FEATURE_SCRIPTING
730 if ( !
pImpl->m_bNoBasicCapabilities )
731 return lcl_getOrCreateLibraryContainer(
true,
pImpl->xBasicLibraries,
GetModel() );
733 BasicManager* pBasMgr = lcl_getBasicManagerForDocument( *
this );
737 catch (
const css::ucb::ContentCreationException&)
742 SAL_WARN(
"sfx.doc",
"SfxObjectShell::GetBasicContainer: falling back to the application - is this really expected here?");
749#if !HAVE_FEATURE_SCRIPTING
753 return pMan ? pMan->
GetLib(0) :
nullptr;
794#if HAVE_FEATURE_SCRIPTING
795 DBG_ASSERT( !
pImpl->bBasicInitialized && !
pImpl->aBasicManager.isValid(),
"Local BasicManager already exists");
798 pImpl->aBasicManager.reset( BasicManagerRepository::getDocumentBasicManager(
GetModel() ) );
800 catch (
const css::ucb::ContentCreationException&)
804 DBG_ASSERT(
pImpl->aBasicManager.isValid(),
"SfxObjectShell::InitBasicManager_Impl: did not get a BasicManager!" );
805 pImpl->bBasicInitialized =
true;
826 return s_EventNameContainer;
837 OSL_ENSURE( !
pImpl->pBaseModel.is() || pModel ==
nullptr,
"Model already set!" );
838 pImpl->pBaseModel.set( pModel );
839 if (
pImpl->pBaseModel.is() )
841 pImpl->pBaseModel->addCloseListener(
new SfxModelListener_Impl(
this) );
848 return pImpl->pBaseModel;
853 pImpl->nStyleFilter = nSet;
858 return pImpl->nStyleFilter;
864 WeakReference< XInterface >& rTheCurrentComponent = theCurrentComponent;
866 Reference< XInterface > xOldCurrentComp(rTheCurrentComponent);
867 if ( _rxComponent == xOldCurrentComp )
875#if HAVE_FEATURE_SCRIPTING
877 rTheCurrentComponent = _rxComponent;
885 if ( _rxComponent.is() )
887 OUString aVBAConstName = lclGetVBAGlobalConstName( _rxComponent );
888 if ( !aVBAConstName.isEmpty() )
891 s_aRegisteredVBAConstants[ _rxComponent.get() ] = aVBAConstName;
895 else if ( xOldCurrentComp.is() )
897 OUString aVBAConstName = lclGetVBAGlobalConstName( xOldCurrentComp );
898 if ( !aVBAConstName.isEmpty() )
901 s_aRegisteredVBAConstants.erase( xOldCurrentComp.get() );
909 return theCurrentComponent;
916 OUString aFact( rFact );
917 OUString aPrefix(
"private:factory/");
918 if ( aFact.startsWith( aPrefix ) )
919 aFact = aFact.copy( aPrefix.getLength() );
920 sal_Int32
nPos = aFact.indexOf(
'?' );
923 aFact = aFact.copy( 0,
nPos );
925 aFact = aFact.replaceAll(
"4",
"");
926 aFact = aFact.toAsciiLowerCase();
932 OUString aServiceName = rFact;
934 if ( aFact ==
"swriter" )
936 aServiceName =
"com.sun.star.text.TextDocument";
938 else if ( aFact ==
"sweb" || aFact ==
"swriter/web" )
940 aServiceName =
"com.sun.star.text.WebDocument";
942 else if ( aFact ==
"sglobal" || aFact ==
"swriter/globaldocument" )
944 aServiceName =
"com.sun.star.text.GlobalDocument";
946 else if ( aFact ==
"scalc" )
948 aServiceName =
"com.sun.star.sheet.SpreadsheetDocument";
950 else if ( aFact ==
"sdraw" )
952 aServiceName =
"com.sun.star.drawing.DrawingDocument";
954 else if ( aFact ==
"simpress" )
956 aServiceName =
"com.sun.star.presentation.PresentationDocument";
958 else if ( aFact ==
"schart" )
960 aServiceName =
"com.sun.star.chart.ChartDocument";
962 else if ( aFact ==
"smath" )
964 aServiceName =
"com.sun.star.formula.FormulaProperties";
966#if HAVE_FEATURE_SCRIPTING
967 else if ( aFact ==
"sbasic" )
969 aServiceName =
"com.sun.star.script.BasicIDE";
972#if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
973 else if ( aFact ==
"sdatabase" )
975 aServiceName =
"com.sun.star.sdb.OfficeDatabaseDocument";
990 if ( !rServiceName.isEmpty() )
992 uno::Reference < frame::XModel > xDoc( ::comphelper::getProcessServiceFactory()->
createInstance( rServiceName ), UNO_QUERY );
1005 uno::Sequence < beans::PropertyValue > aProps;
1010 OUString aTarget(
"_blank");
1011 if ( pFileNameItem )
1014 aTarget = pTargetItem->GetValue();
1016 uno::Reference < frame::XComponentLoader > xLoader =
1019 Reference <lang::XComponent> xComp;
1022 xComp = xLoader->loadComponentFromURL(
aURL, aTarget, 0, aProps);
1024 catch (
const uno::Exception&)
1035 Reference<lang::XUnoTunnel> xTunnel(xComp, UNO_QUERY);
1039 return comphelper::getSomething_cast<SfxObjectShell>(xTunnel->getSomething(
aSeq));
1054 if (css::uno::Reference<css::container::XChild> xChildModel{ xChild, css::uno::UNO_QUERY })
1066 pImpl->bInitialized =
true;
1069 if ( i_fromInitNew )
1084 SAL_WARN(
"sfx.doc",
"function not implemented" );
1092 SAL_WARN(
"sfx.doc",
"function not implemented" );
1100 SAL_WARN(
"sfx.doc",
"function not implemented" );
1107 SAL_WARN(
"sfx.doc",
"function not implemented" );
1114 SAL_WARN(
"sfx.doc",
"function not implemented" );
short ExecuteQuerySaveDocument(weld::Widget *_pParent, std::u16string_view _rTitle)
Opens the general query save document dialog.
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
SfxApplication * SfxGetpApp()
void TransformItems(sal_uInt16 nSlotId, const SfxItemSet &rSet, uno::Sequence< beans::PropertyValue > &rArgs, const SfxSlot *pSlot)
StarBASIC * GetLib(sal_uInt16 nLib) const
const css::uno::Reference< css::script::XPersistentLibraryContainer > & GetScriptLibraryContainer() const
void SetGlobalUNOConstant(const OUString &rName, const css::uno::Any &_rValue, css::uno::Any *pOldValue=nullptr)
const css::uno::Reference< css::script::XPersistentLibraryContainer > & GetDialogLibraryContainer() const
const OUString & GetValue() const
static OUString GetEventName(GlobalEventId nID)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
static SfxApplication * Get()
css::script::XLibraryContainer * GetBasicContainer()
void ReleaseIndex(sal_uInt16 i)
static BasicManager * GetBasicManager()
void RemoveDdeTopic(SfxObjectShell const *)
css::script::XLibraryContainer * GetDialogContainer()
SAL_DLLPRIVATE std::vector< SfxObjectShell * > & GetObjectShells_Impl() const
void NotifyEvent(const SfxEventHint &rEvent, bool bSynchron=true)
const DdeService * GetDdeService() const
BasicManager * get() const
returns the BasicManager which this instance is currently bound to
const SfxPoolItem * ExecuteSynchron(sal_uInt16 nSlot, const SfxPoolItem **pArgs=nullptr)
SAL_WARN_UNUSED_RESULT SfxViewFrame * GetCurrentViewFrame() const
SAL_DLLPRIVATE bool PrepareClose_Impl(bool bUI)
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
SAL_DLLPRIVATE void CloseAndReleaseStreams_Impl()
OUString GetBaseURL(bool bForSaving=false)
const INetURLObject & GetURLObject() const
SAL_DLLPRIVATE void CanDisposeStorage_Impl(bool bDisposeStorage)
SAL_DLLPRIVATE bool HasStorage_Impl() const
css::uno::Reference< css::embed::XStorage > GetStorage(bool bCreateTempFile=true)
virtual bool HasChangeRecordProtection() const
static SfxObjectShell * CreateObject(const OUString &rServiceName, SfxObjectCreateMode=SfxObjectCreateMode::STANDARD)
virtual bool PrepareClose(bool bUI=true)
css::uno::Reference< css::script::XLibraryContainer > GetBasicContainer()
SfxObjectShell(SfxObjectCreateMode)
Constructor of the class SfxObjectShell.
static OUString GetServiceNameFromFactory(const OUString &rFact)
SAL_DLLPRIVATE void InitBasicManager_Impl()
virtual bool GetProtectionHash(css::uno::Sequence< sal_Int8 > &rPasswordHash)
static OUString CreateShellID(const SfxObjectShell *pShell)
virtual SfxObjectShell * GetObjectShell() override
sal_uInt16 GetAutoStyleFilterIndex() const
static css::uno::Reference< css::lang::XComponent > CreateAndLoadComponent(const SfxItemSet &rSet)
SAL_DLLPRIVATE void SetInitialized_Impl(const bool i_fromInitNew)
bool IsInModalMode() const
bool IsInPrepareClose() const
virtual css::uno::Sequence< OUString > GetEventNames()
SfxProgress * GetProgress() const
BasicManager * GetBasicManager() const
static SfxObjectShell * GetShellFromComponent(const css::uno::Reference< css::uno::XInterface > &xComp)
bool IsEnableSetModified() const
SfxObjectCreateMode eCreateMode
virtual ~SfxObjectShell() override
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetNext(const SfxObjectShell &rPrev, const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
virtual bool IsChangeRecording() const
static void SetCurrentComponent(const css::uno::Reference< css::uno::XInterface > &_rxComponent)
static SfxObjectShell * GetParentShell(const css::uno::Reference< css::uno::XInterface > &xChild)
SAL_DLLPRIVATE void FreeSharedFile(const OUString &aTempFileURL)
virtual bool Close() override
void SetAutoStyleFilterIndex(sal_uInt16 nSet)
css::uno::Reference< css::script::XLibraryContainer > GetDialogContainer()
virtual void SetProtectionPassword(const OUString &rPassword)
static SfxObjectShell * CreateObjectByFactoryName(const OUString &rURL, SfxObjectCreateMode=SfxObjectCreateMode::STANDARD)
SAL_DLLPRIVATE SfxObjectShell_Impl * Get_Impl()
SfxMedium * GetMedium() const
css::uno::Reference< css::frame::XModel3 > GetModel() const
css::uno::Reference< css::frame::XModel3 > GetBaseModel() const
std::unique_ptr< struct SfxObjectShell_Impl > pImpl
StarBASIC * GetBasic() const
static css::uno::Reference< css::uno::XInterface > GetCurrentComponent()
void EnableSetModified(bool bEnable=true)
virtual void SetChangeRecording(bool bActivate, bool bLockAllViews=false)
bool IsSaveVersionOnClose() const
static SAL_WARN_UNUSED_RESULT SfxObjectShell * GetFirst(const std::function< bool(const SfxObjectShell *)> &isObjectShell=nullptr, bool bOnlyVisible=true)
SfxObjectCreateMode GetCreateMode() const
bool Stamp_GetPrintCancelState() const
SAL_DLLPRIVATE void SetActivateEvent_Impl(SfxEventHintId)
static SAL_WARN_UNUSED_RESULT SfxObjectShell * Current()
void SetBaseModel(SfxBaseModel *pModel)
void Stamp_SetPrintCancelState(bool bState)
virtual bool IsVoidItem() const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
SfxBindings & GetBindings()
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetNext(const SfxViewFrame &rPrev, const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
static SAL_WARN_UNUSED_RESULT SfxViewFrame * GetFirst(const SfxObjectShell *pDoc=nullptr, bool bOnlyVisible=true)
SfxFrame & GetFrame() const
virtual SfxObjectShell * GetObjectShell() override
weld::Window * GetFrameWeld() const
static void SetViewFrame(SfxViewFrame *)
static BasicManager * getDocumentBasicManager(const css::uno::Reference< css::frame::XModel > &_rxDocumentModel)
static void CloseMethod(SfxBindings &rBindings)
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
Reference< XComponentContext > getProcessComponentContext()
#define SFX_GLOBAL_CLASSID
@ DISABLE_DOCUMENT_RECOVERY
@ DISABLE_EMBEDDED_SCRIPTS
OUString SfxResId(TranslateId aId)
virtual ~SfxObjectShell_Impl()
SfxObjectShell_Impl(SfxObjectShell &_rDocShell)
SfxBasicManagerHolder aBasicManager
bool m_bNoBasicCapabilities