LibreOffice Module sw (master)  1
DocumentStatisticsManager.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  */
20 #include <doc.hxx>
21 #include <fldbas.hxx>
22 #include <docsh.hxx>
24 #include <IDocumentState.hxx>
26 #include <view.hxx>
27 #include <ndtxt.hxx>
28 #include <fmtfld.hxx>
29 #include <rootfrm.hxx>
30 #include <docufld.hxx>
31 #include <docstat.hxx>
32 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
33 #include <com/sun/star/frame/XModel.hpp>
34 
35 using namespace ::com::sun::star;
36 
37 namespace sw
38 {
39 
41  : m_rDoc( i_rSwdoc ),
42  mpDocStat( new SwDocStat ),
44  maStatsUpdateIdle( i_rSwdoc, "sw::DocumentStatisticsManager maStatsUpdateIdle" )
45 {
46  maStatsUpdateIdle.SetPriority( TaskPriority::LOWEST );
48 }
49 
50 void DocumentStatisticsManager::DocInfoChgd(bool const isEnableSetModified)
51 {
54  if (isEnableSetModified)
55  {
57  }
58 }
59 
61 {
62  return *mpDocStat;
63 }
64 
66 {
67  mpDocStat->bModified = bSet;
68 }
69 
70 const SwDocStat& DocumentStatisticsManager::GetUpdatedDocStat( bool bCompleteAsync, bool bFields )
71 {
72  if( mpDocStat->bModified || !mbInitialized)
73  {
74  UpdateDocStat( bCompleteAsync, bFields );
75  }
76  return *mpDocStat;
77 }
78 
80 {
81  *mpDocStat = rStat;
82  mbInitialized = true;
83 }
84 
85 void DocumentStatisticsManager::UpdateDocStat( bool bCompleteAsync, bool bFields )
86 {
87  if( !mpDocStat->bModified && mbInitialized)
88  return;
89 
90  if (!bCompleteAsync)
91  {
94  std::numeric_limits<tools::Long>::max(), bFields)) {}
95  }
96  else if (IncrementalDocStatCalculate(5000, bFields))
98  else
100 }
101 
102 // returns true while there is more to do
104 {
105  mbInitialized = true;
106  mpDocStat->Reset();
107  mpDocStat->nPara = 0; // default is 1!
108 
109  // This is the inner loop - at least while the paras are dirty.
110  for( SwNodeOffset i = m_rDoc.GetNodes().Count(); i > SwNodeOffset(0) && nChars > 0; )
111  {
112  SwNode* pNd = m_rDoc.GetNodes()[ --i ];
113  switch( pNd->GetNodeType() )
114  {
115  case SwNodeType::Text:
116  {
117  tools::Long const nOldChars(mpDocStat->nChar);
118  SwTextNode *pText = static_cast< SwTextNode * >( pNd );
119  if (pText->CountWords(*mpDocStat, 0, pText->GetText().getLength()))
120  {
121  nChars -= (mpDocStat->nChar - nOldChars);
122  }
123  break;
124  }
125  case SwNodeType::Table: ++mpDocStat->nTable; break;
126  case SwNodeType::Grf: ++mpDocStat->nGrf; break;
127  case SwNodeType::Ole: ++mpDocStat->nOLE; break;
128  case SwNodeType::Section: break;
129  default: break;
130  }
131  }
132 
133  // #i93174#: notes contain paragraphs that are not nodes
134  {
136  std::vector<SwFormatField*> vFields;
137  pPostits->GatherFields(vFields);
138  for(auto pFormatField : vFields)
139  {
140  const auto pField = static_cast<SwPostItField const*>(pFormatField->GetField());
141  mpDocStat->nAllPara += pField->GetNumberOfParagraphs();
142  }
143  }
144 
146  SetDocStatModified( false );
147 
148  css::uno::Sequence < css::beans::NamedValue > aStat( mpDocStat->nPage ? 8 : 7);
149  auto pStat = aStat.getArray();
150  sal_Int32 n=0;
151  pStat[n].Name = "TableCount";
152  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nTable);
153  pStat[n].Name = "ImageCount";
154  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nGrf);
155  pStat[n].Name = "ObjectCount";
156  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nOLE);
157  if ( mpDocStat->nPage )
158  {
159  pStat[n].Name = "PageCount";
160  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPage);
161  }
162  pStat[n].Name = "ParagraphCount";
163  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPara);
164  pStat[n].Name = "WordCount";
165  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nWord);
166  pStat[n].Name = "CharacterCount";
167  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nChar);
168  pStat[n].Name = "NonWhitespaceCharacterCount";
169  pStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nCharExcludingSpaces);
170 
171  // For e.g. autotext documents there is no pSwgInfo (#i79945)
172  SwDocShell* pObjShell(m_rDoc.GetDocShell());
173  if (pObjShell)
174  {
175  const uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
176  pObjShell->GetModel(), uno::UNO_QUERY_THROW);
177  const uno::Reference<document::XDocumentProperties> xDocProps(
178  xDPS->getDocumentProperties());
179  // #i96786#: do not set modified flag when updating statistics
180  const bool bDocWasModified( m_rDoc.getIDocumentState().IsModified() );
181  const ModifyBlocker_Impl b(pObjShell);
182  // rhbz#1081176: don't jump to cursor pos because of (temporary)
183  // activation of modified flag triggering move to input position
184  auto aViewGuard(pObjShell->LockAllViews());
185  xDocProps->setDocumentStatistics(aStat);
186  if (!bDocWasModified)
187  {
189  }
190  }
191 
192  // optionally update stat. fields
193  if (bFields)
194  {
196  pType->UpdateFields();
197  }
198 
199  return nChars < 0;
200 }
201 
202 IMPL_LINK( DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void )
203 {
204  if (IncrementalDocStatCalculate(32000))
205  pIdle->Start();
206  SwView* pView = m_rDoc.GetDocShell() ? m_rDoc.GetDocShell()->GetView() : nullptr;
207  if( pView )
208  pView->UpdateDocStats();
209 }
210 
212 {
214 }
215 
216 }
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:241
const SwDocStat & GetUpdatedDocStat(bool bCompleteAsync, bool bFields) override
Updates the document statistics if the document has been modified and returns a reference to the resu...
const OUString & GetText() const
Definition: ndtxt.hxx:218
virtual const SwRootFrame * GetCurrentLayout() const =0
SwDocShell * GetDocShell()
Definition: doc.hxx:1351
virtual void SetModified()=0
Must be called manually at changes of format.
bool IncrementalDocStatCalculate(tools::Long nChars, bool bFields=true)
continue computing a chunk of document statistics
long Long
sal_Int64 n
Definition: doc.hxx:187
SwSectionNode is derived from SwStartNode.
bool mbInitialized
Dialog to specify the properties of date form field.
void GatherFields(std::vector< SwFormatField * > &rvFormatFields, bool bCollectOnlyInDocNodes=true) const
Definition: fldbas.cxx:203
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
SwDoc & m_rDoc
Definition: docbm.cxx:1201
SwNodeType GetNodeType() const
Definition: node.hxx:146
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
virtual void UpdateFields()
Definition: fldbas.cxx:213
void UpdateDocStats()
Definition: view2.cxx:1541
int i
virtual bool IsModified() const =0
Changes of document?
void DocInfoChgd(bool isEnableSetModified) override
DocInfo has changed (notify via DocShell): make required fields update.
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:394
void UpdateDocStat(bool bCompleteAsync, bool bFields) override
Updates the internal document's statistics.
std::unique_ptr< SwDocStat > mpDocStat
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
const SwDocStat & GetDocStat() const override
Document - Statistics.
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
void Stop()
SwNodes & GetNodes()
Definition: doc.hxx:408
SwTableNode is derived from SwStartNode.
void SetInvokeHandler(const Link< Timer *, void > &rLink)
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
bool CountWords(SwDocStat &rStat, sal_Int32 nStart, sal_Int32 nEnd) const
count words in given range - returns true if we refreshed out count
Definition: txtedt.cxx:2029
SwNodeOffset Count() const
Definition: ndarr.hxx:141
virtual void Start(bool bStartTimer=true) override
void SetPriority(TaskPriority ePriority)
const SwView * GetView() const
Definition: docsh.hxx:221
virtual SwFieldType * GetSysFieldType(const SwFieldIds eWhich) const =0
void SetDocStat(const SwDocStat &rStat) override
Set the document statistics.
sal_uInt16 GetPageNum() const
Definition: rootfrm.hxx:312
Definition: view.hxx:144
bool m_bDetectedRangeSegmentation false
virtual void ResetModified()=0
Base class of the Writer document model elements.
Definition: node.hxx:81