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 #include <com/sun/star/frame/XModel.hpp>
35 
36 using namespace ::com::sun::star;
37 
38 namespace sw
39 {
40 
42  mpDocStat( new SwDocStat ),
43  mbInitialized( false ),
44  maStatsUpdateIdle( i_rSwdoc )
45 
46 {
47  maStatsUpdateIdle.SetPriority( TaskPriority::LOWEST );
49  maStatsUpdateIdle.SetDebugName( "sw::DocumentStatisticsManager maStatsUpdateIdle" );
50 }
51 
52 void DocumentStatisticsManager::DocInfoChgd(bool const isEnableSetModified)
53 {
56  if (isEnableSetModified)
57  {
59  }
60 }
61 
63 {
64  return *mpDocStat;
65 }
66 
68 {
69  mpDocStat->bModified = bSet;
70 }
71 
72 const SwDocStat& DocumentStatisticsManager::GetUpdatedDocStat( bool bCompleteAsync, bool bFields )
73 {
74  if( mpDocStat->bModified || !mbInitialized)
75  {
76  UpdateDocStat( bCompleteAsync, bFields );
77  }
78  return *mpDocStat;
79 }
80 
82 {
83  *mpDocStat = rStat;
84  mbInitialized = true;
85 }
86 
87 void DocumentStatisticsManager::UpdateDocStat( bool bCompleteAsync, bool bFields )
88 {
89  if( mpDocStat->bModified || !mbInitialized)
90  {
91  if (!bCompleteAsync)
92  {
95  std::numeric_limits<long>::max(), bFields)) {}
96  }
97  else if (IncrementalDocStatCalculate(5000, bFields))
99  else
101  }
102 }
103 
104 // returns true while there is more to do
106 {
107  mbInitialized = true;
108  mpDocStat->Reset();
109  mpDocStat->nPara = 0; // default is 1!
110 
111  // This is the inner loop - at least while the paras are dirty.
112  for( sal_uLong i = m_rDoc.GetNodes().Count(); i > 0 && nChars > 0; )
113  {
114  SwNode* pNd = m_rDoc.GetNodes()[ --i ];
115  switch( pNd->GetNodeType() )
116  {
117  case SwNodeType::Text:
118  {
119  long const nOldChars(mpDocStat->nChar);
120  SwTextNode *pText = static_cast< SwTextNode * >( pNd );
121  if (pText->CountWords(*mpDocStat, 0, pText->GetText().getLength()))
122  {
123  nChars -= (mpDocStat->nChar - nOldChars);
124  }
125  break;
126  }
127  case SwNodeType::Table: ++mpDocStat->nTable; break;
128  case SwNodeType::Grf: ++mpDocStat->nGrf; break;
129  case SwNodeType::Ole: ++mpDocStat->nOLE; break;
130  case SwNodeType::Section: break;
131  default: break;
132  }
133  }
134 
135  // #i93174#: notes contain paragraphs that are not nodes
136  {
138  std::vector<SwFormatField*> vFields;
139  pPostits->GatherFields(vFields);
140  for(auto pFormatField : vFields)
141  {
142  const auto pField = static_cast<SwPostItField const*>(pFormatField->GetField());
143  mpDocStat->nAllPara += pField->GetNumberOfParagraphs();
144  }
145  }
146 
148  SetDocStatModified( false );
149 
150  css::uno::Sequence < css::beans::NamedValue > aStat( mpDocStat->nPage ? 8 : 7);
151  sal_Int32 n=0;
152  aStat[n].Name = "TableCount";
153  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nTable);
154  aStat[n].Name = "ImageCount";
155  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nGrf);
156  aStat[n].Name = "ObjectCount";
157  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nOLE);
158  if ( mpDocStat->nPage )
159  {
160  aStat[n].Name = "PageCount";
161  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPage);
162  }
163  aStat[n].Name = "ParagraphCount";
164  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nPara);
165  aStat[n].Name = "WordCount";
166  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nWord);
167  aStat[n].Name = "CharacterCount";
168  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nChar);
169  aStat[n].Name = "NonWhitespaceCharacterCount";
170  aStat[n++].Value <<= static_cast<sal_Int32>(mpDocStat->nCharExcludingSpaces);
171 
172  // For e.g. autotext documents there is no pSwgInfo (#i79945)
173  SwDocShell* pObjShell(m_rDoc.GetDocShell());
174  if (pObjShell)
175  {
176  const uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
177  pObjShell->GetModel(), uno::UNO_QUERY_THROW);
178  const uno::Reference<document::XDocumentProperties> xDocProps(
179  xDPS->getDocumentProperties());
180  // #i96786#: do not set modified flag when updating statistics
181  const bool bDocWasModified( m_rDoc.getIDocumentState().IsModified() );
182  const ModifyBlocker_Impl b(pObjShell);
183  // rhbz#1081176: don't jump to cursor pos because of (temporary)
184  // activation of modified flag triggering move to input position
185  auto aViewGuard(pObjShell->LockAllViews());
186  xDocProps->setDocumentStatistics(aStat);
187  if (!bDocWasModified)
188  {
190  }
191  }
192 
193  // optionally update stat. fields
194  if (bFields)
195  {
197  pType->UpdateFields();
198  }
199 
200  return nChars < 0;
201 }
202 
203 IMPL_LINK( DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void )
204 {
205  if (IncrementalDocStatCalculate(32000))
206  pIdle->Start();
207  SwView* pView = m_rDoc.GetDocShell() ? m_rDoc.GetDocShell()->GetView() : nullptr;
208  if( pView )
209  pView->UpdateDocStats();
210 }
211 
213 {
215 }
216 
217 }
218 /* 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:211
virtual const SwRootFrame * GetCurrentLayout() const =0
SwDocShell * GetDocShell()
Definition: doc.hxx:1350
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
sal_Int64 n
Definition: doc.hxx:186
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:193
void UpdateFields() const
Definition: fldbas.hxx:285
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 SetDebugName(const char *pDebugName)
void UpdateDocStats()
Definition: view2.cxx:1315
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:393
void UpdateDocStat(bool bCompleteAsync, bool bFields) override
Updates the internal document's statistics.
std::unique_ptr< SwDocStat > mpDocStat
PDFDocument & m_rDoc
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:404
const SwDocStat & GetDocStat() const override
Document - Statistics.
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
bool IncrementalDocStatCalculate(long nChars, bool bFields=true)
continue computing a chunk of document statistics
void Stop()
SwNodes & GetNodes()
Definition: doc.hxx:405
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:1971
void SetPriority(TaskPriority ePriority)
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
virtual void ResetModified()=0
Base class of the Writer document model elements.
Definition: node.hxx:79