LibreOffice Module sw (master) 1
docglos.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
26
27#include <doc.hxx>
29#include <IDocumentUndoRedo.hxx>
31#include <shellio.hxx>
32#include <pam.hxx>
33#include <swundo.hxx>
34#include <acorrect.hxx>
35#include <crsrsh.hxx>
36#include <docsh.hxx>
37
38using namespace ::com::sun::star;
39
41 const uno::Reference<document::XDocumentProperties>& xSourceDocProps)
42{
43 OSL_ENSURE(xSourceDocProps.is(), "null reference");
44
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");
50
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();
59
60 for (const auto& rTgtProp : tgtprops) {
61 try {
62 xTargetUD->removeProperty(rTgtProp.Name);
63 } catch (uno::Exception &) {
64 // ignore
65 }
66 }
67
68 uno::Reference<beans::XPropertySetInfo> xSetInfo
69 = xSourceUDSet->getPropertySetInfo();
70 const uno::Sequence<beans::Property> srcprops = xSetInfo->getProperties();
71
72 for (const auto& rSrcProp : srcprops) {
73 try {
74 OUString name = rSrcProp.Name;
75 xTargetUD->addProperty(name, rSrcProp.Attributes,
76 xSourceUDSet->getPropertyValue(name));
77 } catch (uno::Exception &) {
78 // ignore
79 }
80 }
81}
82
83void SwDoc::ReplaceDocumentProperties(const SwDoc& rSource, bool mailMerge)
84{
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");
90
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");
96
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());
119
120 if( mailMerge ) // Note: Not sure this is needed.
121 {
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() );
125 }
126
127 ReplaceUserDefinedDocumentProperties( xSourceDocProps );
128}
129
131bool SwDoc::InsertGlossary( SwTextBlocks& rBlock, const OUString& rEntry,
132 SwPaM& rPaM, SwCursorShell* pShell )
133{
134 bool bRet = false;
135 const sal_uInt16 nIdx = rBlock.GetIndex( rEntry );
136 if( USHRT_MAX != nIdx )
137 {
138 bool bSav_IsInsGlossary = mbInsOnlyTextGlssry;
139 mbInsOnlyTextGlssry = rBlock.IsOnlyTextBlock( nIdx );
140
141 if( rBlock.BeginGetDoc( nIdx ) )
142 {
143 SwDoc* pGDoc = rBlock.GetDoc();
144
145 // tdf#53023 - remove the last empty paragraph (check SwXMLTextBlockParContext dtor)
147 {
148 SwPaM aPaM(*pGDoc->GetNodes()[pGDoc->GetNodes().GetEndOfContent().GetIndex() - 1]);
150 }
151
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
155 // entry document.
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);
163
164 // StartAllAction();
166
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) );
171 aCpyPam.SetMark();
172
173 // till the nodes array's end
174 aCpyPam.GetPoint()->Assign( pGDoc->GetNodes().GetEndOfContent().GetIndex()-SwNodeOffset(1) );
175 pContentNd = aCpyPam.GetPointContentNode();
176 if (pContentNd)
177 aCpyPam.GetPoint()->SetContent( pContentNd->Len() );
178
179 GetIDocumentUndoRedo().StartUndo( SwUndoId::INSGLOSSARY, nullptr );
180 SwPaM *_pStartCursor = &rPaM, *_pStartCursor2 = _pStartCursor;
181 do {
182
183 SwPosition& rInsPos = *_pStartCursor->GetPoint();
184 SwStartNode* pBoxSttNd = const_cast<SwStartNode*>(rInsPos.GetNode().
185 FindTableBoxStartNode());
186
187 if( pBoxSttNd && SwNodeOffset(2) == pBoxSttNd->EndOfSectionIndex() -
188 pBoxSttNd->GetIndex() &&
189 aCpyPam.GetPoint()->GetNode() != aCpyPam.GetMark()->GetNode() )
190 {
191 // We copy more than one Node to the current Box.
192 // However, we have to remove the BoxAttributes then.
193 ClearBoxNumAttrs( rInsPos.GetNode() );
194 }
195
196 SwDontExpandItem aACD;
197 aACD.SaveDontExpandItems( rInsPos );
198
200
201 aACD.RestoreDontExpandItems( rInsPos );
202 if( pShell )
203 pShell->SaveTableBoxContent( &rInsPos );
204 } while( (_pStartCursor = _pStartCursor->GetNext()) !=
205 _pStartCursor2 );
206 GetIDocumentUndoRedo().EndUndo( SwUndoId::INSGLOSSARY, nullptr );
207
209 if( !getIDocumentFieldsAccess().IsExpFieldsLocked() )
211 bRet = true;
212 }
213 mbInsOnlyTextGlssry = bSav_IsInsGlossary;
214 }
215 rBlock.EndGetDoc();
216 return bRet;
217}
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
@ CheckPosInFly
check if target position is in fly anchored at source range
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, SwCopyFlags flags) const =0
Copy a selected content range to a position.
virtual bool DelFullPara(SwPaM &)=0
Delete full paragraphs.
virtual void LockExpFields()=0
virtual void SetFixFields(const DateTime *pNewDateTime)=0
virtual void UpdateExpFields(SwTextField *pField, bool bUpdateRefFields)=0
virtual void UnlockExpFields()=0
css::uno::Reference< css::frame::XModel3 > GetModel() const
virtual sal_Int32 Len() const
Definition: node.cxx:1256
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:867
Definition: doc.hxx:197
bool InsertGlossary(SwTextBlocks &rBlock, const OUString &rEntry, SwPaM &rPaM, SwCursorShell *pShell=nullptr)
inserts an AutoText block
Definition: docglos.cxx:131
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
SwNodes & GetNodes()
Definition: doc.hxx:422
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:371
void ReplaceDocumentProperties(const SwDoc &rSource, bool mailMerge=false)
Replace document properties with those from rSource.
Definition: docglos.cxx:83
void ReplaceUserDefinedDocumentProperties(const css::uno::Reference< css::document::XDocumentProperties > &xSourceDocProps)
Replace all user defined document properties with xSourceDocProps.
Definition: docglos.cxx:40
bool mbInsOnlyTextGlssry
Definition: doc.hxx:328
void ClearBoxNumAttrs(SwNode &rNode)
Definition: ndtbl.cxx:4237
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
void RestoreDontExpandItems(const SwPosition &rPos)
Definition: acorrect.cxx:670
void SaveDontExpandItems(const SwPosition &rPos)
Definition: acorrect.cxx:654
Marks a node in the document model.
Definition: ndindex.hxx:31
Base class of the Writer document model elements.
Definition: node.hxx:98
SwNodeOffset GetIndex() const
Definition: node.hxx:312
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:380
SwNodeOffset EndOfSectionIndex() const
Definition: node.hxx:691
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:163
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:165
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1299
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
const SwPosition * GetMark() const
Definition: pam.hxx:255
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:643
SwContentNode * GetPointContentNode() const
Definition: pam.hxx:279
SwPaM * GetNext()
Definition: pam.hxx:314
const SwPosition * GetPoint() const
Definition: pam.hxx:253
Starts a section of nodes in the document model.
Definition: node.hxx:348
SwDoc * GetDoc()
Definition: swblocks.cxx:481
bool BeginGetDoc(sal_uInt16)
Definition: swblocks.cxx:359
bool IsOnlyTextBlock(sal_uInt16) const
Definition: swblocks.cxx:507
sal_uInt16 GetIndex(const OUString &) const
Definition: swblocks.cxx:261
void EndGetDoc()
Definition: swblocks.cxx:379
const char * name
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
Marks a position in the document model.
Definition: pam.hxx:38
SwNode & GetNode() const
Definition: pam.hxx:81
void Assign(const SwNode &rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset=0)
These all set both nNode and nContent.
Definition: pam.cxx:231
void SetContent(sal_Int32 nContentIndex)
Set content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:267