1*647a425cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647a425cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647a425cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647a425cSAndrew Rist  * distributed with this work for additional information
6*647a425cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647a425cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647a425cSAndrew Rist  * "License"); you may not use this file except in compliance
9*647a425cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647a425cSAndrew Rist  *
11*647a425cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647a425cSAndrew Rist  *
13*647a425cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647a425cSAndrew Rist  * software distributed under the License is distributed on an
15*647a425cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647a425cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647a425cSAndrew Rist  * specific language governing permissions and limitations
18*647a425cSAndrew Rist  * under the License.
19*647a425cSAndrew Rist  *
20*647a425cSAndrew Rist  *************************************************************/
21*647a425cSAndrew Rist 
22*647a425cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_stoc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vector>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
30cdf0e10cSrcweir #include <com/sun/star/registry/MergeConflictException.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "mergekeys.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::rtl;
37cdf0e10cSrcweir using namespace ::osl;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace stoc_impreg
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 
44cdf0e10cSrcweir struct Link
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     OUString m_name;
47cdf0e10cSrcweir     OUString m_target;
48cdf0e10cSrcweir 
Linkstoc_impreg::Link49cdf0e10cSrcweir     inline Link( OUString const & name, OUString const & target )
50cdf0e10cSrcweir         : m_name( name )
51cdf0e10cSrcweir         , m_target( target )
52cdf0e10cSrcweir         {}
53cdf0e10cSrcweir };
54cdf0e10cSrcweir typedef ::std::vector< Link > t_links;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir //==================================================================================================
mergeKeys(Reference<registry::XRegistryKey> const & xDest,Reference<registry::XRegistryKey> const & xSource,t_links & links)57cdf0e10cSrcweir static void mergeKeys(
58cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xDest,
59cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xSource,
60cdf0e10cSrcweir     t_links & links )
61cdf0e10cSrcweir     // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     if (!xSource.is() || !xSource->isValid()) {
64cdf0e10cSrcweir         throw registry::InvalidRegistryException(
65cdf0e10cSrcweir             OUSTR("source key is null or invalid!"),
66cdf0e10cSrcweir             Reference<XInterface>() );
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir     if (!xDest.is() || !xDest->isValid()) {
69cdf0e10cSrcweir         throw registry::InvalidRegistryException(
70cdf0e10cSrcweir             OUSTR("destination key is null or invalid!"),
71cdf0e10cSrcweir             Reference<XInterface>() );
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     // write value
75cdf0e10cSrcweir     switch (xSource->getValueType())
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir     case registry::RegistryValueType_NOT_DEFINED:
78cdf0e10cSrcweir         break;
79cdf0e10cSrcweir     case registry::RegistryValueType_LONG:
80cdf0e10cSrcweir         xDest->setLongValue( xSource->getLongValue() );
81cdf0e10cSrcweir         break;
82cdf0e10cSrcweir     case registry::RegistryValueType_ASCII:
83cdf0e10cSrcweir         xDest->setAsciiValue( xSource->getAsciiValue() );
84cdf0e10cSrcweir         break;
85cdf0e10cSrcweir     case registry::RegistryValueType_STRING:
86cdf0e10cSrcweir         xDest->setStringValue( xSource->getStringValue() );
87cdf0e10cSrcweir         break;
88cdf0e10cSrcweir     case registry::RegistryValueType_BINARY:
89cdf0e10cSrcweir         xDest->setBinaryValue( xSource->getBinaryValue() );
90cdf0e10cSrcweir         break;
91cdf0e10cSrcweir     case registry::RegistryValueType_LONGLIST:
92cdf0e10cSrcweir         xDest->setLongListValue( xSource->getLongListValue() );
93cdf0e10cSrcweir         break;
94cdf0e10cSrcweir     case registry::RegistryValueType_ASCIILIST:
95cdf0e10cSrcweir         xDest->setAsciiListValue( xSource->getAsciiListValue() );
96cdf0e10cSrcweir         break;
97cdf0e10cSrcweir     case registry::RegistryValueType_STRINGLIST:
98cdf0e10cSrcweir         xDest->setStringListValue( xSource->getStringListValue() );
99cdf0e10cSrcweir         break;
100cdf0e10cSrcweir     default:
101cdf0e10cSrcweir         OSL_ASSERT(false);
102cdf0e10cSrcweir         break;
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     // sub keys
106cdf0e10cSrcweir     Sequence< OUString > sourceKeys( xSource->getKeyNames() );
107cdf0e10cSrcweir     OUString const * pSourceKeys = sourceKeys.getConstArray();
108cdf0e10cSrcweir     for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         // key name
111cdf0e10cSrcweir         OUString name( pSourceKeys[ nPos ] );
112cdf0e10cSrcweir         sal_Int32 nSlash = name.lastIndexOf( '/' );
113cdf0e10cSrcweir         if (nSlash >= 0)
114cdf0e10cSrcweir         {
115cdf0e10cSrcweir             name = name.copy( nSlash +1 );
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             // try to open exisiting dest key or create new one
121cdf0e10cSrcweir             Reference< registry::XRegistryKey > xDestKey( xDest->createKey( name ) );
122cdf0e10cSrcweir             Reference< registry::XRegistryKey > xSourceKey( xSource->openKey( name ) );
123cdf0e10cSrcweir             mergeKeys( xDestKey, xSourceKey, links );
124cdf0e10cSrcweir             xSourceKey->closeKey();
125cdf0e10cSrcweir             xDestKey->closeKey();
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         else // link
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             // remove existing key
130cdf0e10cSrcweir             Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
131cdf0e10cSrcweir             if (xDestKey.is() && xDestKey->isValid()) // something to remove
132cdf0e10cSrcweir             {
133cdf0e10cSrcweir                 xDestKey->closeKey();
134cdf0e10cSrcweir                 if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
135cdf0e10cSrcweir                 {
136cdf0e10cSrcweir                     xDest->deleteLink( name );
137cdf0e10cSrcweir                 }
138cdf0e10cSrcweir                 else
139cdf0e10cSrcweir                 {
140cdf0e10cSrcweir                     xDest->deleteKey( name );
141cdf0e10cSrcweir                 }
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             links.push_back( Link(
145cdf0e10cSrcweir                 pSourceKeys[ nPos ], // abs path
146cdf0e10cSrcweir                 xSource->getResolvedName( name ) // abs resolved name
147cdf0e10cSrcweir                 ) );
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir //==================================================================================================
mergeKeys(Reference<registry::XRegistryKey> const & xDest,Reference<registry::XRegistryKey> const & xSource)153cdf0e10cSrcweir void mergeKeys(
154cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xDest,
155cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xSource )
156cdf0e10cSrcweir     // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir     if (!xDest.is() || !xDest->isValid()) {
159cdf0e10cSrcweir         throw registry::InvalidRegistryException(
160cdf0e10cSrcweir             OUSTR("destination key is null or invalid!"),
161cdf0e10cSrcweir             Reference<XInterface>() );
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir     if (xDest->isReadOnly())
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         throw registry::InvalidRegistryException(
166cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
167cdf0e10cSrcweir                           "destination registry is read-only!  cannot merge!") ),
168cdf0e10cSrcweir             Reference< XInterface >() );
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     t_links links;
172cdf0e10cSrcweir     links.reserve( 16 );
173cdf0e10cSrcweir     mergeKeys( xDest, xSource, links );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     for ( size_t nPos = links.size(); nPos--; )
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         Link const & r = links[ nPos ];
178cdf0e10cSrcweir         OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir }
183