Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svtools/source/uno/statusbarcontroller.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <svtools/statusbarcontroller.hxx>
21
#include <com/sun/star/beans/PropertyValue.hpp>
22
#include <com/sun/star/frame/XDispatchProvider.hpp>
23
#include <com/sun/star/frame/XFrame.hpp>
24
#include <com/sun/star/lang/DisposedException.hpp>
25
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
26
#include <com/sun/star/util/URLTransformer.hpp>
27
#include <com/sun/star/ui/XStatusbarItem.hpp>
28
#include <cppuhelper/queryinterface.hxx>
29
#include <utility>
30
#include <vcl/svapp.hxx>
31
#include <vcl/window.hxx>
32
#include <vcl/status.hxx>
33
#include <toolkit/helper/vclunohelper.hxx>
34
#include <comphelper/processfactory.hxx>
35
36
using namespace ::cppu;
37
using namespace css::awt;
38
using namespace css::uno;
39
using namespace css::util;
40
using namespace css::beans;
41
using namespace css::lang;
42
using namespace css::frame;
43
44
namespace svt
45
{
46
47
StatusbarController::StatusbarController(
48
    const Reference< XComponentContext >& rxContext,
49
    const Reference< XFrame >& xFrame,
50
    OUString aCommandURL,
51
    unsigned short nID ) :
52
0
    OWeakObject()
53
0
    ,   m_bInitialized( false )
54
0
    ,   m_bDisposed( false )
55
0
    ,   m_nID( nID )
56
0
    ,   m_xFrame( xFrame )
57
0
    ,   m_xContext( rxContext )
58
0
    ,   m_aCommandURL(std::move( aCommandURL ))
59
0
{
60
0
}
61
62
StatusbarController::StatusbarController() :
63
0
    OWeakObject()
64
0
    ,   m_bInitialized( false )
65
0
    ,   m_bDisposed( false )
66
0
    ,   m_nID( 0 )
67
0
{
68
0
}
69
70
StatusbarController::~StatusbarController()
71
0
{
72
0
}
73
74
Reference< XFrame > StatusbarController::getFrameInterface() const
75
0
{
76
0
    SolarMutexGuard aSolarMutexGuard;
77
0
    return m_xFrame;
78
0
}
79
80
Reference< XURLTransformer > StatusbarController::getURLTransformer() const
81
0
{
82
0
    SolarMutexGuard aSolarMutexGuard;
83
0
    if ( !m_xURLTransformer.is() && m_xContext.is() )
84
0
    {
85
0
        m_xURLTransformer = css::util::URLTransformer::create( m_xContext );
86
0
    }
87
88
0
    return m_xURLTransformer;
89
0
}
90
91
// XInterface
92
Any SAL_CALL StatusbarController::queryInterface( const Type& rType )
93
0
{
94
0
    Any a = ::cppu::queryInterface(
95
0
                rType ,
96
0
                static_cast< XStatusbarController* >( this ),
97
0
                static_cast< XStatusListener* >( this ),
98
0
                static_cast< XEventListener* >( this ),
99
0
                static_cast< XInitialization* >( this ),
100
0
                static_cast< XComponent* >( this ),
101
0
                static_cast< XUpdatable* >( this ));
102
103
0
    if ( a.hasValue() )
104
0
        return a;
105
106
0
    return OWeakObject::queryInterface( rType );
107
0
}
108
109
void SAL_CALL StatusbarController::acquire() noexcept
110
0
{
111
0
    OWeakObject::acquire();
112
0
}
113
114
void SAL_CALL StatusbarController::release() noexcept
115
0
{
116
0
    OWeakObject::release();
117
0
}
118
119
void SAL_CALL StatusbarController::initialize( const Sequence< Any >& aArguments )
120
0
{
121
0
    SolarMutexGuard aSolarMutexGuard;
122
123
0
    if ( m_bDisposed )
124
0
        throw DisposedException();
125
126
0
    if ( m_bInitialized )
127
0
        return;
128
129
0
    m_bInitialized = true;
130
131
0
    PropertyValue aPropValue;
132
0
    for ( const auto& rArgument : aArguments )
133
0
    {
134
0
        if ( rArgument >>= aPropValue )
135
0
        {
136
0
            if ( aPropValue.Name == "Frame" )
137
0
                aPropValue.Value >>= m_xFrame;
138
0
            else if ( aPropValue.Name == "CommandURL" )
139
0
                aPropValue.Value >>= m_aCommandURL;
140
0
            else if ( aPropValue.Name == "ServiceManager" )
141
0
            {
142
0
                Reference<XMultiServiceFactory> xMSF;
143
0
                aPropValue.Value >>= xMSF;
144
0
                if( xMSF.is() )
145
0
                    m_xContext = comphelper::getComponentContext(xMSF);
146
0
            }
147
0
            else if ( aPropValue.Name == "ParentWindow" )
148
0
                aPropValue.Value >>= m_xParentWindow;
149
0
            else if ( aPropValue.Name == "Identifier" )
150
0
                aPropValue.Value >>= m_nID;
151
0
            else if ( aPropValue.Name == "StatusbarItem" )
152
0
                aPropValue.Value >>= m_xStatusbarItem;
153
0
        }
154
0
    }
155
156
0
    if ( !m_aCommandURL.isEmpty() )
157
0
        m_aListenerMap.emplace( m_aCommandURL, Reference< XDispatch >() );
158
0
}
159
160
void SAL_CALL StatusbarController::update()
161
0
{
162
0
    {
163
0
        SolarMutexGuard aSolarMutexGuard;
164
0
        if ( m_bDisposed )
165
0
            throw DisposedException();
166
0
    }
167
168
    // Bind all registered listeners to their dispatch objects
169
0
    bindListener();
170
0
}
171
172
// XComponent
173
void SAL_CALL StatusbarController::dispose()
174
0
{
175
0
    Reference< XComponent > xThis = this;
176
177
0
    {
178
0
        SolarMutexGuard aSolarMutexGuard;
179
0
        if ( m_bDisposed )
180
0
            return;
181
0
    }
182
183
0
    {
184
0
        std::unique_lock aGuard(m_aMutex);
185
0
        css::lang::EventObject aEvent( xThis );
186
0
        m_aEventListeners.disposeAndClear( aGuard, aEvent );
187
0
    }
188
189
0
    SolarMutexGuard aSolarMutexGuard;
190
0
    Reference< XStatusListener > xStatusListener = this;
191
0
    Reference< XURLTransformer > xURLTransformer = getURLTransformer();
192
0
    css::util::URL aTargetURL;
193
0
    for (auto const& listener : m_aListenerMap)
194
0
    {
195
0
        try
196
0
        {
197
0
            Reference< XDispatch > xDispatch(listener.second);
198
0
            aTargetURL.Complete = listener.first;
199
0
            xURLTransformer->parseStrict( aTargetURL );
200
201
0
            if ( xDispatch.is() && xStatusListener.is() )
202
0
                xDispatch->removeStatusListener( xStatusListener, aTargetURL );
203
0
        }
204
0
        catch ( Exception& )
205
0
        {
206
0
        }
207
0
    }
208
209
    // clear hash map
210
0
    m_aListenerMap.clear();
211
212
    // release references
213
0
    m_xURLTransformer.clear();
214
0
    m_xContext.clear();
215
0
    m_xFrame.clear();
216
0
    m_xParentWindow.clear();
217
0
    m_xStatusbarItem.clear();
218
219
0
    m_bDisposed = true;
220
0
}
221
222
void SAL_CALL StatusbarController::addEventListener( const Reference< XEventListener >& xListener )
223
0
{
224
0
    std::unique_lock aGuard(m_aMutex);
225
0
    m_aEventListeners.addInterface( aGuard, xListener );
226
0
}
227
228
void SAL_CALL StatusbarController::removeEventListener( const Reference< XEventListener >& xListener )
229
0
{
230
0
    std::unique_lock aGuard(m_aMutex);
231
0
    m_aEventListeners.removeInterface( aGuard, xListener );
232
0
}
233
234
// XEventListener
235
void SAL_CALL StatusbarController::disposing( const EventObject& Source )
236
0
{
237
0
    SolarMutexGuard aSolarMutexGuard;
238
239
0
    if ( m_bDisposed )
240
0
        return;
241
242
0
    Reference< XFrame > xFrame( Source.Source, UNO_QUERY );
243
0
    if ( xFrame.is() )
244
0
    {
245
0
        if ( xFrame == m_xFrame )
246
0
            m_xFrame.clear();
247
0
        return;
248
0
    }
249
250
0
    Reference< XDispatch > xDispatch( Source.Source, UNO_QUERY );
251
0
    if ( !xDispatch.is() )
252
0
        return;
253
254
0
    for (auto & listener : m_aListenerMap)
255
0
    {
256
        // Compare references and release dispatch references if they are equal.
257
0
        if ( xDispatch == listener.second )
258
0
            listener.second.clear();
259
0
    }
260
0
}
261
262
// XStatusListener
263
void SAL_CALL StatusbarController::statusChanged( const FeatureStateEvent& Event )
264
0
{
265
0
    SolarMutexGuard aSolarMutexGuard;
266
267
0
    if ( m_bDisposed )
268
0
        return;
269
270
0
    VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( m_xParentWindow );
271
0
    if ( pWindow && pWindow->GetType() == WindowType::STATUSBAR && m_nID != 0 )
272
0
    {
273
0
        OUString   aStrValue;
274
0
        StatusBar* pStatusBar = static_cast<StatusBar *>(pWindow.get());
275
276
0
        if ( Event.State >>= aStrValue )
277
0
            pStatusBar->SetItemText( m_nID, aStrValue );
278
0
        else if ( !Event.State.hasValue() )
279
0
            pStatusBar->SetItemText( m_nID, u""_ustr );
280
0
    }
281
0
}
282
283
// XStatusbarController
284
sal_Bool SAL_CALL StatusbarController::mouseButtonDown(
285
    const css::awt::MouseEvent& )
286
0
{
287
0
    return false;
288
0
}
289
290
sal_Bool SAL_CALL StatusbarController::mouseMove(
291
    const css::awt::MouseEvent& )
292
0
{
293
0
    return false;
294
0
}
295
296
sal_Bool SAL_CALL StatusbarController::mouseButtonUp(
297
    const css::awt::MouseEvent& )
298
0
{
299
0
    return false;
300
0
}
301
302
void SAL_CALL StatusbarController::command(
303
    const css::awt::Point&,
304
    ::sal_Int32,
305
    sal_Bool,
306
    const css::uno::Any& )
307
0
{
308
0
}
309
310
void SAL_CALL StatusbarController::paint(
311
    const css::uno::Reference< css::awt::XGraphics >&,
312
    const css::awt::Rectangle&,
313
    ::sal_Int32 )
314
0
{
315
0
}
316
317
void SAL_CALL StatusbarController::click( const css::awt::Point& )
318
0
{
319
0
    SolarMutexGuard aSolarMutexGuard;
320
321
0
    if ( m_bDisposed )
322
0
        return;
323
324
0
    Sequence< PropertyValue > aArgs;
325
0
    execute( aArgs );
326
0
}
327
328
void SAL_CALL StatusbarController::doubleClick( const css::awt::Point& )
329
0
{
330
0
}
331
332
void StatusbarController::addStatusListener( const OUString& aCommandURL )
333
0
{
334
0
    Reference< XDispatch >       xDispatch;
335
0
    Reference< XStatusListener > xStatusListener;
336
0
    css::util::URL    aTargetURL;
337
338
0
    {
339
0
        SolarMutexGuard aSolarMutexGuard;
340
0
        URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
341
342
        // Already in the list of status listener. Do nothing.
343
0
        if ( pIter != m_aListenerMap.end() )
344
0
            return;
345
346
        // Check if we are already initialized. Implementation starts adding itself as status listener when
347
        // initialize is called.
348
0
        if ( !m_bInitialized )
349
0
        {
350
            // Put into the unordered_map of status listener. Will be activated when initialized is called
351
0
            m_aListenerMap.emplace( aCommandURL, Reference< XDispatch >() );
352
0
            return;
353
0
        }
354
0
        else
355
0
        {
356
            // Add status listener directly as initialize has already been called.
357
0
            Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
358
0
            if ( m_xContext.is() && xDispatchProvider.is() )
359
0
            {
360
0
                Reference< XURLTransformer > xURLTransformer = getURLTransformer();
361
0
                aTargetURL.Complete = aCommandURL;
362
0
                xURLTransformer->parseStrict( aTargetURL );
363
0
                xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
364
365
0
                xStatusListener = this;
366
0
                URLToDispatchMap::iterator aIter = m_aListenerMap.find( aCommandURL );
367
0
                if ( aIter != m_aListenerMap.end() )
368
0
                {
369
0
                    Reference< XDispatch > xOldDispatch( aIter->second );
370
0
                    aIter->second = xDispatch;
371
372
0
                    try
373
0
                    {
374
0
                        if ( xOldDispatch.is() )
375
0
                            xOldDispatch->removeStatusListener( xStatusListener, aTargetURL );
376
0
                    }
377
0
                    catch ( Exception& )
378
0
                    {
379
0
                    }
380
0
                }
381
0
                else
382
0
                    m_aListenerMap.emplace( aCommandURL, xDispatch );
383
0
            }
384
0
        }
385
0
    }
386
387
    // Call without locked mutex as we are called back from dispatch implementation
388
0
    try
389
0
    {
390
0
        if ( xDispatch.is() )
391
0
            xDispatch->addStatusListener( xStatusListener, aTargetURL );
392
0
    }
393
0
    catch ( Exception& )
394
0
    {
395
0
    }
396
0
}
397
398
void StatusbarController::bindListener()
399
0
{
400
0
    std::vector< Listener > aDispatchVector;
401
0
    Reference< XStatusListener > xStatusListener;
402
403
0
    {
404
0
        SolarMutexGuard aSolarMutexGuard;
405
406
0
        if ( !m_bInitialized )
407
0
            return;
408
409
        // Collect all registered command URL's and store them temporary
410
0
        Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
411
0
        if ( m_xContext.is() && xDispatchProvider.is() )
412
0
        {
413
0
            xStatusListener = this;
414
0
            for (auto & listener : m_aListenerMap)
415
0
            {
416
0
                Reference< XURLTransformer > xURLTransformer = getURLTransformer();
417
0
                css::util::URL aTargetURL;
418
0
                aTargetURL.Complete = listener.first;
419
0
                xURLTransformer->parseStrict( aTargetURL );
420
421
0
                Reference< XDispatch > xDispatch(listener.second);
422
0
                if ( xDispatch.is() )
423
0
                {
424
                    // We already have a dispatch object => we have to requery.
425
                    // Release old dispatch object and remove it as listener
426
0
                    try
427
0
                    {
428
0
                        xDispatch->removeStatusListener( xStatusListener, aTargetURL );
429
0
                    }
430
0
                    catch ( Exception& )
431
0
                    {
432
0
                    }
433
0
                }
434
435
0
                listener.second.clear();
436
0
                xDispatch.clear();
437
438
                // Query for dispatch object. Old dispatch will be released with this, too.
439
0
                try
440
0
                {
441
0
                    xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
442
0
                }
443
0
                catch ( Exception& )
444
0
                {
445
0
                }
446
0
                listener.second = xDispatch;
447
448
0
                aDispatchVector.emplace_back(std::move(aTargetURL), xDispatch);
449
0
            }
450
0
        }
451
0
    }
452
453
    // Call without locked mutex as we are called back from dispatch implementation
454
0
    if ( !xStatusListener.is() )
455
0
        return;
456
457
0
    for (Listener & rListener : aDispatchVector)
458
0
    {
459
0
        try
460
0
        {
461
0
            if ( rListener.xDispatch.is() )
462
0
                rListener.xDispatch->addStatusListener( xStatusListener, rListener.aURL );
463
0
            else if ( rListener.aURL.Complete == m_aCommandURL )
464
0
            {
465
                // Send status changed for the main URL, if we cannot get a valid dispatch object.
466
                // UI disables the button. Catch exception as we release our mutex, it is possible
467
                // that someone else already disposed this instance!
468
0
                FeatureStateEvent aFeatureStateEvent;
469
0
                aFeatureStateEvent.IsEnabled = false;
470
0
                aFeatureStateEvent.FeatureURL = rListener.aURL;
471
0
                aFeatureStateEvent.State = Any();
472
0
                xStatusListener->statusChanged( aFeatureStateEvent );
473
0
            }
474
0
        }
475
0
        catch ( ... ){}
476
0
    }
477
0
}
478
479
::tools::Rectangle StatusbarController::getControlRect() const
480
0
{
481
0
    ::tools::Rectangle aRect;
482
483
0
    {
484
0
        SolarMutexGuard aSolarMutexGuard;
485
486
0
        if ( m_bDisposed )
487
0
            throw DisposedException();
488
489
0
        if ( m_xParentWindow.is() )
490
0
        {
491
0
            VclPtr< StatusBar > pStatusBar = dynamic_cast< StatusBar* >( VCLUnoHelper::GetWindow( m_xParentWindow ) );
492
0
            if ( pStatusBar && pStatusBar->GetType() == WindowType::STATUSBAR )
493
0
                aRect = pStatusBar->GetItemRect( m_nID );
494
0
        }
495
0
    }
496
497
0
    return aRect;
498
0
}
499
500
void StatusbarController::execute( const css::uno::Sequence< css::beans::PropertyValue >& aArgs )
501
0
{
502
0
    Reference< XDispatch >       xDispatch;
503
0
    Reference< XURLTransformer > xURLTransformer;
504
0
    OUString                aCommandURL;
505
506
0
    {
507
0
        SolarMutexGuard aSolarMutexGuard;
508
509
0
        if ( m_bDisposed )
510
0
            throw DisposedException();
511
512
0
        if ( m_bInitialized &&
513
0
             m_xFrame.is() &&
514
0
             m_xContext.is() &&
515
0
             !m_aCommandURL.isEmpty() )
516
0
        {
517
0
            xURLTransformer = getURLTransformer();
518
0
            aCommandURL = m_aCommandURL;
519
0
            URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
520
0
            if ( pIter != m_aListenerMap.end() )
521
0
                xDispatch = pIter->second;
522
0
        }
523
0
    }
524
525
0
    if ( !(xDispatch.is() && xURLTransformer.is()) )
526
0
        return;
527
528
0
    try
529
0
    {
530
0
        css::util::URL aTargetURL;
531
532
0
        aTargetURL.Complete = aCommandURL;
533
0
        xURLTransformer->parseStrict( aTargetURL );
534
0
        xDispatch->dispatch( aTargetURL, aArgs );
535
0
    }
536
0
    catch ( DisposedException& )
537
0
    {
538
0
    }
539
0
}
540
541
void StatusbarController::execute(
542
    const OUString& aCommandURL,
543
    const Sequence< css::beans::PropertyValue >& aArgs )
544
0
{
545
0
    Reference< XDispatch >      xDispatch;
546
0
    css::util::URL   aTargetURL;
547
548
0
    {
549
0
        SolarMutexGuard aSolarMutexGuard;
550
551
0
        if ( m_bDisposed )
552
0
            throw DisposedException();
553
554
0
        if ( m_bInitialized &&
555
0
             m_xFrame.is() &&
556
0
             m_xContext.is() &&
557
0
             !m_aCommandURL.isEmpty() )
558
0
        {
559
0
            Reference< XURLTransformer > xURLTransformer( getURLTransformer() );
560
0
            aTargetURL.Complete = aCommandURL;
561
0
            xURLTransformer->parseStrict( aTargetURL );
562
563
0
            URLToDispatchMap::iterator pIter = m_aListenerMap.find( aCommandURL );
564
0
            if ( pIter != m_aListenerMap.end() )
565
0
                xDispatch = pIter->second;
566
0
            else
567
0
            {
568
0
                Reference< css::frame::XDispatchProvider > xDispatchProvider(
569
0
                    m_xFrame->getController(), UNO_QUERY );
570
0
                if ( xDispatchProvider.is() )
571
0
                    xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
572
0
            }
573
0
        }
574
0
    }
575
576
0
    if ( xDispatch.is() )
577
0
    {
578
0
        try
579
0
        {
580
0
            xDispatch->dispatch( aTargetURL, aArgs );
581
0
        }
582
0
        catch ( DisposedException& )
583
0
        {
584
0
        }
585
0
    }
586
0
}
587
588
} // svt
589
590
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */