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 .
23 #include <osl/diagnose.h>
25 #include <com/sun/star/registry/XRegistryKey.hpp>
26 #include <com/sun/star/registry/MergeConflictException.hpp>
28 #include "mergekeys.hxx"
30 using namespace ::osl
;
31 using namespace css::uno
;
32 using namespace ::com::sun::star
;
42 Link( OUString
const & name
, OUString
const & target
)
47 typedef ::std::vector
< Link
> t_links
;
50 static void mergeKeys(
51 Reference
< registry::XRegistryKey
> const & xDest
,
52 Reference
< registry::XRegistryKey
> const & xSource
,
54 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
56 if (!xSource
.is() || !xSource
->isValid()) {
57 throw registry::InvalidRegistryException(
58 "source key is null or invalid!" );
60 if (!xDest
.is() || !xDest
->isValid()) {
61 throw registry::InvalidRegistryException(
62 "destination key is null or invalid!" );
66 switch (xSource
->getValueType())
68 case registry::RegistryValueType_NOT_DEFINED
:
70 case registry::RegistryValueType_LONG
:
71 xDest
->setLongValue( xSource
->getLongValue() );
73 case registry::RegistryValueType_ASCII
:
74 xDest
->setAsciiValue( xSource
->getAsciiValue() );
76 case registry::RegistryValueType_STRING
:
77 xDest
->setStringValue( xSource
->getStringValue() );
79 case registry::RegistryValueType_BINARY
:
80 xDest
->setBinaryValue( xSource
->getBinaryValue() );
82 case registry::RegistryValueType_LONGLIST
:
83 xDest
->setLongListValue( xSource
->getLongListValue() );
85 case registry::RegistryValueType_ASCIILIST
:
86 xDest
->setAsciiListValue( xSource
->getAsciiListValue() );
88 case registry::RegistryValueType_STRINGLIST
:
89 xDest
->setStringListValue( xSource
->getStringListValue() );
97 Sequence
< OUString
> sourceKeys( xSource
->getKeyNames() );
98 OUString
const * pSourceKeys
= sourceKeys
.getConstArray();
99 for ( sal_Int32 nPos
= sourceKeys
.getLength(); nPos
--; )
102 OUString
name( pSourceKeys
[ nPos
] );
103 sal_Int32 nSlash
= name
.lastIndexOf( '/' );
106 name
= name
.copy( nSlash
+1 );
109 if (xSource
->getKeyType( name
) == registry::RegistryKeyType_KEY
)
111 // try to open exisiting dest key or create new one
112 Reference
< registry::XRegistryKey
> xDestKey( xDest
->createKey( name
) );
113 Reference
< registry::XRegistryKey
> xSourceKey( xSource
->openKey( name
) );
114 mergeKeys( xDestKey
, xSourceKey
, links
);
115 xSourceKey
->closeKey();
116 xDestKey
->closeKey();
120 // remove existing key
121 Reference
< registry::XRegistryKey
> xDestKey( xDest
->openKey( name
) );
122 if (xDestKey
.is() && xDestKey
->isValid()) // something to remove
124 xDestKey
->closeKey();
125 if (xDest
->getKeyType( name
) == registry::RegistryKeyType_LINK
)
127 xDest
->deleteLink( name
);
131 xDest
->deleteKey( name
);
135 links
.push_back( Link(
136 pSourceKeys
[ nPos
], // abs path
137 xSource
->getResolvedName( name
) // abs resolved name
145 Reference
< registry::XRegistryKey
> const & xDest
,
146 Reference
< registry::XRegistryKey
> const & xSource
)
147 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
149 if (!xDest
.is() || !xDest
->isValid()) {
150 throw registry::InvalidRegistryException(
151 "destination key is null or invalid!" );
153 if (xDest
->isReadOnly())
155 throw registry::InvalidRegistryException(
156 "destination registry is read-only! cannot merge!" );
161 mergeKeys( xDest
, xSource
, links
);
163 for ( size_t nPos
= links
.size(); nPos
--; )
165 Link
const & r
= links
[ nPos
];
166 OSL_VERIFY( xDest
->createLink( r
.m_name
, r
.m_target
) );
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */