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