quota.js 7.1 KB

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