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     throw (RuntimeException)
147 {
148     OSL_ASSERT(rEvent.ResourceId.is());
149 
150     sal_Int32 nEventType = 0;
151     rEvent.UserData >>= nEventType;
152     switch (nEventType)
153     {
154         case ResourceActivationRequestEvent:
155             if (rEvent.ResourceId->isBoundToURL(
156                 FrameworkHelper::msCenterPaneURL,
157                 AnchorBindingMode_DIRECT))
158             {
159                 // A resource directly bound to the center pane has been
160                 // requested.
161                 if (rEvent.ResourceId->getResourceTypePrefix().equals(
162                     FrameworkHelper::msViewURLPrefix))
163                 {
164                     // The requested resource is a view.  Show or hide the
165                     // resource managed by this ResourceManager accordingly.
166                     HandleMainViewSwitch(
167                         rEvent.ResourceId->getResourceURL(),
168                         rEvent.Configuration,
169                         true);
170                 }
171             }
172             else if (rEvent.ResourceId->compareTo(mxResourceId) == 0)
173             {
174                 // The resource managed by this ResourceManager has been
175                 // explicitly been requested (maybe by us).  Remember this
176                 // setting.
177                 HandleResourceRequest(true, rEvent.Configuration);
178             }
179             break;
180 
181         case ResourceDeactivationRequestEvent:
182             if (rEvent.ResourceId->compareTo(mxMainViewAnchorId) == 0)
183             {
184                 HandleMainViewSwitch(
185                     OUString(),
186                     rEvent.Configuration,
187                     false);
188             }
189             else if (rEvent.ResourceId->compareTo(mxResourceId) == 0)
190             {
191                 // The resource managed by this ResourceManager has been
192                 // explicitly been requested to be hidden (maybe by us).
193                 // Remember this setting.
194                 HandleResourceRequest(false, rEvent.Configuration);
195             }
196             break;
197     }
198 }
199 
200 
201 
202 
UpdateForMainViewShell(void)203 void ResourceManager::UpdateForMainViewShell (void)
204 {
205     if (mxConfigurationController.is())
206     {
207         ConfigurationController::Lock aLock (mxConfigurationController);
208 
209         if (mbIsEnabled
210             && mpActiveMainViewContainer->find(msCurrentMainViewURL)
211                != mpActiveMainViewContainer->end())
212         {
213             // Activate resource.
214             mxConfigurationController->requestResourceActivation(
215                 mxResourceId->getAnchor(),
216                 ResourceActivationMode_ADD);
217             mxConfigurationController->requestResourceActivation(
218                 mxResourceId,
219                 ResourceActivationMode_REPLACE);
220         }
221         else
222         {
223             mxConfigurationController->requestResourceDeactivation(mxResourceId);
224         }
225     }
226 }
227 
228 
229 
230 
HandleMainViewSwitch(const OUString & rsViewURL,const Reference<XConfiguration> & rxConfiguration,const bool bIsActivated)231 void ResourceManager::HandleMainViewSwitch (
232     const OUString& rsViewURL,
233     const Reference<XConfiguration>& rxConfiguration,
234     const bool bIsActivated)
235 {
236     (void)rxConfiguration;
237     if (bIsActivated)
238         msCurrentMainViewURL = rsViewURL;
239     else
240         msCurrentMainViewURL = OUString();
241     UpdateForMainViewShell();
242 }
243 
244 
245 
246 
HandleResourceRequest(bool bActivation,const Reference<XConfiguration> & rxConfiguration)247 void ResourceManager::HandleResourceRequest(
248     bool bActivation,
249     const Reference<XConfiguration>& rxConfiguration)
250 {
251     if (mbIsEnabled)
252     {
253         Sequence<Reference<XResourceId> > aCenterViews = rxConfiguration->getResources(
254             FrameworkHelper::CreateResourceId(FrameworkHelper::msCenterPaneURL),
255             FrameworkHelper::msViewURLPrefix,
256             AnchorBindingMode_DIRECT);
257         if (aCenterViews.getLength() == 1)
258         {
259             if (bActivation)
260             {
261                 mpActiveMainViewContainer->insert(aCenterViews[0]->getResourceURL());
262             }
263             else
264             {
265                 MainViewContainer::iterator iElement (
266                     mpActiveMainViewContainer->find(aCenterViews[0]->getResourceURL()));
267                 if (iElement != mpActiveMainViewContainer->end())
268                     mpActiveMainViewContainer->erase(iElement);
269             }
270         }
271     }
272 }
273 
274 
275 
276 
disposing(const lang::EventObject & rEvent)277 void SAL_CALL ResourceManager::disposing (
278     const lang::EventObject& rEvent)
279     throw (RuntimeException)
280 {
281     if (mxConfigurationController.is()
282         && rEvent.Source == mxConfigurationController)
283     {
284         // Without the configuration controller this class can do nothing.
285         mxConfigurationController = NULL;
286         dispose();
287     }
288 }
289 
290 } } // end of namespace sd::framework
291