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 <editsh.hxx>
22 #include <fldbas.hxx>
23 #include <docsh.hxx>
25 #include <IDocumentState.hxx>
27 #include <view.hxx>
28 #include <ndtxt.hxx>
29 #include <calbck.hxx>
30 #include <fmtfld.hxx>
31 #include <rootfrm.hxx>
32 #include <docufld.hxx>
33 #include <docstat.hxx>
34 #include <vector>
35 #include <viewsh.hxx>
36 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
37 #include <wrtsh.hxx>
38 #include <viewopt.hxx>
39 
40 using namespace ::com::sun::star;
41 
42 namespace sw
43 {
44 
46  mpDocStat( new SwDocStat ),
47  mbInitialized( false ),
48  maStatsUpdateIdle( i_rSwdoc )
49 
50 {
51  maStatsUpdateIdle.SetPriority( TaskPriority::LOWEST );
53  maStatsUpdateIdle.SetDebugName( "sw::DocumentStatisticsManager maStatsUpdateIdle" );
54 }
55 
56 void DocumentStatisticsManager::DocInfoChgd(bool const isEnableSetModified)
57 {
60  if (isEnableSetModified)
61  {
63  }
64 }
65 
67 {
68  return *mpDocStat;
69 }
70 
72 {
73  mpDocStat->bModified = bSet;
74 }
75 
76 const SwDocStat& DocumentStatisticsManager::GetUpdatedDocStat( bool bCompleteAsync, bool bFields )
77 {
78  if( mpDocStat->bModified || !mbInitialized)
79  {
80  UpdateDocStat( bCompleteAsync, bFields );
81  }
82  return *mpDocStat;
83 }
84 
86 {
87  *mpDocStat = rStat;
88  mbInitialized = true;
89 }
90 
91 void DocumentStatisticsManager::UpdateDocStat( bool bCompleteAsync, bool bFields )
92 {
93  if( mpDocStat->bModified || !mbInitialized)
94  {
95  if (!bCompleteAsync)
96  {
99  std::numeric_limits<long>::max(), bFields)) {}
100  }
101  else if (IncrementalDocStatCalculate(5000, bFields))
103  else
105  }
106 }
107 
108 // returns true while there is more to do
110 {
111  mbInitialized = true;
112  mpDocStat->Reset();
113  mpDocStat->nPara = 0; // default is 1!
114 
115  // This is the inner loop - at least while the paras are dirty.
116  for( sal_uLong i = m_rDoc.GetNodes().Count(); i > 0 && nChars > 0; )
117  {
118  SwNode* pNd = m_rDoc.GetNodes()[ --i ];
119  switch( pNd->GetNodeType() )
120  {
121  case SwNodeType::Text:
122  {
123  long const nOldChars(mpDocStat->nChar);
124  SwTextNode *pText = static_cast< SwTextNode * >( pNd );
125  if (pText->CountWords(*mpDocStat, 0, pText->GetText().getLength()))
126  {
127  nChars -= (mpDocStat->nChar - nOldChars);
128  }
129  break;
130  }
131  case SwNodeType::Table: ++mpDocStat->nTable; break;
132  case SwNodeType::Grf: ++mpDocStat->nGrf; break;
133  case SwNodeType::Ole: ++mpDocStat->nOLE; break;
134  case SwNodeType::Section: break;
135  default: break;
136  }
137  }
138 
139  // #i93174#: notes contain paragraphs that are not nodes
140  {
142  SwIterator<SwFormatField,SwFieldType> aIter( *pPostits );
143  for( SwFormatField* pFormatField = aIter.First(); pFormatField; pFormatField = aIter.Next() )
144  {
145  if (pFormatField->IsFieldInDoc())
146  {
147  SwPostItField const * const pField(
148  static_cast<SwPostItField const*>(pFormatField->GetField()));
149  mpDocStat->nAllPara += pField->GetNumberOfParagraphs();
150  }
151  }
152  }
153 
155  SetDocStatModified( false );
156 
157  css::uno::Sequence < css::beans::NamedValue > aStat( mpDocStat->nPage ? 8 : 7);
158  sal_Int32 n=0;
159  aStat[n].Name = "TableCount";
160  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nTable);
161  aStat[n].Name = "ImageCount";
162  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nGrf);
163  aStat[n].Name = "ObjectCount";
164  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nOLE);
165  if ( mpDocStat->nPage )
166  {
167  aStat[n].Name = "PageCount";
168  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPage);
169  }
170  aStat[n].Name = "ParagraphCount";
171  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPara);
172  aStat[n].Name = "WordCount";
173  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nWord);
174  aStat[n].Name = "CharacterCount";
175  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nChar);
176  aStat[n].Name = "NonWhitespaceCharacterCount";
177  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nCharExcludingSpaces);
178 
179  // For e.g. autotext documents there is no pSwgInfo (#i79945)
180  SwDocShell* pObjShell(m_rDoc.GetDocShell());
181  if (pObjShell)
182  {
183  const uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
184  pObjShell->GetModel(), uno::UNO_QUERY_THROW);
185  const uno::Reference<document::XDocumentProperties> xDocProps(
186  xDPS->getDocumentProperties());
187  // #i96786#: do not set modified flag when updating statistics
188  const bool bDocWasModified( m_rDoc.getIDocumentState().IsModified() );
189  const ModifyBlocker_Impl b(pObjShell);
190  // rhbz#1081176: don't jump to cursor pos because of (temporary)
191  // activation of modified flag triggering move to input position
192  auto aViewGuard(pObjShell->LockAllViews());
193  xDocProps->setDocumentStatistics(aStat);
194  if (!bDocWasModified)
195  {
197  }
198  }
199 
200  // optionally update stat. fields
201  if (bFields)
202  {
204  pType->UpdateFields();
205  }
206 
207  return nChars < 0;
208 }
209 
210 IMPL_LINK( DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void )
211 {
212  if (IncrementalDocStatCalculate(32000))
213  pIdle->Start();
214  SwView* pView = m_rDoc.GetDocShell() ? m_rDoc.GetDocShell()->GetView() : nullptr;
215  if( pView )
216  pView->UpdateDocStats();
217 }
218 
220 {
222 }
223 
224 }
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:233
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:1816
const OUString & GetText() const
Definition: ndtxt.hxx:211
virtual const SwRootFrame * GetCurrentLayout() const =0
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
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.
Definition: accfrmobj.cxx:40
void UpdateFields() const
Definition: fldbas.hxx:271
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:389
SwNodeType GetNodeType() const
Definition: node.hxx:144
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
virtual void Start() override
void UpdateDocStats()
Definition: view2.cxx:1273
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:426
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:437
SwDocShell * GetDocShell()
Definition: view.cxx:1115
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:403
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:1980
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