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