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 css::uno
;
31 using namespace ::com::sun::star
;
43 Link( OUString name
, OUString target
)
44 : m_name(std::move( name
))
45 , m_target(std::move( target
))
51 typedef ::std::vector
< Link
> t_links
;
54 static void mergeKeys(
55 Reference
< registry::XRegistryKey
> const & xDest
,
56 Reference
< registry::XRegistryKey
> const & xSource
,
58 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
60 if (!xSource
.is() || !xSource
->isValid()) {
61 throw registry::InvalidRegistryException(
62 u
"source key is null or invalid!"_ustr
);
64 if (!xDest
.is() || !xDest
->isValid()) {
65 throw registry::InvalidRegistryException(
66 u
"destination key is null or invalid!"_ustr
);
70 switch (xSource
->getValueType())
72 case registry::RegistryValueType_NOT_DEFINED
:
74 case registry::RegistryValueType_LONG
:
75 xDest
->setLongValue( xSource
->getLongValue() );
77 case registry::RegistryValueType_ASCII
:
78 xDest
->setAsciiValue( xSource
->getAsciiValue() );
80 case registry::RegistryValueType_STRING
:
81 xDest
->setStringValue( xSource
->getStringValue() );
83 case registry::RegistryValueType_BINARY
:
84 xDest
->setBinaryValue( xSource
->getBinaryValue() );
86 case registry::RegistryValueType_LONGLIST
:
87 xDest
->setLongListValue( xSource
->getLongListValue() );
89 case registry::RegistryValueType_ASCIILIST
:
90 xDest
->setAsciiListValue( xSource
->getAsciiListValue() );
92 case registry::RegistryValueType_STRINGLIST
:
93 xDest
->setStringListValue( xSource
->getStringListValue() );
101 Sequence
< OUString
> sourceKeys( xSource
->getKeyNames() );
102 for ( sal_Int32 nPos
= sourceKeys
.getLength(); nPos
--; )
105 OUString
name( sourceKeys
[ nPos
] );
106 sal_Int32 nSlash
= name
.lastIndexOf( '/' );
109 name
= name
.copy( nSlash
+1 );
112 if (xSource
->getKeyType( name
) == registry::RegistryKeyType_KEY
)
114 // try to open existing dest key or create new one
115 Reference
< registry::XRegistryKey
> xDestKey( xDest
->createKey( name
) );
116 Reference
< registry::XRegistryKey
> xSourceKey( xSource
->openKey( name
) );
117 mergeKeys( xDestKey
, xSourceKey
, links
);
118 xSourceKey
->closeKey();
119 xDestKey
->closeKey();
123 // remove existing key
124 Reference
< registry::XRegistryKey
> xDestKey( xDest
->openKey( name
) );
125 if (xDestKey
.is() && xDestKey
->isValid()) // something to remove
127 xDestKey
->closeKey();
128 if (xDest
->getKeyType( name
) == registry::RegistryKeyType_LINK
)
130 xDest
->deleteLink( name
);
134 xDest
->deleteKey( name
);
138 links
.push_back( Link(
139 sourceKeys
[ nPos
], // abs path
140 xSource
->getResolvedName( name
) // abs resolved name
148 Reference
< registry::XRegistryKey
> const & xDest
,
149 Reference
< registry::XRegistryKey
> const & xSource
)
150 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
152 if (!xDest
.is() || !xDest
->isValid()) {
153 throw registry::InvalidRegistryException(
154 u
"destination key is null or invalid!"_ustr
);
156 if (xDest
->isReadOnly())
158 throw registry::InvalidRegistryException(
159 u
"destination registry is read-only! cannot merge!"_ustr
);
164 mergeKeys( xDest
, xSource
, links
);
166 for ( size_t nPos
= links
.size(); nPos
--; )
168 Link
const & r
= links
[ nPos
];
169 OSL_VERIFY( xDest
->createLink( r
.m_name
, r
.m_target
) );
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */