Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / stoc / source / implementationregistration / mergekeys.cxx
blobdde219b9ada77460614894070742114c67aa7e01
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <utility>
22 #include <vector>
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;
34 namespace stoc_impreg
37 namespace {
39 struct Link
41 OUString m_name;
42 OUString m_target;
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,
58 t_links & links )
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!" );
70 // write value
71 switch (xSource->getValueType())
73 case registry::RegistryValueType_NOT_DEFINED:
74 break;
75 case registry::RegistryValueType_LONG:
76 xDest->setLongValue( xSource->getLongValue() );
77 break;
78 case registry::RegistryValueType_ASCII:
79 xDest->setAsciiValue( xSource->getAsciiValue() );
80 break;
81 case registry::RegistryValueType_STRING:
82 xDest->setStringValue( xSource->getStringValue() );
83 break;
84 case registry::RegistryValueType_BINARY:
85 xDest->setBinaryValue( xSource->getBinaryValue() );
86 break;
87 case registry::RegistryValueType_LONGLIST:
88 xDest->setLongListValue( xSource->getLongListValue() );
89 break;
90 case registry::RegistryValueType_ASCIILIST:
91 xDest->setAsciiListValue( xSource->getAsciiListValue() );
92 break;
93 case registry::RegistryValueType_STRINGLIST:
94 xDest->setStringListValue( xSource->getStringListValue() );
95 break;
96 default:
97 OSL_ASSERT(false);
98 break;
101 // sub keys
102 Sequence< OUString > sourceKeys( xSource->getKeyNames() );
103 OUString const * pSourceKeys = sourceKeys.getConstArray();
104 for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
106 // key name
107 OUString name( pSourceKeys[ nPos ] );
108 sal_Int32 nSlash = name.lastIndexOf( '/' );
109 if (nSlash >= 0)
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();
123 else // link
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 );
134 else
136 xDest->deleteKey( name );
140 links.push_back( Link(
141 pSourceKeys[ nPos ], // abs path
142 xSource->getResolvedName( name ) // abs resolved name
143 ) );
149 void mergeKeys(
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!" );
164 t_links links;
165 links.reserve( 16 );
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: */