im/modules/ibSounds.jsm
author Jorg K <jorgk@jorgk.com>
Thu, 12 Apr 2018 21:51:55 +0200
changeset 30531 e7b1d8cd4f3bf56febb93ed1ae0ae8a884d84848
parent 30204 1ac48db6177e9ea975af601c25770fc50db1b6dc
permissions -rw-r--r--
Bug 1452764 - Bug 1444601 follow-up: Restore bundle_read_smime so S/MIME decryption feedback shows again. a=jorgk DONTBUILD

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

this.EXPORTED_SYMBOLS = ["Sounds"];

ChromeUtils.import("resource:///modules/hiddenWindow.jsm");
ChromeUtils.import("resource:///modules/imServices.jsm");
ChromeUtils.import("resource:///modules/ibInterruptions.jsm");

var Sounds = {
  soundEvents: ["contact-signed-on", "contact-signed-off", "new-text"],
  soundFiles: {
    incoming: "chrome://instantbird-sounds/skin/receive.wav",
    outgoing: "chrome://instantbird-sounds/skin/send.wav",
    login: "chrome://instantbird-sounds/skin/login.wav",
    logout: "chrome://instantbird-sounds/skin/logout.wav",
    alert: "chrome://instantbird-sounds/skin/alert.wav"
  },

  getBoolPref: aPrefName =>
    Services.prefs.getBoolPref("messenger.options.playSounds." + aPrefName),

  play: function sh_play(aEvent, aPref, aSubject, aTopic) {
    if (!this.getBoolPref(aPref) || !this.getBoolPref(aEvent) ||
        !Interruptions.requestInterrupt(aTopic, aSubject, "sound"))
      return;

    new (getHiddenHTMLWindow().Audio)(this.soundFiles[aEvent])
                              .setAttribute("autoplay", "true");
  },

  observe: function(aObject, aTopic, aMsg) {
    switch(aTopic) {
    case "contact-signed-on":
      this.play("login", "blist", aObject, aTopic);
      break;

    case "contact-signed-off":
      this.play("logout", "blist", aObject, aTopic);
      break;

    case "new-text":
      if (aObject.outgoing)
        this.play("outgoing", "message", aObject, aTopic);
      else if (aObject.incoming && !aObject.system) {
        if (!aObject.conversation.isChat)
          this.play("incoming", "message", aObject, aTopic);
        else if (aObject.containsNick)
          this.play("alert", "message", aObject, aTopic);
      }
      break;

    default:
      throw "bad notification";
    }
  },

  init: function() {
    for (let topic of this.soundEvents)
      Services.obs.addObserver(this, topic);
  }
};