1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <sot/storage.hxx>
23 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
24 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
25 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
26 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
27 #include <com/sun/star/ui/dialogs/XControlAccess.hpp>
28 #include <com/sun/star/util/CloseVetoException.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/beans/XPropertyAccess.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/beans/PropertyValue.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/document/XCmisDocument.hpp>
35 #include <com/sun/star/document/XExporter.hpp>
36 #include <com/sun/star/security/XCertificate.hpp>
37 #include <com/sun/star/task/ErrorCodeIOException.hpp>
38 #include <com/sun/star/task/InteractionHandler.hpp>
39 #include <com/sun/star/task/XStatusIndicator.hpp>
40 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
41 #include <comphelper/processfactory.hxx>
42 #include <comphelper/servicehelper.hxx>
43 #include <com/sun/star/security/CertificateValidity.hpp>
45 #include <com/sun/star/security/DocumentSignatureInformation.hpp>
46 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
47 #include <tools/diagnose_ex.h>
48 #include <tools/urlobj.hxx>
49 #include <svl/whiter.hxx>
50 #include <svl/intitem.hxx>
51 #include <svl/eitem.hxx>
52 #include <svl/visitem.hxx>
53 #include <vcl/weld.hxx>
54 #include <vcl/wrkwin.hxx>
55 #include <svtools/sfxecode.hxx>
56 #include <svtools/ehdl.hxx>
57 #include <sal/log.hxx>
59 #include <comphelper/string.hxx>
60 #include <basic/sbx.hxx>
61 #include <basic/sberrors.hxx>
62 #include <unotools/pathoptions.hxx>
63 #include <unotools/useroptions.hxx>
64 #include <unotools/saveopt.hxx>
65 #include <svtools/asynclink.hxx>
66 #include <svtools/DocumentToGraphicRenderer.hxx>
67 #include <vcl/gdimtf.hxx>
68 #include <comphelper/fileformat.h>
69 #include <comphelper/documentconstants.hxx>
70 #include <comphelper/propertyvalue.hxx>
71 #include <comphelper/storagehelper.hxx>
72 #include <tools/link.hxx>
74 #include <sfx2/asyncfunc.hxx>
75 #include <sfx2/app.hxx>
76 #include <sfx2/signaturestate.hxx>
77 #include <sfx2/sfxresid.hxx>
78 #include <sfx2/event.hxx>
79 #include <sfx2/request.hxx>
80 #include <sfx2/printer.hxx>
81 #include <sfx2/viewsh.hxx>
82 #include <sfx2/dinfdlg.hxx>
83 #include <sfx2/docfilt.hxx>
84 #include <sfx2/docfile.hxx>
85 #include <sfx2/dispatch.hxx>
86 #include <sfx2/objitem.hxx>
87 #include <sfx2/objsh.hxx>
88 #include <objshimp.hxx>
89 #include <sfxtypes.hxx>
90 #include <sfx2/module.hxx>
91 #include <sfx2/viewfrm.hxx>
92 #include <versdlg.hxx>
93 #include <sfx2/strings.hrc>
94 #include <sfx2/docfac.hxx>
95 #include <sfx2/fcontnr.hxx>
96 #include <sfx2/sfxhelp.hxx>
97 #include <sfx2/msgpool.hxx>
98 #include <sfx2/objface.hxx>
99 #include <sfx2/checkin.hxx>
100 #include <sfx2/infobar.hxx>
101 #include <sfx2/sfxuno.hxx>
102 #include <sfx2/sfxsids.hrc>
103 #include <SfxRedactionHelper.hxx>
105 #include <com/sun/star/document/XDocumentSubStorageSupplier.hpp>
106 #include <com/sun/star/embed/XTransactedObject.hpp>
107 #include <com/sun/star/util/XCloneable.hpp>
108 #include <com/sun/star/util/XCloseable.hpp>
109 #include <com/sun/star/document/XDocumentProperties.hpp>
110 #include <com/sun/star/text/XPageCursor.hpp>
111 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
113 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
114 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
115 #include <com/sun/star/frame/XDesktop2.hpp>
116 #include <com/sun/star/frame/Desktop.hpp>
117 #include <com/sun/star/frame/XLayoutManager.hpp>
119 #include <guisaveas.hxx>
120 #include <saveastemplatedlg.hxx>
122 #include <cppuhelper/implbase.hxx>
123 #include <unotools/ucbstreamhelper.hxx>
124 #include <unotools/streamwrap.hxx>
126 #include <svx/unoshape.hxx>
127 #include <svx/xlineit0.hxx>
128 #include <com/sun/star/util/Color.hpp>
130 #include <autoredactdialog.hxx>
132 using namespace ::com::sun::star
;
133 using namespace ::com::sun::star::lang
;
134 using namespace ::com::sun::star::uno
;
135 using namespace ::com::sun::star::ui::dialogs
;
136 using namespace ::com::sun::star::awt
;
137 using namespace ::com::sun::star::container
;
138 using namespace ::com::sun::star::beans
;
139 using namespace ::com::sun::star::document
;
140 using namespace ::com::sun::star::security
;
141 using namespace ::com::sun::star::task
;
142 using namespace ::com::sun::star::graphic
;
144 #define ShellClass_SfxObjectShell
145 #include <sfxslots.hxx>
147 SFX_IMPL_SUPERCLASS_INTERFACE(SfxObjectShell
, SfxShell
)
149 void SfxObjectShell::InitInterface_Impl()
153 class SfxClosePreventer_Impl
: public ::cppu::WeakImplHelper
< css::util::XCloseListener
>
155 bool m_bGotOwnership
;
156 bool m_bPreventClose
;
159 SfxClosePreventer_Impl();
161 bool HasOwnership() const { return m_bGotOwnership
; }
163 void SetPreventClose( bool bPrevent
) { m_bPreventClose
= bPrevent
; }
165 virtual void SAL_CALL
queryClosing( const lang::EventObject
& aEvent
, sal_Bool bDeliverOwnership
) override
;
167 virtual void SAL_CALL
notifyClosing( const lang::EventObject
& aEvent
) override
;
169 virtual void SAL_CALL
disposing( const lang::EventObject
& aEvent
) override
;
173 SfxClosePreventer_Impl::SfxClosePreventer_Impl()
174 : m_bGotOwnership( false )
175 , m_bPreventClose( true )
179 void SAL_CALL
SfxClosePreventer_Impl::queryClosing( const lang::EventObject
&, sal_Bool bDeliverOwnership
)
181 if ( m_bPreventClose
)
183 if ( !m_bGotOwnership
)
184 m_bGotOwnership
= bDeliverOwnership
;
186 throw util::CloseVetoException();
190 void SAL_CALL
SfxClosePreventer_Impl::notifyClosing( const lang::EventObject
& )
193 void SAL_CALL
SfxClosePreventer_Impl::disposing( const lang::EventObject
& )
197 class SfxInstanceCloseGuard_Impl
199 rtl::Reference
<SfxClosePreventer_Impl
> m_xPreventer
;
200 uno::Reference
< util::XCloseable
> m_xCloseable
;
203 SfxInstanceCloseGuard_Impl() {}
205 ~SfxInstanceCloseGuard_Impl();
207 bool Init_Impl( const uno::Reference
< util::XCloseable
>& xCloseable
);
210 bool SfxInstanceCloseGuard_Impl::Init_Impl( const uno::Reference
< util::XCloseable
>& xCloseable
)
212 bool bResult
= false;
214 // do not allow reinit after the successful init
215 if ( xCloseable
.is() && !m_xCloseable
.is() )
219 m_xPreventer
= new SfxClosePreventer_Impl();
220 xCloseable
->addCloseListener( m_xPreventer
.get() );
221 m_xCloseable
= xCloseable
;
224 catch( uno::Exception
& )
226 OSL_FAIL( "Could not register close listener!" );
233 SfxInstanceCloseGuard_Impl::~SfxInstanceCloseGuard_Impl()
235 if ( !m_xCloseable
.is() || !m_xPreventer
.is() )
240 m_xCloseable
->removeCloseListener( m_xPreventer
.get() );
242 catch( uno::Exception
& )
248 if ( m_xPreventer
.is() )
250 m_xPreventer
->SetPreventClose( false );
252 if ( m_xPreventer
->HasOwnership() )
253 m_xCloseable
->close( true ); // TODO: do it asynchronously
256 catch( uno::Exception
& )
262 void SfxObjectShell::PrintExec_Impl(SfxRequest
&rReq
)
264 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst(this);
267 rReq
.SetSlot( SID_PRINTDOC
);
268 pFrame
->GetViewShell()->ExecuteSlot(rReq
);
273 void SfxObjectShell::PrintState_Impl(SfxItemSet
&rSet
)
275 bool bPrinting
= false;
276 SfxViewFrame
* pFrame
= SfxViewFrame::GetFirst( this );
279 SfxPrinter
*pPrinter
= pFrame
->GetViewShell()->GetPrinter();
280 bPrinting
= pPrinter
&& pPrinter
->IsPrinting();
282 rSet
.Put( SfxBoolItem( SID_PRINTOUT
, bPrinting
) );
285 bool SfxObjectShell::APISaveAs_Impl(const OUString
& aFileName
, SfxItemSet
& rItemSet
)
292 OUString aFilterName
;
293 const SfxStringItem
* pFilterNameItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_FILTER_NAME
, false);
294 if( pFilterNameItem
)
296 aFilterName
= pFilterNameItem
->GetValue();
300 const SfxStringItem
* pContentTypeItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_CONTENTTYPE
, false);
301 if ( pContentTypeItem
)
303 std::shared_ptr
<const SfxFilter
> pFilter
= SfxFilterMatcher( GetFactory().GetFactoryName() ).GetFilter4Mime( pContentTypeItem
->GetValue(), SfxFilterFlags::EXPORT
);
305 aFilterName
= pFilter
->GetName();
309 // in case no filter defined use default one
310 if( aFilterName
.isEmpty() )
312 std::shared_ptr
<const SfxFilter
> pFilt
= SfxFilter::GetDefaultFilterFromFactory(GetFactory().GetFactoryName());
314 DBG_ASSERT( pFilt
, "No default filter!\n" );
316 aFilterName
= pFilt
->GetFilterName();
318 rItemSet
.Put(SfxStringItem(SID_FILTER_NAME
, aFilterName
));
323 SfxObjectShellRef
xLock( this ); // ???
325 // use the title that is provided in the media descriptor
326 const SfxStringItem
* pDocTitleItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_DOCINFO_TITLE
, false);
328 getDocProperties()->setTitle( pDocTitleItem
->GetValue() );
330 bOk
= CommonSaveAs_Impl(INetURLObject(aFileName
), aFilterName
, rItemSet
);
337 void SfxObjectShell::CheckOut( )
341 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
342 xCmisDoc
->checkOut( );
344 // Remove the info bar
345 SfxViewFrame
* pViewFrame
= GetFrame();
346 pViewFrame
->RemoveInfoBar( "checkout" );
348 catch ( const uno::RuntimeException
& e
)
350 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
351 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
356 void SfxObjectShell::CancelCheckOut( )
360 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
361 xCmisDoc
->cancelCheckOut( );
363 uno::Reference
< util::XModifiable
> xModifiable( GetModel( ), uno::UNO_QUERY
);
364 if ( xModifiable
.is( ) )
365 xModifiable
->setModified( false );
367 catch ( const uno::RuntimeException
& e
)
369 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
370 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
375 void SfxObjectShell::CheckIn( )
379 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
380 // Pop up dialog to ask for comment and major
381 SfxCheckinDialog
checkinDlg(GetFrame()->GetWindow().GetFrameWeld());
382 if (checkinDlg
.run() == RET_OK
)
384 xCmisDoc
->checkIn(checkinDlg
.IsMajor(), checkinDlg
.GetComment());
385 uno::Reference
< util::XModifiable
> xModifiable( GetModel( ), uno::UNO_QUERY
);
386 if ( xModifiable
.is( ) )
387 xModifiable
->setModified( false );
390 catch ( const uno::RuntimeException
& e
)
392 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
393 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
398 uno::Sequence
< document::CmisVersion
> SfxObjectShell::GetCmisVersions( ) const
402 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
403 return xCmisDoc
->getAllVersions( );
405 catch ( const uno::RuntimeException
& e
)
407 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
408 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
411 return uno::Sequence
< document::CmisVersion
> ( );
414 void SfxObjectShell::ExecFile_Impl(SfxRequest
&rReq
)
416 weld::Window
* pDialogParent
= rReq
.GetFrameWeld();
419 SfxViewFrame
* pFrame
= GetFrame();
421 pFrame
= SfxViewFrame::GetFirst(this);
423 pDialogParent
= pFrame
->GetWindow().GetFrameWeld();
426 sal_uInt16 nId
= rReq
.GetSlot();
428 if( SID_SIGNATURE
== nId
|| SID_MACRO_SIGNATURE
== nId
)
430 if ( QueryHiddenInformation( HiddenWarningFact::WhenSigning
, nullptr ) == RET_YES
)
431 ( SID_SIGNATURE
== nId
) ? SignDocumentContent(pDialogParent
) : SignScriptingContent(pDialogParent
);
435 if ( !GetMedium() && nId
!= SID_CLOSEDOC
)
441 // this guard is created here to have it destruction at the end of the method
442 SfxInstanceCloseGuard_Impl aModelGuard
;
444 bool bIsPDFExport
= false;
445 bool bIsAutoRedact
= false;
446 std::vector
<std::pair
<RedactionTarget
*, OUString
>> aRedactionTargets
;
451 SfxViewFrame
* pFrame
= GetFrame();
453 pFrame
= SfxViewFrame::GetFirst( this );
457 if ( !IsOwnStorageFormat( *GetMedium() ) )
460 SfxVersionDialog
aDlg(pDialogParent
, pFrame
, IsSaveVersionOnClose());
462 SetSaveVersionOnClose(aDlg
.IsSaveVersionOnClose());
467 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
470 const SfxDocumentInfoItem
* pDocInfItem
= rReq
.GetArg
<SfxDocumentInfoItem
>(SID_DOCINFO
);
473 // parameter, e.g. from replayed macro
474 pDocInfItem
->UpdateDocumentInfo(getDocProperties(), true);
475 SetUseUserData( pDocInfItem
->IsUseUserData() );
476 SetUseThumbnailSave( pDocInfItem
->IsUseThumbnailSave() );
480 // no argument containing DocInfo; check optional arguments
481 bool bReadOnly
= IsReadOnly();
482 const SfxBoolItem
* pROItem
= rReq
.GetArg
<SfxBoolItem
>(SID_DOC_READONLY
);
484 // override readonly attribute of document
485 // e.g. if a readonly document is saved elsewhere and user asks for editing DocInfo before
486 bReadOnly
= pROItem
->GetValue();
489 const OUString
aURL( HasName() ? GetMedium()->GetName() : GetFactory().GetFactoryURL() );
491 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
492 uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties();
494 SfxDocumentInfoItem
aDocInfoItem( aURL
, getDocProperties(), aCmisProperties
,
495 IsUseUserData(), IsUseThumbnailSave() );
496 if ( !GetSlotState( SID_DOCTEMPLATE
) )
497 // templates not supported
498 aDocInfoItem
.SetTemplate(false);
500 SfxItemSet
aSet(GetPool(), svl::Items
<SID_DOCINFO
, SID_DOCINFO
, SID_DOC_READONLY
, SID_DOC_READONLY
,
501 SID_EXPLORER_PROPS_START
, SID_EXPLORER_PROPS_START
, SID_BASEURL
, SID_BASEURL
>{} );
502 aSet
.Put( aDocInfoItem
);
503 aSet
.Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
504 aSet
.Put( SfxStringItem( SID_EXPLORER_PROPS_START
, GetTitle() ) );
505 aSet
.Put( SfxStringItem( SID_BASEURL
, GetMedium()->GetBaseURL() ) );
507 // creating dialog is done via virtual method; application will
508 // add its own statistics page
509 std::shared_ptr
<SfxRequest
> pReq
= std::make_shared
<SfxRequest
>(rReq
);
510 std::shared_ptr
<SfxDocumentInfoDialog
> xDlg(CreateDocumentInfoDialog(rReq
.GetFrameWeld(), aSet
));
511 SfxTabDialogController::runAsync(xDlg
, [this, xDlg
, xCmisDoc
, pReq
](sal_Int32 nResult
)
513 if (RET_OK
== nResult
)
515 const SfxDocumentInfoItem
* pDocInfoItem
= SfxItemSet::GetItem
<SfxDocumentInfoItem
>(xDlg
->GetOutputItemSet(), SID_DOCINFO
, false);
518 // user has done some changes to DocumentInfo
519 pDocInfoItem
->UpdateDocumentInfo(getDocProperties());
520 const uno::Sequence
< document::CmisProperty
>& aNewCmisProperties
=
521 pDocInfoItem
->GetCmisProperties( );
522 if ( aNewCmisProperties
.hasElements( ) )
523 xCmisDoc
->updateCmisProperties( aNewCmisProperties
);
524 SetUseUserData( pDocInfoItem
->IsUseUserData() );
525 SetUseThumbnailSave( pDocInfoItem
-> IsUseThumbnailSave() );
526 // add data from dialog for possible recording purpose
527 pReq
->AppendItem( SfxDocumentInfoItem( GetTitle(),
528 getDocProperties(), aNewCmisProperties
, IsUseUserData(), IsUseThumbnailSave() ) );
531 css::uno::Reference
< css::uno::XInterface
> xInterface
;
532 const SfxUnoAnyItem
* pUnoAny
= pReq
->GetArg
<SfxUnoAnyItem
>(FN_PARAM_2
);
533 AsyncFunc
* pAsyncFunc
= pUnoAny
&& (pUnoAny
->GetValue() >>= xInterface
) ?
534 comphelper::getUnoTunnelImplementation
<AsyncFunc
>(xInterface
) : nullptr;
536 pAsyncFunc
->Execute();
541 // nothing done; no recording
551 case SID_AUTOREDACTDOC
:
553 SfxAutoRedactDialog
aDlg(pDialogParent
);
554 sal_Int16 nResult
= aDlg
.run();
556 if (nResult
!= RET_OK
|| !aDlg
.hasTargets() || !aDlg
.isValidState())
562 // else continue with normal redaction
563 bIsAutoRedact
= true;
564 aDlg
.getTargets(aRedactionTargets
);
571 css::uno::Reference
<css::frame::XModel
> xModel
= GetModel();
575 uno::Reference
< lang::XComponent
> xSourceDoc( xModel
);
577 DocumentToGraphicRenderer
aRenderer(xSourceDoc
, false);
579 // Get the page margins of the original doc
580 PageMargins aPageMargins
= {-1, -1, -1, -1};
581 if (aRenderer
.isWriter())
582 aPageMargins
= SfxRedactionHelper::getPageMarginsForWriter(xModel
);
583 else if (aRenderer
.isCalc())
584 aPageMargins
= SfxRedactionHelper::getPageMarginsForCalc(xModel
);
586 sal_Int32 nPages
= aRenderer
.getPageCount();
587 std::vector
< GDIMetaFile
> aMetaFiles
;
588 std::vector
< ::Size
> aPageSizes
;
590 // Convert the pages of the document to gdimetafiles
591 SfxRedactionHelper::getPageMetaFilesFromDoc(aMetaFiles
, aPageSizes
, nPages
, aRenderer
);
593 // Create an empty Draw component.
594 uno::Reference
<frame::XDesktop2
> xDesktop
= css::frame::Desktop::create(comphelper::getProcessComponentContext());
595 uno::Reference
<lang::XComponent
> xComponent
= xDesktop
->loadComponentFromURL("private:factory/sdraw", "_default", 0, {});
597 // Add the doc pages to the new draw document
598 SfxRedactionHelper::addPagesToDraw(xComponent
, nPages
, aMetaFiles
, aPageSizes
, aPageMargins
, aRedactionTargets
, bIsAutoRedact
);
600 // Show the Redaction toolbar
601 SfxViewFrame
* pViewFrame
= SfxViewFrame::Current();
604 SfxRedactionHelper::showRedactionToolbar(pViewFrame
);
609 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
610 case SID_DIRECTEXPORTDOCASPDF
:
612 uno::Reference
< lang::XComponent
> xComponent( GetCurrentComponent(), uno::UNO_QUERY
);
613 if (!xComponent
.is())
616 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xComponent
, uno::UNO_QUERY
);
618 // Redaction finalization takes place in Draw
619 if ( xServiceInfo
.is() && xServiceInfo
->supportsService("com.sun.star.drawing.DrawingDocument")
620 && SfxRedactionHelper::isRedactMode(rReq
) )
622 OUString
sRedactionStyle(SfxRedactionHelper::getStringParam(rReq
, SID_REDACTION_STYLE
));
624 // Access the draw pages
625 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(xComponent
, uno::UNO_QUERY
);
626 uno::Reference
<drawing::XDrawPages
> xDrawPages
= xDrawPagesSupplier
->getDrawPages();
628 sal_Int32 nPageCount
= xDrawPages
->getCount();
629 for (sal_Int32 nPageNum
= 0; nPageNum
< nPageCount
; ++nPageNum
)
632 uno::Reference
< drawing::XDrawPage
> xPage( xDrawPages
->getByIndex( nPageNum
), uno::UNO_QUERY
);
637 // Go through all shapes
638 sal_Int32 nShapeCount
= xPage
->getCount();
639 for (sal_Int32 nShapeNum
= 0; nShapeNum
< nShapeCount
; ++nShapeNum
)
641 uno::Reference
< drawing::XShape
> xCurrShape(xPage
->getByIndex(nShapeNum
), uno::UNO_QUERY
);
642 if (!xCurrShape
.is())
645 uno::Reference
< beans::XPropertySet
> xPropSet(xCurrShape
, uno::UNO_QUERY
);
649 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
654 if (xInfo
->hasPropertyByName("Name"))
656 uno::Any aAnyShapeName
= xPropSet
->getPropertyValue("Name");
657 aAnyShapeName
>>= sShapeName
;
662 // Rectangle redaction
663 if (sShapeName
== "RectangleRedactionShape"
664 && xInfo
->hasPropertyByName("FillTransparence") && xInfo
->hasPropertyByName("FillColor"))
666 xPropSet
->setPropertyValue("FillTransparence", css::uno::makeAny(static_cast<sal_Int16
>(0)));
667 if (sRedactionStyle
== "White")
669 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_WHITE
));
670 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_SOLID
));
671 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_BLACK
));
675 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_BLACK
));
676 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_NONE
));
679 // Freeform redaction
680 else if (sShapeName
== "FreeformRedactionShape"
681 && xInfo
->hasPropertyByName("LineTransparence") && xInfo
->hasPropertyByName("LineColor"))
683 xPropSet
->setPropertyValue("LineTransparence", css::uno::makeAny(static_cast<sal_Int16
>(0)));
685 if (sRedactionStyle
== "White")
687 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_WHITE
));
691 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_BLACK
));
699 case SID_EXPORTDOCASPDF
:
702 case SID_EXPORTDOCASEPUB
:
703 case SID_DIRECTEXPORTDOCASEPUB
:
706 case SID_SAVEASREMOTE
:
709 // derived class may decide to abort this
710 if( !QuerySlotExecutable( nId
) )
712 rReq
.SetReturnValue( SfxBoolItem( 0, false ) );
716 //!! detailed analysis of an error code
717 SfxObjectShellRef
xLock( this );
719 // the model can not be closed till the end of this method
720 // if somebody tries to close it during this time the model will be closed
721 // at the end of the method
722 aModelGuard
.Init_Impl( uno::Reference
< util::XCloseable
>( GetModel(), uno::UNO_QUERY
) );
724 ErrCode nErrorCode
= ERRCODE_NONE
;
726 // by default versions should be preserved always except in case of an explicit
727 // SaveAs via GUI, so the flag must be set accordingly
728 pImpl
->bPreserveVersions
= (nId
== SID_SAVEDOC
);
731 SfxErrorContext
aEc( ERRCTX_SFX_SAVEASDOC
, GetTitle() ); // ???
733 if ( nId
== SID_SAVEASDOC
|| nId
== SID_SAVEASREMOTE
)
735 // in case of plugin mode the SaveAs operation means SaveTo
736 const SfxBoolItem
* pViewOnlyItem
= SfxItemSet::GetItem
<SfxBoolItem
>(GetMedium()->GetItemSet(), SID_VIEWONLY
, false);
737 if ( pViewOnlyItem
&& pViewOnlyItem
->GetValue() )
738 rReq
.AppendItem( SfxBoolItem( SID_SAVETO
, true ) );
741 // TODO/LATER: do the following GUI related actions in standalone method
743 // Introduce a status indicator for GUI operation
744 const SfxUnoAnyItem
* pStatusIndicatorItem
= rReq
.GetArg
<SfxUnoAnyItem
>(SID_PROGRESS_STATUSBAR_CONTROL
);
745 if ( !pStatusIndicatorItem
)
747 // get statusindicator
748 uno::Reference
< task::XStatusIndicator
> xStatusIndicator
;
749 uno::Reference
< frame::XController
> xCtrl( GetModel()->getCurrentController() );
752 uno::Reference
< task::XStatusIndicatorFactory
> xStatFactory( xCtrl
->getFrame(), uno::UNO_QUERY
);
753 if( xStatFactory
.is() )
754 xStatusIndicator
= xStatFactory
->createStatusIndicator();
757 OSL_ENSURE( xStatusIndicator
.is(), "Can not retrieve default status indicator!" );
759 if ( xStatusIndicator
.is() )
761 SfxUnoAnyItem
aStatIndItem( SID_PROGRESS_STATUSBAR_CONTROL
, uno::makeAny( xStatusIndicator
) );
763 if ( nId
== SID_SAVEDOC
)
765 // in case of saving it is not possible to transport the parameters from here
766 // but it is not clear here whether the saving will be done or saveAs operation
767 GetMedium()->GetItemSet()->Put( aStatIndItem
);
770 rReq
.AppendItem( aStatIndItem
);
773 else if ( nId
== SID_SAVEDOC
)
775 // in case of saving it is not possible to transport the parameters from here
776 // but it is not clear here whether the saving will be done or saveAs operation
777 GetMedium()->GetItemSet()->Put( *pStatusIndicatorItem
);
780 // Introduce an interaction handler for GUI operation
781 const SfxUnoAnyItem
* pInteractionHandlerItem
= rReq
.GetArg
<SfxUnoAnyItem
>(SID_INTERACTIONHANDLER
);
782 if ( !pInteractionHandlerItem
)
784 uno::Reference
<css::awt::XWindow
> xParentWindow
;
785 uno::Reference
<frame::XController
> xCtrl(GetModel()->getCurrentController());
787 xParentWindow
= xCtrl
->getFrame()->getContainerWindow();
789 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
791 uno::Reference
< task::XInteractionHandler2
> xInteract(
792 task::InteractionHandler::createWithParent(xContext
, xParentWindow
) );
794 SfxUnoAnyItem
aInteractionItem( SID_INTERACTIONHANDLER
, uno::makeAny( xInteract
) );
795 if ( nId
== SID_SAVEDOC
)
797 // in case of saving it is not possible to transport the parameters from here
798 // but it is not clear here whether the saving will be done or saveAs operation
799 GetMedium()->GetItemSet()->Put( aInteractionItem
);
802 rReq
.AppendItem( aInteractionItem
);
804 else if ( nId
== SID_SAVEDOC
)
806 // in case of saving it is not possible to transport the parameters from here
807 // but it is not clear here whether the saving will be done or saveAs operation
808 GetMedium()->GetItemSet()->Put( *pInteractionHandlerItem
);
812 bool bPreselectPassword
= false;
813 const SfxUnoAnyItem
* pOldEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(GetMedium()->GetItemSet(), SID_ENCRYPTIONDATA
, false);
814 const SfxStringItem
* pOldPasswordItem
= SfxItemSet::GetItem
<SfxStringItem
>(GetMedium()->GetItemSet(), SID_PASSWORD
, false);
815 if ( pOldEncryptionDataItem
|| pOldPasswordItem
)
816 bPreselectPassword
= true;
818 uno::Sequence
< beans::PropertyValue
> aDispatchArgs
;
819 if ( rReq
.GetArgs() )
824 bool bForceSaveAs
= nId
== SID_SAVEDOC
&& IsReadOnlyMedium();
825 const SfxSlot
* pSlot
= GetModule()->GetSlotPool()->GetSlot( bForceSaveAs
? SID_SAVEASDOC
: nId
);
827 throw uno::Exception("no slot", nullptr);
829 SfxStoringHelper aHelper
;
831 if ( QueryHiddenInformation( bIsPDFExport
? HiddenWarningFact::WhenCreatingPDF
: HiddenWarningFact::WhenSaving
, nullptr ) != RET_YES
)
833 // the user has decided not to store the document
834 throw task::ErrorCodeIOException(
835 "SfxObjectShell::ExecFile_Impl: ERRCODE_IO_ABORT",
836 uno::Reference
< uno::XInterface
>(), sal_uInt32(ERRCODE_IO_ABORT
));
839 aHelper
.GUIStoreModel( GetModel(),
840 OUString::createFromAscii( pSlot
->GetUnoName() ),
843 GetDocumentSignatureState() );
846 // merge aDispatchArgs to the request
847 SfxAllItemSet
aResultParams( GetPool() );
848 TransformParameters( nId
,
851 rReq
.SetArgs( aResultParams
);
853 // the StoreAsURL/StoreToURL method have called this method with false
854 // so it has to be restored to true here since it is a call from GUI
855 GetMedium()->SetUpdatePickList( true );
857 // TODO: in future it must be done in following way
858 // if document is opened from GUI, it immediately appears in the picklist
859 // if the document is a new one then it appears in the picklist immediately
860 // after SaveAs operation triggered from GUI
862 catch( const task::ErrorCodeIOException
& aErrorEx
)
864 TOOLS_WARN_EXCEPTION( "sfx.doc", "Fatal IO error during save");
865 nErrorCode
= ErrCode(aErrorEx
.ErrCode
);
869 nErrorCode
= ERRCODE_IO_GENERAL
;
872 // by default versions should be preserved always except in case of an explicit
873 // SaveAs via GUI, so the flag must be reset to guarantee this
874 pImpl
->bPreserveVersions
= true;
875 ErrCode lErr
=GetErrorCode();
877 if ( !lErr
&& nErrorCode
)
880 if ( lErr
&& nErrorCode
== ERRCODE_NONE
)
882 const SfxBoolItem
* pWarnItem
= rReq
.GetArg
<SfxBoolItem
>(SID_FAIL_ON_WARNING
);
883 if ( pWarnItem
&& pWarnItem
->GetValue() )
887 // may be nErrorCode should be shown in future
888 if ( lErr
!= ERRCODE_IO_ABORT
)
890 SfxErrorContext
aEc(ERRCTX_SFX_SAVEASDOC
,GetTitle());
891 ErrorHandler::HandleError(lErr
, pDialogParent
);
894 if (nId
== SID_DIRECTEXPORTDOCASPDF
&&
895 SfxRedactionHelper::isRedactMode(rReq
))
897 // Return the finalized redaction shapes back to normal (gray & transparent)
898 uno::Reference
< lang::XComponent
> xComponent( GetCurrentComponent(), uno::UNO_QUERY
);
899 if (!xComponent
.is())
902 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xComponent
, uno::UNO_QUERY
);
904 // Redaction finalization takes place in Draw
905 if ( xServiceInfo
.is() && xServiceInfo
->supportsService("com.sun.star.drawing.DrawingDocument") )
907 // Access the draw pages
908 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(xComponent
, uno::UNO_QUERY
);
909 uno::Reference
<drawing::XDrawPages
> xDrawPages
= xDrawPagesSupplier
->getDrawPages();
911 sal_Int32 nPageCount
= xDrawPages
->getCount();
912 for (sal_Int32 nPageNum
= 0; nPageNum
< nPageCount
; ++nPageNum
)
915 uno::Reference
< drawing::XDrawPage
> xPage( xDrawPages
->getByIndex( nPageNum
), uno::UNO_QUERY
);
920 // Go through all shapes
921 sal_Int32 nShapeCount
= xPage
->getCount();
922 for (sal_Int32 nShapeNum
= 0; nShapeNum
< nShapeCount
; ++nShapeNum
)
924 uno::Reference
< drawing::XShape
> xCurrShape(xPage
->getByIndex(nShapeNum
), uno::UNO_QUERY
);
925 if (!xCurrShape
.is())
928 uno::Reference
< beans::XPropertySet
> xPropSet(xCurrShape
, uno::UNO_QUERY
);
932 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
936 // Not a shape we converted?
937 if (!xInfo
->hasPropertyByName("Name"))
941 if (xInfo
->hasPropertyByName("Name"))
943 uno::Any aAnyShapeName
= xPropSet
->getPropertyValue("Name");
944 aAnyShapeName
>>= sShapeName
;
949 // Rectangle redaction
950 if (sShapeName
== "RectangleRedactionShape"
951 && xInfo
->hasPropertyByName("FillTransparence") && xInfo
->hasPropertyByName("FillColor"))
953 xPropSet
->setPropertyValue("FillTransparence", css::uno::makeAny(static_cast<sal_Int16
>(50)));
954 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_GRAY7
));
955 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_NONE
));
958 // Freeform redaction
959 else if (sShapeName
== "FreeformRedactionShape")
961 xPropSet
->setPropertyValue("LineTransparence", css::uno::makeAny(static_cast<sal_Int16
>(50)));
962 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_GRAY7
));
971 if ( nId
== SID_EXPORTDOCASPDF
)
973 // This function is used by the SendMail function that needs information if an export
974 // file was written or not. This could be due to cancellation of the export
975 // or due to an error. So IO abort must be handled like an error!
976 nErrorCode
= ( lErr
!= ERRCODE_IO_ABORT
) && ( nErrorCode
== ERRCODE_NONE
) ? nErrorCode
: lErr
;
979 if ( ( nId
== SID_SAVEASDOC
|| nId
== SID_SAVEASREMOTE
) && nErrorCode
== ERRCODE_NONE
)
981 const SfxBoolItem
* saveTo
= rReq
.GetArg
<SfxBoolItem
>(SID_SAVETO
);
982 if (saveTo
== nullptr || !saveTo
->GetValue())
984 SfxViewFrame
*pFrame
= GetFrame();
986 pFrame
->RemoveInfoBar("readonly");
987 SetReadOnlyUI(false);
991 rReq
.SetReturnValue( SfxBoolItem(0, nErrorCode
== ERRCODE_NONE
) );
1001 SfxAllItemSet
aArgs( GetPool() );
1002 aArgs
.Put( SfxBoolItem( SID_SAVEACOPYITEM
, true ) );
1003 SfxRequest
aSaveACopyReq( SID_EXPORTDOC
, SfxCallMode::API
, aArgs
);
1004 ExecFile_Impl( aSaveACopyReq
);
1005 if ( !aSaveACopyReq
.IsDone() )
1013 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1017 // Evaluate Parameter
1018 const SfxBoolItem
* pSaveItem
= rReq
.GetArg
<SfxBoolItem
>(SID_CLOSEDOC_SAVE
);
1019 const SfxStringItem
* pNameItem
= rReq
.GetArg
<SfxStringItem
>(SID_CLOSEDOC_FILENAME
);
1022 if ( pSaveItem
->GetValue() )
1026 #if HAVE_FEATURE_SCRIPTING
1027 SbxBase::SetError( ERRCODE_BASIC_WRONG_ARGS
);
1032 SfxAllItemSet
aArgs( GetPool() );
1033 SfxStringItem
aTmpItem( SID_FILE_NAME
, pNameItem
->GetValue() );
1034 aArgs
.Put( aTmpItem
, aTmpItem
.Which() );
1035 SfxRequest
aSaveAsReq( SID_SAVEASDOC
, SfxCallMode::API
, aArgs
);
1036 ExecFile_Impl( aSaveAsReq
);
1037 if ( !aSaveAsReq
.IsDone() )
1047 // Cancelled by the user?
1048 if (!PrepareClose())
1050 rReq
.SetReturnValue( SfxBoolItem(0, false) );
1055 SetModified( false );
1056 ErrCode lErr
= GetErrorCode();
1057 ErrorHandler::HandleError(lErr
, pDialogParent
);
1059 rReq
.SetReturnValue( SfxBoolItem(0, true) );
1061 rReq
.ReleaseArgs(); // because the pool is destroyed in Close
1066 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1067 case SID_DOCTEMPLATE
:
1069 // save as document templates
1070 SfxSaveAsTemplateDialog
aDlg(pDialogParent
, GetModel());
1080 case SID_CANCELCHECKOUT
:
1082 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(nullptr,
1083 VclMessageType::Question
, VclButtonsType::YesNo
, SfxResId(STR_QUERY_CANCELCHECKOUT
)));
1084 if (xBox
->run() == RET_YES
)
1088 // Reload the document as we may still have local changes
1089 SfxViewFrame
*pFrame
= GetFrame();
1091 pFrame
->GetDispatcher()->Execute(SID_RELOAD
);
1102 // Prevent entry in the Pick-lists
1104 GetMedium()->SetUpdatePickList( false );
1106 // Ignore()-branches have already returned
1111 void SfxObjectShell::GetState_Impl(SfxItemSet
&rSet
)
1113 SfxWhichIter
aIter( rSet
);
1115 for ( sal_uInt16 nWhich
= aIter
.FirstWhich(); nWhich
; nWhich
= aIter
.NextWhich() )
1119 case SID_DOCTEMPLATE
:
1121 if ( !GetFactory().GetTemplateFilter() )
1122 rSet
.DisableItem( nWhich
);
1129 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
1130 const uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties();
1132 if ( xCmisDoc
->isVersionable( ) && aCmisProperties
.hasElements( ) )
1134 // Loop over the CMIS Properties to find cmis:isVersionSeriesCheckedOut
1135 bool bIsGoogleFile
= false;
1136 bool bCheckedOut
= false;
1137 for ( const auto& rCmisProperty
: aCmisProperties
)
1139 if ( rCmisProperty
.Id
== "cmis:isVersionSeriesCheckedOut" )
1141 uno::Sequence
< sal_Bool
> bTmp
;
1142 rCmisProperty
.Value
>>= bTmp
;
1143 bCheckedOut
= bTmp
[0];
1145 // using title to know if it's a Google Drive file
1146 // maybe there's a safer way.
1147 if ( rCmisProperty
.Name
== "title" )
1148 bIsGoogleFile
= true;
1150 bShow
= !bCheckedOut
&& !bIsGoogleFile
;
1155 rSet
.DisableItem( nWhich
);
1156 rSet
.Put( SfxVisibilityItem( nWhich
, false ) );
1161 case SID_CANCELCHECKOUT
:
1165 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
1166 uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties( );
1168 if ( xCmisDoc
->isVersionable( ) && aCmisProperties
.hasElements( ) )
1170 // Loop over the CMIS Properties to find cmis:isVersionSeriesCheckedOut
1171 bool bCheckedOut
= false;
1172 auto pProp
= std::find_if(aCmisProperties
.begin(), aCmisProperties
.end(),
1173 [](const document::CmisProperty
& rProp
) { return rProp
.Id
== "cmis:isVersionSeriesCheckedOut"; });
1174 if (pProp
!= aCmisProperties
.end())
1176 uno::Sequence
< sal_Bool
> bTmp
;
1177 pProp
->Value
>>= bTmp
;
1178 bCheckedOut
= bTmp
[0];
1180 bShow
= bCheckedOut
;
1185 rSet
.DisableItem( nWhich
);
1186 rSet
.Put( SfxVisibilityItem( nWhich
, false ) );
1193 SfxObjectShell
*pDoc
= this;
1194 SfxViewFrame
* pFrame
= GetFrame();
1196 pFrame
= SfxViewFrame::GetFirst( this );
1198 if ( !pFrame
|| !pDoc
->HasName() ||
1199 !IsOwnStorageFormat( *pDoc
->GetMedium() ) )
1200 rSet
.DisableItem( nWhich
);
1205 if ( !IsReadOnly() )
1206 rSet
.Put(SfxStringItem(
1207 nWhich
, SfxResId(STR_SAVEDOC
)));
1209 rSet
.DisableItem(nWhich
);
1218 rSet
.Put(SfxStringItem(nWhich
, SfxResId(STR_CLOSEDOC
)));
1224 if( !( pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
) )
1226 rSet
.DisableItem( nWhich
);
1229 if ( /*!pCombinedFilters ||*/ !GetMedium() )
1230 rSet
.DisableItem( nWhich
);
1232 rSet
.Put( SfxStringItem( nWhich
, SfxResId(STR_SAVEASDOC
) ) );
1238 if( !( pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
) )
1240 rSet
.DisableItem( nWhich
);
1243 if ( /*!pCombinedFilters ||*/ !GetMedium() )
1244 rSet
.DisableItem( nWhich
);
1246 rSet
.Put( SfxStringItem( nWhich
, SfxResId(STR_SAVEACOPY
) ) );
1250 case SID_EXPORTDOCASPDF
:
1251 case SID_DIRECTEXPORTDOCASPDF
:
1252 case SID_EXPORTDOCASEPUB
:
1253 case SID_DIRECTEXPORTDOCASEPUB
:
1255 case SID_AUTOREDACTDOC
:
1260 case SID_DOC_MODIFIED
:
1262 rSet
.Put( SfxBoolItem( SID_DOC_MODIFIED
, IsModified() ) );
1268 rSet
.Put( SfxBoolItem( SID_MODIFIED
, IsModified() ) );
1272 case SID_DOCINFO_TITLE
:
1274 rSet
.Put( SfxStringItem(
1275 SID_DOCINFO_TITLE
, getDocProperties()->getTitle() ) );
1280 if( GetMedium() && HasName() )
1281 rSet
.Put( SfxStringItem(
1282 SID_FILE_NAME
, GetMedium()->GetName() ) );
1287 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst(this);
1290 SignatureState eState
= GetDocumentSignatureState();
1291 InfoBarType
aInfoBarType(InfoBarType::Info
);
1292 OUString
sMessage("");
1296 case SignatureState::BROKEN
:
1297 sMessage
= SfxResId(STR_SIGNATURE_BROKEN
);
1298 aInfoBarType
= InfoBarType::Danger
;
1300 case SignatureState::INVALID
:
1301 sMessage
= SfxResId(STR_SIGNATURE_INVALID
);
1302 // Warning only, I've tried Danger and it looked too scary
1303 aInfoBarType
= InfoBarType::Warning
;
1305 case SignatureState::NOTVALIDATED
:
1306 sMessage
= SfxResId(STR_SIGNATURE_NOTVALIDATED
);
1307 aInfoBarType
= InfoBarType::Warning
;
1309 case SignatureState::PARTIAL_OK
:
1310 sMessage
= SfxResId(STR_SIGNATURE_PARTIAL_OK
);
1311 aInfoBarType
= InfoBarType::Warning
;
1313 case SignatureState::OK
:
1314 sMessage
= SfxResId(STR_SIGNATURE_OK
);
1315 aInfoBarType
= InfoBarType::Info
;
1317 case SignatureState::NOTVALIDATED_PARTIAL_OK
:
1318 sMessage
= SfxResId(STR_SIGNATURE_NOTVALIDATED_PARTIAL_OK
);
1319 aInfoBarType
= InfoBarType::Warning
;
1321 //FIXME SignatureState::Unknown, own message?
1327 if ( !pFrame
->HasInfoBarWithID("signature") )
1329 if ( !sMessage
.isEmpty() )
1331 auto pInfoBar
= pFrame
->AppendInfoBar("signature", sMessage
, aInfoBarType
);
1332 if (pInfoBar
== nullptr || pInfoBar
->IsDisposed())
1334 VclPtrInstance
<PushButton
> xBtn(&(pFrame
->GetWindow()));
1335 xBtn
->SetText(SfxResId(STR_SIGNATURE_SHOW
));
1336 xBtn
->SetSizePixel(xBtn
->GetOptimalSize());
1337 xBtn
->SetClickHdl(LINK(this, SfxObjectShell
, SignDocumentHandler
));
1338 pInfoBar
->addButton(xBtn
);
1341 else // info bar exists already
1343 if ( eState
== SignatureState::NOSIGNATURES
)
1344 pFrame
->RemoveInfoBar("signature");
1346 pFrame
->UpdateInfoBar("signature", sMessage
, aInfoBarType
);
1350 rSet
.Put( SfxUInt16Item( SID_SIGNATURE
, static_cast<sal_uInt16
>(GetDocumentSignatureState()) ) );
1353 case SID_MACRO_SIGNATURE
:
1355 // the slot makes sense only if there is a macro in the document
1356 if ( pImpl
->documentStorageHasMacros() || pImpl
->aMacroMode
.hasMacroLibrary() )
1357 rSet
.Put( SfxUInt16Item( SID_MACRO_SIGNATURE
, static_cast<sal_uInt16
>(GetScriptingSignatureState()) ) );
1359 rSet
.DisableItem( nWhich
);
1362 case SID_DOC_REPAIR
:
1364 SfxUndoManager
* pIUndoMgr
= GetUndoManager();
1366 rSet
.Put( SfxBoolItem(nWhich
, pIUndoMgr
->IsEmptyActions()) );
1368 rSet
.DisableItem( nWhich
);
1375 IMPL_LINK_NOARG(SfxObjectShell
, SignDocumentHandler
, Button
*, void)
1377 GetDispatcher()->Execute(SID_SIGNATURE
);
1380 void SfxObjectShell::ExecProps_Impl(SfxRequest
&rReq
)
1382 switch ( rReq
.GetSlot() )
1386 SetModified( rReq
.GetArgs()->Get(SID_MODIFIED
).GetValue() );
1392 SetTitle( rReq
.GetArgs()->Get(SID_DOCTITLE
).GetValue() );
1396 case SID_DOCINFO_AUTHOR
:
1397 getDocProperties()->setAuthor( static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue() );
1400 case SID_DOCINFO_COMMENTS
:
1401 getDocProperties()->setDescription( static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue() );
1404 case SID_DOCINFO_KEYWORDS
:
1406 const OUString aStr
= static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue();
1407 getDocProperties()->setKeywords(
1408 ::comphelper::string::convertCommaSeparated(aStr
) );
1415 void SfxObjectShell::StateProps_Impl(SfxItemSet
&rSet
)
1417 SfxWhichIter
aIter(rSet
);
1418 for ( sal_uInt16 nSID
= aIter
.FirstWhich(); nSID
; nSID
= aIter
.NextWhich() )
1422 case SID_DOCINFO_AUTHOR
:
1424 rSet
.Put( SfxStringItem( nSID
,
1425 getDocProperties()->getAuthor() ) );
1429 case SID_DOCINFO_COMMENTS
:
1431 rSet
.Put( SfxStringItem( nSID
,
1432 getDocProperties()->getDescription()) );
1436 case SID_DOCINFO_KEYWORDS
:
1438 rSet
.Put( SfxStringItem( nSID
, ::comphelper::string::
1439 convertCommaSeparated(getDocProperties()->getKeywords())) );
1445 OSL_FAIL( "Not supported anymore!" );
1449 case SID_DOCFULLNAME
:
1451 rSet
.Put( SfxStringItem( SID_DOCFULLNAME
, GetTitle(SFX_TITLE_FULLNAME
) ) );
1457 rSet
.Put( SfxStringItem( SID_DOCTITLE
, GetTitle() ) );
1461 case SID_DOC_READONLY
:
1463 rSet
.Put( SfxBoolItem( SID_DOC_READONLY
, IsReadOnly() ) );
1469 rSet
.Put( SfxBoolItem( SID_DOC_SAVED
, !IsModified() ) );
1475 rSet
.Put( SfxBoolItem( SID_CLOSING
, false ) );
1479 case SID_DOC_LOADING
:
1480 rSet
.Put( SfxBoolItem( nSID
, ! ( pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
) ) );
1483 case SID_IMG_LOADING
:
1484 rSet
.Put( SfxBoolItem( nSID
, ! ( pImpl
->nLoadedFlags
& SfxLoadedFlags::IMAGES
) ) );
1491 void SfxObjectShell::ExecView_Impl(SfxRequest
&rReq
)
1493 switch ( rReq
.GetSlot() )
1497 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst( this );
1499 pFrame
->GetFrame().Appear();
1500 rReq
.SetReturnValue( SfxObjectItem( 0, pFrame
) );
1508 void SfxObjectShell::StateView_Impl(SfxItemSet
& /*rSet*/)
1512 SignatureState
SfxObjectShell::ImplCheckSignaturesInformation( const uno::Sequence
< security::DocumentSignatureInformation
>& aInfos
)
1514 bool bCertValid
= true;
1515 SignatureState nResult
= SignatureState::NOSIGNATURES
;
1516 bool bCompleteSignature
= true;
1517 if( aInfos
.hasElements() )
1519 nResult
= SignatureState::OK
;
1520 for ( const auto& rInfo
: aInfos
)
1524 sal_Int32 nCertStat
= rInfo
.CertificateStatus
;
1525 bCertValid
= nCertStat
== security::CertificateValidity::VALID
;
1528 if ( !rInfo
.SignatureIsValid
)
1530 nResult
= SignatureState::BROKEN
;
1531 break; // we know enough
1533 bCompleteSignature
&= !rInfo
.PartialDocumentSignature
;
1537 if (nResult
== SignatureState::OK
&& !bCertValid
&& !bCompleteSignature
)
1538 nResult
= SignatureState::NOTVALIDATED_PARTIAL_OK
;
1539 else if (nResult
== SignatureState::OK
&& !bCertValid
)
1540 nResult
= SignatureState::NOTVALIDATED
;
1541 else if ( nResult
== SignatureState::OK
&& bCertValid
&& !bCompleteSignature
)
1542 nResult
= SignatureState::PARTIAL_OK
;
1544 // this code must not check whether the document is modified
1545 // it should only check the provided info
1550 /// Does this ZIP storage have a signature stream?
1551 static bool HasSignatureStream(const uno::Reference
<embed::XStorage
>& xStorage
)
1556 if (xStorage
->hasByName("META-INF"))
1561 uno::Reference
<embed::XStorage
> xMetaInf
1562 = xStorage
->openStorageElement("META-INF", embed::ElementModes::READ
);
1565 return xMetaInf
->hasByName("documentsignatures.xml")
1566 || xMetaInf
->hasByName("macrosignatures.xml")
1567 || xMetaInf
->hasByName("packagesignatures.xml");
1570 catch (const css::io::IOException
& rException
)
1572 SAL_WARN("sfx.doc", "HasSignatureStream: failed to open META-INF: " << rException
.Message
);
1577 return xStorage
->hasByName("_xmlsignatures");
1580 uno::Sequence
< security::DocumentSignatureInformation
> SfxObjectShell::GetDocumentSignatureInformation( bool bScriptingContent
, const uno::Reference
< security::XDocumentDigitalSignatures
>& xSigner
)
1582 uno::Sequence
< security::DocumentSignatureInformation
> aResult
;
1583 uno::Reference
< security::XDocumentDigitalSignatures
> xLocSigner
= xSigner
;
1585 bool bSupportsSigning
= GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->GetSupportsSigning();
1586 if (GetMedium() && !GetMedium()->GetName().isEmpty() && ((IsOwnStorageFormat(*GetMedium()) && GetMedium()->GetStorage().is()) || bSupportsSigning
))
1590 if ( !xLocSigner
.is() )
1595 uno::Reference
< beans::XPropertySet
> xPropSet( GetStorage(), uno::UNO_QUERY_THROW
);
1596 xPropSet
->getPropertyValue("Version") >>= aVersion
;
1598 catch( uno::Exception
& )
1602 xLocSigner
.set( security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), aVersion
) );
1606 if ( bScriptingContent
)
1607 aResult
= xLocSigner
->verifyScriptingContentSignatures( GetMedium()->GetZipStorageToSign_Impl(),
1608 uno::Reference
< io::XInputStream
>() );
1611 if (GetMedium()->GetStorage().is())
1613 // Something ZIP-based.
1614 // Only call into xmlsecurity if we see a signature stream,
1615 // as libxmlsec init is expensive.
1616 if (HasSignatureStream(GetMedium()->GetZipStorageToSign_Impl()))
1617 aResult
= xLocSigner
->verifyDocumentContentSignatures( GetMedium()->GetZipStorageToSign_Impl(),
1618 uno::Reference
< io::XInputStream
>() );
1622 // Not ZIP-based, e.g. PDF.
1623 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(GetMedium()->GetName(), StreamMode::READ
));
1624 uno::Reference
<io::XStream
> xStream(new utl::OStreamWrapper(*pStream
));
1625 uno::Reference
<io::XInputStream
> xInputStream(xStream
, uno::UNO_QUERY
);
1626 aResult
= xLocSigner
->verifyDocumentContentSignatures(uno::Reference
<embed::XStorage
>(), xInputStream
);
1630 catch( css::uno::Exception
& )
1638 SignatureState
SfxObjectShell::ImplGetSignatureState( bool bScriptingContent
)
1640 SignatureState
* pState
= bScriptingContent
? &pImpl
->nScriptingSignatureState
: &pImpl
->nDocumentSignatureState
;
1642 if ( *pState
== SignatureState::UNKNOWN
)
1644 *pState
= SignatureState::NOSIGNATURES
;
1646 uno::Sequence
< security::DocumentSignatureInformation
> aInfos
= GetDocumentSignatureInformation( bScriptingContent
);
1647 *pState
= ImplCheckSignaturesInformation( aInfos
);
1650 if ( *pState
== SignatureState::OK
|| *pState
== SignatureState::NOTVALIDATED
1651 || *pState
== SignatureState::PARTIAL_OK
)
1654 *pState
= SignatureState::INVALID
;
1660 bool SfxObjectShell::PrepareForSigning(weld::Window
* pDialogParent
)
1662 // check whether the document is signed
1663 ImplGetSignatureState(); // document signature
1664 if (GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->IsOwnFormat())
1665 ImplGetSignatureState( true ); // script signature
1666 bool bHasSign
= ( pImpl
->nScriptingSignatureState
!= SignatureState::NOSIGNATURES
|| pImpl
->nDocumentSignatureState
!= SignatureState::NOSIGNATURES
);
1668 // the target ODF version on saving (only valid when signing ODF of course)
1669 SvtSaveOptions aSaveOpt
;
1670 SvtSaveOptions::ODFDefaultVersion nVersion
= aSaveOpt
.GetODFDefaultVersion();
1672 // the document is not new and is not modified
1673 OUString
aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1675 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty()
1676 || (GetMedium()->GetFilter()->IsOwnFormat() && aODFVersion
!= ODFVER_012_TEXT
&& !bHasSign
) )
1678 // the document might need saving ( new, modified or in ODF1.1 format without signature )
1680 if ( nVersion
>= SvtSaveOptions::ODFVER_012
)
1682 OUString
sQuestion(bHasSign
? SfxResId(STR_XMLSEC_QUERY_SAVESIGNEDBEFORESIGN
) : SfxResId(RID_SVXSTR_XMLSEC_QUERY_SAVEBEFORESIGN
));
1683 std::unique_ptr
<weld::MessageDialog
> xQuestion(Application::CreateMessageDialog(pDialogParent
,
1684 VclMessageType::Question
, VclButtonsType::YesNo
, sQuestion
));
1687 if (xQuestion
->run() == RET_YES
)
1689 sal_uInt16 nId
= SID_SAVEDOC
;
1690 if ( !GetMedium() || GetMedium()->GetName().isEmpty() )
1691 nId
= SID_SAVEASDOC
;
1692 SfxRequest
aSaveRequest( nId
, SfxCallMode::SLOT
, GetPool() );
1693 //ToDo: Review. We needed to call SetModified, otherwise the document would not be saved.
1695 ExecFile_Impl( aSaveRequest
);
1697 // Check if it is stored a format which supports signing
1698 if (GetMedium() && GetMedium()->GetFilter() && !GetMedium()->GetName().isEmpty()
1699 && ((!GetMedium()->GetFilter()->IsOwnFormat()
1700 && !GetMedium()->GetFilter()->GetSupportsSigning())
1701 || (GetMedium()->GetFilter()->IsOwnFormat()
1702 && !GetMedium()->HasStorage_Impl())))
1704 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
1705 pDialogParent
, VclMessageType::Info
, VclButtonsType::Ok
,
1706 SfxResId(STR_INFO_WRONGDOCFORMAT
)));
1714 // When the document is modified then we must not show the
1715 // digital signatures dialog
1716 // If we have come here then the user denied to save.
1723 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(pDialogParent
,
1724 VclMessageType::Warning
, VclButtonsType::Ok
, SfxResId(STR_XMLSEC_ODF12_EXPECTED
)));
1729 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty() )
1733 // the document is not modified currently, so it can not become modified after signing
1734 pImpl
->m_bAllowModifiedBackAfterSigning
= false;
1735 if ( IsEnableSetModified() )
1737 EnableSetModified( false );
1738 pImpl
->m_bAllowModifiedBackAfterSigning
= true;
1741 // we have to store to the original document, the original medium should be closed for this time
1742 if ( ConnectTmpStorage_Impl( pMedium
->GetStorage(), pMedium
) )
1744 GetMedium()->CloseAndRelease();
1750 void SfxObjectShell::RecheckSignature(bool bAlsoRecheckScriptingSignature
)
1752 if (bAlsoRecheckScriptingSignature
)
1753 pImpl
->nScriptingSignatureState
= SignatureState::UNKNOWN
; // Re-Check
1755 pImpl
->nDocumentSignatureState
= SignatureState::UNKNOWN
; // Re-Check
1757 Invalidate(SID_SIGNATURE
);
1758 Invalidate(SID_MACRO_SIGNATURE
);
1759 Broadcast(SfxHint(SfxHintId::TitleChanged
));
1762 void SfxObjectShell::AfterSigning(bool bSignSuccess
, bool bSignScriptingContent
)
1764 pImpl
->m_bSavingForSigning
= true;
1765 DoSaveCompleted( GetMedium() );
1766 pImpl
->m_bSavingForSigning
= false;
1769 RecheckSignature(bSignScriptingContent
);
1771 if ( pImpl
->m_bAllowModifiedBackAfterSigning
)
1772 EnableSetModified();
1775 bool SfxObjectShell::CheckIsReadonly(bool bSignScriptingContent
)
1777 if (GetMedium()->IsOriginallyReadOnly())
1779 // If the file is physically read-only, we just show the existing signatures
1782 OUString
aODFVersion(
1783 comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1784 uno::Reference
<security::XDocumentDigitalSignatures
> xSigner(
1785 security::DocumentDigitalSignatures::createWithVersionAndValidSignature(
1786 comphelper::getProcessComponentContext(), aODFVersion
, HasValidSignatures()));
1787 if (bSignScriptingContent
)
1788 xSigner
->showScriptingContentSignatures(GetMedium()->GetZipStorageToSign_Impl(),
1789 uno::Reference
<io::XInputStream
>());
1792 uno::Reference
<embed::XStorage
> xStorage
= GetMedium()->GetZipStorageToSign_Impl();
1794 xSigner
->showDocumentContentSignatures(xStorage
,
1795 uno::Reference
<io::XInputStream
>());
1798 std::unique_ptr
<SvStream
> pStream(
1799 utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ
));
1800 uno::Reference
<io::XInputStream
> xStream(new utl::OStreamWrapper(*pStream
));
1801 xSigner
->showDocumentContentSignatures(uno::Reference
<embed::XStorage
>(),
1806 catch (const uno::Exception
&)
1808 SAL_WARN("sfx.doc", "Couldn't use signing functionality!");
1815 bool SfxObjectShell::HasValidSignatures() const
1817 return pImpl
->nDocumentSignatureState
== SignatureState::OK
1818 || pImpl
->nDocumentSignatureState
== SignatureState::NOTVALIDATED
1819 || pImpl
->nDocumentSignatureState
== SignatureState::PARTIAL_OK
;
1822 SignatureState
SfxObjectShell::GetDocumentSignatureState()
1824 return ImplGetSignatureState();
1827 void SfxObjectShell::SignDocumentContent(weld::Window
* pDialogParent
)
1829 if (!PrepareForSigning(pDialogParent
))
1832 if (CheckIsReadonly(false))
1835 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
, false, HasValidSignatures());
1837 AfterSigning(bSignSuccess
, false);
1840 bool SfxObjectShell::SignDocumentContentUsingCertificate(const Reference
<XCertificate
>& xCertificate
)
1842 // 1. PrepareForSigning
1844 // check whether the document is signed
1845 ImplGetSignatureState(false); // document signature
1846 if (GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->IsOwnFormat())
1847 ImplGetSignatureState( true ); // script signature
1848 bool bHasSign
= ( pImpl
->nScriptingSignatureState
!= SignatureState::NOSIGNATURES
|| pImpl
->nDocumentSignatureState
!= SignatureState::NOSIGNATURES
);
1850 // the target ODF version on saving (only valid when signing ODF of course)
1851 SvtSaveOptions aSaveOpt
;
1852 SvtSaveOptions::ODFDefaultVersion nVersion
= aSaveOpt
.GetODFDefaultVersion();
1854 // the document is not new and is not modified
1855 OUString
aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1857 if (IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty()
1858 || (GetMedium()->GetFilter()->IsOwnFormat() && aODFVersion
!= ODFVER_012_TEXT
&& !bHasSign
))
1860 if ( nVersion
>= SvtSaveOptions::ODFVER_012
)
1862 sal_uInt16 nId
= SID_SAVEDOC
;
1863 if ( !GetMedium() || GetMedium()->GetName().isEmpty() )
1864 nId
= SID_SAVEASDOC
;
1865 SfxRequest
aSaveRequest( nId
, SfxCallMode::SLOT
, GetPool() );
1866 //ToDo: Review. We needed to call SetModified, otherwise the document would not be saved.
1868 ExecFile_Impl( aSaveRequest
);
1870 // Check if it is stored a format which supports signing
1871 if (GetMedium() && GetMedium()->GetFilter() && !GetMedium()->GetName().isEmpty()
1872 && ((!GetMedium()->GetFilter()->IsOwnFormat()
1873 && !GetMedium()->GetFilter()->GetSupportsSigning())
1874 || (GetMedium()->GetFilter()->IsOwnFormat()
1875 && !GetMedium()->HasStorage_Impl())))
1885 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty() )
1889 // the document is not modified currently, so it can not become modified after signing
1890 pImpl
->m_bAllowModifiedBackAfterSigning
= false;
1891 if ( IsEnableSetModified() )
1893 EnableSetModified( false );
1894 pImpl
->m_bAllowModifiedBackAfterSigning
= true;
1897 // we have to store to the original document, the original medium should be closed for this time
1898 bool bResult
= ConnectTmpStorage_Impl( pMedium
->GetStorage(), pMedium
);
1903 GetMedium()->CloseAndRelease();
1905 // 2. Check Read-Only
1906 if (GetMedium()->IsOriginallyReadOnly())
1910 bool bSignSuccess
= GetMedium()->SignDocumentContentUsingCertificate(HasValidSignatures(), xCertificate
);
1913 AfterSigning(bSignSuccess
, false);
1918 void SfxObjectShell::SignSignatureLine(weld::Window
* pDialogParent
,
1919 const OUString
& aSignatureLineId
,
1920 const Reference
<XCertificate
>& xCert
,
1921 const Reference
<XGraphic
>& xValidGraphic
,
1922 const Reference
<XGraphic
>& xInvalidGraphic
,
1923 const OUString
& aComment
)
1925 if (!PrepareForSigning(pDialogParent
))
1928 if (CheckIsReadonly(false))
1931 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
,
1932 false, HasValidSignatures(), aSignatureLineId
, xCert
, xValidGraphic
, xInvalidGraphic
, aComment
);
1934 AfterSigning(bSignSuccess
, false);
1936 // Reload the document to get the updated graphic
1937 // FIXME: Update just the signature line graphic instead of reloading the document
1938 SfxViewFrame
*pFrame
= GetFrame();
1940 pFrame
->GetDispatcher()->Execute(SID_RELOAD
);
1943 SignatureState
SfxObjectShell::GetScriptingSignatureState()
1945 return ImplGetSignatureState( true );
1948 void SfxObjectShell::SignScriptingContent(weld::Window
* pDialogParent
)
1950 if (!PrepareForSigning(pDialogParent
))
1953 if (CheckIsReadonly(true))
1956 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
, true, HasValidSignatures());
1958 AfterSigning(bSignSuccess
, true);
1963 class theSfxObjectShellUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theSfxObjectShellUnoTunnelId
> {};
1966 const uno::Sequence
<sal_Int8
>& SfxObjectShell::getUnoTunnelId()
1968 return theSfxObjectShellUnoTunnelId::get().getSeq();
1971 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */