LibreOffice Module stoc (master) 1
mergekeys.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <utility>
22#include <vector>
23
24#include <osl/diagnose.h>
25
26#include <com/sun/star/registry/XRegistryKey.hpp>
27
28#include "mergekeys.hxx"
29
30using namespace ::osl;
31using namespace css::uno;
32using namespace ::com::sun::star;
33
34namespace stoc_impreg
35{
36
37namespace {
38
39struct Link
40{
41 OUString m_name;
42 OUString m_target;
43
44 Link( OUString name, OUString target )
45 : m_name(std::move( name ))
46 , m_target(std::move( target ))
47 {}
48};
49
50}
51
52typedef ::std::vector< Link > t_links;
53
54
55static 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 )
60{
61 if (!xSource.is() || !xSource->isValid()) {
62 throw registry::InvalidRegistryException(
63 "source key is null or invalid!" );
64 }
65 if (!xDest.is() || !xDest->isValid()) {
66 throw registry::InvalidRegistryException(
67 "destination key is null or invalid!" );
68 }
69
70 // write value
71 switch (xSource->getValueType())
72 {
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;
99 }
100
101 // sub keys
102 Sequence< OUString > sourceKeys( xSource->getKeyNames() );
103 OUString const * pSourceKeys = sourceKeys.getConstArray();
104 for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
105 {
106 // key name
107 OUString name( pSourceKeys[ nPos ] );
108 sal_Int32 nSlash = name.lastIndexOf( '/' );
109 if (nSlash >= 0)
110 {
111 name = name.copy( nSlash +1 );
112 }
113
114 if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
115 {
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();
122 }
123 else // link
124 {
125 // remove existing key
126 Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
127 if (xDestKey.is() && xDestKey->isValid()) // something to remove
128 {
129 xDestKey->closeKey();
130 if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
131 {
132 xDest->deleteLink( name );
133 }
134 else
135 {
136 xDest->deleteKey( name );
137 }
138 }
139
140 links.push_back( Link(
141 pSourceKeys[ nPos ], // abs path
142 xSource->getResolvedName( name ) // abs resolved name
143 ) );
144 }
145 }
146}
147
148
150 Reference< registry::XRegistryKey > const & xDest,
151 Reference< registry::XRegistryKey > const & xSource )
152 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
153{
154 if (!xDest.is() || !xDest->isValid()) {
155 throw registry::InvalidRegistryException(
156 "destination key is null or invalid!" );
157 }
158 if (xDest->isReadOnly())
159 {
160 throw registry::InvalidRegistryException(
161 "destination registry is read-only! cannot merge!" );
162 }
163
164 t_links links;
165 links.reserve( 16 );
166 mergeKeys( xDest, xSource, links );
167
168 for ( size_t nPos = links.size(); nPos--; )
169 {
170 Link const & r = links[ nPos ];
171 OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );
172 }
173}
174
175}
176
177/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * name
sal_uInt16 nPos
OUString m_target
Definition: mergekeys.cxx:42
OUString m_name
Definition: mergekeys.cxx:41
::std::vector< Link > t_links
Definition: mergekeys.cxx:52
static void mergeKeys(Reference< registry::XRegistryKey > const &xDest, Reference< registry::XRegistryKey > const &xSource, t_links &links)
Definition: mergekeys.cxx:55