1*06bcd5d2SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*06bcd5d2SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*06bcd5d2SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*06bcd5d2SAndrew Rist * distributed with this work for additional information 6*06bcd5d2SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*06bcd5d2SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*06bcd5d2SAndrew Rist * "License"); you may not use this file except in compliance 9*06bcd5d2SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*06bcd5d2SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*06bcd5d2SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*06bcd5d2SAndrew Rist * software distributed under the License is distributed on an 15*06bcd5d2SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*06bcd5d2SAndrew Rist * KIND, either express or implied. See the License for the 17*06bcd5d2SAndrew Rist * specific language governing permissions and limitations 18*06bcd5d2SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*06bcd5d2SAndrew Rist *************************************************************/ 21*06bcd5d2SAndrew Rist 22*06bcd5d2SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef SDEXT_PRESENTER_TIMER_HXX 25cdf0e10cSrcweir #define SDEXT_PRESENTER_TIMER_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <com/sun/star/awt/XCallback.hpp> 28cdf0e10cSrcweir #include <com/sun/star/awt/XRequestCallback.hpp> 29cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx> 30cdf0e10cSrcweir #include <cppuhelper/compbase1.hxx> 31cdf0e10cSrcweir #include <osl/mutex.hxx> 32cdf0e10cSrcweir #include <osl/time.h> 33cdf0e10cSrcweir #include <rtl/ref.hxx> 34cdf0e10cSrcweir #include <sal/types.h> 35cdf0e10cSrcweir #include <boost/enable_shared_from_this.hpp> 36cdf0e10cSrcweir #include <boost/function.hpp> 37cdf0e10cSrcweir #include <vector> 38cdf0e10cSrcweir 39cdf0e10cSrcweir namespace css = ::com::sun::star; 40cdf0e10cSrcweir 41cdf0e10cSrcweir namespace sdext { namespace presenter { 42cdf0e10cSrcweir 43cdf0e10cSrcweir class PresenterClockInternalTimer; 44cdf0e10cSrcweir 45cdf0e10cSrcweir /** The timer allows tasks to be scheduled for execution at a specified time 46cdf0e10cSrcweir in the future. 47cdf0e10cSrcweir */ 48cdf0e10cSrcweir class PresenterTimer 49cdf0e10cSrcweir { 50cdf0e10cSrcweir public: 51cdf0e10cSrcweir /** A task is called with the current time. 52cdf0e10cSrcweir */ 53cdf0e10cSrcweir typedef ::boost::function<void(const TimeValue&)> Task; 54cdf0e10cSrcweir 55cdf0e10cSrcweir static const sal_Int32 NotAValidTaskId = 0; 56cdf0e10cSrcweir 57cdf0e10cSrcweir static sal_Int32 ScheduleSingleTaskRelative ( 58cdf0e10cSrcweir const Task& rTask, 59cdf0e10cSrcweir const sal_Int64 nDelay); 60cdf0e10cSrcweir 61cdf0e10cSrcweir static sal_Int32 ScheduleSingleTaskAbsolute ( 62cdf0e10cSrcweir const Task& rTask, 63cdf0e10cSrcweir const TimeValue& rDueTime); 64cdf0e10cSrcweir 65cdf0e10cSrcweir /** Schedule a task to be executed repeatedly. The task is executed the 66cdf0e10cSrcweir first time after nFirst nano-seconds (1000000000 corresponds to one 67cdf0e10cSrcweir second). After that task is executed in intervalls that are 68cdf0e10cSrcweir nIntervall ns long until CancelTask is called. 69cdf0e10cSrcweir */ 70cdf0e10cSrcweir static sal_Int32 ScheduleRepeatedTask ( 71cdf0e10cSrcweir const Task& rTask, 72cdf0e10cSrcweir const sal_Int64 nFirst, 73cdf0e10cSrcweir const sal_Int64 nIntervall); 74cdf0e10cSrcweir 75cdf0e10cSrcweir static void CancelTask (const sal_Int32 nTaskId); 76cdf0e10cSrcweir }; 77cdf0e10cSrcweir 78cdf0e10cSrcweir 79cdf0e10cSrcweir 80cdf0e10cSrcweir typedef cppu::WeakComponentImplHelper1< 81cdf0e10cSrcweir css::awt::XCallback 82cdf0e10cSrcweir > PresenterClockTimerInterfaceBase; 83cdf0e10cSrcweir 84cdf0e10cSrcweir /** A timer that calls its listeners, typically clocks, every second to 85cdf0e10cSrcweir update their current time value. 86cdf0e10cSrcweir */ 87cdf0e10cSrcweir class PresenterClockTimer 88cdf0e10cSrcweir : protected ::cppu::BaseMutex, 89cdf0e10cSrcweir public PresenterClockTimerInterfaceBase 90cdf0e10cSrcweir { 91cdf0e10cSrcweir public: 92cdf0e10cSrcweir class Listener { public: 93cdf0e10cSrcweir virtual void TimeHasChanged (const oslDateTime& rCurrentTime) = 0; 94cdf0e10cSrcweir }; 95cdf0e10cSrcweir typedef ::boost::shared_ptr<Listener> SharedListener; 96cdf0e10cSrcweir 97cdf0e10cSrcweir static ::rtl::Reference<PresenterClockTimer> Instance ( 98cdf0e10cSrcweir const css::uno::Reference<css::uno::XComponentContext>& rxContext); 99cdf0e10cSrcweir 100cdf0e10cSrcweir void AddListener (const SharedListener& rListener); 101cdf0e10cSrcweir void RemoveListener (const SharedListener& rListener); 102cdf0e10cSrcweir 103cdf0e10cSrcweir static oslDateTime GetCurrentTime (void); 104cdf0e10cSrcweir 105cdf0e10cSrcweir /** Return the difference between the two different times in 106cdf0e10cSrcweir nanoseconds. 107cdf0e10cSrcweir */ 108cdf0e10cSrcweir static sal_Int64 GetTimeDifference (const oslDateTime& rNow, const oslDateTime& rThen); 109cdf0e10cSrcweir 110cdf0e10cSrcweir // XCallback 111cdf0e10cSrcweir 112cdf0e10cSrcweir virtual void SAL_CALL notify (const css::uno::Any& rUserData) 113cdf0e10cSrcweir throw (css::uno::RuntimeException); 114cdf0e10cSrcweir 115cdf0e10cSrcweir private: 116cdf0e10cSrcweir static ::rtl::Reference<PresenterClockTimer> mpInstance; 117cdf0e10cSrcweir 118cdf0e10cSrcweir ::osl::Mutex maMutex; 119cdf0e10cSrcweir typedef ::std::vector<SharedListener> ListenerContainer; 120cdf0e10cSrcweir ListenerContainer maListeners; 121cdf0e10cSrcweir oslDateTime maDateTime; 122cdf0e10cSrcweir sal_Int32 mnTimerTaskId; 123cdf0e10cSrcweir bool mbIsCallbackPending; 124cdf0e10cSrcweir css::uno::Reference<css::awt::XRequestCallback> mxRequestCallback; 125cdf0e10cSrcweir 126cdf0e10cSrcweir PresenterClockTimer ( 127cdf0e10cSrcweir const css::uno::Reference<css::uno::XComponentContext>& rxContext); 128cdf0e10cSrcweir ~PresenterClockTimer (void); 129cdf0e10cSrcweir 130cdf0e10cSrcweir void CheckCurrentTime (const TimeValue& rCurrentTime); 131cdf0e10cSrcweir }; 132cdf0e10cSrcweir 133cdf0e10cSrcweir 134cdf0e10cSrcweir 135cdf0e10cSrcweir 136cdf0e10cSrcweir 137cdf0e10cSrcweir } } // end of namespace ::sdext::presenter 138cdf0e10cSrcweir 139cdf0e10cSrcweir #endif 140