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 <calbck.hxx>
29 #include <fmtfld.hxx>
30 #include <rootfrm.hxx>
31 #include <docufld.hxx>
32 #include <docstat.hxx>
33 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
34 
35 using namespace ::com::sun::star;
36 
37 namespace sw
38 {
39 
41  mpDocStat( new SwDocStat ),
42  mbInitialized( false ),
43  maStatsUpdateIdle( i_rSwdoc )
44 
45 {
46  maStatsUpdateIdle.SetPriority( TaskPriority::LOWEST );
48  maStatsUpdateIdle.SetDebugName( "sw::DocumentStatisticsManager maStatsUpdateIdle" );
49 }
50 
51 void DocumentStatisticsManager::DocInfoChgd(bool const isEnableSetModified)
52 {
55  if (isEnableSetModified)
56  {
58  }
59 }
60 
62 {
63  return *mpDocStat;
64 }
65 
67 {
68  mpDocStat->bModified = bSet;
69 }
70 
71 const SwDocStat& DocumentStatisticsManager::GetUpdatedDocStat( bool bCompleteAsync, bool bFields )
72 {
73  if( mpDocStat->bModified || !mbInitialized)
74  {
75  UpdateDocStat( bCompleteAsync, bFields );
76  }
77  return *mpDocStat;
78 }
79 
81 {
82  *mpDocStat = rStat;
83  mbInitialized = true;
84 }
85 
86 void DocumentStatisticsManager::UpdateDocStat( bool bCompleteAsync, bool bFields )
87 {
88  if( mpDocStat->bModified || !mbInitialized)
89  {
90  if (!bCompleteAsync)
91  {
94  std::numeric_limits<long>::max(), bFields)) {}
95  }
96  else if (IncrementalDocStatCalculate(5000, bFields))
98  else
100  }
101 }
102 
103 // returns true while there is more to do
105 {
106  mbInitialized = true;
107  mpDocStat->Reset();
108  mpDocStat->nPara = 0; // default is 1!
109 
110  // This is the inner loop - at least while the paras are dirty.
111  for( sal_uLong i = m_rDoc.GetNodes().Count(); i > 0 && nChars > 0; )
112  {
113  SwNode* pNd = m_rDoc.GetNodes()[ --i ];
114  switch( pNd->GetNodeType() )
115  {
116  case SwNodeType::Text:
117  {
118  long const nOldChars(mpDocStat->nChar);
119  SwTextNode *pText = static_cast< SwTextNode * >( pNd );
120  if (pText->CountWords(*mpDocStat, 0, pText->GetText().getLength()))
121  {
122  nChars -= (mpDocStat->nChar - nOldChars);
123  }
124  break;
125  }
126  case SwNodeType::Table: ++mpDocStat->nTable; break;
127  case SwNodeType::Grf: ++mpDocStat->nGrf; break;
128  case SwNodeType::Ole: ++mpDocStat->nOLE; break;
129  case SwNodeType::Section: break;
130  default: break;
131  }
132  }
133 
134  // #i93174#: notes contain paragraphs that are not nodes
135  {
137  SwIterator<SwFormatField,SwFieldType> aIter( *pPostits );
138  for( SwFormatField* pFormatField = aIter.First(); pFormatField; pFormatField = aIter.Next() )
139  {
140  if (pFormatField->IsFieldInDoc())
141  {
142  SwPostItField const * const pField(
143  static_cast<SwPostItField const*>(pFormatField->GetField()));
144  mpDocStat->nAllPara += pField->GetNumberOfParagraphs();
145  }
146  }
147  }
148 
150  SetDocStatModified( false );
151 
152  css::uno::Sequence < css::beans::NamedValue > aStat( mpDocStat->nPage ? 8 : 7);
153  sal_Int32 n=0;
154  aStat[n].Name = "TableCount";
155  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nTable);
156  aStat[n].Name = "ImageCount";
157  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nGrf);
158  aStat[n].Name = "ObjectCount";
159  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nOLE);
160  if ( mpDocStat->nPage )
161  {
162  aStat[n].Name = "PageCount";
163  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPage);
164  }
165  aStat[n].Name = "ParagraphCount";
166  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPara);
167  aStat[n].Name = "WordCount";
168  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nWord);
169  aStat[n].Name = "CharacterCount";
170  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nChar);
171  aStat[n].Name = "NonWhitespaceCharacterCount";
172  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nCharExcludingSpaces);
173 
174  // For e.g. autotext documents there is no pSwgInfo (#i79945)
175  SwDocShell* pObjShell(m_rDoc.GetDocShell());
176  if (pObjShell)
177  {
178  const uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
179  pObjShell->GetModel(), uno::UNO_QUERY_THROW);
180  const uno::Reference<document::XDocumentProperties> xDocProps(
181  xDPS->getDocumentProperties());
182  // #i96786#: do not set modified flag when updating statistics
183  const bool bDocWasModified( m_rDoc.getIDocumentState().IsModified() );
184  const ModifyBlocker_Impl b(pObjShell);
185  // rhbz#1081176: don't jump to cursor pos because of (temporary)
186  // activation of modified flag triggering move to input position
187  auto aViewGuard(pObjShell->LockAllViews());
188  xDocProps->setDocumentStatistics(aStat);
189  if (!bDocWasModified)
190  {
192  }
193  }
194 
195  // optionally update stat. fields
196  if (bFields)
197  {
199  pType->UpdateFields();
200  }
201 
202  return nChars < 0;
203 }
204 
205 IMPL_LINK( DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void )
206 {
207  if (IncrementalDocStatCalculate(32000))
208  pIdle->Start();
209  SwView* pView = m_rDoc.GetDocShell() ? m_rDoc.GetDocShell()->GetView() : nullptr;
210  if( pView )
211  pView->UpdateDocStats();
212 }
213 
215 {
217 }
218 
219 }
220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:234
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...
sal_uLong Count() const
Definition: ndarr.hxx:143
sal_Int32 GetNumberOfParagraphs() const
Definition: docufld.cxx:1827
const OUString & GetText() const
Definition: ndtxt.hxx:210
virtual const SwRootFrame * GetCurrentLayout() const =0
SwDocShell * GetDocShell()
Definition: doc.hxx:1342
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
Definition: doc.hxx:185
SwSectionNode is derived from SwStartNode.
bool mbInitialized
Dialog to specify the properties of date form field.
void UpdateFields() const
Definition: fldbas.hxx:272
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:356
SwNodeType GetNodeType() const
Definition: node.hxx:144
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
virtual void Start() override
void UpdateDocStats()
Definition: view2.cxx:1315
virtual bool IsModified() const =0
Changes of document?
int i
void DocInfoChgd(bool isEnableSetModified) override
DocInfo has changed (notify via DocShell): make required fields update.
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
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:404
SwDocShell * GetDocShell()
Definition: view.cxx:1128
const SwDocStat & GetDocStat() const override
Document - Statistics.
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
bool IncrementalDocStatCalculate(long nChars, bool bFields=true)
continue computing a chunk of document statistics
void Stop()
SwNodes & GetNodes()
Definition: doc.hxx:402
SwTableNode is derived from SwStartNode.
void SetInvokeHandler(const Link< Timer *, void > &rLink)
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:1977
void SetPriority(TaskPriority ePriority)
const SwView * GetView() const
Definition: docsh.hxx:220
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:307
Definition: view.hxx:146
void SetDebugName(const sal_Char *pDebugName)
virtual void ResetModified()=0
Base class of the Writer document model elements.
Definition: node.hxx:79