Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / stoc / source / implementationregistration / mergekeys.cxx
blobdf24ca697ec2c85a8bc99c1cfc444402df2b62a6
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 <vector>
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;
34 namespace stoc_impreg
37 struct Link
39 OUString m_name;
40 OUString m_target;
42 Link( OUString const & name, OUString const & target )
43 : m_name( name )
44 , m_target( target )
47 typedef ::std::vector< Link > t_links;
50 static void mergeKeys(
51 Reference< registry::XRegistryKey > const & xDest,
52 Reference< registry::XRegistryKey > const & xSource,
53 t_links & links )
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!" );
65 // write value
66 switch (xSource->getValueType())
68 case registry::RegistryValueType_NOT_DEFINED:
69 break;
70 case registry::RegistryValueType_LONG:
71 xDest->setLongValue( xSource->getLongValue() );
72 break;
73 case registry::RegistryValueType_ASCII:
74 xDest->setAsciiValue( xSource->getAsciiValue() );
75 break;
76 case registry::RegistryValueType_STRING:
77 xDest->setStringValue( xSource->getStringValue() );
78 break;
79 case registry::RegistryValueType_BINARY:
80 xDest->setBinaryValue( xSource->getBinaryValue() );
81 break;
82 case registry::RegistryValueType_LONGLIST:
83 xDest->setLongListValue( xSource->getLongListValue() );
84 break;
85 case registry::RegistryValueType_ASCIILIST:
86 xDest->setAsciiListValue( xSource->getAsciiListValue() );
87 break;
88 case registry::RegistryValueType_STRINGLIST:
89 xDest->setStringListValue( xSource->getStringListValue() );
90 break;
91 default:
92 OSL_ASSERT(false);
93 break;
96 // sub keys
97 Sequence< OUString > sourceKeys( xSource->getKeyNames() );
98 OUString const * pSourceKeys = sourceKeys.getConstArray();
99 for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
101 // key name
102 OUString name( pSourceKeys[ nPos ] );
103 sal_Int32 nSlash = name.lastIndexOf( '/' );
104 if (nSlash >= 0)
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();
118 else // link
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 );
129 else
131 xDest->deleteKey( name );
135 links.push_back( Link(
136 pSourceKeys[ nPos ], // abs path
137 xSource->getResolvedName( name ) // abs resolved name
138 ) );
144 void mergeKeys(
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!" );
159 t_links links;
160 links.reserve( 16 );
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: */