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 <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
21 #include <com/sun/star/document/XDocumentProperties.hpp>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/beans/XPropertySetInfo.hpp>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <osl/diagnose.h>
28 #include <IDocumentContentOperations.hxx>
29 #include <IDocumentUndoRedo.hxx>
30 #include <IDocumentFieldsAccess.hxx>
31 #include <shellio.hxx>
34 #include <acorrect.hxx>
38 using namespace ::com::sun::star
;
40 void SwDoc::ReplaceUserDefinedDocumentProperties(
41 const uno::Reference
<document::XDocumentProperties
>& xSourceDocProps
)
43 OSL_ENSURE(xSourceDocProps
.is(), "null reference");
45 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(
46 GetDocShell()->GetModel(), uno::UNO_QUERY_THROW
);
47 uno::Reference
<document::XDocumentProperties
> xDocProps(
48 xDPS
->getDocumentProperties() );
49 OSL_ENSURE(xDocProps
.is(), "null reference");
51 uno::Reference
<beans::XPropertySet
> xSourceUDSet(
52 xSourceDocProps
->getUserDefinedProperties(), uno::UNO_QUERY_THROW
);
53 uno::Reference
<beans::XPropertyContainer
> xTargetUD(
54 xDocProps
->getUserDefinedProperties());
55 uno::Reference
<beans::XPropertySet
> xTargetUDSet(xTargetUD
,
56 uno::UNO_QUERY_THROW
);
57 const uno::Sequence
<beans::Property
> tgtprops
58 = xTargetUDSet
->getPropertySetInfo()->getProperties();
60 for (const auto& rTgtProp
: tgtprops
) {
62 xTargetUD
->removeProperty(rTgtProp
.Name
);
63 } catch (uno::Exception
&) {
68 uno::Reference
<beans::XPropertySetInfo
> xSetInfo
69 = xSourceUDSet
->getPropertySetInfo();
70 const uno::Sequence
<beans::Property
> srcprops
= xSetInfo
->getProperties();
72 for (const auto& rSrcProp
: srcprops
) {
74 OUString name
= rSrcProp
.Name
;
75 xTargetUD
->addProperty(name
, rSrcProp
.Attributes
,
76 xSourceUDSet
->getPropertyValue(name
));
77 } catch (uno::Exception
&) {
83 void SwDoc::ReplaceDocumentProperties(const SwDoc
& rSource
, bool mailMerge
)
85 uno::Reference
<document::XDocumentPropertiesSupplier
> xSourceDPS(
86 rSource
.GetDocShell()->GetModel(), uno::UNO_QUERY_THROW
);
87 uno::Reference
<document::XDocumentProperties
> xSourceDocProps(
88 xSourceDPS
->getDocumentProperties() );
89 OSL_ENSURE(xSourceDocProps
.is(), "null reference");
91 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS(
92 GetDocShell()->GetModel(), uno::UNO_QUERY_THROW
);
93 uno::Reference
<document::XDocumentProperties
> xDocProps(
94 xDPS
->getDocumentProperties() );
95 OSL_ENSURE(xDocProps
.is(), "null reference");
97 xDocProps
->setAuthor(xSourceDocProps
->getAuthor());
98 xDocProps
->setGenerator(xSourceDocProps
->getGenerator());
99 xDocProps
->setCreationDate(xSourceDocProps
->getCreationDate());
100 xDocProps
->setTitle(xSourceDocProps
->getTitle());
101 xDocProps
->setSubject(xSourceDocProps
->getSubject());
102 xDocProps
->setDescription(xSourceDocProps
->getDescription());
103 xDocProps
->setKeywords(xSourceDocProps
->getKeywords());
104 xDocProps
->setLanguage(xSourceDocProps
->getLanguage());
105 // Note: These below originally weren't copied for mailmerge, but I don't see why not.
106 xDocProps
->setModifiedBy(xSourceDocProps
->getModifiedBy());
107 xDocProps
->setModificationDate(xSourceDocProps
->getModificationDate());
108 xDocProps
->setPrintedBy(xSourceDocProps
->getPrintedBy());
109 xDocProps
->setPrintDate(xSourceDocProps
->getPrintDate());
110 xDocProps
->setTemplateName(xSourceDocProps
->getTemplateName());
111 xDocProps
->setTemplateURL(xSourceDocProps
->getTemplateURL());
112 xDocProps
->setTemplateDate(xSourceDocProps
->getTemplateDate());
113 xDocProps
->setAutoloadURL(xSourceDocProps
->getAutoloadURL());
114 xDocProps
->setAutoloadSecs(xSourceDocProps
->getAutoloadSecs());
115 xDocProps
->setDefaultTarget(xSourceDocProps
->getDefaultTarget());
116 xDocProps
->setDocumentStatistics(xSourceDocProps
->getDocumentStatistics());
117 xDocProps
->setEditingCycles(xSourceDocProps
->getEditingCycles());
118 xDocProps
->setEditingDuration(xSourceDocProps
->getEditingDuration());
120 if( mailMerge
) // Note: Not sure this is needed.
122 // Manually set the creation date, otherwise author field isn't filled
123 // during MM, as it's set when saving the document the first time.
124 xDocProps
->setCreationDate( xSourceDocProps
->getModificationDate() );
127 ReplaceUserDefinedDocumentProperties( xSourceDocProps
);
130 /// inserts an AutoText block
131 bool SwDoc::InsertGlossary( SwTextBlocks
& rBlock
, const OUString
& rEntry
,
132 SwPaM
& rPaM
, SwCursorShell
* pShell
)
135 const sal_uInt16 nIdx
= rBlock
.GetIndex( rEntry
);
136 if( USHRT_MAX
!= nIdx
)
138 bool bSav_IsInsGlossary
= mbInsOnlyTextGlssry
;
139 mbInsOnlyTextGlssry
= rBlock
.IsOnlyTextBlock( nIdx
);
141 if( rBlock
.BeginGetDoc( nIdx
) )
143 SwDoc
* pGDoc
= rBlock
.GetDoc();
145 // tdf#53023 - remove the last empty paragraph (check SwXMLTextBlockParContext dtor)
146 if (mbInsOnlyTextGlssry
)
148 SwPaM
aPaM(*pGDoc
->GetNodes()[pGDoc
->GetNodes().GetEndOfContent().GetIndex() - 1]);
149 pGDoc
->getIDocumentContentOperations().DelFullPara(aPaM
);
152 // Update all fixed fields, with the right DocInfo.
153 // FIXME: UGLY: Because we cannot limit the range in which to do
154 // field updates, we must update the fixed fields at the glossary
156 // To be able to do this, we copy the document properties of the
157 // target document to the glossary document
158 // OSL_ENSURE(GetDocShell(), "no SwDocShell"); // may be clipboard!
159 OSL_ENSURE(pGDoc
->GetDocShell(), "no SwDocShell at glossary");
160 if (GetDocShell() && pGDoc
->GetDocShell())
161 pGDoc
->ReplaceDocumentProperties( *this );
162 pGDoc
->getIDocumentFieldsAccess().SetFixFields(nullptr);
165 getIDocumentFieldsAccess().LockExpFields();
167 SwNodeIndex
aStt( pGDoc
->GetNodes().GetEndOfExtras(), 1 );
168 SwContentNode
* pContentNd
= pGDoc
->GetNodes().GoNext( &aStt
);
169 const SwTableNode
* pTableNd
= pContentNd
->FindTableNode();
170 SwPaM
aCpyPam( pTableNd
? *const_cast<SwNode
*>(static_cast<SwNode
const *>(pTableNd
)) : *static_cast<SwNode
*>(pContentNd
) );
173 // till the nodes array's end
174 aCpyPam
.GetPoint()->Assign( pGDoc
->GetNodes().GetEndOfContent().GetIndex()-SwNodeOffset(1) );
175 pContentNd
= aCpyPam
.GetPointContentNode();
177 aCpyPam
.GetPoint()->SetContent( pContentNd
->Len() );
179 GetIDocumentUndoRedo().StartUndo( SwUndoId::INSGLOSSARY
, nullptr );
180 SwPaM
*_pStartCursor
= &rPaM
, *_pStartCursor2
= _pStartCursor
;
183 SwPosition
& rInsPos
= *_pStartCursor
->GetPoint();
184 SwStartNode
* pBoxSttNd
= const_cast<SwStartNode
*>(rInsPos
.GetNode().
185 FindTableBoxStartNode());
187 if( pBoxSttNd
&& SwNodeOffset(2) == pBoxSttNd
->EndOfSectionIndex() -
188 pBoxSttNd
->GetIndex() &&
189 aCpyPam
.GetPoint()->GetNode() != aCpyPam
.GetMark()->GetNode() )
191 // We copy more than one Node to the current Box.
192 // However, we have to remove the BoxAttributes then.
193 ClearBoxNumAttrs( rInsPos
.GetNode() );
196 SwDontExpandItem aACD
;
197 aACD
.SaveDontExpandItems( rInsPos
);
199 pGDoc
->getIDocumentContentOperations().CopyRange(aCpyPam
, rInsPos
, SwCopyFlags::CheckPosInFly
);
201 aACD
.RestoreDontExpandItems( rInsPos
);
203 pShell
->SaveTableBoxContent( &rInsPos
);
204 } while( (_pStartCursor
= _pStartCursor
->GetNext()) !=
206 GetIDocumentUndoRedo().EndUndo( SwUndoId::INSGLOSSARY
, nullptr );
208 getIDocumentFieldsAccess().UnlockExpFields();
209 if( !getIDocumentFieldsAccess().IsExpFieldsLocked() )
210 getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
213 mbInsOnlyTextGlssry
= bSav_IsInsGlossary
;
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */