Line data Source code
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 <vector>
22 :
23 : #include <com/sun/star/registry/XRegistryKey.hpp>
24 : #include <com/sun/star/registry/MergeConflictException.hpp>
25 :
26 : #include "mergekeys.hxx"
27 :
28 : using namespace ::osl;
29 : using namespace css::uno;
30 : using namespace ::com::sun::star;
31 :
32 : namespace stoc_impreg
33 : {
34 :
35 0 : struct Link
36 : {
37 : OUString m_name;
38 : OUString m_target;
39 :
40 0 : inline Link( OUString const & name, OUString const & target )
41 : : m_name( name )
42 0 : , m_target( target )
43 0 : {}
44 : };
45 : typedef ::std::vector< Link > t_links;
46 :
47 :
48 16 : static void mergeKeys(
49 : Reference< registry::XRegistryKey > const & xDest,
50 : Reference< registry::XRegistryKey > const & xSource,
51 : t_links & links )
52 : // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
53 : {
54 16 : if (!xSource.is() || !xSource->isValid()) {
55 : throw registry::InvalidRegistryException(
56 0 : "source key is null or invalid!" );
57 : }
58 16 : if (!xDest.is() || !xDest->isValid()) {
59 : throw registry::InvalidRegistryException(
60 0 : "destination key is null or invalid!" );
61 : }
62 :
63 : // write value
64 16 : switch (xSource->getValueType())
65 : {
66 : case registry::RegistryValueType_NOT_DEFINED:
67 12 : break;
68 : case registry::RegistryValueType_LONG:
69 0 : xDest->setLongValue( xSource->getLongValue() );
70 0 : break;
71 : case registry::RegistryValueType_ASCII:
72 4 : xDest->setAsciiValue( xSource->getAsciiValue() );
73 4 : break;
74 : case registry::RegistryValueType_STRING:
75 0 : xDest->setStringValue( xSource->getStringValue() );
76 0 : break;
77 : case registry::RegistryValueType_BINARY:
78 0 : xDest->setBinaryValue( xSource->getBinaryValue() );
79 0 : break;
80 : case registry::RegistryValueType_LONGLIST:
81 0 : xDest->setLongListValue( xSource->getLongListValue() );
82 0 : break;
83 : case registry::RegistryValueType_ASCIILIST:
84 0 : xDest->setAsciiListValue( xSource->getAsciiListValue() );
85 0 : break;
86 : case registry::RegistryValueType_STRINGLIST:
87 0 : xDest->setStringListValue( xSource->getStringListValue() );
88 0 : break;
89 : default:
90 : OSL_ASSERT(false);
91 0 : break;
92 : }
93 :
94 : // sub keys
95 16 : Sequence< OUString > sourceKeys( xSource->getKeyNames() );
96 16 : OUString const * pSourceKeys = sourceKeys.getConstArray();
97 46 : for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
98 : {
99 : // key name
100 14 : OUString name( pSourceKeys[ nPos ] );
101 14 : sal_Int32 nSlash = name.lastIndexOf( '/' );
102 14 : if (nSlash >= 0)
103 : {
104 14 : name = name.copy( nSlash +1 );
105 : }
106 :
107 14 : if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
108 : {
109 : // try to open exisiting dest key or create new one
110 14 : Reference< registry::XRegistryKey > xDestKey( xDest->createKey( name ) );
111 28 : Reference< registry::XRegistryKey > xSourceKey( xSource->openKey( name ) );
112 14 : mergeKeys( xDestKey, xSourceKey, links );
113 14 : xSourceKey->closeKey();
114 28 : xDestKey->closeKey();
115 : }
116 : else // link
117 : {
118 : // remove existing key
119 0 : Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
120 0 : if (xDestKey.is() && xDestKey->isValid()) // something to remove
121 : {
122 0 : xDestKey->closeKey();
123 0 : if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
124 : {
125 0 : xDest->deleteLink( name );
126 : }
127 : else
128 : {
129 0 : xDest->deleteKey( name );
130 : }
131 : }
132 :
133 : links.push_back( Link(
134 0 : pSourceKeys[ nPos ], // abs path
135 0 : xSource->getResolvedName( name ) // abs resolved name
136 0 : ) );
137 : }
138 30 : }
139 16 : }
140 :
141 :
142 2 : void mergeKeys(
143 : Reference< registry::XRegistryKey > const & xDest,
144 : Reference< registry::XRegistryKey > const & xSource )
145 : // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
146 : {
147 2 : if (!xDest.is() || !xDest->isValid()) {
148 : throw registry::InvalidRegistryException(
149 0 : "destination key is null or invalid!" );
150 : }
151 2 : if (xDest->isReadOnly())
152 : {
153 : throw registry::InvalidRegistryException(
154 0 : "destination registry is read-only! cannot merge!" );
155 : }
156 :
157 2 : t_links links;
158 2 : links.reserve( 16 );
159 2 : mergeKeys( xDest, xSource, links );
160 :
161 4 : for ( size_t nPos = links.size(); nPos--; )
162 : {
163 0 : Link const & r = links[ nPos ];
164 0 : OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );
165 2 : }
166 2 : }
167 :
168 : }
169 :
170 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|