Workaround for issue #1
[indicator-lunar-calendar.git] / indicator-lunar-calendar.js
index 77b778d..ea4470f 100755 (executable)
@@ -28,13 +28,14 @@ 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();
 Gtk.init(null);
 var indicator = AppIndicator3.Indicator.new(
     "lunar-indicator",
-    __dirname + '/鼠.svg',
+    __dirname + '/icons/鼠.svg',
     AppIndicator3.IndicatorCategory.APPLICATION_STATUS
 );
 indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE);
@@ -70,9 +71,17 @@ function update_indicator() {
     long_date += '\n' + lunar.hour + '時';
     
     /* output to indicator */
-    indicator.set_icon(__dirname + '/' + lunar.zodiac + '.svg');
+    indicator.set_icon(__dirname + '/icons/' + lunar.zodiac + '.svg');
     indicator.set_label(compact_date, '');
     item.set_label(long_date);
+    
+    console.log('Indicator updated. ' + lunar.hour + ' Time: ' + new Date());
+    /* DO NOT REMOVE THE ABOVE LINE.
+     * I have no absolutely no idea why but the indicator doesn't get
+     * updated if this line is removed.
+     * It wasn't needed though before implementing the DBus section below.
+     * So perhaps some kind of conflict between the two?
+     */
 }
 
 var job = new CronJob({
@@ -82,4 +91,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();