tdf#35361 Add a Quick Look plugins for .od* files on macOS
[LibreOffice.git] / sw / source / core / doc / DocumentTimerManager.cxx
blob44df4c33729d7989b261f027e6dfca1f77e09fbf
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 SfxCloseVetoLock lock(m_rDoc.GetDocShell());
163 BlockIdling();
164 StopIdling();
166 IdleJob eJob = GetNextIdleJob();
168 switch ( eJob )
170 case IdleJob::Grammar:
171 m_rDoc.StartGrammarChecking();
172 break;
174 case IdleJob::Layout:
175 for ( auto pLayout : m_rDoc.GetAllLayouts() )
176 if( pLayout->IsIdleFormat() )
178 pLayout->GetCurrShell()->LayoutIdle();
179 break;
181 break;
183 case IdleJob::Fields:
185 SwViewShell* pShell( m_rDoc.getIDocumentLayoutAccess().GetCurrentViewShell() );
186 SwRootFrame* pTmpRoot = m_rDoc.getIDocumentLayoutAccess().GetCurrentLayout();
188 // Action brackets!
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 );
213 break;
216 case IdleJob::Busy:
217 break;
218 case IdleJob::None:
219 break;
222 if ( IdleJob::None != eJob )
223 StartIdling();
224 UnblockIdling();
226 #ifdef TIMELOG
227 if( pModLogFile && 1 != (long)pModLogFile )
228 delete pModLogFile, static_cast<long&>(pModLogFile) = 1;
229 #endif
232 DocumentTimerManager::~DocumentTimerManager() {}
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */