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)
250 , bRememberSignature(false)
258 if ( !bScriptSupport )
259 pImpl->m_bNoBasicCapabilities =
true;
263 pImpl->m_bDocRecoverySupport =
false;
278 , bIsInGenerateThumbnail(false)
279 , mbAvoidRecentDocs(false)
280 , bRememberSignature(false)
291 pImpl->pBaseModel.set(
nullptr );
293 pImpl->pReloadTimer.reset();
296 if ( USHRT_MAX !=
pImpl->nVisualDocumentNumber && pSfxApp )
300 pImpl->aBasicManager.reset(
nullptr);
305 pImpl->pBaseModel.set(
nullptr );
311 if (
pImpl->mxObjectContainer )
313 pImpl->mxObjectContainer->CloseEmbeddedObjects();
314 pImpl->mxObjectContainer.reset();
317 if (
pImpl->bOwnsStorage &&
pImpl->m_xDocStorage.is() )
318 pImpl->m_xDocStorage->dispose();
324#if HAVE_FEATURE_MULTIUSER_ENVIRONMENT
333 if ( !
pImpl->aTempName.isEmpty() )
336 osl::FileBase::getFileURLFromSystemPath(
pImpl->aTempName, aTmp );
337 ::utl::UCBContentHelper::Kill( aTmp );
344 pImpl->bIsPrintJobCancelable = bState;
350 return pImpl->bIsPrintJobCancelable;
365 if ( !
pImpl->bClosing )
371 pImpl->bClosing =
true;
372 Reference< util::XCloseable > xCloseable(
GetBaseModel(), UNO_QUERY );
374 if ( xCloseable.is() )
378 xCloseable->close(
true );
382 pImpl->bClosing =
false;
386 if (
pImpl->bClosing )
395 auto it = std::find( rDocs.begin(), rDocs.end(),
this );
396 if ( it != rDocs.end() )
416 if (!aShellID.isEmpty())
419 sal_Int64 nShellID =
reinterpret_cast<sal_Int64
>(pShell);
420 aShellID =
"0x" + OUString::number(nShellID, 16);
428 const std::function<
bool (
const SfxObjectShell* )>& isObjectShell,
437 if ( bOnlyVisible && pSh->IsPreview() && pSh->IsReadOnly() )
440 if ( (!isObjectShell || isObjectShell( pSh)) &&
454 const std::function<
bool (
const SfxObjectShell* )>& isObjectShell,
463 if ( rDocs[
nPos] == &rPrev )
473 if ( (!isObjectShell || isObjectShell( pSh)) &&
490 return pImpl->bInPrepareClose;
511 if(
pImpl->bInPrepareClose ||
pImpl->bPreparedForClose )
513 BoolEnv_Impl aBoolEnv( *
pImpl );
528 if ( pFrm->GetViewShell() )
530 bool bRet = pFrm->GetViewShell()->PrepareClose( bUI );
541 pImpl->bPreparedForClose =
true;
560 const Reference<XTitle> xTitle(*
pImpl->pBaseModel, UNO_QUERY_THROW);
561 const OUString sTitle = xTitle->getTitle ();
573 const SfxPoolItem* ppArgs[] = { &aWarnItem,
nullptr };
580 const SfxPoolItem* ppArgs[] = { &aItem, &aWarnItem,
nullptr };
586 const SfxPoolItem* ppArgs[] = { &aWarnItem,
nullptr };
592 if (
auto pBoolItem =
dynamic_cast< const SfxBoolItem *
>( pPoolItem ) )
593 if ( !pBoolItem->GetValue() )
603 pImpl->bPreparedForClose =
true;
608#if HAVE_FEATURE_SCRIPTING
616 const_cast< SfxObjectShell&
>( _rDocument ).InitBasicManager_Impl();
624 OSL_ENSURE( !Reference< XEmbeddedScripts >( _rDocument.
GetModel(), UNO_QUERY ).is(),
625 "lcl_getBasicManagerForDocument: inconsistency: no Basic, but an XEmbeddedScripts?" );
626 Reference< XModel > xForeignDocument;
627 Reference< XScriptInvocationContext > xContext( _rDocument.
GetModel(), UNO_QUERY );
630 xForeignDocument.set( xContext->getScriptContainer(), UNO_QUERY );
631 OSL_ENSURE( xForeignDocument.is() && xForeignDocument != _rDocument.
GetModel(),
632 "lcl_getBasicManagerForDocument: no Basic, but providing ourself as script container?" );
636 if ( xForeignDocument.is() )
647#if HAVE_FEATURE_SCRIPTING
650 pBasMgr = lcl_getBasicManagerForDocument( *
this );
654 catch (
const css::ucb::ContentCreationException&)
664#if !HAVE_FEATURE_SCRIPTING
667 if (
pImpl->m_bNoBasicCapabilities )
670 if ( !
pImpl->bBasicInitialized )
673 return pImpl->aBasicManager.isValid();
678#if HAVE_FEATURE_SCRIPTING
681 const Reference< XLibraryContainer >&
682 lcl_getOrCreateLibraryContainer(
bool _bScript, Reference< XLibraryContainer >& _rxContainer,
683 const Reference< XModel >& _rxDocument )
685 if ( !_rxContainer.is() )
689 Reference< XStorageBasedDocument > xStorageDoc( _rxDocument, UNO_QUERY );
690 const Reference< XComponentContext > xContext(
691 ::comphelper::getProcessComponentContext() );
692 _rxContainer.set ( _bScript
693 ? DocumentScriptLibraryContainer::create(
694 xContext, xStorageDoc )
695 : DocumentDialogLibraryContainer::create(
696 xContext, xStorageDoc )
699 catch (
const Exception&)
711#if HAVE_FEATURE_SCRIPTING
714 if ( !
pImpl->m_bNoBasicCapabilities )
715 return lcl_getOrCreateLibraryContainer(
false,
pImpl->xDialogLibraries,
GetModel() );
717 BasicManager* pBasMgr = lcl_getBasicManagerForDocument( *
this );
721 catch (
const css::ucb::ContentCreationException&)
726 SAL_WARN(
"sfx.doc",
"SfxObjectShell::GetDialogContainer: falling back to the application - is this really expected here?");
733#if HAVE_FEATURE_SCRIPTING
738 if ( !
pImpl->m_bNoBasicCapabilities )
739 return lcl_getOrCreateLibraryContainer(
true,
pImpl->xBasicLibraries,
GetModel() );
741 BasicManager* pBasMgr = lcl_getBasicManagerForDocument( *
this );
745 catch (
const css::ucb::ContentCreationException&)
750 SAL_WARN(
"sfx.doc",
"SfxObjectShell::GetBasicContainer: falling back to the application - is this really expected here?");
757#if !HAVE_FEATURE_SCRIPTING
761 return pMan ? pMan->
GetLib(0) :
nullptr;
802#if HAVE_FEATURE_SCRIPTING
803 DBG_ASSERT( !
pImpl->bBasicInitialized && !
pImpl->aBasicManager.isValid(),
"Local BasicManager already exists");
806 pImpl->aBasicManager.reset( BasicManagerRepository::getDocumentBasicManager(
GetModel() ) );
808 catch (
const css::ucb::ContentCreationException&)
812 DBG_ASSERT(
pImpl->aBasicManager.isValid(),
"SfxObjectShell::InitBasicManager_Impl: did not get a BasicManager!" );
813 pImpl->bBasicInitialized =
true;
834 return s_EventNameContainer;
845 OSL_ENSURE( !
pImpl->pBaseModel.is() || pModel ==
nullptr,
"Model already set!" );
846 pImpl->pBaseModel.set( pModel );
847 if (
pImpl->pBaseModel.is() )
849 pImpl->pBaseModel->addCloseListener(
new SfxModelListener_Impl(
this) );
856 return pImpl->pBaseModel;
861 pImpl->nStyleFilter = nSet;
866 return pImpl->nStyleFilter;
872 WeakReference< XInterface >& rTheCurrentComponent = theCurrentComponent;
874 Reference< XInterface > xOldCurrentComp(rTheCurrentComponent);
875 if ( _rxComponent == xOldCurrentComp )
883#if HAVE_FEATURE_SCRIPTING
885 rTheCurrentComponent = _rxComponent;
893 if ( _rxComponent.is() )
895 OUString aVBAConstName = lclGetVBAGlobalConstName( _rxComponent );
896 if ( !aVBAConstName.isEmpty() )
899 s_aRegisteredVBAConstants[ _rxComponent.get() ] = aVBAConstName;
903 else if ( xOldCurrentComp.is() )
905 OUString aVBAConstName = lclGetVBAGlobalConstName( xOldCurrentComp );
906 if ( !aVBAConstName.isEmpty() )
909 s_aRegisteredVBAConstants.erase( xOldCurrentComp.get() );
917 return theCurrentComponent;
924 OUString aFact( rFact );
925 OUString aPrefix(
"private:factory/");
926 if ( aFact.startsWith( aPrefix ) )
927 aFact = aFact.copy( aPrefix.getLength() );
928 sal_Int32
nPos = aFact.indexOf(
'?' );
931 aFact = aFact.copy( 0,
nPos );
933 aFact = aFact.replaceAll(
"4",
"");
934 aFact = aFact.toAsciiLowerCase();
940 OUString aServiceName = rFact;
942 if ( aFact ==
"swriter" )
944 aServiceName =
"com.sun.star.text.TextDocument";
946 else if ( aFact ==
"sweb" || aFact ==
"swriter/web" )
948 aServiceName =
"com.sun.star.text.WebDocument";
950 else if ( aFact ==
"sglobal" || aFact ==
"swriter/globaldocument" )
952 aServiceName =
"com.sun.star.text.GlobalDocument";
954 else if ( aFact ==
"scalc" )
956 aServiceName =
"com.sun.star.sheet.SpreadsheetDocument";
958 else if ( aFact ==
"sdraw" )
960 aServiceName =
"com.sun.star.drawing.DrawingDocument";
962 else if ( aFact ==
"simpress" )
964 aServiceName =
"com.sun.star.presentation.PresentationDocument";
966 else if ( aFact ==
"schart" )
968 aServiceName =
"com.sun.star.chart.ChartDocument";
970 else if ( aFact ==
"smath" )
972 aServiceName =
"com.sun.star.formula.FormulaProperties";
974#if HAVE_FEATURE_SCRIPTING
975 else if ( aFact ==
"sbasic" )
977 aServiceName =
"com.sun.star.script.BasicIDE";
980#if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
981 else if ( aFact ==
"sdatabase" )
983 aServiceName =
"com.sun.star.sdb.OfficeDatabaseDocument";
998 if ( !rServiceName.isEmpty() )
1000 uno::Reference < frame::XModel > xDoc( ::comphelper::getProcessServiceFactory()->
createInstance( rServiceName ), UNO_QUERY );
1013 uno::Sequence < beans::PropertyValue > aProps;
1018 OUString aTarget(
"_blank");
1019 if ( pFileNameItem )
1022 aTarget = pTargetItem->GetValue();
1024 uno::Reference < frame::XComponentLoader > xLoader =
1027 Reference <lang::XComponent> xComp;
1030 xComp = xLoader->loadComponentFromURL(
aURL, aTarget, 0, aProps);
1032 catch (
const uno::Exception&)
1043 Reference<lang::XUnoTunnel> xTunnel(xComp, UNO_QUERY);
1047 return comphelper::getSomething_cast<SfxObjectShell>(xTunnel->getSomething(
aSeq));
1062 if (css::uno::Reference<css::container::XChild> xChildModel{ xChild, css::uno::UNO_QUERY })
1074 pImpl->bInitialized =
true;
1077 if ( i_fromInitNew )
1092 SAL_WARN(
"sfx.doc",
"function not implemented" );
1100 SAL_WARN(
"sfx.doc",
"function not implemented" );
1108 SAL_WARN(
"sfx.doc",
"function not implemented" );
1115 SAL_WARN(
"sfx.doc",
"function not implemented" );
1122 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