/src/libreoffice/xmloff/source/style/MultiPropertySetHelper.cxx
Line | Count | Source |
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 | | |
26 | | #include <sal/log.hxx> |
27 | | |
28 | | using ::com::sun::star::beans::XMultiPropertySet; |
29 | | using ::com::sun::star::beans::XPropertySet; |
30 | | using ::com::sun::star::beans::XPropertySetInfo; |
31 | | using ::com::sun::star::uno::Any; |
32 | | using ::com::sun::star::uno::Reference; |
33 | | using ::com::sun::star::uno::UNO_QUERY; |
34 | | |
35 | | |
36 | | MultiPropertySetHelper::MultiPropertySetHelper( |
37 | | std::span<const OUString> pNames ) : |
38 | 3.43k | pPropertyNames( pNames ), |
39 | 3.43k | pValues( nullptr ) |
40 | 3.43k | { |
41 | 3.43k | } |
42 | | |
43 | | |
44 | | MultiPropertySetHelper::~MultiPropertySetHelper() |
45 | 3.43k | { |
46 | 3.43k | pValues = nullptr; // memory 'owned' by aValues |
47 | 3.43k | } |
48 | | |
49 | | |
50 | | void MultiPropertySetHelper::hasProperties( |
51 | | const Reference<XPropertySetInfo> & rInfo ) |
52 | 3.43k | { |
53 | 3.43k | SAL_WARN_IF( !rInfo.is(), "xmloff", "I'd really like an XPropertySetInfo here." ); |
54 | | |
55 | | // allocate sequence index |
56 | 3.43k | if ( !pSequenceIndex ) |
57 | 3.43k | pSequenceIndex.reset( new sal_Int16[pPropertyNames.size()] ); |
58 | | |
59 | | // construct pSequenceIndex |
60 | 3.43k | sal_Int16 nNumberOfProperties = 0; |
61 | | |
62 | 20.5k | for( size_t i = 0; i < pPropertyNames.size(); i++ ) |
63 | 17.0k | { |
64 | | // ask for property |
65 | 17.0k | bool bHasProperty = |
66 | 17.0k | rInfo->hasPropertyByName( pPropertyNames[i] ); |
67 | | |
68 | | // set index and increment (if appropriate) |
69 | 17.0k | pSequenceIndex[i]= bHasProperty ? nNumberOfProperties : -1; |
70 | 17.0k | if ( bHasProperty ) |
71 | 3.43k | nNumberOfProperties++; |
72 | 17.0k | } |
73 | | |
74 | | // construct property sequence from index array |
75 | 3.43k | if ( aPropertySequence.getLength() != nNumberOfProperties ) |
76 | 3.43k | aPropertySequence.realloc( nNumberOfProperties ); |
77 | 3.43k | OUString* pPropertySequence = aPropertySequence.getArray(); |
78 | 20.5k | for( size_t i = 0; i < pPropertyNames.size(); i ++ ) |
79 | 17.0k | { |
80 | 17.0k | sal_Int16 nIndex = pSequenceIndex[i]; |
81 | 17.0k | if ( nIndex != -1 ) |
82 | 3.43k | pPropertySequence[nIndex] = pPropertyNames[i]; |
83 | 17.0k | } |
84 | 3.43k | } |
85 | | |
86 | | bool MultiPropertySetHelper::checkedProperties() |
87 | 5.31k | { |
88 | 5.31k | return (nullptr != pSequenceIndex); |
89 | 5.31k | } |
90 | | |
91 | | |
92 | | void MultiPropertySetHelper::getValues( |
93 | | const Reference<XMultiPropertySet> & rMultiPropertySet ) |
94 | 1.81k | { |
95 | 1.81k | SAL_WARN_IF( !rMultiPropertySet.is(), "xmloff", "We need an XMultiPropertySet." ); |
96 | | |
97 | 1.81k | aValues = rMultiPropertySet->getPropertyValues( aPropertySequence ); |
98 | 1.81k | pValues = aValues.getConstArray(); |
99 | 1.81k | } |
100 | | |
101 | | void MultiPropertySetHelper::getValues( |
102 | | const Reference<XPropertySet> & rPropertySet ) |
103 | 0 | { |
104 | 0 | SAL_WARN_IF( !rPropertySet.is(), "xmloff", "We need an XPropertySet." ); |
105 | | |
106 | | // re-alloc aValues (if necessary) and fill with values from XPropertySet |
107 | 0 | sal_Int16 nSupportedPropertiesCount = |
108 | 0 | static_cast<sal_Int16>(aPropertySequence.getLength()); |
109 | 0 | if ( aValues.getLength() != nSupportedPropertiesCount ) |
110 | 0 | aValues.realloc( nSupportedPropertiesCount ); |
111 | 0 | Any* pMutableArray = aValues.getArray(); |
112 | 0 | for( sal_Int16 i = 0; i < nSupportedPropertiesCount; i++ ) |
113 | 0 | { |
114 | 0 | pMutableArray[i] = rPropertySet->getPropertyValue( |
115 | 0 | pPropertyNames[ pSequenceIndex[ i ] ] ); |
116 | 0 | } |
117 | | |
118 | | // re-establish pValues pointer |
119 | 0 | pValues = aValues.getConstArray(); |
120 | 0 | } |
121 | | |
122 | | |
123 | | const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex, |
124 | | const Reference< XPropertySet> & rPropSet, |
125 | | bool bTryMulti ) |
126 | 1.81k | { |
127 | 1.81k | if( !pValues ) |
128 | 1.81k | { |
129 | 1.81k | if( bTryMulti ) |
130 | 1.81k | { |
131 | 1.81k | Reference < XMultiPropertySet > xMultiPropSet( rPropSet, |
132 | 1.81k | UNO_QUERY ); |
133 | 1.81k | if( xMultiPropSet.is() ) |
134 | 1.81k | getValues( xMultiPropSet ); |
135 | 0 | else |
136 | 0 | getValues( rPropSet ); |
137 | 1.81k | } |
138 | 0 | else |
139 | 0 | { |
140 | 0 | getValues( rPropSet ); |
141 | 0 | } |
142 | 1.81k | } |
143 | | |
144 | 1.81k | return getValue( nIndex ); |
145 | 1.81k | } |
146 | | |
147 | | const Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex, |
148 | | const Reference< XMultiPropertySet> & rMultiPropSet ) |
149 | 0 | { |
150 | 0 | if( !pValues ) |
151 | 0 | getValues( rMultiPropSet ); |
152 | |
|
153 | 0 | return getValue( nIndex ); |
154 | 0 | } |
155 | | |
156 | | // inline methods defined in header: |
157 | | // inline Any& MultiPropertySetHelper::getValue( sal_Int16 nIndex ) |
158 | | // inline sal_Bool MultiPropertySetHelper::hasProperty( sal_Int16 nValueNo ) |
159 | | |
160 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |