~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kalarm/lib/synchtimer.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include "kalarm.h"
 
22
#include "synchtimer.moc"
 
23
#include <kdebug.h>
22
24
#include <QTimer>
23
 
#include <kdebug.h>
24
 
#include "synchtimer.moc"
25
25
 
26
26
 
27
27
/*=============================================================================
31
31
 
32
32
SynchTimer::SynchTimer()
33
33
{
34
 
        mTimer = new QTimer(this);
35
 
        mTimer->setSingleShot(true);
 
34
    mTimer = new QTimer(this);
 
35
    mTimer->setSingleShot(true);
36
36
}
37
37
 
38
38
SynchTimer::~SynchTimer()
39
39
{
40
 
        delete mTimer;
41
 
        mTimer = 0;
 
40
    delete mTimer;
 
41
    mTimer = 0;
42
42
}
43
43
 
44
44
/******************************************************************************
46
46
*/
47
47
void SynchTimer::connecT(QObject* receiver, const char* member)
48
48
{
49
 
        Connection connection(receiver, member);
50
 
        if (mConnections.contains(connection))
51
 
                return;           // the slot is already connected, so ignore request
52
 
        connect(mTimer, SIGNAL(timeout()), receiver, member);
53
 
        mConnections.append(connection);
54
 
        if (!mTimer->isActive())
55
 
        {
56
 
                connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
57
 
                start();
58
 
        }
 
49
    Connection connection(receiver, member);
 
50
    if (mConnections.contains(connection))
 
51
        return;           // the slot is already connected, so ignore request
 
52
    connect(mTimer, SIGNAL(timeout()), receiver, member);
 
53
    mConnections.append(connection);
 
54
    if (!mTimer->isActive())
 
55
    {
 
56
        connect(mTimer, SIGNAL(timeout()), this, SLOT(slotTimer()));
 
57
        start();
 
58
    }
59
59
}
60
60
 
61
61
/******************************************************************************
63
63
*/
64
64
void SynchTimer::disconnecT(QObject* receiver, const char* member)
65
65
{
66
 
        if (mTimer)
67
 
        {
68
 
                mTimer->disconnect(receiver, member);
69
 
                if (member)
70
 
                {
71
 
                        int i = mConnections.indexOf(Connection(receiver, member));
72
 
                        if (i >= 0)
73
 
                                mConnections.removeAt(i);
74
 
                }
75
 
                else
76
 
                {
77
 
                        for (int i = 0;  i < mConnections.count();  )
78
 
                        {
79
 
                                if (mConnections[i].receiver == receiver)
80
 
                                        mConnections.removeAt(i);
81
 
                                else
82
 
                                        ++i;
83
 
                        }
84
 
                }
85
 
                if (mConnections.isEmpty())
86
 
                {
87
 
                        mTimer->disconnect();
88
 
                        mTimer->stop();
89
 
                }
90
 
        }
 
66
    if (mTimer)
 
67
    {
 
68
        mTimer->disconnect(receiver, member);
 
69
        if (member)
 
70
        {
 
71
            int i = mConnections.indexOf(Connection(receiver, member));
 
72
            if (i >= 0)
 
73
                mConnections.removeAt(i);
 
74
        }
 
75
        else
 
76
        {
 
77
            for (int i = 0;  i < mConnections.count();  )
 
78
            {
 
79
                if (mConnections[i].receiver == receiver)
 
80
                    mConnections.removeAt(i);
 
81
                else
 
82
                    ++i;
 
83
            }
 
84
        }
 
85
        if (mConnections.isEmpty())
 
86
        {
 
87
            mTimer->disconnect();
 
88
            mTimer->stop();
 
89
        }
 
90
    }
91
91
}
92
92
 
93
93
 
100
100
 
101
101
MinuteTimer* MinuteTimer::instance()
102
102
{
103
 
        if (!mInstance)
104
 
                mInstance = new MinuteTimer;
105
 
        return mInstance;
 
103
    if (!mInstance)
 
104
        mInstance = new MinuteTimer;
 
105
    return mInstance;
106
106
}
107
107
 
108
108
/******************************************************************************
113
113
*/
114
114
void MinuteTimer::slotTimer()
115
115
{
116
 
        kDebug();
117
 
        int interval = 62 - QTime::currentTime().second();
118
 
        mTimer->start(interval * 1000);     // execute a single shot
 
116
    kDebug();
 
117
    int interval = 62 - QTime::currentTime().second();
 
118
    mTimer->start(interval * 1000);     // execute a single shot
119
119
}
120
120
 
121
121
 
127
127
QList<DailyTimer*> DailyTimer::mFixedTimers;
128
128
 
129
129
DailyTimer::DailyTimer(const QTime& timeOfDay, bool fixed)
130
 
        : mTime(timeOfDay),
131
 
          mFixed(fixed)
 
130
    : mTime(timeOfDay),
 
131
      mFixed(fixed)
132
132
{
133
 
        if (fixed)
134
 
                mFixedTimers.append(this);
 
133
    if (fixed)
 
134
        mFixedTimers.append(this);
135
135
}
136
136
 
137
137
DailyTimer::~DailyTimer()
138
138
{
139
 
        if (mFixed)
140
 
                mFixedTimers.removeAt(mFixedTimers.indexOf(this));
 
139
    if (mFixed)
 
140
        mFixedTimers.removeAt(mFixedTimers.indexOf(this));
141
141
}
142
142
 
143
143
DailyTimer* DailyTimer::fixedInstance(const QTime& timeOfDay, bool create)
144
144
{
145
 
        for (int i = 0, end = mFixedTimers.count();  i < end;  ++i)
146
 
                if (mFixedTimers[i]->mTime == timeOfDay)
147
 
                        return mFixedTimers[i];
148
 
        return create ? new DailyTimer(timeOfDay, true) : 0;
 
145
    for (int i = 0, end = mFixedTimers.count();  i < end;  ++i)
 
146
        if (mFixedTimers[i]->mTime == timeOfDay)
 
147
            return mFixedTimers[i];
 
148
    return create ? new DailyTimer(timeOfDay, true) : 0;
149
149
}
150
150
 
151
151
/******************************************************************************
154
154
*/
155
155
void DailyTimer::disconnect(const QTime& timeOfDay, QObject* receiver, const char* member)
156
156
{
157
 
        DailyTimer* timer = fixedInstance(timeOfDay, false);
158
 
        if (!timer)
159
 
                return;
160
 
        timer->disconnecT(receiver, member);
161
 
        if (!timer->hasConnections())
162
 
                delete timer;
 
157
    DailyTimer* timer = fixedInstance(timeOfDay, false);
 
158
    if (!timer)
 
159
        return;
 
160
    timer->disconnecT(receiver, member);
 
161
    if (!timer->hasConnections())
 
162
        delete timer;
163
163
}
164
164
 
165
165
/******************************************************************************
167
167
*/
168
168
void DailyTimer::changeTime(const QTime& newTimeOfDay, bool triggerMissed)
169
169
{
170
 
        if (mFixed)
171
 
                return;
172
 
        if (mTimer->isActive())
173
 
        {
174
 
                mTimer->stop();
175
 
                bool triggerNow = false;
176
 
                if (triggerMissed)
177
 
                {
178
 
                        QTime now = QTime::currentTime();
179
 
                        if (now >= newTimeOfDay  &&  now < mTime)
180
 
                        {
181
 
                                // The trigger time is now earlier and it has already arrived today.
182
 
                                // Trigger a timer event immediately.
183
 
                                triggerNow = true;
184
 
                        }
185
 
                }
186
 
                mTime = newTimeOfDay;
187
 
                if (triggerNow)
188
 
                        mTimer->start(0);    // trigger immediately
189
 
                else
190
 
                        start();
191
 
        }
192
 
        else
193
 
                mTime = newTimeOfDay;
 
170
    if (mFixed)
 
171
        return;
 
172
    if (mTimer->isActive())
 
173
    {
 
174
        mTimer->stop();
 
175
        bool triggerNow = false;
 
176
        if (triggerMissed)
 
177
        {
 
178
            QTime now = QTime::currentTime();
 
179
            if (now >= newTimeOfDay  &&  now < mTime)
 
180
            {
 
181
                // The trigger time is now earlier and it has already arrived today.
 
182
                // Trigger a timer event immediately.
 
183
                triggerNow = true;
 
184
            }
 
185
        }
 
186
        mTime = newTimeOfDay;
 
187
        if (triggerNow)
 
188
            mTimer->start(0);    // trigger immediately
 
189
        else
 
190
            start();
 
191
    }
 
192
    else
 
193
        mTime = newTimeOfDay;
194
194
}
195
195
 
196
196
/******************************************************************************
200
200
*/
201
201
void DailyTimer::start()
202
202
{
203
 
        // TIMEZONE = local time
204
 
        QDateTime now = QDateTime::currentDateTime();
205
 
        // Find out whether to trigger today or tomorrow.
206
 
        // In preference, use the last trigger date to determine this, since
207
 
        // that will avoid possible errors due to daylight savings time changes.
208
 
        bool today;
209
 
        if (mLastDate.isValid())
210
 
                today = (mLastDate < now.date());
211
 
        else
212
 
                today = (now.time() < mTime);
213
 
        QDateTime next;
214
 
        if (today)
215
 
                next = QDateTime(now.date(), mTime);
216
 
        else
217
 
                next = QDateTime(now.date().addDays(1), mTime);
218
 
        uint interval = next.toTime_t() - now.toTime_t();
219
 
        mTimer->start(interval * 1000);    // execute a single shot
220
 
        kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
 
203
    // TIMEZONE = local time
 
204
    QDateTime now = QDateTime::currentDateTime();
 
205
    // Find out whether to trigger today or tomorrow.
 
206
    // In preference, use the last trigger date to determine this, since
 
207
    // that will avoid possible errors due to daylight savings time changes.
 
208
    bool today;
 
209
    if (mLastDate.isValid())
 
210
        today = (mLastDate < now.date());
 
211
    else
 
212
        today = (now.time() < mTime);
 
213
    QDateTime next;
 
214
    if (today)
 
215
        next = QDateTime(now.date(), mTime);
 
216
    else
 
217
        next = QDateTime(now.date().addDays(1), mTime);
 
218
    uint interval = next.toTime_t() - now.toTime_t();
 
219
    mTimer->start(interval * 1000);    // execute a single shot
 
220
    kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
221
221
}
222
222
 
223
223
/******************************************************************************
228
228
*/
229
229
void DailyTimer::slotTimer()
230
230
{
231
 
        // TIMEZONE = local time
232
 
        QDateTime now = QDateTime::currentDateTime();
233
 
        mLastDate = now.date();
234
 
        QDateTime next = QDateTime(mLastDate.addDays(1), mTime);
235
 
        uint interval = next.toTime_t() - now.toTime_t();
236
 
        mTimer->start(interval * 1000);    // execute a single shot
237
 
        kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
 
231
    // TIMEZONE = local time
 
232
    QDateTime now = QDateTime::currentDateTime();
 
233
    mLastDate = now.date();
 
234
    QDateTime next = QDateTime(mLastDate.addDays(1), mTime);
 
235
    uint interval = next.toTime_t() - now.toTime_t();
 
236
    mTimer->start(interval * 1000);    // execute a single shot
 
237
    kDebug() << "at" << mTime.hour() << ":" << mTime.minute() << ": interval =" << interval/3600 << ":" << (interval/60)%60 << ":" << interval%60;
238
238
}
 
239
 
 
240
// vim: et sw=4: