1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_stoc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <vector>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/registry/MergeConflictException.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "mergekeys.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::rtl;
41*cdf0e10cSrcweir using namespace ::osl;
42*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43*cdf0e10cSrcweir using namespace ::com::sun::star;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace stoc_impreg
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir struct Link
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir     OUString m_name;
51*cdf0e10cSrcweir     OUString m_target;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     inline Link( OUString const & name, OUString const & target )
54*cdf0e10cSrcweir         : m_name( name )
55*cdf0e10cSrcweir         , m_target( target )
56*cdf0e10cSrcweir         {}
57*cdf0e10cSrcweir };
58*cdf0e10cSrcweir typedef ::std::vector< Link > t_links;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir //==================================================================================================
61*cdf0e10cSrcweir static void mergeKeys(
62*cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xDest,
63*cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xSource,
64*cdf0e10cSrcweir     t_links & links )
65*cdf0e10cSrcweir     // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     if (!xSource.is() || !xSource->isValid()) {
68*cdf0e10cSrcweir         throw registry::InvalidRegistryException(
69*cdf0e10cSrcweir             OUSTR("source key is null or invalid!"),
70*cdf0e10cSrcweir             Reference<XInterface>() );
71*cdf0e10cSrcweir     }
72*cdf0e10cSrcweir     if (!xDest.is() || !xDest->isValid()) {
73*cdf0e10cSrcweir         throw registry::InvalidRegistryException(
74*cdf0e10cSrcweir             OUSTR("destination key is null or invalid!"),
75*cdf0e10cSrcweir             Reference<XInterface>() );
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     // write value
79*cdf0e10cSrcweir     switch (xSource->getValueType())
80*cdf0e10cSrcweir     {
81*cdf0e10cSrcweir     case registry::RegistryValueType_NOT_DEFINED:
82*cdf0e10cSrcweir         break;
83*cdf0e10cSrcweir     case registry::RegistryValueType_LONG:
84*cdf0e10cSrcweir         xDest->setLongValue( xSource->getLongValue() );
85*cdf0e10cSrcweir         break;
86*cdf0e10cSrcweir     case registry::RegistryValueType_ASCII:
87*cdf0e10cSrcweir         xDest->setAsciiValue( xSource->getAsciiValue() );
88*cdf0e10cSrcweir         break;
89*cdf0e10cSrcweir     case registry::RegistryValueType_STRING:
90*cdf0e10cSrcweir         xDest->setStringValue( xSource->getStringValue() );
91*cdf0e10cSrcweir         break;
92*cdf0e10cSrcweir     case registry::RegistryValueType_BINARY:
93*cdf0e10cSrcweir         xDest->setBinaryValue( xSource->getBinaryValue() );
94*cdf0e10cSrcweir         break;
95*cdf0e10cSrcweir     case registry::RegistryValueType_LONGLIST:
96*cdf0e10cSrcweir         xDest->setLongListValue( xSource->getLongListValue() );
97*cdf0e10cSrcweir         break;
98*cdf0e10cSrcweir     case registry::RegistryValueType_ASCIILIST:
99*cdf0e10cSrcweir         xDest->setAsciiListValue( xSource->getAsciiListValue() );
100*cdf0e10cSrcweir         break;
101*cdf0e10cSrcweir     case registry::RegistryValueType_STRINGLIST:
102*cdf0e10cSrcweir         xDest->setStringListValue( xSource->getStringListValue() );
103*cdf0e10cSrcweir         break;
104*cdf0e10cSrcweir     default:
105*cdf0e10cSrcweir         OSL_ASSERT(false);
106*cdf0e10cSrcweir         break;
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     // sub keys
110*cdf0e10cSrcweir     Sequence< OUString > sourceKeys( xSource->getKeyNames() );
111*cdf0e10cSrcweir     OUString const * pSourceKeys = sourceKeys.getConstArray();
112*cdf0e10cSrcweir     for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
113*cdf0e10cSrcweir     {
114*cdf0e10cSrcweir         // key name
115*cdf0e10cSrcweir         OUString name( pSourceKeys[ nPos ] );
116*cdf0e10cSrcweir         sal_Int32 nSlash = name.lastIndexOf( '/' );
117*cdf0e10cSrcweir         if (nSlash >= 0)
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             name = name.copy( nSlash +1 );
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             // try to open exisiting dest key or create new one
125*cdf0e10cSrcweir             Reference< registry::XRegistryKey > xDestKey( xDest->createKey( name ) );
126*cdf0e10cSrcweir             Reference< registry::XRegistryKey > xSourceKey( xSource->openKey( name ) );
127*cdf0e10cSrcweir             mergeKeys( xDestKey, xSourceKey, links );
128*cdf0e10cSrcweir             xSourceKey->closeKey();
129*cdf0e10cSrcweir             xDestKey->closeKey();
130*cdf0e10cSrcweir         }
131*cdf0e10cSrcweir         else // link
132*cdf0e10cSrcweir         {
133*cdf0e10cSrcweir             // remove existing key
134*cdf0e10cSrcweir             Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
135*cdf0e10cSrcweir             if (xDestKey.is() && xDestKey->isValid()) // something to remove
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 xDestKey->closeKey();
138*cdf0e10cSrcweir                 if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
139*cdf0e10cSrcweir                 {
140*cdf0e10cSrcweir                     xDest->deleteLink( name );
141*cdf0e10cSrcweir                 }
142*cdf0e10cSrcweir                 else
143*cdf0e10cSrcweir                 {
144*cdf0e10cSrcweir                     xDest->deleteKey( name );
145*cdf0e10cSrcweir                 }
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             links.push_back( Link(
149*cdf0e10cSrcweir                 pSourceKeys[ nPos ], // abs path
150*cdf0e10cSrcweir                 xSource->getResolvedName( name ) // abs resolved name
151*cdf0e10cSrcweir                 ) );
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //==================================================================================================
157*cdf0e10cSrcweir void mergeKeys(
158*cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xDest,
159*cdf0e10cSrcweir     Reference< registry::XRegistryKey > const & xSource )
160*cdf0e10cSrcweir     // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir     if (!xDest.is() || !xDest->isValid()) {
163*cdf0e10cSrcweir         throw registry::InvalidRegistryException(
164*cdf0e10cSrcweir             OUSTR("destination key is null or invalid!"),
165*cdf0e10cSrcweir             Reference<XInterface>() );
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir     if (xDest->isReadOnly())
168*cdf0e10cSrcweir     {
169*cdf0e10cSrcweir         throw registry::InvalidRegistryException(
170*cdf0e10cSrcweir             OUString( RTL_CONSTASCII_USTRINGPARAM(
171*cdf0e10cSrcweir                           "destination registry is read-only!  cannot merge!") ),
172*cdf0e10cSrcweir             Reference< XInterface >() );
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     t_links links;
176*cdf0e10cSrcweir     links.reserve( 16 );
177*cdf0e10cSrcweir     mergeKeys( xDest, xSource, links );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     for ( size_t nPos = links.size(); nPos--; )
180*cdf0e10cSrcweir     {
181*cdf0e10cSrcweir         Link const & r = links[ nPos ];
182*cdf0e10cSrcweir         OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir }
187