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: mergekeys.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_stoc.hxx"
36 #include <com/sun/star/registry/XRegistryKey.hpp>
37 #include <com/sun/star/registry/MergeConflictException.hpp>
39 #include "mergekeys.hxx"
41 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
43 using namespace ::rtl
;
44 using namespace ::osl
;
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star
;
56 inline Link( OUString
const & name
, OUString
const & target
)
61 typedef ::std::vector
< Link
> t_links
;
63 //==================================================================================================
64 static void mergeKeys(
65 Reference
< registry::XRegistryKey
> const & xDest
,
66 Reference
< registry::XRegistryKey
> const & xSource
,
68 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
70 if (!xSource
.is() || !xSource
->isValid()) {
71 throw registry::InvalidRegistryException(
72 OUSTR("source key is null or invalid!"),
73 Reference
<XInterface
>() );
75 if (!xDest
.is() || !xDest
->isValid()) {
76 throw registry::InvalidRegistryException(
77 OUSTR("destination key is null or invalid!"),
78 Reference
<XInterface
>() );
82 switch (xSource
->getValueType())
84 case registry::RegistryValueType_NOT_DEFINED
:
86 case registry::RegistryValueType_LONG
:
87 xDest
->setLongValue( xSource
->getLongValue() );
89 case registry::RegistryValueType_ASCII
:
90 xDest
->setAsciiValue( xSource
->getAsciiValue() );
92 case registry::RegistryValueType_STRING
:
93 xDest
->setStringValue( xSource
->getStringValue() );
95 case registry::RegistryValueType_BINARY
:
96 xDest
->setBinaryValue( xSource
->getBinaryValue() );
98 case registry::RegistryValueType_LONGLIST
:
99 xDest
->setLongListValue( xSource
->getLongListValue() );
101 case registry::RegistryValueType_ASCIILIST
:
102 xDest
->setAsciiListValue( xSource
->getAsciiListValue() );
104 case registry::RegistryValueType_STRINGLIST
:
105 xDest
->setStringListValue( xSource
->getStringListValue() );
113 Sequence
< OUString
> sourceKeys( xSource
->getKeyNames() );
114 OUString
const * pSourceKeys
= sourceKeys
.getConstArray();
115 for ( sal_Int32 nPos
= sourceKeys
.getLength(); nPos
--; )
118 OUString
name( pSourceKeys
[ nPos
] );
119 sal_Int32 nSlash
= name
.lastIndexOf( '/' );
122 name
= name
.copy( nSlash
+1 );
125 if (xSource
->getKeyType( name
) == registry::RegistryKeyType_KEY
)
127 // try to open exisiting dest key or create new one
128 Reference
< registry::XRegistryKey
> xDestKey( xDest
->createKey( name
) );
129 Reference
< registry::XRegistryKey
> xSourceKey( xSource
->openKey( name
) );
130 mergeKeys( xDestKey
, xSourceKey
, links
);
131 xSourceKey
->closeKey();
132 xDestKey
->closeKey();
136 // remove existing key
137 Reference
< registry::XRegistryKey
> xDestKey( xDest
->openKey( name
) );
138 if (xDestKey
.is() && xDestKey
->isValid()) // something to remove
140 xDestKey
->closeKey();
141 if (xDest
->getKeyType( name
) == registry::RegistryKeyType_LINK
)
143 xDest
->deleteLink( name
);
147 xDest
->deleteKey( name
);
151 links
.push_back( Link(
152 pSourceKeys
[ nPos
], // abs path
153 xSource
->getResolvedName( name
) // abs resolved name
159 //==================================================================================================
161 Reference
< registry::XRegistryKey
> const & xDest
,
162 Reference
< registry::XRegistryKey
> const & xSource
)
163 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
165 if (!xDest
.is() || !xDest
->isValid()) {
166 throw registry::InvalidRegistryException(
167 OUSTR("destination key is null or invalid!"),
168 Reference
<XInterface
>() );
170 if (xDest
->isReadOnly())
172 throw registry::InvalidRegistryException(
173 OUString( RTL_CONSTASCII_USTRINGPARAM(
174 "destination registry is read-only! cannot merge!") ),
175 Reference
< XInterface
>() );
180 mergeKeys( xDest
, xSource
, links
);
182 for ( size_t nPos
= links
.size(); nPos
--; )
184 Link
const & r
= links
[ nPos
];
185 OSL_VERIFY( xDest
->createLink( r
.m_name
, r
.m_target
) );