toolkit/mozapps/extensions/content/selectAddons.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/content/selectAddons.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,347 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +"use strict";
    1.11 +
    1.12 +Components.utils.import("resource://gre/modules/AddonManager.jsm");
    1.13 +Components.utils.import("resource://gre/modules/addons/AddonRepository.jsm");
    1.14 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +const Cc = Components.classes;
    1.17 +const Ci = Components.interfaces;
    1.18 +
    1.19 +var gView = null;
    1.20 +
    1.21 +function showView(aView) {
    1.22 +  gView = aView;
    1.23 +
    1.24 +  gView.show();
    1.25 +
    1.26 +  // If the view's show method immediately showed a different view then don't
    1.27 +  // do anything else
    1.28 +  if (gView != aView)
    1.29 +    return;
    1.30 +
    1.31 +  let viewNode = document.getElementById(gView.nodeID);
    1.32 +  viewNode.parentNode.selectedPanel = viewNode;
    1.33 +
    1.34 +  // For testing dispatch an event when the view changes
    1.35 +  var event = document.createEvent("Events");
    1.36 +  event.initEvent("ViewChanged", true, true);
    1.37 +  viewNode.dispatchEvent(event);
    1.38 +}
    1.39 +
    1.40 +function showButtons(aCancel, aBack, aNext, aDone) {
    1.41 +  document.getElementById("cancel").hidden = !aCancel;
    1.42 +  document.getElementById("back").hidden = !aBack;
    1.43 +  document.getElementById("next").hidden = !aNext;
    1.44 +  document.getElementById("done").hidden = !aDone;
    1.45 +}
    1.46 +
    1.47 +function isAddonDistroInstalled(aID) {
    1.48 +  let branch = Services.prefs.getBranch("extensions.installedDistroAddon.");
    1.49 +  if (!branch.prefHasUserValue(aID))
    1.50 +    return false;
    1.51 +
    1.52 +  return branch.getBoolPref(aID);
    1.53 +}
    1.54 +
    1.55 +function orderForScope(aScope) {
    1.56 +  return aScope == AddonManager.SCOPE_PROFILE ? 1 : 0;
    1.57 +}
    1.58 +
    1.59 +var gAddons = {};
    1.60 +
    1.61 +var gChecking = {
    1.62 +  nodeID: "checking",
    1.63 +
    1.64 +  _progress: null,
    1.65 +  _addonCount: 0,
    1.66 +  _completeCount: 0,
    1.67 +
    1.68 +  show: function gChecking_show() {
    1.69 +    showButtons(true, false, false, false);
    1.70 +    this._progress = document.getElementById("checking-progress");
    1.71 +
    1.72 +    let self = this;
    1.73 +    AddonManager.getAllAddons(function gChecking_getAllAddons(aAddons) {
    1.74 +      if (aAddons.length == 0) {
    1.75 +        window.close();
    1.76 +        return;
    1.77 +      }
    1.78 +
    1.79 +      aAddons = aAddons.filter(function gChecking_filterAddons(aAddon) {
    1.80 +        if (aAddon.type == "plugin" || aAddon.type == "service")
    1.81 +          return false;
    1.82 +
    1.83 +        if (aAddon.type == "theme") {
    1.84 +          // Don't show application shipped themes
    1.85 +          if (aAddon.scope == AddonManager.SCOPE_APPLICATION)
    1.86 +            return false;
    1.87 +          // Don't show already disabled themes
    1.88 +          if (aAddon.userDisabled)
    1.89 +            return false;
    1.90 +        }
    1.91 +
    1.92 +        return true;
    1.93 +      });
    1.94 +
    1.95 +      self._addonCount = aAddons.length;
    1.96 +      self._progress.value = 0;
    1.97 +      self._progress.max = aAddons.length;
    1.98 +      self._progress.mode = "determined";
    1.99 +
   1.100 +      // Ensure compatibility overrides are up to date before checking for
   1.101 +      // individual addon updates.
   1.102 +      let ids = [addon.id for each (addon in aAddons)];
   1.103 +      AddonRepository.repopulateCache(ids, function gChecking_repopulateCache() {
   1.104 +        for (let addonItem of aAddons) {
   1.105 +          // Ignore disabled themes
   1.106 +          if (addonItem.type != "theme" || !addonItem.userDisabled) {
   1.107 +            gAddons[addonItem.id] = {
   1.108 +              addon: addonItem,
   1.109 +              install: null,
   1.110 +              wasActive: addonItem.isActive
   1.111 +            }
   1.112 +          }
   1.113 +
   1.114 +          addonItem.findUpdates(self, AddonManager.UPDATE_WHEN_NEW_APP_INSTALLED);
   1.115 +        }
   1.116 +      });
   1.117 +    });
   1.118 +  },
   1.119 +
   1.120 +  onUpdateAvailable: function gChecking_onUpdateAvailable(aAddon, aInstall) {
   1.121 +    // If the add-on can be upgraded then remember the new version
   1.122 +    if (aAddon.permissions & AddonManager.PERM_CAN_UPGRADE)
   1.123 +      gAddons[aAddon.id].install = aInstall;
   1.124 +  },
   1.125 +
   1.126 +  onUpdateFinished: function gChecking_onUpdateFinished(aAddon, aError) {
   1.127 +    this._completeCount++;
   1.128 +    this._progress.value = this._completeCount;
   1.129 +
   1.130 +    if (this._completeCount < this._addonCount)
   1.131 +      return;
   1.132 +
   1.133 +    var addons = [gAddons[id] for (id in gAddons)];
   1.134 +
   1.135 +    addons.sort(function sortAddons(a, b) {
   1.136 +      let orderA = orderForScope(a.addon.scope);
   1.137 +      let orderB = orderForScope(b.addon.scope);
   1.138 +
   1.139 +      if (orderA != orderB)
   1.140 +        return orderA - orderB;
   1.141 +
   1.142 +      return String.localeCompare(a.addon.name, b.addon.name);
   1.143 +    });
   1.144 +
   1.145 +    let rows = document.getElementById("select-rows");
   1.146 +    let lastAddon = null;
   1.147 +    for (let entry of addons) {
   1.148 +      if (lastAddon &&
   1.149 +          orderForScope(entry.addon.scope) != orderForScope(lastAddon.scope)) {
   1.150 +        let separator = document.createElement("separator");
   1.151 +        rows.appendChild(separator);
   1.152 +      }
   1.153 +
   1.154 +      let row = document.createElement("row");
   1.155 +      row.setAttribute("id", entry.addon.id);
   1.156 +      row.setAttribute("class", "addon");
   1.157 +      rows.appendChild(row);
   1.158 +      row.setAddon(entry.addon, entry.install, entry.wasActive,
   1.159 +                   isAddonDistroInstalled(entry.addon.id));
   1.160 +
   1.161 +      if (entry.install)
   1.162 +        entry.install.addListener(gUpdate);
   1.163 +
   1.164 +      lastAddon = entry.addon;
   1.165 +    }
   1.166 +
   1.167 +    showView(gSelect);
   1.168 +  }
   1.169 +};
   1.170 +
   1.171 +var gSelect = {
   1.172 +  nodeID: "select",
   1.173 +
   1.174 +  show: function gSelect_show() {
   1.175 +    this.updateButtons();
   1.176 +  },
   1.177 +
   1.178 +  updateButtons: function gSelect_updateButtons() {
   1.179 +    for (let row = document.getElementById("select-rows").firstChild;
   1.180 +         row; row = row.nextSibling) {
   1.181 +      if (row.localName == "separator")
   1.182 +        continue;
   1.183 +
   1.184 +      if (row.action) {
   1.185 +        showButtons(false, false, true, false);
   1.186 +        return;
   1.187 +      }
   1.188 +    }
   1.189 +
   1.190 +    showButtons(false, false, false, true);
   1.191 +  },
   1.192 +
   1.193 +  next: function gSelect_next() {
   1.194 +    showView(gConfirm);
   1.195 +  },
   1.196 +
   1.197 +  done: function gSelect_done() {
   1.198 +    window.close();
   1.199 +  }
   1.200 +};
   1.201 +
   1.202 +var gConfirm = {
   1.203 +  nodeID: "confirm",
   1.204 +
   1.205 +  show: function gConfirm_show() {
   1.206 +    showButtons(false, true, false, true);
   1.207 +
   1.208 +    let box = document.getElementById("confirm-scrollbox").firstChild;
   1.209 +    while (box) {
   1.210 +      box.hidden = true;
   1.211 +      while (box.lastChild != box.firstChild)
   1.212 +        box.removeChild(box.lastChild);
   1.213 +      box = box.nextSibling;
   1.214 +    }
   1.215 +
   1.216 +    for (let row = document.getElementById("select-rows").firstChild;
   1.217 +         row; row = row.nextSibling) {
   1.218 +      if (row.localName == "separator")
   1.219 +        continue;
   1.220 +
   1.221 +      let action = row.action;
   1.222 +      if (!action)
   1.223 +        continue;
   1.224 +
   1.225 +      let list = document.getElementById(action + "-list");
   1.226 +
   1.227 +      list.hidden = false;
   1.228 +      let item = document.createElement("hbox");
   1.229 +      item.setAttribute("id", row._addon.id);
   1.230 +      item.setAttribute("class", "addon");
   1.231 +      item.setAttribute("type", row._addon.type);
   1.232 +      item.setAttribute("name", row._addon.name);
   1.233 +      if (action == "update" || action == "enable")
   1.234 +        item.setAttribute("active", "true");
   1.235 +      list.appendChild(item);
   1.236 +
   1.237 +      if (action == "update")
   1.238 +        showButtons(false, true, true, false);
   1.239 +    }
   1.240 +  },
   1.241 +
   1.242 +  back: function gConfirm_back() {
   1.243 +    showView(gSelect);
   1.244 +  },
   1.245 +
   1.246 +  next: function gConfirm_next() {
   1.247 +    showView(gUpdate);
   1.248 +  },
   1.249 +
   1.250 +  done: function gConfirm_done() {
   1.251 +    for (let row = document.getElementById("select-rows").firstChild;
   1.252 +         row; row = row.nextSibling) {
   1.253 +      if (row.localName != "separator")
   1.254 +        row.apply();
   1.255 +    }
   1.256 +
   1.257 +    window.close();
   1.258 +  }
   1.259 +}
   1.260 +
   1.261 +var gUpdate = {
   1.262 +  nodeID: "update",
   1.263 +
   1.264 +  _progress: null,
   1.265 +  _waitingCount: 0,
   1.266 +  _completeCount: 0,
   1.267 +  _errorCount: 0,
   1.268 +
   1.269 +  show: function gUpdate_show() {
   1.270 +    showButtons(true, false, false, false);
   1.271 +
   1.272 +    this._progress = document.getElementById("update-progress");
   1.273 +
   1.274 +    for (let row = document.getElementById("select-rows").firstChild;
   1.275 +         row; row = row.nextSibling) {
   1.276 +      if (row.localName != "separator")
   1.277 +        row.apply();
   1.278 +    }
   1.279 +
   1.280 +    this._progress.mode = "determined";
   1.281 +    this._progress.max = this._waitingCount;
   1.282 +    this._progress.value = this._completeCount;
   1.283 +  },
   1.284 +
   1.285 +  checkComplete: function gUpdate_checkComplete() {
   1.286 +    this._progress.value = this._completeCount;
   1.287 +    if (this._completeCount < this._waitingCount)
   1.288 +      return;
   1.289 +
   1.290 +    if (this._errorCount > 0) {
   1.291 +      showView(gErrors);
   1.292 +      return;
   1.293 +    }
   1.294 +
   1.295 +    window.close();
   1.296 +  },
   1.297 +
   1.298 +  onDownloadStarted: function gUpdate_onDownloadStarted(aInstall) {
   1.299 +    this._waitingCount++;
   1.300 +  },
   1.301 +
   1.302 +  onDownloadFailed: function gUpdate_onDownloadFailed(aInstall) {
   1.303 +    this._errorCount++;
   1.304 +    this._completeCount++;
   1.305 +    this.checkComplete();
   1.306 +  },
   1.307 +
   1.308 +  onInstallFailed: function gUpdate_onInstallFailed(aInstall) {
   1.309 +    this._errorCount++;
   1.310 +    this._completeCount++;
   1.311 +    this.checkComplete();
   1.312 +  },
   1.313 +
   1.314 +  onInstallEnded: function gUpdate_onInstallEnded(aInstall) {
   1.315 +    this._completeCount++;
   1.316 +    this.checkComplete();
   1.317 +  }
   1.318 +};
   1.319 +
   1.320 +var gErrors = {
   1.321 +  nodeID: "errors",
   1.322 +
   1.323 +  show: function gErrors_show() {
   1.324 +    showButtons(false, false, false, true);
   1.325 +  },
   1.326 +
   1.327 +  done: function gErrors_done() {
   1.328 +    window.close();
   1.329 +  }
   1.330 +};
   1.331 +
   1.332 +window.addEventListener("load", function loadEventListener() {
   1.333 +                                         showView(gChecking); }, false);
   1.334 +
   1.335 +// When closing the window cancel any pending or in-progress installs
   1.336 +window.addEventListener("unload", function unloadEventListener() {
   1.337 +  for (let id in gAddons) {
   1.338 +    let entry = gAddons[id];
   1.339 +    if (!entry.install)
   1.340 +      return;
   1.341 +
   1.342 +    aEntry.install.removeListener(gUpdate);
   1.343 +
   1.344 +    if (entry.install.state != AddonManager.STATE_INSTALLED &&
   1.345 +        entry.install.state != AddonManager.STATE_DOWNLOAD_FAILED &&
   1.346 +        entry.install.state != AddonManager.STATE_INSTALL_FAILED) {
   1.347 +      entry.install.cancel();
   1.348 +    }
   1.349 +  }
   1.350 +}, false);

mercurial