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
35using namespace ::com::sun::star;
36
37namespace sw
38{
39
41 : m_rDoc( i_rSwdoc ),
42 mpDocStat( new SwDocStat ),
43 mbInitialized( false ),
44 maStatsUpdateIdle( i_rSwdoc, "sw::DocumentStatisticsManager maStatsUpdateIdle" )
45{
46 maStatsUpdateIdle.SetPriority( TaskPriority::LOWEST );
48}
49
50void 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
70const 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
85void 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
202IMPL_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: */
bool mbInitialized
virtual SwFieldType * GetSysFieldType(const SwFieldIds eWhich) const =0
virtual const SwRootFrame * GetCurrentLayout() const =0
virtual void ResetModified()=0
virtual void SetModified()=0
Must be called manually at changes of format.
virtual bool IsModified() const =0
Changes of document?
virtual void Start(bool bStartTimer=true) override
css::uno::Reference< css::frame::XModel3 > GetModel() const
const SwView * GetView() const
Definition: docsh.hxx:221
virtual std::unique_ptr< LockAllViewsGuard > LockAllViews() override
Definition: docsh.cxx:372
Definition: doc.hxx:194
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:400
SwNodes & GetNodes()
Definition: doc.hxx:417
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:363
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:411
SwDocShell * GetDocShell()
Definition: doc.hxx:1359
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:242
virtual void UpdateFields()
Definition: fldbas.cxx:213
void GatherFields(std::vector< SwFormatField * > &rvFormatFields, bool bCollectOnlyInDocNodes=true) const
Definition: fldbas.cxx:203
Base class of the Writer document model elements.
Definition: node.hxx:98
SwNodeType GetNodeType() const
Definition: node.hxx:166
SwNodeOffset Count() const
Definition: ndarr.hxx:142
sal_uInt16 GetPageNum() const
Definition: rootfrm.hxx:312
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:111
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:1997
const OUString & GetText() const
Definition: ndtxt.hxx:242
Definition: view.hxx:146
void UpdateDocStats()
Definition: view2.cxx:1612
void SetPriority(TaskPriority ePriority)
void Stop()
void SetInvokeHandler(const Link< Timer *, void > &rLink)
std::unique_ptr< SwDocStat > mpDocStat
const SwDocStat & GetDocStat() const override
Document - Statistics.
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...
void SetDocStat(const SwDocStat &rStat) override
Set the document statistics.
void UpdateDocStat(bool bCompleteAsync, bool bFields) override
Updates the internal document's statistics.
bool IncrementalDocStatCalculate(tools::Long nChars, bool bFields=true)
continue computing a chunk of document statistics
void DocInfoChgd(bool isEnableSetModified) override
DocInfo has changed (notify via DocShell): make required fields update.
SwDoc & m_rDoc
Definition: docbm.cxx:1202
sal_Int64 n
int i
Dialog to specify the properties of date form field.
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
long Long
@ Table
SwTableNode is derived from SwStartNode.
@ Section
SwSectionNode is derived from SwStartNode.