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_FRAMEWORK_HELPER_HXX
25 #define SD_FRAMEWORK_FRAMEWORK_HELPER_HXX
26 
27 #include "ViewShell.hxx"
28 
29 #include "tools/SdGlobalResourceContainer.hxx"
30 
31 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
32 #include <com/sun/star/drawing/framework/XView.hpp>
33 #include <com/sun/star/lang/XEventListener.hpp>
34 
35 #include <boost/scoped_ptr.hpp>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/enable_shared_from_this.hpp>
38 #include <boost/function.hpp>
39 #include <map>
40 
41 namespace sd {
42 class ViewShell;
43 class ViewShellBase;
44 }
45 
46 namespace rtl {
47 class OUString;
48 }
49 
50 namespace css = ::com::sun::star;
51 namespace cssu = ::com::sun::star::uno;
52 namespace cssdf = ::com::sun::star::drawing::framework;
53 
54 namespace sd { namespace framework {
55 
56 /** The FrameworkHelper is a convenience class that simplifies the
57     access to the drawing framework.
58     It has three main tasks:
59     1. Provide frequently used strings of resource URLs and event names.
60     2. Provide shortcuts for accessing the sd framework.
61     3. Ease the migration to the drawing framwork.
62 
63     Note that a FrameworkHelper disposes itself when one of the resource
64     controllers called by it throws a DisposedException.
65 */
66 class FrameworkHelper
67     : public ::boost::enable_shared_from_this<FrameworkHelper>,
68       public SdGlobalResource
69 {
70 public:
71     // URLs of frequently used panes.
72     static const ::rtl::OUString msPaneURLPrefix;
73     static const ::rtl::OUString msCenterPaneURL;
74     static const ::rtl::OUString msFullScreenPaneURL;
75     static const ::rtl::OUString msLeftImpressPaneURL;
76     static const ::rtl::OUString msLeftDrawPaneURL;
77     static const ::rtl::OUString msSidebarPaneURL;
78 
79     // URLs of frequently used views.
80     static const ::rtl::OUString msViewURLPrefix;
81     static const ::rtl::OUString msImpressViewURL;
82     static const ::rtl::OUString msDrawViewURL;
83     static const ::rtl::OUString msOutlineViewURL;
84     static const ::rtl::OUString msNotesViewURL;
85     static const ::rtl::OUString msHandoutViewURL;
86     static const ::rtl::OUString msSlideSorterURL;
87     static const ::rtl::OUString msPresentationViewURL;
88     static const ::rtl::OUString msSidebarViewURL;
89 
90     // URLs of frequently used tool bars.
91     static const ::rtl::OUString msToolBarURLPrefix;
92     static const ::rtl::OUString msViewTabBarURL;
93 
94     // URLs of task panels.
95     static const ::rtl::OUString msTaskPanelURLPrefix;
96     static const ::rtl::OUString msAllMasterPagesTaskPanelURL;
97     static const ::rtl::OUString msRecentMasterPagesTaskPanelURL;
98     static const ::rtl::OUString msUsedMasterPagesTaskPanelURL;
99     static const ::rtl::OUString msLayoutTaskPanelURL;
100     static const ::rtl::OUString msTableDesignPanelURL;
101     static const ::rtl::OUString msCustomAnimationTaskPanelURL;
102     static const ::rtl::OUString msSlideTransitionTaskPanelURL;
103 
104     // Names of frequently used events.
105     static const ::rtl::OUString msResourceActivationRequestEvent;
106     static const ::rtl::OUString msResourceDeactivationRequestEvent;
107     static const ::rtl::OUString msResourceActivationEvent;
108     static const ::rtl::OUString msResourceDeactivationEvent;
109     static const ::rtl::OUString msResourceDeactivationEndEvent;
110     static const ::rtl::OUString msConfigurationUpdateStartEvent;
111     static const ::rtl::OUString msConfigurationUpdateEndEvent;
112 
113     // Service names of the common controllers.
114     static const ::rtl::OUString msModuleControllerService;
115     static const ::rtl::OUString msConfigurationControllerService;
116 
117     /** Return the FrameworkHelper object that is associated with the given
118         ViewShellBase.  If such an object does not yet exist, a new one is
119         created.
120     */
121     static ::boost::shared_ptr<FrameworkHelper> Instance (ViewShellBase& rBase);
122 
123     static ::boost::shared_ptr<FrameworkHelper> Instance (
124         const cssu::Reference<css::frame::XController>& rxController);
125 
126     /** Mark the FrameworkHelper object for the given ViewShellBase as
127         disposed.  A following ReleaseInstance() call will destroy the
128         FrameworkHelper object.
129 
130         Do not call this method.  It is an internally used method that can
131         not be made private.
132     */
133     static void DisposeInstance (ViewShellBase& rBase);
134 
135     /** Destroy the FrameworkHelper object for the given ViewShellBase.
136 
137         Do not call this method.  It is an internally used method that can
138         not be made private.
139     */
140     static void ReleaseInstance (ViewShellBase& rBase);
141 
142     /** Return an identifier for the given view URL.  This identifier can be
143         used in a switch statement.  See GetViewURL() for a mapping in the
144         opposite direction.
145     */
146     static ViewShell::ShellType GetViewId (const rtl::OUString& rsViewURL);
147 
148     /** Return a view URL for the given identifier.  See GetViewId() for a
149         mapping in the opposite direction.
150     */
151     static ::rtl::OUString GetViewURL (ViewShell::ShellType eType);
152 
153     /** Return a ViewShell pointer for the given XView reference.  This
154         assumes that the given reference is implemented by the
155         ViewShellWrapper class that supports the XTunnel interface.
156         @return
157             When the ViewShell pointer can not be inferred from the given
158             reference then an empty pointer is returned.
159     */
160     static ::boost::shared_ptr<ViewShell> GetViewShell (
161         const cssu::Reference<cssdf::XView>& rxView);
162 
163     typedef ::boost::function<bool(const cssdf::ConfigurationChangeEvent&)>
164         ConfigurationChangeEventFilter;
165     typedef ::boost::function<void(bool bEventSeen)> Callback;
166     typedef ::boost::function<
167         void(
168             const cssu::Reference<
169                 cssdf::XResourceId>&)
170         > ResourceFunctor;
171 
172     /** Test whether the called FrameworkHelper object is valid.
173         @return
174             When the object has already been disposed then <FALSE/> is returned.
175     */
176     bool IsValid (void);
177 
178     /** Return a pointer to the view shell that is displayed in the
179         specified pane.  See GetView() for a variant that returns a
180         reference to XView instead of a ViewShell pointer.
181         @return
182             An empty pointer is returned when for example the specified pane
183             does not exist or is not visible or does not show a view or one
184             of the involved objects does not support XUnoTunnel (where
185             necessary).
186     */
187     ::boost::shared_ptr<ViewShell> GetViewShell (const ::rtl::OUString& rsPaneURL);
188 
189     /** Return a reference to the view that is displayed in the specified
190         pane.  See GetViewShell () for a variant that returns a ViewShell
191         pointer instead of a reference to XView.
192         @param rxPaneOrViewId
193             When this ResourceId specifies a view then that view is
194             returned.  When it belongs to a pane then one view in that pane
195             is returned.
196         @return
197             An empty reference is returned when for example the specified pane
198             does not exist or is not visible or does not show a view or one
199             of the involved objects does not support XTunnel (where
200             necessary).
201     */
202     cssu::Reference<cssdf::XView> GetView (
203         const cssu::Reference<cssdf::XResourceId>& rxPaneOrViewId);
204 
205     /** Return the XWindow that is represented by the pane with the
206         given resource id.
207     */
208     cssu::Reference<css::awt::XWindow> GetPaneWindow (
209         const cssu::Reference<cssdf::XResourceId>& rxPaneId);
210 
211     /** Return the XResource object with the given resource id.
212     */
213     cssu::Reference<cssdf::XResource> GetResource (
214         const cssu::Reference<cssdf::XResourceId>& rxResourceId);
215 
216     /** Request the specified view to be displayed in the specified pane.
217         When the pane is not visible its creation is also requested.  The
218         update that creates the actual view object is done asynchronously.
219         @param rsResourceURL
220             The resource URL of the view to show.
221         @param rsAnchorURL
222             The URL of the pane in which to show the view.
223         @return
224             The resource id of the requested view is returned.  With that
225             the caller can, for example, call RunOnResourceActivation() to
226             do some initialization after the requested view becomes active.
227     */
228     cssu::Reference<cssdf::XResourceId> RequestView (
229         const ::rtl::OUString& rsResourceURL,
230         const ::rtl::OUString& rsAnchorURL);
231 
232     /** Request the deactivation of the specified resource.
233     */
234     void RequestResourceDeactivation (
235         const cssu::Reference<cssdf::XResourceId>& rxResourceId);
236 
237     /** Process a slot call that requests a view shell change.
238     */
239     void HandleModeChangeSlot (
240         sal_uLong nSlotId,
241         SfxRequest& rRequest);
242 
243     /** Run the given callback when the specified event is notified by the
244         ConfigurationManager.  When there are no pending requests and
245         therefore no events would be notified (in the foreseeable future)
246         then the callback is called immediately.
247         The callback is called with a flag that tells the callback whether
248         the event it waits for has been sent.
249     */
250     void RunOnConfigurationEvent(
251         const ::rtl::OUString& rsEventType,
252         const Callback& rCallback);
253 
254     /** Run the given callback when the specified resource has been
255         activated.  When the resource is active already when this method is
256         called then rCallback is called before this method returns.
257         @param rxResourceId
258             Wait for the activation of this resource before calling
259             rCallback.
260         @param rCallback
261             The callback to be called when the resource is activated.
262 
263     */
264     void RunOnResourceActivation(
265         const cssu::Reference<cssdf::XResourceId>& rxResourceId,
266         const Callback& rCallback);
267 
268     /** Run the given callback when the specified resource has been
269         deactivated.  When the resource is not active already when
270         this method is called then rCallback is called before this
271         method returns.
272         @param rxResourceId
273             Wait for the deactivation of this resource before calling
274             rCallback.
275         @param rCallback
276             The callback to be called when the resource is
277             deactivated.
278         @param bRunOnDeactivationEnd
279             The callback is run either when the deactivation starts
280             and the callback can still access the resource or when the
281             deactivatio is complete and the resource is no longer available.
282 
283     */
284     void RunOnResourceDeactivation(
285         const cssu::Reference<cssdf::XResourceId>& rxResourceId,
286         const Callback& rCallback,
287         const bool bRunOnDeactivationEnd);
288 
289     /** Normally the requested changes of the configuration are executed
290         asynchronously.  However, there is at least one situation (searching
291         with the Outliner) where the surrounding code does not cope with
292         this.  So, instead of calling Reschedule until the global event loop
293         executes the configuration update, this method does (almost) the
294         same without the reschedules.
295 
296         Do not use this method until there is absolutely no other way.
297     */
298     void RequestSynchronousUpdate (void);
299 
300     /** Block until the specified event is notified by the configuration
301         controller.  When the configuration controller is not processing any
302         requests the method returns immediately.
303     */
304     void WaitForEvent (const ::rtl::OUString& rsEventName) const;
305 
306     /** This is a short cut for WaitForEvent(msConfigurationUpdateEndEvent).
307         Call this method to execute the pending requests.
308     */
309     void WaitForUpdate (void) const;
310 
311     /** Explicit request for an update of the current configuration.  Call
312         this method when one of the resources managed by the sd framework
313         has been activated or deactivated from the outside, i.e. not by the
314         framework itself.  An example for this is a click on the closer
315         button of one of the side panes.
316     */
317     void UpdateConfiguration (void);
318 
319     /** Return a string representation of the given XResourceId object.
320     */
321     static ::rtl::OUString ResourceIdToString (
322         const cssu::Reference<
323             cssdf::XResourceId>& rxResourceId);
324 
325     /** Create a new XResourceId object for the given resource URL.
326     */
327     static cssu::Reference<
328         cssdf::XResourceId>
329             CreateResourceId (
330                 const ::rtl::OUString& rsResourceURL);
331 
332     /** Create a new XResourceId object for the given resource URL and a
333         single anchor URL.
334     */
335     static cssu::Reference<
336         cssdf::XResourceId>
337             CreateResourceId (
338                 const ::rtl::OUString& rsResourceURL,
339                 const ::rtl::OUString& rsAnchorURL);
340 
341     /** Create a new XResourceId object for the given resource URL and the
342         two given anchor URLs.
343     */
344     static cssu::Reference<
345         cssdf::XResourceId>
346             CreateResourceId (
347                 const ::rtl::OUString& rsResourceURL,
348                 const ::rtl::OUString& rsFirstAnchorURL,
349                 const ::rtl::OUString& rsSecondAnchorURL);
350 
351     /** Create a new XResourceId object for the given resource URL.
352     */
353     static cssu::Reference<
354         cssdf::XResourceId>
355             CreateResourceId (
356                 const ::rtl::OUString& rsResourceURL,
357                 const cssu::Reference<
358                     cssdf::XResourceId>& rxAnchor);
359 
360     cssu::Reference<cssdf::XConfigurationController>
361         GetConfigurationController (void) const;
362 
363 
364 private:
365     typedef ::std::map<
366         ViewShellBase*,
367         ::boost::shared_ptr<FrameworkHelper> > InstanceMap;
368     /** The instance map holds (at least) one FrameworkHelper instance for
369         every ViewShellBase object.
370     */
371     static InstanceMap maInstanceMap;
372     class ViewURLMap;
373     static ::boost::scoped_ptr<ViewURLMap> mpViewURLMap;
374 
375     ViewShellBase& mrBase;
376     cssu::Reference<cssdf::XConfigurationController>
377         mxConfigurationController;
378 
379     class DisposeListener;
380     friend class DisposeListener;
381     cssu::Reference<css::lang::XComponent>
382         mxDisposeListener;
383 
384     FrameworkHelper (ViewShellBase& rBase);
385     FrameworkHelper (const FrameworkHelper& rHelper); // Not implemented.
386     ~FrameworkHelper (void);
387     class Deleter; friend class Deleter;
388     FrameworkHelper& operator= (const FrameworkHelper& rHelper); // Not implemented.
389 
390     void Initialize (void);
391 
392     void Dispose (void);
393 
394     /** Run the given callback when an event of the specified type is
395         received from the ConfigurationController or when the
396         ConfigurationController has no pending change requests.
397         @param rsEventType
398             Run rCallback only on this event.
399         @param rFilter
400             This filter has to return <TRUE/> in order for rCallback to be
401             called.
402         @param rCallback
403             The callback functor to be called.
404     */
405     void RunOnEvent(
406         const ::rtl::OUString& rsEventType,
407         const ConfigurationChangeEventFilter& rFilter,
408         const Callback& rCallback) const;
409 
410     /** This disposing method is forwarded from the inner DisposeListener class.
411     */
412     void disposing (const css::lang::EventObject& rEventObject);
413 };
414 
415 
416 } } // end of namespace sd::framework
417 
418 
419 
420 namespace sd { namespace framework {
421 
422 namespace {
423 
424     class FrameworkHelperAllPassFilter
425     {
426     public:
operator ()(const cssdf::ConfigurationChangeEvent &)427         bool operator() (const cssdf::ConfigurationChangeEvent&) { return true; }
428     };
429 
430 
431     class FrameworkHelperResourceIdFilter
432     {
433     public:
434         FrameworkHelperResourceIdFilter (
435             const cssu::Reference<cssdf::XResourceId>& rxResourceId);
operator ()(const cssdf::ConfigurationChangeEvent & rEvent)436         bool operator() (const cssdf::ConfigurationChangeEvent& rEvent)
437         { return mxResourceId.is() && rEvent.ResourceId.is()
438                 && mxResourceId->compareTo(rEvent.ResourceId) == 0; }
439     private:
440         cssu::Reference<cssdf::XResourceId> mxResourceId;
441     };
442 
443 } // end of anonymous namespace
444 
445 
446 
447 
448 } } // end of namespace sd::framework
449 
450 
451 #endif
452