bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / style / MultiPropertySetHelper.cxx
blob33ea04d9577abfb46751452aaa39c246d8cacbc3
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 .
21 #include "MultiPropertySetHelper.hxx"
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/beans/XMultiPropertySet.hpp>
25 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <algorithm>
30 using ::com::sun::star::beans::XMultiPropertySet;
31 using ::com::sun::star::beans::XPropertySet;
32 using ::com::sun::star::beans::XPropertySetInfo;
33 using ::com::sun::star::lang::XServiceInfo;
34 using ::com::sun::star::uno::Any;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::uno::UNO_QUERY;
38 using ::std::sort;
41 MultiPropertySetHelper::MultiPropertySetHelper(
42 const sal_Char** pNames ) :
43 pPropertyNames( NULL ),
44 nLength( 0 ),
45 aPropertySequence(),
46 pSequenceIndex( NULL ),
47 aValues(),
48 pValues( NULL )
50 // first count the elements
51 for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
52 nLength++;
54 // allocate array and create strings
55 pPropertyNames = new OUString[nLength];
56 for( sal_Int16 i = 0; i < nLength; i++ )
57 pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
61 MultiPropertySetHelper::~MultiPropertySetHelper()
63 pValues = NULL; // memory 'owned' by aValues
65 delete[] pSequenceIndex;
66 delete[] pPropertyNames;
70 void MultiPropertySetHelper::hasProperties(
71 const Reference<XPropertySetInfo> & rInfo )
73 DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
75 // allocate sequence index
76 if ( NULL == pSequenceIndex )
77 pSequenceIndex = new sal_Int16[nLength] ;
79 // construct pSequenceIndex
80 sal_Int16 nNumberOfProperties = 0;
81 sal_Int16 i;
83 for( i = 0; i < nLength; i++ )
85 // ask for property
86 bool bHasProperty =
87 rInfo->hasPropertyByName( pPropertyNames[i] );
89 // set index and increment (if appropriate)
90 pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
91 if ( bHasProperty )
92 nNumberOfProperties++;
95 // construct property sequence from index array
96 if ( aPropertySequence.getLength() != nNumberOfProperties )
97 aPropertySequence.realloc( nNumberOfProperties );
98 OUString* pPropertySequence = aPropertySequence.getArray();
99 for( i = 0; i < nLength; i ++ )
101 sal_Int16 nIndex = pSequenceIndex[i];
102 if ( nIndex != -1 )
103 pPropertySequence[nIndex] = pPropertyNames[i];
107 bool MultiPropertySetHelper::checkedProperties()
109 return (NULL != pSequenceIndex);
114 void MultiPropertySetHelper::getValues(
115 const Reference<XMultiPropertySet> & rMultiPropertySet )
117 DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
119 aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
120 pValues = aValues.getConstArray();
123 void MultiPropertySetHelper::getValues(
124 const Reference<XPropertySet> & rPropertySet )
126 DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
128 // re-alloc aValues (if necessary) and fill with values from XPropertySet
129 sal_Int16 nSupportedPropertiesCount =
130 (sal_Int16)aPropertySequence.getLength();
131 if ( aValues.getLength() != nSupportedPropertiesCount )
132 aValues.realloc( nSupportedPropertiesCount );
133 Any* pMutableArray = aValues.getArray();
134 for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
136 pMutableArray[i] = rPropertySet->getPropertyValue(
137 pPropertyNames[ pSequenceIndex[ i ] ] );
140 // re-establish pValues pointer
141 pValues = aValues.getConstArray();
145 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
146 const Reference< XPropertySet> & rPropSet,
147 bool bTryMulti )
149 if( !pValues )
151 if( bTryMulti )
153 Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
154 UNO_QUERY );
155 if( xMultiPropSet.is() )
156 getValues( xMultiPropSet );
157 else
158 getValues( rPropSet );
160 else
162 getValues( rPropSet );
166 return getValue( nIndex );
169 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
170 const Reference< XMultiPropertySet> & rMultiPropSet )
172 if( !pValues )
173 getValues( rMultiPropSet );
175 return getValue( nIndex );
178 // inline methods defined in header:
179 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
180 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */