1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <DocumentTimerManager.hxx>
22 #include <DocumentSettingManager.hxx>
23 #include <IDocumentFieldsAccess.hxx>
24 #include <IDocumentLayoutAccess.hxx>
25 #include <rootfrm.hxx>
27 #include <unotools/lingucfg.hxx>
28 #include <unotools/linguprops.hxx>
29 #include <fldupde.hxx>
30 #include <sfx2/progress.hxx>
31 #include <viewopt.hxx>
35 #include <vcl/scheduler.hxx>
36 #include <comphelper/lok.hxx>
41 DocumentTimerManager::DocumentTimerManager(SwDoc
& i_rSwdoc
)
43 , m_nIdleBlockCount(0)
44 , m_bStartOnUnblock(false)
45 , m_aDocIdle(i_rSwdoc
, "sw::DocumentTimerManager m_aDocIdle")
46 , m_aFireIdleJobsTimer("sw::DocumentTimerManager m_aFireIdleJobsTimer")
47 , m_bWaitForLokInit(true)
49 m_aDocIdle
.SetPriority(TaskPriority::LOWEST
);
50 m_aDocIdle
.SetInvokeHandler(LINK(this, DocumentTimerManager
, DoIdleJobs
));
52 m_aFireIdleJobsTimer
.SetInvokeHandler(LINK(this, DocumentTimerManager
, FireIdleJobsTimeout
));
53 m_aFireIdleJobsTimer
.SetTimeout(1000); // Enough time for LOK to render the first tiles.
56 void DocumentTimerManager::StartIdling()
58 if (m_bWaitForLokInit
&& comphelper::LibreOfficeKit::isActive())
60 // Start the idle jobs only after a certain delay.
61 m_bWaitForLokInit
= false;
63 m_aFireIdleJobsTimer
.Start();
67 m_bWaitForLokInit
= false;
68 m_bStartOnUnblock
= true;
69 if (0 == m_nIdleBlockCount
)
71 if (!m_aDocIdle
.IsActive())
78 void DocumentTimerManager::StopIdling()
80 m_bStartOnUnblock
= false;
84 void DocumentTimerManager::BlockIdling()
86 assert(SAL_MAX_UINT32
!= m_nIdleBlockCount
);
90 void DocumentTimerManager::UnblockIdling()
92 assert(0 != m_nIdleBlockCount
);
95 if ((0 == m_nIdleBlockCount
) && m_bStartOnUnblock
)
97 if (!m_aDocIdle
.IsActive())
104 IMPL_LINK(DocumentTimerManager
, FireIdleJobsTimeout
, Timer
*, , void)
106 // Now we can run the idle jobs, assuming we finished LOK initialization.
110 DocumentTimerManager::IdleJob
DocumentTimerManager::GetNextIdleJob() const
112 SwRootFrame
* pTmpRoot
= m_rDoc
.getIDocumentLayoutAccess().GetCurrentLayout();
114 !SfxProgress::GetActiveProgress( m_rDoc
.GetDocShell() ) )
116 SwViewShell
* pShell(m_rDoc
.getIDocumentLayoutAccess().GetCurrentViewShell());
117 for(const SwViewShell
& rSh
: pShell
->GetRingContainer())
118 if( rSh
.ActionPend() )
119 return IdleJob::Busy
;
121 if( pTmpRoot
->IsNeedGrammarCheck() )
123 bool bIsOnlineSpell
= pShell
->GetViewOptions()->IsOnlineSpell();
124 bool bIsAutoGrammar
= false;
125 SvtLinguConfig().GetProperty( UPN_IS_GRAMMAR_AUTO
) >>= bIsAutoGrammar
;
127 if( bIsOnlineSpell
&& bIsAutoGrammar
&& m_rDoc
.StartGrammarChecking( true ) )
128 return IdleJob::Grammar
;
131 // If we're dragging re-layout doesn't occur so avoid a busy loop.
132 if (!pShell
->HasDrawViewDrag())
134 for ( auto pLayout
: m_rDoc
.GetAllLayouts() )
136 if( pLayout
->IsIdleFormat() )
137 return IdleJob::Layout
;
141 SwFieldUpdateFlags nFieldUpdFlag
= m_rDoc
.GetDocumentSettingManager().getFieldUpdateFlags(true);
142 if( ( AUTOUPD_FIELD_ONLY
== nFieldUpdFlag
143 || AUTOUPD_FIELD_AND_CHARTS
== nFieldUpdFlag
)
144 && m_rDoc
.getIDocumentFieldsAccess().GetUpdateFields().IsFieldsDirty() )
146 if( m_rDoc
.getIDocumentFieldsAccess().GetUpdateFields().IsInUpdateFields()
147 || m_rDoc
.getIDocumentFieldsAccess().IsExpFieldsLocked() )
148 return IdleJob::Busy
;
149 return IdleJob::Fields
;
153 return IdleJob::None
;
156 IMPL_LINK_NOARG( DocumentTimerManager
, DoIdleJobs
, Timer
*, void )
159 static ::rtl::Logfile
* pModLogFile
= new ::rtl::Logfile( "First DoIdleJobs" );
161 SfxCloseVetoLock
lock(m_rDoc
.GetDocShell());
166 IdleJob eJob
= GetNextIdleJob();
170 case IdleJob::Grammar
:
171 m_rDoc
.StartGrammarChecking();
174 case IdleJob::Layout
:
175 for ( auto pLayout
: m_rDoc
.GetAllLayouts() )
176 if( pLayout
->IsIdleFormat() )
178 pLayout
->GetCurrShell()->LayoutIdle();
183 case IdleJob::Fields
:
185 SwViewShell
* pShell( m_rDoc
.getIDocumentLayoutAccess().GetCurrentViewShell() );
186 SwRootFrame
* pTmpRoot
= m_rDoc
.getIDocumentLayoutAccess().GetCurrentLayout();
189 m_rDoc
.getIDocumentFieldsAccess().GetUpdateFields().SetInUpdateFields( true );
191 pTmpRoot
->StartAllAction();
193 // no jump on update of fields #i85168#
194 const bool bOldLockView
= pShell
->IsViewLocked();
195 pShell
->LockView( true );
197 auto pChapterFieldType
= m_rDoc
.getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Chapter
);
198 pChapterFieldType
->CallSwClientNotify(sw::LegacyModifyHint( nullptr, nullptr )); // ChapterField
199 m_rDoc
.getIDocumentFieldsAccess().UpdateExpFields( nullptr, false ); // Updates ExpressionFields
200 m_rDoc
.getIDocumentFieldsAccess().UpdateTableFields(nullptr); // Tables
201 m_rDoc
.getIDocumentFieldsAccess().UpdateRefFields(); // References
203 // Validate and update the paragraph signatures.
204 if (SwEditShell
* pSh
= m_rDoc
.GetEditShell())
205 pSh
->ValidateAllParagraphSignatures(true);
207 pTmpRoot
->EndAllAction();
209 pShell
->LockView( bOldLockView
);
211 m_rDoc
.getIDocumentFieldsAccess().GetUpdateFields().SetInUpdateFields( false );
212 m_rDoc
.getIDocumentFieldsAccess().GetUpdateFields().SetFieldsDirty( false );
222 if ( IdleJob::None
!= eJob
)
227 if( pModLogFile
&& 1 != (long)pModLogFile
)
228 delete pModLogFile
, static_cast<long&>(pModLogFile
) = 1;
232 DocumentTimerManager::~DocumentTimerManager() {}
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */