lok: register view callback also for nested sm view
[LibreOffice.git] / comphelper / source / misc / configurationhelper.cxx
blobf3853baeff816a789478ddb7c0a4ff270ecc737a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/configurationhelper.hxx>
21 #include <comphelper/sequence.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
28 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
29 #include <com/sun/star/util/XChangesBatch.hpp>
32 namespace comphelper{
35 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
36 const OUString& sPackage,
37 EConfigurationModes eMode )
39 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
40 css::configuration::theDefaultProvider::get( rxContext ) );
42 std::vector< css::uno::Any > lParams;
43 css::beans::PropertyValue aParam ;
45 // set root path
46 aParam.Name = "nodepath";
47 aParam.Value <<= sPackage;
48 lParams.emplace_back(aParam);
50 // enable all locales mode
51 if (eMode & EConfigurationModes::AllLocales)
53 aParam.Name = "locale";
54 aParam.Value <<= OUString("*");
55 lParams.emplace_back(aParam);
58 // open it
59 css::uno::Reference< css::uno::XInterface > xCFG;
61 bool bReadOnly(eMode & EConfigurationModes::ReadOnly);
62 if (bReadOnly)
63 xCFG = xConfigProvider->createInstanceWithArguments(
64 "com.sun.star.configuration.ConfigurationAccess",
65 comphelper::containerToSequence(lParams));
66 else
67 xCFG = xConfigProvider->createInstanceWithArguments(
68 "com.sun.star.configuration.ConfigurationUpdateAccess",
69 comphelper::containerToSequence(lParams));
71 return xCFG;
75 css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG ,
76 const OUString& sRelPath,
77 const OUString& sKey )
79 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
81 css::uno::Reference< css::beans::XPropertySet > xProps;
82 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
83 if (!xProps.is())
85 throw css::container::NoSuchElementException(
86 "The requested path \"" + sRelPath + "\" does not exist.");
88 return xProps->getPropertyValue(sKey);
92 void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface >& xCFG ,
93 const OUString& sRelPath,
94 const OUString& sKey ,
95 const css::uno::Any& aValue )
97 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
99 css::uno::Reference< css::beans::XPropertySet > xProps;
100 xAccess->getByHierarchicalName(sRelPath) >>= xProps;
101 if (!xProps.is())
103 throw css::container::NoSuchElementException(
104 "The requested path \"" + sRelPath + "\" does not exist.");
106 xProps->setPropertyValue(sKey, aValue);
110 css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface >& xCFG ,
111 const OUString& sRelPathToSet,
112 const OUString& sSetNode )
114 css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
115 css::uno::Reference< css::container::XNameAccess > xSet;
116 xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
117 if (!xSet.is())
119 throw css::container::NoSuchElementException(
120 "The requested path \"" + sRelPathToSet + "\" does not exist." );
123 css::uno::Reference< css::uno::XInterface > xNode;
124 if (xSet->hasByName(sSetNode))
125 xSet->getByName(sSetNode) >>= xNode;
126 else
128 css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
129 xNode = xNodeFactory->createInstance();
130 css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
131 xSetReplace->insertByName(sSetNode, css::uno::Any(xNode));
134 return xNode;
138 css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
139 const OUString& sPackage,
140 const OUString& sRelPath,
141 const OUString& sKey ,
142 EConfigurationModes eMode )
144 css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
145 return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
149 void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
150 const OUString& sPackage,
151 const OUString& sRelPath,
152 const OUString& sKey ,
153 const css::uno::Any& aValue ,
154 EConfigurationModes eMode )
156 css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
157 ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
158 ConfigurationHelper::flush(xCFG);
162 void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
164 css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
165 xBatch->commitChanges();
168 } // namespace comphelper
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */