tdf#35361 Add a Quick Look plugins for .od* files on macOS
[LibreOffice.git] / sw / source / core / txtnode / OnlineAccessibilityCheck.cxx
blobd861084d7018cfc9819199aac8c4ec2a882459a0
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 !comphelper::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 if (SwDocShell* pShell = m_rDocument.GetDocShell())
125 if (SfxDispatcher* pDispatcher = pShell->GetDispatcher())
127 if (SfxBindings* pBindings = pDispatcher->GetBindings())
129 pBindings->Invalidate(FN_STAT_ACCESSIBILITY_CHECK);
135 void OnlineAccessibilityCheck::runAccessibilityCheck(SwNode* pNode)
137 m_aAccessibilityCheck.getIssueCollection().clear();
139 m_aAccessibilityCheck.checkNode(pNode);
141 for (SwFrameFormat* const& pFrameFormat : pNode->GetAnchoredFlys())
143 SdrObject* pObject = pFrameFormat->FindSdrObject();
144 if (pObject)
145 m_aAccessibilityCheck.checkObject(pNode, pObject);
148 auto aCollection = m_aAccessibilityCheck.getIssueCollection();
150 pNode->getAccessibilityCheckStatus().pCollection
151 = std::make_unique<sfx::AccessibilityIssueCollection>(aCollection);
154 void OnlineAccessibilityCheck::runDocumentLevelAccessibilityCheck()
156 m_aAccessibilityCheck.getIssueCollection().clear();
157 m_aAccessibilityCheck.checkDocumentProperties();
158 auto aCollection = m_aAccessibilityCheck.getIssueCollection();
159 m_pDocumentAccessibilityIssues
160 = std::make_unique<sfx::AccessibilityIssueCollection>(aCollection);
163 void OnlineAccessibilityCheck::initialCheck()
165 if (m_bInitialCheck)
166 return;
168 runDocumentLevelAccessibilityCheck();
170 auto const& pNodes = m_rDocument.GetNodes();
171 for (SwNodeOffset n(0); n < pNodes.Count(); ++n)
173 SwNode* pNode = pNodes[n];
174 if (pNode)
176 runAccessibilityCheck(pNode);
177 updateNodeStatus(pNode);
181 updateStatusbar();
183 m_bInitialCheck = true;
186 void OnlineAccessibilityCheck::updateCheckerActivity()
188 bool bOnlineCheckStatus
189 = !comphelper::IsFuzzing()
190 && officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
192 if (bOnlineCheckStatus != m_bOnlineCheckStatus)
194 m_pPreviousNode.reset();
195 m_nPreviousNodeIndex = SwNodeOffset(-1);
196 m_bInitialCheck = false; // force initial check
198 if (!bOnlineCheckStatus)
200 clearAccessibilityIssuesFromAllNodes(); // cleanup all accessibility check data on nodes
201 m_nAccessibilityIssues = -1;
203 else
205 m_nAccessibilityIssues = 0;
208 m_bOnlineCheckStatus = bOnlineCheckStatus;
210 updateStatusbar();
214 void OnlineAccessibilityCheck::update(const SwPosition& rNewPos)
216 updateCheckerActivity();
218 if (!m_bOnlineCheckStatus)
219 return;
221 initialCheck();
223 lookForPreviousNodeAndUpdate(rNewPos);
226 void OnlineAccessibilityCheck::lookForPreviousNodeAndUpdate(const SwPosition& rNewPos)
228 auto nCurrenNodeIndex = rNewPos.GetNodeIndex();
229 auto* pCurrentNode = &rNewPos.GetNode();
231 if (!pCurrentNode->IsContentNode() && !pCurrentNode->IsTableNode())
232 return;
234 auto pCurrentWeak = std::make_unique<WeakNodeContainer>(pCurrentNode);
235 if (!pCurrentWeak->isAlive())
236 return;
238 // Check if previous node was deleted
239 if (!m_pPreviousNode || !m_pPreviousNode->isAlive())
241 m_pPreviousNode = std::move(pCurrentWeak);
242 m_nPreviousNodeIndex = nCurrenNodeIndex;
243 return;
246 // Check if node index changed
247 if (nCurrenNodeIndex == m_nPreviousNodeIndex)
248 return;
250 // Check if previous node is valid
251 if (m_nPreviousNodeIndex < SwNodeOffset(0)
252 || m_nPreviousNodeIndex >= pCurrentNode->GetNodes().Count())
254 m_pPreviousNode = std::move(pCurrentWeak);
255 m_nPreviousNodeIndex = nCurrenNodeIndex;
256 return;
259 // Run the document level Accessibility Check
260 runDocumentLevelAccessibilityCheck();
262 // Get the real previous node from index
263 SwNode* pNode = pCurrentNode->GetNodes()[m_nPreviousNodeIndex];
265 if (pNode && (pNode->IsContentNode() || pNode->IsTableNode()))
267 runAccessibilityCheck(pNode);
268 updateNodeStatus(pNode);
270 // Assign previous node and index
271 m_pPreviousNode = std::move(pCurrentWeak);
272 m_nPreviousNodeIndex = nCurrenNodeIndex;
274 else
276 runAccessibilityCheck(pCurrentNode);
277 updateNodeStatus(pCurrentNode);
279 m_pPreviousNode.reset();
280 m_nPreviousNodeIndex = SwNodeOffset(-1);
283 updateStatusbar();
286 void OnlineAccessibilityCheck::clearAccessibilityIssuesFromAllNodes()
288 auto const& pNodes = m_rDocument.GetNodes();
289 for (SwNodeOffset n(0); n < pNodes.Count(); ++n)
291 SwNode* pNode = pNodes[n];
292 if (pNode)
294 pNode->getAccessibilityCheckStatus().reset();
298 m_aNodes.clear();
299 updateStatusbar();
302 void OnlineAccessibilityCheck::resetAndQueue(SwNode* pNode, bool bIssueObjectNameChanged)
304 if (comphelper::IsFuzzing())
305 return;
307 bool bOnlineCheckStatus
308 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
309 if (!bOnlineCheckStatus)
310 return;
312 pNode->getAccessibilityCheckStatus().reset();
313 m_aNodes.erase(pNode);
314 if (&pNode->GetNodes() == &m_rDocument.GetNodes()) // don't add undo array
316 runAccessibilityCheck(pNode);
317 updateNodeStatus(pNode, bIssueObjectNameChanged);
319 updateStatusbar();
322 void OnlineAccessibilityCheck::resetAndQueueDocumentLevel()
324 if (comphelper::IsFuzzing())
325 return;
327 bool bOnlineCheckStatus
328 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
329 if (!bOnlineCheckStatus)
330 return;
332 runDocumentLevelAccessibilityCheck();
333 updateStatusbar();
336 } // end sw
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */