1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <com/sun/star/beans/NamedValue.hpp>
23 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
24 #include <com/sun/star/configuration/theDefaultProvider.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <i18nlangtag/languagetag.hxx>
30 #include <officecfg/Setup.hxx>
31 #include <rtl/ustring.hxx>
32 #include <sal/log.hxx>
33 #include <unotools/configitem.hxx>
34 #include <unotools/configmgr.hxx>
35 #include <comphelper/processfactory.hxx>
39 class RegisterConfigItemHelper
{
41 RegisterConfigItemHelper(
42 utl::ConfigManager
& manager
, utl::ConfigItem
& item
):
43 manager_(manager
), item_(&item
)
45 manager
.registerConfigItem(item_
);
48 ~RegisterConfigItemHelper() {
49 if (item_
!= nullptr) {
50 manager_
.removeConfigItem(*item_
);
54 void keep() { item_
= nullptr; }
57 utl::ConfigManager
& manager_
;
58 utl::ConfigItem
* item_
;
60 RegisterConfigItemHelper(const RegisterConfigItemHelper
&) = delete;
61 RegisterConfigItemHelper
& operator=(const RegisterConfigItemHelper
&) = delete;
64 css::uno::Reference
< css::lang::XMultiServiceFactory
>
65 getConfigurationProvider() {
66 return css::configuration::theDefaultProvider::get( comphelper::getProcessComponentContext() );
71 OUString
utl::ConfigManager::getAboutBoxProductVersion() {
72 return officecfg::Setup::Product::ooSetupVersionAboutBox::get();
75 OUString
utl::ConfigManager::getAboutBoxProductVersionSuffix() {
76 return officecfg::Setup::Product::ooSetupVersionAboutBoxSuffix::get();
79 OUString
utl::ConfigManager::getDefaultCurrency() {
80 return officecfg::Setup::L10N::ooSetupCurrency::get();
83 OUString
utl::ConfigManager::getUILocale() {
84 return officecfg::Setup::L10N::ooLocale::get();
87 OUString
utl::ConfigManager::getWorkLocale() {
88 return officecfg::Setup::L10N::ooSetupSystemLocale::get();
91 OUString
utl::ConfigManager::getProductExtension() {
92 return officecfg::Setup::Product::ooSetupExtension::get();
95 OUString
utl::ConfigManager::getProductName() {
96 return officecfg::Setup::Product::ooName::get();
99 OUString
utl::ConfigManager::getProductVersion() {
100 return officecfg::Setup::Product::ooSetupVersion::get();
103 OUString
utl::ConfigManager::getVendor() {
104 return officecfg::Setup::Product::ooVendor::get();
107 void utl::ConfigManager::storeConfigItems() {
108 getConfigManager().doStoreConfigItems();
111 utl::ConfigManager
& utl::ConfigManager::getConfigManager() {
112 static utl::ConfigManager theConfigManager
;
113 return theConfigManager
;
116 css::uno::Reference
< css::container::XHierarchicalNameAccess
>
117 utl::ConfigManager::acquireTree(utl::ConfigItem
const & item
) {
118 css::uno::Sequence
< css::uno::Any
> args
{ css::uno::Any(css::beans::NamedValue(
120 css::uno::Any("/org.openoffice." + item
.GetSubTreeName()))) };
121 if (item
.GetMode() & ConfigItemMode::AllLocales
) {
123 args
.getArray()[1] <<= css::beans::NamedValue(u
"locale"_ustr
, css::uno::Any(u
"*"_ustr
));
125 return css::uno::Reference
< css::container::XHierarchicalNameAccess
>(
126 getConfigurationProvider()->createInstanceWithArguments(
127 u
"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr
,
129 css::uno::UNO_QUERY_THROW
);
132 css::uno::Reference
< css::container::XHierarchicalNameAccess
>
133 utl::ConfigManager::acquireTree(std::u16string_view rSubTreeName
) {
134 css::uno::Sequence
< css::uno::Any
> args
{ css::uno::Any(css::beans::NamedValue(
136 css::uno::Any(OUString::Concat(u
"/org.openoffice.") + rSubTreeName
))) };
137 return css::uno::Reference
< css::container::XHierarchicalNameAccess
>(
138 getConfigurationProvider()->createInstanceWithArguments(
139 u
"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr
,
141 css::uno::UNO_QUERY_THROW
);
144 utl::ConfigManager::ConfigManager() {}
146 utl::ConfigManager::~ConfigManager() {
147 SAL_WARN_IF(!items_
.empty(), "unotools.config", "ConfigManager not empty");
150 css::uno::Reference
< css::container::XHierarchicalNameAccess
>
151 utl::ConfigManager::addConfigItem(utl::ConfigItem
& item
) {
152 RegisterConfigItemHelper
reg(*this, item
);
153 css::uno::Reference
< css::container::XHierarchicalNameAccess
> tree(
159 void utl::ConfigManager::removeConfigItem(utl::ConfigItem
& item
) {
160 std::erase(items_
, &item
);
163 void utl::ConfigManager::registerConfigItem(utl::ConfigItem
* item
) {
164 assert(item
!= nullptr);
165 items_
.push_back(item
);
168 void utl::ConfigManager::doStoreConfigItems() {
169 for (auto const& item
: items_
)
171 if (item
->IsModified()) {
173 item
->ClearModified();
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */