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 #pragma once
21 
22 #include <sal/config.h>
23 
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <rtl/ustring.hxx>
30 #include <osl/mutex.hxx>
31 
32 namespace com::sun::star::drawing::framework { class XControllerManager; }
33 namespace com::sun::star::drawing::framework { class XResourceFactory; }
34 namespace com::sun::star::util { class XURLTransformer; }
35 
36 namespace sd::framework {
37 
38 /** Container of resource factories of the drawing framework.
39 */
40 class ResourceFactoryManager
41 {
42 public:
43     explicit ResourceFactoryManager (
44         const css::uno::Reference<css::drawing::framework::XControllerManager>& rxManager);
45 
46     ~ResourceFactoryManager();
47 
48     /** Register a resource factory for one type of resource.
49         @param rsURL
50             The type of the resource that will be created by the factory.
51         @param rxFactory
52             The factory that will create resource objects of the specified type.
53         @throws css::uno::RuntimeException
54     */
55     void AddFactory (
56         const OUString& rsURL,
57         const css::uno::Reference<css::drawing::framework::XResourceFactory>& rxFactory);
58 
59     /** Unregister the specified factory.
60         @param rsURL
61             Unregister only the factory for this URL.  When the same factory
62             is registered for other URLs then these remain registered.
63         @throws css::uno::RuntimeException
64     */
65     void RemoveFactoryForURL(
66         const OUString& rsURL);
67 
68     /** Unregister the specified factory.
69         @param rxFactory
70             Unregister the this factory for all URLs that it has been
71             registered for.
72         @throws css::uno::RuntimeException
73     */
74     void RemoveFactoryForReference(
75         const css::uno::Reference<css::drawing::framework::XResourceFactory>& rxFactory);
76 
77     /** Return a factory that can create resources specified by the given URL.
78         @param rsCompleteURL
79             This URL specifies the type of the resource.  It may contain arguments.
80         @return
81             When a factory for the specified URL has been registered by a
82             previous call to AddFactory() then a reference to that factory
83             is returned.  Otherwise an empty reference is returned.
84         @throws css::uno::RuntimeException
85     */
86     css::uno::Reference<css::drawing::framework::XResourceFactory> GetFactory (
87         const OUString& rsURL);
88 
89 private:
90     ::osl::Mutex maMutex;
91     typedef std::unordered_map<
92         OUString,
93         css::uno::Reference<css::drawing::framework::XResourceFactory> > FactoryMap;
94     FactoryMap maFactoryMap;
95 
96     typedef ::std::vector<
97         ::std::pair<
98             OUString,
99             css::uno::Reference<css::drawing::framework::XResourceFactory> > >
100         FactoryPatternList;
101     FactoryPatternList maFactoryPatternList;
102 
103     css::uno::Reference<css::drawing::framework::XControllerManager> mxControllerManager;
104     css::uno::Reference<css::util::XURLTransformer> mxURLTransformer;
105 
106     /** Look up the factory for the given URL.
107         @param rsURLBase
108             The css::tools::URL.Main part of a URL. Arguments have to be
109             stripped off by the caller.
110         @return
111             When the factory has not yet been added then return NULL.
112         @throws css::uno::RuntimeException
113     */
114     css::uno::Reference<css::drawing::framework::XResourceFactory> FindFactory (
115         const OUString& rsURLBase);
116 };
117 
118 } // end of namespace sd::framework
119 
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
121