]> jfr.im git - irc/quakenet/qwebirc.git/commitdiff
add html5 notifications
authorChris Porter <redacted>
Sun, 17 Aug 2014 22:08:26 +0000 (23:08 +0100)
committerChris Porter <redacted>
Sun, 17 Aug 2014 22:08:26 +0000 (23:08 +0100)
js/ui/baseui.js
js/ui/baseuiwindow.js
js/ui/notifications.js
js/ui/panes/options.js

index c20129e3e4b87696ef908a2a53c25dc44b008ba3..34623fbf786470545db29907b07f6fa7ce8c33f3 100644 (file)
@@ -412,16 +412,25 @@ qwebirc.ui.NotificationUI = new Class({
     
     this.__beeper = new qwebirc.ui.Beeper(this.uiOptions);
     this.__flasher = new qwebirc.ui.Flasher(this.uiOptions);
-    
-    this.beep = this.__beeper.beep.bind(this.__beeper);
-    
-    this.flash = this.__flasher.flash.bind(this.__flasher);
+    this.__notifier = new qwebirc.ui.Notifier(this.uiOptions);
+
     this.cancelFlash = this.__flasher.cancelFlash.bind(this.__flasher);
   },
+  beep: function() {
+    this.__beeper.beep();
+  },
+  notify: function(title, message, callback) {
+    this.__beeper.beep();
+    this.__flasher.flash();
+    this.__notifier.notify(title, message, callback);
+  },
   setBeepOnMention: function(value) {
     if(value)
       this.__beeper.soundInit();
   },
+  setNotifications: function(value) {
+    this.__notifier.setEnabled(value);
+  },
   updateTitle: function(text) {
     if(this.__flasher.updateTitle(text))
       this.parent(text);
@@ -429,6 +438,7 @@ qwebirc.ui.NotificationUI = new Class({
   focusChange: function(value) {
     this.parent(value);
     this.__flasher.focusChange(value);
+    this.__notifier.focusChange(value);
   }
 });
 
index 22200adb40055aa7794874d36646774fbda793c5..1466907324b93502f1e128f944074bb68e8b99ac 100644 (file)
@@ -98,8 +98,10 @@ qwebirc.ui.Window = new Class({
   addLine: function(type, line, colour, element) {
     var hilight = qwebirc.ui.HILIGHT_NONE;
     var lhilight = false;
-    
+
     if(type) {
+      var selectMe = function() { this.parentObject.selectWindow(this); }.bind(this);
+      var message = $defined(line) ? line["m"] : null;
       hilight = qwebirc.ui.HILIGHT_ACTIVITY;
       
       if(type.match(/(NOTICE|ACTION|MSG)$/)) {
@@ -108,15 +110,13 @@ qwebirc.ui.Window = new Class({
             hilight = qwebirc.ui.HILIGHT_ACTIVITY;
           } else {
             hilight = qwebirc.ui.HILIGHT_US;
-            this.parentObject.beep();
-            this.parentObject.flash();
+            this.parentObject.notify("Private message from " + this.name, message, selectMe);
           }
         }
-        if(!type.match(/^OUR/) && this.client.hilightController.match(line["m"])) {
+        if(!type.match(/^OUR/) && this.client.hilightController.match(message)) {
           lhilight = true;
           hilight = qwebirc.ui.HILIGHT_US;
-          this.parentObject.beep();
-          this.parentObject.flash();
+          this.parentObject.notify("Hilighted in " + this.name, message, selectMe);
         } else if(hilight != qwebirc.ui.HILIGHT_US) {
           hilight = qwebirc.ui.HILIGHT_SPEECH;
         }
index efee7edf4d88b9f92f2f609b3da64973c8f3efb3..796da149ba573c16094633aee1e3e8971d7fc3a2 100644 (file)
@@ -143,3 +143,60 @@ qwebirc.ui.Flasher = new Class({
       this.cancelFlash();
   }
 });
+
+qwebirc.ui.Notifier = new Class({
+  initialize: function(uiOptions) {
+    this.uiOptions = uiOptions;
+
+    this.windowFocused = false;
+    this.previous = null;
+    this.setEnabled(this.uiOptions.NOTIFICATIONS);
+  },
+  focusChange: function(value) {
+    this.windowFocused = value;
+  },
+  setEnabled: function(value) {
+    this.enabled = value;
+    if(!value)
+      return;
+
+    if(this.isGranted())
+      return;
+
+    Notification.requestPermission(function (permission) {
+      if (!("permission" in Notification))
+        Notification.permission = permission;
+    });
+  },
+  isGranted: function() {
+    if(!("Notification" in window))
+      return false;
+
+    return Notification.permission === "granted";
+  },
+  notify: function(title, message, callback) {
+    if(this.windowFocused && !this.enabled || !this.isGranted())
+      return;
+
+    if(this.previous)
+      this.previous.close();
+
+    var n = new Notification(title, {body: message, icon: qwebirc.global.staticBaseURL + "images/qwebircsmall.png"});
+    var delay = function() {
+      n.close();
+      this.previous = null;
+    }.bind(this).delay(5000);
+
+    this.previous = n;
+    if(callback) {
+      n.addEventListener("click", function() {
+        this.previous = null;
+        window.focus();
+        callback();
+      });
+      n.addEventListener("close", function() {
+        this.previous = null;
+      }.bind(this));
+    }
+  }
+});
index 1ed73e466438372d5702238bc7fdd35e6a81054d..7ee9310e4059825926d0c0f1069128fd203d8b98 100644 (file)
@@ -14,12 +14,23 @@ qwebirc.ui.supportsFocus = function() {
  * settableByURL...
  */
 qwebirc.config.DEFAULT_OPTIONS = [
-  [1, "BEEP_ON_MENTION", "Beep when nick mentioned or on query activity", true, {
+  [1, "BEEP_ON_MENTION", "Beep on activity", true, {
     applyChanges: function(value, ui) {
       if(ui.setBeepOnMention)
         ui.setBeepOnMention(value);
     }
   }],
+  [16, "NOTIFICATIONS", "Emit HTML5 notifications on activity", false, {
+    enabled: function() {
+      if(!("Notification" in window))
+        return [false, false]; /* [disabled, default_value] */
+      return [true];
+    },
+    applyChanges: function(value, ui) {
+      if(ui.setNotifications)
+        ui.setNotifications(value);
+    }
+  }],
   [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, {
     enabled: qwebirc.ui.supportsFocus
   }],