Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "ResourceFactoryManager.hxx"
22 : #include <tools/wldcrd.hxx>
23 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 : #include <com/sun/star/lang/XComponent.hpp>
25 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 : #include <com/sun/star/util/URLTransformer.hpp>
27 : #include <comphelper/processfactory.hxx>
28 : #include <boost/bind.hpp>
29 : #include <algorithm>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 : using namespace ::com::sun::star::drawing::framework;
34 : using ::rtl::OUString;
35 :
36 : namespace sd { namespace framework {
37 :
38 0 : ResourceFactoryManager::ResourceFactoryManager (const Reference<XControllerManager>& rxManager)
39 : : maMutex(),
40 : maFactoryMap(),
41 : maFactoryPatternList(),
42 : mxControllerManager(rxManager),
43 0 : mxURLTransformer()
44 : {
45 : // Create the URL transformer.
46 0 : Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
47 0 : mxURLTransformer = util::URLTransformer::create(xContext);
48 0 : }
49 :
50 :
51 :
52 :
53 0 : ResourceFactoryManager::~ResourceFactoryManager (void)
54 : {
55 0 : Reference<lang::XComponent> xComponent (mxURLTransformer, UNO_QUERY);
56 0 : if (xComponent.is())
57 0 : xComponent->dispose();
58 0 : }
59 :
60 :
61 :
62 :
63 0 : void ResourceFactoryManager::AddFactory (
64 : const OUString& rsURL,
65 : const Reference<XResourceFactory>& rxFactory)
66 : throw (RuntimeException)
67 : {
68 0 : if ( ! rxFactory.is())
69 0 : throw lang::IllegalArgumentException();
70 0 : if (rsURL.isEmpty())
71 0 : throw lang::IllegalArgumentException();
72 :
73 0 : ::osl::MutexGuard aGuard (maMutex);
74 :
75 0 : if (rsURL.indexOf('*') >= 0 || rsURL.indexOf('?') >= 0)
76 : {
77 : // The URL is a URL pattern not an single URL.
78 0 : maFactoryPatternList.push_back(FactoryPatternList::value_type(rsURL, rxFactory));
79 : }
80 : else
81 : {
82 0 : maFactoryMap[rsURL] = rxFactory;
83 0 : }
84 0 : }
85 :
86 :
87 :
88 :
89 0 : void ResourceFactoryManager::RemoveFactoryForURL (
90 : const OUString& rsURL)
91 : throw (RuntimeException)
92 : {
93 0 : if (rsURL.isEmpty())
94 0 : throw lang::IllegalArgumentException();
95 :
96 0 : ::osl::MutexGuard aGuard (maMutex);
97 :
98 0 : FactoryMap::iterator iFactory (maFactoryMap.find(rsURL));
99 0 : if (iFactory != maFactoryMap.end())
100 : {
101 0 : maFactoryMap.erase(iFactory);
102 : }
103 : else
104 : {
105 : // The URL may be a pattern. Look that up.
106 0 : FactoryPatternList::iterator iPattern;
107 0 : for (iPattern=maFactoryPatternList.begin();
108 0 : iPattern!=maFactoryPatternList.end();
109 : ++iPattern)
110 : {
111 0 : if (iPattern->first == rsURL)
112 : {
113 : // Found the pattern. Remove it.
114 0 : maFactoryPatternList.erase(iPattern);
115 0 : break;
116 : }
117 : }
118 0 : }
119 0 : }
120 :
121 :
122 :
123 :
124 :
125 0 : void ResourceFactoryManager::RemoveFactoryForReference(
126 : const Reference<XResourceFactory>& rxFactory)
127 : throw (RuntimeException)
128 : {
129 0 : ::osl::MutexGuard aGuard (maMutex);
130 :
131 : // Collect a list with all keys that map to the given factory.
132 0 : ::std::vector<OUString> aKeys;
133 0 : FactoryMap::const_iterator iFactory;
134 0 : for (iFactory=maFactoryMap.begin(); iFactory!=maFactoryMap.end(); ++iFactory)
135 0 : if (iFactory->second == rxFactory)
136 0 : aKeys.push_back(iFactory->first);
137 :
138 : // Remove the entries whose keys we just have collected.
139 0 : ::std::vector<OUString>::const_iterator iKey;
140 0 : for (iKey=aKeys.begin(); iKey!=aKeys.end(); ++iKey)
141 0 : maFactoryMap.erase(maFactoryMap.find(*iKey));
142 :
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 0 : rxFactory)));
153 0 : if (iNewEnd != maFactoryPatternList.end())
154 0 : maFactoryPatternList.erase(iNewEnd, maFactoryPatternList.end());
155 0 : }
156 :
157 :
158 :
159 :
160 0 : Reference<XResourceFactory> ResourceFactoryManager::GetFactory (
161 : const OUString& rsCompleteURL)
162 : throw (RuntimeException)
163 : {
164 0 : OUString sURLBase (rsCompleteURL);
165 0 : if (mxURLTransformer.is())
166 : {
167 0 : util::URL aURL;
168 0 : aURL.Complete = rsCompleteURL;
169 0 : if (mxURLTransformer->parseStrict(aURL))
170 0 : sURLBase = aURL.Main;
171 : }
172 :
173 0 : Reference<XResourceFactory> xFactory = FindFactory(sURLBase);
174 :
175 0 : if ( ! xFactory.is() && mxControllerManager.is())
176 : {
177 0 : Reference<XModuleController> xModuleController(mxControllerManager->getModuleController());
178 0 : if (xModuleController.is())
179 : {
180 : // Ask the module controller to provide a factory of the
181 : // requested view type. Note that this can (and should) cause
182 : // intermediate calls to AddFactory().
183 0 : xModuleController->requestResource(sURLBase);
184 :
185 0 : xFactory = FindFactory(sURLBase);
186 0 : }
187 : }
188 :
189 0 : return xFactory;
190 : }
191 :
192 :
193 :
194 :
195 0 : Reference<XResourceFactory> ResourceFactoryManager::FindFactory (const OUString& rsURLBase)
196 : throw (RuntimeException)
197 : {
198 0 : ::osl::MutexGuard aGuard (maMutex);
199 0 : FactoryMap::const_iterator iFactory (maFactoryMap.find(rsURLBase));
200 0 : if (iFactory != maFactoryMap.end())
201 0 : return iFactory->second;
202 : else
203 : {
204 : // Check the URL patterns.
205 0 : FactoryPatternList::const_iterator iPattern;
206 0 : for (iPattern=maFactoryPatternList.begin();
207 0 : iPattern!=maFactoryPatternList.end();
208 : ++iPattern)
209 : {
210 0 : WildCard aWildCard (iPattern->first);
211 0 : if (aWildCard.Matches(rsURLBase))
212 0 : return iPattern->second;
213 0 : }
214 : }
215 0 : return NULL;
216 : }
217 :
218 9 : } } // end of namespace sd::framework
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|