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