<commit_before>this.EXPORTED_SYMBOLS = ["ui"];

var { interfaces: Ci, utils: Cu, classes: Cc } = Components;

Cu.import("resource:///modules/imServices.jsm");
Cu.import("resource:///modules/imXPCOMUtils.jsm");
Cu.import("resource:///modules/imWindows.jsm");
Cu.import("chrome://otr/content/otr.js");
Cu.import("chrome://otr/content/coniks/coniks.js");

var privDialog = "chrome://otr/content/priv.xul";
var authDialog = "chrome://otr/content/auth.xul";
var prefsDialog = "chrome://otr/content/prefs.xul";
var addFingerDialog = "chrome://otr/content/addfinger.xul";

XPCOMUtils.defineLazyGetter(this, "_", () =>
  l10nHelper("chrome://otr/locale/ui.properties")
);

var authVerify = "otr-auth-unverified";

var authLabelMap = new Map([
  ["otr:auth-error", _("auth.error")],
  ["otr:auth-success", _("auth.success")],
  ["otr:auth-successThem", _("auth.successThem")],
  ["otr:auth-fail", _("auth.fail")],
  ["otr:auth-waiting", _("auth.waiting")],
]);

var trustMap = new Map([
  [otr.trustState.TRUST_NOT_PRIVATE, {
    startLabel: _("start.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: true,
    disableAuth: true,
    class: "not_private",
  }],
  [otr.trustState.TRUST_UNVERIFIED, {
    startLabel: _("refresh.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: false,
    class: "unverified",
  }],
  [otr.trustState.TRUST_PRIVATE, {
    startLabel: _("refresh.label"),
    authLabel: _("reauth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: false,
    class: "private",
  }],
  [otr.trustState.TRUST_FINISHED, {
    startLabel: _("start.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: true,
    class: "finished",
  }],
]);

var windowRefs = new Map();

var ui = {

  debug: false,
  log: function log(msg) {
    if (!ui.debug)
      return;
    let csl = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
    csl.logStringMessage(msg);
  },

  prefs: null,
  setPrefs: function() {
    let branch = "extensions.otr.";
    let prefs = {
      requireEncryption: true,
      verifyNudge: true
    };
    let defaults = Services.prefs.getDefaultBranch(branch);
    Object.keys(prefs).forEach(function(key) {
      defaults.setBoolPref(key, prefs[key]);
    });
    ui.prefs = Services.prefs.getBranch(branch);
  },

  // Kind of hacky but pretty effective.
  _addedNodes: [],

  addMenuObserver: function() {
    let iter = Services.ww.getWindowEnumerator();
    while (iter.hasMoreElements())
      ui.addMenus(iter.getNext());
    Services.obs.addObserver(ui, "domwindowopened", false);
  },

  removeMenuObserver: function() {
    ui._addedNodes.forEach(n => {
      let p = n.parentNode;
      if (p) p.removeChild(n);
    });
    ui._addedNodes.length = 0;
    Services.obs.removeObserver(ui, "domwindowopened");
  },

  addMenus: function(win) {
    let doc = win.document;

    // Account for unready windows
    if (doc.readyState !== "complete") {
      let listen = function() {
        win.removeEventListener("load", listen);
        ui.addMenus(win);
      };
      win.addEventListener("load", listen);
      return;
    }

    ui.addPrefMenu(doc);
    ui.addBuddyContextMenu(doc);
  },

  addPrefMenu: function(doc) {
    let toolsMenuPopup = doc.getElementById("toolsMenuPopup");
    if (!toolsMenuPopup)
      return;  // Not the tools menu

    let sep = doc.createElement("menuseparator");

    let menuitem = doc.createElement("menuitem");
    menuitem.setAttribute("label", _("prefs.label"));
    menuitem.addEventListener("command", function(e) {
      e.preventDefault();
      let features = "chrome,centerscreen,dialog=no,resizable=no,minimizable=no";
      Services.ww.openWindow(null, prefsDialog, "otrPrefs", features, null);
    });

    toolsMenuPopup.appendChild(sep);
    toolsMenuPopup.appendChild(menuitem);

    ui._addedNodes.push(sep);
    ui._addedNodes.push(menuitem);
  },

  addBuddyContextMenu: function(doc) {
    let buddyContextMenu = doc.getElementById("buddyListContextMenu");
    if (!buddyContextMenu)
      return;  // Not the buddy list context menu

    let sep = doc.createElement("menuseparator");
    let menuitem = doc.createElement("menuitem");

    menuitem.setAttribute("label", _("buddycontextmenu.label"));
    menuitem.addEventListener("command", function() {
      let target = buddyContextMenu.triggerNode;
      if (target.localName == "contact") {
        let contact = target.contact;
        let args = ui.contactWrapper(contact);
        args.wrappedJSObject = args;
        let features = "chrome,modal,centerscreen,resizable=no,minimizable=no";
        Services.ww.openWindow(null, addFingerDialog, "", features, args);
      }
    });

    buddyContextMenu.addEventListener("popupshowing", function(e) {
      let target = e.target.triggerNode;
      if (target.localName == "contact") {
        menuitem.hidden = false;
        sep.hidden = false;
      } else {
        menuitem.hidden = true;
        sep.hidden = true;
      }
    }, false);

    buddyContextMenu.appendChild(sep);
    buddyContextMenu.appendChild(menuitem);

    ui._addedNodes.push(sep);
    ui._addedNodes.push(menuitem);
  },

  init: function() {
    ui.setPrefs();
    otr.init({
      requireEncryption: ui.prefs.getBoolPref("requireEncryption"),
      verifyNudge: ui.prefs.getBoolPref("verifyNudge")
    });
    otr.addObserver(ui);
    otr.loadFiles().then(function() {
      Services.obs.addObserver(otr, "new-ui-conversation", false);
      // Disabled until #76 is resolved.
      // Services.obs.addObserver(ui, "contact-added", false);
      Services.obs.addObserver(ui, "account-added", false);
      Services.obs.addObserver(ui, "conversation-loaded", false);
      Services.obs.addObserver(ui, "conversation-closed", false);
      Services.obs.addObserver(ui, "prpl-quit", false);
      ui.prefs.addObserver("", ui, false);
      Conversations._conversations.forEach(ui.initConv);
      ui.addMenuObserver();
      return coniks.init();
    }).catch(function(err) { throw err; });
  },

  disconnect: function(aConv) {
    if (aConv)
      return otr.disconnect(aConv, true);
    let conversations = Services.conversations.getConversations();
    while (conversations.hasMoreElements()) {
      let conv = conversations.getNext();
      if (conv.isChat)<commit_msg>Remove hacky _addedNodes<commit_after>this.EXPORTED_SYMBOLS = ["ui"];

var { interfaces: Ci, utils: Cu, classes: Cc } = Components;

Cu.import("resource:///modules/imServices.jsm");
Cu.import("resource:///modules/imXPCOMUtils.jsm");
Cu.import("resource:///modules/imWindows.jsm");
Cu.import("chrome://otr/content/otr.js");
Cu.import("chrome://otr/content/coniks/coniks.js");

var privDialog = "chrome://otr/content/priv.xul";
var authDialog = "chrome://otr/content/auth.xul";
var prefsDialog = "chrome://otr/content/prefs.xul";
var addFingerDialog = "chrome://otr/content/addfinger.xul";

XPCOMUtils.defineLazyGetter(this, "_", () =>
  l10nHelper("chrome://otr/locale/ui.properties")
);

var authVerify = "otr-auth-unverified";

var authLabelMap = new Map([
  ["otr:auth-error", _("auth.error")],
  ["otr:auth-success", _("auth.success")],
  ["otr:auth-successThem", _("auth.successThem")],
  ["otr:auth-fail", _("auth.fail")],
  ["otr:auth-waiting", _("auth.waiting")],
]);

var trustMap = new Map([
  [otr.trustState.TRUST_NOT_PRIVATE, {
    startLabel: _("start.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: true,
    disableAuth: true,
    class: "not_private",
  }],
  [otr.trustState.TRUST_UNVERIFIED, {
    startLabel: _("refresh.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: false,
    class: "unverified",
  }],
  [otr.trustState.TRUST_PRIVATE, {
    startLabel: _("refresh.label"),
    authLabel: _("reauth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: false,
    class: "private",
  }],
  [otr.trustState.TRUST_FINISHED, {
    startLabel: _("start.label"),
    authLabel: _("auth.label"),
    disableStart: false,
    disableEnd: false,
    disableAuth: true,
    class: "finished",
  }],
]);

var windowRefs = new Map();

var ui = {

  debug: false,
  log: function log(msg) {
    if (!ui.debug)
      return;
    let csl = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
    csl.logStringMessage(msg);
  },

  prefs: null,
  setPrefs: function() {
    let branch = "extensions.otr.";
    let prefs = {
      requireEncryption: true,
      verifyNudge: true
    };
    let defaults = Services.prefs.getDefaultBranch(branch);
    Object.keys(prefs).forEach(function(key) {
      defaults.setBoolPref(key, prefs[key]);
    });
    ui.prefs = Services.prefs.getBranch(branch);
  },

  addMenuObserver: function() {
    let iter = Services.ww.getWindowEnumerator();
    while (iter.hasMoreElements())
      ui.addMenus(iter.getNext());
    Services.obs.addObserver(ui, "domwindowopened", false);
  },

  removeMenuObserver: function() {
    let iter = Services.ww.getWindowEnumerator();
    while (iter.hasMoreElements())
      ui.removeMenus(iter.getNext());
    Services.obs.removeObserver(ui, "domwindowopened");
  },

  addMenus: function(win) {
    let doc = win.document;
    // Account for unready windows
    if (doc.readyState !== "complete") {
      let listen = function() {
        win.removeEventListener("load", listen);
        ui.addMenus(win);
      };
      win.addEventListener("load", listen);
      return;
    }
    ui.addPrefMenu(doc);
    ui.addBuddyContextMenu(doc);
  },

  removeMenus: function(win) {
    let doc = win.document;
    ui.removePrefMenu(doc);
    ui.removeBuddyContextMenu(doc);
  },

  addPrefMenu: function(doc) {
    let toolsMenuPopup = doc.getElementById("toolsMenuPopup");
    if (!toolsMenuPopup)
      return;  // Not the tools menu
    let sep = doc.createElement("menuseparator");
    sep.setAttribute("id", "otrsep");
    let menuitem = doc.createElement("menuitem");
    menuitem.setAttribute("label", _("prefs.label"));
    menuitem.setAttribute("id", "otrpref");
    menuitem.addEventListener("command", function(e) {
      e.preventDefault();
      let features = "chrome,centerscreen,dialog=no,resizable=no,minimizable=no";
      Services.ww.openWindow(null, prefsDialog, "otrPrefs", features, null);
    });
    toolsMenuPopup.appendChild(sep);
    toolsMenuPopup.appendChild(menuitem);
  },

  removePrefMenu: function(doc) {
    let s = doc.getElementById("otrsep");
    if (s)
      s.parentNode.removeChild(s);
    let p = doc.getElementById("otrpref");
    if (p)
      p.parentNode.removeChild(p);
  },

  addBuddyContextMenu: function(doc) {
    let buddyContextMenu = doc.getElementById("buddyListContextMenu");
    if (!buddyContextMenu)
      return;  // Not the buddy list context menu
    let sep = doc.createElement("menuseparator");
    sep.setAttribute("id", "otrsep");
    let menuitem = doc.createElement("menuitem");
    menuitem.setAttribute("label", _("buddycontextmenu.label"));
    menuitem.setAttribute("id", "otrcont");
    menuitem.addEventListener("command", function() {
      let target = buddyContextMenu.triggerNode;
      if (target.localName == "contact") {
        let contact = target.contact;
        let args = ui.contactWrapper(contact);
        args.wrappedJSObject = args;
        let features = "chrome,modal,centerscreen,resizable=no,minimizable=no";
        Services.ww.openWindow(null, addFingerDialog, "", features, args);
      }
    });
    buddyContextMenu.addEventListener("popupshowing", function(e) {
      let target = e.target.triggerNode;
      if (target.localName == "contact") {
        menuitem.hidden = false;
        sep.hidden = false;
      } else {
        menuitem.hidden = true;
        sep.hidden = true;
      }
    }, false);
    buddyContextMenu.appendChild(sep);
    buddyContextMenu.appendChild(menuitem);
  },

  removeBuddyContextMenu: function(doc) {
    let s = doc.getElementById("otrsep");
    if (s)
      s.parentNode.removeChild(s);
    let p = doc.getElementById("otrcont");
    if (p)
      p.parentNode.removeChild(p);
  },

  init: function() {
    ui.setPrefs();
    otr.init({
      requireEncryption: ui.prefs.getBoolPref("requireEncryption"),
      verifyNudge: ui.prefs.getBoolPref("verifyNudge")
    });
    otr.addObserver(ui);
    otr.loadFiles().then(function() {
      Services.obs.addObserver(otr, "new-ui-conversation", false);
      // Disabled until #76 is resolved.
      // Services.obs.addObserver(ui, "contact-added", false);
      Services.obs.addObserver(ui, "account-added", false);
      Services.obs.addObserver(ui, "conversation-loaded", false);
      Services.obs.addObserver(ui, "conversation-closed", false);
      Services.obs.addObserver(ui, "prpl-quit", false);
      ui.prefs.addObserver("", ui, false);
      Conversations._conversations.forEach(ui.initConv);
      ui.addMenuObserver();
      return coniks.init();
    }).catch(function(err) { throw err; });
  },

  disconnect: function(aConv) {
    if (aConv)
      return otr.disconnect(aConv, true);
    let conversations = Services.conversations.getConversations();
    while (conversations.hasMoreElements()) {
      let conv = conversations.getNext();
      if (conv.isChat)
{'fraction_non_alphanumeric': 0.15674262233843855, 'fraction_numerical': 0.0008218154650728427, 'mean_word_length': 3.505553685627735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '616963', 'n_tokens_mistral': 4404, 'n_tokens_neox': 4396, 'n_words': 744}