Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / headermenucontroller.cxx
blob985fbd3826274dab91dc9bc538a9c82d0eb6a65d
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 <uielement/headermenucontroller.hxx>
22 #include <services.h>
24 #include <strings.hrc>
25 #include <classes/fwkresid.hxx>
27 #include <com/sun/star/awt/MenuItemStyle.hpp>
28 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
29 #include <com/sun/star/container/XNameContainer.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <vcl/svapp.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <osl/mutex.hxx>
35 #include <cppuhelper/supportsservice.hxx>
36 #include <toolkit/awt/vclxmenu.hxx>
38 // Defines
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::frame;
43 using namespace com::sun::star::beans;
44 using namespace com::sun::star::util;
45 using namespace com::sun::star::style;
46 using namespace com::sun::star::container;
48 const sal_uInt16 ALL_MENUITEM_ID = 1;
50 namespace framework
53 // XInterface, XTypeProvider, XServiceInfo
55 OUString SAL_CALL HeaderMenuController::getImplementationName()
57 return "com.sun.star.comp.framework.HeaderMenuController";
60 sal_Bool SAL_CALL HeaderMenuController::supportsService( const OUString& sServiceName )
62 return cppu::supportsService(this, sServiceName);
65 css::uno::Sequence< OUString > SAL_CALL HeaderMenuController::getSupportedServiceNames()
67 return { SERVICENAME_POPUPMENUCONTROLLER };
70 HeaderMenuController::HeaderMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext, bool _bFooter ) :
71 svt::PopupMenuControllerBase( xContext )
72 ,m_bFooter(_bFooter)
76 HeaderMenuController::~HeaderMenuController()
80 // private function
81 void HeaderMenuController::fillPopupMenu( const Reference< css::frame::XModel >& rModel, Reference< css::awt::XPopupMenu > const & rPopupMenu )
83 SolarMutexGuard aSolarMutexGuard;
85 resetPopupMenu( rPopupMenu );
87 Reference< XStyleFamiliesSupplier > xStyleFamiliesSupplier( rModel, UNO_QUERY );
88 if (!xStyleFamiliesSupplier.is())
89 return;
91 Reference< XNameAccess > xStyleFamilies = xStyleFamiliesSupplier->getStyleFamilies();
93 OUString aCmd( ".uno:InsertPageHeader" );
94 OUString aHeaderFooterIsOnStr( "HeaderIsOn" );
95 if ( m_bFooter )
97 aCmd = ".uno:InsertPageFooter";
98 aHeaderFooterIsOnStr = "FooterIsOn";
100 static constexpr OUStringLiteral aIsPhysicalStr( u"IsPhysical" );
101 static constexpr OUStringLiteral aDisplayNameStr( u"DisplayName" );
105 Reference< XNameContainer > xNameContainer;
106 if ( xStyleFamilies->getByName("PageStyles") >>= xNameContainer )
108 Sequence< OUString > aSeqNames = xNameContainer->getElementNames();
110 sal_uInt16 nId = 2;
111 sal_uInt16 nCount = 0;
112 bool bAllOneState( true );
113 bool bLastCheck( true );
114 bool bFirstChecked( false );
115 bool bFirstItemInserted( false );
116 for ( sal_Int32 n = 0; n < aSeqNames.getLength(); n++ )
118 OUString aName = aSeqNames[n];
119 Reference< XPropertySet > xPropSet( xNameContainer->getByName( aName ), UNO_QUERY );
120 if ( xPropSet.is() )
122 bool bIsPhysical( false );
123 if (( xPropSet->getPropertyValue( aIsPhysicalStr ) >>= bIsPhysical ) && bIsPhysical )
125 OUString aDisplayName;
126 bool bHeaderIsOn( false );
127 xPropSet->getPropertyValue( aDisplayNameStr ) >>= aDisplayName;
128 xPropSet->getPropertyValue( aHeaderFooterIsOnStr ) >>= bHeaderIsOn;
130 OUStringBuffer aStrBuf( aCmd
131 + "?PageStyle:string="
132 + aDisplayName
133 + "&On:bool=" );
134 if ( !bHeaderIsOn )
135 aStrBuf.append( "true" );
136 else
137 aStrBuf.append( "false" );
138 OUString aCommand( aStrBuf.makeStringAndClear() );
139 rPopupMenu->insertItem(nId, aDisplayName, css::awt::MenuItemStyle::CHECKABLE, nCount);
140 if ( !bFirstItemInserted )
142 bFirstItemInserted = true;
143 bFirstChecked = bHeaderIsOn;
146 rPopupMenu->setCommand(nId, aCommand);
147 rPopupMenu->checkItem(nId, bHeaderIsOn);
148 ++nId;
150 // Check if all entries have the same state
151 if( bAllOneState && n && bHeaderIsOn != bLastCheck )
152 bAllOneState = false;
153 bLastCheck = bHeaderIsOn;
154 ++nCount;
159 if ( bAllOneState && ( nCount > 1 ))
161 // Insert special item for all command
162 rPopupMenu->insertItem(ALL_MENUITEM_ID, FwkResId(STR_MENU_HEADFOOTALL), 0, 0);
164 OUStringBuffer aStrBuf( aCmd + "?On:bool=" );
166 // Command depends on check state of first menu item entry
167 if ( !bFirstChecked )
168 aStrBuf.append( "true" );
169 else
170 aStrBuf.append( "false" );
172 rPopupMenu->setCommand(1, aStrBuf.makeStringAndClear());
173 rPopupMenu->insertSeparator(1);
177 catch ( const css::container::NoSuchElementException& )
182 // XEventListener
183 void SAL_CALL HeaderMenuController::disposing( const EventObject& )
185 Reference< css::awt::XMenuListener > xHolder(this);
187 std::unique_lock aLock( m_aMutex );
188 m_xFrame.clear();
189 m_xDispatch.clear();
191 if ( m_xPopupMenu.is() )
192 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
193 m_xPopupMenu.clear();
196 // XStatusListener
197 void SAL_CALL HeaderMenuController::statusChanged( const FeatureStateEvent& Event )
199 Reference< css::frame::XModel > xModel;
201 if ( Event.State >>= xModel )
203 std::unique_lock aLock( m_aMutex );
204 m_xModel = xModel;
205 if ( m_xPopupMenu.is() )
206 fillPopupMenu( xModel, m_xPopupMenu );
210 // XMenuListener
211 void SAL_CALL HeaderMenuController::updatePopupMenu()
213 std::unique_lock aLock( m_aMutex );
215 throwIfDisposed(aLock);
217 Reference< css::frame::XModel > xModel( m_xModel );
218 aLock.unlock();
220 if ( !xModel.is() )
221 svt::PopupMenuControllerBase::updatePopupMenu();
223 aLock.lock();
224 if ( m_xPopupMenu.is() && m_xModel.is() )
225 fillPopupMenu( m_xModel, m_xPopupMenu );
230 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
231 framework_HeaderMenuController_get_implementation(
232 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
234 return cppu::acquire(new framework::HeaderMenuController(context));
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */