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 : #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>
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::drawing::framework;
33 :
34 : #undef VERBOSE
35 : //#define VERBOSE 1
36 :
37 : namespace sd { namespace framework {
38 :
39 212 : ResourceFactoryManager::ResourceFactoryManager (const Reference<XControllerManager>& rxManager)
40 : : maMutex(),
41 : maFactoryMap(),
42 : maFactoryPatternList(),
43 : mxControllerManager(rxManager),
44 212 : mxURLTransformer()
45 : {
46 : // Create the URL transformer.
47 212 : Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
48 212 : mxURLTransformer = util::URLTransformer::create(xContext);
49 212 : }
50 :
51 424 : ResourceFactoryManager::~ResourceFactoryManager (void)
52 : {
53 212 : Reference<lang::XComponent> xComponent (mxURLTransformer, UNO_QUERY);
54 212 : if (xComponent.is())
55 0 : xComponent->dispose();
56 212 : }
57 :
58 2596 : void ResourceFactoryManager::AddFactory (
59 : const OUString& rsURL,
60 : const Reference<XResourceFactory>& rxFactory)
61 : throw (RuntimeException)
62 : {
63 2596 : if ( ! rxFactory.is())
64 0 : throw lang::IllegalArgumentException();
65 2596 : if (rsURL.isEmpty())
66 0 : throw lang::IllegalArgumentException();
67 :
68 2596 : ::osl::MutexGuard aGuard (maMutex);
69 :
70 2596 : if (rsURL.indexOf('*') >= 0 || rsURL.indexOf('?') >= 0)
71 : {
72 : // The URL is a URL pattern not an single URL.
73 0 : maFactoryPatternList.push_back(FactoryPatternList::value_type(rsURL, rxFactory));
74 :
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
80 : }
81 : else
82 : {
83 2596 : maFactoryMap[rsURL] = rxFactory;
84 :
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
90 2596 : }
91 2596 : }
92 :
93 0 : void ResourceFactoryManager::RemoveFactoryForURL (
94 : const OUString& rsURL)
95 : throw (RuntimeException)
96 : {
97 0 : if (rsURL.isEmpty())
98 0 : throw lang::IllegalArgumentException();
99 :
100 0 : ::osl::MutexGuard aGuard (maMutex);
101 :
102 0 : FactoryMap::iterator iFactory (maFactoryMap.find(rsURL));
103 0 : if (iFactory != maFactoryMap.end())
104 : {
105 0 : maFactoryMap.erase(iFactory);
106 : }
107 : else
108 : {
109 : // The URL may be a pattern. Look that up.
110 0 : FactoryPatternList::iterator iPattern;
111 0 : for (iPattern=maFactoryPatternList.begin();
112 0 : iPattern!=maFactoryPatternList.end();
113 : ++iPattern)
114 : {
115 0 : if (iPattern->first == rsURL)
116 : {
117 : // Found the pattern. Remove it.
118 0 : maFactoryPatternList.erase(iPattern);
119 0 : break;
120 : }
121 : }
122 0 : }
123 0 : }
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 1372 : Reference<XResourceFactory> ResourceFactoryManager::GetFactory (
158 : const OUString& rsCompleteURL)
159 : throw (RuntimeException)
160 : {
161 1372 : OUString sURLBase (rsCompleteURL);
162 1372 : if (mxURLTransformer.is())
163 : {
164 1372 : util::URL aURL;
165 1372 : aURL.Complete = rsCompleteURL;
166 1372 : if (mxURLTransformer->parseStrict(aURL))
167 1372 : sURLBase = aURL.Main;
168 : }
169 :
170 1372 : Reference<XResourceFactory> xFactory = FindFactory(sURLBase);
171 :
172 1372 : if ( ! xFactory.is() && mxControllerManager.is())
173 : {
174 696 : Reference<XModuleController> xModuleController(mxControllerManager->getModuleController());
175 696 : if (xModuleController.is())
176 : {
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 696 : xModuleController->requestResource(sURLBase);
181 :
182 696 : xFactory = FindFactory(sURLBase);
183 696 : }
184 : }
185 :
186 1372 : return xFactory;
187 : }
188 :
189 2068 : Reference<XResourceFactory> ResourceFactoryManager::FindFactory (const OUString& rsURLBase)
190 : throw (RuntimeException)
191 : {
192 2068 : ::osl::MutexGuard aGuard (maMutex);
193 2068 : FactoryMap::const_iterator iFactory (maFactoryMap.find(rsURLBase));
194 2068 : if (iFactory != maFactoryMap.end())
195 1152 : return iFactory->second;
196 : else
197 : {
198 : // Check the URL patterns.
199 916 : FactoryPatternList::const_iterator iPattern;
200 2748 : for (iPattern=maFactoryPatternList.begin();
201 1832 : iPattern!=maFactoryPatternList.end();
202 : ++iPattern)
203 : {
204 0 : WildCard aWildCard (iPattern->first);
205 0 : if (aWildCard.Matches(rsURLBase))
206 0 : return iPattern->second;
207 0 : }
208 : }
209 916 : return NULL;
210 : }
211 :
212 114 : } } // end of namespace sd::framework
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|