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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
/*
* Copyright (C) 2013 Paolo Borelli <pborelli@gnome.org>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace Clocks {
namespace Alarm {
private struct AlarmTime {
public int hour;
public int minute;
}
private class Item : Object, ContentItem {
const int SNOOZE_MINUTES = 9;
const int RING_MINUTES = 3;
// FIXME: should we add a "MISSED" state where the alarm stopped
// ringing but we keep showing the ringing panel?
public enum State {
READY,
RINGING,
SNOOZING
}
public string title_icon { get; set; default = null; }
public bool selectable { get; set; default = true; }
public bool selected { get; set; default = false; }
public string id { get; construct set; }
public string name {
get {
return _name;
}
set {
_name = value;
setup_bell ();
}
}
public AlarmTime time { get; set; }
public Utils.Weekdays days { get; set; }
public State state { get; private set; }
public string time_label {
owned get {
return Utils.WallClock.get_default ().format_time (alarm_time);
}
}
public string snooze_time_label {
owned get {
return Utils.WallClock.get_default ().format_time (snooze_time);
}
}
public string days_label {
owned get {
return days != null ? days.get_label () : null;
}
}
[CCode (notify = false)]
public bool active {
get {
return _active;
}
set {
if (value != _active) {
_active = value;
if (_active) {
reset ();
} else if (state == State.RINGING) {
stop ();
}
notify_property ("active");
}
}
}
private string _name;
private bool _active;
private GLib.DateTime alarm_time;
private GLib.DateTime snooze_time;
private GLib.DateTime ring_end_time;
private Utils.Bell bell;
private GLib.Notification notification;
public Item (string? id = null) {
var guid = id != null ? id : GLib.DBus.generate_guid ();
Object (id: guid);
}
private void setup_bell () {
bell = new Utils.Bell ("alarm-clock-elapsed");
notification = new GLib.Notification (_("Alarm"));
notification.set_body (name);
notification.add_button (_("Stop"), "app.stop-alarm::".concat (id));
notification.add_button (_("Snooze"), "app.snooze-alarm::".concat (id));
}
public void reset () {
update_alarm_time ();
update_snooze_time (alarm_time);
state = State.READY;
}
private void update_alarm_time () {
var wallclock = Utils.WallClock.get_default ();
var now = wallclock.date_time;
var dt = new GLib.DateTime (wallclock.timezone,
now.get_year (),
now.get_month (),
now.get_day_of_month (),
time.hour,
time.minute,
0);
if (days == null || days.empty) {
// Alarm without days.
if (dt.compare (now) <= 0) {
// Time already passed, ring tomorrow.
dt = dt.add_days (1);
}
} else {
// Alarm with at least one day set.
// Find the next possible day for ringing
while (dt.compare (now) <= 0 || ! days.get ((Utils.Weekdays.Day) (dt.get_day_of_week () -1))) {
dt = dt.add_days (1);
}
}
alarm_time = dt;
}
private void update_snooze_time (GLib.DateTime start_time) {
snooze_time = start_time.add_minutes (SNOOZE_MINUTES);
}
public virtual signal void ring () {
var app = GLib.Application.get_default () as Clocks.Application;
app.send_notification ("alarm-clock-elapsed", notification);
bell.ring ();
}
private void start_ringing (GLib.DateTime now) {
update_snooze_time (now);
ring_end_time = now.add_minutes (RING_MINUTES);
state = State.RINGING;
ring ();
}
public void snooze () {
bell.stop ();
state = State.SNOOZING;
}
public void stop () {
bell.stop ();
update_snooze_time (alarm_time);
state = State.READY;
}
private bool compare_with_item (Item i) {
return (this.alarm_time.compare (i.alarm_time) == 0 && this.active && i.active);
}
public bool check_duplicate_alarm (List<Item> alarms) {
update_alarm_time ();
foreach (var item in alarms) {
if (this.compare_with_item (item)) {
return true;
}
}
return false;
}
// Update the state and ringing time. Ring or stop
// depending on the current time.
// Returns true if the state changed, false otherwise.
public bool tick () {
if (!active) {
return false;
}
State last_state = state;
var wallclock = Utils.WallClock.get_default ();
var now = wallclock.date_time;
if (state == State.RINGING && now.compare (ring_end_time) > 0) {
stop ();
}
if (state == State.SNOOZING && now.compare (snooze_time) > 0) {
start_ringing (now);
}
if (state == State.READY && now.compare (alarm_time) > 0) {
start_ringing (now);
update_alarm_time (); // reschedule for the next repeat
}
return state != last_state;
}
public void serialize (GLib.VariantBuilder builder) {
builder.open (new GLib.VariantType ("a{sv}"));
builder.add ("{sv}", "name", new GLib.Variant.string (name));
builder.add ("{sv}", "id", new GLib.Variant.string (id));
builder.add ("{sv}", "active", new GLib.Variant.boolean (active));
builder.add ("{sv}", "hour", new GLib.Variant.int32 (time.hour));
builder.add ("{sv}", "minute", new GLib.Variant.int32 (time.minute));
builder.add ("{sv}", "days", days.serialize ());
builder.close ();
}
public static ContentItem? deserialize (GLib.Variant alarm_variant) {
string? name = null;
string? id = null;
bool active = true;
int hour = -1;
int minute = -1;
Utils.Weekdays days = null;
foreach (var v in alarm_variant) {
var key = v.get_child_value (0).get_string ();
if (key == "name") {
name = v.get_child_value (1).get_child_value (0).get_string ();
} else if (key == "id") {
id = v.get_child_value (1).get_child_value (0).get_string ();
} else if (key == "active") {
active = v.get_child_value (1).get_child_value (0).get_boolean ();
} else if (key == "hour") {
hour = v.get_child_value (1).get_child_value (0).get_int32 ();
} else if (key == "minute") {
minute = v.get_child_value (1).get_child_value (0).get_int32 ();
} else if (key == "days") {
days = Utils.Weekdays.deserialize (v.get_child_value (1).get_child_value (0));
}
}
if (name != null && hour >= 0 && minute >= 0) {
Item alarm = new Item (id);
alarm.name = name;
alarm.active = active;
alarm.time = { hour, minute };
alarm.days = days;
alarm.reset ();
return alarm;
} else {
warning ("Invalid alarm %s", name != null ? name : "name missing");
}
return null;
}
}
[GtkTemplate (ui = "/org/gnome/clocks/ui/alarmtile.ui")]
private class Tile : Gtk.Grid {
public Item alarm { get; construct set; }
[GtkChild]
private Gtk.Label time_label;
[GtkChild]
private Gtk.Widget name_label;
public Tile (Item alarm) {
Object (alarm: alarm);
alarm.bind_property ("name", name_label, "label", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
alarm.notify["active"].connect (update);
alarm.notify["state"].connect (update);
alarm.notify["time"].connect (update);
alarm.notify["days"].connect (update);
update ();
}
private void update () {
string text, sub_text;
if (alarm.active) {
get_style_context ().add_class ("active");
} else {
get_style_context ().remove_class ("active");
}
if (alarm.state == Item.State.SNOOZING) {
get_style_context ().add_class ("snoozing");
text = alarm.snooze_time_label;
sub_text = "(%s)".printf (alarm.time_label);
} else {
get_style_context ().remove_class ("snoozing");
text = alarm.time_label;
sub_text = alarm.days_label;
}
if (sub_text != null && sub_text != "") {
time_label.label = "%s\n<span size='xx-small'>%s</span>".printf (text, sub_text);
} else {
time_label.label = text;
}
}
}
[GtkTemplate (ui = "/org/gnome/clocks/ui/alarmsetupdialog.ui")]
private class SetupDialog : Gtk.Dialog {
private Utils.WallClock.Format format;
[GtkChild]
private Gtk.Grid time_grid;
[GtkChild]
private Gtk.SpinButton h_spinbutton;
[GtkChild]
private Gtk.SpinButton m_spinbutton;
[GtkChild]
private Gtk.Entry name_entry;
private AmPmToggleButton am_pm_button;
private Gtk.ToggleButton[] day_buttons;
[GtkChild]
private Gtk.Switch active_switch;
[GtkChild]
private Gtk.Box day_buttons_box;
[GtkChild]
private Gtk.Stack am_pm_stack;
[GtkChild]
private Gtk.Revealer label_revealer;
private List<Item> other_alarms;
public SetupDialog (Gtk.Window parent, Item? alarm, ListModel all_alarms) {
Object (transient_for: parent, title: alarm != null ? _("Edit Alarm") : _("New Alarm"), use_header_bar: 1);
other_alarms = new List<Item> ();
var n = all_alarms.get_n_items ();
for (int i = 0; i < n; i++) {
var item = all_alarms.get_object (i) as Item;
if (alarm != item) {
other_alarms.prepend (all_alarms.get_object (i) as Item);
}
}
// Force LTR since we do not want to reverse [hh] : [mm]
time_grid.set_direction (Gtk.TextDirection.LTR);
// Create an array with the weekday buttons with
// day_buttons[0] referencing the button for Monday, and so on.
// Also declare toogled signal connection.
day_buttons = new Gtk.ToggleButton[7];
for (int i = 0; i < 7; i++) {
var button = new Gtk.ToggleButton.with_label (Utils.Weekdays.abbreviation ((Utils.Weekdays.Day) i));
day_buttons[i] = button;
day_buttons[i].toggled.connect (() => {
avoid_duplicate_alarm ();
});
}
// Pack the buttons, starting with the first day of the week
// depending on the locale.
var first_weekday = Utils.Weekdays.get_first_weekday ();
for (int i = 0; i < 7; i++) {
var day_number = (first_weekday + i) % 7;
day_buttons_box.pack_start (day_buttons[day_number]);
}
format = Utils.WallClock.get_default ().format;
am_pm_button = new AmPmToggleButton ();
am_pm_button.clicked.connect (() => {
avoid_duplicate_alarm ();
});
if (format == Utils.WallClock.Format.TWENTYFOUR) {
h_spinbutton.set_range (0, 23);
} else {
h_spinbutton.set_range (1, 12);
am_pm_button.hexpand = false;
am_pm_button.vexpand = false;
am_pm_button.halign = Gtk.Align.CENTER;
am_pm_button.valign = Gtk.Align.CENTER;
am_pm_stack.add (am_pm_button);
am_pm_stack.visible_child = am_pm_button;
}
set_from_alarm (alarm);
}
// Sets up the dialog to show the values of alarm.
public void set_from_alarm (Item? alarm) {
string name;
bool active;
int hour;
int minute;
unowned Utils.Weekdays? days;
if (alarm == null) {
var wc = Utils.WallClock.get_default ();
name = _("Alarm");
hour = wc.date_time.get_hour ();
minute = wc.date_time.get_minute ();
days = null;
active = true;
} else {
name = alarm.name;
hour = alarm.time.hour;
minute = alarm.time.minute;
days = alarm.days;
active = alarm.active;
}
// Set the time.
if (format == Utils.WallClock.Format.TWELVE) {
if (hour < 12) {
am_pm_button.choice = AmPmToggleButton.AmPm.AM;
} else {
am_pm_button.choice = AmPmToggleButton.AmPm.PM;
hour -= 12;
}
if (hour == 0) {
hour = 12;
}
}
h_spinbutton.set_value (hour);
m_spinbutton.set_value (minute);
// Set the name.
name_entry.set_text (name);
// Set the toggle buttons for weekdays.
if (days != null) {
for (int i = 0; i < 7; i++) {
day_buttons[i].active = days.get ((Utils.Weekdays.Day) i);
}
}
// Set On/Off switch.
active_switch.active = active;
}
// Sets alarm according to the current dialog settings.
public void apply_to_alarm (Item alarm) {
var name = name_entry.get_text ();
var active = active_switch.active;
var hour = h_spinbutton.get_value_as_int ();
var minute = m_spinbutton.get_value_as_int ();
if (format == Utils.WallClock.Format.TWELVE) {
var choice = am_pm_button.choice;
if (choice == AmPmToggleButton.AmPm.AM && hour == 12) {
hour = 0;
} else if (choice == AmPmToggleButton.AmPm.PM && hour != 12) {
hour += 12;
}
}
AlarmTime time = { hour, minute };
Utils.Weekdays days = new Utils.Weekdays ();
for (int i = 0; i < 7; i++) {
days.set ((Utils.Weekdays.Day) i, day_buttons[i].active);
}
alarm.freeze_notify ();
alarm.name = name;
alarm.active = active;
alarm.time = time;
alarm.days = days;
// Force update of alarm_time before notifying the changes
alarm.reset ();
alarm.thaw_notify ();
}
private void avoid_duplicate_alarm () {
var alarm = new Item ();
apply_to_alarm (alarm);
var duplicate = alarm.check_duplicate_alarm (other_alarms);
this.set_response_sensitive (1, !duplicate);
label_revealer.set_reveal_child (duplicate);
}
[GtkCallback]
private void entry_changed (Gtk.Editable editable) {
avoid_duplicate_alarm ();
}
[GtkCallback]
private void spinbuttons_changed (Gtk.Editable editable) {
avoid_duplicate_alarm ();
}
[GtkCallback]
private void active_changed () {
avoid_duplicate_alarm ();
}
[GtkCallback]
private bool show_leading_zeros (Gtk.SpinButton spin_button) {
spin_button.set_text ("%02i".printf (spin_button.get_value_as_int ()));
return true;
}
}
[GtkTemplate (ui = "/org/gnome/clocks/ui/alarmringing.ui")]
private class RingingPanel : Gtk.Grid {
public Item alarm {
get {
return _alarm;
}
set {
if (_alarm != null) {
_alarm.disconnect (alarm_state_handler);
}
_alarm = value;
if (_alarm != null) {
alarm_state_handler = _alarm.notify["state"].connect (() => {
if (alarm.state != Item.State.RINGING) {
dismiss ();
}
});
}
}
}
private Item? _alarm;
private ulong alarm_state_handler;
[GtkChild]
private Gtk.Label time_label;
[GtkCallback]
private void stop_clicked () {
alarm.stop ();
}
[GtkCallback]
private void snooze_clicked () {
alarm.snooze ();
}
public virtual signal void dismiss () {
alarm = null;
}
public void update () {
if (alarm != null) {
if (alarm.state == Item.State.SNOOZING) {
time_label.label = alarm.snooze_time_label;
} else {
time_label.label = alarm.time_label;
}
}
}
}
[GtkTemplate (ui = "/org/gnome/clocks/ui/alarm.ui")]
public class Face : Gtk.Stack, Clocks.Clock {
public string label { get; construct set; }
public HeaderBar header_bar { get; construct set; }
public PanelId panel_id { get; construct set; }
private ContentStore alarms;
private GLib.Settings settings;
private Gtk.Button new_button;
[GtkChild]
private Gtk.Widget empty_view;
[GtkChild]
private ContentView content_view;
[GtkChild]
private RingingPanel ringing_panel;
public Face (HeaderBar header_bar) {
Object (label: _("Alarm"),
header_bar: header_bar,
panel_id: PanelId.ALARM);
alarms = new ContentStore ();
settings = new GLib.Settings ("org.gnome.clocks");
var app = GLib.Application.get_default ();
var action = app.lookup_action ("stop-alarm");
((GLib.SimpleAction)action).activate.connect ((action, param) => {
var a = (Item)alarms.find ((a) => {
return ((Item)a).id == param.get_string ();
});
if (a != null) {
a.stop ();
}
});
action = app.lookup_action ("snooze-alarm");
((GLib.SimpleAction)action).activate.connect ((action, param) => {
var a = (Item)alarms.find ((a) => {
return ((Item)a).id == param.get_string ();
});
if (a != null) {
a.snooze ();
}
});
// Translators: "New" refers to an alarm
new_button = new Gtk.Button.with_label (C_("Alarm", "New"));
new_button.valign = Gtk.Align.CENTER;
new_button.no_show_all = true;
new_button.action_name = "win.new";
header_bar.pack_start (new_button);
content_view.bind_model (alarms, (item) => {
return new Tile ((Item)item);
});
content_view.set_header_bar (header_bar);
load ();
show_all ();
alarms.items_changed.connect ((position, removed, added) => {
save ();
reset_view ();
});
reset_view ();
// Start ticking...
Utils.WallClock.get_default ().tick.connect (() => {
alarms.foreach ((i) => {
var a = (Item)i;
if (a.tick ()) {
if (a.state == Item.State.RINGING) {
show_ringing_panel (a);
ring ();
} else if (ringing_panel.alarm == a) {
ringing_panel.update ();
}
}
});
});
}
public signal void ring ();
[GtkCallback]
private void item_activated (ContentItem item) {
Item alarm = (Item) item;
if (alarm.state == Item.State.SNOOZING) {
show_ringing_panel (alarm);
} else {
edit (alarm);
}
}
[GtkCallback]
private void dismiss_ringing_panel () {
reset_view ();
}
[GtkCallback]
private void visible_child_changed () {
if (visible_child == empty_view || visible_child == content_view) {
header_bar.mode = HeaderBar.Mode.NORMAL;
} else if (visible_child == ringing_panel) {
header_bar.mode = HeaderBar.Mode.STANDALONE;
}
}
private void load () {
alarms.deserialize (settings.get_value ("alarms"), Item.deserialize);
}
private void save () {
settings.set_value ("alarms", alarms.serialize ());
}
private void edit (Item alarm) {
var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), alarm, alarms);
// Disable alarm while editing it and remember the original active state.
var saved_active = alarm.active;
alarm.active = false;
dialog.response.connect ((dialog, response) => {
if (response == 1) {
((SetupDialog) dialog).apply_to_alarm (alarm);
save ();
} else {
alarm.active = saved_active;
}
dialog.destroy ();
});
dialog.show_all ();
}
private void show_ringing_panel (Item alarm) {
ringing_panel.alarm = alarm;
ringing_panel.update ();
visible_child = ringing_panel;
}
private void reset_view () {
visible_child = alarms.get_n_items () == 0 ? empty_view : content_view;
request_header_bar_update ();
}
public void activate_new () {
var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), null, alarms);
dialog.response.connect ((dialog, response) => {
if (response == 1) {
var alarm = new Item ();
((SetupDialog) dialog).apply_to_alarm (alarm);
alarms.add (alarm);
save ();
}
dialog.destroy ();
});
dialog.show_all ();
}
public void activate_select_all () {
content_view.select_all ();
}
public void activate_select_none () {
content_view.unselect_all ();
}
public bool escape_pressed () {
return content_view.escape_pressed ();
}
public void update_header_bar () {
switch (header_bar.mode) {
case HeaderBar.Mode.NORMAL:
new_button.show ();
content_view.update_header_bar ();
break;
case HeaderBar.Mode.SELECTION:
content_view.update_header_bar ();
break;
case HeaderBar.Mode.STANDALONE:
header_bar.title = ringing_panel.alarm.name;
break;
default:
assert_not_reached ();
}
}
}
} // namespace Alarm
} // namespace Clocks
|