android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / doc / DocumentTimerManager.cxx
blob13f85a2026c898bac72a78e0da7129af7b1b5373
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <doc.hxx>
22 #include <DocumentSettingManager.hxx>
23 #include <IDocumentFieldsAccess.hxx>
24 #include <IDocumentLayoutAccess.hxx>
25 #include <rootfrm.hxx>
26 #include <viewsh.hxx>
27 #include <unotools/lingucfg.hxx>
28 #include <unotools/linguprops.hxx>
29 #include <fldupde.hxx>
30 #include <sfx2/progress.hxx>
31 #include <viewopt.hxx>
32 #include <docsh.hxx>
33 #include <docfld.hxx>
34 #include <fldbas.hxx>
35 #include <vcl/scheduler.hxx>
36 #include <comphelper/lok.hxx>
37 #include <editsh.hxx>
39 namespace sw
41 DocumentTimerManager::DocumentTimerManager(SwDoc& i_rSwdoc)
42 : m_rDoc(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;
62 StopIdling();
63 m_aFireIdleJobsTimer.Start();
64 return;
67 m_bWaitForLokInit = false;
68 m_bStartOnUnblock = true;
69 if (0 == m_nIdleBlockCount)
71 if (!m_aDocIdle.IsActive())
72 m_aDocIdle.Start();
73 else
74 Scheduler::Wakeup();
78 void DocumentTimerManager::StopIdling()
80 m_bStartOnUnblock = false;
81 m_aDocIdle.Stop();
84 void DocumentTimerManager::BlockIdling()
86 assert(SAL_MAX_UINT32 != m_nIdleBlockCount);
87 ++m_nIdleBlockCount;
90 void DocumentTimerManager::UnblockIdling()
92 assert(0 != m_nIdleBlockCount);
93 --m_nIdleBlockCount;
95 if ((0 == m_nIdleBlockCount) && m_bStartOnUnblock)
97 if (!m_aDocIdle.IsActive())
98 m_aDocIdle.Start();
99 else
100 Scheduler::Wakeup();
104 IMPL_LINK(DocumentTimerManager, FireIdleJobsTimeout, Timer*, , void)
106 // Now we can run the idle jobs, assuming we finished LOK initialization.
107 StartIdling();
110 DocumentTimerManager::IdleJob DocumentTimerManager::GetNextIdleJob() const
112 SwRootFrame* pTmpRoot = m_rDoc.getIDocumentLayoutAccess().GetCurrentLayout();
113 if( pTmpRoot &&
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 )
158 #ifdef TIMELOG
159 static ::rtl::Logfile* pModLogFile = new ::rtl::Logfile( "First DoIdleJobs" );
160 #endif
161 BlockIdling();
162 StopIdling();
164 IdleJob eJob = GetNextIdleJob();
166 switch ( eJob )
168 case IdleJob::Grammar:
169 m_rDoc.StartGrammarChecking();
170 break;
172 case IdleJob::Layout:
173 for ( auto pLayout : m_rDoc.GetAllLayouts() )
174 if( pLayout->IsIdleFormat() )
176 pLayout->GetCurrShell()->LayoutIdle();
177 break;
179 break;
181 case IdleJob::Fields:
183 SwViewShell* pShell( m_rDoc.getIDocumentLayoutAccess().GetCurrentViewShell() );
184 SwRootFrame* pTmpRoot = m_rDoc.getIDocumentLayoutAccess().GetCurrentLayout();
186 // Action brackets!
187 m_rDoc.getIDocumentFieldsAccess().GetUpdateFields().SetInUpdateFields( true );
189 pTmpRoot->StartAllAction();
191 // no jump on update of fields #i85168#
192 const bool bOldLockView = pShell->IsViewLocked();
193 pShell->LockView( true );
195 auto pChapterFieldType = m_rDoc.getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Chapter );
196 pChapterFieldType->CallSwClientNotify(sw::LegacyModifyHint( nullptr, nullptr )); // ChapterField
197 m_rDoc.getIDocumentFieldsAccess().UpdateExpFields( nullptr, false ); // Updates ExpressionFields
198 m_rDoc.getIDocumentFieldsAccess().UpdateTableFields(nullptr); // Tables
199 m_rDoc.getIDocumentFieldsAccess().UpdateRefFields(); // References
201 // Validate and update the paragraph signatures.
202 if (SwEditShell* pSh = m_rDoc.GetEditShell())
203 pSh->ValidateAllParagraphSignatures(true);
205 pTmpRoot->EndAllAction();
207 pShell->LockView( bOldLockView );
209 m_rDoc.getIDocumentFieldsAccess().GetUpdateFields().SetInUpdateFields( false );
210 m_rDoc.getIDocumentFieldsAccess().GetUpdateFields().SetFieldsDirty( false );
211 break;
214 case IdleJob::Busy:
215 break;
216 case IdleJob::None:
217 break;
220 if ( IdleJob::None != eJob )
221 StartIdling();
222 UnblockIdling();
224 #ifdef TIMELOG
225 if( pModLogFile && 1 != (long)pModLogFile )
226 delete pModLogFile, static_cast<long&>(pModLogFile) = 1;
227 #endif
230 DocumentTimerManager::~DocumentTimerManager() {}
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */