browser/base/content/sync/quota.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cc = Components.classes;
michael@0 7 const Cr = Components.results;
michael@0 8 const Cu = Components.utils;
michael@0 9
michael@0 10 Cu.import("resource://services-sync/main.js");
michael@0 11 Cu.import("resource://gre/modules/DownloadUtils.jsm");
michael@0 12
michael@0 13 let gSyncQuota = {
michael@0 14
michael@0 15 init: function init() {
michael@0 16 this.bundle = document.getElementById("quotaStrings");
michael@0 17 let caption = document.getElementById("treeCaption");
michael@0 18 caption.firstChild.nodeValue = this.bundle.getString("quota.treeCaption.label");
michael@0 19
michael@0 20 gUsageTreeView.init();
michael@0 21 this.tree = document.getElementById("usageTree");
michael@0 22 this.tree.view = gUsageTreeView;
michael@0 23
michael@0 24 this.loadData();
michael@0 25 },
michael@0 26
michael@0 27 loadData: function loadData() {
michael@0 28 this._usage_req = Weave.Service.getStorageInfo(Weave.INFO_COLLECTION_USAGE,
michael@0 29 function (error, usage) {
michael@0 30 delete gSyncQuota._usage_req;
michael@0 31 // displayUsageData handles null values, so no need to check 'error'.
michael@0 32 gUsageTreeView.displayUsageData(usage);
michael@0 33 });
michael@0 34
michael@0 35 let usageLabel = document.getElementById("usageLabel");
michael@0 36 let bundle = this.bundle;
michael@0 37
michael@0 38 this._quota_req = Weave.Service.getStorageInfo(Weave.INFO_QUOTA,
michael@0 39 function (error, quota) {
michael@0 40 delete gSyncQuota._quota_req;
michael@0 41
michael@0 42 if (error) {
michael@0 43 usageLabel.value = bundle.getString("quota.usageError.label");
michael@0 44 return;
michael@0 45 }
michael@0 46 let used = gSyncQuota.convertKB(quota[0]);
michael@0 47 if (!quota[1]) {
michael@0 48 // No quota on the server.
michael@0 49 usageLabel.value = bundle.getFormattedString(
michael@0 50 "quota.usageNoQuota.label", used);
michael@0 51 return;
michael@0 52 }
michael@0 53 let percent = Math.round(100 * quota[0] / quota[1]);
michael@0 54 let total = gSyncQuota.convertKB(quota[1]);
michael@0 55 usageLabel.value = bundle.getFormattedString(
michael@0 56 "quota.usagePercentage.label", [percent].concat(used).concat(total));
michael@0 57 });
michael@0 58 },
michael@0 59
michael@0 60 onCancel: function onCancel() {
michael@0 61 if (this._usage_req) {
michael@0 62 this._usage_req.abort();
michael@0 63 }
michael@0 64 if (this._quota_req) {
michael@0 65 this._quota_req.abort();
michael@0 66 }
michael@0 67 return true;
michael@0 68 },
michael@0 69
michael@0 70 onAccept: function onAccept() {
michael@0 71 let engines = gUsageTreeView.getEnginesToDisable();
michael@0 72 for each (let engine in engines) {
michael@0 73 Weave.Service.engineManager.get(engine).enabled = false;
michael@0 74 }
michael@0 75 if (engines.length) {
michael@0 76 // The 'Weave' object will disappear once the window closes.
michael@0 77 let Service = Weave.Service;
michael@0 78 Weave.Utils.nextTick(function() { Service.sync(); });
michael@0 79 }
michael@0 80 return this.onCancel();
michael@0 81 },
michael@0 82
michael@0 83 convertKB: function convertKB(value) {
michael@0 84 return DownloadUtils.convertByteUnits(value * 1024);
michael@0 85 }
michael@0 86
michael@0 87 };
michael@0 88
michael@0 89 let gUsageTreeView = {
michael@0 90
michael@0 91 _ignored: {keys: true,
michael@0 92 meta: true,
michael@0 93 clients: true},
michael@0 94
michael@0 95 /*
michael@0 96 * Internal data structures underlaying the tree.
michael@0 97 */
michael@0 98 _collections: [],
michael@0 99 _byname: {},
michael@0 100
michael@0 101 init: function init() {
michael@0 102 let retrievingLabel = gSyncQuota.bundle.getString("quota.retrieving.label");
michael@0 103 for each (let engine in Weave.Service.engineManager.getEnabled()) {
michael@0 104 if (this._ignored[engine.name])
michael@0 105 continue;
michael@0 106
michael@0 107 // Some engines use the same pref, which means they can only be turned on
michael@0 108 // and off together. We need to combine them here as well.
michael@0 109 let existing = this._byname[engine.prefName];
michael@0 110 if (existing) {
michael@0 111 existing.engines.push(engine.name);
michael@0 112 continue;
michael@0 113 }
michael@0 114
michael@0 115 let obj = {name: engine.prefName,
michael@0 116 title: this._collectionTitle(engine),
michael@0 117 engines: [engine.name],
michael@0 118 enabled: true,
michael@0 119 sizeLabel: retrievingLabel};
michael@0 120 this._collections.push(obj);
michael@0 121 this._byname[engine.prefName] = obj;
michael@0 122 }
michael@0 123 },
michael@0 124
michael@0 125 _collectionTitle: function _collectionTitle(engine) {
michael@0 126 try {
michael@0 127 return gSyncQuota.bundle.getString(
michael@0 128 "collection." + engine.prefName + ".label");
michael@0 129 } catch (ex) {
michael@0 130 return engine.Name;
michael@0 131 }
michael@0 132 },
michael@0 133
michael@0 134 /*
michael@0 135 * Process the quota information as returned by info/collection_usage.
michael@0 136 */
michael@0 137 displayUsageData: function displayUsageData(data) {
michael@0 138 for each (let coll in this._collections) {
michael@0 139 coll.size = 0;
michael@0 140 // If we couldn't retrieve any data, just blank out the label.
michael@0 141 if (!data) {
michael@0 142 coll.sizeLabel = "";
michael@0 143 continue;
michael@0 144 }
michael@0 145
michael@0 146 for each (let engineName in coll.engines)
michael@0 147 coll.size += data[engineName] || 0;
michael@0 148 let sizeLabel = "";
michael@0 149 sizeLabel = gSyncQuota.bundle.getFormattedString(
michael@0 150 "quota.sizeValueUnit.label", gSyncQuota.convertKB(coll.size));
michael@0 151 coll.sizeLabel = sizeLabel;
michael@0 152 }
michael@0 153 let sizeColumn = this.treeBox.columns.getNamedColumn("size");
michael@0 154 this.treeBox.invalidateColumn(sizeColumn);
michael@0 155 },
michael@0 156
michael@0 157 /*
michael@0 158 * Handle click events on the tree.
michael@0 159 */
michael@0 160 onTreeClick: function onTreeClick(event) {
michael@0 161 if (event.button == 2)
michael@0 162 return;
michael@0 163
michael@0 164 let row = {}, col = {};
michael@0 165 this.treeBox.getCellAt(event.clientX, event.clientY, row, col, {});
michael@0 166 if (col.value && col.value.id == "enabled")
michael@0 167 this.toggle(row.value);
michael@0 168 },
michael@0 169
michael@0 170 /*
michael@0 171 * Toggle enabled state of an engine.
michael@0 172 */
michael@0 173 toggle: function toggle(row) {
michael@0 174 // Update the tree
michael@0 175 let collection = this._collections[row];
michael@0 176 collection.enabled = !collection.enabled;
michael@0 177 this.treeBox.invalidateRow(row);
michael@0 178
michael@0 179 // Display which ones will be removed
michael@0 180 let freeup = 0;
michael@0 181 let toremove = [];
michael@0 182 for each (collection in this._collections) {
michael@0 183 if (collection.enabled)
michael@0 184 continue;
michael@0 185 toremove.push(collection.name);
michael@0 186 freeup += collection.size;
michael@0 187 }
michael@0 188
michael@0 189 let caption = document.getElementById("treeCaption");
michael@0 190 if (!toremove.length) {
michael@0 191 caption.className = "";
michael@0 192 caption.firstChild.nodeValue = gSyncQuota.bundle.getString(
michael@0 193 "quota.treeCaption.label");
michael@0 194 return;
michael@0 195 }
michael@0 196
michael@0 197 toremove = [this._byname[coll].title for each (coll in toremove)];
michael@0 198 toremove = toremove.join(gSyncQuota.bundle.getString("quota.list.separator"));
michael@0 199 caption.firstChild.nodeValue = gSyncQuota.bundle.getFormattedString(
michael@0 200 "quota.removal.label", [toremove]);
michael@0 201 if (freeup)
michael@0 202 caption.firstChild.nodeValue += gSyncQuota.bundle.getFormattedString(
michael@0 203 "quota.freeup.label", gSyncQuota.convertKB(freeup));
michael@0 204 caption.className = "captionWarning";
michael@0 205 },
michael@0 206
michael@0 207 /*
michael@0 208 * Return a list of engines (or rather their pref names) that should be
michael@0 209 * disabled.
michael@0 210 */
michael@0 211 getEnginesToDisable: function getEnginesToDisable() {
michael@0 212 return [coll.name for each (coll in this._collections) if (!coll.enabled)];
michael@0 213 },
michael@0 214
michael@0 215 // nsITreeView
michael@0 216
michael@0 217 get rowCount() {
michael@0 218 return this._collections.length;
michael@0 219 },
michael@0 220
michael@0 221 getRowProperties: function(index) { return ""; },
michael@0 222 getCellProperties: function(row, col) { return ""; },
michael@0 223 getColumnProperties: function(col) { return ""; },
michael@0 224 isContainer: function(index) { return false; },
michael@0 225 isContainerOpen: function(index) { return false; },
michael@0 226 isContainerEmpty: function(index) { return false; },
michael@0 227 isSeparator: function(index) { return false; },
michael@0 228 isSorted: function() { return false; },
michael@0 229 canDrop: function(index, orientation, dataTransfer) { return false; },
michael@0 230 drop: function(row, orientation, dataTransfer) {},
michael@0 231 getParentIndex: function(rowIndex) {},
michael@0 232 hasNextSibling: function(rowIndex, afterIndex) { return false; },
michael@0 233 getLevel: function(index) { return 0; },
michael@0 234 getImageSrc: function(row, col) {},
michael@0 235
michael@0 236 getCellValue: function(row, col) {
michael@0 237 return this._collections[row].enabled;
michael@0 238 },
michael@0 239
michael@0 240 getCellText: function getCellText(row, col) {
michael@0 241 let collection = this._collections[row];
michael@0 242 switch (col.id) {
michael@0 243 case "collection":
michael@0 244 return collection.title;
michael@0 245 case "size":
michael@0 246 return collection.sizeLabel;
michael@0 247 default:
michael@0 248 return "";
michael@0 249 }
michael@0 250 },
michael@0 251
michael@0 252 setTree: function setTree(tree) {
michael@0 253 this.treeBox = tree;
michael@0 254 },
michael@0 255
michael@0 256 toggleOpenState: function(index) {},
michael@0 257 cycleHeader: function(col) {},
michael@0 258 selectionChanged: function() {},
michael@0 259 cycleCell: function(row, col) {},
michael@0 260 isEditable: function(row, col) { return false; },
michael@0 261 isSelectable: function (row, col) { return false; },
michael@0 262 setCellValue: function(row, col, value) {},
michael@0 263 setCellText: function(row, col, value) {},
michael@0 264 performAction: function(action) {},
michael@0 265 performActionOnRow: function(action, row) {},
michael@0 266 performActionOnCell: function(action, row, col) {}
michael@0 267
michael@0 268 };

mercurial