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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
|
/*
* Copyright 2013 Canonical Ltd.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
* Ted Gould <ted@canonical.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h> /* strstr() */
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <url-dispatcher.h>
#include "dbus-shared.h"
#include "planner-eds.h"
#include "timezone-file.h"
#include "timezone-geoclue.h"
#include "service.h"
#include "settings-shared.h"
#include "utils.h"
#define SKEW_CHECK_INTERVAL_SEC 10
#define SKEW_DIFF_THRESHOLD_USEC ((SKEW_CHECK_INTERVAL_SEC+5) * G_USEC_PER_SEC)
G_DEFINE_TYPE (IndicatorDatetimeService,
indicator_datetime_service,
G_TYPE_OBJECT)
enum
{
SIGNAL_NAME_LOST,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum
{
SECTION_HEADER = (1<<0),
SECTION_CALENDAR = (1<<1),
SECTION_APPOINTMENTS = (1<<2),
SECTION_LOCATIONS = (1<<3),
SECTION_SETTINGS = (1<<4),
};
enum
{
PROFILE_PHONE,
PROFILE_DESKTOP,
PROFILE_GREETER,
N_PROFILES
};
static const char * const menu_names[N_PROFILES] =
{
"phone",
"desktop",
"desktop_greeter"
};
struct ProfileMenuInfo
{
/* the root level -- the header is the only child of this */
GMenu * menu;
/* parent of the sections. This is the header's submenu */
GMenu * submenu;
guint export_id;
};
struct _IndicatorDatetimeServicePrivate
{
GCancellable * cancellable;
GSettings * settings;
IndicatorDatetimeTimezone * tz_file;
IndicatorDatetimeTimezone * tz_geoclue;
IndicatorDatetimePlanner * planner;
guint own_id;
guint actions_export_id;
GDBusConnection * conn;
guint rebuild_id;
int rebuild_flags;
struct ProfileMenuInfo menus[N_PROFILES];
GDateTime * skew_time;
guint skew_timer;
guint header_timer;
guint timezone_timer;
/* Which year/month to show in the calendar,
and which day should get the cursor.
This value is reflected in the calendar action's state */
GDateTime * calendar_date;
GSimpleActionGroup * actions;
GSimpleAction * phone_header_action;
GSimpleAction * desktop_header_action;
GSimpleAction * calendar_action;
GDBusProxy * login1_manager;
/* all the appointments in the selected calendar_date's month.
Used when populating the 'appointment-days' entry in
create_calendar_state() */
GSList * calendar_appointments;
/* appointments over the next few weeks.
Used when building SECTION_APPOINTMENTS */
GSList * upcoming_appointments;
};
typedef IndicatorDatetimeServicePrivate priv_t;
/***
****
***/
static void
indicator_clear_timer (guint * tag)
{
if (*tag)
{
g_source_remove (*tag);
*tag = 0;
}
}
/***
****
***/
static void rebuild_now (IndicatorDatetimeService * self, int section);
static void rebuild_soon (IndicatorDatetimeService * self, int section);
static inline void
rebuild_header_soon (IndicatorDatetimeService * self)
{
rebuild_soon (self, SECTION_HEADER);
}
static inline void
rebuild_calendar_section_soon (IndicatorDatetimeService * self)
{
rebuild_soon (self, SECTION_CALENDAR);
}
static inline void
rebuild_appointments_section_soon (IndicatorDatetimeService * self)
{
rebuild_soon (self, SECTION_APPOINTMENTS);
}
static inline void
rebuild_locations_section_soon (IndicatorDatetimeService * self)
{
rebuild_soon (self, SECTION_LOCATIONS);
}
static inline void
rebuild_settings_section_soon (IndicatorDatetimeService * self)
{
rebuild_soon (self, SECTION_SETTINGS);
}
/***
**** TIMEZONE TIMER
***/
/*
* Periodically rebuild the sections that have time format strings
* that are dependent on the current time:
*
* 1. appointment menuitems' time format strings depend on the
* current time; for example, they don't show the day of week
* if the appointment is today.
*
* 2. location menuitems' time format strings depend on the
* current time; for example, they don't show the day of the week
* if the local date and location date are the same.
*
* 3. the "local date" menuitem in the calendar section is,
* obviously, dependent on the local time.
*
* In short, we want to update whenever the number of days between two zone
* might have changed. We do that by updating when either zone's day changes.
*
* Since not all UTC offsets are evenly divisible by hours
* (examples: Newfoundland UTC-03:30, Nepal UTC+05:45), refreshing on the hour
* is not enough. We need to refresh at HH:00, HH:15, HH:30, and HH:45.
*/
static guint
calculate_seconds_until_next_fifteen_minutes (GDateTime * now)
{
char * str;
gint minute;
guint seconds;
GTimeSpan diff;
GDateTime * next;
GDateTime * start_of_next;
minute = g_date_time_get_minute (now);
minute = 15 - (minute % 15);
next = g_date_time_add_minutes (now, minute);
start_of_next = g_date_time_new_local (g_date_time_get_year (next),
g_date_time_get_month (next),
g_date_time_get_day_of_month (next),
g_date_time_get_hour (next),
g_date_time_get_minute (next),
0.1);
str = g_date_time_format (start_of_next, "%F %T");
g_debug ("%s %s the next timestamp rebuild will be at %s", G_STRLOC, G_STRFUNC, str);
g_free (str);
diff = g_date_time_difference (start_of_next, now);
seconds = (diff + (G_TIME_SPAN_SECOND-1)) / G_TIME_SPAN_SECOND;
g_date_time_unref (start_of_next);
g_date_time_unref (next);
return seconds;
}
static void start_timezone_timer (IndicatorDatetimeService * self);
static gboolean
on_timezone_timer (gpointer gself)
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
rebuild_soon (self, SECTION_CALENDAR |
SECTION_APPOINTMENTS |
SECTION_LOCATIONS);
/* Restarting the timer to recalculate the interval. This helps us to hit
our marks despite clock skew, suspend+resume, leap seconds, etc */
start_timezone_timer (self);
return G_SOURCE_REMOVE;
}
static void
start_timezone_timer (IndicatorDatetimeService * self)
{
GDateTime * now;
guint seconds;
priv_t * p = self->priv;
indicator_clear_timer (&p->timezone_timer);
now = indicator_datetime_service_get_localtime (self);
seconds = calculate_seconds_until_next_fifteen_minutes (now);
p->timezone_timer = g_timeout_add_seconds (seconds, on_timezone_timer, self);
g_date_time_unref (now);
}
/***
**** HEADER TIMER
***/
/*
* This is to periodically rebuild the header's action's state.
*
* If the label shows seconds, update when we reach the next second.
* Otherwise, update when we reach the next minute.
*/
static guint
calculate_milliseconds_until_next_minute (GDateTime * now)
{
GDateTime * next;
GDateTime * start_of_next;
GTimeSpan interval_usec;
guint interval_msec;
next = g_date_time_add_minutes (now, 1);
start_of_next = g_date_time_new_local (g_date_time_get_year (next),
g_date_time_get_month (next),
g_date_time_get_day_of_month (next),
g_date_time_get_hour (next),
g_date_time_get_minute (next),
0.1);
interval_usec = g_date_time_difference (start_of_next, now);
interval_msec = (interval_usec + 999) / 1000;
g_date_time_unref (start_of_next);
g_date_time_unref (next);
return interval_msec;
}
static gint
calculate_milliseconds_until_next_second (GDateTime * now)
{
gint interval_usec;
guint interval_msec;
interval_usec = G_USEC_PER_SEC - g_date_time_get_microsecond (now);
interval_msec = (interval_usec + 999) / 1000;
return interval_msec;
}
static void start_header_timer (IndicatorDatetimeService * self);
static gboolean
on_header_timer (gpointer gself)
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
rebuild_now (self, SECTION_HEADER);
/* Restarting the timer to recalculate the interval. This helps us to hit
our marks despite clock skew, suspend+resume, leap seconds, etc */
start_header_timer (self);
return G_SOURCE_REMOVE;
}
static char * get_header_label_format_string (IndicatorDatetimeService *);
static void
start_header_timer (IndicatorDatetimeService * self)
{
guint interval_msec;
gboolean header_shows_seconds = FALSE;
priv_t * p = self->priv;
GDateTime * now = indicator_datetime_service_get_localtime (self);
indicator_clear_timer (&p->header_timer);
if (g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CLOCK_S))
{
char * fmt = get_header_label_format_string (self);
header_shows_seconds = fmt && (strstr(fmt,"%s") || strstr(fmt,"%S") ||
strstr(fmt,"%T") || strstr(fmt,"%X") ||
strstr(fmt,"%c"));
g_free (fmt);
}
if (header_shows_seconds)
interval_msec = calculate_milliseconds_until_next_second (now);
else
interval_msec = calculate_milliseconds_until_next_minute (now);
interval_msec += 50; /* add a small margin to ensure the callback
fires /after/ next is reached */
p->header_timer = g_timeout_add_full (G_PRIORITY_HIGH,
interval_msec,
on_header_timer,
self,
NULL);
g_date_time_unref (now);
}
/**
* General purpose handler for rebuilding sections and restarting their timers
* when time jumps for whatever reason:
*
* - clock skew
* - laptop suspend + resume
* - geoclue detects that we've changed timezones
* - Unity is running inside a TARDIS
*/
static void
on_local_time_jumped (IndicatorDatetimeService * self)
{
g_debug ("%s %s", G_STRLOC, G_STRFUNC);
/* these calls accomplish two things:
1. rebuild the necessary states / menuitems when time jumps
2. restart the timers so their new wait interval is correct */
on_header_timer (self);
on_timezone_timer (self);
}
static gboolean
skew_timer_func (gpointer gself)
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
GDateTime * now = indicator_datetime_service_get_localtime (self);
priv_t * p = self->priv;
/* check for clock skew: has too much time passed since the last check? */
if (p->skew_time != NULL)
{
const GTimeSpan diff = g_date_time_difference (now, p->skew_time);
if (diff > SKEW_DIFF_THRESHOLD_USEC)
on_local_time_jumped (self);
}
g_clear_pointer (&p->skew_time, g_date_time_unref);
p->skew_time = now;
return G_SOURCE_CONTINUE;
}
/***
****
**** HEADER SECTION
****
***/
static gchar *
get_header_label_format_string (IndicatorDatetimeService * self)
{
char * fmt;
GSettings * s = self->priv->settings;
const TimeFormatMode mode = g_settings_get_enum (s, SETTINGS_TIME_FORMAT_S);
if (mode == TIME_FORMAT_MODE_CUSTOM)
{
fmt = g_settings_get_string (s, SETTINGS_CUSTOM_TIME_FORMAT_S);
}
else
{
gboolean show_day = g_settings_get_boolean (s, SETTINGS_SHOW_DAY_S);
gboolean show_date = g_settings_get_boolean (s, SETTINGS_SHOW_DATE_S);
fmt = generate_full_format_string (show_day, show_date, s);
}
return fmt;
}
static GVariant *
create_desktop_header_state (IndicatorDatetimeService * self)
{
GVariantBuilder b;
gchar * fmt;
gchar * str;
gboolean visible;
GDateTime * now;
const gchar * title = _("Date and Time");
visible = g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CLOCK_S);
/* build the time string for the label & a11y */
fmt = get_header_label_format_string (self);
now = indicator_datetime_service_get_localtime (self);
str = g_date_time_format (now, fmt);
if (str == NULL)
{
str = g_strdup (_("Unsupported date format"));
g_warning ("%s", str);
}
g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add (&b, "{sv}", "accessible-desc", g_variant_new_string (str));
g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (str));
g_variant_builder_add (&b, "{sv}", "title", g_variant_new_string (title));
g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (visible));
/* cleanup */
g_date_time_unref (now);
g_free (fmt);
return g_variant_builder_end (&b);
}
static gboolean
service_has_alarms (IndicatorDatetimeService * self);
static GVariant *
create_phone_header_state (IndicatorDatetimeService * self)
{
GVariantBuilder b;
GDateTime * now;
const gchar * fmt;
gchar * label;
gboolean has_alarms;
gchar * a11y;
gchar * title;
g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT);
/* label */
now = indicator_datetime_service_get_localtime (self);
fmt = get_terse_time_format_string (now);
label = g_date_time_format (now, fmt);
/* icon */
if ((has_alarms = service_has_alarms (self)))
{
GIcon * icon;
icon = g_themed_icon_new_with_default_fallbacks ("alarm-symbolic");
g_variant_builder_add (&b, "{sv}", "icon", g_icon_serialize (icon));
g_object_unref (icon);
}
/* a11y */
if (has_alarms)
a11y = g_strdup_printf (_("%s (has alarms)"), label);
else
a11y = g_strdup (label);
g_variant_builder_add (&b, "{sv}", "accessible-desc",
g_variant_new_take_string (a11y));
g_variant_builder_add (&b, "{sv}", "visible", g_variant_new_boolean (TRUE));
g_variant_builder_add (&b, "{sv}", "label", g_variant_new_take_string (label));
/* title is day-of-week */
title = g_date_time_format (now, _("%A"));
g_variant_builder_add (&b, "{sv}", "title", g_variant_new_take_string (title));
/* cleanup */
g_date_time_unref (now);
return g_variant_builder_end (&b);
}
/***
****
**** CALENDAR SECTION
****
***/
static GDateTime *
get_calendar_date (IndicatorDatetimeService * self)
{
GDateTime * date;
priv_t * p = self->priv;
if (p->calendar_date)
date = g_date_time_ref (p->calendar_date);
else
date = indicator_datetime_service_get_localtime (self);
return date;
}
static GVariant *
create_calendar_state (IndicatorDatetimeService * self)
{
guint i;
const char * key;
gboolean days[32] = { 0 };
GVariantBuilder dict_builder;
GVariantBuilder day_builder;
GDateTime * date;
GSList * l;
gboolean b;
priv_t * p = self->priv;
g_variant_builder_init (&dict_builder, G_VARIANT_TYPE_DICTIONARY);
key = "appointment-days";
for (l=p->calendar_appointments; l!=NULL; l=l->next)
{
const struct IndicatorDatetimeAppt * appt = l->data;
days[g_date_time_get_day_of_month (appt->begin)] = TRUE;
}
g_variant_builder_init (&day_builder, G_VARIANT_TYPE("ai"));
for (i=0; i<G_N_ELEMENTS(days); i++)
if (days[i])
g_variant_builder_add (&day_builder, "i", i);
g_variant_builder_add (&dict_builder, "{sv}", key,
g_variant_builder_end (&day_builder));
key = "calendar-day";
date = get_calendar_date (self);
g_variant_builder_add (&dict_builder, "{sv}", key,
g_variant_new_int64 (g_date_time_to_unix (date)));
g_date_time_unref (date);
key = "show-week-numbers";
b = g_settings_get_boolean (p->settings, SETTINGS_SHOW_WEEK_NUMBERS_S);
g_variant_builder_add (&dict_builder, "{sv}", key, g_variant_new_boolean (b));
return g_variant_builder_end (&dict_builder);
}
static void
update_calendar_action_state (IndicatorDatetimeService * self)
{
g_simple_action_set_state (self->priv->calendar_action,
create_calendar_state (self));
}
static void
add_localtime_menuitem (GMenu * menu,
IndicatorDatetimeService * self,
const char * time_format,
const char * icon_name)
{
GDateTime * now;
char * label;
GMenuItem * menu_item;
now = indicator_datetime_service_get_localtime (self);
label = g_date_time_format (now, time_format);
menu_item = g_menu_item_new (label, NULL);
if (icon_name && *icon_name)
g_menu_item_set_attribute (menu_item, G_MENU_ATTRIBUTE_ICON, "s", icon_name);
g_menu_item_set_action_and_target_value (menu_item, "indicator.activate-planner", g_variant_new_int64(0));
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
g_free (label);
g_date_time_unref (now);
}
static void
add_calendar_menuitem (GMenu * menu)
{
GMenuItem * menu_item;
menu_item = g_menu_item_new ("[calendar]", NULL);
g_menu_item_set_action_and_target_value (menu_item, "indicator.calendar", g_variant_new_int64(0));
g_menu_item_set_attribute (menu_item, "x-canonical-type", "s", "com.canonical.indicator.calendar");
g_menu_item_set_attribute (menu_item, "activation-action", "s", "indicator.activate-planner");
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
}
static GMenuModel *
create_desktop_calendar_section (IndicatorDatetimeService * self)
{
GMenu * menu = g_menu_new ();
/* strftime(3) format string to show the day of the week and the date */
add_localtime_menuitem (menu, self, _("%A, %e %B %Y"), "calendar");
if (g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_CALENDAR_S))
add_calendar_menuitem (menu);
return G_MENU_MODEL (menu);
}
static GMenuModel *
create_phone_calendar_section (IndicatorDatetimeService * self)
{
GMenu * menu = g_menu_new ();
/* strftime(3) format string to show date */
add_localtime_menuitem (menu, self, _("%e %B %Y"), "calendar");
return G_MENU_MODEL (menu);
}
/***
****
**** APPOINTMENTS SECTION
****
***/
static gboolean
service_has_alarms (IndicatorDatetimeService * self)
{
gboolean has_alarms = FALSE;
GSList * appts;
GSList * l;
appts = self->priv->upcoming_appointments;
for (l=appts; l!=NULL; l=l->next)
{
struct IndicatorDatetimeAppt * appt = l->data;
if ((has_alarms = appt->has_alarms))
break;
}
return has_alarms;
}
static char *
get_appointment_time_format (struct IndicatorDatetimeAppt * appt,
GDateTime * now,
GSettings * settings,
gboolean terse)
{
char * fmt;
gboolean full_day = g_date_time_difference (appt->end, appt->begin) == G_TIME_SPAN_DAY;
if (appt->is_daily)
{
const char * time_fmt = terse ? get_terse_time_format_string (appt->begin)
: get_full_time_format_string (settings);
fmt = join_date_and_time_format_strings (_("Daily"), time_fmt);
}
else if (full_day)
{
/* TRANSLATORS: a strftime(3) format showing full day events.
* "%A" means a full text day (Wednesday), "%a" means abbreviated (Wed). */
fmt = g_strdup (_("%A"));
}
else
{
fmt = terse ? generate_terse_format_string_at_time (now, appt->begin)
: generate_full_format_string_at_time (now, appt->begin, settings);
}
return fmt;
}
static void
add_appointments (IndicatorDatetimeService * self, GMenu * menu, gboolean terse)
{
const int MAX_APPTS = 5;
GDateTime * now = indicator_datetime_service_get_localtime (self);
GSList * appts;
GSList * l;
int i;
/* build appointment menuitems */
appts = self->priv->upcoming_appointments;
for (l=appts, i=0; l!=NULL && i<MAX_APPTS; l=l->next, i++)
{
struct IndicatorDatetimeAppt * appt = l->data;
char * fmt = get_appointment_time_format (appt, now, self->priv->settings, terse);
const gint64 unix_time = g_date_time_to_unix (appt->begin);
GMenuItem * menu_item;
menu_item = g_menu_item_new (appt->summary, NULL);
if (appt->color && !appt->has_alarms)
g_menu_item_set_attribute (menu_item, "x-canonical-color",
"s", appt->color);
g_menu_item_set_attribute (menu_item, "x-canonical-time",
"x", unix_time);
g_menu_item_set_attribute (menu_item, "x-canonical-time-format",
"s", fmt);
g_menu_item_set_attribute (menu_item, "x-canonical-type",
"s", appt->has_alarms ? "com.canonical.indicator.alarm"
: "com.canonical.indicator.appointment");
g_menu_item_set_action_and_target_value (menu_item,
"indicator.activate-planner",
g_variant_new_int64 (unix_time));
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
g_free (fmt);
}
/* cleanup */
g_date_time_unref (now);
}
static GMenuModel *
create_phone_appointments_section (IndicatorDatetimeService * self)
{
GMenu * menu = g_menu_new ();
GMenuItem * menu_item;
menu_item = g_menu_item_new (_("Clock"), NULL);
g_menu_item_set_attribute (menu_item, G_MENU_ATTRIBUTE_ICON, "s", "clock");
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
add_appointments (self, menu, TRUE);
return G_MENU_MODEL (menu);
}
static GMenuModel *
create_desktop_appointments_section (IndicatorDatetimeService * self)
{
GMenu * menu = g_menu_new ();
if (g_settings_get_boolean (self->priv->settings, SETTINGS_SHOW_EVENTS_S))
{
GMenuItem * menu_item;
add_appointments (self, menu, FALSE);
/* add the 'Add Event…' menuitem */
menu_item = g_menu_item_new (_("Add Event…"), NULL);
g_menu_item_set_action_and_target_value (menu_item,
"indicator.activate-planner",
g_variant_new_int64 (0));
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
}
return G_MENU_MODEL (menu);
}
/***
****
**** LOCATIONS SECTION
****
***/
static void
on_current_timezone_changed (IndicatorDatetimeService * self)
{
on_local_time_jumped (self);
}
/* When the 'auto-detect timezone' boolean setting changes,
start or stop watching geoclue and /etc/timezone */
static void
set_detect_location_enabled (IndicatorDatetimeService * self, gboolean enabled)
{
gboolean changed = FALSE;
priv_t * p = self->priv;
/* geoclue */
if (!p->tz_geoclue && enabled)
{
p->tz_geoclue = indicator_datetime_timezone_geoclue_new ();
g_signal_connect_swapped (p->tz_geoclue, "notify::timezone",
G_CALLBACK(on_current_timezone_changed),
self);
changed = TRUE;
}
else if (p->tz_geoclue && !enabled)
{
g_signal_handlers_disconnect_by_func (p->tz_geoclue,
on_current_timezone_changed,
self);
g_clear_object (&p->tz_geoclue);
changed = TRUE;
}
/* timezone file */
if (!p->tz_file && enabled)
{
p->tz_file = indicator_datetime_timezone_file_new (TIMEZONE_FILE);
g_signal_connect_swapped (p->tz_file, "notify::timezone",
G_CALLBACK(on_current_timezone_changed),
self);
changed = TRUE;
}
else if (p->tz_file && !enabled)
{
g_signal_handlers_disconnect_by_func (p->tz_file,
on_current_timezone_changed,
self);
g_clear_object (&p->tz_file);
changed = TRUE;
}
if (changed)
on_current_timezone_changed (self);
}
/* A temp struct used by create_locations_section()
for pruning duplicates and sorting. */
struct TimeLocation
{
GTimeSpan offset;
gchar * zone;
gchar * name;
gboolean visible;
GDateTime * local_time;
};
static void
time_location_free (struct TimeLocation * loc)
{
g_date_time_unref (loc->local_time);
g_free (loc->name);
g_free (loc->zone);
g_slice_free (struct TimeLocation, loc);
}
static struct TimeLocation*
time_location_new (const char * zone,
const char * name,
gboolean visible)
{
struct TimeLocation * loc = g_slice_new (struct TimeLocation);
GTimeZone * tz = g_time_zone_new (zone);
loc->zone = g_strdup (zone);
loc->name = g_strdup (name);
loc->visible = visible;
loc->local_time = g_date_time_new_now (tz);
loc->offset = g_date_time_get_utc_offset (loc->local_time);
g_time_zone_unref (tz);
return loc;
}
static int
time_location_compare (const struct TimeLocation * a,
const struct TimeLocation * b)
{
int ret = 0;
if (!ret && (a->offset != b->offset)) /* primary key */
ret = (a->offset < b->offset) ? -1 : 1;
if (!ret)
ret = g_strcmp0 (a->name, b->name); /* secondary key */
if (!ret)
ret = a->visible - b->visible; /* tertiary key */
return ret;
}
static GSList*
locations_add (GSList * locations,
const char * zone,
const char * name,
gboolean visible)
{
struct TimeLocation * loc = time_location_new (zone, name, visible);
if (g_slist_find_custom (locations, loc, (GCompareFunc)time_location_compare))
{
g_debug("%s Skipping duplicate zone '%s' name '%s'", G_STRLOC, zone, name);
time_location_free (loc);
}
else
{
g_debug ("%s Adding zone '%s', name '%s'", G_STRLOC, zone, name);
locations = g_slist_append (locations, loc);
}
return locations;
}
static GMenuModel *
create_locations_section (IndicatorDatetimeService * self)
{
guint i;
GMenu * menu;
GSList * l;
GSList * locations = NULL;
gchar ** user_locations;
gboolean visible;
IndicatorDatetimeTimezone * detected_timezones[2];
priv_t * p = self->priv;
GDateTime * now = indicator_datetime_service_get_localtime (self);
set_detect_location_enabled (self,
g_settings_get_boolean (p->settings, SETTINGS_SHOW_DETECTED_S));
menu = g_menu_new ();
/***
**** Build a list of locations to add: use geo_timezone,
**** current_timezone, and SETTINGS_LOCATIONS_S, but omit duplicates.
***/
/* maybe add the auto-detected timezones */
detected_timezones[0] = p->tz_geoclue;
detected_timezones[1] = p->tz_file;
visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_DETECTED_S);
for (i=0; i<G_N_ELEMENTS(detected_timezones); i++)
{
if (detected_timezones[i] != NULL)
{
const char * tz = indicator_datetime_timezone_get_timezone (detected_timezones[i]);
if (tz && *tz)
{
gchar * name = get_current_zone_name (tz, p->settings);
locations = locations_add (locations, tz, name, visible);
g_free (name);
}
}
}
/* maybe add the user-specified locations */
user_locations = g_settings_get_strv (p->settings, SETTINGS_LOCATIONS_S);
if (user_locations != NULL)
{
visible = g_settings_get_boolean (p->settings, SETTINGS_SHOW_LOCATIONS_S);
for (i=0; user_locations[i] != NULL; i++)
{
gchar * zone;
gchar * name;
split_settings_location (user_locations[i], &zone, &name);
locations = locations_add (locations, zone, name, visible);
g_free (name);
g_free (zone);
}
g_strfreev (user_locations);
user_locations = NULL;
}
/* now build menuitems for all the locations */
for (l=locations; l!=NULL; l=l->next)
{
struct TimeLocation * loc = l->data;
if (loc->visible)
{
char * detailed_action;
char * fmt;
GMenuItem * menu_item;
detailed_action = g_strdup_printf ("indicator.set-location::%s %s",
loc->zone,
loc->name);
fmt = generate_full_format_string_at_time (now, loc->local_time, p->settings);
menu_item = g_menu_item_new (loc->name, detailed_action);
g_menu_item_set_attribute (menu_item, "x-canonical-type",
"s", "com.canonical.indicator.location");
g_menu_item_set_attribute (menu_item, "x-canonical-timezone",
"s", loc->zone);
g_menu_item_set_attribute (menu_item, "x-canonical-time-format",
"s", fmt);
g_menu_append_item (menu, menu_item);
g_object_unref (menu_item);
g_free (fmt);
g_free (detailed_action);
}
}
g_date_time_unref (now);
g_slist_free_full (locations, (GDestroyNotify)time_location_free);
return G_MENU_MODEL (menu);
}
/***
**** SET LOCATION
***/
struct setlocation_data
{
IndicatorDatetimeService * service;
char * timezone_id;
char * name;
};
static void
setlocation_data_free (struct setlocation_data * data)
{
g_free (data->timezone_id);
g_free (data->name);
g_slice_free (struct setlocation_data, data);
}
static void
on_datetime1_set_timezone_response (GObject * object,
GAsyncResult * res,
gpointer gdata)
{
GError * err;
GVariant * answers;
struct setlocation_data * data = gdata;
err = NULL;
answers = g_dbus_proxy_call_finish (G_DBUS_PROXY(object), res, &err);
if (err != NULL)
{
if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Could not set new timezone: %s", err->message);
g_error_free (err);
}
else
{
char * timezone_name = g_strdup_printf ("%s %s",
data->timezone_id,
data->name);
g_settings_set_string (data->service->priv->settings,
SETTINGS_TIMEZONE_NAME_S,
timezone_name);
g_free (timezone_name);
g_variant_unref (answers);
}
setlocation_data_free (data);
}
static void
on_datetime1_proxy_ready (GObject * object G_GNUC_UNUSED,
GAsyncResult * res,
gpointer gdata)
{
GError * err;
GDBusProxy * proxy;
struct setlocation_data * data = gdata;
err = NULL;
proxy = g_dbus_proxy_new_for_bus_finish (res, &err);
if (err != NULL)
{
if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Could not grab DBus proxy for timedated: %s", err->message);
g_error_free (err);
setlocation_data_free (data);
}
else
{
g_dbus_proxy_call (proxy,
"SetTimezone",
g_variant_new ("(sb)", data->timezone_id, TRUE),
G_DBUS_CALL_FLAGS_NONE,
-1,
data->service->priv->cancellable,
on_datetime1_set_timezone_response,
data);
g_object_unref (proxy);
}
}
static void
indicator_datetime_service_set_location (IndicatorDatetimeService * self,
const char * timezone_id,
const char * name)
{
priv_t * p = self->priv;
struct setlocation_data * data;
g_return_if_fail (INDICATOR_IS_DATETIME_SERVICE (self));
g_return_if_fail (name && *name);
g_return_if_fail (timezone_id && *timezone_id);
data = g_slice_new0 (struct setlocation_data);
data->timezone_id = g_strdup (timezone_id);
data->name = g_strdup (name);
data->service = self;
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.timedate1",
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
p->cancellable,
on_datetime1_proxy_ready,
data);
}
static void
on_set_location (GSimpleAction * a G_GNUC_UNUSED,
GVariant * param,
gpointer gself)
{
char * zone;
char * name;
IndicatorDatetimeService * self;
self = INDICATOR_DATETIME_SERVICE (gself);
split_settings_location (g_variant_get_string (param, NULL), &zone, &name);
indicator_datetime_service_set_location (self, zone, name);
g_free (name);
g_free (zone);
}
/***
****
***/
static GMenuModel *
create_desktop_settings_section (IndicatorDatetimeService * self G_GNUC_UNUSED)
{
GMenu * menu = g_menu_new ();
g_menu_append (menu, _("Date & Time Settings…"), "indicator.activate-desktop-settings");
return G_MENU_MODEL (menu);
}
static GMenuModel *
create_phone_settings_section (IndicatorDatetimeService * self G_GNUC_UNUSED)
{
GMenu * menu = g_menu_new ();
g_menu_append (menu, _("Time & Date settings…"), "indicator.activate-phone-settings");
return G_MENU_MODEL (menu);
}
static void
create_menu (IndicatorDatetimeService * self, int profile)
{
GMenu * menu;
GMenu * submenu;
GMenuItem * header;
GMenuModel * sections[16];
const gchar * header_action;
int i;
int n = 0;
g_assert (0<=profile && profile<N_PROFILES);
g_assert (self->priv->menus[profile].menu == NULL);
switch (profile)
{
case PROFILE_PHONE:
sections[n++] = create_phone_calendar_section (self);
sections[n++] = create_phone_appointments_section (self);
sections[n++] = create_phone_settings_section (self);
header_action = "indicator.phone-header";
break;
case PROFILE_DESKTOP:
sections[n++] = create_desktop_calendar_section (self);
sections[n++] = create_desktop_appointments_section (self);
sections[n++] = create_locations_section (self);
sections[n++] = create_desktop_settings_section (self);
header_action = "indicator.desktop-header";
break;
case PROFILE_GREETER:
sections[n++] = create_desktop_calendar_section (self);
header_action = "indicator.desktop-header";
break;
}
/* add sections to the submenu */
submenu = g_menu_new ();
for (i=0; i<n; ++i)
{
g_menu_append_section (submenu, NULL, sections[i]);
g_object_unref (sections[i]);
}
/* add submenu to the header */
header = g_menu_item_new (NULL, header_action);
g_menu_item_set_attribute (header, "x-canonical-type",
"s", "com.canonical.indicator.root");
g_menu_item_set_submenu (header, G_MENU_MODEL (submenu));
g_object_unref (submenu);
/* add header to the menu */
menu = g_menu_new ();
g_menu_append_item (menu, header);
g_object_unref (header);
self->priv->menus[profile].menu = menu;
self->priv->menus[profile].submenu = submenu;
}
/***
**** GActions
***/
/* Run a particular program based on an activation */
static void
execute_command (const gchar * cmd)
{
GError * err = NULL;
g_debug ("Issuing command '%s'", cmd);
if (!g_spawn_command_line_async (cmd, &err))
{
g_warning ("Unable to start \"%s\": %s", cmd, err->message);
g_error_free (err);
}
}
static void
on_desktop_settings_activated (GSimpleAction * a G_GNUC_UNUSED,
GVariant * param G_GNUC_UNUSED,
gpointer gself G_GNUC_UNUSED)
{
#ifdef HAVE_CCPANEL
execute_command ("gnome-control-center indicator-datetime");
#else
execute_command ("gnome-control-center datetime");
#endif
}
static void
on_phone_settings_activated (GSimpleAction * a G_GNUC_UNUSED,
GVariant * param G_GNUC_UNUSED,
gpointer gself G_GNUC_UNUSED)
{
url_dispatch_send ("settings:///system/time-date", NULL, NULL);
}
static void
on_activate_planner (GSimpleAction * a G_GNUC_UNUSED,
GVariant * param,
gpointer gself)
{
priv_t * p = INDICATOR_DATETIME_SERVICE(gself)->priv;
if (p->planner != NULL)
{
const gint64 t = g_variant_get_int64 (param);
if (t)
{
GDateTime * date_time = g_date_time_new_from_unix_local (t);
indicator_datetime_planner_activate_time (p->planner, date_time);
g_date_time_unref (date_time);
}
else /* no time specified... */
{
indicator_datetime_planner_activate (p->planner);
}
}
}
static void
on_calendar_action_activated (GSimpleAction * action G_GNUC_UNUSED,
GVariant * state,
gpointer gself)
{
gint64 unix_time;
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
if ((unix_time = g_variant_get_int64 (state)))
{
GDateTime * date = g_date_time_new_from_unix_local (unix_time);
indicator_datetime_service_set_calendar_date (self, date);
g_date_time_unref (date);
}
else /* unset */
{
indicator_datetime_service_set_calendar_date (self, NULL);
}
}
static void
init_gactions (IndicatorDatetimeService * self)
{
GSimpleAction * a;
priv_t * p = self->priv;
GActionEntry entries[] = {
{ "activate-desktop-settings", on_desktop_settings_activated },
{ "activate-phone-settings", on_phone_settings_activated },
{ "activate-planner", on_activate_planner, "x", NULL },
{ "set-location", on_set_location, "s" }
};
p->actions = g_simple_action_group_new ();
g_action_map_add_action_entries (G_ACTION_MAP(p->actions),
entries,
G_N_ELEMENTS(entries),
self);
/* add the header actions */
a = g_simple_action_new_stateful ("desktop-header", NULL,
create_desktop_header_state (self));
g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
p->desktop_header_action = a;
a = g_simple_action_new_stateful ("phone-header", NULL,
create_phone_header_state (self));
g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
p->phone_header_action = a;
/* add the calendar action */
a = g_simple_action_new_stateful ("calendar",
G_VARIANT_TYPE_INT64,
create_calendar_state (self));
g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a));
g_signal_connect (a, "activate",
G_CALLBACK(on_calendar_action_activated), self);
p->calendar_action = a;
rebuild_now (self, SECTION_HEADER);
}
/***
****
***/
/**
* A small helper function for rebuild_now().
* - removes the previous section
* - adds and unrefs the new section
*/
static void
rebuild_section (GMenu * parent, int pos, GMenuModel * new_section)
{
g_menu_remove (parent, pos);
g_menu_insert_section (parent, pos, NULL, new_section);
g_object_unref (new_section);
}
static void
rebuild_now (IndicatorDatetimeService * self, int sections)
{
priv_t * p = self->priv;
struct ProfileMenuInfo * phone = &p->menus[PROFILE_PHONE];
struct ProfileMenuInfo * desktop = &p->menus[PROFILE_DESKTOP];
struct ProfileMenuInfo * greeter = &p->menus[PROFILE_GREETER];
if (sections & SECTION_HEADER)
{
g_simple_action_set_state (p->desktop_header_action,
create_desktop_header_state (self));
g_simple_action_set_state (p->phone_header_action,
create_phone_header_state (self));
}
if (sections & SECTION_CALENDAR)
{
rebuild_section (phone->submenu, 0, create_phone_calendar_section (self));
rebuild_section (desktop->submenu, 0, create_desktop_calendar_section (self));
rebuild_section (greeter->submenu, 0, create_desktop_calendar_section (self));
}
if (sections & SECTION_APPOINTMENTS)
{
rebuild_section (phone->submenu, 1, create_phone_appointments_section (self));
rebuild_section (desktop->submenu, 1, create_desktop_appointments_section (self));
}
if (sections & SECTION_LOCATIONS)
{
rebuild_section (desktop->submenu, 2, create_locations_section (self));
}
if (sections & SECTION_SETTINGS)
{
rebuild_section (phone->submenu, 2, create_phone_settings_section (self));
rebuild_section (desktop->submenu, 3, create_desktop_settings_section (self));
}
}
static int
rebuild_timeout_func (IndicatorDatetimeService * self)
{
priv_t * p = self->priv;
rebuild_now (self, p->rebuild_flags);
p->rebuild_flags = 0;
p->rebuild_id = 0;
return G_SOURCE_REMOVE;
}
static void
rebuild_soon (IndicatorDatetimeService * self, int section)
{
priv_t * p = self->priv;
p->rebuild_flags |= section;
if (p->rebuild_id == 0)
{
/* Change events seem to come over the bus in small bursts. This msec
value is an arbitrary number that tries to be large enough to fold
multiple events into a single rebuild, but small enough that the
user won't notice any lag. */
static const int REBUILD_INTERVAL_MSEC = 500;
p->rebuild_id = g_timeout_add (REBUILD_INTERVAL_MSEC,
(GSourceFunc)rebuild_timeout_func,
self);
}
}
/***
**** org.freedesktop.login1.Manager
***/
static void
on_login1_manager_signal (GDBusProxy * proxy G_GNUC_UNUSED,
gchar * sender_name G_GNUC_UNUSED,
gchar * signal_name,
GVariant * parameters,
gpointer gself)
{
if (!g_strcmp0 (signal_name, "PrepareForSleep"))
{
gboolean sleeping = FALSE;
g_variant_get (parameters, "(b)", &sleeping);
if (!sleeping)
on_local_time_jumped (INDICATOR_DATETIME_SERVICE (gself));
}
}
static void
on_login1_manager_proxy_ready (GObject * object G_GNUC_UNUSED,
GAsyncResult * res,
gpointer gself)
{
GError * err;
GDBusProxy * proxy;
err = NULL;
proxy = g_dbus_proxy_new_for_bus_finish (res, &err);
if (err != NULL)
{
if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("Could not grab DBus proxy for logind: %s", err->message);
g_error_free (err);
}
else
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
self->priv->login1_manager = proxy;
g_signal_connect (proxy, "g-signal",
G_CALLBACK(on_login1_manager_signal), self);
}
}
/***
**** Appointments
***/
static void
set_calendar_appointments (IndicatorDatetimeService * self,
GSList * appointments)
{
priv_t * p = self->priv;
/* repopulate the list */
indicator_datetime_planner_free_appointments (p->calendar_appointments);
p->calendar_appointments = appointments;
/* sync the menus/actions */
update_calendar_action_state (self);
rebuild_calendar_section_soon (self);
}
static void
on_calendar_appointments_ready (GObject * source,
GAsyncResult * res,
gpointer self)
{
IndicatorDatetimePlanner * planner;
GError * error;
GSList * appointments;
planner = INDICATOR_DATETIME_PLANNER (source);
error = NULL;
appointments = indicator_datetime_planner_get_appointments_finish (planner,
res,
&error);
if (error != NULL)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("can't get this month's appointments: %s", error->message);
g_error_free (error);
}
else
{
set_calendar_appointments (INDICATOR_DATETIME_SERVICE (self),
appointments);
}
}
static void
set_upcoming_appointments (IndicatorDatetimeService * self,
GSList * appointments)
{
priv_t * p = self->priv;
/* repopulate the list */
indicator_datetime_planner_free_appointments (p->upcoming_appointments);
p->upcoming_appointments = appointments;
/* sync the menus/actions */
rebuild_appointments_section_soon (self);
}
static void
on_upcoming_appointments_ready (GObject * source,
GAsyncResult * res,
gpointer self)
{
IndicatorDatetimePlanner * planner;
GError * error;
GSList * appointments;
planner = INDICATOR_DATETIME_PLANNER (source);
error = NULL;
appointments = indicator_datetime_planner_get_appointments_finish (planner,
res,
&error);
if (error != NULL)
{
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning ("can't get upcoming appointments: %s", error->message);
g_error_free (error);
}
else
{
set_upcoming_appointments (INDICATOR_DATETIME_SERVICE (self),
appointments);
}
}
static void
update_appointment_lists (IndicatorDatetimeService * self)
{
IndicatorDatetimePlanner * planner;
GDateTime * calendar_date;
GDateTime * begin;
GDateTime * end;
int y, m, d;
planner = self->priv->planner;
calendar_date = get_calendar_date (self);
/* get all the appointments in the calendar month */
g_date_time_get_ymd (calendar_date, &y, &m, &d);
begin = g_date_time_new_local (y, m, 1, 0, 0, 0.1);
end = g_date_time_new_local (y, m, g_date_get_days_in_month(m,y), 23, 59, 59.9);
if (begin && end)
indicator_datetime_planner_get_appointments (planner, begin, end,
on_calendar_appointments_ready,
self);
g_clear_pointer (&begin, g_date_time_unref);
g_clear_pointer (&end, g_date_time_unref);
/* get the upcoming appointments */
begin = g_date_time_ref (calendar_date);
end = g_date_time_add_months (begin, 1);
if (begin && end)
indicator_datetime_planner_get_appointments (planner, begin, end,
on_upcoming_appointments_ready,
self);
g_clear_pointer (&begin, g_date_time_unref);
g_clear_pointer (&end, g_date_time_unref);
g_clear_pointer (&calendar_date, g_date_time_unref);
}
/***
**** GDBus
***/
static void
on_bus_acquired (GDBusConnection * connection,
const gchar * name,
gpointer gself)
{
int i;
guint id;
GError * err = NULL;
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE(gself);
priv_t * p = self->priv;
g_debug ("bus acquired: %s", name);
p->conn = g_object_ref (G_OBJECT (connection));
/* export the actions */
if ((id = g_dbus_connection_export_action_group (connection,
BUS_PATH,
G_ACTION_GROUP (p->actions),
&err)))
{
p->actions_export_id = id;
}
else
{
g_warning ("cannot export action group: %s", err->message);
g_clear_error (&err);
}
/* export the menus */
for (i=0; i<N_PROFILES; ++i)
{
char * path = g_strdup_printf ("%s/%s", BUS_PATH, menu_names[i]);
struct ProfileMenuInfo * menu = &p->menus[i];
if ((id = g_dbus_connection_export_menu_model (connection,
path,
G_MENU_MODEL (menu->menu),
&err)))
{
menu->export_id = id;
}
else
{
g_warning ("cannot export %s menu: %s", menu_names[i], err->message);
g_clear_error (&err);
}
g_free (path);
}
}
static void
unexport (IndicatorDatetimeService * self)
{
int i;
priv_t * p = self->priv;
/* unexport the menus */
for (i=0; i<N_PROFILES; ++i)
{
guint * id = &self->priv->menus[i].export_id;
if (*id)
{
g_dbus_connection_unexport_menu_model (p->conn, *id);
*id = 0;
}
}
/* unexport the actions */
if (p->actions_export_id)
{
g_dbus_connection_unexport_action_group (p->conn, p->actions_export_id);
p->actions_export_id = 0;
}
}
static void
on_name_lost (GDBusConnection * connection G_GNUC_UNUSED,
const gchar * name,
gpointer gself)
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE (gself);
g_debug ("%s %s name lost %s", G_STRLOC, G_STRFUNC, name);
unexport (self);
g_signal_emit (self, signals[SIGNAL_NAME_LOST], 0, NULL);
}
/***
**** GObject virtual functions
***/
static void
my_dispose (GObject * o)
{
int i;
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE(o);
priv_t * p = self->priv;
if (p->own_id)
{
g_bus_unown_name (p->own_id);
p->own_id = 0;
}
unexport (self);
if (p->cancellable != NULL)
{
g_cancellable_cancel (p->cancellable);
g_clear_object (&p->cancellable);
}
set_detect_location_enabled (self, FALSE);
if (p->planner != NULL)
{
g_signal_handlers_disconnect_by_data (p->planner, self);
g_clear_object (&p->planner);
}
g_clear_pointer (&p->upcoming_appointments, indicator_datetime_planner_free_appointments);
g_clear_pointer (&p->calendar_appointments, indicator_datetime_planner_free_appointments);
if (p->login1_manager != NULL)
{
g_signal_handlers_disconnect_by_data (p->login1_manager, self);
g_clear_object (&p->login1_manager);
}
indicator_clear_timer (&p->skew_timer);
indicator_clear_timer (&p->rebuild_id);
indicator_clear_timer (&p->timezone_timer);
indicator_clear_timer (&p->header_timer);
if (p->settings != NULL)
{
g_signal_handlers_disconnect_by_data (p->settings, self);
g_clear_object (&p->settings);
}
g_clear_object (&p->actions);
for (i=0; i<N_PROFILES; ++i)
g_clear_object (&p->menus[i].menu);
g_clear_object (&p->calendar_action);
g_clear_object (&p->desktop_header_action);
g_clear_object (&p->phone_header_action);
g_clear_object (&p->conn);
G_OBJECT_CLASS (indicator_datetime_service_parent_class)->dispose (o);
}
static void
my_finalize (GObject * o)
{
IndicatorDatetimeService * self = INDICATOR_DATETIME_SERVICE(o);
priv_t * p = self->priv;
g_clear_pointer (&p->skew_time, g_date_time_unref);
g_clear_pointer (&p->calendar_date, g_date_time_unref);
G_OBJECT_CLASS (indicator_datetime_service_parent_class)->finalize (o);
}
/***
**** Instantiation
***/
static void
indicator_datetime_service_init (IndicatorDatetimeService * self)
{
guint i, n;
priv_t * p;
GString * gstr = g_string_new (NULL);
/* these are the settings that affect the
contents of the respective sections */
const char * const header_settings[] = {
SETTINGS_SHOW_CLOCK_S,
SETTINGS_TIME_FORMAT_S,
SETTINGS_SHOW_SECONDS_S,
SETTINGS_SHOW_DAY_S,
SETTINGS_SHOW_DATE_S,
SETTINGS_CUSTOM_TIME_FORMAT_S
};
const char * const calendar_settings[] = {
SETTINGS_SHOW_CALENDAR_S,
SETTINGS_SHOW_WEEK_NUMBERS_S
};
const char * const appointment_settings[] = {
SETTINGS_SHOW_EVENTS_S,
SETTINGS_TIME_FORMAT_S,
SETTINGS_SHOW_SECONDS_S
};
const char * const location_settings[] = {
SETTINGS_TIME_FORMAT_S,
SETTINGS_SHOW_SECONDS_S,
SETTINGS_CUSTOM_TIME_FORMAT_S,
SETTINGS_SHOW_LOCATIONS_S,
SETTINGS_LOCATIONS_S,
SETTINGS_SHOW_DETECTED_S,
SETTINGS_TIMEZONE_NAME_S
};
const char * const time_format_string_settings[] = {
SETTINGS_TIME_FORMAT_S,
SETTINGS_SHOW_SECONDS_S,
SETTINGS_CUSTOM_TIME_FORMAT_S
};
/* init the priv pointer */
p = G_TYPE_INSTANCE_GET_PRIVATE (self,
INDICATOR_TYPE_DATETIME_SERVICE,
IndicatorDatetimeServicePrivate);
self->priv = p;
p->cancellable = g_cancellable_new ();
/***
**** Create the planner and listen for changes
***/
p->planner = indicator_datetime_planner_eds_new ();
g_signal_connect_swapped (p->planner, "appointments-changed",
G_CALLBACK(update_appointment_lists), self);
/***
**** Create the settings object and listen for changes
***/
p->settings = g_settings_new (SETTINGS_INTERFACE);
for (i=0, n=G_N_ELEMENTS(header_settings); i<n; i++)
{
g_string_printf (gstr, "changed::%s", header_settings[i]);
g_signal_connect_swapped (p->settings, gstr->str,
G_CALLBACK(rebuild_header_soon), self);
}
for (i=0, n=G_N_ELEMENTS(calendar_settings); i<n; i++)
{
g_string_printf (gstr, "changed::%s", calendar_settings[i]);
g_signal_connect_swapped (p->settings, gstr->str,
G_CALLBACK(rebuild_calendar_section_soon), self);
}
for (i=0, n=G_N_ELEMENTS(appointment_settings); i<n; i++)
{
g_string_printf (gstr, "changed::%s", appointment_settings[i]);
g_signal_connect_swapped (p->settings, gstr->str,
G_CALLBACK(rebuild_appointments_section_soon), self);
}
for (i=0, n=G_N_ELEMENTS(location_settings); i<n; i++)
{
g_string_printf (gstr, "changed::%s", location_settings[i]);
g_signal_connect_swapped (p->settings, gstr->str,
G_CALLBACK(rebuild_locations_section_soon), self);
}
/* The keys in time_format_string_settings affect the time format strings we build.
When these change, we need to rebuild everything that has a time format string. */
for (i=0, n=G_N_ELEMENTS(time_format_string_settings); i<n; i++)
{
g_string_printf (gstr, "changed::%s", time_format_string_settings[i]);
g_signal_connect_swapped (p->settings, gstr->str,
G_CALLBACK(on_local_time_jumped), self);
}
init_gactions (self);
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
p->cancellable,
on_login1_manager_proxy_ready,
self);
p->skew_timer = g_timeout_add_seconds (SKEW_CHECK_INTERVAL_SEC,
skew_timer_func,
self);
p->own_id = g_bus_own_name (G_BUS_TYPE_SESSION,
BUS_NAME,
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
on_bus_acquired,
NULL,
on_name_lost,
self,
NULL);
on_local_time_jumped (self);
for (i=0; i<N_PROFILES; ++i)
create_menu (self, i);
g_string_free (gstr, TRUE);
}
static void
indicator_datetime_service_class_init (IndicatorDatetimeServiceClass * klass)
{
GObjectClass * object_class = G_OBJECT_CLASS (klass);
object_class->dispose = my_dispose;
object_class->finalize = my_finalize;
g_type_class_add_private (klass, sizeof (IndicatorDatetimeServicePrivate));
signals[SIGNAL_NAME_LOST] = g_signal_new (
INDICATOR_DATETIME_SERVICE_SIGNAL_NAME_LOST,
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (IndicatorDatetimeServiceClass, name_lost),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
/***
**** Public API
***/
IndicatorDatetimeService *
indicator_datetime_service_new (void)
{
GObject * o = g_object_new (INDICATOR_TYPE_DATETIME_SERVICE, NULL);
return INDICATOR_DATETIME_SERVICE (o);
}
/* This currently just returns the system time,
As we add test coverage, we'll need this to bypass the system time. */
GDateTime *
indicator_datetime_service_get_localtime (IndicatorDatetimeService * self G_GNUC_UNUSED)
{
return g_date_time_new_now_local ();
}
void
indicator_datetime_service_set_calendar_date (IndicatorDatetimeService * self,
GDateTime * date)
{
gboolean dirty;
priv_t * p = self->priv;
dirty = !date || !p->calendar_date || g_date_time_compare (date, p->calendar_date);
/* update calendar_date */
g_clear_pointer (&p->calendar_date, g_date_time_unref);
if (date != NULL)
p->calendar_date = g_date_time_ref (date);
/* sync the menuitems and action states */
if (dirty)
update_appointment_lists (self);
}
|