Bump version to 4.3-4
[LibreOffice.git] / unotools / source / config / configmgr.cxx
bloba84db5e702234ca505e2eef014134772b2b1215d
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 <sal/config.h>
22 #include <list>
24 #include <boost/noncopyable.hpp>
25 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
27 #include <com/sun/star/configuration/theDefaultProvider.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/uno/Any.hxx>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <osl/diagnose.h>
33 #include <rtl/instance.hxx>
34 #include <rtl/ustring.h>
35 #include <rtl/ustring.hxx>
36 #include <unotools/configitem.hxx>
37 #include <unotools/configmgr.hxx>
38 #include <comphelper/processfactory.hxx>
40 namespace {
42 class RegisterConfigItemHelper: private boost::noncopyable {
43 public:
44 RegisterConfigItemHelper(
45 utl::ConfigManager & manager, utl::ConfigItem & item):
46 manager_(manager), item_(&item)
48 manager.registerConfigItem(item_);
51 ~RegisterConfigItemHelper() {
52 if (item_ != 0) {
53 manager_.removeConfigItem(*item_);
57 void keep() { item_ = 0; }
59 private:
60 utl::ConfigManager & manager_;
61 utl::ConfigItem * item_;
64 css::uno::Reference< css::lang::XMultiServiceFactory >
65 getConfigurationProvider() {
66 return css::configuration::theDefaultProvider::get( comphelper::getProcessComponentContext() );
69 OUString getConfigurationString(OUString const & module, OUString const & path)
71 css::uno::Sequence< css::uno::Any > args(1);
72 args[0] <<= css::beans::NamedValue(
73 OUString("nodepath"),
74 css::uno::makeAny(module));
75 return
76 css::uno::Reference< css::container::XHierarchicalNameAccess >(
77 getConfigurationProvider()->createInstanceWithArguments(
78 OUString("com.sun.star.configuration.ConfigurationAccess"),
79 args),
80 css::uno::UNO_QUERY_THROW)->
81 getByHierarchicalName(path).get< OUString >();
84 struct theConfigManager:
85 public rtl::Static< utl::ConfigManager, theConfigManager >
86 {};
90 OUString utl::ConfigManager::getAboutBoxProductVersion() {
91 return getConfigurationString(
92 OUString("/org.openoffice.Setup"),
93 OUString("Product/ooSetupVersionAboutBox"));
96 OUString utl::ConfigManager::getAboutBoxProductVersionSuffix() {
97 return getConfigurationString(
98 OUString("/org.openoffice.Setup"),
99 OUString("Product/ooSetupVersionAboutBoxSuffix"));
102 OUString utl::ConfigManager::getDefaultCurrency() {
103 return getConfigurationString(
104 OUString("/org.openoffice.Setup"),
105 OUString("L10N/ooSetupCurrency"));
108 OUString utl::ConfigManager::getLocale() {
109 return getConfigurationString(
110 OUString("/org.openoffice.Setup"),
111 OUString("L10N/ooLocale"));
114 OUString utl::ConfigManager::getProductExtension() {
115 return getConfigurationString(
116 OUString("/org.openoffice.Setup"),
117 OUString("Product/ooSetupExtension"));
120 OUString utl::ConfigManager::getProductName() {
121 return getConfigurationString(
122 OUString("/org.openoffice.Setup"),
123 OUString("Product/ooName"));
126 OUString utl::ConfigManager::getProductVersion() {
127 return getConfigurationString(
128 OUString("/org.openoffice.Setup"),
129 OUString("Product/ooSetupVersion"));
132 OUString utl::ConfigManager::getVendor() {
133 return getConfigurationString(
134 OUString("/org.openoffice.Setup"),
135 OUString("Product/ooVendor"));
138 void utl::ConfigManager::storeConfigItems() {
139 getConfigManager().doStoreConfigItems();
142 utl::ConfigManager & utl::ConfigManager::getConfigManager() {
143 return theConfigManager::get();
146 css::uno::Reference< css::container::XHierarchicalNameAccess >
147 utl::ConfigManager::acquireTree(utl::ConfigItem & item) {
148 css::uno::Sequence< css::uno::Any > args(1);
149 args[0] <<= css::beans::NamedValue(
150 OUString("nodepath"),
151 css::uno::makeAny(
152 OUString("/org.openoffice.") + item.GetSubTreeName()));
153 if ((item.GetMode() & CONFIG_MODE_ALL_LOCALES) != 0) {
154 args.realloc(2);
155 args[1] <<= css::beans::NamedValue(OUString("locale"), css::uno::makeAny(OUString("*")));
157 return css::uno::Reference< css::container::XHierarchicalNameAccess >(
158 getConfigurationProvider()->createInstanceWithArguments(
159 OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
160 args),
161 css::uno::UNO_QUERY_THROW);
164 utl::ConfigManager::ConfigManager() {}
166 utl::ConfigManager::~ConfigManager() {
167 OSL_ASSERT(items_.empty());
170 css::uno::Reference< css::container::XHierarchicalNameAccess >
171 utl::ConfigManager::addConfigItem(utl::ConfigItem & item) {
172 RegisterConfigItemHelper reg(*this, item);
173 css::uno::Reference< css::container::XHierarchicalNameAccess > tree(
174 acquireTree(item));
175 reg.keep();
176 return tree;
179 void utl::ConfigManager::removeConfigItem(utl::ConfigItem & item) {
180 for (std::list< ConfigItem * >::iterator i(items_.begin());
181 i != items_.end(); ++i)
183 if (*i == &item) {
184 items_.erase(i);
185 break;
190 void utl::ConfigManager::registerConfigItem(utl::ConfigItem * item) {
191 OSL_ASSERT(item != 0);
192 items_.push_back(item);
195 void utl::ConfigManager::doStoreConfigItems() {
196 for (std::list< ConfigItem * >::iterator i(items_.begin());
197 i != items_.end(); ++i)
199 if ((*i)->IsModified()) {
200 (*i)->Commit();
201 (*i)->ClearModified();
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */