From: adrianiainlam Date: Mon, 9 May 2016 23:16:45 +0000 (+0100) Subject: fix time inaccuracy due to suspend/resume X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=988731c91cb7789aca00d1749139c3edc4c0b441;p=indicator-lunar-calendar.git fix time inaccuracy due to suspend/resume --- 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();