-
Notifications
You must be signed in to change notification settings - Fork 1
/
qnewalarmdialog.cpp
431 lines (344 loc) · 11 KB
/
qnewalarmdialog.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <QDateTime>
#include <QFileInfo>
#include <QDesktopWidget>
#include <osso-intl.h>
#include <libalarm.h>
#include <gconfitem.h>
#include "qnewalarmdialog.h"
#include "ui_newalarmdialog.h"
#include "alarmsndpick.h"
#include "utils.h"
/* osso-clock text needed for snooze/stop button translation */
#define TEXTDOMAIN "osso-clock"
#define STOP_BUTTON_KEY "cloc_bd_stop"
#define SNOOZE_BUTTON_KEY "cloc_bd_alarm_notification_snooze"
QString sndFile;
bool dateChoosen = false;
bool fromEdit = false;
QNewAlarmDialog::QNewAlarmDialog(QWidget *parent, bool edit, QString Aname,
const QTime &time, uint32_t wday, bool Acheck, long Acook, bool show):
QDialog(parent),
ui(new Ui::QNewAlarmDialog)
{
time_t nextDate;
GConfItem *item = new GConfItem("/apps/clock/alarm-tone");
setAttribute(Qt::WA_Maemo5AutoOrientation, true);
ui->setupUi(this);
// get the current default alarm tone
sndFile = item->value().toString();
ui->soundButton->setEnabled(false); // <- remove once custom sounds work!
ui->soundButton->setSoundFile(sndFile);
isEditing = edit;
deleted = 1;
realcookie = Acook;
this->time = time;
name = Aname;
this->wday = wday;
enabled = Acheck;
showed = show;
if (edit)
{
setWindowTitle(_("cloc_ti_edit_alarm_title"));
// we need to find it's current alarm tone
alarm_event_t *eve = alarmd_event_get(realcookie);
QString aSnd = QString::fromUtf8(eve->sound);
if (!aSnd.isEmpty()) // a tone is set
{
sndFile = aSnd;
/* why do we need that ? */
aSnd.remove(QRegExp("\\.ogg$|\\.mp3$|\\.aac$"));
aSnd = sndFile;
ui->soundButton->setSoundFile(sndFile);
}
// cleanup memory
alarm_event_delete(eve);
sndFile = aSnd;
}
else
setWindowTitle(_("clock_ti_new_alarm"));
ui->titleEdit->setPlaceholderText(_("cloc_va_placeholder_title"));
ui->daysButton->setDays(wday);
ui->checkBox->setText(_("cloc_fi_active"));
ui->checkBox->setChecked(enabled);
intl("osso-connectivity-ui");
ui->landscapeButtonBox->button(QDialogButtonBox::Retry)->setText(_("conn_bd_devices_delete"));
ui->landscapeButtonBox->button(QDialogButtonBox::Apply)->setText(_("conn_bd_receive_ok"));
ui->portraitButtonBox->button(QDialogButtonBox::Retry)->setText(_("conn_bd_devices_delete"));
ui->portraitButtonBox->button(QDialogButtonBox::Apply)->setText(_("conn_bd_receive_ok"));
intl("osso-clock");
ui->landscapeButtonBox->button(QDialogButtonBox::Retry)->setVisible(edit);
ui->portraitButtonBox->button(QDialogButtonBox::Retry)->setVisible(edit);
// set alarmname
if (!name.isEmpty())
ui->titleEdit->setText(name);
// set time
ui->timeButton->setCurrentTime(time);
if (realcookie && !wday)
{
// get date&time alarm
alarm_event_t *ae = alarmd_event_get(realcookie);
if (ae->snooze_total)
nextDate = ae->snooze_total;
else if (ae->trigger != -1)
nextDate = ae->trigger;
else if (ae->alarm_time != -1)
nextDate = ae->alarm_time;
else
nextDate = QDateTime::currentDateTime().toTime_t();
/* we can't enable an alarm that is in the past */
if (nextDate < (time_t)QDateTime::currentDateTime().toTime_t())
enableSave(false);
alarm_event_delete(ae);
}
else
nextDate = QDateTime::currentDateTime().toTime_t();
ui->dateButton->setCurrentDate(nextDate);
ui->daysButton->setDays(wday);
if (wday)
ui->dateButton->hide();
if ( !isEditing )
ui->checkBox->hide();
connect(QApplication::desktop(), SIGNAL(resized(int)),
this, SLOT(orientationChanged()));
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
connect(ui->timeButton, SIGNAL(selected(QTime)),
this, SLOT(timeChanged(QTime)));
connect(ui->dateButton, SIGNAL(selected(QDate)),
this, SLOT(dateChanged(QDate)));
connect(ui->daysButton, SIGNAL(selected(uint32_t)),
this, SLOT(daysChanged(uint32_t)));
if (!wday)
timer.start(1000);
orientationChanged();
}
QNewAlarmDialog::~QNewAlarmDialog()
{
delete ui;
}
void QNewAlarmDialog::shrink()
{
QApplication::processEvents();
adjustSize();
}
void QNewAlarmDialog::orientationChanged()
{
QRect geom = QApplication::desktop()->screenGeometry();
setUpdatesEnabled(false);
if (geom.width() < geom.height())
{
ui->landscapeButtonBox->hide();
ui->portraitButtonBox->show();
}
else
{
ui->portraitButtonBox->hide();
ui->landscapeButtonBox->show();
}
shrink();
setUpdatesEnabled(true);
}
void QNewAlarmDialog::timeChanged(const QTime &time)
{
// The long date string
if (!alarmDateInFuture())
{
// show if the alarm is still today or else tomorrow
time_t date;
if (time >= QTime::currentTime())
date = QDateTime::currentDateTime().toTime_t();
else
date = QDateTime::currentDateTime().addDays(1).toTime_t();
ui->dateButton->setCurrentDate(date);
enableSave(true);
}
}
void QNewAlarmDialog::dateChanged(const QDate &date)
{
Q_UNUSED(date);
dateChoosen = true;
enabled = true;
ui->checkBox->setChecked(enabled);
enableSaveIfDateInFuture();
}
void QNewAlarmDialog::daysChanged(uint32_t days)
{
wday = days;
if (days)
{
timer.stop();
// a repeating alarm; "active checkbox" and "done" can always be enabled
enableSave(true);
ui->dateButton->hide();
shrink();
}
else
{
timer.start();
ui->dateButton->show();
enableSaveIfDateInFuture();
}
}
void QNewAlarmDialog::on_soundButton_pressed()
{
AlarmSndPick *hw = new AlarmSndPick(this);
hw->exec();
if (hw->selected != "nothing")
{
sndFile = hw->selected;
ui->soundButton->setSoundFile(sndFile);
}
delete hw;
}
void QNewAlarmDialog::on_landscapeButtonBox_clicked(QAbstractButton* button)
{
intl("osso-connectivity-ui");
if ( button->text() == _("conn_bd_devices_delete") )
{
// really remove alarm
deleted = 1;
removeAlarm(realcookie);
realcookie = -1;
}
else
{
deleted = 0;
if ( realcookie != 0 )
alarmd_event_del(realcookie); // remove cookie if exists
addAlarm();
}
intl("osso-clock");
this->accept();
}
void QNewAlarmDialog::on_portraitButtonBox_clicked(QAbstractButton* button)
{
on_landscapeButtonBox_clicked(button);
}
void QNewAlarmDialog::removeAlarm(long cookie)
{
alarmd_event_del(cookie);
}
void QNewAlarmDialog::addAlarm()
{
intl("osso-clock");
name = ui->titleEdit->text();
time = ui->timeButton->currentTime();
enabled = ui->checkBox->isChecked();
alarm_event_t * event = 0;
alarm_action_t * act = 0;
// Setup
event = alarm_event_create(); // a new alarm
// The application id field is needed for every alarm you set
alarm_event_set_alarm_appid(event, "worldclock_alarmd_id");
// Alarm description displayed when the alarm is triggered
QByteArray ba = name.toUtf8();
const char *str1 = ba.data();
alarm_event_set_message(event, str1);
/* Set the textdomain to be used while making
* translation lookups */
alarm_event_set_attr_string(event, "textdomain", TEXTDOMAIN);
/* Set the title of the alarm. */
//alarm_event_set_title(event, "Alarm"); "cloc_ti_alarm_notification_title"
alarm_event_set_alarm_appid(event,"worldclock_alarmd_id");
// Snooze action
act = alarm_event_add_actions(event, 1);
alarm_action_set_label(act, SNOOZE_BUTTON_KEY);
act->flags = ALARM_ACTION_WHEN_RESPONDED;
act->flags |= ALARM_ACTION_TYPE_SNOOZE;
if ( !enabled )
event->flags |= ALARM_EVENT_DISABLED;
event->flags |= ALARM_EVENT_ACTDEAD;
event->flags |= ALARM_EVENT_SHOW_ICON;
event->flags |= ALARM_EVENT_POSTPONE_DELAYED;
intl("osso-clock");
if (wday)
event->flags |= ALARM_EVENT_BACK_RESCHEDULE;
// Acknowledge action
act = alarm_event_add_actions(event, 1);
alarm_action_set_label(act, STOP_BUTTON_KEY);
if(!wday)
{
act->flags |= ALARM_ACTION_WHEN_RESPONDED;
act->flags |= ALARM_ACTION_TYPE_DISABLE;
} else {
act->flags = ALARM_ACTION_WHEN_RESPONDED;
act->flags |= ALARM_ACTION_TYPE_NOP;
}
QDateTime alarmDate;
alarmDate.setTime(ui->timeButton->currentTime());
if (!wday)
{
if (!alarmDateInFuture())
{
event->alarm_time = -1;
event->alarm_tm.tm_hour = alarmDate.time().hour();
event->alarm_tm.tm_min = alarmDate.time().minute();
}
else
{
alarmDate.setDate(ui->dateButton->currentDate());
event->alarm_time = alarmDate.toTime_t();
event->alarm_tm.tm_hour = -1;
event->alarm_tm.tm_min = -1;
}
}
else
{
event->recur_count = -1;
event->recur_secs = 0; // We re not using this way for recurrence
alarm_recur_t* recur = alarm_event_add_recurrences(event, 1);
// Set event time
recur->special = ALARM_RECUR_SPECIAL_NONE;
recur->mask_mon = ALARM_RECUR_MON_ALL;
recur->mask_mday = ALARM_RECUR_MDAY_ALL;
recur->mask_hour = (1ul << alarmDate.time().hour());
recur->mask_min = (1ull << alarmDate.time().minute());
recur->mask_wday = wday;
Q_ASSERT(event->recurrence_cnt == 1);
}
// set the alarm signal
if (sndFile.isEmpty())
{
// noting changed and not a default tone: so get the custom one
GConfItem *item = new GConfItem("/apps/clock/alarm-custom");
sndFile = item->value().toString();
}
alarm_event_set_sound(event, sndFile.toUtf8().constData());
realcookie = alarmd_event_add(event); // add alarm
// cleanup
alarm_event_delete(event);
event = alarmd_event_get(realcookie);
if (enabled)
showAlarmTimeBanner(event->trigger);
alarm_event_delete(event);
dateChoosen = false;
act = 0;
event = 0;
}
void QNewAlarmDialog::timeout()
{
if (wday)
return;
QDate date = ui->dateButton->currentDate();
QTime time = ui->timeButton->currentTime();
/* give it 5 seconds, just in case */
if (QDateTime(date, time) < QDateTime::currentDateTime().addSecs(5))
enableSave(false);
}
void QNewAlarmDialog::enableSave(bool enable)
{
ui->checkBox->setEnabled(enable);
ui->landscapeButtonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
ui->portraitButtonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
}
bool QNewAlarmDialog::enableSaveIfDateInFuture()
{
bool rv = alarmDateInFuture();
enableSave(rv);
return rv;
}
bool QNewAlarmDialog::alarmDateInFuture()
{
QDateTime date(ui->dateButton->currentDate(),
ui->timeButton->currentTime());
return date > QDateTime::currentDateTime() ? true : false;
}