2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-29 14:02:24 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2004-11-17 12:39:56 +00:00
|
|
|
|
|
|
|
#include <comphelper/configurationhelper.hxx>
|
2011-11-14 22:07:23 +01:00
|
|
|
#include <comphelper/processfactory.hxx>
|
2004-11-17 12:39:56 +00:00
|
|
|
#include <com/sun/star/beans/XPropertySet.hpp>
|
2011-11-14 22:07:23 +01:00
|
|
|
#include <com/sun/star/configuration/theDefaultProvider.hpp>
|
2005-09-30 09:26:21 +00:00
|
|
|
#include <com/sun/star/container/XNameAccess.hpp>
|
|
|
|
#include <com/sun/star/container/XNameContainer.hpp>
|
|
|
|
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
|
|
|
|
|
2004-11-17 12:39:56 +00:00
|
|
|
|
|
|
|
namespace comphelper{
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
2012-10-16 14:05:41 +02:00
|
|
|
css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
|
2013-01-28 10:16:20 +01:00
|
|
|
const OUString& sPackage,
|
2012-10-16 14:05:41 +02:00
|
|
|
sal_Int32 eMode )
|
2004-11-17 12:39:56 +00:00
|
|
|
{
|
|
|
|
css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
|
2012-10-16 14:05:41 +02:00
|
|
|
css::configuration::theDefaultProvider::get( rxContext ) );
|
2004-11-17 12:39:56 +00:00
|
|
|
|
|
|
|
::comphelper::SequenceAsVector< css::uno::Any > lParams;
|
|
|
|
css::beans::PropertyValue aParam ;
|
|
|
|
|
|
|
|
// set root path
|
2013-01-28 10:16:20 +01:00
|
|
|
aParam.Name = "nodepath";
|
2004-11-17 12:39:56 +00:00
|
|
|
aParam.Value <<= sPackage;
|
|
|
|
lParams.push_back(css::uno::makeAny(aParam));
|
|
|
|
|
|
|
|
// enable all locales mode
|
|
|
|
if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
|
|
|
|
{
|
2013-01-28 10:16:20 +01:00
|
|
|
aParam.Name = "locale";
|
|
|
|
aParam.Value <<= OUString("*");
|
2004-11-17 12:39:56 +00:00
|
|
|
lParams.push_back(css::uno::makeAny(aParam));
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable lazy writing
|
|
|
|
sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
|
2013-01-28 10:16:20 +01:00
|
|
|
aParam.Name = "lazywrite";
|
2004-11-17 12:39:56 +00:00
|
|
|
aParam.Value = css::uno::makeAny(bLazy);
|
|
|
|
lParams.push_back(css::uno::makeAny(aParam));
|
|
|
|
|
|
|
|
// open it
|
|
|
|
css::uno::Reference< css::uno::XInterface > xCFG;
|
|
|
|
|
|
|
|
sal_Bool bReadOnly = ((eMode & ConfigurationHelper::E_READONLY)==ConfigurationHelper::E_READONLY);
|
|
|
|
if (bReadOnly)
|
|
|
|
xCFG = xConfigProvider->createInstanceWithArguments(
|
2013-01-28 10:16:20 +01:00
|
|
|
OUString("com.sun.star.configuration.ConfigurationAccess"),
|
2004-11-17 12:39:56 +00:00
|
|
|
lParams.getAsConstList());
|
|
|
|
else
|
|
|
|
xCFG = xConfigProvider->createInstanceWithArguments(
|
2013-01-28 10:16:20 +01:00
|
|
|
OUString("com.sun.star.configuration.ConfigurationUpdateAccess"),
|
2004-11-17 12:39:56 +00:00
|
|
|
lParams.getAsConstList());
|
|
|
|
|
|
|
|
return xCFG;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
|
|
css::uno::Any ConfigurationHelper::readRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
|
|
|
|
const ::rtl::OUString& sRelPath,
|
|
|
|
const ::rtl::OUString& sKey )
|
|
|
|
{
|
|
|
|
css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
|
|
|
|
|
|
|
|
css::uno::Reference< css::beans::XPropertySet > xProps;
|
|
|
|
xAccess->getByHierarchicalName(sRelPath) >>= xProps;
|
2005-04-01 15:16:27 +00:00
|
|
|
if (!xProps.is())
|
|
|
|
{
|
|
|
|
::rtl::OUStringBuffer sMsg(256);
|
|
|
|
sMsg.appendAscii("The requested path \"");
|
|
|
|
sMsg.append (sRelPath );
|
|
|
|
sMsg.appendAscii("\" does not exists." );
|
|
|
|
|
|
|
|
throw css::container::NoSuchElementException(
|
|
|
|
sMsg.makeStringAndClear(),
|
|
|
|
css::uno::Reference< css::uno::XInterface >());
|
|
|
|
}
|
2004-11-17 12:39:56 +00:00
|
|
|
return xProps->getPropertyValue(sKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
|
|
void ConfigurationHelper::writeRelativeKey(const css::uno::Reference< css::uno::XInterface > xCFG ,
|
|
|
|
const ::rtl::OUString& sRelPath,
|
|
|
|
const ::rtl::OUString& sKey ,
|
|
|
|
const css::uno::Any& aValue )
|
|
|
|
{
|
|
|
|
css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
|
|
|
|
|
|
|
|
css::uno::Reference< css::beans::XPropertySet > xProps;
|
|
|
|
xAccess->getByHierarchicalName(sRelPath) >>= xProps;
|
2005-04-01 15:16:27 +00:00
|
|
|
if (!xProps.is())
|
|
|
|
{
|
|
|
|
::rtl::OUStringBuffer sMsg(256);
|
|
|
|
sMsg.appendAscii("The requested path \"");
|
|
|
|
sMsg.append (sRelPath );
|
|
|
|
sMsg.appendAscii("\" does not exists." );
|
|
|
|
|
|
|
|
throw css::container::NoSuchElementException(
|
|
|
|
sMsg.makeStringAndClear(),
|
|
|
|
css::uno::Reference< css::uno::XInterface >());
|
|
|
|
}
|
2004-11-17 12:39:56 +00:00
|
|
|
xProps->setPropertyValue(sKey, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
2005-09-30 09:26:21 +00:00
|
|
|
css::uno::Reference< css::uno::XInterface > ConfigurationHelper::makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > xCFG ,
|
|
|
|
const ::rtl::OUString& sRelPathToSet,
|
|
|
|
const ::rtl::OUString& sSetNode )
|
|
|
|
{
|
|
|
|
css::uno::Reference< css::container::XHierarchicalNameAccess > xAccess(xCFG, css::uno::UNO_QUERY_THROW);
|
|
|
|
css::uno::Reference< css::container::XNameAccess > xSet;
|
|
|
|
xAccess->getByHierarchicalName(sRelPathToSet) >>= xSet;
|
|
|
|
if (!xSet.is())
|
|
|
|
{
|
|
|
|
::rtl::OUStringBuffer sMsg(256);
|
|
|
|
sMsg.appendAscii("The requested path \"");
|
|
|
|
sMsg.append (sRelPathToSet );
|
|
|
|
sMsg.appendAscii("\" does not exists." );
|
|
|
|
|
|
|
|
throw css::container::NoSuchElementException(
|
|
|
|
sMsg.makeStringAndClear(),
|
|
|
|
css::uno::Reference< css::uno::XInterface >());
|
|
|
|
}
|
|
|
|
|
|
|
|
css::uno::Reference< css::uno::XInterface > xNode;
|
|
|
|
if (xSet->hasByName(sSetNode))
|
|
|
|
xSet->getByName(sSetNode) >>= xNode;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
css::uno::Reference< css::lang::XSingleServiceFactory > xNodeFactory(xSet, css::uno::UNO_QUERY_THROW);
|
|
|
|
xNode = xNodeFactory->createInstance();
|
|
|
|
css::uno::Reference< css::container::XNameContainer > xSetReplace(xSet, css::uno::UNO_QUERY_THROW);
|
|
|
|
xSetReplace->insertByName(sSetNode, css::uno::makeAny(xNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
return xNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
2012-10-16 14:05:41 +02:00
|
|
|
css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
|
2004-11-17 12:39:56 +00:00
|
|
|
const ::rtl::OUString& sPackage,
|
|
|
|
const ::rtl::OUString& sRelPath,
|
|
|
|
const ::rtl::OUString& sKey ,
|
|
|
|
sal_Int32 eMode )
|
|
|
|
{
|
2012-10-16 14:05:41 +02:00
|
|
|
css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
|
2004-11-17 12:39:56 +00:00
|
|
|
return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
2012-10-16 14:05:41 +02:00
|
|
|
void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::uno::XComponentContext >& rxContext,
|
2004-11-17 12:39:56 +00:00
|
|
|
const ::rtl::OUString& sPackage,
|
|
|
|
const ::rtl::OUString& sRelPath,
|
|
|
|
const ::rtl::OUString& sKey ,
|
|
|
|
const css::uno::Any& aValue ,
|
|
|
|
sal_Int32 eMode )
|
|
|
|
{
|
2012-10-16 14:05:41 +02:00
|
|
|
css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(rxContext, sPackage, eMode);
|
2004-11-17 12:39:56 +00:00
|
|
|
ConfigurationHelper::writeRelativeKey(xCFG, sRelPath, sKey, aValue);
|
|
|
|
ConfigurationHelper::flush(xCFG);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
|
|
void ConfigurationHelper::flush(const css::uno::Reference< css::uno::XInterface >& xCFG)
|
|
|
|
{
|
|
|
|
css::uno::Reference< css::util::XChangesBatch > xBatch(xCFG, css::uno::UNO_QUERY_THROW);
|
|
|
|
xBatch->commitChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace comphelper
|
2010-10-14 08:27:31 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|