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