bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / framework / configuration / ResourceFactoryManager.cxx
blobe3a7cd911783bd6ef76c968388e072f07459b71f
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 "ResourceFactoryManager.hxx"
21 #include <tools/wldcrd.hxx>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/lang/XComponent.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/util/URLTransformer.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <boost/bind.hpp>
28 #include <algorithm>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::drawing::framework;
34 #undef VERBOSE
35 //#define VERBOSE 1
37 namespace sd { namespace framework {
39 ResourceFactoryManager::ResourceFactoryManager (const Reference<XControllerManager>& rxManager)
40 : maMutex(),
41 maFactoryMap(),
42 maFactoryPatternList(),
43 mxControllerManager(rxManager),
44 mxURLTransformer()
46 // Create the URL transformer.
47 Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
48 mxURLTransformer = util::URLTransformer::create(xContext);
51 ResourceFactoryManager::~ResourceFactoryManager()
53 Reference<lang::XComponent> xComponent (mxURLTransformer, UNO_QUERY);
54 if (xComponent.is())
55 xComponent->dispose();
58 void ResourceFactoryManager::AddFactory (
59 const OUString& rsURL,
60 const Reference<XResourceFactory>& rxFactory)
61 throw (RuntimeException)
63 if ( ! rxFactory.is())
64 throw lang::IllegalArgumentException();
65 if (rsURL.isEmpty())
66 throw lang::IllegalArgumentException();
68 ::osl::MutexGuard aGuard (maMutex);
70 if (rsURL.indexOf('*') >= 0 || rsURL.indexOf('?') >= 0)
72 // The URL is a URL pattern not an single URL.
73 maFactoryPatternList.push_back(FactoryPatternList::value_type(rsURL, rxFactory));
75 #if defined VERBOSE && VERBOSE>=1
76 OSL_TRACE("ResourceFactoryManager::AddFactory pattern %s %x\n",
77 OUStringToOString(rsURL, RTL_TEXTENCODING_UTF8).getStr(),
78 rxFactory.get());
79 #endif
81 else
83 maFactoryMap[rsURL] = rxFactory;
85 #if defined VERBOSE && VERBOSE>=1
86 OSL_TRACE("ResourceFactoryManager::AddFactory fixed %s %x\n",
87 OUStringToOString(rsURL, RTL_TEXTENCODING_UTF8).getStr(),
88 rxFactory.get());
89 #endif
93 void ResourceFactoryManager::RemoveFactoryForURL (
94 const OUString& rsURL)
95 throw (RuntimeException)
97 if (rsURL.isEmpty())
98 throw lang::IllegalArgumentException();
100 ::osl::MutexGuard aGuard (maMutex);
102 FactoryMap::iterator iFactory (maFactoryMap.find(rsURL));
103 if (iFactory != maFactoryMap.end())
105 maFactoryMap.erase(iFactory);
107 else
109 // The URL may be a pattern. Look that up.
110 FactoryPatternList::iterator iPattern;
111 for (iPattern=maFactoryPatternList.begin();
112 iPattern!=maFactoryPatternList.end();
113 ++iPattern)
115 if (iPattern->first == rsURL)
117 // Found the pattern. Remove it.
118 maFactoryPatternList.erase(iPattern);
119 break;
125 void ResourceFactoryManager::RemoveFactoryForReference(
126 const Reference<XResourceFactory>& rxFactory)
127 throw (RuntimeException)
129 ::osl::MutexGuard aGuard (maMutex);
131 // Collect a list with all keys that map to the given factory.
132 ::std::vector<OUString> aKeys;
133 FactoryMap::const_iterator iFactory;
134 for (iFactory=maFactoryMap.begin(); iFactory!=maFactoryMap.end(); ++iFactory)
135 if (iFactory->second == rxFactory)
136 aKeys.push_back(iFactory->first);
138 // Remove the entries whose keys we just have collected.
139 ::std::vector<OUString>::const_iterator iKey;
140 for (iKey=aKeys.begin(); iKey!=aKeys.end(); ++iKey)
141 maFactoryMap.erase(maFactoryMap.find(*iKey));
143 // Remove the pattern entries whose factories are identical to the given
144 // factory.
145 FactoryPatternList::iterator iNewEnd (
146 std::remove_if(
147 maFactoryPatternList.begin(),
148 maFactoryPatternList.end(),
149 ::boost::bind(
150 std::equal_to<Reference<XResourceFactory> >(),
151 ::boost::bind(&FactoryPatternList::value_type::second, _1),
152 rxFactory)));
153 if (iNewEnd != maFactoryPatternList.end())
154 maFactoryPatternList.erase(iNewEnd, maFactoryPatternList.end());
157 Reference<XResourceFactory> ResourceFactoryManager::GetFactory (
158 const OUString& rsCompleteURL)
159 throw (RuntimeException)
161 OUString sURLBase (rsCompleteURL);
162 if (mxURLTransformer.is())
164 util::URL aURL;
165 aURL.Complete = rsCompleteURL;
166 if (mxURLTransformer->parseStrict(aURL))
167 sURLBase = aURL.Main;
170 Reference<XResourceFactory> xFactory = FindFactory(sURLBase);
172 if ( ! xFactory.is() && mxControllerManager.is())
174 Reference<XModuleController> xModuleController(mxControllerManager->getModuleController());
175 if (xModuleController.is())
177 // Ask the module controller to provide a factory of the
178 // requested view type. Note that this can (and should) cause
179 // intermediate calls to AddFactory().
180 xModuleController->requestResource(sURLBase);
182 xFactory = FindFactory(sURLBase);
186 return xFactory;
189 Reference<XResourceFactory> ResourceFactoryManager::FindFactory (const OUString& rsURLBase)
190 throw (RuntimeException)
192 ::osl::MutexGuard aGuard (maMutex);
193 FactoryMap::const_iterator iFactory (maFactoryMap.find(rsURLBase));
194 if (iFactory != maFactoryMap.end())
195 return iFactory->second;
196 else
198 // Check the URL patterns.
199 FactoryPatternList::const_iterator iPattern;
200 for (iPattern=maFactoryPatternList.begin();
201 iPattern!=maFactoryPatternList.end();
202 ++iPattern)
204 WildCard aWildCard (iPattern->first);
205 if (aWildCard.Matches(rsURLBase))
206 return iPattern->second;
209 return NULL;
212 } } // end of namespace sd::framework
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */