1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: updatedata.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "updatedata.hxx"
36 #include <com/sun/star/configuration/backend/NodeAttribute.hpp>
37 #include <com/sun/star/configuration/backend/XLayerHandler.hpp>
44 // -----------------------------------------------------------------------------
47 // -----------------------------------------------------------------------------
49 ElementUpdate::ElementUpdate(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, sal_Int16 _nFlagsMask
)
53 , m_nFlagsMask(_nFlagsMask
)
56 // -----------------------------------------------------------------------------
58 sal_Int16
ElementUpdate::updateFlags(sal_Int16 _nFlags
) const
60 return (_nFlags
& ~m_nFlagsMask
) | (m_nFlags
& m_nFlagsMask
);
62 // -----------------------------------------------------------------------------
64 NodeUpdate
* ElementUpdate::asNodeUpdate(bool )
68 // -----------------------------------------------------------------------------
70 PropertyUpdate
* ElementUpdate::asPropertyUpdate()
74 // -----------------------------------------------------------------------------
75 // -----------------------------------------------------------------------------
77 NodeUpdate::NodeUpdate(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, sal_Int16 _nFlagsMask
, Op _op
)
78 : ElementUpdate(_pParent
,_aName
,_nFlags
, _nFlagsMask
)
81 , m_aRemovedElements()
85 // -----------------------------------------------------------------------------
87 NodeUpdate
* NodeUpdate::asNodeUpdate(bool _bMerged
)
89 return (!_bMerged
|| m_op
== modify
90 || (updateFlags() & backenduno::NodeAttribute::FUSE
) != 0)
93 // -----------------------------------------------------------------------------
95 bool NodeUpdate::addNodeUpdate(rtl::Reference
<ElementUpdate
> const & _aNode
)
97 OSL_PRECOND( _aNode
.is(), "ERROR: NodeUpdate: Trying to add NULL node.");
98 OSL_PRECOND( _aNode
->getParent() == this, "ERROR: NodeUpdate: Node being added has wrong parent.");
99 OSL_ENSURE(m_aNodes
.find(_aNode
->getName()) == m_aNodes
.end(),
100 "NodeUpdate: Child node being added already exists in this node.");
102 return m_aNodes
.insert( ElementList::value_type(_aNode
->getName(),_aNode
) ).second
;
104 // -----------------------------------------------------------------------------
106 bool NodeUpdate::addPropertyUpdate(rtl::Reference
<ElementUpdate
> const & _aProp
)
108 OSL_PRECOND( _aProp
.is(), "ERROR: NodeUpdate: Trying to add NULL property.");
109 OSL_PRECOND( _aProp
->getParent() == this, "ERROR: NodeUpdate: Property being added has wrong parent.");
110 OSL_ENSURE(m_aProperties
.find(_aProp
->getName()) == m_aProperties
.end(),
111 "NodeUpdate: Property being added already exists in this node.");
113 return m_aProperties
.insert( ElementList::value_type(_aProp
->getName(),_aProp
) ).second
;
115 // -----------------------------------------------------------------------------
117 void NodeUpdate::removeNodeByName(rtl::OUString
const & _aName
)
119 ElementList::iterator it
= m_aNodes
.find(_aName
);
120 OSL_ENSURE(it
!= m_aNodes
.end(),
121 "NodeUpdate: Child node being removed is not in this node.");
123 if (it
!= m_aNodes
.end())
125 m_aRemovedElements
.insert(*it
);
129 // -----------------------------------------------------------------------------
131 void NodeUpdate::removePropertyByName (rtl::OUString
const & _aName
)
133 ElementList::iterator it
= m_aProperties
.find(_aName
);
134 OSL_ENSURE(it
!= m_aProperties
.end(),
135 "NodeUpdate: Property being removed is not in this node.");
137 if (it
!= m_aProperties
.end())
139 m_aRemovedElements
.insert(*it
);
140 m_aProperties
.erase(it
);
143 // -----------------------------------------------------------------------------
145 rtl::Reference
<ElementUpdate
> NodeUpdate::getNodeByName(rtl::OUString
const & _aName
) const
147 ElementList::const_iterator it
= m_aNodes
.find(_aName
);
149 return it
!= m_aNodes
.end() ? it
->second
: rtl::Reference
<ElementUpdate
>();
151 // -----------------------------------------------------------------------------
153 rtl::Reference
<ElementUpdate
> NodeUpdate::getPropertyByName (rtl::OUString
const & _aName
) const
155 ElementList::const_iterator it
= m_aProperties
.find(_aName
);
157 return it
!= m_aProperties
.end() ? it
->second
: rtl::Reference
<ElementUpdate
>();
159 // -----------------------------------------------------------------------------
161 void NodeUpdate::writeChildrenToLayer(backenduno::XLayerHandler
* _pLayer
)
164 for (ElementList::const_iterator itP
= beginProperties(); itP
!= endProperties(); ++itP
)
165 itP
->second
->writeToLayer(_pLayer
);
167 for (ElementList::const_iterator itN
= beginNodes(); itN
!= endNodes(); ++itN
)
168 itN
->second
->writeToLayer(_pLayer
);
170 // -----------------------------------------------------------------------------
171 // -----------------------------------------------------------------------------
173 NodeModification::NodeModification(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, sal_Int16 _nFlagsMask
, sal_Bool _bReset
)
174 : NodeUpdate(_pParent
,_aName
,_nFlags
, _nFlagsMask
, _bReset
? reset
: modify
)
177 // -----------------------------------------------------------------------------
179 void NodeModification::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
183 if ( this->getOperation() == reset
&& // if we have an empty
184 !this->changedFlags() && // 'reset' node, that means
185 !this->hasChildren() ) // we need to write
188 _pLayer
->overrideNode( this->getName(), this->updateFlags(), false );
189 this->writeChildrenToLayer(_pLayer
);
192 // -----------------------------------------------------------------------------
193 // -----------------------------------------------------------------------------
195 NodeReplace::NodeReplace(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
)
196 : NodeUpdate(_pParent
,_aName
,_nFlags
, _nFlags
, replace
)
198 , m_aTemplateComponent()
201 // -----------------------------------------------------------------------------
203 NodeReplace::NodeReplace(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, rtl::OUString
const & _aTemplateName
, rtl::OUString
const & _aTemplateComponent
)
204 : NodeUpdate(_pParent
,_aName
,_nFlags
, _nFlags
, replace
)
205 , m_aTemplateName(_aTemplateName
)
206 , m_aTemplateComponent(_aTemplateComponent
)
209 // -----------------------------------------------------------------------------
211 bool NodeReplace::hasTemplate() const
213 return m_aTemplateName
.getLength() != 0;
215 // -----------------------------------------------------------------------------
217 void NodeReplace::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
221 if (this->hasTemplate())
223 backenduno::TemplateIdentifier
aTemplate( m_aTemplateName
, m_aTemplateComponent
);
224 _pLayer
->addOrReplaceNodeFromTemplate( this->getName(), aTemplate
, this->updateFlags() );
227 _pLayer
->addOrReplaceNode( this->getName(), this->updateFlags() );
229 this->writeChildrenToLayer(_pLayer
);
232 // -----------------------------------------------------------------------------
233 // -----------------------------------------------------------------------------
235 NodeDrop::NodeDrop(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
)
236 : ElementUpdate(_pParent
,_aName
,0,0)
239 // -----------------------------------------------------------------------------
241 void NodeDrop::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
244 _pLayer
->dropNode(this->getName());
246 // -----------------------------------------------------------------------------
247 // -----------------------------------------------------------------------------
249 PropertyUpdate::PropertyUpdate(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, sal_Int16 _nFlagsMask
, uno::Type
const & _aType
)
250 : ElementUpdate(_pParent
,_aName
,_nFlags
,_nFlagsMask
)
254 // -----------------------------------------------------------------------------
256 PropertyUpdate
* PropertyUpdate::asPropertyUpdate()
260 // -----------------------------------------------------------------------------
262 static uno::Any
makeResetMarker()
264 uno::Reference
< backenduno::XLayerHandler
> xNull
;
265 return uno::makeAny(xNull
);
267 // -----------------------------------------------------------------------------
269 inline bool PropertyUpdate::isResetMarker(uno::Any
const & _aValue
)
271 OSL_ENSURE( _aValue
.getValueTypeClass() != uno::TypeClass_INTERFACE
||
272 _aValue
== makeResetMarker() && _aValue
.getValueType() == makeResetMarker().getValueType(),
273 "Unexpected any: Interface reference will be taken as reset marker");
275 return _aValue
.getValueTypeClass() == uno::TypeClass_INTERFACE
;
277 // -----------------------------------------------------------------------------
279 uno::Any
const & PropertyUpdate::getResetMarker()
281 static uno::Any
const aMarker
= makeResetMarker();
283 OSL_ASSERT( isResetMarker(aMarker
) );
287 // -----------------------------------------------------------------------------
289 bool PropertyUpdate::setValueFor(rtl::OUString
const & _aLocale
, uno::Any
const & _aValueUpdate
)
291 OSL_PRECOND( !isResetMarker(_aValueUpdate
), "PropertyUpdate: ERROR: Trying to set a reset marker as regular value" );
293 OSL_ENSURE(m_aValues
.find(_aLocale
) == m_aValues
.end(),
294 "PropertyUpdate: Locale being added already has a value in this property.");
296 if (_aValueUpdate
.hasValue())
298 if (m_aType
.getTypeClass() == uno::TypeClass_ANY
)
299 m_aType
= _aValueUpdate
.getValueType();
302 OSL_ENSURE( m_aType
== _aValueUpdate
.getValueType() ||
303 m_aType
== uno::Type(),
304 "ValueType mismatch in PropertyUpdate");
306 return m_aValues
.insert( ValueList::value_type(_aLocale
,_aValueUpdate
) ).second
;
308 // -----------------------------------------------------------------------------
310 bool PropertyUpdate::resetValueFor(rtl::OUString
const & _aLocale
)
312 OSL_ENSURE(m_aValues
.find(_aLocale
) == m_aValues
.end(),
313 "PropertyUpdate: Locale being reset already has a value in this property.");
315 return m_aValues
.insert( ValueList::value_type(_aLocale
,getResetMarker()) ).second
;
317 // -----------------------------------------------------------------------------
319 void PropertyUpdate::removeValueFor(rtl::OUString
const & _aLocale
)
321 OSL_ENSURE(m_aValues
.find(_aLocale
) != m_aValues
.end(),
322 "PropertyUpdate: Locale being removed is not in this node.");
324 m_aValues
.erase(_aLocale
);
326 // -----------------------------------------------------------------------------
328 void PropertyUpdate::finishValue()
330 if (m_aType
.getTypeClass() == uno::TypeClass_ANY
)
331 m_aType
= uno::Type();
333 // -----------------------------------------------------------------------------
335 bool PropertyUpdate::hasValueFor(rtl::OUString
const & _aLocale
) const
337 ValueList::const_iterator it
= m_aValues
.find(_aLocale
);
339 return it
!= m_aValues
.end() && ! isResetMarker(it
->second
);
341 // -----------------------------------------------------------------------------
343 bool PropertyUpdate::hasResetFor(rtl::OUString
const & _aLocale
) const
345 ValueList::const_iterator it
= m_aValues
.find(_aLocale
);
347 return it
!= m_aValues
.end() && isResetMarker(it
->second
);
349 // -----------------------------------------------------------------------------
351 bool PropertyUpdate::hasChangeFor(rtl::OUString
const & _aLocale
) const
353 ValueList::const_iterator it
= m_aValues
.find(_aLocale
);
355 return it
!= m_aValues
.end();
357 // -----------------------------------------------------------------------------
359 uno::Any
PropertyUpdate::getValueFor(rtl::OUString
const & _aLocale
) const
361 ValueList::const_iterator it
= m_aValues
.find(_aLocale
);
363 OSL_ENSURE(it
!= m_aValues
.end() && !isResetMarker(it
->second
),
364 "PropertyUpdate: Should not call getValue() unless hasValue() returns true" );
366 return it
!= m_aValues
.end() && !isResetMarker(it
->second
) ? it
->second
: uno::Any();
368 // -----------------------------------------------------------------------------
370 void PropertyUpdate::writeValueToLayer(backenduno::XLayerHandler
* _pLayer
, uno::Any
const & _aValue
)
373 if ( !isResetMarker(_aValue
) )
374 _pLayer
->setPropertyValue(_aValue
);
376 // else - to reset - do nothing
378 // -----------------------------------------------------------------------------
380 void PropertyUpdate::writeValueToLayerFor(backenduno::XLayerHandler
* _pLayer
, uno::Any
const & _aValue
, rtl::OUString
const & _aLocale
)
383 if (_aLocale
== this->primarySlot())
384 this->writeValueToLayer(_pLayer
,_aValue
);
386 else if ( !isResetMarker(_aValue
) )
387 _pLayer
->setPropertyValueForLocale(_aValue
,_aLocale
);
389 // else - to reset - do nothing
391 // -----------------------------------------------------------------------------
393 void PropertyUpdate::writeValuesToLayer(backenduno::XLayerHandler
* _pLayer
)
396 for (ValueList::const_iterator itV
= beginValues(); itV
!= endValues(); ++itV
)
397 this->writeValueToLayerFor(_pLayer
, itV
->second
, itV
->first
);
399 // -----------------------------------------------------------------------------
401 void PropertyUpdate::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
405 _pLayer
->overrideProperty( this->getName(), this->updateFlags(), this->m_aType
, false );
406 this->writeValuesToLayer(_pLayer
);
407 _pLayer
->endProperty();
409 // -----------------------------------------------------------------------------
410 // -----------------------------------------------------------------------------
413 PropertyAdd::PropertyAdd(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, uno::Type
const & _aType
)
414 : ElementUpdate(_pParent
,_aName
,_nFlags
,_nFlags
)
415 , m_aValueType(_aType
)
420 // -----------------------------------------------------------------------------
422 PropertyAdd::PropertyAdd(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
, sal_Int16 _nFlags
, uno::Any
const & _aValue
)
423 : ElementUpdate(_pParent
,_aName
,_nFlags
,_nFlags
)
424 , m_aValueType(_aValue
.getValueType())
428 // -----------------------------------------------------------------------------
430 void PropertyAdd::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
433 if (this->hasValue())
434 _pLayer
->addPropertyWithValue(this->getName(),this->updateFlags(),this->getValue());
436 _pLayer
->addProperty(this->getName(),this->updateFlags(),this->getValueType());
438 // -----------------------------------------------------------------------------
439 // -----------------------------------------------------------------------------
441 PropertyReset::PropertyReset(NodeUpdate
* _pParent
, rtl::OUString
const & _aName
)
442 : ElementUpdate(_pParent
,_aName
,0,0)
445 // -----------------------------------------------------------------------------
447 void PropertyReset::writeToLayer(backenduno::XLayerHandler
* _pLayer
)
449 (void) _pLayer
; // avoid warning about unused parameter
451 // skip - nothing to write
453 // -----------------------------------------------------------------------------
454 // -----------------------------------------------------------------------------
455 } // namespace backend
457 // -------------------------------------------------------------------------
458 } // namespace configmgr