michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: function ItemPinHelper(aUnpinnedPrefName) { michael@0: this._prefKey = aUnpinnedPrefName; michael@0: } michael@0: michael@0: // Cache preferences on a static variable shared michael@0: // by all instances registered to the same pref key. michael@0: ItemPinHelper._prefValue = {}; michael@0: michael@0: ItemPinHelper.prototype = { michael@0: _getPrefValue: function _getPrefValue() { michael@0: if (ItemPinHelper._prefValue[this._prefKey]) michael@0: return ItemPinHelper._prefValue[this._prefKey]; michael@0: michael@0: try { michael@0: // getComplexValue throws if pref never set. Really. michael@0: let prefValue = Services.prefs.getComplexValue(this._prefKey, Ci.nsISupportsString); michael@0: ItemPinHelper._prefValue[this._prefKey] = JSON.parse(prefValue.data); michael@0: } catch(e) { michael@0: ItemPinHelper._prefValue[this._prefKey] = []; michael@0: } michael@0: michael@0: return ItemPinHelper._prefValue[this._prefKey]; michael@0: }, michael@0: michael@0: _setPrefValue: function _setPrefValue(aNewValue) { michael@0: let stringified = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString); michael@0: stringified.data = JSON.stringify(aNewValue); michael@0: michael@0: Services.prefs.setComplexValue(this._prefKey, Ci.nsISupportsString, stringified); michael@0: ItemPinHelper._prefValue[this._prefKey] = aNewValue; michael@0: }, michael@0: michael@0: isPinned: function isPinned(aItemId) { michael@0: // Bookmarks are visible on StartUI (pinned) by default michael@0: return this._getPrefValue().indexOf(aItemId) === -1; michael@0: }, michael@0: michael@0: setUnpinned: function setPinned(aItemId) { michael@0: let unpinned = this._getPrefValue(); michael@0: unpinned.push(aItemId); michael@0: this._setPrefValue(unpinned); michael@0: }, michael@0: michael@0: setPinned: function unsetPinned(aItemId) { michael@0: let unpinned = this._getPrefValue(); michael@0: michael@0: let index = unpinned.indexOf(aItemId); michael@0: unpinned.splice(index, 1); michael@0: michael@0: this._setPrefValue(unpinned); michael@0: }, michael@0: }