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 #include "precompiled_sd.hxx"
25
26 #include "ResourceManager.hxx"
27
28 #include "framework/FrameworkHelper.hxx"
29 #include "framework/ConfigurationController.hxx"
30 #include <com/sun/star/drawing/framework/XControllerManager.hpp>
31
32 #include <comphelper/stl_types.hxx>
33 #include <set>
34
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::drawing::framework;
38
39 using ::rtl::OUString;
40 using ::sd::framework::FrameworkHelper;
41
42 namespace {
43 static const sal_Int32 ResourceActivationRequestEvent = 0;
44 static const sal_Int32 ResourceDeactivationRequestEvent = 1;
45 }
46
47
48
49
50 namespace sd { namespace framework {
51
52 class ResourceManager::MainViewContainer
53 : public ::std::set<OUString, ::comphelper::UStringLess>
54 {
55 public:
MainViewContainer(void)56 MainViewContainer (void) {}
57 };
58
59
60
61
62 //===== ResourceManager =======================================================
63
ResourceManager(const Reference<frame::XController> & rxController,const Reference<XResourceId> & rxResourceId)64 ResourceManager::ResourceManager (
65 const Reference<frame::XController>& rxController,
66 const Reference<XResourceId>& rxResourceId)
67 : ResourceManagerInterfaceBase(MutexOwner::maMutex),
68 mxConfigurationController(),
69 mpActiveMainViewContainer(new MainViewContainer()),
70 mxResourceId(rxResourceId),
71 mxMainViewAnchorId(FrameworkHelper::Instance(rxController)->CreateResourceId(
72 FrameworkHelper::msCenterPaneURL)),
73 msCurrentMainViewURL(),
74 mbIsEnabled(true)
75 {
76 Reference<XControllerManager> xControllerManager (rxController, UNO_QUERY);
77 if (xControllerManager.is())
78 {
79 mxConfigurationController = xControllerManager->getConfigurationController();
80
81 if (mxConfigurationController.is())
82 {
83 mxConfigurationController->addConfigurationChangeListener(
84 this,
85 FrameworkHelper::msResourceActivationRequestEvent,
86 makeAny(ResourceActivationRequestEvent));
87 mxConfigurationController->addConfigurationChangeListener(
88 this,
89 FrameworkHelper::msResourceDeactivationRequestEvent,
90 makeAny(ResourceDeactivationRequestEvent));
91 }
92 }
93 }
94
95
96
97
~ResourceManager(void)98 ResourceManager::~ResourceManager (void)
99 {
100 }
101
102
103
104
AddActiveMainView(const OUString & rsMainViewURL)105 void ResourceManager::AddActiveMainView (
106 const OUString& rsMainViewURL)
107 {
108 mpActiveMainViewContainer->insert(rsMainViewURL);
109 }
110
111
112
113
disposing(void)114 void SAL_CALL ResourceManager::disposing (void)
115 {
116 if (mxConfigurationController.is())
117 {
118 mxConfigurationController->removeConfigurationChangeListener(this);
119 mxConfigurationController = NULL;
120 }
121 }
122
123
124
125
Enable(void)126 void ResourceManager::Enable (void)
127 {
128 mbIsEnabled = true;
129 UpdateForMainViewShell();
130 }
131
132
133
134
Disable(void)135 void ResourceManager::Disable (void)
136 {
137 mbIsEnabled = false;
138 UpdateForMainViewShell();
139 }
140
141
142
143
notifyConfigurationChange(const ConfigurationChangeEvent & rEvent)144 void SAL_CALL ResourceManager::notifyConfigurationChange (
145 const ConfigurationChangeEvent& rEvent)
146 {
147 OSL_ASSERT(rEvent.ResourceId.is());
148
149 sal_Int32 nEventType = 0;
150 rEvent.UserData >>= nEventType;
151 switch (nEventType)
152 {
153 case ResourceActivationRequestEvent:
154 if (rEvent.ResourceId->isBoundToURL(
155 FrameworkHelper::msCenterPaneURL,
156 AnchorBindingMode_DIRECT))
157 {
158 // A resource directly bound to the center pane has been
159 // requested.
160 if (rEvent.ResourceId->getResourceTypePrefix().equals(
161 FrameworkHelper::msViewURLPrefix))
162 {
163 // The requested resource is a view. Show or hide the
164 // resource managed by this ResourceManager accordingly.
165 HandleMainViewSwitch(
166 rEvent.ResourceId->getResourceURL(),
167 rEvent.Configuration,
168 true);
169 }
170 }
171 else if (rEvent.ResourceId->compareTo(mxResourceId) == 0)
172 {
173 // The resource managed by this ResourceManager has been
174 // explicitly been requested (maybe by us). Remember this
175 // setting.
176 HandleResourceRequest(true, rEvent.Configuration);
177 }
178 break;
179
180 case ResourceDeactivationRequestEvent:
181 if (rEvent.ResourceId->compareTo(mxMainViewAnchorId) == 0)
182 {
183 HandleMainViewSwitch(
184 OUString(),
185 rEvent.Configuration,
186 false);
187 }
188 else if (rEvent.ResourceId->compareTo(mxResourceId) == 0)
189 {
190 // The resource managed by this ResourceManager has been
191 // explicitly been requested to be hidden (maybe by us).
192 // Remember this setting.
193 HandleResourceRequest(false, rEvent.Configuration);
194 }
195 break;
196 }
197 }
198
199
200
201
UpdateForMainViewShell(void)202 void ResourceManager::UpdateForMainViewShell (void)
203 {
204 if (mxConfigurationController.is())
205 {
206 ConfigurationController::Lock aLock (mxConfigurationController);
207
208 if (mbIsEnabled
209 && mpActiveMainViewContainer->find(msCurrentMainViewURL)
210 != mpActiveMainViewContainer->end())
211 {
212 // Activate resource.
213 mxConfigurationController->requestResourceActivation(
214 mxResourceId->getAnchor(),
215 ResourceActivationMode_ADD);
216 mxConfigurationController->requestResourceActivation(
217 mxResourceId,
218 ResourceActivationMode_REPLACE);
219 }
220 else
221 {
222 mxConfigurationController->requestResourceDeactivation(mxResourceId);
223 }
224 }
225 }
226
227
228
229
HandleMainViewSwitch(const OUString & rsViewURL,const Reference<XConfiguration> & rxConfiguration,const bool bIsActivated)230 void ResourceManager::HandleMainViewSwitch (
231 const OUString& rsViewURL,
232 const Reference<XConfiguration>& rxConfiguration,
233 const bool bIsActivated)
234 {
235 (void)rxConfiguration;
236 if (bIsActivated)
237 msCurrentMainViewURL = rsViewURL;
238 else
239 msCurrentMainViewURL = OUString();
240 UpdateForMainViewShell();
241 }
242
243
244
245
HandleResourceRequest(bool bActivation,const Reference<XConfiguration> & rxConfiguration)246 void ResourceManager::HandleResourceRequest(
247 bool bActivation,
248 const Reference<XConfiguration>& rxConfiguration)
249 {
250 if (mbIsEnabled)
251 {
252 Sequence<Reference<XResourceId> > aCenterViews = rxConfiguration->getResources(
253 FrameworkHelper::CreateResourceId(FrameworkHelper::msCenterPaneURL),
254 FrameworkHelper::msViewURLPrefix,
255 AnchorBindingMode_DIRECT);
256 if (aCenterViews.getLength() == 1)
257 {
258 if (bActivation)
259 {
260 mpActiveMainViewContainer->insert(aCenterViews[0]->getResourceURL());
261 }
262 else
263 {
264 MainViewContainer::iterator iElement (
265 mpActiveMainViewContainer->find(aCenterViews[0]->getResourceURL()));
266 if (iElement != mpActiveMainViewContainer->end())
267 mpActiveMainViewContainer->erase(iElement);
268 }
269 }
270 }
271 }
272
273
274
275
disposing(const lang::EventObject & rEvent)276 void SAL_CALL ResourceManager::disposing (
277 const lang::EventObject& rEvent)
278 {
279 if (mxConfigurationController.is()
280 && rEvent.Source == mxConfigurationController)
281 {
282 // Without the configuration controller this class can do nothing.
283 mxConfigurationController = NULL;
284 dispose();
285 }
286 }
287
288 } } // end of namespace sd::framework
289