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 SDEXT_PRESENTER_PRESENTER_FRAMEWORK_OBSERVER_HXX
25 #define SDEXT_PRESENTER_PRESENTER_FRAMEWORK_OBSERVER_HXX
26 
27 #include <com/sun/star/drawing/framework/XConfigurationChangeListener.hpp>
28 #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
29 #include <cppuhelper/basemutex.hxx>
30 #include <cppuhelper/compbase1.hxx>
31 #include <boost/function.hpp>
32 #include <boost/noncopyable.hpp>
33 
34 namespace css = ::com::sun::star;
35 
36 namespace sdext { namespace presenter {
37 
38 
39 typedef ::cppu::WeakComponentImplHelper1 <
40     ::com::sun::star::drawing::framework::XConfigurationChangeListener
41     > PresenterFrameworkObserverInterfaceBase;
42 
43 /** Watch the drawing framework for changes and run callbacks when a certain
44     change takes place.
45 */
46 class PresenterFrameworkObserver
47     : private ::boost::noncopyable,
48       private ::cppu::BaseMutex,
49       public PresenterFrameworkObserverInterfaceBase
50 {
51 public:
52     typedef ::boost::function<bool(void)> Predicate;
53     typedef ::boost::function<void(bool)> Action;
54 
55     /** Register an action callback to be run when the specified resource is
56         activated.  The action may be called synchronously when the resource
57         is already active or asynchronously when the resource is actived in
58         the future.
59         @param rxController
60             The controller gives access to the drawing framework.
61         @param rxResourceId
62             The id of the resource to watch for activation.
63         @param rAction
64             The callback that is called when the resource is activated.
65     */
66     static void RunOnResourceActivation (
67         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
68         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId,
69         const Action& rAction);
70     static void RunOnUpdateEnd (
71         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
72         const Action& rAction);
73 
74     virtual void SAL_CALL disposing (void);
75     virtual void SAL_CALL disposing (const css::lang::EventObject& rEvent)
76         throw (css::uno::RuntimeException);
77     virtual void SAL_CALL notifyConfigurationChange (
78         const css::drawing::framework::ConfigurationChangeEvent& rEvent)
79         throw (css::uno::RuntimeException);
80 
81 private:
82     ::rtl::OUString msEventType;
83     ::css::uno::Reference<css::drawing::framework::XConfigurationController> mxConfigurationController;
84     Predicate maPredicate;
85     Action maAction;
86 
87     /** Create a new PresenterFrameworkObserver object.
88         @param rsEventName
89             An event name other than ConfigurationUpdateEnd.  When the
90             observer shall only listen for ConfigurationUpdateEnd then pass
91             an empty name.
92         @param rPredicate
93             This functor tests whether the action is to be executed or not.
94         @param rAction
95             The functor to execute when the predicate returns true,
96             e.g. when some resource has been created.
97     */
98     PresenterFrameworkObserver (
99         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
100         const ::rtl::OUString& rsEventName,
101         const Predicate& rPredicate,
102         const Action& rAction);
103     virtual ~PresenterFrameworkObserver (void);
104 
105     void Shutdown (void);
106 
107     /** Predicate that returns true when the specified resource is active
108         with respect to the given configuration controller.
109     */
110     static bool HasResource (
111         const css::uno::Reference<css::drawing::framework::XConfigurationController>&rxController,
112         const css::uno::Reference<css::drawing::framework::XResourceId>& rxResourceId);
113 
114     /** Predicate that always returns true.
115     */
116     static bool True (void);
117 
118     /** Predicate that always returns false.
119     */
120     static bool False (void);
121 };
122 
123 
124 } }  // end of namespace ::sdext::presenter
125 
126 #endif
127