1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
29 #define __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
30 
31 //_______________________________________________
32 // own includes
33 
34 #include <services.h>
35 #include <general.h>
36 
37 //_______________________________________________
38 // interface includes
39 #include <com/sun/star/uno/Sequence.hxx>
40 #include <com/sun/star/uno/Any.hxx>
41 
42 #ifndef _COM_SUN_STAR_BEANS_PROPERTYVALUE_HXX_
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #endif
45 #include <com/sun/star/uno/XInterface.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 
48 //_______________________________________________
49 // other includes
50 #include <rtl/ustrbuf.hxx>
51 
52 //_______________________________________________
53 // namespaces
54 
55 #ifndef css
56 namespace css = ::com::sun::star;
57 #endif
58 
59 namespace framework{
60     namespace pattern{
61         namespace configuration{
62 
63 //_______________________________________________
64 // definitions
65 
66 //-----------------------------------------------
67 class ConfigurationHelper
68 {
69     //-------------------------------------------
70     // const
71     public:
72 
73         //---------------------------------------
74         /** @short  allow opening of a configuration access
75                     in different working modes.
76 
77             @descr  All enum values must be useable as flags
78                     mapped into a int32 value!
79          */
80         enum EOpenMode
81         {
82             /// open it readonly (default=readwrite!)
83             E_READONLY = 1,
84             /// disable fallback handling for localized cfg nodes
85             E_ALL_LOCALES = 2
86         };
87 
88     //-------------------------------------------
89     // interface
90     public:
91 
92         //---------------------------------------
93         /**
94             @short  opens a configuration access.
95 
96             @descr  TODO
97 
98             @param  xSMGR
99                     this method need an uno service manager for internal work.
100 
101             @param  sPackage
102                     name the configuration file.
103                     e.g. "/.org.openoffice.Setup"
104                     Note: It must start with "/" but end without(!) "/"!
105 
106             @param  sRelPath
107                     describe the relativ path of the requested key inside
108                     the specified package.
109                     e.g. "Office/Factories"
110                     Note: Its not allowed to start or end with a "/"!
111                     Further you must use encoded path elements if
112                     e.g. set nodes are involved.
113 
114             @param  nOpenFlags
115                     force opening of the configuration access in special mode.
116                     see enum EOpenMode for further informations.
117          */
118         static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR     ,
119                                                                       const ::rtl::OUString&                                        sPackage  ,
120                                                                       const ::rtl::OUString&                                        sRelPath  ,
121                                                                             sal_Int32                                               nOpenFlags)
122         {
123             static ::rtl::OUString PATH_SEPERATOR = ::rtl::OUString::createFromAscii("/");
124 
125             css::uno::Reference< css::uno::XInterface > xCFG;
126 
127             try
128             {
129                 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
130                     xSMGR->createInstance(SERVICENAME_CFGPROVIDER), css::uno::UNO_QUERY_THROW);
131 
132                 ::rtl::OUStringBuffer sPath(1024);
133                 sPath.append(sPackage      );
134                 sPath.append(PATH_SEPERATOR);
135                 sPath.append(sRelPath      );
136 
137                 sal_Bool bReadOnly   = ((nOpenFlags & ConfigurationHelper::E_READONLY   ) == ConfigurationHelper::E_READONLY   );
138                 sal_Bool bAllLocales = ((nOpenFlags & ConfigurationHelper::E_ALL_LOCALES) == ConfigurationHelper::E_ALL_LOCALES);
139 
140                 sal_Int32 c = 1;
141                 if (bAllLocales)
142                     c = 2;
143 
144                 css::uno::Sequence< css::uno::Any > lParams(c);
145                 css::beans::PropertyValue           aParam;
146 
147                 aParam.Name    = ::rtl::OUString::createFromAscii("nodepath");
148                 aParam.Value <<= sPath.makeStringAndClear();
149                 lParams[0]   <<= aParam;
150 
151                 if (bAllLocales)
152                 {
153                     aParam.Name    = ::rtl::OUString::createFromAscii("*");
154                     aParam.Value <<= sal_True;
155                     lParams[1]   <<= aParam;
156                 }
157 
158                 if (bReadOnly)
159                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS, lParams);
160                 else
161                     xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
162             }
163             catch(const css::uno::RuntimeException& exRun)
164                 { throw exRun; }
165             catch(const css::uno::Exception&)
166                 { xCFG.clear(); }
167 
168             return xCFG;
169         }
170 };
171 
172         } // namespace configuration
173     } // namespace pattern
174 } // namespace framework
175 
176 #endif // __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
177