DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (47b55c578f95)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/* 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/. */

const EXPORTED_SYMBOLS = ["Notifications"];
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

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

var Notifications = {
  get ellipsis () {
    let ellipsis = "[\u2026]";

    try {
      ellipsis =
        Services.prefs.getComplexValue("intl.ellipsis",
                                       Ci.nsIPrefLocalizedString).data;
    } catch (e) { }
    return ellipsis;
  },

  _showMessageNotification: function (aMessage) {
    let parser = Cc["@mozilla.org/xmlextras/domparser;1"].createInstance(Ci.nsIDOMParser);
    let doc = parser.parseFromString(aMessage.message, "text/html");
    let body = doc.querySelector("body");
    let encoder =
      Cc["@mozilla.org/layout/documentEncoder;1?type=text/plain"]
       .createInstance(Ci.nsIDocumentEncoder);
    encoder.init(doc, "text/plain", 0);
    encoder.setNode(body);
    let messageText = encoder.encodeToString().replace(/\s+/g, " ");

    // Crop the end of the text if needed.
    if (messageText.length > 50)
      messageText = messageText.substr(0, 50) + this.ellipsis;

    // Use the buddy icon if available for the icon of the notification.
    let icon;
    let conv = aMessage.conversation;
    if (!conv.isChat) {
      let buddy = conv.buddy;
      if (buddy)
        icon = buddy.buddyIconFilename;
    }
    if (!icon)
      icon = "chrome://messenger/skin/userIcon.png";

    // Handle third person messages
    let name = aMessage.alias || aMessage.who;
    if (messageText.startsWith("/me "))
      messageText = messageText.replace(/^\/me/, name);

    // If the TB window doesn't have the focus, show the notification!
    if (!Services.wm.getMostRecentWindow("mail:3pane").document.hasFocus()) {
      Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService)
        .showAlertNotification(icon, name, messageText, true, "", this);
    }
  },

  init: function() {
    Services.obs.addObserver(Notifications, "new-directed-incoming-message", false);
    Services.obs.addObserver(Notifications, "alertclickcallback", false);
  },

  _notificationPrefName: "mail.chat.show_desktop_notifications",
  observe: function(aSubject, aTopic, aData) {
    if (aTopic == "new-directed-incoming-message") {
      if (Services.prefs.getBoolPref(this._notificationPrefName))
        this._showMessageNotification(aSubject);
    } else if (aTopic == "alertclickcallback") {
      // Focus the conversation if the notification is clicked.
      let mainWindow = Services.wm.getMostRecentWindow("mail:3pane");
      if (mainWindow) {
        mainWindow.focus();
        mainWindow.showChatTab();
      } else {
        window.openDialog("chrome://messenger/content/", "_blank",
                          "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar",
                          null, {tabType: "chat", tabParams: {}});
      }
    }
  }
};