1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
28*cdf0e10cSrcweir #define _XMLOFF_CONDITIONALMULTIPROPERTYSETHELPER_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <rtl/ustring.hxx>
31*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
32*cdf0e10cSrcweir #include <tools/debug.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir namespace com { namespace sun { namespace star {
36*cdf0e10cSrcweir 	namespace beans { class XMultiPropertySet; }
37*cdf0e10cSrcweir 	namespace beans { class XPropertySet; }
38*cdf0e10cSrcweir 	namespace beans { class XPropertySetInfo; }
39*cdf0e10cSrcweir } } }
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /**
43*cdf0e10cSrcweir  * The MultiPropertySetHelper performs the follwing functions:
44*cdf0e10cSrcweir  *
45*cdf0e10cSrcweir  * Given a list of property names (as sal_Char** or OUString*), it can
46*cdf0e10cSrcweir  * query an XMultiPropertySet (or XPropertySet) which of these properties
47*cdf0e10cSrcweir  * it supports (method hasProperties(...)). The properties *MUST* be
48*cdf0e10cSrcweir  * sorted alphabetically.
49*cdf0e10cSrcweir  *
50*cdf0e10cSrcweir  * Then, the X(Multi)PropertySet can be queried for values, and only
51*cdf0e10cSrcweir  * the supported properties are queried. (method getValues(...)) The
52*cdf0e10cSrcweir  * values are stored in the helper itself.
53*cdf0e10cSrcweir  *
54*cdf0e10cSrcweir  * Finally, each property can be queried for existence
55*cdf0e10cSrcweir  * (method hasProperty(...)) or its value (method (getValue(...))).
56*cdf0e10cSrcweir  *
57*cdf0e10cSrcweir  * After some initial preparation (hasProperties, getValues) the
58*cdf0e10cSrcweir  * MultiPropertySetHelper can be used similarly to an
59*cdf0e10cSrcweir  * XPropertySet in that you can query the values in the places where you
60*cdf0e10cSrcweir  * need them. However, if an XMultiPropertySet is supplied, the queries
61*cdf0e10cSrcweir  * are more efficient, often significantly so.
62*cdf0e10cSrcweir  */
63*cdf0e10cSrcweir class MultiPropertySetHelper
64*cdf0e10cSrcweir {
65*cdf0e10cSrcweir 	/// names of all properties
66*cdf0e10cSrcweir 	::rtl::OUString* pPropertyNames;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	/// length of pPropertyNames array
69*cdf0e10cSrcweir 	sal_Int16 nLength;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	/// the sequence of property names that the current (multi)
72*cdf0e10cSrcweir 	/// property set implementation supports
73*cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::rtl::OUString > aPropertySequence;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	/// an array of indices that maps from pPropertyNames indices to
76*cdf0e10cSrcweir 	/// aPropertySequence indices
77*cdf0e10cSrcweir 	sal_Int16* pSequenceIndex;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	/// the last set of values retrieved by getValues
80*cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > aValues;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	/// result of aValues.getConstArray()
83*cdf0e10cSrcweir 	const ::com::sun::star::uno::Any* pValues;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	/// an empty Any
86*cdf0e10cSrcweir 	::com::sun::star::uno::Any aEmptyAny;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir public:
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	MultiPropertySetHelper( const sal_Char** pNames );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	MultiPropertySetHelper( const ::rtl::OUString* pNames );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	~MultiPropertySetHelper();
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	/**
98*cdf0e10cSrcweir 	 * Call hasPropertiesByName for the provided XPropertySetInfo and build
99*cdf0e10cSrcweir 	 * list of allowed properties.
100*cdf0e10cSrcweir 	 */
101*cdf0e10cSrcweir 	void hasProperties( const ::com::sun::star::uno::Reference<
102*cdf0e10cSrcweir 							::com::sun::star::beans::XPropertySetInfo> & );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     /**
106*cdf0e10cSrcweir      * Return whether hasProperties was called
107*cdf0e10cSrcweir      * (i.e. if we are ready to call getValues)
108*cdf0e10cSrcweir      */
109*cdf0e10cSrcweir     sal_Bool checkedProperties();
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	/**
112*cdf0e10cSrcweir 	 * Get values from the XMultiPropertySet.
113*cdf0e10cSrcweir 	 *
114*cdf0e10cSrcweir 	 * May only be called after hasProperties() was called for the
115*cdf0e10cSrcweir 	 * appropriate XPropertySetInfo.
116*cdf0e10cSrcweir 	 */
117*cdf0e10cSrcweir     void getValues( const ::com::sun::star::uno::Reference<
118*cdf0e10cSrcweir 							::com::sun::star::beans::XMultiPropertySet> & );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	/**
121*cdf0e10cSrcweir 	 * Get values from the XPropertySet. This can be much slower than
122*cdf0e10cSrcweir 	 * getValues( const Reference<XMultiPropertySet& ) and hence
123*cdf0e10cSrcweir 	 * should be avoided.
124*cdf0e10cSrcweir 	 *
125*cdf0e10cSrcweir 	 * May only be called after hasProperties() was called for the
126*cdf0e10cSrcweir 	 * appropriate XPropertySetInfo.
127*cdf0e10cSrcweir 	 */
128*cdf0e10cSrcweir 	void getValues( const ::com::sun::star::uno::Reference<
129*cdf0e10cSrcweir 							::com::sun::star::beans::XPropertySet> & );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	/**
134*cdf0e10cSrcweir 	 * Get a value from the values array.
135*cdf0e10cSrcweir 	 *
136*cdf0e10cSrcweir 	 * May only be called after getValues() was called.
137*cdf0e10cSrcweir 	 */
138*cdf0e10cSrcweir 	inline const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	/**
141*cdf0e10cSrcweir 	 * Find out if this property is supported.
142*cdf0e10cSrcweir 	 *
143*cdf0e10cSrcweir 	 * May only be called after hasProperties() was called.
144*cdf0e10cSrcweir 	 */
145*cdf0e10cSrcweir 	inline sal_Bool hasProperty( sal_Int16 nIndex );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	/**
148*cdf0e10cSrcweir 	 * Get a value from the XPropertySet on demand.
149*cdf0e10cSrcweir 	 *
150*cdf0e10cSrcweir 	 * If neither getValues nor getValueOnDemand has been called already
151*cdf0e10cSrcweir 	 * after the last call to resetValues, the values are retrieved
152*cdf0e10cSrcweir 	 * using getValues. Otherwise the value already retrieved is returned.
153*cdf0e10cSrcweir 	 * In case XMultiPropertySet is supported by the XPropertySet and
154*cdf0e10cSrcweir 	 * bTryMult is set, the XMultiPropertySet is used to get the values.
155*cdf0e10cSrcweir 	 *
156*cdf0e10cSrcweir 	 */
157*cdf0e10cSrcweir 	const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
158*cdf0e10cSrcweir 						const ::com::sun::star::uno::Reference<
159*cdf0e10cSrcweir 							::com::sun::star::beans::XPropertySet> &,
160*cdf0e10cSrcweir 						sal_Bool bTryMulti = sal_False );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	/**
163*cdf0e10cSrcweir 	 * Get a value from the XMultiPropertySet on demand.
164*cdf0e10cSrcweir 	 *
165*cdf0e10cSrcweir 	 * If neither getValues nor getValueOnDemand has been called already
166*cdf0e10cSrcweir 	 * after the last call to resetValues, the values are retrieved
167*cdf0e10cSrcweir 	 * using getValues. Otherwise the value already retrieved is returned.
168*cdf0e10cSrcweir 	 * In case XMultiPropertySet is supported by the XPropertySet,
169*cdf0e10cSrcweir 	 * XMultiPropertySet is used to get the values.
170*cdf0e10cSrcweir 	 *
171*cdf0e10cSrcweir 	 */
172*cdf0e10cSrcweir 	const ::com::sun::star::uno::Any& getValue( sal_Int16 nIndex,
173*cdf0e10cSrcweir 						const ::com::sun::star::uno::Reference<
174*cdf0e10cSrcweir 							::com::sun::star::beans::XMultiPropertySet> & );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	inline void resetValues() { pValues = 0; }
177*cdf0e10cSrcweir };
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir // inline implementations of the often-called methods getValue and hasProperty:
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir const ::com::sun::star::uno::Any& MultiPropertySetHelper::getValue(
183*cdf0e10cSrcweir 	sal_Int16 nValueNo )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir 	DBG_ASSERT( pValues != NULL,
186*cdf0e10cSrcweir 				"called getValue() without calling getValues() before");
187*cdf0e10cSrcweir 	DBG_ASSERT( pSequenceIndex != NULL,
188*cdf0e10cSrcweir 				"called getValue() without calling hasProperties() before" );
189*cdf0e10cSrcweir 	DBG_ASSERT( nValueNo < nLength, "index out of range" );
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 	sal_Int16 nIndex = pSequenceIndex[ nValueNo ];
192*cdf0e10cSrcweir 	return ( nIndex != -1 ) ? pValues[ nIndex ] : aEmptyAny;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo )
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir 	DBG_ASSERT( pSequenceIndex != NULL,
198*cdf0e10cSrcweir 				"called getValue() without calling hasProperties() before" );
199*cdf0e10cSrcweir 	DBG_ASSERT( nValueNo < nLength, "index out of range" );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	return pSequenceIndex[ nValueNo ] != -1;
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir #endif
205