merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / treemgr / roottree.cxx
blob0b8e895cb0ed97d0d121bf1fd1fddd3fb4f8fbfe
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: roottree.cxx,v $
10 * $Revision: 1.21 $
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"
33 #include <stdio.h>
35 #include "roottree.hxx"
36 #include "roottreeimpl.hxx"
37 #include "viewaccess.hxx"
38 #include "viewfactory.hxx"
39 #include "noderef.hxx"
40 #include "nodechangeinfo.hxx"
41 #include "treechangelist.hxx"
43 namespace configmgr
45 namespace configuration
47 //-----------------------------------------------------------------------------
48 // factory methods
49 //-----------------------------------------------------------------------------
51 rtl::Reference< Tree > createReadOnlyTree( AbsolutePath const& aRootPath,
52 sharable::Node * cacheNode,
53 unsigned int nDepth,
54 TemplateProvider const& aTemplateProvider)
56 return new RootTree(view::createReadOnlyStrategy(),
57 aRootPath, cacheNode, nDepth,
58 aTemplateProvider
59 );
61 //-----------------------------------------------------------------------------
63 rtl::Reference< Tree > createUpdatableTree( AbsolutePath const& aRootPath,
64 sharable::Node * cacheNode,
65 unsigned int nDepth,
66 TemplateProvider const& aTemplateProvider)
68 return new RootTree(view::createDeferredChangeStrategy(),
69 aRootPath, cacheNode, nDepth,
70 aTemplateProvider
71 );
74 //-----------------------------------------------------------------------------
75 // update on notify method
76 //-----------------------------------------------------------------------------
77 bool adjustToChanges( NodeChangesInformation& rLocalChanges,
78 rtl::Reference< Tree > const& aBaseTree, NodeRef const& aBaseNode,
79 SubtreeChange const& aExternalChange)
81 OSL_PRECOND( !isEmpty(aBaseTree.get()), "ERROR: Configuration: Tree operation requires a valid Tree");
82 OSL_PRECOND( aBaseNode.isValid() && aBaseTree->isValidNode(aBaseNode.getOffset()), "ERROR: Configuration: NodeRef does not match Tree");
84 if (!isEmpty(aBaseTree.get()))
86 OSL_ENSURE(rLocalChanges.empty(), "Should pass empty container to adjustToChanges(...)");
88 view::ViewTreeAccess(aBaseTree.get()).adjustToChanges(rLocalChanges, aBaseNode, aExternalChange);
90 return !rLocalChanges.empty();
92 else
93 return false;
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
98 // class CommitHelper
99 //-----------------------------------------------------------------------------
100 struct CommitHelper::Data
102 std::vector< rtl::Reference<ElementTree> > m_aRemovedElements; // filled to keep the elements alive 'till after notification
105 //-----------------------------------------------------------------------------
106 CommitHelper::CommitHelper(rtl::Reference< Tree > const& aTree)
107 : m_pData( )
108 , m_pTree( aTree.get() )
110 OSL_ENSURE(m_pTree, "INTERNAL ERROR: Unexpected NULL tree in commit helper");
112 //-----------------------------------------------------------------------------
113 CommitHelper::~CommitHelper()
117 //-----------------------------------------------------------------------------
118 bool CommitHelper::prepareCommit(TreeChangeList& rChangeList)
120 OSL_ENSURE(m_pTree,"ERROR: CommitHelper: Cannot commit without a tree");
121 if (m_pTree == NULL)
122 return false;
124 OSL_ENSURE(m_pData.get() == NULL,"ERROR: CommitHelper: Need to reset before reusing");
125 m_pData.reset( new Data() );
127 // get and check the changes
128 std::auto_ptr<SubtreeChange> pTreeChange(view::ViewTreeAccess(m_pTree).preCommitChanges(m_pData->m_aRemovedElements));
129 if (pTreeChange.get() == NULL)
130 return false;
132 // find the name and path of the change
133 OSL_ENSURE(m_pTree->getSimpleRootName() == pTreeChange->getNodeName(), "ERROR in Commit: Change name mismatch");
135 // now fill the TreeChangeList
136 rChangeList.setRootPath( m_pTree->getRootPath() );
137 rChangeList.root.swap( *pTreeChange );
139 return true;
141 //-----------------------------------------------------------------------------
143 void CommitHelper::finishCommit(TreeChangeList& rChangeList)
145 OSL_ENSURE(m_pTree,"INTERNAL ERROR: Nothing to finish without a tree");
147 // find the name and path of the change
148 AbsolutePath aPath = m_pTree->getRootPath();
150 OSL_ENSURE( rChangeList.getRootNodePath().toString() == aPath.toString(), "ERROR: FinishCommit cannot handle rebased changes trees");
151 if ( !matches(rChangeList.getRootNodePath(), aPath) )
152 throw configuration::Exception("INTERNAL ERROR: FinishCommit cannot handle rebased changes trees");
154 view::ViewTreeAccess(m_pTree).finishCommit(rChangeList.root);
156 //-----------------------------------------------------------------------------
158 void CommitHelper::failedCommit(TreeChangeList& rChangeList)
160 OSL_ENSURE(m_pTree,"INTERNAL ERROR: Nothing to finish without a tree");
162 AbsolutePath aPath = m_pTree->getRootPath();
164 OSL_ENSURE( rChangeList.getRootNodePath().toString() == aPath.toString(), "ERROR: FinishCommit cannot handle rebased changes trees");
165 if ( !matches(rChangeList.getRootNodePath(), aPath) )
166 throw configuration::Exception("INTERNAL ERROR: FinishCommit cannot handle rebased changes trees");
168 view::ViewTreeAccess(m_pTree).recoverFailedCommit(rChangeList.root);
170 //-----------------------------------------------------------------------------