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 .
24 #include <osl/diagnose.h>
26 #include <com/sun/star/registry/XRegistryKey.hpp>
28 #include "mergekeys.hxx"
30 using namespace ::osl
;
31 using namespace css::uno
;
32 using namespace ::com::sun::star
;
44 Link( OUString name
, OUString target
)
45 : m_name(std::move( name
))
46 , m_target(std::move( target
))
52 typedef ::std::vector
< Link
> t_links
;
55 static void mergeKeys(
56 Reference
< registry::XRegistryKey
> const & xDest
,
57 Reference
< registry::XRegistryKey
> const & xSource
,
59 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
61 if (!xSource
.is() || !xSource
->isValid()) {
62 throw registry::InvalidRegistryException(
63 "source key is null or invalid!" );
65 if (!xDest
.is() || !xDest
->isValid()) {
66 throw registry::InvalidRegistryException(
67 "destination key is null or invalid!" );
71 switch (xSource
->getValueType())
73 case registry::RegistryValueType_NOT_DEFINED
:
75 case registry::RegistryValueType_LONG
:
76 xDest
->setLongValue( xSource
->getLongValue() );
78 case registry::RegistryValueType_ASCII
:
79 xDest
->setAsciiValue( xSource
->getAsciiValue() );
81 case registry::RegistryValueType_STRING
:
82 xDest
->setStringValue( xSource
->getStringValue() );
84 case registry::RegistryValueType_BINARY
:
85 xDest
->setBinaryValue( xSource
->getBinaryValue() );
87 case registry::RegistryValueType_LONGLIST
:
88 xDest
->setLongListValue( xSource
->getLongListValue() );
90 case registry::RegistryValueType_ASCIILIST
:
91 xDest
->setAsciiListValue( xSource
->getAsciiListValue() );
93 case registry::RegistryValueType_STRINGLIST
:
94 xDest
->setStringListValue( xSource
->getStringListValue() );
102 Sequence
< OUString
> sourceKeys( xSource
->getKeyNames() );
103 OUString
const * pSourceKeys
= sourceKeys
.getConstArray();
104 for ( sal_Int32 nPos
= sourceKeys
.getLength(); nPos
--; )
107 OUString
name( pSourceKeys
[ nPos
] );
108 sal_Int32 nSlash
= name
.lastIndexOf( '/' );
111 name
= name
.copy( nSlash
+1 );
114 if (xSource
->getKeyType( name
) == registry::RegistryKeyType_KEY
)
116 // try to open existing dest key or create new one
117 Reference
< registry::XRegistryKey
> xDestKey( xDest
->createKey( name
) );
118 Reference
< registry::XRegistryKey
> xSourceKey( xSource
->openKey( name
) );
119 mergeKeys( xDestKey
, xSourceKey
, links
);
120 xSourceKey
->closeKey();
121 xDestKey
->closeKey();
125 // remove existing key
126 Reference
< registry::XRegistryKey
> xDestKey( xDest
->openKey( name
) );
127 if (xDestKey
.is() && xDestKey
->isValid()) // something to remove
129 xDestKey
->closeKey();
130 if (xDest
->getKeyType( name
) == registry::RegistryKeyType_LINK
)
132 xDest
->deleteLink( name
);
136 xDest
->deleteKey( name
);
140 links
.push_back( Link(
141 pSourceKeys
[ nPos
], // abs path
142 xSource
->getResolvedName( name
) // abs resolved name
150 Reference
< registry::XRegistryKey
> const & xDest
,
151 Reference
< registry::XRegistryKey
> const & xSource
)
152 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
154 if (!xDest
.is() || !xDest
->isValid()) {
155 throw registry::InvalidRegistryException(
156 "destination key is null or invalid!" );
158 if (xDest
->isReadOnly())
160 throw registry::InvalidRegistryException(
161 "destination registry is read-only! cannot merge!" );
166 mergeKeys( xDest
, xSource
, links
);
168 for ( size_t nPos
= links
.size(); nPos
--; )
170 Link
const & r
= links
[ nPos
];
171 OSL_VERIFY( xDest
->createLink( r
.m_name
, r
.m_target
) );
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */