1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SD_FRAMEWORK_RESOURCE_MANAGER_HXX
25 #define SD_FRAMEWORK_RESOURCE_MANAGER_HXX
26 
27 #include <com/sun/star/drawing/framework/XConfiguration.hpp>
28 #include <com/sun/star/drawing/framework/XResource.hpp>
29 #include <com/sun/star/drawing/framework/XResourceFactory.hpp>
30 #include <boost/noncopyable.hpp>
31 #include <boost/shared_ptr.hpp>
32 #include <map>
33 #include <vector>
34 
35 namespace css = ::com::sun::star;
36 
37 namespace sd { namespace framework {
38 
39 class ConfigurationControllerBroadcaster;
40 class ResourceFactoryManager;
41 
42 /** Manage the set of active resources.  Activate and deactivate resources.
43 */
44 class ConfigurationControllerResourceManager
45     : ::boost::noncopyable
46 {
47 public:
48     /** For every active resource both the resource itself as well as its
49         creating factory are remembered, so that on deactivation, the
50         resource can be deactivated by this factory.
51     */
52     class ResourceDescriptor
53     {
54     public:
55         css::uno::Reference<css::drawing::framework::XResource> mxResource;
56         css::uno::Reference<css::drawing::framework::XResourceFactory> mxResourceFactory;
57     };
58 
59     /** A new ResourceManager object is created with the resource factory
60         container for creating resources and the event broadcaster for
61         notifying ConfigurationChangeListeners of activated or deactivated
62         resources.
63     */
64     ConfigurationControllerResourceManager (
65         const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
66         const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster);
67 
68     ~ConfigurationControllerResourceManager (void);
69 
70     /** Activate all the resources that are specified by resource ids in
71         rResources.  The resource ids of activated resources are added to
72         the given configuration.  Activated resources are notified to all
73         interested ConfigurationChangeListeners.
74     */
75     void ActivateResources (
76         const ::std::vector<
77             css::uno::Reference<css::drawing::framework::XResourceId> >& rResources,
78         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration);
79 
80     /** Deactivate all the resources that are specified by resource ids in
81         rResources.  The resource ids of deactivated resources are removed
82         from the given configuration.  Activated resources are notified to all
83         interested ConfigurationChangeListeners.
84     */
85     void DeactivateResources (
86         const ::std::vector<
87             css::uno::Reference<css::drawing::framework::XResourceId> >& rResources,
88         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration);
89 
90     /** Return the descriptor for the specified resource.
91         @return
92             When there is no active resource for the given resource id then
93             an empty descriptor is returned.
94     */
95     ResourceDescriptor GetResource (
96         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId);
97 
98 private:
99     osl::Mutex maMutex;
100 
101     class ResourceComparator
102     {
103     public:
104         bool operator() (
105             const css::uno::Reference<css::drawing::framework::XResourceId>& rxId1,
106             const css::uno::Reference<css::drawing::framework::XResourceId>& rxId2) const;
107     };
108 
109     typedef ::std::map<
110         css::uno::Reference<css::drawing::framework::XResourceId>,
111         ResourceDescriptor,
112         ResourceComparator> ResourceMap;
113     ResourceMap maResourceMap;
114 
115     ::boost::shared_ptr<ResourceFactoryManager> mpResourceFactoryContainer;
116 
117     /** This broadcaster is used to notify the activation and deactivation
118         of resources.
119     */
120     ::boost::shared_ptr<ConfigurationControllerBroadcaster> mpBroadcaster;
121 
122     void ActivateResource (
123         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
124         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration);
125 
126     void DeactivateResource (
127         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
128         const css::uno::Reference<css::drawing::framework::XConfiguration>& rxConfiguration);
129 
130     void AddResource (
131         const css::uno::Reference<css::drawing::framework::XResource>& rxResource,
132         const css::uno::Reference<css::drawing::framework::XResourceFactory>& rxFactory);
133 
134     ResourceDescriptor RemoveResource (
135         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId);
136 };
137 
138 
139 } } // end of namespace sd::framework
140 
141 #endif
142