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