Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
21 : #define __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
22 :
23 : #include <services.h>
24 : #include <general.h>
25 :
26 : #include <com/sun/star/uno/Sequence.hxx>
27 : #include <com/sun/star/uno/Any.hxx>
28 :
29 : #include <com/sun/star/beans/PropertyValue.hpp>
30 : #include <com/sun/star/configuration/theDefaultProvider.hpp>
31 : #include <com/sun/star/uno/XInterface.hpp>
32 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 :
34 : #include <rtl/ustrbuf.hxx>
35 :
36 : namespace framework{
37 : namespace pattern{
38 : namespace configuration{
39 :
40 : //-----------------------------------------------
41 : class ConfigurationHelper
42 : {
43 :
44 : public:
45 :
46 : //---------------------------------------
47 : /** @short allow opening of a configuration access
48 : in different working modes.
49 :
50 : @descr All enum values must be useable as flags
51 : mapped into a int32 value!
52 : */
53 : enum EOpenMode
54 : {
55 : /// open it readonly (default=readwrite!)
56 : E_READONLY = 1,
57 : /// disable fallback handling for localized cfg nodes
58 : E_ALL_LOCALES = 2
59 : };
60 :
61 : //-------------------------------------------
62 : // interface
63 : public:
64 :
65 : //---------------------------------------
66 : /**
67 : @short opens a configuration access.
68 :
69 : @descr TODO
70 :
71 : @param rxContext
72 : this method need an uno service manager for internal work.
73 :
74 : @param sPackage
75 : name the configuration file.
76 : e.g. "/.org.openoffice.Setup"
77 : Note: It must start with "/" but end without(!) "/"!
78 :
79 : @param sRelPath
80 : describe the relativ path of the requested key inside
81 : the specified package.
82 : e.g. "Office/Factories"
83 : Note: Its not allowed to start or end with a "/"!
84 : Further you must use encoded path elements if
85 : e.g. set nodes are involved.
86 :
87 : @param nOpenFlags
88 : force opening of the configuration access in special mode.
89 : see enum EOpenMode for further informations.
90 : */
91 0 : static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
92 : const ::rtl::OUString& sPackage ,
93 : const ::rtl::OUString& sRelPath ,
94 : sal_Int32 nOpenFlags)
95 : {
96 0 : css::uno::Reference< css::uno::XInterface > xCFG;
97 :
98 : try
99 : {
100 : css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider =
101 0 : css::configuration::theDefaultProvider::get( rxContext );
102 :
103 0 : ::rtl::OUStringBuffer sPath(1024);
104 0 : sPath.append(sPackage );
105 0 : sPath.append(static_cast<sal_Unicode>('/'));
106 0 : sPath.append(sRelPath );
107 :
108 0 : sal_Bool bReadOnly = ((nOpenFlags & ConfigurationHelper::E_READONLY ) == ConfigurationHelper::E_READONLY );
109 0 : sal_Bool bAllLocales = ((nOpenFlags & ConfigurationHelper::E_ALL_LOCALES) == ConfigurationHelper::E_ALL_LOCALES);
110 :
111 0 : sal_Int32 c = 1;
112 0 : if (bAllLocales)
113 0 : c = 2;
114 :
115 0 : css::uno::Sequence< css::uno::Any > lParams(c);
116 0 : css::beans::PropertyValue aParam;
117 :
118 0 : aParam.Name = ::rtl::OUString("nodepath");
119 0 : aParam.Value <<= sPath.makeStringAndClear();
120 0 : lParams[0] <<= aParam;
121 :
122 0 : if (bAllLocales)
123 : {
124 0 : aParam.Name = ::rtl::OUString("*");
125 0 : aParam.Value <<= sal_True;
126 0 : lParams[1] <<= aParam;
127 : }
128 :
129 0 : if (bReadOnly)
130 0 : xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGREADACCESS, lParams);
131 : else
132 0 : xCFG = xConfigProvider->createInstanceWithArguments(SERVICENAME_CFGUPDATEACCESS, lParams);
133 : }
134 0 : catch(const css::uno::RuntimeException& exRun)
135 0 : { throw exRun; }
136 0 : catch(const css::uno::Exception&)
137 0 : { xCFG.clear(); }
138 :
139 0 : return xCFG;
140 : }
141 : };
142 :
143 : } // namespace configuration
144 : } // namespace pattern
145 : } // namespace framework
146 :
147 : #endif // __FRAMEWORK_PATTERN_CONFIGURATION_HXX_
148 :
149 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|