1*c142477cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*c142477cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*c142477cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*c142477cSAndrew Rist * distributed with this work for additional information 6*c142477cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*c142477cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*c142477cSAndrew Rist * "License"); you may not use this file except in compliance 9*c142477cSAndrew Rist * with the License. You may obtain a copy of the License at 10*c142477cSAndrew Rist * 11*c142477cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*c142477cSAndrew Rist * 13*c142477cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*c142477cSAndrew Rist * software distributed under the License is distributed on an 15*c142477cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*c142477cSAndrew Rist * KIND, either express or implied. See the License for the 17*c142477cSAndrew Rist * specific language governing permissions and limitations 18*c142477cSAndrew Rist * under the License. 19*c142477cSAndrew Rist * 20*c142477cSAndrew Rist *************************************************************/ 21*c142477cSAndrew Rist 22*c142477cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sdext.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "PresenterConfigurationAccess.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp> 30cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp> 31cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 32cdf0e10cSrcweir #include <com/sun/star/util/XChangesBatch.hpp> 33cdf0e10cSrcweir 34cdf0e10cSrcweir using namespace ::com::sun::star; 35cdf0e10cSrcweir using namespace ::com::sun::star::uno; 36cdf0e10cSrcweir using ::rtl::OUString; 37cdf0e10cSrcweir 38cdf0e10cSrcweir #define A2S(pString) (::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(pString))) 39cdf0e10cSrcweir 40cdf0e10cSrcweir namespace sdext { namespace presenter { 41cdf0e10cSrcweir 42cdf0e10cSrcweir const ::rtl::OUString PresenterConfigurationAccess::msPresenterScreenRootName = 43cdf0e10cSrcweir A2S("/org.openoffice.Office.extension.PresenterScreen/"); 44cdf0e10cSrcweir 45cdf0e10cSrcweir PresenterConfigurationAccess::PresenterConfigurationAccess ( 46cdf0e10cSrcweir const Reference<XComponentContext>& rxContext, 47cdf0e10cSrcweir const OUString& rsRootName, 48cdf0e10cSrcweir WriteMode eMode) 49cdf0e10cSrcweir : mxRoot(), 50cdf0e10cSrcweir maNode() 51cdf0e10cSrcweir { 52cdf0e10cSrcweir try 53cdf0e10cSrcweir { 54cdf0e10cSrcweir Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager()); 55cdf0e10cSrcweir if (xFactory.is()) 56cdf0e10cSrcweir { 57cdf0e10cSrcweir Sequence<Any> aCreationArguments(3); 58cdf0e10cSrcweir aCreationArguments[0] = makeAny(beans::PropertyValue( 59cdf0e10cSrcweir A2S("nodepath"), 60cdf0e10cSrcweir 0, 61cdf0e10cSrcweir makeAny(rsRootName), 62cdf0e10cSrcweir beans::PropertyState_DIRECT_VALUE)); 63cdf0e10cSrcweir aCreationArguments[1] = makeAny(beans::PropertyValue( 64cdf0e10cSrcweir A2S("depth"), 65cdf0e10cSrcweir 0, 66cdf0e10cSrcweir makeAny((sal_Int32)-1), 67cdf0e10cSrcweir beans::PropertyState_DIRECT_VALUE)); 68cdf0e10cSrcweir aCreationArguments[2] = makeAny(beans::PropertyValue( 69cdf0e10cSrcweir A2S("lazywrite"), 70cdf0e10cSrcweir 0, 71cdf0e10cSrcweir makeAny(true), 72cdf0e10cSrcweir beans::PropertyState_DIRECT_VALUE)); 73cdf0e10cSrcweir 74cdf0e10cSrcweir OUString sAccessService; 75cdf0e10cSrcweir if (eMode == READ_ONLY) 76cdf0e10cSrcweir sAccessService = A2S("com.sun.star.configuration.ConfigurationAccess"); 77cdf0e10cSrcweir else 78cdf0e10cSrcweir sAccessService = A2S("com.sun.star.configuration.ConfigurationUpdateAccess"); 79cdf0e10cSrcweir 80cdf0e10cSrcweir Reference<lang::XMultiServiceFactory> xProvider ( 81cdf0e10cSrcweir xFactory->createInstanceWithContext( 82cdf0e10cSrcweir A2S("com.sun.star.configuration.ConfigurationProvider"), 83cdf0e10cSrcweir rxContext), 84cdf0e10cSrcweir UNO_QUERY_THROW); 85cdf0e10cSrcweir mxRoot = xProvider->createInstanceWithArguments( 86cdf0e10cSrcweir sAccessService, aCreationArguments); 87cdf0e10cSrcweir maNode <<= mxRoot; 88cdf0e10cSrcweir } 89cdf0e10cSrcweir } 90cdf0e10cSrcweir catch (Exception& rException) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir OSL_TRACE ("caught exception while opening configuration: %s", 93cdf0e10cSrcweir ::rtl::OUStringToOString(rException.Message, 94cdf0e10cSrcweir RTL_TEXTENCODING_UTF8).getStr()); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir 99cdf0e10cSrcweir 100cdf0e10cSrcweir 101cdf0e10cSrcweir PresenterConfigurationAccess::~PresenterConfigurationAccess (void) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir } 104cdf0e10cSrcweir 105cdf0e10cSrcweir 106cdf0e10cSrcweir 107cdf0e10cSrcweir 108cdf0e10cSrcweir bool PresenterConfigurationAccess::IsValid (void) const 109cdf0e10cSrcweir { 110cdf0e10cSrcweir return mxRoot.is(); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir 114cdf0e10cSrcweir 115cdf0e10cSrcweir 116cdf0e10cSrcweir Any PresenterConfigurationAccess::GetConfigurationNode (const OUString& sPathToNode) 117cdf0e10cSrcweir { 118cdf0e10cSrcweir return GetConfigurationNode( 119cdf0e10cSrcweir Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY), 120cdf0e10cSrcweir sPathToNode); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir 123cdf0e10cSrcweir 124cdf0e10cSrcweir 125cdf0e10cSrcweir 126cdf0e10cSrcweir Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties ( 127cdf0e10cSrcweir const OUString& sPathToNode) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir return GetNodeProperties( 130cdf0e10cSrcweir Reference<container::XHierarchicalNameAccess>(mxRoot, UNO_QUERY), 131cdf0e10cSrcweir sPathToNode); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir 135cdf0e10cSrcweir 136cdf0e10cSrcweir 137cdf0e10cSrcweir bool PresenterConfigurationAccess::GoToChild (const ::rtl::OUString& rsPathToNode) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir if ( ! IsValid()) 140cdf0e10cSrcweir return false; 141cdf0e10cSrcweir 142cdf0e10cSrcweir Reference<container::XHierarchicalNameAccess> xNode (maNode, UNO_QUERY); 143cdf0e10cSrcweir if (xNode.is()) 144cdf0e10cSrcweir { 145cdf0e10cSrcweir maNode = GetConfigurationNode( 146cdf0e10cSrcweir Reference<container::XHierarchicalNameAccess>(maNode, UNO_QUERY), 147cdf0e10cSrcweir rsPathToNode); 148cdf0e10cSrcweir if (Reference<XInterface>(maNode, UNO_QUERY).is()) 149cdf0e10cSrcweir return true; 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir mxRoot = NULL; 153cdf0e10cSrcweir return false; 154cdf0e10cSrcweir } 155cdf0e10cSrcweir 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir 159cdf0e10cSrcweir bool PresenterConfigurationAccess::GoToChild (const Predicate& rPredicate) 160cdf0e10cSrcweir { 161cdf0e10cSrcweir if ( ! IsValid()) 162cdf0e10cSrcweir return false; 163cdf0e10cSrcweir 164cdf0e10cSrcweir maNode = Find(Reference<container::XNameAccess>(maNode,UNO_QUERY), rPredicate); 165cdf0e10cSrcweir if (Reference<XInterface>(maNode, UNO_QUERY).is()) 166cdf0e10cSrcweir return true; 167cdf0e10cSrcweir 168cdf0e10cSrcweir mxRoot = NULL; 169cdf0e10cSrcweir return false; 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir 173cdf0e10cSrcweir 174cdf0e10cSrcweir 175cdf0e10cSrcweir bool PresenterConfigurationAccess::SetProperty ( 176cdf0e10cSrcweir const ::rtl::OUString& rsPropertyName, 177cdf0e10cSrcweir const Any& rValue) 178cdf0e10cSrcweir { 179cdf0e10cSrcweir Reference<beans::XPropertySet> xProperties (maNode, UNO_QUERY); 180cdf0e10cSrcweir if (xProperties.is()) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir xProperties->setPropertyValue(rsPropertyName, rValue); 183cdf0e10cSrcweir return true; 184cdf0e10cSrcweir } 185cdf0e10cSrcweir else 186cdf0e10cSrcweir return false; 187cdf0e10cSrcweir } 188cdf0e10cSrcweir 189cdf0e10cSrcweir 190cdf0e10cSrcweir 191cdf0e10cSrcweir 192cdf0e10cSrcweir Any PresenterConfigurationAccess::GetConfigurationNode ( 193cdf0e10cSrcweir const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode, 194cdf0e10cSrcweir const OUString& sPathToNode) 195cdf0e10cSrcweir { 196cdf0e10cSrcweir if (sPathToNode.getLength() == 0) 197cdf0e10cSrcweir return Any(rxNode); 198cdf0e10cSrcweir 199cdf0e10cSrcweir try 200cdf0e10cSrcweir { 201cdf0e10cSrcweir if (rxNode.is()) 202cdf0e10cSrcweir { 203cdf0e10cSrcweir return rxNode->getByHierarchicalName(sPathToNode); 204cdf0e10cSrcweir } 205cdf0e10cSrcweir } 206cdf0e10cSrcweir catch (Exception& rException) 207cdf0e10cSrcweir { 208cdf0e10cSrcweir OSL_TRACE ("caught exception while getting configuration node %s: %s", 209cdf0e10cSrcweir ::rtl::OUStringToOString(sPathToNode, RTL_TEXTENCODING_UTF8).getStr(), 210cdf0e10cSrcweir ::rtl::OUStringToOString(rException.Message, RTL_TEXTENCODING_UTF8).getStr()); 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir return Any(); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir 217cdf0e10cSrcweir 218cdf0e10cSrcweir 219cdf0e10cSrcweir Reference<beans::XPropertySet> PresenterConfigurationAccess::GetNodeProperties ( 220cdf0e10cSrcweir const css::uno::Reference<css::container::XHierarchicalNameAccess>& rxNode, 221cdf0e10cSrcweir const ::rtl::OUString& rsPathToNode) 222cdf0e10cSrcweir { 223cdf0e10cSrcweir return Reference<beans::XPropertySet>(GetConfigurationNode(rxNode, rsPathToNode), UNO_QUERY); 224cdf0e10cSrcweir } 225cdf0e10cSrcweir 226cdf0e10cSrcweir 227cdf0e10cSrcweir 228cdf0e10cSrcweir 229cdf0e10cSrcweir void PresenterConfigurationAccess::CommitChanges (void) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir Reference<util::XChangesBatch> xConfiguration (mxRoot, UNO_QUERY); 232cdf0e10cSrcweir if (xConfiguration.is()) 233cdf0e10cSrcweir xConfiguration->commitChanges(); 234cdf0e10cSrcweir } 235cdf0e10cSrcweir 236cdf0e10cSrcweir 237cdf0e10cSrcweir 238cdf0e10cSrcweir 239cdf0e10cSrcweir Any PresenterConfigurationAccess::GetValue (const rtl::OUString& sKey) 240cdf0e10cSrcweir { 241cdf0e10cSrcweir Reference<container::XNameAccess> xAccess (GetConfigurationNode(sKey), UNO_QUERY); 242cdf0e10cSrcweir if (xAccess.is()) 243cdf0e10cSrcweir { 244cdf0e10cSrcweir return xAccess->getByName(sKey); 245cdf0e10cSrcweir } 246cdf0e10cSrcweir else 247cdf0e10cSrcweir { 248cdf0e10cSrcweir return Any(); 249cdf0e10cSrcweir } 250cdf0e10cSrcweir } 251cdf0e10cSrcweir 252cdf0e10cSrcweir 253cdf0e10cSrcweir 254cdf0e10cSrcweir 255cdf0e10cSrcweir void PresenterConfigurationAccess::ForAll ( 256cdf0e10cSrcweir const Reference<container::XNameAccess>& rxContainer, 257cdf0e10cSrcweir const ::std::vector<OUString>& rArguments, 258cdf0e10cSrcweir const ItemProcessor& rProcessor) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir if (rxContainer.is()) 261cdf0e10cSrcweir { 262cdf0e10cSrcweir ::std::vector<Any> aValues(rArguments.size()); 263cdf0e10cSrcweir Sequence<OUString> aKeys (rxContainer->getElementNames()); 264cdf0e10cSrcweir for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir bool bHasAllValues (true); 267cdf0e10cSrcweir const OUString& rsKey (aKeys[nItemIndex]); 268cdf0e10cSrcweir Reference<container::XNameAccess> xSetItem (rxContainer->getByName(rsKey), UNO_QUERY); 269cdf0e10cSrcweir Reference<beans::XPropertySet> xSet (xSetItem, UNO_QUERY); 270cdf0e10cSrcweir OSL_ASSERT(xSet.is()); 271cdf0e10cSrcweir if (xSetItem.is()) 272cdf0e10cSrcweir { 273cdf0e10cSrcweir // Get from the current item of the container the children 274cdf0e10cSrcweir // that match the names in the rArguments list. 275cdf0e10cSrcweir for (sal_uInt32 nValueIndex=0; nValueIndex<aValues.size(); ++nValueIndex) 276cdf0e10cSrcweir { 277cdf0e10cSrcweir if ( ! xSetItem->hasByName(rArguments[nValueIndex])) 278cdf0e10cSrcweir bHasAllValues = false; 279cdf0e10cSrcweir else 280cdf0e10cSrcweir aValues[nValueIndex] = xSetItem->getByName(rArguments[nValueIndex]); 281cdf0e10cSrcweir } 282cdf0e10cSrcweir } 283cdf0e10cSrcweir else 284cdf0e10cSrcweir bHasAllValues = false; 285cdf0e10cSrcweir if (bHasAllValues) 286cdf0e10cSrcweir rProcessor(rsKey,aValues); 287cdf0e10cSrcweir } 288cdf0e10cSrcweir } 289cdf0e10cSrcweir } 290cdf0e10cSrcweir 291cdf0e10cSrcweir 292cdf0e10cSrcweir 293cdf0e10cSrcweir 294cdf0e10cSrcweir void PresenterConfigurationAccess::ForAll ( 295cdf0e10cSrcweir const Reference<container::XNameAccess>& rxContainer, 296cdf0e10cSrcweir const PropertySetProcessor& rProcessor) 297cdf0e10cSrcweir { 298cdf0e10cSrcweir if (rxContainer.is()) 299cdf0e10cSrcweir { 300cdf0e10cSrcweir Sequence<OUString> aKeys (rxContainer->getElementNames()); 301cdf0e10cSrcweir for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 302cdf0e10cSrcweir { 303cdf0e10cSrcweir const OUString& rsKey (aKeys[nItemIndex]); 304cdf0e10cSrcweir Reference<beans::XPropertySet> xSet (rxContainer->getByName(rsKey), UNO_QUERY); 305cdf0e10cSrcweir if (xSet.is()) 306cdf0e10cSrcweir rProcessor(rsKey, xSet); 307cdf0e10cSrcweir } 308cdf0e10cSrcweir } 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir 312cdf0e10cSrcweir 313cdf0e10cSrcweir 314cdf0e10cSrcweir void PresenterConfigurationAccess::FillList( 315cdf0e10cSrcweir const Reference<container::XNameAccess>& rxContainer, 316cdf0e10cSrcweir const ::rtl::OUString& rsArgument, 317cdf0e10cSrcweir ::std::vector<OUString>& rList) 318cdf0e10cSrcweir { 319cdf0e10cSrcweir try 320cdf0e10cSrcweir { 321cdf0e10cSrcweir if (rxContainer.is()) 322cdf0e10cSrcweir { 323cdf0e10cSrcweir Sequence<OUString> aKeys (rxContainer->getElementNames()); 324cdf0e10cSrcweir rList.resize(aKeys.getLength()); 325cdf0e10cSrcweir for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 326cdf0e10cSrcweir { 327cdf0e10cSrcweir Reference<container::XNameAccess> xSetItem ( 328cdf0e10cSrcweir rxContainer->getByName(aKeys[nItemIndex]), UNO_QUERY); 329cdf0e10cSrcweir if (xSetItem.is()) 330cdf0e10cSrcweir { 331cdf0e10cSrcweir xSetItem->getByName(rsArgument) >>= rList[nItemIndex]; 332cdf0e10cSrcweir } 333cdf0e10cSrcweir } 334cdf0e10cSrcweir } 335cdf0e10cSrcweir } 336cdf0e10cSrcweir catch (RuntimeException&) 337cdf0e10cSrcweir {} 338cdf0e10cSrcweir } 339cdf0e10cSrcweir 340cdf0e10cSrcweir 341cdf0e10cSrcweir 342cdf0e10cSrcweir 343cdf0e10cSrcweir Any PresenterConfigurationAccess::Find ( 344cdf0e10cSrcweir const Reference<container::XNameAccess>& rxContainer, 345cdf0e10cSrcweir const Predicate& rPredicate) 346cdf0e10cSrcweir { 347cdf0e10cSrcweir if (rxContainer.is()) 348cdf0e10cSrcweir { 349cdf0e10cSrcweir Sequence<OUString> aKeys (rxContainer->getElementNames()); 350cdf0e10cSrcweir for (sal_Int32 nItemIndex=0; nItemIndex<aKeys.getLength(); ++nItemIndex) 351cdf0e10cSrcweir { 352cdf0e10cSrcweir Reference<beans::XPropertySet> xProperties ( 353cdf0e10cSrcweir rxContainer->getByName(aKeys[nItemIndex]), 354cdf0e10cSrcweir UNO_QUERY); 355cdf0e10cSrcweir if (xProperties.is()) 356cdf0e10cSrcweir if (rPredicate(aKeys[nItemIndex], xProperties)) 357cdf0e10cSrcweir return Any(xProperties); 358cdf0e10cSrcweir } 359cdf0e10cSrcweir } 360cdf0e10cSrcweir return Any(); 361cdf0e10cSrcweir } 362cdf0e10cSrcweir 363cdf0e10cSrcweir 364cdf0e10cSrcweir 365cdf0e10cSrcweir 366cdf0e10cSrcweir bool PresenterConfigurationAccess::IsStringPropertyEqual ( 367cdf0e10cSrcweir const ::rtl::OUString& rsValue, 368cdf0e10cSrcweir const ::rtl::OUString& rsPropertyName, 369cdf0e10cSrcweir const css::uno::Reference<css::beans::XPropertySet>& rxNode) 370cdf0e10cSrcweir { 371cdf0e10cSrcweir OUString sValue; 372cdf0e10cSrcweir if (GetProperty(rxNode, rsPropertyName) >>= sValue) 373cdf0e10cSrcweir return sValue == rsValue; 374cdf0e10cSrcweir else 375cdf0e10cSrcweir return false; 376cdf0e10cSrcweir } 377cdf0e10cSrcweir 378cdf0e10cSrcweir 379cdf0e10cSrcweir 380cdf0e10cSrcweir 381cdf0e10cSrcweir Any PresenterConfigurationAccess::GetProperty ( 382cdf0e10cSrcweir const Reference<beans::XPropertySet>& rxProperties, 383cdf0e10cSrcweir const OUString& rsKey) 384cdf0e10cSrcweir { 385cdf0e10cSrcweir OSL_ASSERT(rxProperties.is()); 386cdf0e10cSrcweir if ( ! rxProperties.is()) 387cdf0e10cSrcweir return Any(); 388cdf0e10cSrcweir try 389cdf0e10cSrcweir { 390cdf0e10cSrcweir Reference<beans::XPropertySetInfo> xInfo (rxProperties->getPropertySetInfo()); 391cdf0e10cSrcweir if (xInfo.is()) 392cdf0e10cSrcweir if ( ! xInfo->hasPropertyByName(rsKey)) 393cdf0e10cSrcweir return Any(); 394cdf0e10cSrcweir return rxProperties->getPropertyValue(rsKey); 395cdf0e10cSrcweir } 396cdf0e10cSrcweir catch (beans::UnknownPropertyException&) 397cdf0e10cSrcweir { 398cdf0e10cSrcweir } 399cdf0e10cSrcweir return Any(); 400cdf0e10cSrcweir } 401cdf0e10cSrcweir 402cdf0e10cSrcweir 403cdf0e10cSrcweir 404cdf0e10cSrcweir 405cdf0e10cSrcweir } } // end of namespace sdext::tools 406cdf0e10cSrcweir 407