fix time inaccuracy due to suspend/resume
authoradrianiainlam <adrianiainlam@gmail.com>
Mon, 9 May 2016 23:16:45 +0000 (00:16 +0100)
committeradrianiainlam <adrianiainlam@gmail.com>
Mon, 9 May 2016 23:16:45 +0000 (00:16 +0100)
README.md
indicator-lunar-calendar.js

index 6a08b74..962a09b 100644 (file)
--- 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
 
index e93f83e..64fb0ea 100755 (executable)
@@ -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();