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