update credits
[LibreOffice.git] / xmloff / source / style / MultiPropertySetHelper.cxx
blob298f7293594a657da75a19b4095497902a9417c1
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>
26 #include <comphelper/stl_types.hxx>
28 // STL includes
29 #include <algorithm>
32 using ::com::sun::star::beans::XMultiPropertySet;
33 using ::com::sun::star::beans::XPropertySet;
34 using ::com::sun::star::beans::XPropertySetInfo;
35 using ::com::sun::star::lang::XServiceInfo;
36 using ::com::sun::star::uno::Any;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Sequence;
39 using ::com::sun::star::uno::UNO_QUERY;
40 using ::comphelper::UStringLess;
41 using ::std::sort;
44 MultiPropertySetHelper::MultiPropertySetHelper(
45 const sal_Char** pNames ) :
46 pPropertyNames( NULL ),
47 nLength( 0 ),
48 aPropertySequence(),
49 pSequenceIndex( NULL ),
50 aValues(),
51 pValues( NULL )
53 // first count the elements
54 for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
55 nLength++;
57 // allocate array and create strings
58 pPropertyNames = new OUString[nLength];
59 for( sal_Int16 i = 0; i < nLength; i++ )
60 pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
64 MultiPropertySetHelper::~MultiPropertySetHelper()
66 pValues = NULL; // memory 'owned' by aValues
68 delete[] pSequenceIndex;
69 delete[] pPropertyNames;
73 void MultiPropertySetHelper::hasProperties(
74 const Reference<XPropertySetInfo> & rInfo )
76 DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
78 // allocate sequence index
79 if ( NULL == pSequenceIndex )
80 pSequenceIndex = new sal_Int16[nLength] ;
82 // construct pSequenceIndex
83 sal_Int16 nNumberOfProperties = 0;
84 sal_Int16 i;
86 for( i = 0; i < nLength; i++ )
88 // ask for property
89 sal_Bool bHasProperty =
90 rInfo->hasPropertyByName( pPropertyNames[i] );
92 // set index and increment (if appropriate)
93 pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
94 if ( bHasProperty )
95 nNumberOfProperties++;
98 // construct property sequence from index array
99 if ( aPropertySequence.getLength() != nNumberOfProperties )
100 aPropertySequence.realloc( nNumberOfProperties );
101 OUString* pPropertySequence = aPropertySequence.getArray();
102 for( i = 0; i < nLength; i ++ )
104 sal_Int16 nIndex = pSequenceIndex[i];
105 if ( nIndex != -1 )
106 pPropertySequence[nIndex] = pPropertyNames[i];
110 sal_Bool MultiPropertySetHelper::checkedProperties()
112 return (NULL != pSequenceIndex);
117 void MultiPropertySetHelper::getValues(
118 const Reference<XMultiPropertySet> & rMultiPropertySet )
120 DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
122 aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
123 pValues = aValues.getConstArray();
126 void MultiPropertySetHelper::getValues(
127 const Reference<XPropertySet> & rPropertySet )
129 DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
131 // re-alloc aValues (if necessary) and fill with values from XPropertySet
132 sal_Int16 nSupportedPropertiesCount =
133 (sal_Int16)aPropertySequence.getLength();
134 if ( aValues.getLength() != nSupportedPropertiesCount )
135 aValues.realloc( nSupportedPropertiesCount );
136 Any* pMutableArray = aValues.getArray();
137 for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
139 pMutableArray[i] = rPropertySet->getPropertyValue(
140 pPropertyNames[ pSequenceIndex[ i ] ] );
143 // re-establish pValues pointer
144 pValues = aValues.getConstArray();
148 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
149 const Reference< XPropertySet> & rPropSet,
150 sal_Bool bTryMulti )
152 if( !pValues )
154 if( bTryMulti )
156 Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
157 UNO_QUERY );
158 if( xMultiPropSet.is() )
159 getValues( xMultiPropSet );
160 else
161 getValues( rPropSet );
163 else
165 getValues( rPropSet );
169 return getValue( nIndex );
172 const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
173 const Reference< XMultiPropertySet> & rMultiPropSet )
175 if( !pValues )
176 getValues( rMultiPropSet );
178 return getValue( nIndex );
181 // inline methods defined in header:
182 // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
183 // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */