Line data Source code
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 :
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>
27 :
28 : // STL includes
29 : #include <algorithm>
30 :
31 :
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 ::rtl::OUString;
42 : using ::std::sort;
43 :
44 :
45 87 : MultiPropertySetHelper::MultiPropertySetHelper(
46 : const sal_Char** pNames ) :
47 : pPropertyNames( NULL ),
48 : nLength( 0 ),
49 : aPropertySequence(),
50 : pSequenceIndex( NULL ),
51 : aValues(),
52 87 : pValues( NULL )
53 : {
54 : // first count the elements
55 423 : for( const sal_Char** pPtr = pNames; *pPtr != NULL; pPtr++ )
56 336 : nLength++;
57 :
58 : // allocate array and create strings
59 87 : pPropertyNames = new OUString[nLength];
60 423 : for( sal_Int16 i = 0; i < nLength; i++ )
61 336 : pPropertyNames[i] = OUString::createFromAscii( pNames[i] );
62 87 : }
63 :
64 :
65 174 : MultiPropertySetHelper::~MultiPropertySetHelper()
66 : {
67 87 : pValues = NULL; // memory 'owned' by aValues
68 :
69 87 : delete[] pSequenceIndex;
70 87 : delete[] pPropertyNames;
71 87 : }
72 :
73 :
74 87 : void MultiPropertySetHelper::hasProperties(
75 : const Reference<XPropertySetInfo> & rInfo )
76 : {
77 : DBG_ASSERT( rInfo.is(), "I'd really like an XPropertySetInfo here." );
78 :
79 : // allocate sequence index
80 87 : if ( NULL == pSequenceIndex )
81 87 : pSequenceIndex = new sal_Int16[nLength] ;
82 :
83 : // construct pSequenceIndex
84 87 : sal_Int16 nNumberOfProperties = 0;
85 : sal_Int16 i;
86 :
87 423 : for( i = 0; i < nLength; i++ )
88 : {
89 : // ask for property
90 : sal_Bool bHasProperty =
91 336 : rInfo->hasPropertyByName( pPropertyNames[i] );
92 :
93 : // set index and increment (if appropriate)
94 336 : pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1;
95 336 : if ( bHasProperty )
96 181 : nNumberOfProperties++;
97 : }
98 :
99 : // construct property sequence from index array
100 87 : if ( aPropertySequence.getLength() != nNumberOfProperties )
101 87 : aPropertySequence.realloc( nNumberOfProperties );
102 87 : OUString* pPropertySequence = aPropertySequence.getArray();
103 423 : for( i = 0; i < nLength; i ++ )
104 : {
105 336 : sal_Int16 nIndex = pSequenceIndex[i];
106 336 : if ( nIndex != -1 )
107 181 : pPropertySequence[nIndex] = pPropertyNames[i];
108 : }
109 87 : }
110 :
111 122 : sal_Bool MultiPropertySetHelper::checkedProperties()
112 : {
113 122 : return (NULL != pSequenceIndex);
114 : }
115 :
116 :
117 :
118 32 : void MultiPropertySetHelper::getValues(
119 : const Reference<XMultiPropertySet> & rMultiPropertySet )
120 : {
121 : DBG_ASSERT( rMultiPropertySet.is(), "We need an XMultiPropertySet." );
122 :
123 32 : aValues = rMultiPropertySet->getPropertyValues( aPropertySequence );
124 32 : pValues = aValues.getConstArray();
125 32 : }
126 :
127 0 : void MultiPropertySetHelper::getValues(
128 : const Reference<XPropertySet> & rPropertySet )
129 : {
130 : DBG_ASSERT( rPropertySet.is(), "We need an XPropertySet." );
131 :
132 : // re-alloc aValues (if necessary) and fill with values from XPropertySet
133 : sal_Int16 nSupportedPropertiesCount =
134 0 : (sal_Int16)aPropertySequence.getLength();
135 0 : if ( aValues.getLength() != nSupportedPropertiesCount )
136 0 : aValues.realloc( nSupportedPropertiesCount );
137 0 : Any* pMutableArray = aValues.getArray();
138 0 : for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ )
139 : {
140 0 : pMutableArray[i] = rPropertySet->getPropertyValue(
141 0 : pPropertyNames[ pSequenceIndex[ i ] ] );
142 : }
143 :
144 : // re-establish pValues pointer
145 0 : pValues = aValues.getConstArray();
146 0 : }
147 :
148 :
149 32 : const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
150 : const Reference< XPropertySet> & rPropSet,
151 : sal_Bool bTryMulti )
152 : {
153 32 : if( !pValues )
154 : {
155 32 : if( bTryMulti )
156 : {
157 : Reference < XMultiPropertySet > xMultiPropSet( rPropSet,
158 32 : UNO_QUERY );
159 32 : if( xMultiPropSet.is() )
160 32 : getValues( xMultiPropSet );
161 : else
162 0 : getValues( rPropSet );
163 : }
164 : else
165 : {
166 0 : getValues( rPropSet );
167 : }
168 : }
169 :
170 32 : return getValue( nIndex );
171 : }
172 :
173 57 : const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex,
174 : const Reference< XMultiPropertySet> & rMultiPropSet )
175 : {
176 57 : if( !pValues )
177 0 : getValues( rMultiPropSet );
178 :
179 57 : return getValue( nIndex );
180 : }
181 :
182 : // inline methods defined in header:
183 : // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex )
184 : // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|