From 988731c91cb7789aca00d1749139c3edc4c0b441 Mon Sep 17 00:00:00 2001 From: adrianiainlam Date: Tue, 10 May 2016 00:16:45 +0100 Subject: [PATCH] fix time inaccuracy due to suspend/resume --- README.md | 8 +++++++- indicator-lunar-calendar.js | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a08b74..962a09b 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,17 @@ shown above is using timezone UTC+1. [4]: https://github.com/roadmanfong/LunarCalendar - - [node-cron][5] (by @ncb000gt) + - [node-cron (cron)][5] (by @ncb000gt) npm package: https://www.npmjs.com/package/cron [5]: https://github.com/ncb000gt/node-cron + + - [node-dbus (dbus-native)][6] (by @sidorares) + + npm package: https://www.npmjs.com/package/dbus-native + + [6]: https://github.com/sidorares/node-dbus ## Usage diff --git a/indicator-lunar-calendar.js b/indicator-lunar-calendar.js index e93f83e..64fb0ea 100755 --- a/indicator-lunar-calendar.js +++ b/indicator-lunar-calendar.js @@ -28,6 +28,7 @@ var Gtk = GNode.importNS('Gtk'); var AppIndicator3 = GNode.importNS('AppIndicator3'); var CronJob = require('cron').CronJob; var LunarCalendar = require('lunar-calendar-zh'); +var DBus = require('dbus-native'); /* setup indicator object */ GNode.startLoop(); @@ -82,4 +83,23 @@ var job = new CronJob({ }); update_indicator(); + +/* Detect resume from suspend and update date/time */ +var bus = DBus.systemBus(); +var service = bus.getService('org.freedesktop.login1'); +service.getInterface( + '/org/freedesktop/login1', + 'org.freedesktop.login1.Manager', + function(err, nm) { + nm.addListener('PrepareForSleep', function(arg) { + // PrepareForSleep returns false when resuming from suspend + if(!arg) { + job.stop(); // force cronjob to recalculate time + job.start(); + update_indicator(); + } + }); + } +); + Gtk.main(); -- 2.7.4