1*f8e07b45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f8e07b45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f8e07b45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f8e07b45SAndrew Rist  * distributed with this work for additional information
6*f8e07b45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f8e07b45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f8e07b45SAndrew Rist  * "License"); you may not use this file except in compliance
9*f8e07b45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f8e07b45SAndrew Rist  *
11*f8e07b45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f8e07b45SAndrew Rist  *
13*f8e07b45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f8e07b45SAndrew Rist  * software distributed under the License is distributed on an
15*f8e07b45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f8e07b45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f8e07b45SAndrew Rist  * specific language governing permissions and limitations
18*f8e07b45SAndrew Rist  * under the License.
19*f8e07b45SAndrew Rist  *
20*f8e07b45SAndrew Rist  *************************************************************/
21*f8e07b45SAndrew Rist 
22*f8e07b45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
25cdf0e10cSrcweir #define __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_______________________________________________
28cdf0e10cSrcweir // own includes
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <services.h>
31cdf0e10cSrcweir #include <general.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //_______________________________________________
34cdf0e10cSrcweir // interface includes
35cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
36cdf0e10cSrcweir #include <com/sun/star/uno/Any.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HXX_
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //_______________________________________________
45cdf0e10cSrcweir // other includes
46cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //_______________________________________________
49cdf0e10cSrcweir // namespaces
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #ifndef css
52cdf0e10cSrcweir namespace css = ::com::sun::star;
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace framework{
56cdf0e10cSrcweir     namespace pattern{
57cdf0e10cSrcweir         namespace configuration{
58cdf0e10cSrcweir 
59cdf0e10cSrcweir //_______________________________________________
60cdf0e10cSrcweir // definitions
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //-----------------------------------------------
63cdf0e10cSrcweir class ConfigurationHelper
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     //-------------------------------------------
66cdf0e10cSrcweir     // const
67cdf0e10cSrcweir     public:
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         //---------------------------------------
70cdf0e10cSrcweir         /** @short  allow opening of a configuration access
71cdf0e10cSrcweir                     in different working modes.
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             @descr  All enum values must be useable as flags
74cdf0e10cSrcweir                     mapped into a int32 value!
75cdf0e10cSrcweir          */
76cdf0e10cSrcweir         enum EOpenMode
77cdf0e10cSrcweir         {
78cdf0e10cSrcweir             /// open it readonly (default=readwrite!)
79cdf0e10cSrcweir             E_READONLY = 1,
80cdf0e10cSrcweir             /// disable fallback handling for localized cfg nodes
81cdf0e10cSrcweir             E_ALL_LOCALES = 2
82cdf0e10cSrcweir         };
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     //-------------------------------------------
85cdf0e10cSrcweir     // interface
86cdf0e10cSrcweir     public:
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         //---------------------------------------
89cdf0e10cSrcweir         /**
90cdf0e10cSrcweir             @short  opens a configuration access.
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             @descr  TODO
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             @param  xSMGR
95cdf0e10cSrcweir                     this method need an uno service manager for internal work.
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             @param  sPackage
98cdf0e10cSrcweir                     name the configuration file.
99cdf0e10cSrcweir                     e.g. "/.org.openoffice.Setup"
100cdf0e10cSrcweir                     Note: It must start with "/" but end without(!) "/"!
101cdf0e10cSrcweir 
102cdf0e10cSrcweir             @param  sRelPath
103cdf0e10cSrcweir                     describe the relativ path of the requested key inside
104cdf0e10cSrcweir                     the specified package.
105cdf0e10cSrcweir                     e.g. "Office/Factories"
106cdf0e10cSrcweir                     Note: Its not allowed to start or end with a "/"!
107cdf0e10cSrcweir                     Further you must use encoded path elements if
108cdf0e10cSrcweir                     e.g. set nodes are involved.
109cdf0e10cSrcweir 
110cdf0e10cSrcweir             @param  nOpenFlags
111cdf0e10cSrcweir                     force opening of the configuration access in special mode.
112cdf0e10cSrcweir                     see enum EOpenMode for further informations.
113cdf0e10cSrcweir          */
openConfig(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR,const::rtl::OUString & sPackage,const::rtl::OUString & sRelPath,sal_Int32 nOpenFlags)114cdf0e10cSrcweir         static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR     ,
115cdf0e10cSrcweir                                                                       const ::rtl::OUString&                                        sPackage  ,
116cdf0e10cSrcweir                                                                       const ::rtl::OUString&                                        sRelPath  ,
117cdf0e10cSrcweir                                                                             sal_Int32                                               nOpenFlags)
118cdf0e10cSrcweir         {
119cdf0e10cSrcweir             static ::rtl::OUString PATH_SEPERATOR = ::rtl::OUString::createFromAscii("/");
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface > xCFG;
122cdf0e10cSrcweir 
123cdf0e10cSrcweir             try
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
126cdf0e10cSrcweir                     xSMGR->createInstance(SERVICENAME_CFGPROVIDER), css::uno::UNO_QUERY_THROW);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir                 ::rtl::OUStringBuffer sPath(1024);
129cdf0e10cSrcweir                 sPath.append(sPackage      );
130cdf0e10cSrcweir                 sPath.append(PATH_SEPERATOR);
131cdf0e10cSrcweir                 sPath.append(sRelPath      );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir                 sal_Bool bReadOnly   = ((nOpenFlags & ConfigurationHelper::E_READONLY   ) == ConfigurationHelper::E_READONLY   );
134cdf0e10cSrcweir                 sal_Bool bAllLocales = ((nOpenFlags & ConfigurationHelper::E_ALL_LOCALES) == ConfigurationHelper::E_ALL_LOCALES);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir                 sal_Int32 c = 1;
137cdf0e10cSrcweir                 if (bAllLocales)
138cdf0e10cSrcweir                     c = 2;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir                 css::uno::Sequence< css::uno::Any > lParams(c);
141cdf0e10cSrcweir                 css::beans::PropertyValue           aParam;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                 aParam.Name    = ::rtl::OUString::createFromAscii("nodepath");
144cdf0e10cSrcweir                 aParam.Value <<= sPath.makeStringAndClear();
145cdf0e10cSrcweir                 lParams[0]   <<= aParam;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                 if (bAllLocales)
148cdf0e10cSrcweir                 {
149cdf0e10cSrcweir                     aParam.Name    = ::rtl::OUString::createFromAscii("*");
150cdf0e10cSrcweir                     aParam.Value <<= sal_True;
151cdf0e10cSrcweir                     lParams[1]   <<= aParam;
152cdf0e10cSrcweir                 }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir                 if (bReadOnly)
155cdf0e10cSrcweir                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS, lParams);
156cdf0e10cSrcweir                 else
157cdf0e10cSrcweir                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir             catch(const css::uno::RuntimeException& exRun)
160cdf0e10cSrcweir                 { throw exRun; }
161cdf0e10cSrcweir             catch(const css::uno::Exception&)
162cdf0e10cSrcweir                 { xCFG.clear(); }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             return xCFG;
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir };
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         } // namespace configuration
169cdf0e10cSrcweir     } // namespace pattern
170cdf0e10cSrcweir } // namespace framework
171cdf0e10cSrcweir 
172cdf0e10cSrcweir #endif // __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
173