summaryrefslogtreecommitdiffstats
path: root/plugins/timecontrol/timecontrol.cpp
blob: 1b56ff05653eeab8a655146713c01bffb7ce6d80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/***************************************************************************
                          timecontrol.cpp  -  description
                             -------------------
    begin                : Son Jan 12 2003
    copyright            : (C) 2003 by Martin Witte
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tdeaboutdata.h>
#include <tdeconfig.h>

#include "timecontrol.h"
#include "timecontrol-configuration.h"
#include "../../src/include/pluginmanager.h"
#include "../../src/include/aboutwidget.h"

//const char *AlarmListElement            = "alarmlist";
//const char *AlarmElement                = "alarm";
const char *AlarmDateElement            = "date";
const char *AlarmTimeElement            = "time";
const char *AlarmDailyElement           = "daily";
const char *AlarmWeekdayMaskElement     = "weekdayMask";
const char *AlarmEnabledElement         = "enabled";
const char *AlarmStationIDElement       = "stationID";
//const char *AlarmFrequencyElement       = "frequency";
const char *AlarmVolumeElement          = "volume";
const char *AlarmTypeElement            = "type";


///////////////////////////////////////////////////////////////////////
//// plugin library functions

PLUGIN_LIBRARY_FUNCTIONS(TimeControl, "tderadio-timecontrol", i18n("Time Control and Alarm Functions"));

///////////////////////////////////////////////////////////////////////


TimeControl::TimeControl (const TQString &n)
    : PluginBase(n, i18n("TimeControl Plugin")),
      m_waitingFor(NULL),
      m_countdownSeconds(0),
      m_alarmTimer(this),
      m_countdownTimer(this)
{
    TQObject::connect(&m_alarmTimer,     TQT_SIGNAL(timeout()), this, TQT_SLOT(slotQTimerAlarmTimeout()));
    TQObject::connect(&m_countdownTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotQTimerCountdownTimeout()));
}


TimeControl::~TimeControl ()
{
    m_waitingFor = NULL;
}

bool   TimeControl::connectI (Interface *i)
{
    bool a = ITimeControl::connectI(i);
    bool b = PluginBase::connectI(i);
    return a || b;
}

bool   TimeControl::disconnectI (Interface *i)
{
    bool a = ITimeControl::disconnectI(i);
    bool b = PluginBase::disconnectI(i);
    return a || b;
}

bool TimeControl::setAlarms (const AlarmVector &al)
{
    if (m_alarms != al) {
        m_waitingFor = NULL;

        m_alarms = al;

        slotQTimerAlarmTimeout();

        notifyAlarmsChanged(m_alarms);
    }
    return true;
}


bool TimeControl::setCountdownSeconds(int n)
{
    int old = m_countdownSeconds;
    m_countdownSeconds = n;
    if (old != n)
        notifyCountdownSecondsChanged(n);
    return true;
}


bool TimeControl::startCountdown()
{
    m_countdownEnd = TQDateTime::currentDateTime().addSecs(m_countdownSeconds);
    m_countdownTimer.start(m_countdownSeconds * 1000, true);

    notifyCountdownStarted(getCountdownEnd());

    return true;
}


bool TimeControl::stopCountdown()
{
    m_countdownTimer.stop();
    m_countdownEnd = TQDateTime();

    notifyCountdownStopped();

    return true;
}


TQDateTime TimeControl::getNextAlarmTime() const
{
    const Alarm *a = getNextAlarm();
    if (a)
        return a->nextAlarm();
    else
        return TQDateTime();
}


const Alarm *TimeControl::getNextAlarm () const
{
    TQDateTime now = TQDateTime::currentDateTime(),
              next;

    const Alarm *retval = NULL;

    for (ciAlarmVector i = m_alarms.begin(); i != m_alarms.end(); ++i) {
        TQDateTime n = i->nextAlarm();
        if (n.isValid() && n > now && ( ! next.isValid() || n < next)) {
            next = n;
            retval = &(*i);
        }
    }

    TQDateTime old = m_nextAlarm_tmp;
    m_nextAlarm_tmp = next;
    if (old != m_nextAlarm_tmp) {
        notifyNextAlarmChanged(retval);
    }

    return retval;
}


TQDateTime TimeControl::getCountdownEnd () const
{
    if (m_countdownTimer.isActive())
        return m_countdownEnd;
    else
        return TQDateTime();
}


void TimeControl::slotQTimerCountdownTimeout()
{
    stopCountdown();

    notifyCountdownZero();
}


void TimeControl::slotQTimerAlarmTimeout()
{
    if (m_waitingFor) {
        notifyAlarm(*m_waitingFor);
    }

    TQDateTime now  = TQDateTime::currentDateTime();
    Alarm const *n = getNextAlarm();
    TQDateTime na   = getNextAlarmTime();

    m_waitingFor = NULL;

    if (na.isValid()) {

        int days  = now.daysTo(na);
        int msecs = now.time().msecsTo(na.time());

        if (days > 1) {
            m_alarmTimer.start(24 * 3600 * 1000, true);

        } else if (days >= 0) {

            if (days > 0)
                msecs += days * 24 * 3600 * 1000;

            if (msecs > 0) {
                m_waitingFor = n;
                m_alarmTimer.start(msecs, true);
            }
        }
    }
}


void    TimeControl::restoreState (TDEConfig *config)
{
    AlarmVector al;

    config->setGroup(TQString("timecontrol-") + name());

    int nAlarms = config->readNumEntry ("nAlarms", 0);
    for (int idx = 1; idx <= nAlarms; ++idx) {

        TQString num = TQString().setNum(idx);
        TQDateTime d = config->readDateTimeEntry(AlarmTimeElement       + num);
        bool enable = config->readBoolEntry(AlarmEnabledElement        + num, false);
        bool daily  = config->readBoolEntry(AlarmDailyElement          + num, false);
        int weekdayMask = config->readNumEntry(AlarmWeekdayMaskElement + num, 0x7F);
        float vol   = config->readDoubleNumEntry(AlarmVolumeElement    + num, 1);
        TQString sid = config->readEntry(AlarmStationIDElement          + num, TQString());
        int type    = config->readNumEntry(AlarmTypeElement            + num, 0);

        enable &= d.isValid();

        Alarm a ( d, daily, enable);
        a.setVolumePreset(vol);
        a.setWeekdayMask(weekdayMask);
        a.setStationID(sid);
        a.setAlarmType((Alarm::AlarmType)type);
        al.push_back(a);
    }

    setAlarms(al);
    setCountdownSeconds(config->readNumEntry("countdownSeconds", 30*60));
}


void    TimeControl::saveState    (TDEConfig *config) const
{
    config->setGroup(TQString("timecontrol-") + name());

    config->writeEntry("nAlarms", m_alarms.size());
    int idx = 1;
    ciAlarmVector end = m_alarms.end();
    for (ciAlarmVector i = m_alarms.begin(); i != end; ++i, ++idx) {
        TQString num = TQString().setNum(idx);
        config->writeEntry (AlarmTimeElement      + num, i->alarmTime());
        config->writeEntry (AlarmEnabledElement   + num, i->isEnabled());
        config->writeEntry (AlarmDailyElement     + num, i->isDaily());
        config->writeEntry (AlarmWeekdayMaskElement + num, i->weekdayMask());
        config->writeEntry (AlarmVolumeElement    + num, i->volumePreset());
        config->writeEntry (AlarmStationIDElement + num, i->stationID());
        config->writeEntry (AlarmTypeElement      + num, i->alarmType());
    }

    config->writeEntry("countdownSeconds",  m_countdownSeconds);
}


ConfigPageInfo TimeControl::createConfigurationPage()
{
    TimeControlConfiguration *conf = new TimeControlConfiguration(NULL);
    connectI(conf);
    return ConfigPageInfo (conf, i18n("Alarms"), i18n("Setup Alarms"), "tderadio_kalarm");
}


AboutPageInfo TimeControl::createAboutPage()
{
/*    TDEAboutData aboutData("tderadio",
                         NULL,
                         NULL,
                         I18N_NOOP("Time Control Plugin for TDERadio."
                                   "<P>"
                                   "Provides Alarms and Sleep Countdown"
                                   "<P>"),
                         TDEAboutData::License_GPL,
                         "(c) 2002-2005 Martin Witte, Klas Kalass",
                         0,
                         "http://sourceforge.net/projects/tderadio",
                         0);
    aboutData.addAuthor("Martin Witte",  "", "[email protected]");
    aboutData.addAuthor("Klas Kalass",   "", "[email protected]");

    return AboutPageInfo(
              new TDERadioAboutWidget(aboutData, TDERadioAboutWidget::AbtTabbed),
              i18n("Alarms"),
              i18n("Time Control Plugin"),
              "tderadio_kalarm"
           );*/
    return AboutPageInfo();
}


#include "timecontrol.moc"