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 <sal/config.h>
21 #include <unotools/options.hxx>
22 
23 #include <algorithm>
24 
25 using utl::detail::Options;
26 using utl::ConfigurationBroadcaster;
27 
~ConfigurationListener()28 utl::ConfigurationListener::~ConfigurationListener() {}
29 
ConfigurationBroadcaster()30 ConfigurationBroadcaster::ConfigurationBroadcaster()
31 : m_nBroadcastBlocked( 0 )
32 , m_nBlockedHint( ConfigurationHints::NONE )
33 {
34 }
35 
ConfigurationBroadcaster(ConfigurationBroadcaster const & rSource)36 ConfigurationBroadcaster::ConfigurationBroadcaster(ConfigurationBroadcaster const & rSource)
37 : mpList( rSource.mpList ? new IMPL_ConfigurationListenerList(*rSource.mpList) : nullptr )
38 , m_nBroadcastBlocked( rSource.m_nBroadcastBlocked )
39 , m_nBlockedHint( rSource.m_nBlockedHint )
40 {
41 }
42 
~ConfigurationBroadcaster()43 ConfigurationBroadcaster::~ConfigurationBroadcaster()
44 {
45 }
46 
operator =(ConfigurationBroadcaster const & other)47 ConfigurationBroadcaster & ConfigurationBroadcaster::operator =(
48     ConfigurationBroadcaster const & other)
49 {
50     if (&other != this) {
51         mpList.reset(
52             other.mpList == nullptr ? nullptr : new IMPL_ConfigurationListenerList(*other.mpList));
53         m_nBroadcastBlocked = other.m_nBroadcastBlocked;
54         m_nBlockedHint = other.m_nBlockedHint;
55     }
56     return *this;
57 }
58 
AddListener(utl::ConfigurationListener * pListener)59 void ConfigurationBroadcaster::AddListener( utl::ConfigurationListener* pListener )
60 {
61     if ( !mpList )
62         mpList.reset(new IMPL_ConfigurationListenerList);
63     mpList->push_back( pListener );
64 }
65 
RemoveListener(utl::ConfigurationListener const * pListener)66 void ConfigurationBroadcaster::RemoveListener( utl::ConfigurationListener const * pListener )
67 {
68     if ( mpList ) {
69         auto it = std::find(mpList->begin(), mpList->end(), pListener);
70         if ( it != mpList->end() )
71             mpList->erase( it );
72     }
73 }
74 
NotifyListeners(ConfigurationHints nHint)75 void ConfigurationBroadcaster::NotifyListeners( ConfigurationHints nHint )
76 {
77     if ( m_nBroadcastBlocked )
78         m_nBlockedHint |= nHint;
79     else
80     {
81         nHint |= m_nBlockedHint;
82         m_nBlockedHint = ConfigurationHints::NONE;
83         if ( mpList ) {
84             for ( size_t n = 0; n < mpList->size(); n++ )
85                 (*mpList)[ n ]->ConfigurationChanged( this, nHint );
86         }
87     }
88 }
89 
BlockBroadcasts(bool bBlock)90 void ConfigurationBroadcaster::BlockBroadcasts( bool bBlock )
91 {
92     if ( bBlock )
93         ++m_nBroadcastBlocked;
94     else if ( m_nBroadcastBlocked )
95     {
96         if ( --m_nBroadcastBlocked == 0 )
97             NotifyListeners( ConfigurationHints::NONE );
98     }
99 }
100 
Options()101 Options::Options()
102 {
103 }
104 
~Options()105 Options::~Options()
106 {
107 }
108 
ConfigurationChanged(ConfigurationBroadcaster *,ConfigurationHints nHint)109 void Options::ConfigurationChanged( ConfigurationBroadcaster*, ConfigurationHints nHint )
110 {
111     NotifyListeners( nHint );
112 }
113 
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
115