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 <com/sun/star/style/XStyleFamiliesSupplier.hpp>
23 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
24 #include <com/sun/star/util/CloseVetoException.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/document/XCmisDocument.hpp>
28 #include <com/sun/star/drawing/LineStyle.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/security/XCertificate.hpp>
31 #include <com/sun/star/task/ErrorCodeIOException.hpp>
32 #include <com/sun/star/task/InteractionHandler.hpp>
33 #include <com/sun/star/task/XStatusIndicator.hpp>
34 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/servicehelper.hxx>
37 #include <com/sun/star/security/CertificateValidity.hpp>
38 #include <com/sun/star/drawing/XDrawView.hpp>
40 #include <com/sun/star/security/DocumentSignatureInformation.hpp>
41 #include <com/sun/star/security/DocumentDigitalSignatures.hpp>
42 #include <tools/diagnose_ex.h>
43 #include <tools/urlobj.hxx>
44 #include <svl/whiter.hxx>
45 #include <svl/intitem.hxx>
46 #include <svl/eitem.hxx>
47 #include <svl/visitem.hxx>
48 #include <svtools/sfxecode.hxx>
49 #include <svtools/ehdl.hxx>
50 #include <sal/log.hxx>
51 #include <sfx2/app.hxx>
53 #include <comphelper/string.hxx>
54 #include <basic/sbxcore.hxx>
55 #include <basic/sberrors.hxx>
56 #include <unotools/moduleoptions.hxx>
57 #include <unotools/saveopt.hxx>
58 #include <svtools/DocumentToGraphicRenderer.hxx>
59 #include <vcl/gdimtf.hxx>
60 #include <vcl/svapp.hxx>
61 #include <vcl/weld.hxx>
62 #include <comphelper/documentconstants.hxx>
63 #include <comphelper/storagehelper.hxx>
64 #include <tools/link.hxx>
66 #include <asyncfunc.hxx>
67 #include <sfx2/signaturestate.hxx>
68 #include <sfx2/sfxresid.hxx>
69 #include <sfx2/request.hxx>
70 #include <sfx2/printer.hxx>
71 #include <sfx2/viewsh.hxx>
72 #include <sfx2/dinfdlg.hxx>
73 #include <sfx2/docfilt.hxx>
74 #include <sfx2/docfile.hxx>
75 #include <sfx2/dispatch.hxx>
76 #include <sfx2/objitem.hxx>
77 #include <sfx2/objsh.hxx>
78 #include <objshimp.hxx>
79 #include <sfx2/module.hxx>
80 #include <sfx2/viewfrm.hxx>
81 #include <versdlg.hxx>
82 #include <sfx2/strings.hrc>
83 #include <sfx2/docfac.hxx>
84 #include <sfx2/fcontnr.hxx>
85 #include <sfx2/msgpool.hxx>
86 #include <sfx2/objface.hxx>
87 #include <checkin.hxx>
88 #include <sfx2/infobar.hxx>
89 #include <sfx2/sfxuno.hxx>
90 #include <sfx2/sfxsids.hrc>
91 #include <SfxRedactionHelper.hxx>
93 #include <com/sun/star/util/XCloseable.hpp>
94 #include <com/sun/star/document/XDocumentProperties.hpp>
96 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
97 #include <com/sun/star/frame/XDesktop2.hpp>
98 #include <com/sun/star/frame/Desktop.hpp>
100 #include <guisaveas.hxx>
101 #include <saveastemplatedlg.hxx>
103 #include <cppuhelper/implbase.hxx>
104 #include <unotools/ucbstreamhelper.hxx>
105 #include <unotools/streamwrap.hxx>
106 #include <comphelper/sequenceashashmap.hxx>
108 #include <autoredactdialog.hxx>
110 using namespace ::com::sun::star
;
111 using namespace ::com::sun::star::lang
;
112 using namespace ::com::sun::star::uno
;
113 using namespace ::com::sun::star::ui::dialogs
;
114 using namespace ::com::sun::star::awt
;
115 using namespace ::com::sun::star::container
;
116 using namespace ::com::sun::star::beans
;
117 using namespace ::com::sun::star::document
;
118 using namespace ::com::sun::star::security
;
119 using namespace ::com::sun::star::task
;
120 using namespace ::com::sun::star::graphic
;
122 #define ShellClass_SfxObjectShell
123 #include <sfxslots.hxx>
125 SFX_IMPL_SUPERCLASS_INTERFACE(SfxObjectShell
, SfxShell
)
127 void SfxObjectShell::InitInterface_Impl()
133 class SfxClosePreventer_Impl
: public ::cppu::WeakImplHelper
< css::util::XCloseListener
>
135 bool m_bGotOwnership
;
136 bool m_bPreventClose
;
139 SfxClosePreventer_Impl();
141 bool HasOwnership() const { return m_bGotOwnership
; }
143 void SetPreventClose( bool bPrevent
) { m_bPreventClose
= bPrevent
; }
145 virtual void SAL_CALL
queryClosing( const lang::EventObject
& aEvent
, sal_Bool bDeliverOwnership
) override
;
147 virtual void SAL_CALL
notifyClosing( const lang::EventObject
& aEvent
) override
;
149 virtual void SAL_CALL
disposing( const lang::EventObject
& aEvent
) override
;
155 SfxClosePreventer_Impl::SfxClosePreventer_Impl()
156 : m_bGotOwnership( false )
157 , m_bPreventClose( true )
161 void SAL_CALL
SfxClosePreventer_Impl::queryClosing( const lang::EventObject
&, sal_Bool bDeliverOwnership
)
163 if ( m_bPreventClose
)
165 if ( !m_bGotOwnership
)
166 m_bGotOwnership
= bDeliverOwnership
;
168 throw util::CloseVetoException();
172 void SAL_CALL
SfxClosePreventer_Impl::notifyClosing( const lang::EventObject
& )
175 void SAL_CALL
SfxClosePreventer_Impl::disposing( const lang::EventObject
& )
180 class SfxInstanceCloseGuard_Impl
182 rtl::Reference
<SfxClosePreventer_Impl
> m_xPreventer
;
183 uno::Reference
< util::XCloseable
> m_xCloseable
;
186 SfxInstanceCloseGuard_Impl() {}
188 ~SfxInstanceCloseGuard_Impl();
190 bool Init_Impl( const uno::Reference
< util::XCloseable
>& xCloseable
);
195 bool SfxInstanceCloseGuard_Impl::Init_Impl( const uno::Reference
< util::XCloseable
>& xCloseable
)
197 bool bResult
= false;
199 // do not allow reinit after the successful init
200 if ( xCloseable
.is() && !m_xCloseable
.is() )
204 m_xPreventer
= new SfxClosePreventer_Impl();
205 xCloseable
->addCloseListener( m_xPreventer
.get() );
206 m_xCloseable
= xCloseable
;
209 catch( uno::Exception
& )
211 OSL_FAIL( "Could not register close listener!" );
218 SfxInstanceCloseGuard_Impl::~SfxInstanceCloseGuard_Impl()
220 if ( !m_xCloseable
.is() || !m_xPreventer
.is() )
225 m_xCloseable
->removeCloseListener( m_xPreventer
.get() );
227 catch( uno::Exception
& )
233 if ( m_xPreventer
.is() )
235 m_xPreventer
->SetPreventClose( false );
237 if ( m_xPreventer
->HasOwnership() )
238 m_xCloseable
->close( true ); // TODO: do it asynchronously
241 catch( uno::Exception
& )
247 void SfxObjectShell::PrintExec_Impl(SfxRequest
&rReq
)
249 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst(this);
252 rReq
.SetSlot( SID_PRINTDOC
);
253 pFrame
->GetViewShell()->ExecuteSlot(rReq
);
258 void SfxObjectShell::PrintState_Impl(SfxItemSet
&rSet
)
260 bool bPrinting
= false;
261 SfxViewFrame
* pFrame
= SfxViewFrame::GetFirst( this );
264 SfxPrinter
*pPrinter
= pFrame
->GetViewShell()->GetPrinter();
265 bPrinting
= pPrinter
&& pPrinter
->IsPrinting();
267 rSet
.Put( SfxBoolItem( SID_PRINTOUT
, bPrinting
) );
270 bool SfxObjectShell::APISaveAs_Impl(const OUString
& aFileName
, SfxItemSet
& rItemSet
,
271 const css::uno::Sequence
<css::beans::PropertyValue
>& rArgs
)
277 OUString aFilterName
;
278 const SfxStringItem
* pFilterNameItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_FILTER_NAME
, false);
279 if( pFilterNameItem
)
281 aFilterName
= pFilterNameItem
->GetValue();
285 const SfxStringItem
* pContentTypeItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_CONTENTTYPE
, false);
286 if ( pContentTypeItem
)
288 std::shared_ptr
<const SfxFilter
> pFilter
= SfxFilterMatcher( GetFactory().GetFactoryName() ).GetFilter4Mime( pContentTypeItem
->GetValue(), SfxFilterFlags::EXPORT
);
290 aFilterName
= pFilter
->GetName();
294 // in case no filter defined use default one
295 if( aFilterName
.isEmpty() )
297 std::shared_ptr
<const SfxFilter
> pFilt
= SfxFilter::GetDefaultFilterFromFactory(GetFactory().GetFactoryName());
299 DBG_ASSERT( pFilt
, "No default filter!\n" );
301 aFilterName
= pFilt
->GetFilterName();
303 rItemSet
.Put(SfxStringItem(SID_FILTER_NAME
, aFilterName
));
308 SfxObjectShellRef
xLock( this ); // ???
310 // use the title that is provided in the media descriptor
311 const SfxStringItem
* pDocTitleItem
= rItemSet
.GetItem
<SfxStringItem
>(SID_DOCINFO_TITLE
, false);
313 getDocProperties()->setTitle( pDocTitleItem
->GetValue() );
315 bOk
= CommonSaveAs_Impl(INetURLObject(aFileName
), aFilterName
, rItemSet
, rArgs
);
322 void SfxObjectShell::CheckOut( )
326 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
327 xCmisDoc
->checkOut( );
329 // Remove the info bar
330 SfxViewFrame
* pViewFrame
= GetFrame();
331 pViewFrame
->RemoveInfoBar( "checkout" );
333 catch ( const uno::RuntimeException
& e
)
335 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
336 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
341 void SfxObjectShell::CancelCheckOut( )
345 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
346 xCmisDoc
->cancelCheckOut( );
348 uno::Reference
< util::XModifiable
> xModifiable( GetModel( ), uno::UNO_QUERY
);
349 if ( xModifiable
.is( ) )
350 xModifiable
->setModified( false );
352 catch ( const uno::RuntimeException
& e
)
354 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
355 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
360 void SfxObjectShell::CheckIn( )
364 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
365 // Pop up dialog to ask for comment and major
366 SfxCheckinDialog
checkinDlg(GetFrame()->GetWindow().GetFrameWeld());
367 if (checkinDlg
.run() == RET_OK
)
369 xCmisDoc
->checkIn(checkinDlg
.IsMajor(), checkinDlg
.GetComment());
370 uno::Reference
< util::XModifiable
> xModifiable( GetModel( ), uno::UNO_QUERY
);
371 if ( xModifiable
.is( ) )
372 xModifiable
->setModified( false );
375 catch ( const uno::RuntimeException
& e
)
377 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(GetFrame()->GetWindow().GetFrameWeld(),
378 VclMessageType::Warning
, VclButtonsType::Ok
, e
.Message
));
383 uno::Sequence
< document::CmisVersion
> SfxObjectShell::GetCmisVersions( ) const
387 uno::Reference
< document::XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY_THROW
);
388 return xCmisDoc
->getAllVersions( );
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
));
396 return uno::Sequence
< document::CmisVersion
> ( );
399 bool SfxObjectShell::IsSignPDF() const
401 if (pMedium
&& !pMedium
->IsOriginallyReadOnly())
403 const std::shared_ptr
<const SfxFilter
>& pFilter
= pMedium
->GetFilter();
404 if (pFilter
&& pFilter
->GetName() == "draw_pdf_import")
411 uno::Reference
<security::XCertificate
> SfxObjectShell::GetSignPDFCertificate() const
413 uno::Reference
<frame::XModel
> xModel
= GetBaseModel();
416 return uno::Reference
<security::XCertificate
>();
419 uno::Reference
<drawing::XShapes
> xShapes(xModel
->getCurrentSelection(), uno::UNO_QUERY
);
420 if (!xShapes
.is() || xShapes
->getCount() < 1)
422 return uno::Reference
<security::XCertificate
>();
425 uno::Reference
<beans::XPropertySet
> xShapeProps(xShapes
->getByIndex(0), uno::UNO_QUERY
);
426 if (!xShapeProps
.is())
428 return uno::Reference
<security::XCertificate
>();
431 if (!xShapeProps
->getPropertySetInfo()->hasPropertyByName("InteropGrabBag"))
433 return uno::Reference
<security::XCertificate
>();
436 comphelper::SequenceAsHashMap
aMap(xShapeProps
->getPropertyValue("InteropGrabBag"));
437 auto it
= aMap
.find("SignatureCertificate");
438 if (it
== aMap
.end())
440 return uno::Reference
<security::XCertificate
>();
443 return uno::Reference
<security::XCertificate
>(it
->second
, uno::UNO_QUERY
);
446 void SfxObjectShell::ExecFile_Impl(SfxRequest
&rReq
)
448 weld::Window
* pDialogParent
= rReq
.GetFrameWeld();
451 SfxViewFrame
* pFrame
= GetFrame();
453 pFrame
= SfxViewFrame::GetFirst(this);
455 pDialogParent
= pFrame
->GetWindow().GetFrameWeld();
458 sal_uInt16 nId
= rReq
.GetSlot();
460 if( SID_SIGNATURE
== nId
|| SID_MACRO_SIGNATURE
== nId
)
462 if ( QueryHiddenInformation( HiddenWarningFact::WhenSigning
, nullptr ) == RET_YES
)
464 if (SID_SIGNATURE
== nId
)
466 uno::Reference
<security::XCertificate
> xCertificate
= GetSignPDFCertificate();
467 if (xCertificate
.is())
469 SignDocumentContentUsingCertificate(xCertificate
);
471 // Reload to show how the PDF actually looks like after signing. This also
472 // changes "finish signing" on the infobar back to "sign document" as a side
474 SfxViewFrame
* pFrame
= GetFrame();
477 // Store current page before reload.
478 SfxAllItemSet
aSet(SfxGetpApp()->GetPool());
479 uno::Reference
<drawing::XDrawView
> xController(
480 GetBaseModel()->getCurrentController(), uno::UNO_QUERY
);
481 uno::Reference
<beans::XPropertySet
> xPage(xController
->getCurrentPage(),
484 xPage
->getPropertyValue("Number") >>= nPage
;
488 aSet
.Put(SfxInt32Item(SID_PAGE_NUMBER
, nPage
- 1));
490 SfxRequest
aReq(SID_RELOAD
, SfxCallMode::SLOT
, aSet
);
491 pFrame
->ExecReload_Impl(aReq
);
496 SignDocumentContent(pDialogParent
);
501 SignScriptingContent(pDialogParent
);
507 if ( !GetMedium() && nId
!= SID_CLOSEDOC
)
513 // this guard is created here to have it destruction at the end of the method
514 SfxInstanceCloseGuard_Impl aModelGuard
;
516 bool bIsPDFExport
= false;
517 bool bIsAutoRedact
= false;
518 std::vector
<std::pair
<RedactionTarget
*, OUString
>> aRedactionTargets
;
523 SfxViewFrame
* pFrame
= GetFrame();
525 pFrame
= SfxViewFrame::GetFirst( this );
529 if ( !IsOwnStorageFormat( *GetMedium() ) )
532 SfxVersionDialog
aDlg(pDialogParent
, pFrame
, IsSaveVersionOnClose());
534 SetSaveVersionOnClose(aDlg
.IsSaveVersionOnClose());
539 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
542 const SfxDocumentInfoItem
* pDocInfItem
= rReq
.GetArg
<SfxDocumentInfoItem
>(SID_DOCINFO
);
545 // parameter, e.g. from replayed macro
546 pDocInfItem
->UpdateDocumentInfo(getDocProperties(), true);
547 SetUseUserData( pDocInfItem
->IsUseUserData() );
548 SetUseThumbnailSave( pDocInfItem
->IsUseThumbnailSave() );
552 // no argument containing DocInfo; check optional arguments
553 bool bReadOnly
= IsReadOnly();
554 const SfxBoolItem
* pROItem
= rReq
.GetArg
<SfxBoolItem
>(SID_DOC_READONLY
);
556 // override readonly attribute of document
557 // e.g. if a readonly document is saved elsewhere and user asks for editing DocInfo before
558 bReadOnly
= pROItem
->GetValue();
561 const OUString
aURL( HasName() ? GetMedium()->GetName() : GetFactory().GetFactoryURL() );
563 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
564 uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties();
566 SfxDocumentInfoItem
aDocInfoItem( aURL
, getDocProperties(), aCmisProperties
,
567 IsUseUserData(), IsUseThumbnailSave() );
568 if ( !GetSlotState( SID_DOCTEMPLATE
) )
569 // templates not supported
570 aDocInfoItem
.SetTemplate(false);
572 SfxItemSet
aSet(GetPool(), svl::Items
<SID_DOCINFO
, SID_DOCINFO
, SID_DOC_READONLY
, SID_DOC_READONLY
,
573 SID_EXPLORER_PROPS_START
, SID_EXPLORER_PROPS_START
, SID_BASEURL
, SID_BASEURL
>{} );
574 aSet
.Put( aDocInfoItem
);
575 aSet
.Put( SfxBoolItem( SID_DOC_READONLY
, bReadOnly
) );
576 aSet
.Put( SfxStringItem( SID_EXPLORER_PROPS_START
, GetTitle() ) );
577 aSet
.Put( SfxStringItem( SID_BASEURL
, GetMedium()->GetBaseURL() ) );
579 // creating dialog is done via virtual method; application will
580 // add its own statistics page
581 std::shared_ptr
<SfxRequest
> pReq
= std::make_shared
<SfxRequest
>(rReq
);
582 std::shared_ptr
<SfxDocumentInfoDialog
> xDlg(CreateDocumentInfoDialog(rReq
.GetFrameWeld(), aSet
));
583 SfxTabDialogController::runAsync(xDlg
, [this, xDlg
, xCmisDoc
, pReq
](sal_Int32 nResult
)
585 if (RET_OK
== nResult
)
587 const SfxDocumentInfoItem
* pDocInfoItem
= SfxItemSet::GetItem
<SfxDocumentInfoItem
>(xDlg
->GetOutputItemSet(), SID_DOCINFO
, false);
590 // user has done some changes to DocumentInfo
591 pDocInfoItem
->UpdateDocumentInfo(getDocProperties());
592 const uno::Sequence
< document::CmisProperty
>& aNewCmisProperties
=
593 pDocInfoItem
->GetCmisProperties( );
594 if ( aNewCmisProperties
.hasElements( ) )
595 xCmisDoc
->updateCmisProperties( aNewCmisProperties
);
596 SetUseUserData( pDocInfoItem
->IsUseUserData() );
597 SetUseThumbnailSave( pDocInfoItem
-> IsUseThumbnailSave() );
598 // add data from dialog for possible recording purpose
599 pReq
->AppendItem( SfxDocumentInfoItem( GetTitle(),
600 getDocProperties(), aNewCmisProperties
, IsUseUserData(), IsUseThumbnailSave() ) );
603 css::uno::Reference
< css::uno::XInterface
> xInterface
;
604 const SfxUnoAnyItem
* pUnoAny
= pReq
->GetArg
<SfxUnoAnyItem
>(FN_PARAM_2
);
605 AsyncFunc
* pAsyncFunc
= pUnoAny
&& (pUnoAny
->GetValue() >>= xInterface
) ?
606 comphelper::getUnoTunnelImplementation
<AsyncFunc
>(xInterface
) : nullptr;
608 pAsyncFunc
->Execute();
613 // nothing done; no recording
623 case SID_AUTOREDACTDOC
:
625 // Actual redaction takes place on a newly generated Draw document
626 if (!SvtModuleOptions().IsModuleInstalled(SvtModuleOptions::EModule::DRAW
))
628 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
629 pDialogParent
, VclMessageType::Warning
, VclButtonsType::Ok
,
630 SfxResId(STR_REDACTION_NO_DRAW_WARNING
)));
637 SfxAutoRedactDialog
aDlg(pDialogParent
);
638 sal_Int16 nResult
= aDlg
.run();
640 if (nResult
!= RET_OK
|| !aDlg
.hasTargets() || !aDlg
.isValidState())
646 // else continue with normal redaction
647 bIsAutoRedact
= true;
648 aDlg
.getTargets(aRedactionTargets
);
655 css::uno::Reference
<css::frame::XModel
> xModel
= GetModel();
659 uno::Reference
< lang::XComponent
> xSourceDoc( xModel
);
661 // Actual redaction takes place on a newly generated Draw document
662 if (!SvtModuleOptions().IsModuleInstalled(SvtModuleOptions::EModule::DRAW
))
664 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
665 pDialogParent
, VclMessageType::Warning
, VclButtonsType::Ok
,
666 SfxResId(STR_REDACTION_NO_DRAW_WARNING
)));
673 DocumentToGraphicRenderer
aRenderer(xSourceDoc
, false);
675 // Get the page margins of the original doc
676 PageMargins aPageMargins
= {-1, -1, -1, -1};
677 if (aRenderer
.isWriter())
678 aPageMargins
= SfxRedactionHelper::getPageMarginsForWriter(xModel
);
679 else if (aRenderer
.isCalc())
680 aPageMargins
= SfxRedactionHelper::getPageMarginsForCalc(xModel
);
682 sal_Int32 nPages
= aRenderer
.getPageCount();
683 std::vector
< GDIMetaFile
> aMetaFiles
;
684 std::vector
< ::Size
> aPageSizes
;
686 // Convert the pages of the document to gdimetafiles
687 SfxRedactionHelper::getPageMetaFilesFromDoc(aMetaFiles
, aPageSizes
, nPages
, aRenderer
);
689 // Create an empty Draw component.
690 uno::Reference
<frame::XDesktop2
> xDesktop
= css::frame::Desktop::create(comphelper::getProcessComponentContext());
691 uno::Reference
<lang::XComponent
> xComponent
= xDesktop
->loadComponentFromURL("private:factory/sdraw", "_default", 0, {});
693 if (!xComponent
.is())
695 SAL_WARN("sfx.doc", "SID_REDACTDOC: Failed to load new draw component. loadComponentFromURL returned an empty reference.");
700 // Add the doc pages to the new draw document
701 SfxRedactionHelper::addPagesToDraw(xComponent
, nPages
, aMetaFiles
, aPageSizes
, aPageMargins
, aRedactionTargets
, bIsAutoRedact
);
703 // Show the Redaction toolbar
704 SfxViewFrame
* pViewFrame
= SfxViewFrame::Current();
707 SfxRedactionHelper::showRedactionToolbar(pViewFrame
);
712 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
713 case SID_DIRECTEXPORTDOCASPDF
:
715 uno::Reference
< lang::XComponent
> xComponent( GetCurrentComponent(), uno::UNO_QUERY
);
716 if (!xComponent
.is())
719 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xComponent
, uno::UNO_QUERY
);
721 // Redaction finalization takes place in Draw
722 if ( xServiceInfo
.is() && xServiceInfo
->supportsService("com.sun.star.drawing.DrawingDocument")
723 && SfxRedactionHelper::isRedactMode(rReq
) )
725 OUString
sRedactionStyle(SfxRedactionHelper::getStringParam(rReq
, SID_REDACTION_STYLE
));
727 // Access the draw pages
728 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(xComponent
, uno::UNO_QUERY
);
729 uno::Reference
<drawing::XDrawPages
> xDrawPages
= xDrawPagesSupplier
->getDrawPages();
731 sal_Int32 nPageCount
= xDrawPages
->getCount();
732 for (sal_Int32 nPageNum
= 0; nPageNum
< nPageCount
; ++nPageNum
)
735 uno::Reference
< drawing::XDrawPage
> xPage( xDrawPages
->getByIndex( nPageNum
), uno::UNO_QUERY
);
740 // Go through all shapes
741 sal_Int32 nShapeCount
= xPage
->getCount();
742 for (sal_Int32 nShapeNum
= 0; nShapeNum
< nShapeCount
; ++nShapeNum
)
744 uno::Reference
< drawing::XShape
> xCurrShape(xPage
->getByIndex(nShapeNum
), uno::UNO_QUERY
);
745 if (!xCurrShape
.is())
748 uno::Reference
< beans::XPropertySet
> xPropSet(xCurrShape
, uno::UNO_QUERY
);
752 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
757 if (xInfo
->hasPropertyByName("Name"))
759 uno::Any aAnyShapeName
= xPropSet
->getPropertyValue("Name");
760 aAnyShapeName
>>= sShapeName
;
765 // Rectangle redaction
766 if (sShapeName
== "RectangleRedactionShape"
767 && xInfo
->hasPropertyByName("FillTransparence") && xInfo
->hasPropertyByName("FillColor"))
769 xPropSet
->setPropertyValue("FillTransparence", css::uno::makeAny(static_cast<sal_Int16
>(0)));
770 if (sRedactionStyle
== "White")
772 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_WHITE
));
773 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_SOLID
));
774 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_BLACK
));
778 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_BLACK
));
779 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_NONE
));
782 // Freeform redaction
783 else if (sShapeName
== "FreeformRedactionShape"
784 && xInfo
->hasPropertyByName("LineTransparence") && xInfo
->hasPropertyByName("LineColor"))
786 xPropSet
->setPropertyValue("LineTransparence", css::uno::makeAny(static_cast<sal_Int16
>(0)));
788 if (sRedactionStyle
== "White")
790 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_WHITE
));
794 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_BLACK
));
802 case SID_EXPORTDOCASPDF
:
805 case SID_EXPORTDOCASEPUB
:
806 case SID_DIRECTEXPORTDOCASEPUB
:
809 case SID_SAVEASREMOTE
:
812 // derived class may decide to abort this
813 if( !QuerySlotExecutable( nId
) )
815 rReq
.SetReturnValue( SfxBoolItem( 0, false ) );
819 //!! detailed analysis of an error code
820 SfxObjectShellRef
xLock( this );
822 // the model can not be closed till the end of this method
823 // if somebody tries to close it during this time the model will be closed
824 // at the end of the method
825 aModelGuard
.Init_Impl( uno::Reference
< util::XCloseable
>( GetModel(), uno::UNO_QUERY
) );
827 ErrCode nErrorCode
= ERRCODE_NONE
;
829 // by default versions should be preserved always except in case of an explicit
830 // SaveAs via GUI, so the flag must be set accordingly
831 pImpl
->bPreserveVersions
= (nId
== SID_SAVEDOC
);
834 SfxErrorContext
aEc( ERRCTX_SFX_SAVEASDOC
, GetTitle() ); // ???
836 if ( nId
== SID_SAVEASDOC
|| nId
== SID_SAVEASREMOTE
)
838 // in case of plugin mode the SaveAs operation means SaveTo
839 const SfxBoolItem
* pViewOnlyItem
= SfxItemSet::GetItem
<SfxBoolItem
>(GetMedium()->GetItemSet(), SID_VIEWONLY
, false);
840 if ( pViewOnlyItem
&& pViewOnlyItem
->GetValue() )
841 rReq
.AppendItem( SfxBoolItem( SID_SAVETO
, true ) );
844 // TODO/LATER: do the following GUI related actions in standalone method
846 // Introduce a status indicator for GUI operation
847 const SfxUnoAnyItem
* pStatusIndicatorItem
= rReq
.GetArg
<SfxUnoAnyItem
>(SID_PROGRESS_STATUSBAR_CONTROL
);
848 if ( !pStatusIndicatorItem
)
850 // get statusindicator
851 uno::Reference
< task::XStatusIndicator
> xStatusIndicator
;
852 uno::Reference
< frame::XController
> xCtrl( GetModel()->getCurrentController() );
855 uno::Reference
< task::XStatusIndicatorFactory
> xStatFactory( xCtrl
->getFrame(), uno::UNO_QUERY
);
856 if( xStatFactory
.is() )
857 xStatusIndicator
= xStatFactory
->createStatusIndicator();
860 OSL_ENSURE( xStatusIndicator
.is(), "Can not retrieve default status indicator!" );
862 if ( xStatusIndicator
.is() )
864 SfxUnoAnyItem
aStatIndItem( SID_PROGRESS_STATUSBAR_CONTROL
, uno::makeAny( xStatusIndicator
) );
866 if ( nId
== SID_SAVEDOC
)
868 // in case of saving it is not possible to transport the parameters from here
869 // but it is not clear here whether the saving will be done or saveAs operation
870 GetMedium()->GetItemSet()->Put( aStatIndItem
);
873 rReq
.AppendItem( aStatIndItem
);
876 else if ( nId
== SID_SAVEDOC
)
878 // in case of saving it is not possible to transport the parameters from here
879 // but it is not clear here whether the saving will be done or saveAs operation
880 GetMedium()->GetItemSet()->Put( *pStatusIndicatorItem
);
883 // Introduce an interaction handler for GUI operation
884 const SfxUnoAnyItem
* pInteractionHandlerItem
= rReq
.GetArg
<SfxUnoAnyItem
>(SID_INTERACTIONHANDLER
);
885 if ( !pInteractionHandlerItem
)
887 uno::Reference
<css::awt::XWindow
> xParentWindow
;
888 uno::Reference
<frame::XController
> xCtrl(GetModel()->getCurrentController());
890 xParentWindow
= xCtrl
->getFrame()->getContainerWindow();
892 uno::Reference
< uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
894 uno::Reference
< task::XInteractionHandler2
> xInteract(
895 task::InteractionHandler::createWithParent(xContext
, xParentWindow
) );
897 SfxUnoAnyItem
aInteractionItem( SID_INTERACTIONHANDLER
, uno::makeAny( xInteract
) );
898 if ( nId
== SID_SAVEDOC
)
900 // in case of saving it is not possible to transport the parameters from here
901 // but it is not clear here whether the saving will be done or saveAs operation
902 GetMedium()->GetItemSet()->Put( aInteractionItem
);
905 rReq
.AppendItem( aInteractionItem
);
907 else if ( nId
== SID_SAVEDOC
)
909 // in case of saving it is not possible to transport the parameters from here
910 // but it is not clear here whether the saving will be done or saveAs operation
911 GetMedium()->GetItemSet()->Put( *pInteractionHandlerItem
);
915 const SfxStringItem
* pOldPasswordItem
= SfxItemSet::GetItem
<SfxStringItem
>(GetMedium()->GetItemSet(), SID_PASSWORD
, false);
916 const SfxUnoAnyItem
* pOldEncryptionDataItem
= SfxItemSet::GetItem
<SfxUnoAnyItem
>(GetMedium()->GetItemSet(), SID_ENCRYPTIONDATA
, false);
917 bool bPreselectPassword
= (pOldPasswordItem
&& pOldEncryptionDataItem
);
919 uno::Sequence
< beans::PropertyValue
> aDispatchArgs
;
920 if ( rReq
.GetArgs() )
925 bool bForceSaveAs
= nId
== SID_SAVEDOC
&& IsReadOnlyMedium();
926 const SfxSlot
* pSlot
= GetModule()->GetSlotPool()->GetSlot( bForceSaveAs
? SID_SAVEASDOC
: nId
);
928 throw uno::Exception("no slot", nullptr);
930 SfxStoringHelper aHelper
;
932 if ( QueryHiddenInformation( bIsPDFExport
? HiddenWarningFact::WhenCreatingPDF
: HiddenWarningFact::WhenSaving
, nullptr ) != RET_YES
)
934 // the user has decided not to store the document
935 throw task::ErrorCodeIOException(
936 "SfxObjectShell::ExecFile_Impl: ERRCODE_IO_ABORT",
937 uno::Reference
< uno::XInterface
>(), sal_uInt32(ERRCODE_IO_ABORT
));
940 aHelper
.GUIStoreModel( GetModel(),
941 OUString::createFromAscii( pSlot
->GetUnoName() ),
944 GetDocumentSignatureState() );
947 // merge aDispatchArgs to the request
948 SfxAllItemSet
aResultParams( GetPool() );
949 TransformParameters( nId
,
952 rReq
.SetArgs( aResultParams
);
954 // the StoreAsURL/StoreToURL method have called this method with false
955 // so it has to be restored to true here since it is a call from GUI
956 GetMedium()->SetUpdatePickList( true );
958 // TODO: in future it must be done in following way
959 // if document is opened from GUI, it immediately appears in the picklist
960 // if the document is a new one then it appears in the picklist immediately
961 // after SaveAs operation triggered from GUI
963 catch( const task::ErrorCodeIOException
& aErrorEx
)
965 TOOLS_WARN_EXCEPTION( "sfx.doc", "Fatal IO error during save");
966 nErrorCode
= ErrCode(aErrorEx
.ErrCode
);
970 nErrorCode
= ERRCODE_IO_GENERAL
;
973 // by default versions should be preserved always except in case of an explicit
974 // SaveAs via GUI, so the flag must be reset to guarantee this
975 pImpl
->bPreserveVersions
= true;
976 ErrCode lErr
=GetErrorCode();
978 if ( !lErr
&& nErrorCode
)
981 if ( lErr
&& nErrorCode
== ERRCODE_NONE
)
983 const SfxBoolItem
* pWarnItem
= rReq
.GetArg
<SfxBoolItem
>(SID_FAIL_ON_WARNING
);
984 if ( pWarnItem
&& pWarnItem
->GetValue() )
988 // may be nErrorCode should be shown in future
989 if ( lErr
!= ERRCODE_IO_ABORT
)
991 SfxErrorContext
aEc(ERRCTX_SFX_SAVEASDOC
,GetTitle());
992 ErrorHandler::HandleError(lErr
, pDialogParent
);
995 if (nId
== SID_DIRECTEXPORTDOCASPDF
&&
996 SfxRedactionHelper::isRedactMode(rReq
))
998 // Return the finalized redaction shapes back to normal (gray & transparent)
999 uno::Reference
< lang::XComponent
> xComponent( GetCurrentComponent(), uno::UNO_QUERY
);
1000 if (!xComponent
.is())
1003 uno::Reference
< lang::XServiceInfo
> xServiceInfo( xComponent
, uno::UNO_QUERY
);
1005 // Redaction finalization takes place in Draw
1006 if ( xServiceInfo
.is() && xServiceInfo
->supportsService("com.sun.star.drawing.DrawingDocument") )
1008 // Access the draw pages
1009 uno::Reference
<drawing::XDrawPagesSupplier
> xDrawPagesSupplier(xComponent
, uno::UNO_QUERY
);
1010 uno::Reference
<drawing::XDrawPages
> xDrawPages
= xDrawPagesSupplier
->getDrawPages();
1012 sal_Int32 nPageCount
= xDrawPages
->getCount();
1013 for (sal_Int32 nPageNum
= 0; nPageNum
< nPageCount
; ++nPageNum
)
1016 uno::Reference
< drawing::XDrawPage
> xPage( xDrawPages
->getByIndex( nPageNum
), uno::UNO_QUERY
);
1021 // Go through all shapes
1022 sal_Int32 nShapeCount
= xPage
->getCount();
1023 for (sal_Int32 nShapeNum
= 0; nShapeNum
< nShapeCount
; ++nShapeNum
)
1025 uno::Reference
< drawing::XShape
> xCurrShape(xPage
->getByIndex(nShapeNum
), uno::UNO_QUERY
);
1026 if (!xCurrShape
.is())
1029 uno::Reference
< beans::XPropertySet
> xPropSet(xCurrShape
, uno::UNO_QUERY
);
1033 uno::Reference
< beans::XPropertySetInfo
> xInfo
= xPropSet
->getPropertySetInfo();
1037 // Not a shape we converted?
1038 if (!xInfo
->hasPropertyByName("Name"))
1041 OUString sShapeName
;
1042 if (xInfo
->hasPropertyByName("Name"))
1044 uno::Any aAnyShapeName
= xPropSet
->getPropertyValue("Name");
1045 aAnyShapeName
>>= sShapeName
;
1050 // Rectangle redaction
1051 if (sShapeName
== "RectangleRedactionShape"
1052 && xInfo
->hasPropertyByName("FillTransparence") && xInfo
->hasPropertyByName("FillColor"))
1054 xPropSet
->setPropertyValue("FillTransparence", css::uno::makeAny(static_cast<sal_Int16
>(50)));
1055 xPropSet
->setPropertyValue("FillColor", css::uno::makeAny(COL_GRAY7
));
1056 xPropSet
->setPropertyValue("LineStyle", css::uno::makeAny(css::drawing::LineStyle::LineStyle_NONE
));
1059 // Freeform redaction
1060 else if (sShapeName
== "FreeformRedactionShape")
1062 xPropSet
->setPropertyValue("LineTransparence", css::uno::makeAny(static_cast<sal_Int16
>(50)));
1063 xPropSet
->setPropertyValue("LineColor", css::uno::makeAny(COL_GRAY7
));
1072 if ( nId
== SID_EXPORTDOCASPDF
)
1074 // This function is used by the SendMail function that needs information if an export
1075 // file was written or not. This could be due to cancellation of the export
1076 // or due to an error. So IO abort must be handled like an error!
1077 nErrorCode
= ( lErr
!= ERRCODE_IO_ABORT
) && ( nErrorCode
== ERRCODE_NONE
) ? nErrorCode
: lErr
;
1080 if ( ( nId
== SID_SAVEASDOC
|| nId
== SID_SAVEASREMOTE
) && nErrorCode
== ERRCODE_NONE
)
1082 const SfxBoolItem
* saveTo
= rReq
.GetArg
<SfxBoolItem
>(SID_SAVETO
);
1083 if (saveTo
== nullptr || !saveTo
->GetValue())
1085 SfxViewFrame
*pFrame
= GetFrame();
1087 pFrame
->RemoveInfoBar("readonly");
1088 SetReadOnlyUI(false);
1092 rReq
.SetReturnValue( SfxBoolItem(0, nErrorCode
== ERRCODE_NONE
) );
1102 SfxAllItemSet
aArgs( GetPool() );
1103 aArgs
.Put( SfxBoolItem( SID_SAVEACOPYITEM
, true ) );
1104 SfxRequest
aSaveACopyReq( SID_EXPORTDOC
, SfxCallMode::API
, aArgs
);
1105 ExecFile_Impl( aSaveACopyReq
);
1106 if ( !aSaveACopyReq
.IsDone() )
1114 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1118 // Evaluate Parameter
1119 const SfxBoolItem
* pSaveItem
= rReq
.GetArg
<SfxBoolItem
>(SID_CLOSEDOC_SAVE
);
1120 const SfxStringItem
* pNameItem
= rReq
.GetArg
<SfxStringItem
>(SID_CLOSEDOC_FILENAME
);
1123 if ( pSaveItem
->GetValue() )
1127 #if HAVE_FEATURE_SCRIPTING
1128 SbxBase::SetError( ERRCODE_BASIC_WRONG_ARGS
);
1133 SfxAllItemSet
aArgs( GetPool() );
1134 SfxStringItem
aTmpItem( SID_FILE_NAME
, pNameItem
->GetValue() );
1135 aArgs
.Put( aTmpItem
, aTmpItem
.Which() );
1136 SfxRequest
aSaveAsReq( SID_SAVEASDOC
, SfxCallMode::API
, aArgs
);
1137 ExecFile_Impl( aSaveAsReq
);
1138 if ( !aSaveAsReq
.IsDone() )
1148 // Cancelled by the user?
1149 if (!PrepareClose())
1151 rReq
.SetReturnValue( SfxBoolItem(0, false) );
1156 SetModified( false );
1157 ErrCode lErr
= GetErrorCode();
1158 ErrorHandler::HandleError(lErr
, pDialogParent
);
1160 rReq
.SetReturnValue( SfxBoolItem(0, true) );
1162 rReq
.ReleaseArgs(); // because the pool is destroyed in Close
1167 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1168 case SID_DOCTEMPLATE
:
1170 // save as document templates
1171 SfxSaveAsTemplateDialog
aDlg(pDialogParent
, GetModel());
1181 case SID_CANCELCHECKOUT
:
1183 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(nullptr,
1184 VclMessageType::Question
, VclButtonsType::YesNo
, SfxResId(STR_QUERY_CANCELCHECKOUT
)));
1185 if (xBox
->run() == RET_YES
)
1189 // Reload the document as we may still have local changes
1190 SfxViewFrame
*pFrame
= GetFrame();
1192 pFrame
->GetDispatcher()->Execute(SID_RELOAD
);
1203 // Prevent entry in the Pick-lists
1205 GetMedium()->SetUpdatePickList( false );
1207 // Ignore()-branches have already returned
1212 void SfxObjectShell::GetState_Impl(SfxItemSet
&rSet
)
1214 SfxWhichIter
aIter( rSet
);
1216 for ( sal_uInt16 nWhich
= aIter
.FirstWhich(); nWhich
; nWhich
= aIter
.NextWhich() )
1220 case SID_DOCTEMPLATE
:
1222 if ( isExportLocked())
1223 rSet
.DisableItem( nWhich
);
1230 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
1231 const uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties();
1233 if ( xCmisDoc
->isVersionable( ) && aCmisProperties
.hasElements( ) )
1235 // Loop over the CMIS Properties to find cmis:isVersionSeriesCheckedOut
1236 bool bIsGoogleFile
= false;
1237 bool bCheckedOut
= false;
1238 for ( const auto& rCmisProperty
: aCmisProperties
)
1240 if ( rCmisProperty
.Id
== "cmis:isVersionSeriesCheckedOut" )
1242 uno::Sequence
< sal_Bool
> bTmp
;
1243 rCmisProperty
.Value
>>= bTmp
;
1244 bCheckedOut
= bTmp
[0];
1246 // using title to know if it's a Google Drive file
1247 // maybe there's a safer way.
1248 if ( rCmisProperty
.Name
== "title" )
1249 bIsGoogleFile
= true;
1251 bShow
= !bCheckedOut
&& !bIsGoogleFile
;
1256 rSet
.DisableItem( nWhich
);
1257 rSet
.Put( SfxVisibilityItem( nWhich
, false ) );
1262 case SID_CANCELCHECKOUT
:
1266 Reference
< XCmisDocument
> xCmisDoc( GetModel(), uno::UNO_QUERY
);
1267 uno::Sequence
< document::CmisProperty
> aCmisProperties
= xCmisDoc
->getCmisProperties( );
1269 if ( xCmisDoc
->isVersionable( ) && aCmisProperties
.hasElements( ) )
1271 // Loop over the CMIS Properties to find cmis:isVersionSeriesCheckedOut
1272 bool bCheckedOut
= false;
1273 auto pProp
= std::find_if(aCmisProperties
.begin(), aCmisProperties
.end(),
1274 [](const document::CmisProperty
& rProp
) { return rProp
.Id
== "cmis:isVersionSeriesCheckedOut"; });
1275 if (pProp
!= aCmisProperties
.end())
1277 uno::Sequence
< sal_Bool
> bTmp
;
1278 pProp
->Value
>>= bTmp
;
1279 bCheckedOut
= bTmp
[0];
1281 bShow
= bCheckedOut
;
1286 rSet
.DisableItem( nWhich
);
1287 rSet
.Put( SfxVisibilityItem( nWhich
, false ) );
1294 SfxObjectShell
*pDoc
= this;
1295 SfxViewFrame
* pFrame
= GetFrame();
1297 pFrame
= SfxViewFrame::GetFirst( this );
1299 if ( !pFrame
|| !pDoc
->HasName() ||
1300 !IsOwnStorageFormat( *pDoc
->GetMedium() ) )
1301 rSet
.DisableItem( nWhich
);
1306 if ( IsReadOnly() || isSaveLocked())
1308 rSet
.DisableItem(nWhich
);
1311 rSet
.Put(SfxStringItem(nWhich
, SfxResId(STR_SAVEDOC
)));
1320 rSet
.Put(SfxStringItem(nWhich
, SfxResId(STR_CLOSEDOC
)));
1326 if (!(pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
)
1327 || isExportLocked())
1329 rSet
.DisableItem( nWhich
);
1332 if ( /*!pCombinedFilters ||*/ !GetMedium() )
1333 rSet
.DisableItem( nWhich
);
1335 rSet
.Put( SfxStringItem( nWhich
, SfxResId(STR_SAVEASDOC
) ) );
1341 if (!(pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
) || isExportLocked())
1343 rSet
.DisableItem( nWhich
);
1346 if ( /*!pCombinedFilters ||*/ !GetMedium() )
1347 rSet
.DisableItem( nWhich
);
1349 rSet
.Put( SfxStringItem( nWhich
, SfxResId(STR_SAVEACOPY
) ) );
1354 case SID_EXPORTDOCASPDF
:
1355 case SID_DIRECTEXPORTDOCASPDF
:
1356 case SID_EXPORTDOCASEPUB
:
1357 case SID_DIRECTEXPORTDOCASEPUB
:
1359 case SID_AUTOREDACTDOC
:
1360 case SID_SAVEASREMOTE
:
1362 if (isExportLocked())
1363 rSet
.DisableItem( nWhich
);
1367 case SID_DOC_MODIFIED
:
1369 rSet
.Put( SfxBoolItem( SID_DOC_MODIFIED
, IsModified() ) );
1375 rSet
.Put( SfxBoolItem( SID_MODIFIED
, IsModified() ) );
1379 case SID_DOCINFO_TITLE
:
1381 rSet
.Put( SfxStringItem(
1382 SID_DOCINFO_TITLE
, getDocProperties()->getTitle() ) );
1387 if( GetMedium() && HasName() )
1388 rSet
.Put( SfxStringItem(
1389 SID_FILE_NAME
, GetMedium()->GetName() ) );
1394 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst(this);
1397 SignatureState eState
= GetDocumentSignatureState();
1398 InfobarType
aInfobarType(InfobarType::INFO
);
1399 OUString
sMessage("");
1403 case SignatureState::BROKEN
:
1404 sMessage
= SfxResId(STR_SIGNATURE_BROKEN
);
1405 aInfobarType
= InfobarType::DANGER
;
1407 case SignatureState::INVALID
:
1408 sMessage
= SfxResId(STR_SIGNATURE_INVALID
);
1409 // Warning only, I've tried Danger and it looked too scary
1410 aInfobarType
= InfobarType::WARNING
;
1412 case SignatureState::NOTVALIDATED
:
1413 sMessage
= SfxResId(STR_SIGNATURE_NOTVALIDATED
);
1414 aInfobarType
= InfobarType::WARNING
;
1416 case SignatureState::PARTIAL_OK
:
1417 sMessage
= SfxResId(STR_SIGNATURE_PARTIAL_OK
);
1418 aInfobarType
= InfobarType::WARNING
;
1420 case SignatureState::OK
:
1421 sMessage
= SfxResId(STR_SIGNATURE_OK
);
1422 aInfobarType
= InfobarType::INFO
;
1424 case SignatureState::NOTVALIDATED_PARTIAL_OK
:
1425 sMessage
= SfxResId(STR_SIGNATURE_NOTVALIDATED_PARTIAL_OK
);
1426 aInfobarType
= InfobarType::WARNING
;
1428 //FIXME SignatureState::Unknown, own message?
1434 if ( !pFrame
->HasInfoBarWithID("signature") )
1436 if ( !sMessage
.isEmpty() )
1438 auto pInfoBar
= pFrame
->AppendInfoBar("signature", "", sMessage
, aInfobarType
);
1439 if (pInfoBar
== nullptr || pInfoBar
->IsDisposed())
1441 weld::Button
& rBtn
= pInfoBar
->addButton();
1442 rBtn
.set_label(SfxResId(STR_SIGNATURE_SHOW
));
1443 rBtn
.connect_clicked(LINK(this, SfxObjectShell
, SignDocumentHandler
));
1446 else // info bar exists already
1448 if ( eState
== SignatureState::NOSIGNATURES
)
1449 pFrame
->RemoveInfoBar("signature");
1451 pFrame
->UpdateInfoBar("signature", "", sMessage
, aInfobarType
);
1455 rSet
.Put( SfxUInt16Item( SID_SIGNATURE
, static_cast<sal_uInt16
>(GetDocumentSignatureState()) ) );
1458 case SID_MACRO_SIGNATURE
:
1460 // the slot makes sense only if there is a macro in the document
1461 if ( pImpl
->documentStorageHasMacros() || pImpl
->aMacroMode
.hasMacroLibrary() )
1462 rSet
.Put( SfxUInt16Item( SID_MACRO_SIGNATURE
, static_cast<sal_uInt16
>(GetScriptingSignatureState()) ) );
1464 rSet
.DisableItem( nWhich
);
1467 case SID_DOC_REPAIR
:
1469 SfxUndoManager
* pIUndoMgr
= GetUndoManager();
1471 rSet
.Put( SfxBoolItem(nWhich
, pIUndoMgr
->IsEmptyActions()) );
1473 rSet
.DisableItem( nWhich
);
1480 IMPL_LINK_NOARG(SfxObjectShell
, SignDocumentHandler
, weld::Button
&, void)
1482 GetDispatcher()->Execute(SID_SIGNATURE
);
1485 void SfxObjectShell::ExecProps_Impl(SfxRequest
&rReq
)
1487 switch ( rReq
.GetSlot() )
1491 SetModified( rReq
.GetArgs()->Get(SID_MODIFIED
).GetValue() );
1497 SetTitle( rReq
.GetArgs()->Get(SID_DOCTITLE
).GetValue() );
1501 case SID_DOCINFO_AUTHOR
:
1502 getDocProperties()->setAuthor( static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue() );
1505 case SID_DOCINFO_COMMENTS
:
1506 getDocProperties()->setDescription( static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue() );
1509 case SID_DOCINFO_KEYWORDS
:
1511 const OUString aStr
= static_cast<const SfxStringItem
&>(rReq
.GetArgs()->Get(rReq
.GetSlot())).GetValue();
1512 getDocProperties()->setKeywords(
1513 ::comphelper::string::convertCommaSeparated(aStr
) );
1520 void SfxObjectShell::StateProps_Impl(SfxItemSet
&rSet
)
1522 SfxWhichIter
aIter(rSet
);
1523 for ( sal_uInt16 nSID
= aIter
.FirstWhich(); nSID
; nSID
= aIter
.NextWhich() )
1527 case SID_DOCINFO_AUTHOR
:
1529 rSet
.Put( SfxStringItem( nSID
,
1530 getDocProperties()->getAuthor() ) );
1534 case SID_DOCINFO_COMMENTS
:
1536 rSet
.Put( SfxStringItem( nSID
,
1537 getDocProperties()->getDescription()) );
1541 case SID_DOCINFO_KEYWORDS
:
1543 rSet
.Put( SfxStringItem( nSID
, ::comphelper::string::
1544 convertCommaSeparated(getDocProperties()->getKeywords())) );
1550 OSL_FAIL( "Not supported anymore!" );
1554 case SID_DOCFULLNAME
:
1556 rSet
.Put( SfxStringItem( SID_DOCFULLNAME
, GetTitle(SFX_TITLE_FULLNAME
) ) );
1562 rSet
.Put( SfxStringItem( SID_DOCTITLE
, GetTitle() ) );
1566 case SID_DOC_READONLY
:
1568 rSet
.Put( SfxBoolItem( SID_DOC_READONLY
, IsReadOnly() ) );
1574 rSet
.Put( SfxBoolItem( SID_DOC_SAVED
, !IsModified() ) );
1580 rSet
.Put( SfxBoolItem( SID_CLOSING
, false ) );
1584 case SID_DOC_LOADING
:
1585 rSet
.Put( SfxBoolItem( nSID
, ! ( pImpl
->nLoadedFlags
& SfxLoadedFlags::MAINDOCUMENT
) ) );
1588 case SID_IMG_LOADING
:
1589 rSet
.Put( SfxBoolItem( nSID
, ! ( pImpl
->nLoadedFlags
& SfxLoadedFlags::IMAGES
) ) );
1596 void SfxObjectShell::ExecView_Impl(SfxRequest
&rReq
)
1598 switch ( rReq
.GetSlot() )
1602 SfxViewFrame
*pFrame
= SfxViewFrame::GetFirst( this );
1604 pFrame
->GetFrame().Appear();
1605 rReq
.SetReturnValue( SfxObjectItem( 0, pFrame
) );
1613 void SfxObjectShell::StateView_Impl(SfxItemSet
& /*rSet*/)
1617 /// Does this ZIP storage have a signature stream?
1618 static bool HasSignatureStream(const uno::Reference
<embed::XStorage
>& xStorage
)
1623 if (xStorage
->hasByName("META-INF"))
1628 uno::Reference
<embed::XStorage
> xMetaInf
1629 = xStorage
->openStorageElement("META-INF", embed::ElementModes::READ
);
1632 return xMetaInf
->hasByName("documentsignatures.xml")
1633 || xMetaInf
->hasByName("macrosignatures.xml")
1634 || xMetaInf
->hasByName("packagesignatures.xml");
1637 catch (const css::io::IOException
&)
1639 TOOLS_WARN_EXCEPTION("sfx.doc", "HasSignatureStream: failed to open META-INF");
1644 return xStorage
->hasByName("_xmlsignatures");
1647 uno::Sequence
< security::DocumentSignatureInformation
> SfxObjectShell::GetDocumentSignatureInformation( bool bScriptingContent
, const uno::Reference
< security::XDocumentDigitalSignatures
>& xSigner
)
1649 uno::Sequence
< security::DocumentSignatureInformation
> aResult
;
1650 uno::Reference
< security::XDocumentDigitalSignatures
> xLocSigner
= xSigner
;
1652 bool bSupportsSigning
= GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->GetSupportsSigning();
1653 if (GetMedium() && !GetMedium()->GetName().isEmpty() && ((IsOwnStorageFormat(*GetMedium()) && GetMedium()->GetStorage().is()) || bSupportsSigning
))
1657 if ( !xLocSigner
.is() )
1662 uno::Reference
< beans::XPropertySet
> xPropSet( GetStorage(), uno::UNO_QUERY_THROW
);
1663 xPropSet
->getPropertyValue("Version") >>= aVersion
;
1665 catch( uno::Exception
& )
1669 xLocSigner
.set( security::DocumentDigitalSignatures::createWithVersion(comphelper::getProcessComponentContext(), aVersion
) );
1673 if ( bScriptingContent
)
1674 aResult
= xLocSigner
->verifyScriptingContentSignatures( GetMedium()->GetZipStorageToSign_Impl(),
1675 uno::Reference
< io::XInputStream
>() );
1678 if (GetMedium()->GetStorage(false).is())
1680 // Something ZIP-based.
1681 // Only call into xmlsecurity if we see a signature stream,
1682 // as libxmlsec init is expensive.
1683 if (HasSignatureStream(GetMedium()->GetZipStorageToSign_Impl()))
1684 aResult
= xLocSigner
->verifyDocumentContentSignatures( GetMedium()->GetZipStorageToSign_Impl(),
1685 uno::Reference
< io::XInputStream
>() );
1689 // Not ZIP-based, e.g. PDF.
1691 // Create temp file if needed.
1692 GetMedium()->CreateTempFile(/*bReplace=*/false);
1694 std::unique_ptr
<SvStream
> pStream(utl::UcbStreamHelper::CreateStream(GetMedium()->GetName(), StreamMode::READ
));
1695 uno::Reference
<io::XStream
> xStream(new utl::OStreamWrapper(*pStream
));
1696 uno::Reference
<io::XInputStream
> xInputStream(xStream
, uno::UNO_QUERY
);
1697 aResult
= xLocSigner
->verifyDocumentContentSignatures(uno::Reference
<embed::XStorage
>(), xInputStream
);
1701 catch( css::uno::Exception
& )
1709 SignatureState
SfxObjectShell::ImplGetSignatureState( bool bScriptingContent
)
1711 SignatureState
* pState
= bScriptingContent
? &pImpl
->nScriptingSignatureState
: &pImpl
->nDocumentSignatureState
;
1713 if ( *pState
== SignatureState::UNKNOWN
)
1715 *pState
= SignatureState::NOSIGNATURES
;
1717 uno::Sequence
< security::DocumentSignatureInformation
> aInfos
= GetDocumentSignatureInformation( bScriptingContent
);
1718 *pState
= DocumentSignatures::getSignatureState(aInfos
);
1721 if ( *pState
== SignatureState::OK
|| *pState
== SignatureState::NOTVALIDATED
1722 || *pState
== SignatureState::PARTIAL_OK
)
1725 *pState
= SignatureState::INVALID
;
1731 bool SfxObjectShell::PrepareForSigning(weld::Window
* pDialogParent
)
1733 // check whether the document is signed
1734 ImplGetSignatureState(); // document signature
1735 if (GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->IsOwnFormat())
1736 ImplGetSignatureState( true ); // script signature
1737 bool bHasSign
= ( pImpl
->nScriptingSignatureState
!= SignatureState::NOSIGNATURES
|| pImpl
->nDocumentSignatureState
!= SignatureState::NOSIGNATURES
);
1739 // the target ODF version on saving (only valid when signing ODF of course)
1740 SvtSaveOptions aSaveOpt
;
1741 SvtSaveOptions::ODFSaneDefaultVersion nVersion
= aSaveOpt
.GetODFSaneDefaultVersion();
1743 // the document is not new and is not modified
1744 OUString
aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1746 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty()
1747 || (GetMedium()->GetFilter()->IsOwnFormat() && aODFVersion
.compareTo(ODFVER_012_TEXT
) < 0 && !bHasSign
))
1749 // the document might need saving ( new, modified or in ODF1.1 format without signature )
1751 if (nVersion
>= SvtSaveOptions::ODFSVER_012
)
1753 OUString
sQuestion(bHasSign
? SfxResId(STR_XMLSEC_QUERY_SAVESIGNEDBEFORESIGN
) : SfxResId(RID_SVXSTR_XMLSEC_QUERY_SAVEBEFORESIGN
));
1754 std::unique_ptr
<weld::MessageDialog
> xQuestion(Application::CreateMessageDialog(pDialogParent
,
1755 VclMessageType::Question
, VclButtonsType::YesNo
, sQuestion
));
1758 if (xQuestion
->run() == RET_YES
)
1760 sal_uInt16 nId
= SID_SAVEDOC
;
1761 if ( !GetMedium() || GetMedium()->GetName().isEmpty() )
1762 nId
= SID_SAVEASDOC
;
1763 SfxRequest
aSaveRequest( nId
, SfxCallMode::SLOT
, GetPool() );
1764 //ToDo: Review. We needed to call SetModified, otherwise the document would not be saved.
1766 ExecFile_Impl( aSaveRequest
);
1768 // Check if it is stored a format which supports signing
1769 if (GetMedium() && GetMedium()->GetFilter() && !GetMedium()->GetName().isEmpty()
1770 && ((!GetMedium()->GetFilter()->IsOwnFormat()
1771 && !GetMedium()->GetFilter()->GetSupportsSigning())
1772 || (GetMedium()->GetFilter()->IsOwnFormat()
1773 && !GetMedium()->HasStorage_Impl())))
1775 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
1776 pDialogParent
, VclMessageType::Info
, VclButtonsType::Ok
,
1777 SfxResId(STR_INFO_WRONGDOCFORMAT
)));
1785 // When the document is modified then we must not show the
1786 // digital signatures dialog
1787 // If we have come here then the user denied to save.
1794 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(pDialogParent
,
1795 VclMessageType::Warning
, VclButtonsType::Ok
, SfxResId(STR_XMLSEC_ODF12_EXPECTED
)));
1800 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty() )
1804 // the document is not modified currently, so it can not become modified after signing
1805 pImpl
->m_bAllowModifiedBackAfterSigning
= false;
1806 if ( IsEnableSetModified() )
1808 EnableSetModified( false );
1809 pImpl
->m_bAllowModifiedBackAfterSigning
= true;
1812 // we have to store to the original document, the original medium should be closed for this time
1813 if ( ConnectTmpStorage_Impl( pMedium
->GetStorage(), pMedium
) )
1815 GetMedium()->CloseAndRelease();
1821 void SfxObjectShell::RecheckSignature(bool bAlsoRecheckScriptingSignature
)
1823 if (bAlsoRecheckScriptingSignature
)
1824 pImpl
->nScriptingSignatureState
= SignatureState::UNKNOWN
; // Re-Check
1826 pImpl
->nDocumentSignatureState
= SignatureState::UNKNOWN
; // Re-Check
1828 Invalidate(SID_SIGNATURE
);
1829 Invalidate(SID_MACRO_SIGNATURE
);
1830 Broadcast(SfxHint(SfxHintId::TitleChanged
));
1833 void SfxObjectShell::AfterSigning(bool bSignSuccess
, bool bSignScriptingContent
)
1835 pImpl
->m_bSavingForSigning
= true;
1836 DoSaveCompleted( GetMedium() );
1837 pImpl
->m_bSavingForSigning
= false;
1840 RecheckSignature(bSignScriptingContent
);
1842 if ( pImpl
->m_bAllowModifiedBackAfterSigning
)
1843 EnableSetModified();
1846 bool SfxObjectShell::CheckIsReadonly(bool bSignScriptingContent
)
1848 if (GetMedium()->IsOriginallyReadOnly())
1850 // If the file is physically read-only, we just show the existing signatures
1853 OUString
aODFVersion(
1854 comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1855 uno::Reference
<security::XDocumentDigitalSignatures
> xSigner(
1856 security::DocumentDigitalSignatures::createWithVersionAndValidSignature(
1857 comphelper::getProcessComponentContext(), aODFVersion
, HasValidSignatures()));
1858 if (bSignScriptingContent
)
1859 xSigner
->showScriptingContentSignatures(GetMedium()->GetZipStorageToSign_Impl(),
1860 uno::Reference
<io::XInputStream
>());
1863 uno::Reference
<embed::XStorage
> xStorage
= GetMedium()->GetZipStorageToSign_Impl();
1865 xSigner
->showDocumentContentSignatures(xStorage
,
1866 uno::Reference
<io::XInputStream
>());
1869 std::unique_ptr
<SvStream
> pStream(
1870 utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ
));
1871 uno::Reference
<io::XInputStream
> xStream(new utl::OStreamWrapper(*pStream
));
1872 xSigner
->showDocumentContentSignatures(uno::Reference
<embed::XStorage
>(),
1877 catch (const uno::Exception
&)
1879 SAL_WARN("sfx.doc", "Couldn't use signing functionality!");
1886 bool SfxObjectShell::HasValidSignatures() const
1888 return pImpl
->nDocumentSignatureState
== SignatureState::OK
1889 || pImpl
->nDocumentSignatureState
== SignatureState::NOTVALIDATED
1890 || pImpl
->nDocumentSignatureState
== SignatureState::PARTIAL_OK
;
1893 SignatureState
SfxObjectShell::GetDocumentSignatureState()
1895 return ImplGetSignatureState();
1898 void SfxObjectShell::SignDocumentContent(weld::Window
* pDialogParent
)
1900 if (!PrepareForSigning(pDialogParent
))
1903 if (CheckIsReadonly(false))
1906 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
, false, HasValidSignatures());
1908 AfterSigning(bSignSuccess
, false);
1911 bool SfxObjectShell::SignDocumentContentUsingCertificate(const Reference
<XCertificate
>& xCertificate
)
1913 // 1. PrepareForSigning
1915 // check whether the document is signed
1916 ImplGetSignatureState(false); // document signature
1917 if (GetMedium() && GetMedium()->GetFilter() && GetMedium()->GetFilter()->IsOwnFormat())
1918 ImplGetSignatureState( true ); // script signature
1919 bool bHasSign
= ( pImpl
->nScriptingSignatureState
!= SignatureState::NOSIGNATURES
|| pImpl
->nDocumentSignatureState
!= SignatureState::NOSIGNATURES
);
1921 // the target ODF version on saving (only valid when signing ODF of course)
1922 SvtSaveOptions aSaveOpt
;
1923 SvtSaveOptions::ODFSaneDefaultVersion nVersion
= aSaveOpt
.GetODFSaneDefaultVersion();
1925 // the document is not new and is not modified
1926 OUString
aODFVersion(comphelper::OStorageHelper::GetODFVersionFromStorage(GetStorage()));
1928 if (IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty()
1929 || (GetMedium()->GetFilter()->IsOwnFormat() && aODFVersion
.compareTo(ODFVER_012_TEXT
) < 0 && !bHasSign
))
1931 if (nVersion
>= SvtSaveOptions::ODFSVER_012
)
1933 sal_uInt16 nId
= SID_SAVEDOC
;
1934 if ( !GetMedium() || GetMedium()->GetName().isEmpty() )
1935 nId
= SID_SAVEASDOC
;
1936 SfxRequest
aSaveRequest( nId
, SfxCallMode::SLOT
, GetPool() );
1937 //ToDo: Review. We needed to call SetModified, otherwise the document would not be saved.
1939 ExecFile_Impl( aSaveRequest
);
1941 // Check if it is stored a format which supports signing
1942 if (GetMedium() && GetMedium()->GetFilter() && !GetMedium()->GetName().isEmpty()
1943 && ((!GetMedium()->GetFilter()->IsOwnFormat()
1944 && !GetMedium()->GetFilter()->GetSupportsSigning())
1945 || (GetMedium()->GetFilter()->IsOwnFormat()
1946 && !GetMedium()->HasStorage_Impl())))
1956 if ( IsModified() || !GetMedium() || GetMedium()->GetName().isEmpty() )
1960 // the document is not modified currently, so it can not become modified after signing
1961 pImpl
->m_bAllowModifiedBackAfterSigning
= false;
1962 if ( IsEnableSetModified() )
1964 EnableSetModified( false );
1965 pImpl
->m_bAllowModifiedBackAfterSigning
= true;
1968 // we have to store to the original document, the original medium should be closed for this time
1969 bool bResult
= ConnectTmpStorage_Impl( pMedium
->GetStorage(), pMedium
);
1974 GetMedium()->CloseAndRelease();
1976 // 2. Check Read-Only
1977 if (GetMedium()->IsOriginallyReadOnly())
1981 bool bSignSuccess
= GetMedium()->SignDocumentContentUsingCertificate(
1982 GetBaseModel(), HasValidSignatures(), xCertificate
);
1985 AfterSigning(bSignSuccess
, false);
1990 void SfxObjectShell::SignSignatureLine(weld::Window
* pDialogParent
,
1991 const OUString
& aSignatureLineId
,
1992 const Reference
<XCertificate
>& xCert
,
1993 const Reference
<XGraphic
>& xValidGraphic
,
1994 const Reference
<XGraphic
>& xInvalidGraphic
,
1995 const OUString
& aComment
)
1997 if (!PrepareForSigning(pDialogParent
))
2000 if (CheckIsReadonly(false))
2003 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
,
2004 false, HasValidSignatures(), aSignatureLineId
, xCert
, xValidGraphic
, xInvalidGraphic
, aComment
);
2006 AfterSigning(bSignSuccess
, false);
2008 // Reload the document to get the updated graphic
2009 // FIXME: Update just the signature line graphic instead of reloading the document
2010 SfxViewFrame
*pFrame
= GetFrame();
2012 pFrame
->GetDispatcher()->Execute(SID_RELOAD
);
2015 SignatureState
SfxObjectShell::GetScriptingSignatureState()
2017 return ImplGetSignatureState( true );
2020 void SfxObjectShell::SignScriptingContent(weld::Window
* pDialogParent
)
2022 if (!PrepareForSigning(pDialogParent
))
2025 if (CheckIsReadonly(true))
2028 bool bSignSuccess
= GetMedium()->SignContents_Impl(pDialogParent
, true, HasValidSignatures());
2030 AfterSigning(bSignSuccess
, true);
2035 class theSfxObjectShellUnoTunnelId
: public rtl::Static
< UnoTunnelIdInit
, theSfxObjectShellUnoTunnelId
> {};
2038 const uno::Sequence
<sal_Int8
>& SfxObjectShell::getUnoTunnelId()
2040 return theSfxObjectShellUnoTunnelId::get().getSeq();
2043 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */