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