1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SDEXT_PRESENTER_PRESENTER_FRAMEWORK_OBSERVER_HXX
29 #define SDEXT_PRESENTER_PRESENTER_FRAMEWORK_OBSERVER_HXX
30 
31 #include <com/sun/star/drawing/framework/XConfigurationChangeListener.hpp>
32 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
33 #include <cppuhelper/basemutex.hxx>
34 #include <cppuhelper/compbase1.hxx>
35 #include <boost/function.hpp>
36 #include <boost/noncopyable.hpp>
37 
38 namespace css = ::com::sun::star;
39 
40 namespace sdext { namespace presenter {
41 
42 
43 typedef ::cppu::WeakComponentImplHelper1 <
44     ::com::sun::star::drawing::framework::XConfigurationChangeListener
45     > PresenterFrameworkObserverInterfaceBase;
46 
47 /** Watch the drawing framework for changes and run callbacks when a certain
48     change takes place.
49 */
50 class PresenterFrameworkObserver
51     : private ::boost::noncopyable,
52       private ::cppu::BaseMutex,
53       public PresenterFrameworkObserverInterfaceBase
54 {
55 public:
56     typedef ::boost::function<bool(void)> Predicate;
57     typedef ::boost::function<void(bool)> Action;
58 
59     /** Register an action callback to be run when the specified resource is
60         activated.  The action may be called synchronously when the resource
61         is already active or asynchronously when the resource is actived in
62         the future.
63         @param rxController
64             The controller gives access to the drawing framework.
65         @param rxResourceId
66             The id of the resource to watch for activation.
67         @param rAction
68             The callback that is called when the resource is activated.
69     */
70     static void RunOnResourceActivation (
71         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
72         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
73         const Action& rAction);
74     static void RunOnUpdateEnd (
75         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
76         const Action& rAction);
77 
78     virtual void SAL_CALL disposing (void);
79     virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
80         throw (css::uno::RuntimeException);
81     virtual void SAL_CALL notifyConfigurationChange (
82         const css::drawing::framework::ConfigurationChangeEvent& rEvent)
83         throw (css::uno::RuntimeException);
84 
85 private:
86     ::rtl::OUString msEventType;
87     ::css::uno::Reference<css::drawing::framework::XConfigurationController> mxConfigurationController;
88     Predicate maPredicate;
89     Action maAction;
90 
91     /** Create a new PresenterFrameworkObserver object.
92         @param rsEventName
93             An event name other than ConfigurationUpdateEnd.  When the
94             observer shall only listen for ConfigurationUpdateEnd then pass
95             an empty name.
96         @param rPredicate
97             This functor tests whether the action is to be executed or not.
98         @param rAction
99             The functor to execute when the predicate returns true,
100             e.g. when some resource has been created.
101     */
102     PresenterFrameworkObserver (
103         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
104         const ::rtl::OUString& rsEventName,
105         const Predicate& rPredicate,
106         const Action& rAction);
107     virtual ~PresenterFrameworkObserver (void);
108 
109     void Shutdown (void);
110 
111     /** Predicate that returns true when the specified resource is active
112         with respect to the given configuration controller.
113     */
114     static bool HasResource (
115         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
116         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId);
117 
118     /** Predicate that always returns true.
119     */
120     static bool True (void);
121 
122     /** Predicate that always returns false.
123     */
124     static bool False (void);
125 };
126 
127 
128 } }  // end of namespace ::sdext::presenter
129 
130 #endif
131