57
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
// System includes
|
|
19 |
#include <QTimer>
|
|
20 |
#include <QMetaMethod>
|
|
21 |
|
|
22 |
// User includes
|
|
23 |
#include "radiotimerpool.h"
|
|
24 |
#include "radiologger.h"
|
|
25 |
|
|
26 |
const int TIMER_BUFFER_SIZE = 2;
|
|
27 |
|
|
28 |
/*!
|
|
29 |
*
|
|
30 |
*/
|
|
31 |
RadioTimerPool::RadioTimerItem::RadioTimerItem( RadioTimerPool* parent ) :
|
|
32 |
mTimer( new QTimer() )
|
|
33 |
{
|
|
34 |
Radio::connect( mTimer.data(), SIGNAL(timeout()),
|
|
35 |
parent, SLOT(timerFired()) );
|
|
36 |
mTimer->setSingleShot( true );
|
|
37 |
}
|
|
38 |
|
|
39 |
/*!
|
|
40 |
*
|
|
41 |
*/
|
|
42 |
RadioTimerPool::RadioTimerItem::~RadioTimerItem()
|
|
43 |
{
|
|
44 |
}
|
|
45 |
|
|
46 |
/*!
|
|
47 |
*
|
|
48 |
*/
|
|
49 |
void RadioTimerPool::RadioTimerItem::reset()
|
|
50 |
{
|
|
51 |
mTimer->stop();
|
|
52 |
mId = -1;
|
|
53 |
mParam = QVariant();
|
|
54 |
mReceiver = NULL;
|
|
55 |
mReceiverMember.clear();
|
|
56 |
}
|
|
57 |
|
|
58 |
/*!
|
|
59 |
*
|
|
60 |
*/
|
|
61 |
RadioTimerPool::RadioTimerPool( QObject* parent ) :
|
|
62 |
QObject( parent )
|
|
63 |
{
|
|
64 |
createItem();
|
|
65 |
}
|
|
66 |
|
|
67 |
/*!
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
RadioTimerPool::~RadioTimerPool()
|
|
71 |
{
|
|
72 |
qDeleteAll( mTimerMap.values() );
|
|
73 |
}
|
|
74 |
|
|
75 |
/*!
|
|
76 |
*
|
|
77 |
*/
|
|
78 |
void RadioTimerPool::startTimer( int msec, int id, QObject* receiver, const char* member, QVariant param )
|
|
79 |
{
|
|
80 |
LOG_METHOD;
|
|
81 |
RadioTimerItem* availableItem = NULL;
|
|
82 |
foreach ( RadioTimerItem* item, mTimerMap.values() ) {
|
|
83 |
if ( !item->mTimer->isActive() ) {
|
|
84 |
availableItem = item;
|
|
85 |
} else {
|
|
86 |
if ( item->mReceiver == receiver && item->mId == id ) {
|
|
87 |
LOG_FORMAT( "RadioTimerPool::startTimer. Timer %d already running for class %s", id, receiver->metaObject()->className() );
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
if ( !availableItem ) {
|
|
93 |
availableItem = createItem();
|
|
94 |
LOG( "RadioTimerPool::startTimer. Creating new timer" );
|
|
95 |
}
|
|
96 |
LOG_FORMAT( "Amount of timers: %d", mTimerMap.count() );
|
|
97 |
|
|
98 |
availableItem->mId = id;
|
|
99 |
availableItem->mParam = param;
|
|
100 |
availableItem->mReceiver = receiver;
|
|
101 |
|
|
102 |
// SIGNAL() and SLOT() macros add a number in the beginning of member function name for some internal housekeeping
|
|
103 |
// That causes the method to not be found from the meta object so we need to trim it off
|
|
104 |
availableItem->mReceiverMember = member;
|
|
105 |
availableItem->mReceiverMember = availableItem->mReceiverMember.right( availableItem->mReceiverMember.length() - 1 );
|
|
106 |
|
|
107 |
availableItem->mTimer->start( msec );
|
|
108 |
}
|
|
109 |
|
|
110 |
/*!
|
|
111 |
*
|
|
112 |
*/
|
|
113 |
void RadioTimerPool::cancelTimer( int id, const QObject* receiver )
|
|
114 |
{
|
|
115 |
foreach ( RadioTimerItem* item, mTimerMap.values() ) {
|
|
116 |
if ( item->mId == id && item->mReceiver == receiver ) {
|
|
117 |
item->reset();
|
|
118 |
break;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
freeUnusedTimers();
|
|
122 |
}
|
|
123 |
|
|
124 |
/*!
|
|
125 |
*
|
|
126 |
*/
|
|
127 |
bool RadioTimerPool::isTimerActive( int id, const QObject* receiver ) const
|
|
128 |
{
|
|
129 |
foreach ( const RadioTimerItem* item, mTimerMap.values() ) {
|
|
130 |
if ( item->mId == id &&
|
|
131 |
item->mReceiver == receiver &&
|
|
132 |
item->mTimer->isActive() ) {
|
|
133 |
return true;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
return false;
|
|
137 |
}
|
|
138 |
|
|
139 |
/*!
|
|
140 |
*
|
|
141 |
*/
|
|
142 |
void RadioTimerPool::freeUnusedTimers()
|
|
143 |
{
|
|
144 |
QList<RadioTimerItem*> unusedItems;
|
|
145 |
foreach ( RadioTimerItem* item, mTimerMap.values() ) {
|
|
146 |
if ( !item->mTimer->isActive() ) {
|
|
147 |
unusedItems.append( item );
|
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
if ( unusedItems.count() > TIMER_BUFFER_SIZE ) {
|
|
152 |
foreach( const RadioTimerItem* item, unusedItems ) {
|
|
153 |
mTimerMap.remove( item->mTimer.data() );
|
|
154 |
delete item;
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
/*!
|
|
160 |
* Private slot
|
|
161 |
*/
|
|
162 |
void RadioTimerPool::timerFired()
|
|
163 |
{
|
|
164 |
QTimer* timer = static_cast<QTimer*>( sender() );
|
|
165 |
if ( mTimerMap.contains( timer ) ) {
|
|
166 |
RadioTimerItem* item = mTimerMap.value( timer );
|
|
167 |
if ( item->mReceiver ) {
|
|
168 |
QByteArray normalizedSignature = QMetaObject::normalizedSignature( item->mReceiverMember.toAscii().constData() );
|
|
169 |
int methodIndex = item->mReceiver->metaObject()->indexOfMethod( normalizedSignature );
|
|
170 |
if ( methodIndex != -1 ) {
|
|
171 |
QMetaMethod method = item->mReceiver->metaObject()->method( methodIndex );
|
|
172 |
const int paramCount = method.parameterTypes().count();
|
|
173 |
if ( paramCount == 0 ) {
|
|
174 |
method.invoke( item->mReceiver, Qt::QueuedConnection );
|
|
175 |
} else if ( paramCount == 1 ) {
|
|
176 |
method.invoke( item->mReceiver, Qt::QueuedConnection, Q_ARG( int, item->mId ) );
|
|
177 |
} else if ( paramCount == 2 ) {
|
|
178 |
method.invoke( item->mReceiver, Qt::QueuedConnection,
|
|
179 |
Q_ARG( int, item->mId ), Q_ARG( QVariant, item->mParam ) );
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
item->reset();
|
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
/*!
|
|
189 |
*
|
|
190 |
*/
|
|
191 |
RadioTimerPool::RadioTimerItem* RadioTimerPool::createItem()
|
|
192 |
{
|
|
193 |
RadioTimerItem* item = new RadioTimerItem( this );
|
|
194 |
mTimerMap.insert( item->mTimer.data(), item );
|
|
195 |
return item;
|
|
196 |
}
|