1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #pragma once
21 
22 #include <ViewShell.hxx>
23 
24 #include <tools/SdGlobalResourceContainer.hxx>
25 
26 #include <functional>
27 #include <map>
28 #include <memory>
29 
30 namespace com::sun::star::drawing::framework { class XConfigurationController; }
31 namespace com::sun::star::drawing::framework { class XResourceId; }
32 namespace com::sun::star::drawing::framework { class XView; }
33 namespace com::sun::star::drawing::framework { struct ConfigurationChangeEvent; }
34 
35 namespace sd {
36 class ViewShellBase;
37 }
38 
39 
40 namespace sd::framework {
41 
42 /** The FrameworkHelper is a convenience class that simplifies the
43     access to the drawing framework.
44     It has three main tasks:
45     1. Provide frequently used strings of resource URLs and event names.
46     2. Provide shortcuts for accessing the sd framework.
47     3. Ease the migration to the drawing framework.
48 
49     Note that a FrameworkHelper disposes itself when one of the resource
50     controllers called by it throws a DisposedException.
51 */
52 class FrameworkHelper
53     : public std::enable_shared_from_this<FrameworkHelper>,
54       public SdGlobalResource
55 {
56 public:
57     // URLs of frequently used panes.
58     static constexpr OUStringLiteral msPaneURLPrefix = u"private:resource/pane/";
59     static const OUString msCenterPaneURL;
60     static const OUString msFullScreenPaneURL;
61     static const OUString msLeftImpressPaneURL;
62     static const OUString msLeftDrawPaneURL;
63 
64     // URLs of frequently used views.
65     static constexpr OUStringLiteral msViewURLPrefix = u"private:resource/view/";
66     static const OUString msImpressViewURL;
67     static const OUString msDrawViewURL;
68     static const OUString msOutlineViewURL;
69     static const OUString msNotesViewURL;
70     static const OUString msHandoutViewURL;
71     static const OUString msSlideSorterURL;
72     static const OUString msPresentationViewURL;
73     static const OUString msSidebarViewURL;
74 
75     // URLs of frequently used tool bars.
76     static constexpr OUStringLiteral msToolBarURLPrefix = u"private:resource/toolbar/";
77     static const OUString msViewTabBarURL;
78 
79     // Names of frequently used events.
80     static constexpr OUStringLiteral msResourceActivationRequestEvent
81         = u"ResourceActivationRequested";
82     static constexpr OUStringLiteral msResourceDeactivationRequestEvent
83         = u"ResourceDeactivationRequest";
84     static constexpr OUStringLiteral msResourceActivationEvent = u"ResourceActivation";
85     static constexpr OUStringLiteral msResourceDeactivationEvent = u"ResourceDeactivation";
86     static constexpr OUStringLiteral msResourceDeactivationEndEvent = u"ResourceDeactivationEnd";
87     static constexpr OUStringLiteral msConfigurationUpdateStartEvent = u"ConfigurationUpdateStart";
88     static constexpr OUStringLiteral msConfigurationUpdateEndEvent = u"ConfigurationUpdateEnd";
89 
90     /** Return the FrameworkHelper object that is associated with the given
91         ViewShellBase.  If such an object does not yet exist, a new one is
92         created.
93     */
94     static ::std::shared_ptr<FrameworkHelper> Instance (ViewShellBase& rBase);
95 
96     /** Mark the FrameworkHelper object for the given ViewShellBase as
97         disposed.  A following ReleaseInstance() call will destroy the
98         FrameworkHelper object.
99 
100         Do not call this method.  It is an internally used method that can
101         not be made private.
102     */
103     static void DisposeInstance (const ViewShellBase& rBase);
104 
105     /** Destroy the FrameworkHelper object for the given ViewShellBase.
106 
107         Do not call this method.  It is an internally used method that can
108         not be made private.
109     */
110     static void ReleaseInstance (const ViewShellBase& rBase);
111 
112     /** Return an identifier for the given view URL.  This identifier can be
113         used in a switch statement.  See GetViewURL() for a mapping in the
114         opposite direction.
115     */
116     static ViewShell::ShellType GetViewId (const OUString& rsViewURL);
117 
118     /** Return a view URL for the given identifier.  See GetViewId() for a
119         mapping in the opposite direction.
120     */
121     static OUString GetViewURL (ViewShell::ShellType eType);
122 
123     /** Return a ViewShell pointer for the given XView reference.  This
124         assumes that the given reference is implemented by the
125         ViewShellWrapper class that supports the XTunnel interface.
126         @return
127             When the ViewShell pointer can not be inferred from the given
128             reference then an empty pointer is returned.
129     */
130     static ::std::shared_ptr<ViewShell> GetViewShell (
131         const css::uno::Reference<css::drawing::framework::XView>& rxView);
132 
133     typedef ::std::function<bool (const css::drawing::framework::ConfigurationChangeEvent&)>
134         ConfigurationChangeEventFilter;
135     typedef ::std::function<void (bool bEventSeen)> Callback;
136     typedef ::std::function<
137         void (
138             const css::uno::Reference<
139                 css::drawing::framework::XResourceId>&)
140         > ResourceFunctor;
141 
142     /** Test whether the called FrameworkHelper object is valid.
143         @return
144             When the object has already been disposed then <FALSE/> is returned.
145     */
146     bool IsValid() const;
147 
148     /** Return a pointer to the view shell that is displayed in the
149         specified pane.  See GetView() for a variant that returns a
150         reference to XView instead of a ViewShell pointer.
151         @return
152             An empty pointer is returned when for example the specified pane
153             does not exist or is not visible or does not show a view or one
154             of the involved objects does not support XUnoTunnel (where
155             necessary).
156     */
157     ::std::shared_ptr<ViewShell> GetViewShell (const OUString& rsPaneURL);
158 
159     /** Return a reference to the view that is displayed in the specified
160         pane.  See GetViewShell () for a variant that returns a ViewShell
161         pointer instead of a reference to XView.
162         @param rxPaneOrViewId
163             When this ResourceId specifies a view then that view is
164             returned.  When it belongs to a pane then one view in that pane
165             is returned.
166         @return
167             An empty reference is returned when for example the specified pane
168             does not exist or is not visible or does not show a view or one
169             of the involved objects does not support XTunnel (where
170             necessary).
171     */
172     css::uno::Reference<css::drawing::framework::XView> GetView (
173         const css::uno::Reference<css::drawing::framework::XResourceId>& rxPaneOrViewId);
174 
175     /** Request the specified view to be displayed in the specified pane.
176         When the pane is not visible its creation is also requested.  The
177         update that creates the actual view object is done asynchronously.
178         @param rsResourceURL
179             The resource URL of the view to show.
180         @param rsAnchorURL
181             The URL of the pane in which to show the view.
182         @return
183             The resource id of the requested view is returned.  With that
184             the caller can, for example, call RunOnResourceActivation() to
185             do some initialization after the requested view becomes active.
186     */
187     css::uno::Reference<css::drawing::framework::XResourceId> RequestView (
188         const OUString& rsResourceURL,
189         const OUString& rsAnchorURL);
190 
191     /** Process a slot call that requests a view shell change.
192     */
193     void HandleModeChangeSlot (
194         sal_uLong nSlotId,
195         SfxRequest const & rRequest);
196 
197     /** Run the given callback when the specified event is notified by the
198         ConfigurationManager.  When there are no pending requests and
199         therefore no events would be notified (in the foreseeable future)
200         then the callback is called immediately.
201         The callback is called with a flag that tells the callback whether
202         the event it waits for has been sent.
203     */
204     void RunOnConfigurationEvent(
205         const OUString& rsEventType,
206         const Callback& rCallback);
207 
208     /** Run the given callback when the specified resource has been
209         activated.  When the resource is active already when this method is
210         called then rCallback is called before this method returns.
211         @param rxResourceId
212             Wait for the activation of this resource before calling
213             rCallback.
214         @param rCallback
215             The callback to be called when the resource is activated.
216 
217     */
218     void RunOnResourceActivation(
219         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
220         const Callback& rCallback);
221 
222     /** Normally the requested changes of the configuration are executed
223         asynchronously.  However, there is at least one situation (searching
224         with the Outliner) where the surrounding code does not cope with
225         this.  So, instead of calling Reschedule until the global event loop
226         executes the configuration update, this method does (almost) the
227         same without the reschedules.
228 
229         Do not use this method until there is absolutely no other way.
230     */
231     void RequestSynchronousUpdate();
232 
233     /** Block until the specified event is notified by the configuration
234         controller.  When the configuration controller is not processing any
235         requests the method returns immediately.
236     */
237     void WaitForEvent (const OUString& rsEventName) const;
238 
239     /** This is a short cut for WaitForEvent(msConfigurationUpdateEndEvent).
240         Call this method to execute the pending requests.
241     */
242     void WaitForUpdate() const;
243 
244     /** Explicit request for an update of the current configuration.  Call
245         this method when one of the resources managed by the sd framework
246         has been activated or deactivated from the outside, i.e. not by the
247         framework itself.  An example for this is a click on the closer
248         button of one of the side panes.
249     */
250     void UpdateConfiguration();
251 
252     /** Return a string representation of the given XResourceId object.
253     */
254     static OUString ResourceIdToString (
255         const css::uno::Reference<
256             css::drawing::framework::XResourceId>& rxResourceId);
257 
258     /** Create a new XResourceId object for the given resource URL.
259     */
260     static css::uno::Reference<
261         css::drawing::framework::XResourceId>
262             CreateResourceId (
263                 const OUString& rsResourceURL);
264 
265     /** Create a new XResourceId object for the given resource URL and a
266         single anchor URL.
267     */
268     static css::uno::Reference<
269         css::drawing::framework::XResourceId>
270             CreateResourceId (
271                 const OUString& rsResourceURL,
272                 const OUString& rsAnchorURL);
273 
274     /** Create a new XResourceId object for the given resource URL.
275     */
276     static css::uno::Reference<
277         css::drawing::framework::XResourceId>
278             CreateResourceId (
279                 const OUString& rsResourceURL,
280                 const css::uno::Reference<
281                     css::drawing::framework::XResourceId>& rxAnchor);
282 
283     const css::uno::Reference<css::drawing::framework::XConfigurationController>&
GetConfigurationController() const284         GetConfigurationController() const { return mxConfigurationController;}
285 
286 private:
287     typedef ::std::map<
288         const ViewShellBase*,
289         ::std::shared_ptr<FrameworkHelper> > InstanceMap;
290     /** The instance map holds (at least) one FrameworkHelper instance for
291         every ViewShellBase object.
292     */
293     static InstanceMap maInstanceMap;
294     class ViewURLMap;
295     static ViewURLMap maViewURLMap;
296 
297     ViewShellBase& mrBase;
298     css::uno::Reference<css::drawing::framework::XConfigurationController>
299         mxConfigurationController;
300 
301     class DisposeListener;
302     friend class DisposeListener;
303     css::uno::Reference<css::lang::XComponent>
304         mxDisposeListener;
305 
306     FrameworkHelper (ViewShellBase& rBase);
307     FrameworkHelper (const FrameworkHelper& rHelper) = delete;
308     virtual ~FrameworkHelper() override;
309     class Deleter; friend class Deleter;
310     FrameworkHelper& operator= (const FrameworkHelper& rHelper) = delete;
311 
312     void Initialize();
313 
314     void Dispose();
315 
316     /** Run the given callback when an event of the specified type is
317         received from the ConfigurationController or when the
318         ConfigurationController has no pending change requests.
319         @param rsEventType
320             Run rCallback only on this event.
321         @param rFilter
322             This filter has to return <TRUE/> in order for rCallback to be
323             called.
324         @param rCallback
325             The callback functor to be called.
326     */
327     void RunOnEvent(
328         const OUString& rsEventType,
329         const ConfigurationChangeEventFilter& rFilter,
330         const Callback& rCallback) const;
331 
332     /** This disposing method is forwarded from the inner DisposeListener class.
333     */
334     void disposing (const css::lang::EventObject& rEventObject);
335 };
336 
337 } // end of namespace sd::framework
338 
339 
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
341