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