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 .
20 #include <OnlineAccessibilityCheck.hxx>
24 #include <sfx2/bindings.hxx>
25 #include <sfx2/dispatch.hxx>
28 #include <officecfg/Office/Common.hxx>
29 #include <unotools/configmgr.hxx>
33 WeakNodeContainer::WeakNodeContainer(SwNode
* pNode
)
38 auto* pBroadcast
= dynamic_cast<sw::BroadcastingModify
*>(m_pNode
);
42 StartListening(pBroadcast
->GetNotifier());
51 WeakNodeContainer::~WeakNodeContainer() { EndListeningAll(); }
53 bool WeakNodeContainer::isAlive()
57 if (!HasBroadcaster())
62 SwNode
* WeakNodeContainer::getNode()
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()
82 void OnlineAccessibilityCheck::updateNodeStatus(SwNode
* pNode
, bool bIssueObjectNameChanged
)
84 if (!pNode
->IsContentNode() && !pNode
->IsTableNode())
87 m_nAccessibilityIssues
= 0;
89 if (bIssueObjectNameChanged
)
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();
111 iterator
= m_aNodes
.erase(iterator
);
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()
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();
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()
163 runDocumentLevelAccessibilityCheck();
165 auto const& pNodes
= m_rDocument
.GetNodes();
166 for (SwNodeOffset
n(0); n
< pNodes
.Count(); ++n
)
168 SwNode
* pNode
= pNodes
[n
];
171 runAccessibilityCheck(pNode
);
172 updateNodeStatus(pNode
);
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;
200 m_nAccessibilityIssues
= 0;
203 m_bOnlineCheckStatus
= bOnlineCheckStatus
;
209 void OnlineAccessibilityCheck::update(const SwPosition
& rNewPos
)
211 updateCheckerActivity();
213 if (!m_bOnlineCheckStatus
)
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())
229 auto pCurrentWeak
= std::make_unique
<WeakNodeContainer
>(pCurrentNode
);
230 if (!pCurrentWeak
->isAlive())
233 // Check if previous node was deleted
234 if (!m_pPreviousNode
|| !m_pPreviousNode
->isAlive())
236 m_pPreviousNode
= std::move(pCurrentWeak
);
237 m_nPreviousNodeIndex
= nCurrenNodeIndex
;
241 // Check if node index changed
242 if (nCurrenNodeIndex
== m_nPreviousNodeIndex
)
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
;
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
;
272 runAccessibilityCheck(pCurrentNode
);
273 updateNodeStatus(pCurrentNode
);
275 m_pPreviousNode
.reset();
276 m_nPreviousNodeIndex
= SwNodeOffset(-1);
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
];
290 pNode
->getAccessibilityCheckStatus().reset();
298 void OnlineAccessibilityCheck::resetAndQueue(SwNode
* pNode
, bool bIssueObjectNameChanged
)
300 if (utl::ConfigManager::IsFuzzing())
303 bool bOnlineCheckStatus
304 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
305 if (!bOnlineCheckStatus
)
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
);
318 void OnlineAccessibilityCheck::resetAndQueueDocumentLevel()
320 if (utl::ConfigManager::IsFuzzing())
323 bool bOnlineCheckStatus
324 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
325 if (!bOnlineCheckStatus
)
328 runDocumentLevelAccessibilityCheck();
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */