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 !comphelper::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 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();
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()
168 runDocumentLevelAccessibilityCheck();
170 auto const& pNodes
= m_rDocument
.GetNodes();
171 for (SwNodeOffset
n(0); n
< pNodes
.Count(); ++n
)
173 SwNode
* pNode
= pNodes
[n
];
176 runAccessibilityCheck(pNode
);
177 updateNodeStatus(pNode
);
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;
205 m_nAccessibilityIssues
= 0;
208 m_bOnlineCheckStatus
= bOnlineCheckStatus
;
214 void OnlineAccessibilityCheck::update(const SwPosition
& rNewPos
)
216 updateCheckerActivity();
218 if (!m_bOnlineCheckStatus
)
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())
234 auto pCurrentWeak
= std::make_unique
<WeakNodeContainer
>(pCurrentNode
);
235 if (!pCurrentWeak
->isAlive())
238 // Check if previous node was deleted
239 if (!m_pPreviousNode
|| !m_pPreviousNode
->isAlive())
241 m_pPreviousNode
= std::move(pCurrentWeak
);
242 m_nPreviousNodeIndex
= nCurrenNodeIndex
;
246 // Check if node index changed
247 if (nCurrenNodeIndex
== m_nPreviousNodeIndex
)
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
;
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
;
276 runAccessibilityCheck(pCurrentNode
);
277 updateNodeStatus(pCurrentNode
);
279 m_pPreviousNode
.reset();
280 m_nPreviousNodeIndex
= SwNodeOffset(-1);
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
];
294 pNode
->getAccessibilityCheckStatus().reset();
302 void OnlineAccessibilityCheck::resetAndQueue(SwNode
* pNode
, bool bIssueObjectNameChanged
)
304 if (comphelper::IsFuzzing())
307 bool bOnlineCheckStatus
308 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
309 if (!bOnlineCheckStatus
)
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
);
322 void OnlineAccessibilityCheck::resetAndQueueDocumentLevel()
324 if (comphelper::IsFuzzing())
327 bool bOnlineCheckStatus
328 = officecfg::Office::Common::Accessibility::OnlineAccessibilityCheck::get();
329 if (!bOnlineCheckStatus
)
332 runDocumentLevelAccessibilityCheck();
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */