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 
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 
38 using 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 
83 void 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 
131 bool 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  // Update all fixed fields, with the right DocInfo.
146  // FIXME: UGLY: Because we cannot limit the range in which to do
147  // field updates, we must update the fixed fields at the glossary
148  // entry document.
149  // To be able to do this, we copy the document properties of the
150  // target document to the glossary document
151  // OSL_ENSURE(GetDocShell(), "no SwDocShell"); // may be clipboard!
152  OSL_ENSURE(pGDoc->GetDocShell(), "no SwDocShell at glossary");
153  if (GetDocShell() && pGDoc->GetDocShell())
154  pGDoc->ReplaceDocumentProperties( *this );
155  pGDoc->getIDocumentFieldsAccess().SetFixFields(nullptr);
156 
157  // StartAllAction();
159 
160  SwNodeIndex aStt( pGDoc->GetNodes().GetEndOfExtras(), 1 );
161  SwContentNode* pContentNd = pGDoc->GetNodes().GoNext( &aStt );
162  const SwTableNode* pTableNd = pContentNd->FindTableNode();
163  SwPaM aCpyPam( pTableNd ? *const_cast<SwNode*>(static_cast<SwNode const *>(pTableNd)) : *static_cast<SwNode*>(pContentNd) );
164  aCpyPam.SetMark();
165 
166  // till the nodes array's end
167  aCpyPam.GetPoint()->nNode = pGDoc->GetNodes().GetEndOfContent().GetIndex()-1;
168  pContentNd = aCpyPam.GetContentNode();
169  aCpyPam.GetPoint()->nContent.Assign(
170  pContentNd, pContentNd ? pContentNd->Len() : 0 );
171 
173  SwPaM *_pStartCursor = &rPaM, *_pStartCursor2 = _pStartCursor;
174  do {
175 
176  SwPosition& rInsPos = *_pStartCursor->GetPoint();
177  SwStartNode* pBoxSttNd = const_cast<SwStartNode*>(rInsPos.nNode.GetNode().
178  FindTableBoxStartNode());
179 
180  if( pBoxSttNd && 2 == pBoxSttNd->EndOfSectionIndex() -
181  pBoxSttNd->GetIndex() &&
182  aCpyPam.GetPoint()->nNode != aCpyPam.GetMark()->nNode )
183  {
184  // We copy more than one Node to the current Box.
185  // However, we have to remove the BoxAttributes then.
186  ClearBoxNumAttrs( rInsPos.nNode );
187  }
188 
189  SwDontExpandItem aACD;
190  aACD.SaveDontExpandItems( rInsPos );
191 
192  pGDoc->getIDocumentContentOperations().CopyRange( aCpyPam, rInsPos, /*bCopyAll=*/false, /*bCheckPos=*/true );
193 
194  aACD.RestoreDontExpandItems( rInsPos );
195  if( pShell )
196  pShell->SaveTableBoxContent( &rInsPos );
197  } while( (_pStartCursor = _pStartCursor->GetNext()) !=
198  _pStartCursor2 );
200 
202  if( !getIDocumentFieldsAccess().IsExpFieldsLocked() )
203  getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
204  bRet = true;
205  }
206  mbInsOnlyTextGlssry = bSav_IsInsGlossary;
207  }
208  rBlock.EndGetDoc();
209  return bRet;
210 }
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void EndGetDoc()
Definition: swblocks.cxx:374
Starts a section of nodes in the document model.
Definition: node.hxx:303
sal_uLong GetIndex() const
Definition: node.hxx:282
bool IsOnlyTextBlock(sal_uInt16) const
Definition: swblocks.cxx:502
Marks a position in the document model.
Definition: pam.hxx:35
bool mbInsOnlyTextGlssry
Definition: doc.hxx:310
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:868
SwDocShell * GetDocShell()
Definition: doc.hxx:1342
SwNodeIndex nNode
Definition: pam.hxx:37
const SwPosition * GetMark() const
Definition: pam.hxx:209
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
Definition: doc.hxx:185
SwNode & GetNode() const
Definition: ndindex.hxx:119
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
css::uno::Reference< css::frame::XModel > GetModel() const
void ReplaceUserDefinedDocumentProperties(const css::uno::Reference< css::document::XDocumentProperties > &xSourceDocProps)
Replace all user defined document properties with xSourceDocProps.
Definition: docglos.cxx:40
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
void ClearBoxNumAttrs(const SwNodeIndex &rNode)
Definition: ndtbl.cxx:4190
virtual void LockExpFields()=0
SwContentNode * GetContentNode(bool bPoint=true) const
Definition: pam.hxx:229
virtual void UpdateExpFields(SwTextField *pField, bool bUpdateRefFields)=0
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:356
SwIndex nContent
Definition: pam.hxx:38
void RestoreDontExpandItems(const SwPosition &rPos)
Definition: acorrect.cxx:572
bool InsertGlossary(SwTextBlocks &rBlock, const OUString &rEntry, SwPaM &rPaM, SwCursorShell *pShell=nullptr)
inserts an AutoText block
Definition: docglos.cxx:131
void SaveDontExpandItems(const SwPosition &rPos)
Definition: acorrect.cxx:556
bool BeginGetDoc(sal_uInt16)
Definition: swblocks.cxx:354
SwPaM * GetNext()
Definition: pam.hxx:264
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:164
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
const SwPosition * GetPoint() const
Definition: pam.hxx:207
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
Marks a node in the document model.
Definition: ndindex.hxx:31
SwDoc * GetDoc()
Definition: swblocks.cxx:476
sal_uLong EndOfSectionIndex() const
Definition: node.hxx:677
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
sal_uInt16 GetIndex(const OUString &) const
Definition: swblocks.cxx:266
virtual void SetFixFields(const DateTime *pNewDateTime)=0
SwNodes & GetNodes()
Definition: doc.hxx:402
const char * name
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:349
void ReplaceDocumentProperties(const SwDoc &rSource, bool mailMerge=false)
Replace document properties with those from rSource.
Definition: docglos.cxx:83
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:455
virtual void UnlockExpFields()=0
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:162
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1271
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, const bool bCopyAll, bool bCheckPos) const =0
Copy a selected content range to a position.