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  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  return;
90 
91  if (!bCompleteAsync)
92  {
95  std::numeric_limits<tools::Long>::max(), bFields)) {}
96  }
97  else if (IncrementalDocStatCalculate(5000, bFields))
99  else
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  tools::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  std::vector<SwFormatField*> vFields;
138  pPostits->GatherFields(vFields);
139  for(auto pFormatField : vFields)
140  {
141  const auto pField = static_cast<SwPostItField const*>(pFormatField->GetField());
142  mpDocStat->nAllPara += pField->GetNumberOfParagraphs();
143  }
144  }
145 
147  SetDocStatModified( false );
148 
149  css::uno::Sequence < css::beans::NamedValue > aStat( mpDocStat->nPage ? 8 : 7);
150  sal_Int32 n=0;
151  aStat[n].Name = "TableCount";
152  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nTable);
153  aStat[n].Name = "ImageCount";
154  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nGrf);
155  aStat[n].Name = "ObjectCount";
156  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nOLE);
157  if ( mpDocStat->nPage )
158  {
159  aStat[n].Name = "PageCount";
160  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPage);
161  }
162  aStat[n].Name = "ParagraphCount";
163  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPara);
164  aStat[n].Name = "WordCount";
165  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nWord);
166  aStat[n].Name = "CharacterCount";
167  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nChar);
168  aStat[n].Name = "NonWhitespaceCharacterCount";
169  aStat[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:240
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:142
const OUString & GetText() const
Definition: ndtxt.hxx:215
virtual const SwRootFrame * GetCurrentLayout() const =0
SwDocShell * GetDocShell()
Definition: doc.hxx:1352
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
bool IncrementalDocStatCalculate(tools::Long nChars, bool bFields=true)
continue computing a chunk of document statistics
long Long
sal_Int64 n
Definition: doc.hxx:188
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:208
void UpdateFields() const
Definition: fldbas.cxx:149
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
SwDoc & m_rDoc
Definition: docbm.cxx:1204
SwNodeType GetNodeType() const
Definition: node.hxx:145
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
void SetDebugName(const char *pDebugName)
void UpdateDocStats()
Definition: view2.cxx:1450
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:409
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:2004
virtual void Start(bool bStartTimer=true) override
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:313
Definition: view.hxx:144
virtual void ResetModified()=0
Base class of the Writer document model elements.
Definition: node.hxx:80