LibreOffice Module sd (master) 1
ConfigurationAccess.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/lang/XMultiServiceFactory.hpp>
22#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
23#include <com/sun/star/configuration/theDefaultProvider.hpp>
24#include <com/sun/star/container/XNameAccess.hpp>
25#include <com/sun/star/util/XChangesBatch.hpp>
29#include <sal/log.hxx>
30
31using namespace ::com::sun::star;
32using namespace ::com::sun::star::uno;
33
34namespace sd::tools {
35
37 const OUString& rsRootName,
38 const WriteMode eMode)
39{
40 Reference<lang::XMultiServiceFactory> xProvider =
41 configuration::theDefaultProvider::get( ::comphelper::getProcessComponentContext() );
42 Initialize(xProvider, rsRootName, eMode);
43}
44
46 const Reference<lang::XMultiServiceFactory>& rxProvider,
47 const OUString& rsRootName,
48 const WriteMode eMode)
49{
50 try
51 {
52 Sequence<Any> aCreationArguments(comphelper::InitAnyPropertySequence(
53 {
54 {"nodepath", Any(rsRootName)},
55 {"depth", Any(sal_Int32(-1))}
56 }));
57
58 OUString sAccessService;
59 if (eMode == READ_ONLY)
60 sAccessService = "com.sun.star.configuration.ConfigurationAccess";
61 else
62 sAccessService = "com.sun.star.configuration.ConfigurationUpdateAccess";
63
64 mxRoot = rxProvider->createInstanceWithArguments(
65 sAccessService,
66 aCreationArguments);
67 }
68 catch (Exception&)
69 {
70 DBG_UNHANDLED_EXCEPTION("sd.tools");
71 }
72}
73
75 const OUString& sPathToNode)
76{
78 Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY),
79 sPathToNode);
80}
81
83 const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode,
84 const OUString& sPathToNode)
85{
86 if (sPathToNode.isEmpty())
87 return Any(rxNode);
88
89 try
90 {
91 if (rxNode.is())
92 {
93 return rxNode->getByHierarchicalName(sPathToNode);
94 }
95 }
96 catch (const Exception&)
97 {
98 TOOLS_WARN_EXCEPTION("sd", "caught exception while getting configuration node" << sPathToNode);
99 }
100
101 return Any();
102}
103
105{
106 Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY);
107 if (xConfiguration.is())
108 xConfiguration->commitChanges();
109}
110
111} // end of namespace sd::tools
112
113/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ConfigurationAccess(const OUString &rsRootName, const WriteMode eMode)
Create a new object to access the configuration entries below the given root.
void Initialize(const css::uno::Reference< css::lang::XMultiServiceFactory > &rxProvider, const OUString &rsRootName, const WriteMode eMode)
void CommitChanges()
Write any changes that have been made back to the configuration.
css::uno::Any GetConfigurationNode(const OUString &rsPathToNode)
Return a configuration node below the root of the called object.
css::uno::Reference< css::uno::XInterface > mxRoot
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
Mode eMode
@ Exception
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)