xref: /AOO41X/main/framework/source/inc/pattern/configuration.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 #ifndef __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
29*cdf0e10cSrcweir #define __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_______________________________________________
32*cdf0e10cSrcweir // own includes
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <services.h>
35*cdf0e10cSrcweir #include <general.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //_______________________________________________
38*cdf0e10cSrcweir // interface includes
39*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
40*cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HXX_
43*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir //_______________________________________________
49*cdf0e10cSrcweir // other includes
50*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //_______________________________________________
53*cdf0e10cSrcweir // namespaces
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #ifndef css
56*cdf0e10cSrcweir namespace css = ::com::sun::star;
57*cdf0e10cSrcweir #endif
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir namespace framework{
60*cdf0e10cSrcweir     namespace pattern{
61*cdf0e10cSrcweir         namespace configuration{
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir //_______________________________________________
64*cdf0e10cSrcweir // definitions
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir //-----------------------------------------------
67*cdf0e10cSrcweir class ConfigurationHelper
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir     //-------------------------------------------
70*cdf0e10cSrcweir     // const
71*cdf0e10cSrcweir     public:
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         //---------------------------------------
74*cdf0e10cSrcweir         /** @short  allow opening of a configuration access
75*cdf0e10cSrcweir                     in different working modes.
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir             @descr  All enum values must be useable as flags
78*cdf0e10cSrcweir                     mapped into a int32 value!
79*cdf0e10cSrcweir          */
80*cdf0e10cSrcweir         enum EOpenMode
81*cdf0e10cSrcweir         {
82*cdf0e10cSrcweir             /// open it readonly (default=readwrite!)
83*cdf0e10cSrcweir             E_READONLY = 1,
84*cdf0e10cSrcweir             /// disable fallback handling for localized cfg nodes
85*cdf0e10cSrcweir             E_ALL_LOCALES = 2
86*cdf0e10cSrcweir         };
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     //-------------------------------------------
89*cdf0e10cSrcweir     // interface
90*cdf0e10cSrcweir     public:
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         //---------------------------------------
93*cdf0e10cSrcweir         /**
94*cdf0e10cSrcweir             @short  opens a configuration access.
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir             @descr  TODO
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir             @param  xSMGR
99*cdf0e10cSrcweir                     this method need an uno service manager for internal work.
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             @param  sPackage
102*cdf0e10cSrcweir                     name the configuration file.
103*cdf0e10cSrcweir                     e.g. "/.org.openoffice.Setup"
104*cdf0e10cSrcweir                     Note: It must start with "/" but end without(!) "/"!
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             @param  sRelPath
107*cdf0e10cSrcweir                     describe the relativ path of the requested key inside
108*cdf0e10cSrcweir                     the specified package.
109*cdf0e10cSrcweir                     e.g. "Office/Factories"
110*cdf0e10cSrcweir                     Note: Its not allowed to start or end with a "/"!
111*cdf0e10cSrcweir                     Further you must use encoded path elements if
112*cdf0e10cSrcweir                     e.g. set nodes are involved.
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir             @param  nOpenFlags
115*cdf0e10cSrcweir                     force opening of the configuration access in special mode.
116*cdf0e10cSrcweir                     see enum EOpenMode for further informations.
117*cdf0e10cSrcweir          */
118*cdf0e10cSrcweir         static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR     ,
119*cdf0e10cSrcweir                                                                       const ::rtl::OUString&                                        sPackage  ,
120*cdf0e10cSrcweir                                                                       const ::rtl::OUString&                                        sRelPath  ,
121*cdf0e10cSrcweir                                                                             sal_Int32                                               nOpenFlags)
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir             static ::rtl::OUString PATH_SEPERATOR = ::rtl::OUString::createFromAscii("/");
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface > xCFG;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir             try
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
130*cdf0e10cSrcweir                     xSMGR->createInstance(SERVICENAME_CFGPROVIDER), css::uno::UNO_QUERY_THROW);
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir                 ::rtl::OUStringBuffer sPath(1024);
133*cdf0e10cSrcweir                 sPath.append(sPackage      );
134*cdf0e10cSrcweir                 sPath.append(PATH_SEPERATOR);
135*cdf0e10cSrcweir                 sPath.append(sRelPath      );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                 sal_Bool bReadOnly   = ((nOpenFlags & ConfigurationHelper::E_READONLY   ) == ConfigurationHelper::E_READONLY   );
138*cdf0e10cSrcweir                 sal_Bool bAllLocales = ((nOpenFlags & ConfigurationHelper::E_ALL_LOCALES) == ConfigurationHelper::E_ALL_LOCALES);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir                 sal_Int32 c = 1;
141*cdf0e10cSrcweir                 if (bAllLocales)
142*cdf0e10cSrcweir                     c = 2;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir                 css::uno::Sequence< css::uno::Any > lParams(c);
145*cdf0e10cSrcweir                 css::beans::PropertyValue           aParam;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir                 aParam.Name    = ::rtl::OUString::createFromAscii("nodepath");
148*cdf0e10cSrcweir                 aParam.Value <<= sPath.makeStringAndClear();
149*cdf0e10cSrcweir                 lParams[0]   <<= aParam;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir                 if (bAllLocales)
152*cdf0e10cSrcweir                 {
153*cdf0e10cSrcweir                     aParam.Name    = ::rtl::OUString::createFromAscii("*");
154*cdf0e10cSrcweir                     aParam.Value <<= sal_True;
155*cdf0e10cSrcweir                     lParams[1]   <<= aParam;
156*cdf0e10cSrcweir                 }
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir                 if (bReadOnly)
159*cdf0e10cSrcweir                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS, lParams);
160*cdf0e10cSrcweir                 else
161*cdf0e10cSrcweir                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
162*cdf0e10cSrcweir             }
163*cdf0e10cSrcweir             catch(const css::uno::RuntimeException& exRun)
164*cdf0e10cSrcweir                 { throw exRun; }
165*cdf0e10cSrcweir             catch(const css::uno::Exception&)
166*cdf0e10cSrcweir                 { xCFG.clear(); }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             return xCFG;
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir };
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         } // namespace configuration
173*cdf0e10cSrcweir     } // namespace pattern
174*cdf0e10cSrcweir } // namespace framework
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir #endif // __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
177