Files
libreoffice/comphelper/source/misc/configurationhelper.cxx

186 lines
8.5 KiB
C++
Raw Normal View History

/*************************************************************************
*
* $RCSfile: configurationhelper.cxx,v $
*
* $Revision: 1.2 $
*
* last change: $Author: obo $ $Date: 2004-11-17 13:39:56 $
*
* The Contents of this file are made available subject to the terms of
* either of the following licenses
*
* - GNU Lesser General Public License Version 2.1
* - Sun Industry Standards Source License Version 1.1
*
* Sun Microsystems Inc., October, 2000
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Sun Industry Standards Source License Version 1.1
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.1 (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.openoffice.org/license.html.
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2000 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): _______________________________________
*
*
************************************************************************/
//_______________________________________________
// includes
#ifndef _COMPHELPER_CONFIGURATIONHELPER_HXX_
#include <comphelper/configurationhelper.hxx>
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
//_______________________________________________
// namespace
namespace comphelper{
namespace css = ::com::sun::star;
//_______________________________________________
// definitions
//-----------------------------------------------
css::uno::Reference< css::uno::XInterface > ConfigurationHelper::openConfig(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
const ::rtl::OUString& sPackage,
sal_Int32 eMode )
{
css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")), css::uno::UNO_QUERY_THROW);
::comphelper::SequenceAsVector< css::uno::Any > lParams;
css::beans::PropertyValue aParam ;
// set root path
aParam.Name = ::rtl::OUString::createFromAscii("nodepath");
aParam.Value <<= sPackage;
lParams.push_back(css::uno::makeAny(aParam));
// enable all locales mode
if ((eMode & ConfigurationHelper::E_ALL_LOCALES)==ConfigurationHelper::E_ALL_LOCALES)
{
aParam.Name = ::rtl::OUString::createFromAscii("locale");
aParam.Value <<= ::rtl::OUString::createFromAscii("*");
lParams.push_back(css::uno::makeAny(aParam));
}
// enable lazy writing
sal_Bool bLazy = ((eMode & ConfigurationHelper::E_LAZY_WRITE)==ConfigurationHelper::E_LAZY_WRITE);
aParam.Name = ::rtl::OUString::createFromAscii("lazywrite");
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(
::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess"),
lParams.getAsConstList());
else
xCFG = xConfigProvider->createInstanceWithArguments(
::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
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;
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;
xProps->setPropertyValue(sKey, aValue);
}
//-----------------------------------------------
css::uno::Any ConfigurationHelper::readDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
const ::rtl::OUString& sPackage,
const ::rtl::OUString& sRelPath,
const ::rtl::OUString& sKey ,
sal_Int32 eMode )
{
css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
return ConfigurationHelper::readRelativeKey(xCFG, sRelPath, sKey);
}
//-----------------------------------------------
void ConfigurationHelper::writeDirectKey(const css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR ,
const ::rtl::OUString& sPackage,
const ::rtl::OUString& sRelPath,
const ::rtl::OUString& sKey ,
const css::uno::Any& aValue ,
sal_Int32 eMode )
{
css::uno::Reference< css::uno::XInterface > xCFG = ConfigurationHelper::openConfig(xSMGR, sPackage, eMode);
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