LibreOffice Module sd (master) 1
ResourceFactoryManager.cxx
Go to the documentation of this file.
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
21#include <DrawController.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/util/URLTransformer.hpp>
26#include <com/sun/star/drawing/framework/XControllerManager.hpp>
28#include <sal/log.hxx>
29
30#include <algorithm>
31
32using namespace ::com::sun::star;
33using namespace ::com::sun::star::uno;
35
36#undef VERBOSE
37//#define VERBOSE 1
38
39namespace sd::framework {
40
42 : mxControllerManager(rxManager)
43{
44 // Create the URL transformer.
45 Reference<uno::XComponentContext> xContext(::comphelper::getProcessComponentContext());
46 mxURLTransformer = util::URLTransformer::create(xContext);
47}
48
50{
51 for (auto& rXInterfaceResource : maFactoryMap)
52 {
53 Reference<lang::XComponent> xComponent (rXInterfaceResource.second, UNO_QUERY);
54 rXInterfaceResource.second = nullptr;
55 if (xComponent.is())
56 xComponent->dispose();
57 }
58
59 Reference<lang::XComponent> xComponent (mxURLTransformer, UNO_QUERY);
60 if (xComponent.is())
61 xComponent->dispose();
62}
63
65 const OUString& rsURL,
66 const Reference<XResourceFactory>& rxFactory)
67{
68 if ( ! rxFactory.is())
69 throw lang::IllegalArgumentException();
70 if (rsURL.isEmpty())
71 throw lang::IllegalArgumentException();
72
73 std::scoped_lock aGuard (maMutex);
74
75 if (rsURL.indexOf('*') >= 0 || rsURL.indexOf('?') >= 0)
76 {
77 // The URL is a URL pattern not a single URL.
78 maFactoryPatternList.emplace_back(rsURL, rxFactory);
79
80#if defined VERBOSE && VERBOSE>=1
81 SAL_INFO("sd","ResourceFactoryManager::AddFactory pattern " << rsURL << std::hex << rxFactory.get());
82#endif
83 }
84 else
85 {
86 maFactoryMap[rsURL] = rxFactory;
87
88#if defined VERBOSE && VERBOSE>=1
89 SAL_INFO("sd", "ResourceFactoryManager::AddFactory fixed " << rsURL << " 0x" << std::hex << rxFactory.get());
90#endif
91 }
92}
93
95 const OUString& rsURL)
96{
97 if (rsURL.isEmpty())
98 throw lang::IllegalArgumentException();
99
100 std::scoped_lock aGuard (maMutex);
101
102 FactoryMap::iterator iFactory (maFactoryMap.find(rsURL));
103 if (iFactory != maFactoryMap.end())
104 {
105 maFactoryMap.erase(iFactory);
106 }
107 else
108 {
109 // The URL may be a pattern. Look that up.
110 auto iPattern = std::find_if(maFactoryPatternList.begin(), maFactoryPatternList.end(),
111 [&rsURL](const FactoryPatternList::value_type& rPattern) { return rPattern.first == rsURL; });
112 if (iPattern != maFactoryPatternList.end())
113 {
114 // Found the pattern. Remove it.
115 maFactoryPatternList.erase(iPattern);
116 }
117 }
118}
119
121 const Reference<XResourceFactory>& rxFactory)
122{
123 std::scoped_lock aGuard (maMutex);
124
125 // Collect a list with all keys that map to the given factory.
126 ::std::vector<OUString> aKeys;
127 for (const auto& rFactory : maFactoryMap)
128 if (rFactory.second == rxFactory)
129 aKeys.push_back(rFactory.first);
130
131 // Remove the entries whose keys we just have collected.
132 for (const auto& rKey : aKeys)
133 maFactoryMap.erase(rKey);
134
135 // Remove the pattern entries whose factories are identical to the given
136 // factory.
138 std::remove_if(
139 maFactoryPatternList.begin(),
141 [&] (FactoryPatternList::value_type const& it) { return it.second == rxFactory; }),
143}
144
145Reference<XResourceFactory> ResourceFactoryManager::GetFactory (
146 const OUString& rsCompleteURL)
147{
148 OUString sURLBase (rsCompleteURL);
149 if (mxURLTransformer.is())
150 {
151 util::URL aURL;
152 aURL.Complete = rsCompleteURL;
153 if (mxURLTransformer->parseStrict(aURL))
154 sURLBase = aURL.Main;
155 }
156
157 Reference<XResourceFactory> xFactory = FindFactory(sURLBase);
158
159 if ( ! xFactory.is() && mxControllerManager.is())
160 {
161 Reference<XModuleController> xModuleController(mxControllerManager->getModuleController());
162 if (xModuleController.is())
163 {
164 // Ask the module controller to provide a factory of the
165 // requested view type. Note that this can (and should) cause
166 // intermediate calls to AddFactory().
167 xModuleController->requestResource(sURLBase);
168
169 xFactory = FindFactory(sURLBase);
170 }
171 }
172
173 return xFactory;
174}
175
176Reference<XResourceFactory> ResourceFactoryManager::FindFactory (const OUString& rsURLBase)
177{
178 std::scoped_lock aGuard (maMutex);
179 FactoryMap::const_iterator iFactory (maFactoryMap.find(rsURLBase));
180 if (iFactory != maFactoryMap.end())
181 return iFactory->second;
182 else
183 {
184 // Check the URL patterns.
185 auto iPattern = std::find_if(maFactoryPatternList.begin(), maFactoryPatternList.end(),
186 [&rsURLBase](const FactoryPatternList::value_type& rPattern) {
187 WildCard aWildCard (rPattern.first);
188 return aWildCard.Matches(rsURLBase);
189 });
190 if (iPattern != maFactoryPatternList.end())
191 return iPattern->second;
192 }
193 return nullptr;
194}
195
196} // end of namespace sd::framework
197
198/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rtl::Reference<::sd::DrawController > mxControllerManager
void RemoveFactoryForReference(const css::uno::Reference< css::drawing::framework::XResourceFactory > &rxFactory)
Unregister the specified factory.
css::uno::Reference< css::drawing::framework::XResourceFactory > FindFactory(const OUString &rsURLBase)
Look up the factory for the given URL.
css::uno::Reference< css::util::XURLTransformer > mxURLTransformer
void AddFactory(const OUString &rsURL, const css::uno::Reference< css::drawing::framework::XResourceFactory > &rxFactory)
Register a resource factory for one type of resource.
ResourceFactoryManager(const rtl::Reference<::sd::DrawController > &rxManager)
css::uno::Reference< css::drawing::framework::XResourceFactory > GetFactory(const OUString &rsURL)
Return a factory that can create resources specified by the given URL.
void RemoveFactoryForURL(const OUString &rsURL)
Unregister the specified factory.
URL aURL
Reference< XSingleServiceFactory > xFactory
#define SAL_INFO(area, stream)