|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:dbus/dbus.dart'; |
| 4 | + |
| 5 | +const _kDateTimeInterface = 'org.freedesktop.timedate1'; |
| 6 | +const _kDateTimePath = '/org/freedesktop/timedate1'; |
| 7 | +// const _kListTimezoneMethodName = 'ListTimezones'; |
| 8 | +// const _kSetLocalRtcMethodName = 'SetLocalRTC'; |
| 9 | +const _kSetNtpMethodName = 'SetNTP'; |
| 10 | +const _kSetTimeMethodName = 'SetTime'; |
| 11 | +const _kSetTimezoneMethodName = 'SetTimezone'; |
| 12 | +// const _kCanNTPPropertyName = 'CanNTP'; |
| 13 | +// const _kLocalRTCPropertyName = 'LocalRTC'; |
| 14 | +const _kNTPPropertyName = 'NTP'; |
| 15 | +// const _kNTPSynchronizedPropertyName = 'NTPSynchronized'; |
| 16 | +// const _kRTCTimeUSecPropertyName = 'RTCTimeUSec'; |
| 17 | +const _kTimeUSecPropertyName = 'TimeUSec'; |
| 18 | +const _kTimezonePropertyName = 'Timezone'; |
| 19 | + |
| 20 | +class DateTimeService { |
| 21 | + DateTimeService() : _object = _createObject(); |
| 22 | + |
| 23 | + final DBusRemoteObject _object; |
| 24 | + StreamSubscription<DBusPropertiesChangedSignal>? _propertyListener; |
| 25 | + |
| 26 | + static DBusRemoteObject _createObject() => |
| 27 | + DBusRemoteObject(DBusClient.system(), |
| 28 | + name: _kDateTimeInterface, path: DBusObjectPath(_kDateTimePath)); |
| 29 | + |
| 30 | + Future<void> init() async { |
| 31 | + await _initTimezone(); |
| 32 | + await _initNtp(); |
| 33 | + _dateTime = await getDateTime(); |
| 34 | + _propertyListener ??= _object.propertiesChanged.listen(_updateProperties); |
| 35 | + } |
| 36 | + |
| 37 | + void _updateProperties(DBusPropertiesChangedSignal signal) { |
| 38 | + if (signal.hasChangedTimeZone()) { |
| 39 | + _object.getTimezone().then(_updateTimezone); |
| 40 | + _object.getDateTime().then(_updateDateTime); |
| 41 | + } |
| 42 | + if (signal.hasChangedDateTime()) { |
| 43 | + _object.getDateTime().then(_updateDateTime); |
| 44 | + } |
| 45 | + if (signal.hasChangedNtp()) { |
| 46 | + _object.getNtp().then(_updateNtp); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Future<void> dispose() async { |
| 51 | + await _propertyListener?.cancel(); |
| 52 | + await _object.client.close(); |
| 53 | + _propertyListener = null; |
| 54 | + } |
| 55 | + |
| 56 | + // Timezone |
| 57 | + String? _timezone; |
| 58 | + String? get timezone => _timezone; |
| 59 | + set timezone(String? value) { |
| 60 | + if (value == null) return; |
| 61 | + _object.setTimeZone(value); |
| 62 | + } |
| 63 | + |
| 64 | + Stream<String?> get timezoneChanged => _timezoneController.stream; |
| 65 | + final _timezoneController = StreamController<String?>.broadcast(); |
| 66 | + |
| 67 | + void _updateTimezone(String? value) { |
| 68 | + if (_timezone == value) return; |
| 69 | + _timezone = value; |
| 70 | + if (!_timezoneController.isClosed) { |
| 71 | + _timezoneController.add(_timezone); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Future<void> _initTimezone() async { |
| 76 | + _updateTimezone(await _object.getTimezone()); |
| 77 | + } |
| 78 | + |
| 79 | + // Date and time |
| 80 | + DateTime? _dateTime; |
| 81 | + DateTime? get dateTime => _dateTime; |
| 82 | + set dateTime(DateTime? dateTime) { |
| 83 | + if (dateTime == null) return; |
| 84 | + _object.setTime(dateTime.microsecondsSinceEpoch); |
| 85 | + } |
| 86 | + |
| 87 | + void _updateDateTime(DateTime? value) { |
| 88 | + if (_dateTime?.second == value?.second) return; |
| 89 | + getDateTime().then((value) => _dateTime = value); |
| 90 | + } |
| 91 | + |
| 92 | + Future<DateTime?> getDateTime() async { |
| 93 | + return await _object.getDateTime(); |
| 94 | + } |
| 95 | + |
| 96 | + // NTP |
| 97 | + bool? _ntp; |
| 98 | + bool? get ntp => _ntp; |
| 99 | + set ntp(bool? value) { |
| 100 | + if (value == null) return; |
| 101 | + _object.setNtp(value); |
| 102 | + } |
| 103 | + |
| 104 | + Stream<bool?> get ntpChanged => _ntpController.stream; |
| 105 | + final _ntpController = StreamController<bool?>.broadcast(); |
| 106 | + |
| 107 | + void _updateNtp(bool? value) { |
| 108 | + if (_ntp == value) return; |
| 109 | + _ntp = value; |
| 110 | + if (!_ntpController.isClosed) { |
| 111 | + _ntpController.add(_ntp); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + Future<void> _initNtp() async { |
| 116 | + _updateNtp(await _object.getNtp()); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +extension _DateTimeRemoteObject on DBusRemoteObject { |
| 121 | + Future<String?> getTimezone() async { |
| 122 | + final timeZone = |
| 123 | + await getProperty(_kDateTimeInterface, _kTimezonePropertyName); |
| 124 | + return (timeZone as DBusString).value; |
| 125 | + } |
| 126 | + |
| 127 | + Future<void> setTimeZone(String timezone) async { |
| 128 | + final args = [DBusString(timezone), const DBusBoolean(false)]; |
| 129 | + callMethod(_kDateTimeInterface, _kSetTimezoneMethodName, args); |
| 130 | + } |
| 131 | + |
| 132 | + Future<void> setTime(int time) async { |
| 133 | + bool? ntp = await getNtp(); |
| 134 | + if (ntp == null || ntp == true) return; |
| 135 | + final args = [ |
| 136 | + DBusInt64(time), |
| 137 | + const DBusBoolean(false), |
| 138 | + const DBusBoolean(false) |
| 139 | + ]; |
| 140 | + callMethod(_kDateTimeInterface, _kSetTimeMethodName, args); |
| 141 | + } |
| 142 | + |
| 143 | + Future<DateTime?> getDateTime() async { |
| 144 | + final timeUSec = |
| 145 | + await getProperty(_kDateTimeInterface, _kTimeUSecPropertyName); |
| 146 | + return DateTime.fromMicrosecondsSinceEpoch((timeUSec as DBusUint64).value); |
| 147 | + } |
| 148 | + |
| 149 | + Future<bool?> getNtp() async { |
| 150 | + final ntpSync = await getProperty(_kDateTimeInterface, _kNTPPropertyName); |
| 151 | + return (ntpSync as DBusBoolean).value; |
| 152 | + } |
| 153 | + |
| 154 | + Future<void> setNtp(bool? value) async { |
| 155 | + if (value == null) return; |
| 156 | + final args = [DBusBoolean(value), const DBusBoolean(false)]; |
| 157 | + callMethod(_kDateTimeInterface, _kSetNtpMethodName, args); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +extension _ChangedDateTime on DBusPropertiesChangedSignal { |
| 162 | + bool hasChangedTimeZone() { |
| 163 | + return changedProperties.containsKey(_kTimezonePropertyName); |
| 164 | + } |
| 165 | + |
| 166 | + bool hasChangedDateTime() { |
| 167 | + return changedProperties.containsKey(_kTimeUSecPropertyName); |
| 168 | + } |
| 169 | + |
| 170 | + bool hasChangedNtp() { |
| 171 | + return changedProperties.containsKey(_kNTPPropertyName); |
| 172 | + } |
| 173 | +} |
0 commit comments