lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / svx / source / form / dataaccessdescriptor.cxx
blobcdc419b8551e003a665eff6df29f2978acac5517
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svx/dataaccessdescriptor.hxx>
21 #include <comphelper/propertysetinfo.hxx>
22 #include <comphelper/genericpropertyset.hxx>
23 #include <osl/diagnose.h>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <com/sun/star/ucb/XContent.hpp>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <tools/urlobj.hxx>
30 namespace svx
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::ucb;
38 using namespace ::comphelper;
40 class ODADescriptorImpl
42 protected:
43 bool m_bSetOutOfDate : 1;
44 bool m_bSequenceOutOfDate : 1;
46 public:
47 typedef ::std::map< DataAccessDescriptorProperty, Any > DescriptorValues;
48 DescriptorValues m_aValues;
49 Sequence< PropertyValue > m_aAsSequence;
50 Reference< XPropertySet > m_xAsSet;
52 typedef ::std::map< OUString, PropertyMapEntry const * > MapString2PropertyEntry;
54 public:
55 ODADescriptorImpl();
56 ODADescriptorImpl(const ODADescriptorImpl& _rSource);
58 void invalidateExternRepresentations();
60 void updateSequence();
62 /** builds the descriptor from a property value sequence
63 @return <TRUE/>
64 if and only if the sequence contained valid properties only
66 bool buildFrom( const Sequence< PropertyValue >& _rValues );
68 /** builds the descriptor from a property set
69 @return <TRUE/>
70 if and only if the set contained valid properties only
72 bool buildFrom( const Reference< XPropertySet >& _rValues );
74 protected:
75 static PropertyValue buildPropertyValue( const DescriptorValues::const_iterator& _rPos );
76 static const MapString2PropertyEntry& getPropertyMap( );
77 static PropertyMapEntry const * getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos );
81 ODADescriptorImpl::ODADescriptorImpl()
82 :m_bSetOutOfDate(true)
83 ,m_bSequenceOutOfDate(true)
88 ODADescriptorImpl::ODADescriptorImpl(const ODADescriptorImpl& _rSource)
89 :m_bSetOutOfDate( _rSource.m_bSetOutOfDate )
90 ,m_bSequenceOutOfDate( _rSource.m_bSequenceOutOfDate )
91 ,m_aValues( _rSource.m_aValues )
93 if (!m_bSetOutOfDate)
94 m_xAsSet = _rSource.m_xAsSet;
95 if (!m_bSequenceOutOfDate)
96 m_aAsSequence = _rSource.m_aAsSequence;
100 bool ODADescriptorImpl::buildFrom( const Sequence< PropertyValue >& _rValues )
102 const MapString2PropertyEntry& rProperties = getPropertyMap();
104 bool bValidPropsOnly = true;
106 // loop through the sequence, and fill our m_aValues
107 const PropertyValue* pValues = _rValues.getConstArray();
108 const PropertyValue* pValuesEnd = pValues + _rValues.getLength();
109 for (;pValues != pValuesEnd; ++pValues)
111 MapString2PropertyEntry::const_iterator aPropPos = rProperties.find( pValues->Name );
112 if ( aPropPos != rProperties.end() )
114 DataAccessDescriptorProperty eProperty = (DataAccessDescriptorProperty)aPropPos->second->mnHandle;
115 m_aValues[eProperty] = pValues->Value;
117 else
118 // unknown property
119 bValidPropsOnly = false;
122 if (bValidPropsOnly)
124 m_aAsSequence = _rValues;
125 m_bSequenceOutOfDate = false;
127 else
128 m_bSequenceOutOfDate = true;
130 return bValidPropsOnly;
134 bool ODADescriptorImpl::buildFrom( const Reference< XPropertySet >& _rxValues )
136 Reference< XPropertySetInfo > xPropInfo;
137 if (_rxValues.is())
138 xPropInfo = _rxValues->getPropertySetInfo();
139 if (!xPropInfo.is())
141 OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
142 return false;
145 // build a PropertyValue sequence with the current values
146 Sequence< Property > aProperties = xPropInfo->getProperties();
147 const Property* pProperty = aProperties.getConstArray();
148 const Property* pPropertyEnd = pProperty + aProperties.getLength();
150 Sequence< PropertyValue > aValues(aProperties.getLength());
151 PropertyValue* pValues = aValues.getArray();
153 for (;pProperty != pPropertyEnd; ++pProperty, ++pValues)
155 pValues->Name = pProperty->Name;
156 pValues->Value = _rxValues->getPropertyValue(pProperty->Name);
159 bool bValidPropsOnly = buildFrom(aValues);
160 if (bValidPropsOnly)
162 m_xAsSet = _rxValues;
163 m_bSetOutOfDate = false;
165 else
166 m_bSetOutOfDate = true;
168 return bValidPropsOnly;
172 void ODADescriptorImpl::invalidateExternRepresentations()
174 m_bSetOutOfDate = true;
175 m_bSequenceOutOfDate = true;
179 const ODADescriptorImpl::MapString2PropertyEntry& ODADescriptorImpl::getPropertyMap( )
181 // the properties we know
182 static MapString2PropertyEntry s_aProperties;
183 if ( s_aProperties.empty() )
185 static PropertyMapEntry const s_aDesriptorProperties[] =
187 { OUString("ActiveConnection"), daConnection, cppu::UnoType<XConnection>::get(), PropertyAttribute::TRANSIENT, 0 },
188 { OUString("BookmarkSelection"), daBookmarkSelection, cppu::UnoType<bool>::get(), PropertyAttribute::TRANSIENT, 0 },
189 { OUString("Column"), daColumnObject, cppu::UnoType<XPropertySet>::get(), PropertyAttribute::TRANSIENT, 0 },
190 { OUString("ColumnName"), daColumnName, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
191 { OUString("Command"), daCommand, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
192 { OUString("CommandType"), daCommandType, ::cppu::UnoType<sal_Int32>::get(), PropertyAttribute::TRANSIENT, 0 },
193 { OUString("Component"), daComponent, cppu::UnoType<XContent>::get(), PropertyAttribute::TRANSIENT, 0 },
194 { OUString("ConnectionResource"), daConnectionResource, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
195 { OUString("Cursor"), daCursor, cppu::UnoType<XResultSet>::get(), PropertyAttribute::TRANSIENT, 0 },
196 { OUString("DataSourceName"), daDataSource, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
197 { OUString("DatabaseLocation"), daDatabaseLocation, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
198 { OUString("EscapeProcessing"), daEscapeProcessing, cppu::UnoType<bool>::get(), PropertyAttribute::TRANSIENT, 0 },
199 { OUString("Filter"), daFilter, ::cppu::UnoType<OUString>::get(), PropertyAttribute::TRANSIENT, 0 },
200 { OUString("Selection"), daSelection, cppu::UnoType<Sequence< Any >>::get(), PropertyAttribute::TRANSIENT, 0 },
201 { OUString(), 0, css::uno::Type(), 0, 0 }
204 PropertyMapEntry const * pEntry = s_aDesriptorProperties;
205 while ( !pEntry->maName.isEmpty() )
207 s_aProperties[ pEntry->maName ] = pEntry;
208 ++pEntry;
212 return s_aProperties;
216 PropertyMapEntry const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos )
218 const MapString2PropertyEntry& rProperties = getPropertyMap();
220 sal_Int32 nNeededHandle = (sal_Int32)(_rPos->first);
222 for ( MapString2PropertyEntry::const_iterator loop = rProperties.begin();
223 loop != rProperties.end();
224 ++loop
227 if ( nNeededHandle == loop->second->mnHandle )
228 return loop->second;
230 throw RuntimeException();
234 PropertyValue ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator& _rPos )
236 // the map entry
237 PropertyMapEntry const * pProperty = getPropertyMapEntry( _rPos );
239 // build the property value
240 PropertyValue aReturn;
241 aReturn.Name = pProperty->maName;
242 aReturn.Handle = pProperty->mnHandle;
243 aReturn.Value = _rPos->second;
244 aReturn.State = PropertyState_DIRECT_VALUE;
246 // outta here
247 return aReturn;
251 void ODADescriptorImpl::updateSequence()
253 if (!m_bSequenceOutOfDate)
254 return;
256 m_aAsSequence.realloc(m_aValues.size());
257 PropertyValue* pValue = m_aAsSequence.getArray();
259 // loop through all our values
260 for ( DescriptorValues::const_iterator aLoop = m_aValues.begin();
261 aLoop != m_aValues.end();
262 ++aLoop, ++pValue
265 *pValue = buildPropertyValue(aLoop);
268 // don't need to rebuild next time
269 m_bSequenceOutOfDate = false;
272 ODataAccessDescriptor::ODataAccessDescriptor()
273 :m_pImpl(new ODADescriptorImpl)
278 ODataAccessDescriptor::ODataAccessDescriptor( const ODataAccessDescriptor& _rSource )
279 :m_pImpl(new ODADescriptorImpl(*_rSource.m_pImpl))
284 const ODataAccessDescriptor& ODataAccessDescriptor::operator=(const ODataAccessDescriptor& _rSource)
286 delete m_pImpl;
287 m_pImpl = new ODADescriptorImpl(*_rSource.m_pImpl);
288 return *this;
292 ODataAccessDescriptor::ODataAccessDescriptor( const Reference< XPropertySet >& _rValues )
293 :m_pImpl(new ODADescriptorImpl)
295 m_pImpl->buildFrom(_rValues);
299 ODataAccessDescriptor::ODataAccessDescriptor( const Any& _rValues )
300 :m_pImpl(new ODADescriptorImpl)
302 // check if we know the format in the Any
303 Sequence< PropertyValue > aValues;
304 Reference< XPropertySet > xValues;
305 if ( _rValues >>= aValues )
306 m_pImpl->buildFrom( aValues );
307 else if ( _rValues >>= xValues )
308 m_pImpl->buildFrom( xValues );
312 ODataAccessDescriptor::ODataAccessDescriptor( const Sequence< PropertyValue >& _rValues )
313 :m_pImpl(new ODADescriptorImpl)
315 m_pImpl->buildFrom(_rValues);
319 ODataAccessDescriptor::~ODataAccessDescriptor()
321 delete m_pImpl;
325 void ODataAccessDescriptor::clear()
327 m_pImpl->m_aValues.clear();
331 void ODataAccessDescriptor::erase(DataAccessDescriptorProperty _eWhich)
333 OSL_ENSURE(has(_eWhich), "ODataAccessDescriptor::erase: invalid call!");
334 if (has(_eWhich))
335 m_pImpl->m_aValues.erase(_eWhich);
339 bool ODataAccessDescriptor::has(DataAccessDescriptorProperty _eWhich) const
341 return m_pImpl->m_aValues.find(_eWhich) != m_pImpl->m_aValues.end();
345 const Any& ODataAccessDescriptor::operator [] ( DataAccessDescriptorProperty _eWhich ) const
347 if (!has(_eWhich))
349 OSL_FAIL("ODataAccessDescriptor::operator[]: invalid acessor!");
350 static const Any aDummy;
351 return aDummy;
354 return m_pImpl->m_aValues[_eWhich];
358 Any& ODataAccessDescriptor::operator[] ( DataAccessDescriptorProperty _eWhich )
360 m_pImpl->invalidateExternRepresentations();
361 return m_pImpl->m_aValues[_eWhich];
365 void ODataAccessDescriptor::initializeFrom(const Sequence< PropertyValue >& _rValues, bool _bClear)
367 if (_bClear)
368 clear();
369 m_pImpl->buildFrom(_rValues);
373 Sequence< PropertyValue > ODataAccessDescriptor::createPropertyValueSequence()
375 m_pImpl->updateSequence();
376 return m_pImpl->m_aAsSequence;
380 OUString ODataAccessDescriptor::getDataSource() const
382 OUString sDataSourceName;
383 if ( has(daDataSource) )
384 (*this)[daDataSource] >>= sDataSourceName;
385 else if ( has(daDatabaseLocation) )
386 (*this)[daDatabaseLocation] >>= sDataSourceName;
387 return sDataSourceName;
390 void ODataAccessDescriptor::setDataSource(const OUString& _sDataSourceNameOrLocation)
392 if ( !_sDataSourceNameOrLocation.isEmpty() )
394 INetURLObject aURL(_sDataSourceNameOrLocation);
395 (*this)[ (( aURL.GetProtocol() == INetProtocol::File ) ? daDatabaseLocation : daDataSource)] <<= _sDataSourceNameOrLocation;
397 else
398 (*this)[ daDataSource ] <<= OUString();
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */