1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/diagnose.h>
22 #include <com/sun/star/ucb/XContent.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <tools/urlobj.hxx>
30 using namespace ::com::sun::star::uno
;
31 using namespace ::com::sun::star::beans
;
32 using namespace ::com::sun::star::ucb
;
34 typedef std::pair
<OUString
const, DataAccessDescriptorProperty
> PropertyMapEntry
;
36 class ODADescriptorImpl
39 bool m_bSetOutOfDate
: 1;
40 bool m_bSequenceOutOfDate
: 1;
43 typedef ::std::map
< DataAccessDescriptorProperty
, Any
> DescriptorValues
;
44 DescriptorValues m_aValues
;
45 Sequence
< PropertyValue
> m_aAsSequence
;
47 typedef ::std::map
< OUString
, DataAccessDescriptorProperty
> MapString2PropertyEntry
;
51 ODADescriptorImpl(const ODADescriptorImpl
& _rSource
);
53 void invalidateExternRepresentations();
55 void updateSequence();
57 /** builds the descriptor from a property value sequence
59 if and only if the sequence contained valid properties only
61 bool buildFrom( const Sequence
< PropertyValue
>& _rValues
);
63 /** builds the descriptor from a property set
65 if and only if the set contained valid properties only
67 bool buildFrom( const Reference
< XPropertySet
>& _rValues
);
70 static PropertyValue
buildPropertyValue( const DescriptorValues::const_iterator
& _rPos
);
71 static const MapString2PropertyEntry
& getPropertyMap( );
72 static PropertyMapEntry
const * getPropertyMapEntry( const DescriptorValues::const_iterator
& _rPos
);
75 ODADescriptorImpl::ODADescriptorImpl()
76 :m_bSetOutOfDate(true)
77 ,m_bSequenceOutOfDate(true)
81 ODADescriptorImpl::ODADescriptorImpl(const ODADescriptorImpl
& _rSource
)
82 :m_bSetOutOfDate( _rSource
.m_bSetOutOfDate
)
83 ,m_bSequenceOutOfDate( _rSource
.m_bSequenceOutOfDate
)
84 ,m_aValues( _rSource
.m_aValues
)
86 if (!m_bSequenceOutOfDate
)
87 m_aAsSequence
= _rSource
.m_aAsSequence
;
90 bool ODADescriptorImpl::buildFrom( const Sequence
< PropertyValue
>& _rValues
)
92 const MapString2PropertyEntry
& rProperties
= getPropertyMap();
94 bool bValidPropsOnly
= true;
96 // loop through the sequence, and fill our m_aValues
97 for (const PropertyValue
& rValue
: _rValues
)
99 MapString2PropertyEntry::const_iterator aPropPos
= rProperties
.find( rValue
.Name
);
100 if ( aPropPos
!= rProperties
.end() )
102 DataAccessDescriptorProperty eProperty
= aPropPos
->second
;
103 m_aValues
[eProperty
] = rValue
.Value
;
107 bValidPropsOnly
= false;
112 m_aAsSequence
= _rValues
;
113 m_bSequenceOutOfDate
= false;
116 m_bSequenceOutOfDate
= true;
118 return bValidPropsOnly
;
121 bool ODADescriptorImpl::buildFrom( const Reference
< XPropertySet
>& _rxValues
)
123 Reference
< XPropertySetInfo
> xPropInfo
;
125 xPropInfo
= _rxValues
->getPropertySetInfo();
128 OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
132 // build a PropertyValue sequence with the current values
133 const Sequence
< Property
> aProperties
= xPropInfo
->getProperties();
135 Sequence
< PropertyValue
> aValues(aProperties
.getLength());
136 PropertyValue
* pValues
= aValues
.getArray();
138 for (const Property
& rProperty
: aProperties
)
140 pValues
->Name
= rProperty
.Name
;
141 pValues
->Value
= _rxValues
->getPropertyValue(rProperty
.Name
);
145 bool bValidPropsOnly
= buildFrom(aValues
);
146 m_bSetOutOfDate
= !bValidPropsOnly
;
148 return bValidPropsOnly
;
151 void ODADescriptorImpl::invalidateExternRepresentations()
153 m_bSetOutOfDate
= true;
154 m_bSequenceOutOfDate
= true;
157 const ODADescriptorImpl::MapString2PropertyEntry
& ODADescriptorImpl::getPropertyMap( )
159 // the properties we know
160 static MapString2PropertyEntry s_aProperties
162 { OUString("ActiveConnection"), DataAccessDescriptorProperty::Connection
, },
163 { OUString("BookmarkSelection"), DataAccessDescriptorProperty::BookmarkSelection
, },
164 { OUString("Column"), DataAccessDescriptorProperty::ColumnObject
, },
165 { OUString("ColumnName"), DataAccessDescriptorProperty::ColumnName
, },
166 { OUString("Command"), DataAccessDescriptorProperty::Command
, },
167 { OUString("CommandType"), DataAccessDescriptorProperty::CommandType
, },
168 { OUString("Component"), DataAccessDescriptorProperty::Component
, },
169 { OUString("ConnectionResource"), DataAccessDescriptorProperty::ConnectionResource
, },
170 { OUString("Cursor"), DataAccessDescriptorProperty::Cursor
, },
171 { OUString("DataSourceName"), DataAccessDescriptorProperty::DataSource
, },
172 { OUString("DatabaseLocation"), DataAccessDescriptorProperty::DatabaseLocation
, },
173 { OUString("EscapeProcessing"), DataAccessDescriptorProperty::EscapeProcessing
, },
174 { OUString("Filter"), DataAccessDescriptorProperty::Filter
, },
175 { OUString("Selection"), DataAccessDescriptorProperty::Selection
, }
178 return s_aProperties
;
181 PropertyMapEntry
const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator
& _rPos
)
183 const MapString2PropertyEntry
& rProperties
= getPropertyMap();
185 DataAccessDescriptorProperty nNeededHandle
= _rPos
->first
;
187 auto loop
= std::find_if(rProperties
.begin(), rProperties
.end(),
188 [&nNeededHandle
](const MapString2PropertyEntry::value_type
& rProp
) { return nNeededHandle
== rProp
.second
; });
189 if (loop
!= rProperties
.end())
191 throw RuntimeException();
194 PropertyValue
ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator
& _rPos
)
197 PropertyMapEntry
const * pProperty
= getPropertyMapEntry( _rPos
);
199 // build the property value
200 PropertyValue aReturn
;
201 aReturn
.Name
= pProperty
->first
;
202 aReturn
.Handle
= static_cast<sal_Int32
>(pProperty
->second
);
203 aReturn
.Value
= _rPos
->second
;
204 aReturn
.State
= PropertyState_DIRECT_VALUE
;
210 void ODADescriptorImpl::updateSequence()
212 if (!m_bSequenceOutOfDate
)
215 m_aAsSequence
.realloc(m_aValues
.size());
216 PropertyValue
* pValue
= m_aAsSequence
.getArray();
218 // loop through all our values
219 for ( DescriptorValues::const_iterator aLoop
= m_aValues
.begin();
220 aLoop
!= m_aValues
.end();
224 *pValue
= buildPropertyValue(aLoop
);
227 // don't need to rebuild next time
228 m_bSequenceOutOfDate
= false;
231 ODataAccessDescriptor::ODataAccessDescriptor()
232 :m_pImpl(new ODADescriptorImpl
)
236 ODataAccessDescriptor::ODataAccessDescriptor( const ODataAccessDescriptor
& _rSource
)
237 :m_pImpl(new ODADescriptorImpl(*_rSource
.m_pImpl
))
241 ODataAccessDescriptor::ODataAccessDescriptor(ODataAccessDescriptor
&& _rSource
) noexcept
242 :m_pImpl(std::move(_rSource
.m_pImpl
))
246 ODataAccessDescriptor
& ODataAccessDescriptor::operator=(const ODataAccessDescriptor
& _rSource
)
248 if (this != &_rSource
)
249 m_pImpl
.reset(new ODADescriptorImpl(*_rSource
.m_pImpl
));
253 ODataAccessDescriptor
& ODataAccessDescriptor::operator=(ODataAccessDescriptor
&& _rSource
) noexcept
255 m_pImpl
= std::move(_rSource
.m_pImpl
);
259 ODataAccessDescriptor::ODataAccessDescriptor( const Reference
< XPropertySet
>& _rValues
)
260 :m_pImpl(new ODADescriptorImpl
)
262 m_pImpl
->buildFrom(_rValues
);
265 ODataAccessDescriptor::ODataAccessDescriptor( const Any
& _rValues
)
266 :m_pImpl(new ODADescriptorImpl
)
268 // check if we know the format in the Any
269 Sequence
< PropertyValue
> aValues
;
270 Reference
< XPropertySet
> xValues
;
271 if ( _rValues
>>= aValues
)
272 m_pImpl
->buildFrom( aValues
);
273 else if ( _rValues
>>= xValues
)
274 m_pImpl
->buildFrom( xValues
);
277 ODataAccessDescriptor::ODataAccessDescriptor( const Sequence
< PropertyValue
>& _rValues
)
278 :m_pImpl(new ODADescriptorImpl
)
280 m_pImpl
->buildFrom(_rValues
);
283 ODataAccessDescriptor::~ODataAccessDescriptor()
287 void ODataAccessDescriptor::clear()
289 m_pImpl
->m_aValues
.clear();
292 void ODataAccessDescriptor::erase(DataAccessDescriptorProperty _eWhich
)
294 OSL_ENSURE(has(_eWhich
), "ODataAccessDescriptor::erase: invalid call!");
296 m_pImpl
->m_aValues
.erase(_eWhich
);
299 bool ODataAccessDescriptor::has(DataAccessDescriptorProperty _eWhich
) const
301 return m_pImpl
->m_aValues
.find(_eWhich
) != m_pImpl
->m_aValues
.end();
304 const Any
& ODataAccessDescriptor::operator [] ( DataAccessDescriptorProperty _eWhich
) const
308 OSL_FAIL("ODataAccessDescriptor::operator[]: invalid accessor!");
309 static const Any aDummy
;
313 return m_pImpl
->m_aValues
[_eWhich
];
316 Any
& ODataAccessDescriptor::operator[] ( DataAccessDescriptorProperty _eWhich
)
318 m_pImpl
->invalidateExternRepresentations();
319 return m_pImpl
->m_aValues
[_eWhich
];
322 void ODataAccessDescriptor::initializeFrom(const Sequence
< PropertyValue
>& _rValues
)
325 m_pImpl
->buildFrom(_rValues
);
328 Sequence
< PropertyValue
> const & ODataAccessDescriptor::createPropertyValueSequence()
330 m_pImpl
->updateSequence();
331 return m_pImpl
->m_aAsSequence
;
334 OUString
ODataAccessDescriptor::getDataSource() const
336 OUString sDataSourceName
;
337 if ( has(DataAccessDescriptorProperty::DataSource
) )
338 (*this)[DataAccessDescriptorProperty::DataSource
] >>= sDataSourceName
;
339 else if ( has(DataAccessDescriptorProperty::DatabaseLocation
) )
340 (*this)[DataAccessDescriptorProperty::DatabaseLocation
] >>= sDataSourceName
;
341 return sDataSourceName
;
344 void ODataAccessDescriptor::setDataSource(const OUString
& _sDataSourceNameOrLocation
)
346 if ( !_sDataSourceNameOrLocation
.isEmpty() )
348 INetURLObject
aURL(_sDataSourceNameOrLocation
);
349 (*this)[ (( aURL
.GetProtocol() == INetProtocol::File
) ? DataAccessDescriptorProperty::DatabaseLocation
: DataAccessDescriptorProperty::DataSource
)] <<= _sDataSourceNameOrLocation
;
352 (*this)[ DataAccessDescriptorProperty::DataSource
] <<= OUString();
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */