android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / txtnode / OnlineAccessibilityCheck.cxx
blob00ccea620948cc0d2eb42bf2a42b947d9ec89af5
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 .
20 #include <OnlineAccessibilityCheck.hxx>
21 #include <doc.hxx>
22 #include <pam.hxx>
23 #include <txtfrm.hxx>
24 #include <sfx2/bindings.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <docsh.hxx>
27 #include <cmdid.h>
28 #include <officecfg/Office/Common.hxx>
29 #include <unotools/configmgr.hxx>
31 namespace sw
33 WeakNodeContainer::WeakNodeContainer(SwNode* pNode)
34 : m_pNode(pNode)
36 if (m_pNode)
38 auto* pBroadcast = dynamic_cast<sw::BroadcastingModify*>(m_pNode);
39 if (pBroadcast)
41 EndListeningAll();
42 StartListening(pBroadcast->GetNotifier());
44 else
46 m_pNode = nullptr;
51 WeakNodeContainer::~WeakNodeContainer() { EndListeningAll(); }
53 bool WeakNodeContainer::isAlive()
55 if (!m_pNode)
56 return false;
57 if (!HasBroadcaster())
58 m_pNode = nullptr;
59 return m_pNode;
62 SwNode* WeakNodeContainer::getNode()
64 if (isAlive())
65 return m_pNode;
66 return nullptr;
69 OnlineAccessibilityCheck::OnlineAccessibilityCheck(SwDoc& rDocument)
70 : m_rDocument(rDocument)
71 , m_aAccessibilityCheck(&m_rDocument)
72 , m_nPreviousNodeIndex(-1)
73 , m_nAccessibilityIssues(0)
74 , m_bInitialCheck(false)
75 , m_bOnlineCheckStatus(
76 !utl::ConfigManager::IsFuzzing()
77 ? officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get()
78 : false)
82 void OnlineAccessibilityCheck::updateNodeStatus(SwNode* pNode, bool bIssueObjectNameChanged)
84 if (!pNode->IsContentNode() && !pNode->IsTableNode())
85 return;
87 m_nAccessibilityIssues = 0;
89 if (bIssueObjectNameChanged)
90 return;
92 auto it = m_aNodes.find(pNode);
93 if (it == m_aNodes.end())
95 m_aNodes.emplace(pNode, std::make_unique<WeakNodeContainer>(pNode));
98 for (auto iterator = m_aNodes.begin(); iterator != m_aNodes.end();)
100 auto& pWeakContentNode = iterator->second;
101 if (pWeakContentNode->isAlive())
103 auto& rStatus = pWeakContentNode->getNode()->getAccessibilityCheckStatus();
104 if (rStatus.pCollection)
106 m_nAccessibilityIssues += rStatus.pCollection->getIssues().size();
107 ++iterator;
109 else
111 iterator = m_aNodes.erase(iterator);
114 else
116 iterator = m_aNodes.erase(iterator);
121 void OnlineAccessibilityCheck::updateStatusbar()
123 SfxBindings* pBindings = m_rDocument.GetDocShell() && m_rDocument.GetDocShell()->GetDispatcher()
124 ? m_rDocument.GetDocShell()->GetDispatcher()->GetBindings()
125 : nullptr;
126 if (pBindings)
127 pBindings->Invalidate(FN_STAT_ACCESSIBILITY_CHECK);
130 void OnlineAccessibilityCheck::runAccessibilityCheck(SwNode* pNode)
132 m_aAccessibilityCheck.getIssueCollection().clear();
134 m_aAccessibilityCheck.checkNode(pNode);
136 for (SwFrameFormat* const& pFrameFormat : pNode->GetAnchoredFlys())
138 SdrObject* pObject = pFrameFormat->FindSdrObject();
139 if (pObject)
140 m_aAccessibilityCheck.checkObject(pNode, pObject);
143 auto aCollection = m_aAccessibilityCheck.getIssueCollection();
145 pNode->getAccessibilityCheckStatus().pCollection
146 = std::make_unique<sfx::AccessibilityIssueCollection>(aCollection);
149 void OnlineAccessibilityCheck::runDocumentLevelAccessibilityCheck()
151 m_aAccessibilityCheck.getIssueCollection().clear();
152 m_aAccessibilityCheck.checkDocumentProperties();
153 auto aCollection = m_aAccessibilityCheck.getIssueCollection();
154 m_pDocumentAccessibilityIssues
155 = std::make_unique<sfx::AccessibilityIssueCollection>(aCollection);
158 void OnlineAccessibilityCheck::initialCheck()
160 if (m_bInitialCheck)
161 return;
163 runDocumentLevelAccessibilityCheck();
165 auto const& pNodes = m_rDocument.GetNodes();
166 for (SwNodeOffset n(0); n < pNodes.Count(); ++n)
168 SwNode* pNode = pNodes[n];
169 if (pNode)
171 runAccessibilityCheck(pNode);
172 updateNodeStatus(pNode);
176 updateStatusbar();
178 m_bInitialCheck = true;
181 void OnlineAccessibilityCheck::updateCheckerActivity()
183 bool bOnlineCheckStatus
184 = !utl::ConfigManager::IsFuzzing()
185 && officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
187 if (bOnlineCheckStatus != m_bOnlineCheckStatus)
189 m_pPreviousNode.reset();
190 m_nPreviousNodeIndex = SwNodeOffset(-1);
191 m_bInitialCheck = false; // force initial check
193 if (!bOnlineCheckStatus)
195 clearAccessibilityIssuesFromAllNodes(); // cleanup all accessibility check data on nodes
196 m_nAccessibilityIssues = -1;
198 else
200 m_nAccessibilityIssues = 0;
203 m_bOnlineCheckStatus = bOnlineCheckStatus;
205 updateStatusbar();
209 void OnlineAccessibilityCheck::update(const SwPosition& rNewPos)
211 updateCheckerActivity();
213 if (!m_bOnlineCheckStatus)
214 return;
216 initialCheck();
218 lookForPreviousNodeAndUpdate(rNewPos);
221 void OnlineAccessibilityCheck::lookForPreviousNodeAndUpdate(const SwPosition& rNewPos)
223 auto nCurrenNodeIndex = rNewPos.GetNodeIndex();
224 auto* pCurrentNode = &rNewPos.GetNode();
226 if (!pCurrentNode->IsContentNode() && !pCurrentNode->IsTableNode())
227 return;
229 auto pCurrentWeak = std::make_unique<WeakNodeContainer>(pCurrentNode);
230 if (!pCurrentWeak->isAlive())
231 return;
233 // Check if previous node was deleted
234 if (!m_pPreviousNode || !m_pPreviousNode->isAlive())
236 m_pPreviousNode = std::move(pCurrentWeak);
237 m_nPreviousNodeIndex = nCurrenNodeIndex;
238 return;
241 // Check if node index changed
242 if (nCurrenNodeIndex == m_nPreviousNodeIndex)
243 return;
245 // Check if previous node is valid
246 if (m_nPreviousNodeIndex < SwNodeOffset(0)
247 || m_nPreviousNodeIndex >= pCurrentNode->GetNodes().Count())
249 m_pPreviousNode = std::move(pCurrentWeak);
250 m_nPreviousNodeIndex = nCurrenNodeIndex;
251 return;
254 // Run the docement level Accessibility Check
255 runDocumentLevelAccessibilityCheck();
257 // Get the real previous node from index
258 SwNode* pNode = pCurrentNode->GetNodes()[m_nPreviousNodeIndex];
260 if (pNode && (pNode->IsContentNode() || pNode->IsTableNode()))
262 runDocumentLevelAccessibilityCheck();
263 runAccessibilityCheck(pNode);
264 updateNodeStatus(pNode);
266 // Assign previous node and index
267 m_pPreviousNode = std::move(pCurrentWeak);
268 m_nPreviousNodeIndex = nCurrenNodeIndex;
270 else
272 runAccessibilityCheck(pCurrentNode);
273 updateNodeStatus(pCurrentNode);
275 m_pPreviousNode.reset();
276 m_nPreviousNodeIndex = SwNodeOffset(-1);
279 updateStatusbar();
282 void OnlineAccessibilityCheck::clearAccessibilityIssuesFromAllNodes()
284 auto const& pNodes = m_rDocument.GetNodes();
285 for (SwNodeOffset n(0); n < pNodes.Count(); ++n)
287 SwNode* pNode = pNodes[n];
288 if (pNode)
290 pNode->getAccessibilityCheckStatus().reset();
294 m_aNodes.clear();
295 updateStatusbar();
298 void OnlineAccessibilityCheck::resetAndQueue(SwNode* pNode, bool bIssueObjectNameChanged)
300 if (utl::ConfigManager::IsFuzzing())
301 return;
303 bool bOnlineCheckStatus
304 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
305 if (!bOnlineCheckStatus)
306 return;
308 pNode->getAccessibilityCheckStatus().reset();
309 m_aNodes.erase(pNode);
310 if (&pNode->GetNodes() == &m_rDocument.GetNodes()) // don't add undo array
312 runAccessibilityCheck(pNode);
313 updateNodeStatus(pNode, bIssueObjectNameChanged);
315 updateStatusbar();
318 void OnlineAccessibilityCheck::resetAndQueueDocumentLevel()
320 if (utl::ConfigManager::IsFuzzing())
321 return;
323 bool bOnlineCheckStatus
324 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
325 if (!bOnlineCheckStatus)
326 return;
328 runDocumentLevelAccessibilityCheck();
329 updateStatusbar();
332 } // end sw
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */