/src/libreoffice/unotools/source/config/configmgr.cxx
Line | Count | Source |
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 | | #include <sal/config.h> |
21 | | |
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> |
36 | | |
37 | | namespace { |
38 | | |
39 | | class RegisterConfigItemHelper { |
40 | | public: |
41 | | RegisterConfigItemHelper( |
42 | | utl::ConfigManager & manager, utl::ConfigItem & item): |
43 | 0 | manager_(manager), item_(&item) |
44 | 0 | { |
45 | 0 | manager.registerConfigItem(item_); |
46 | 0 | } |
47 | | |
48 | 0 | ~RegisterConfigItemHelper() { |
49 | 0 | if (item_ != nullptr) { |
50 | 0 | manager_.removeConfigItem(*item_); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | void keep() { item_ = nullptr; } |
55 | | |
56 | | private: |
57 | | utl::ConfigManager & manager_; |
58 | | utl::ConfigItem * item_; |
59 | | |
60 | | RegisterConfigItemHelper(const RegisterConfigItemHelper&) = delete; |
61 | | RegisterConfigItemHelper& operator=(const RegisterConfigItemHelper&) = delete; |
62 | | }; |
63 | | |
64 | | css::uno::Reference< css::lang::XMultiServiceFactory > |
65 | 0 | getConfigurationProvider() { |
66 | 0 | return css::configuration::theDefaultProvider::get( comphelper::getProcessComponentContext() ); |
67 | 0 | } |
68 | | |
69 | | } |
70 | | |
71 | 0 | OUString utl::ConfigManager::getAboutBoxProductVersion() { |
72 | 0 | return officecfg::Setup::Product::ooSetupVersionAboutBox::get(); |
73 | 0 | } |
74 | | |
75 | 0 | OUString utl::ConfigManager::getAboutBoxProductVersionSuffix() { |
76 | 0 | return officecfg::Setup::Product::ooSetupVersionAboutBoxSuffix::get(); |
77 | 0 | } |
78 | | |
79 | 0 | OUString utl::ConfigManager::getDefaultCurrency() { |
80 | 0 | return officecfg::Setup::L10N::ooSetupCurrency::get(); |
81 | 0 | } |
82 | | |
83 | 18.0k | OUString utl::ConfigManager::getUILocale() { |
84 | 18.0k | return officecfg::Setup::L10N::ooLocale::get(); |
85 | 18.0k | } |
86 | | |
87 | 0 | OUString utl::ConfigManager::getWorkLocale() { |
88 | 0 | return officecfg::Setup::L10N::ooSetupSystemLocale::get(); |
89 | 0 | } |
90 | | |
91 | 0 | OUString utl::ConfigManager::getProductExtension() { |
92 | 0 | return officecfg::Setup::Product::ooSetupExtension::get(); |
93 | 0 | } |
94 | | |
95 | 8.32k | OUString utl::ConfigManager::getProductName() { |
96 | 8.32k | return officecfg::Setup::Product::ooName::get(); |
97 | 8.32k | } |
98 | | |
99 | 0 | OUString utl::ConfigManager::getProductVersion() { |
100 | 0 | return officecfg::Setup::Product::ooSetupVersion::get(); |
101 | 0 | } |
102 | | |
103 | 0 | OUString utl::ConfigManager::getVendor() { |
104 | 0 | return officecfg::Setup::Product::ooVendor::get(); |
105 | 0 | } |
106 | | |
107 | 0 | void utl::ConfigManager::storeConfigItems() { |
108 | 0 | getConfigManager().doStoreConfigItems(); |
109 | 0 | } |
110 | | |
111 | 12.4k | utl::ConfigManager & utl::ConfigManager::getConfigManager() { |
112 | 12.4k | static utl::ConfigManager theConfigManager; |
113 | 12.4k | return theConfigManager; |
114 | 12.4k | } |
115 | | |
116 | | css::uno::Reference< css::container::XHierarchicalNameAccess > |
117 | 0 | utl::ConfigManager::acquireTree(utl::ConfigItem const & item) { |
118 | 0 | css::uno::Sequence< css::uno::Any > args{ css::uno::Any(css::beans::NamedValue( |
119 | 0 | u"nodepath"_ustr, |
120 | 0 | css::uno::Any("/org.openoffice." + item.GetSubTreeName()))) }; |
121 | 0 | if (item.GetMode() & ConfigItemMode::AllLocales) { |
122 | 0 | args.realloc(2); |
123 | 0 | args.getArray()[1] <<= css::beans::NamedValue(u"locale"_ustr, css::uno::Any(u"*"_ustr)); |
124 | 0 | } |
125 | 0 | return css::uno::Reference< css::container::XHierarchicalNameAccess >( |
126 | 0 | getConfigurationProvider()->createInstanceWithArguments( |
127 | 0 | u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr, |
128 | 0 | args), |
129 | 0 | css::uno::UNO_QUERY_THROW); |
130 | 0 | } |
131 | | |
132 | | css::uno::Reference< css::container::XHierarchicalNameAccess > |
133 | 0 | utl::ConfigManager::acquireTree(std::u16string_view rSubTreeName) { |
134 | 0 | css::uno::Sequence< css::uno::Any > args{ css::uno::Any(css::beans::NamedValue( |
135 | 0 | u"nodepath"_ustr, |
136 | 0 | css::uno::Any(OUString::Concat(u"/org.openoffice.") + rSubTreeName))) }; |
137 | 0 | return css::uno::Reference< css::container::XHierarchicalNameAccess >( |
138 | 0 | getConfigurationProvider()->createInstanceWithArguments( |
139 | 0 | u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr, |
140 | 0 | args), |
141 | 0 | css::uno::UNO_QUERY_THROW); |
142 | 0 | } |
143 | | |
144 | 106 | utl::ConfigManager::ConfigManager() {} |
145 | | |
146 | 106 | utl::ConfigManager::~ConfigManager() { |
147 | 106 | SAL_WARN_IF(!items_.empty(), "unotools.config", "ConfigManager not empty"); |
148 | 106 | } |
149 | | |
150 | | css::uno::Reference< css::container::XHierarchicalNameAccess > |
151 | 0 | utl::ConfigManager::addConfigItem(utl::ConfigItem & item) { |
152 | 0 | RegisterConfigItemHelper reg(*this, item); |
153 | 0 | css::uno::Reference< css::container::XHierarchicalNameAccess > tree( |
154 | 0 | acquireTree(item)); |
155 | 0 | reg.keep(); |
156 | 0 | return tree; |
157 | 0 | } |
158 | | |
159 | 12.4k | void utl::ConfigManager::removeConfigItem(utl::ConfigItem & item) { |
160 | 12.4k | std::erase(items_, &item); |
161 | 12.4k | } |
162 | | |
163 | 0 | void utl::ConfigManager::registerConfigItem(utl::ConfigItem * item) { |
164 | 0 | assert(item != nullptr); |
165 | 0 | items_.push_back(item); |
166 | 0 | } |
167 | | |
168 | 0 | void utl::ConfigManager::doStoreConfigItems() { |
169 | 0 | for (auto const& item : items_) |
170 | 0 | { |
171 | 0 | if (item->IsModified()) { |
172 | 0 | item->Commit(); |
173 | 0 | item->ClearModified(); |
174 | 0 | } |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |