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_TIMER_HXX
25 #define SDEXT_PRESENTER_TIMER_HXX
26 
27 #include <com/sun/star/awt/XCallback.hpp>
28 #include <com/sun/star/awt/XRequestCallback.hpp>
29 #include <cppuhelper/basemutex.hxx>
30 #include <cppuhelper/compbase1.hxx>
31 #include <osl/mutex.hxx>
32 #include <osl/time.h>
33 #include <rtl/ref.hxx>
34 #include <sal/types.h>
35 #include <boost/enable_shared_from_this.hpp>
36 #include <boost/function.hpp>
37 #include <vector>
38 
39 namespace css = ::com::sun::star;
40 
41 namespace sdext { namespace presenter {
42 
43 class PresenterClockInternalTimer;
44 
45 /** The timer allows tasks to be scheduled for execution at a specified time
46     in the future.
47 */
48 class PresenterTimer
49 {
50 public:
51     /** A task is called with the current time.
52     */
53     typedef ::boost::function<void(const TimeValue&)> Task;
54 
55     static const sal_Int32 NotAValidTaskId = 0;
56 
57     static sal_Int32 ScheduleSingleTaskRelative (
58         const Task& rTask,
59         const sal_Int64 nDelay);
60 
61     static sal_Int32 ScheduleSingleTaskAbsolute (
62         const Task& rTask,
63         const TimeValue& rDueTime);
64 
65     /** Schedule a task to be executed repeatedly.  The task is executed the
66         first time after nFirst nano-seconds (1000000000 corresponds to one
67         second).  After that task is executed in intervalls that are
68         nIntervall ns long until CancelTask is called.
69     */
70     static sal_Int32 ScheduleRepeatedTask (
71         const Task& rTask,
72         const sal_Int64 nFirst,
73         const sal_Int64 nIntervall);
74 
75     static void CancelTask (const sal_Int32 nTaskId);
76 };
77 
78 
79 
80 typedef cppu::WeakComponentImplHelper1<
81     css::awt::XCallback
82     > PresenterClockTimerInterfaceBase;
83 
84 /** A timer that calls its listeners, typically clocks, every second to
85     update their current time value.
86 */
87 class PresenterClockTimer
88     : protected ::cppu::BaseMutex,
89       public PresenterClockTimerInterfaceBase
90 {
91 public:
92     class Listener { public:
93         virtual void TimeHasChanged (const oslDateTime& rCurrentTime) = 0;
94     };
95     typedef ::boost::shared_ptr<Listener> SharedListener;
96 
97     static ::rtl::Reference<PresenterClockTimer> Instance (
98         const css::uno::Reference<css::uno::XComponentContext>& rxContext);
99 
100     void AddListener (const SharedListener& rListener);
101     void RemoveListener (const SharedListener& rListener);
102 
103     static oslDateTime GetCurrentTime (void);
104 
105     /** Return the difference between the two different times in
106         nanoseconds.
107     */
108     static sal_Int64 GetTimeDifference (const oslDateTime& rNow, const oslDateTime& rThen);
109 
110     // XCallback
111 
112     virtual void SAL_CALL notify (const css::uno::Any& rUserData)
113         throw (css::uno::RuntimeException);
114 
115 private:
116     static ::rtl::Reference<PresenterClockTimer> mpInstance;
117 
118     ::osl::Mutex maMutex;
119     typedef ::std::vector<SharedListener> ListenerContainer;
120     ListenerContainer maListeners;
121     oslDateTime maDateTime;
122     sal_Int32 mnTimerTaskId;
123     bool mbIsCallbackPending;
124     css::uno::Reference<css::awt::XRequestCallback> mxRequestCallback;
125 
126     PresenterClockTimer (
127         const css::uno::Reference<css::uno::XComponentContext>& rxContext);
128     ~PresenterClockTimer (void);
129 
130     void CheckCurrentTime (const TimeValue& rCurrentTime);
131 };
132 
133 
134 
135 
136 
137 } } // end of namespace ::sdext::presenter
138 
139 #endif
140