1*5b190011SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*5b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*5b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*5b190011SAndrew Rist * distributed with this work for additional information 6*5b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*5b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*5b190011SAndrew Rist * "License"); you may not use this file except in compliance 9*5b190011SAndrew Rist * with the License. You may obtain a copy of the License at 10*5b190011SAndrew Rist * 11*5b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*5b190011SAndrew Rist * 13*5b190011SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*5b190011SAndrew Rist * software distributed under the License is distributed on an 15*5b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*5b190011SAndrew Rist * KIND, either express or implied. See the License for the 17*5b190011SAndrew Rist * specific language governing permissions and limitations 18*5b190011SAndrew Rist * under the License. 19*5b190011SAndrew Rist * 20*5b190011SAndrew Rist *************************************************************/ 21*5b190011SAndrew Rist 22*5b190011SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "precompiled_sd.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include "framework/Configuration.hxx" 27cdf0e10cSrcweir 28cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx" 29cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir 33cdf0e10cSrcweir using namespace ::com::sun::star; 34cdf0e10cSrcweir using namespace ::com::sun::star::uno; 35cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework; 36cdf0e10cSrcweir using ::sd::framework::FrameworkHelper; 37cdf0e10cSrcweir using ::rtl::OUString; 38cdf0e10cSrcweir 39cdf0e10cSrcweir #undef VERBOSE 40cdf0e10cSrcweir 41cdf0e10cSrcweir namespace { 42cdf0e10cSrcweir /** Use the XResourceId::compareTo() method to implement a compare operator 43cdf0e10cSrcweir for STL containers. 44cdf0e10cSrcweir */ 45cdf0e10cSrcweir class XResourceIdLess 46cdf0e10cSrcweir : public ::std::binary_function <Reference<XResourceId>, Reference<XResourceId>, bool> 47cdf0e10cSrcweir { 48cdf0e10cSrcweir public: 49cdf0e10cSrcweir bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const 50cdf0e10cSrcweir { 51cdf0e10cSrcweir return rId1->compareTo(rId2) == -1; 52cdf0e10cSrcweir } 53cdf0e10cSrcweir }; 54cdf0e10cSrcweir 55cdf0e10cSrcweir } // end of anonymous namespace 56cdf0e10cSrcweir 57cdf0e10cSrcweir 58cdf0e10cSrcweir 59cdf0e10cSrcweir 60cdf0e10cSrcweir namespace sd { namespace framework { 61cdf0e10cSrcweir 62cdf0e10cSrcweir 63cdf0e10cSrcweir class Configuration::ResourceContainer 64cdf0e10cSrcweir : public ::std::set<Reference<XResourceId>, XResourceIdLess> 65cdf0e10cSrcweir { 66cdf0e10cSrcweir public: 67cdf0e10cSrcweir ResourceContainer (void) {} 68cdf0e10cSrcweir }; 69cdf0e10cSrcweir 70cdf0e10cSrcweir 71cdf0e10cSrcweir 72cdf0e10cSrcweir 73cdf0e10cSrcweir //----- Service --------------------------------------------------------------- 74cdf0e10cSrcweir 75cdf0e10cSrcweir Reference<XInterface> SAL_CALL Configuration_createInstance ( 76cdf0e10cSrcweir const Reference<XComponentContext>& rxContext) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir (void)rxContext; 79cdf0e10cSrcweir return Reference<XInterface>(static_cast<XWeak*>(new Configuration(NULL,false))); 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir 83cdf0e10cSrcweir 84cdf0e10cSrcweir 85cdf0e10cSrcweir OUString Configuration_getImplementationName (void) throw(RuntimeException) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir return OUString(RTL_CONSTASCII_USTRINGPARAM( 88cdf0e10cSrcweir "com.sun.star.comp.Draw.framework.configuration.Configuration")); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir 92cdf0e10cSrcweir 93cdf0e10cSrcweir 94cdf0e10cSrcweir Sequence<rtl::OUString> SAL_CALL Configuration_getSupportedServiceNames (void) 95cdf0e10cSrcweir throw (RuntimeException) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir static const OUString sServiceName(OUString::createFromAscii( 98cdf0e10cSrcweir "com.sun.star.drawing.framework.Configuration")); 99cdf0e10cSrcweir return Sequence<rtl::OUString>(&sServiceName, 1); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir 102cdf0e10cSrcweir 103cdf0e10cSrcweir 104cdf0e10cSrcweir 105cdf0e10cSrcweir //===== Configuration ========================================================= 106cdf0e10cSrcweir 107cdf0e10cSrcweir Configuration::Configuration ( 108cdf0e10cSrcweir const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster, 109cdf0e10cSrcweir bool bBroadcastRequestEvents) 110cdf0e10cSrcweir : ConfigurationInterfaceBase(MutexOwner::maMutex), 111cdf0e10cSrcweir mpResourceContainer(new ResourceContainer()), 112cdf0e10cSrcweir mxBroadcaster(rxBroadcaster), 113cdf0e10cSrcweir mbBroadcastRequestEvents(bBroadcastRequestEvents) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir 118cdf0e10cSrcweir 119cdf0e10cSrcweir Configuration::Configuration ( 120cdf0e10cSrcweir const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster, 121cdf0e10cSrcweir bool bBroadcastRequestEvents, 122cdf0e10cSrcweir const ResourceContainer& rResourceContainer) 123cdf0e10cSrcweir : ConfigurationInterfaceBase(MutexOwner::maMutex), 124cdf0e10cSrcweir mpResourceContainer(new ResourceContainer(rResourceContainer)), 125cdf0e10cSrcweir mxBroadcaster(rxBroadcaster), 126cdf0e10cSrcweir mbBroadcastRequestEvents(bBroadcastRequestEvents) 127cdf0e10cSrcweir { 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir 131cdf0e10cSrcweir 132cdf0e10cSrcweir 133cdf0e10cSrcweir Configuration::~Configuration (void) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir 138cdf0e10cSrcweir 139cdf0e10cSrcweir 140cdf0e10cSrcweir void SAL_CALL Configuration::disposing (void) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 143cdf0e10cSrcweir mpResourceContainer->clear(); 144cdf0e10cSrcweir mxBroadcaster = NULL; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir //----- XConfiguration -------------------------------------------------------- 151cdf0e10cSrcweir 152cdf0e10cSrcweir void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId) 153cdf0e10cSrcweir throw (RuntimeException) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir ThrowIfDisposed(); 156cdf0e10cSrcweir 157cdf0e10cSrcweir if ( ! rxResourceId.is() || rxResourceId->getResourceURL().getLength()==0) 158cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException(); 159cdf0e10cSrcweir 160cdf0e10cSrcweir if (mpResourceContainer->find(rxResourceId) == mpResourceContainer->end()) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir #ifdef VERBOSE 163cdf0e10cSrcweir OSL_TRACE("Configuration::addResource() %s", 164cdf0e10cSrcweir OUStringToOString( 165cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()); 166cdf0e10cSrcweir #endif 167cdf0e10cSrcweir mpResourceContainer->insert(rxResourceId); 168cdf0e10cSrcweir PostEvent(rxResourceId, true); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir } 171cdf0e10cSrcweir 172cdf0e10cSrcweir 173cdf0e10cSrcweir 174cdf0e10cSrcweir 175cdf0e10cSrcweir void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId) 176cdf0e10cSrcweir throw (RuntimeException) 177cdf0e10cSrcweir { 178cdf0e10cSrcweir ThrowIfDisposed(); 179cdf0e10cSrcweir 180cdf0e10cSrcweir if ( ! rxResourceId.is() || rxResourceId->getResourceURL().getLength()==0) 181cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException(); 182cdf0e10cSrcweir 183cdf0e10cSrcweir ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId)); 184cdf0e10cSrcweir if (iResource != mpResourceContainer->end()) 185cdf0e10cSrcweir { 186cdf0e10cSrcweir #ifdef VERBOSE 187cdf0e10cSrcweir OSL_TRACE("Configuration::removeResource() %s", 188cdf0e10cSrcweir OUStringToOString( 189cdf0e10cSrcweir FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()); 190cdf0e10cSrcweir #endif 191cdf0e10cSrcweir PostEvent(rxResourceId,false); 192cdf0e10cSrcweir mpResourceContainer->erase(iResource); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir } 195cdf0e10cSrcweir 196cdf0e10cSrcweir 197cdf0e10cSrcweir 198cdf0e10cSrcweir 199cdf0e10cSrcweir Sequence<Reference<XResourceId> > SAL_CALL Configuration::getResources ( 200cdf0e10cSrcweir const Reference<XResourceId>& rxAnchorId, 201cdf0e10cSrcweir const ::rtl::OUString& rsResourceURLPrefix, 202cdf0e10cSrcweir AnchorBindingMode eMode) 203cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 206cdf0e10cSrcweir ThrowIfDisposed(); 207cdf0e10cSrcweir 208cdf0e10cSrcweir bool bFilterResources (rsResourceURLPrefix.getLength() > 0); 209cdf0e10cSrcweir 210cdf0e10cSrcweir // Collect the matching resources in a vector. 211cdf0e10cSrcweir ::std::vector<Reference<XResourceId> > aResources; 212cdf0e10cSrcweir ResourceContainer::const_iterator iResource; 213cdf0e10cSrcweir for (iResource=mpResourceContainer->begin(); 214cdf0e10cSrcweir iResource!=mpResourceContainer->end(); 215cdf0e10cSrcweir ++iResource) 216cdf0e10cSrcweir { 217cdf0e10cSrcweir if ( ! (*iResource)->isBoundTo(rxAnchorId,eMode)) 218cdf0e10cSrcweir continue; 219cdf0e10cSrcweir 220cdf0e10cSrcweir 221cdf0e10cSrcweir if (bFilterResources) 222cdf0e10cSrcweir { 223cdf0e10cSrcweir // Apply the given resource prefix as filter. 224cdf0e10cSrcweir 225cdf0e10cSrcweir // Make sure that the resource is bound directly to the anchor. 226cdf0e10cSrcweir if (eMode != AnchorBindingMode_DIRECT 227cdf0e10cSrcweir && ! (*iResource)->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT)) 228cdf0e10cSrcweir { 229cdf0e10cSrcweir continue; 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir // Make sure that the resource URL matches the given prefix. 233cdf0e10cSrcweir if ( ! (*iResource)->getResourceURL().match(rsResourceURLPrefix)) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir continue; 236cdf0e10cSrcweir } 237cdf0e10cSrcweir } 238cdf0e10cSrcweir 239cdf0e10cSrcweir aResources.push_back(*iResource); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir 242cdf0e10cSrcweir // Copy the resources from the vector into a new sequence. 243cdf0e10cSrcweir Sequence<Reference<XResourceId> > aResult (aResources.size()); 244cdf0e10cSrcweir for (sal_uInt32 nIndex=0; nIndex<aResources.size(); ++nIndex) 245cdf0e10cSrcweir aResult[nIndex] = aResources[nIndex]; 246cdf0e10cSrcweir 247cdf0e10cSrcweir return aResult; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir 250cdf0e10cSrcweir 251cdf0e10cSrcweir 252cdf0e10cSrcweir 253cdf0e10cSrcweir sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId) 254cdf0e10cSrcweir throw (RuntimeException) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 257cdf0e10cSrcweir ThrowIfDisposed(); 258cdf0e10cSrcweir 259cdf0e10cSrcweir return rxResourceId.is() 260cdf0e10cSrcweir && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end(); 261cdf0e10cSrcweir } 262cdf0e10cSrcweir 263cdf0e10cSrcweir 264cdf0e10cSrcweir 265cdf0e10cSrcweir 266cdf0e10cSrcweir //----- XCloneable ------------------------------------------------------------ 267cdf0e10cSrcweir 268cdf0e10cSrcweir Reference<util::XCloneable> SAL_CALL Configuration::createClone (void) 269cdf0e10cSrcweir throw (RuntimeException) 270cdf0e10cSrcweir { 271cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 272cdf0e10cSrcweir ThrowIfDisposed(); 273cdf0e10cSrcweir 274cdf0e10cSrcweir Configuration* pConfiguration = new Configuration( 275cdf0e10cSrcweir mxBroadcaster, 276cdf0e10cSrcweir mbBroadcastRequestEvents, 277cdf0e10cSrcweir *mpResourceContainer); 278cdf0e10cSrcweir 279cdf0e10cSrcweir return Reference<util::XCloneable>(pConfiguration); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir 283cdf0e10cSrcweir 284cdf0e10cSrcweir 285cdf0e10cSrcweir //----- XNamed ---------------------------------------------------------------- 286cdf0e10cSrcweir 287cdf0e10cSrcweir OUString SAL_CALL Configuration::getName (void) 288cdf0e10cSrcweir throw (RuntimeException) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir ::osl::MutexGuard aGuard (maMutex); 291cdf0e10cSrcweir OUString aString; 292cdf0e10cSrcweir 293cdf0e10cSrcweir if (rBHelper.bDisposed || rBHelper.bInDispose) 294cdf0e10cSrcweir aString += OUString::createFromAscii("DISPOSED "); 295cdf0e10cSrcweir aString += OUString::createFromAscii("Configuration["); 296cdf0e10cSrcweir 297cdf0e10cSrcweir ResourceContainer::const_iterator iResource; 298cdf0e10cSrcweir for (iResource=mpResourceContainer->begin(); 299cdf0e10cSrcweir iResource!=mpResourceContainer->end(); 300cdf0e10cSrcweir ++iResource) 301cdf0e10cSrcweir { 302cdf0e10cSrcweir if (iResource != mpResourceContainer->begin()) 303cdf0e10cSrcweir aString += OUString::createFromAscii(", "); 304cdf0e10cSrcweir aString += FrameworkHelper::ResourceIdToString(*iResource); 305cdf0e10cSrcweir } 306cdf0e10cSrcweir aString += OUString::createFromAscii("]"); 307cdf0e10cSrcweir 308cdf0e10cSrcweir return aString; 309cdf0e10cSrcweir } 310cdf0e10cSrcweir 311cdf0e10cSrcweir 312cdf0e10cSrcweir 313cdf0e10cSrcweir 314cdf0e10cSrcweir void SAL_CALL Configuration::setName (const OUString& rsName) 315cdf0e10cSrcweir throw (RuntimeException) 316cdf0e10cSrcweir { 317cdf0e10cSrcweir (void)rsName; // rsName is ignored. 318cdf0e10cSrcweir } 319cdf0e10cSrcweir 320cdf0e10cSrcweir 321cdf0e10cSrcweir 322cdf0e10cSrcweir 323cdf0e10cSrcweir 324cdf0e10cSrcweir // ---------------------------------------------------------------------------- 325cdf0e10cSrcweir 326cdf0e10cSrcweir void Configuration::PostEvent ( 327cdf0e10cSrcweir const Reference<XResourceId>& rxResourceId, 328cdf0e10cSrcweir const bool bActivation) 329cdf0e10cSrcweir { 330cdf0e10cSrcweir OSL_ASSERT(rxResourceId.is()); 331cdf0e10cSrcweir 332cdf0e10cSrcweir if (mxBroadcaster.is()) 333cdf0e10cSrcweir { 334cdf0e10cSrcweir ConfigurationChangeEvent aEvent; 335cdf0e10cSrcweir aEvent.ResourceId = rxResourceId; 336cdf0e10cSrcweir if (bActivation) 337cdf0e10cSrcweir if (mbBroadcastRequestEvents) 338cdf0e10cSrcweir aEvent.Type = FrameworkHelper::msResourceActivationRequestEvent; 339cdf0e10cSrcweir else 340cdf0e10cSrcweir aEvent.Type = FrameworkHelper::msResourceActivationEvent; 341cdf0e10cSrcweir else 342cdf0e10cSrcweir if (mbBroadcastRequestEvents) 343cdf0e10cSrcweir aEvent.Type = FrameworkHelper::msResourceDeactivationRequestEvent; 344cdf0e10cSrcweir else 345cdf0e10cSrcweir aEvent.Type = FrameworkHelper::msResourceDeactivationEvent; 346cdf0e10cSrcweir aEvent.Configuration = this; 347cdf0e10cSrcweir 348cdf0e10cSrcweir mxBroadcaster->notifyEvent(aEvent); 349cdf0e10cSrcweir } 350cdf0e10cSrcweir } 351cdf0e10cSrcweir 352cdf0e10cSrcweir 353cdf0e10cSrcweir 354cdf0e10cSrcweir 355cdf0e10cSrcweir void Configuration::ThrowIfDisposed (void) const 356cdf0e10cSrcweir throw (::com::sun::star::lang::DisposedException) 357cdf0e10cSrcweir { 358cdf0e10cSrcweir if (rBHelper.bDisposed || rBHelper.bInDispose) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir throw lang::DisposedException ( 361cdf0e10cSrcweir OUString(RTL_CONSTASCII_USTRINGPARAM( 362cdf0e10cSrcweir "Configuration object has already been disposed")), 363cdf0e10cSrcweir const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this))); 364cdf0e10cSrcweir } 365cdf0e10cSrcweir } 366cdf0e10cSrcweir 367cdf0e10cSrcweir 368cdf0e10cSrcweir 369cdf0e10cSrcweir 370cdf0e10cSrcweir //============================================================================= 371cdf0e10cSrcweir 372cdf0e10cSrcweir bool AreConfigurationsEquivalent ( 373cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration1, 374cdf0e10cSrcweir const Reference<XConfiguration>& rxConfiguration2) 375cdf0e10cSrcweir { 376cdf0e10cSrcweir if (rxConfiguration1.is() != rxConfiguration2.is()) 377cdf0e10cSrcweir return false; 378cdf0e10cSrcweir if ( ! rxConfiguration1.is() && ! rxConfiguration2.is()) 379cdf0e10cSrcweir return true; 380cdf0e10cSrcweir 381cdf0e10cSrcweir // Get the lists of resources from the two given configurations. 382cdf0e10cSrcweir const Sequence<Reference<XResourceId> > aResources1( 383cdf0e10cSrcweir rxConfiguration1->getResources( 384cdf0e10cSrcweir NULL, OUString(), AnchorBindingMode_INDIRECT)); 385cdf0e10cSrcweir const Sequence<Reference<XResourceId> > aResources2( 386cdf0e10cSrcweir rxConfiguration2->getResources( 387cdf0e10cSrcweir NULL, OUString(), AnchorBindingMode_INDIRECT)); 388cdf0e10cSrcweir 389cdf0e10cSrcweir // When the number of resources differ then the configurations can not 390cdf0e10cSrcweir // be equivalent. 391cdf0e10cSrcweir const sal_Int32 nCount (aResources1.getLength()); 392cdf0e10cSrcweir const sal_Int32 nCount2 (aResources2.getLength()); 393cdf0e10cSrcweir if (nCount != nCount2) 394cdf0e10cSrcweir return false; 395cdf0e10cSrcweir 396cdf0e10cSrcweir // Comparison of the two lists of resource ids relies on their 397cdf0e10cSrcweir // ordering. 398cdf0e10cSrcweir for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex) 399cdf0e10cSrcweir { 400cdf0e10cSrcweir const Reference<XResourceId> xResource1 (aResources1[nIndex]); 401cdf0e10cSrcweir const Reference<XResourceId> xResource2 (aResources2[nIndex]); 402cdf0e10cSrcweir if (xResource1.is() && xResource2.is()) 403cdf0e10cSrcweir { 404cdf0e10cSrcweir if (xResource1->compareTo(xResource2) != 0) 405cdf0e10cSrcweir return false; 406cdf0e10cSrcweir } 407cdf0e10cSrcweir else if (xResource1.is() != xResource2.is()) 408cdf0e10cSrcweir { 409cdf0e10cSrcweir return false; 410cdf0e10cSrcweir } 411cdf0e10cSrcweir } 412cdf0e10cSrcweir 413cdf0e10cSrcweir return true; 414cdf0e10cSrcweir } 415cdf0e10cSrcweir 416cdf0e10cSrcweir } } // end of namespace sd::framework 417