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: valuemembernode.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"
35 #include "valuemembernode.hxx"
36 #include "nodeimplobj.hxx"
37 #include "nodechangeimpl.hxx"
39 #include "valuenode.hxx"
40 #include <osl/diagnose.h>
44 namespace configuration
48 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 // internal accessors for direct updates to data
55 //-----------------------------------------------------------------------------
58 void setOriginalValue(sharable::ValueNode
* const& rOriginalAddress
, com::sun::star::uno::Any
const& aNewValue
)
60 rOriginalAddress
->setValue(aNewValue
);
62 //-----------------------------------------------------------------------------
65 void setOriginalToDefault(sharable::ValueNode
* const& rOriginalAddress
)
67 rOriginalAddress
->setToDefault();
69 } // anonymous namespace
72 //-----------------------------------------------------------------------------
73 // class ValueMemberNode
74 //-----------------------------------------------------------------------------
76 ValueMemberNode::ValueMemberNode(sharable::ValueNode
* node
)
78 , m_xDeferredOperation()
80 //-----------------------------------------------------------------------------
81 ValueMemberNode::ValueMemberNode(rtl::Reference
<DeferredImpl
> const& _xDeferred
) // must be valid
82 : m_node( _xDeferred
->getOriginalNode() )
83 , m_xDeferredOperation(_xDeferred
)
85 //-----------------------------------------------------------------------------
87 ValueMemberNode::ValueMemberNode(ValueMemberNode
const& rOriginal
)
88 : m_node(rOriginal
.m_node
)
89 , m_xDeferredOperation(rOriginal
.m_xDeferredOperation
)
91 //-----------------------------------------------------------------------------
92 ValueMemberNode
& ValueMemberNode::operator=(ValueMemberNode
const& rOriginal
)
94 m_node
= rOriginal
.m_node
;
95 m_xDeferredOperation
= rOriginal
.m_xDeferredOperation
;
98 //-----------------------------------------------------------------------------
99 ValueMemberNode::~ValueMemberNode()
102 //-----------------------------------------------------------------------------
104 bool ValueMemberNode::isValid() const
106 OSL_ASSERT( !m_xDeferredOperation
.is() ||
107 m_node
== m_xDeferredOperation
->getOriginalNode());
111 //-----------------------------------------------------------------------------
113 bool ValueMemberNode::hasChange() const
115 return m_xDeferredOperation
.is()
116 && m_xDeferredOperation
->isChange();
118 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
122 // external accessors
123 //-----------------------------------------------------------------------------
125 rtl::OUString
ValueMemberNode::getNodeName() const
127 return m_node
->info
.getName();
129 //-----------------------------------------------------------------------------
131 node::Attributes
ValueMemberNode::getAttributes() const
133 return sharable::node(m_node
)->getAttributes();
135 //-----------------------------------------------------------------------------
138 bool ValueMemberNode::isDefault() const
141 return m_xDeferredOperation
->isToDefault();
143 return m_node
->info
.isDefault();
145 //-----------------------------------------------------------------------------
147 bool ValueMemberNode::canGetDefaultValue() const
149 return m_node
->hasUsableDefault();
151 //-----------------------------------------------------------------------------
153 com::sun::star::uno::Any
ValueMemberNode::getValue() const
156 return m_xDeferredOperation
->getNewValue();
158 return m_node
->getValue();
160 //-----------------------------------------------------------------------------
162 com::sun::star::uno::Any
ValueMemberNode::getDefaultValue() const
164 return m_node
->getDefaultValue();
166 //-----------------------------------------------------------------------------
168 com::sun::star::uno::Type
ValueMemberNode::getValueType() const
170 return m_node
->getValueType();
172 //-----------------------------------------------------------------------------
175 void ValueMemberUpdate::setValue(com::sun::star::uno::Any
const& aNewValue
)
177 if (m_aMemberNode
.m_xDeferredOperation
.is())
178 m_aMemberNode
.m_xDeferredOperation
->setValue(aNewValue
, m_aMemberNode
.m_node
);
180 setOriginalValue(m_aMemberNode
.m_node
, aNewValue
);
182 //-----------------------------------------------------------------------------
184 void ValueMemberUpdate::setDefault()
186 if (m_aMemberNode
.m_xDeferredOperation
.is())
187 m_aMemberNode
.m_xDeferredOperation
->setValueToDefault(m_aMemberNode
.m_node
);
189 setOriginalToDefault(m_aMemberNode
.m_node
);
192 //-----------------------------------------------------------------------------
193 // class ValueMemberNode::DeferredImpl
194 //-----------------------------------------------------------------------------
196 ValueMemberNode::DeferredImpl::DeferredImpl(sharable::ValueNode
* valueNode
)
197 : m_valueNode(valueNode
)
198 , m_aNewValue(valueNode
->getValue())
199 , m_bToDefault(false)
202 //-----------------------------------------------------------------------------
204 void ValueMemberNode::DeferredImpl::setValue(com::sun::star::uno::Any
const& aNewValue
, sharable::ValueNode
* originalNode
)
206 OSL_ENSURE(originalNode
== m_valueNode
, "Incorrect original node passed");
208 m_aNewValue
= aNewValue
;
209 m_bToDefault
= false;
211 m_bChange
= originalNode
->info
.isDefault() || aNewValue
!= originalNode
->getValue();
213 //-----------------------------------------------------------------------------
215 void ValueMemberNode::DeferredImpl::setValueToDefault(sharable::ValueNode
* originalNode
)
217 OSL_ENSURE(originalNode
== m_valueNode
, "Incorrect original node passed");
219 m_aNewValue
= originalNode
->getDefaultValue();
222 m_bChange
= !originalNode
->info
.isDefault();
224 //-----------------------------------------------------------------------------
226 std::auto_ptr
<ValueChange
> ValueMemberNode::DeferredImpl::preCommitChange()
228 OSL_ENSURE(isChange(), "Trying to commit a non-change");
230 sharable::ValueNode
* originalNode
= getOriginalNode();
232 // first find the mode of the change
233 ValueChange::Mode eMode
;
236 eMode
= ValueChange::setToDefault
;
238 else if (! originalNode
->info
.isDefault())
239 eMode
= ValueChange::changeValue
;
242 eMode
= ValueChange::wasDefault
;
244 // now make a ValueChange
245 std::auto_ptr
<ValueChange
>pChange( new ValueChange( originalNode
->info
.getName(),
246 sharable::node(originalNode
)->getAttributes(),
249 originalNode
->getValue()
254 //-----------------------------------------------------------------------------
256 void ValueMemberNode::DeferredImpl::finishCommit(ValueChange
& rChange
)
259 OSL_ENSURE(rChange
.getNewValue() == this->getNewValue(),"Committed change does not match the intended value");
261 sharable::ValueNode
* originalNode
= getOriginalNode();
263 m_aNewValue
= originalNode
->getValue();
264 m_bToDefault
= false;
266 OSL_ENSURE(rChange
.getNewValue() == m_aNewValue
,"Committed change does not match the actual value");
269 //-----------------------------------------------------------------------------
271 void ValueMemberNode::DeferredImpl::revertCommit(ValueChange
& rChange
)
275 OSL_ENSURE(rChange
.getNewValue() == this->getNewValue(),"Reverted change does not match the intended value");
276 OSL_ENSURE(isChange(), "ValueMemeberNode::DeferredImpl: No Changes to revert");
278 //-----------------------------------------------------------------------------
280 void ValueMemberNode::DeferredImpl::failedCommit(ValueChange
&)
282 sharable::ValueNode
* originalNode
= getOriginalNode();
284 // discard the change
285 m_aNewValue
= originalNode
->getValue();
286 m_bToDefault
= false;
290 //-----------------------------------------------------------------------------
291 ValueChangeImpl
* ValueMemberNode::DeferredImpl::collectChange()
293 sharable::ValueNode
* originalNode
= getOriginalNode();
295 com::sun::star::uno::Any aOldValue
= originalNode
->getValue();
300 else if (m_bToDefault
)
302 OSL_ASSERT( m_aNewValue
== originalNode
->getDefaultValue() );
303 return new ValueResetImpl( m_aNewValue
, aOldValue
);
308 return new ValueReplaceImpl( m_aNewValue
, aOldValue
);
311 //-----------------------------------------------------------------------------
313 ValueChangeImpl
* ValueMemberNode::DeferredImpl::adjustToChange(ValueChange
const& rExternalChange
)
319 else if (m_bToDefault
&& rExternalChange
.getMode() == ValueChange::changeDefault
)
321 OSL_ASSERT( m_aNewValue
== rExternalChange
.getOldValue() );
323 m_aNewValue
= rExternalChange
.getNewValue();
325 return new ValueReplaceImpl(m_aNewValue
, rExternalChange
.getOldValue());
327 else // return Surrogate - does not honor m_bToDefault
329 return new ValueReplaceImpl(m_aNewValue
, m_aNewValue
);
332 //-----------------------------------------------------------------------------
334 //-----------------------------------------------------------------------------