2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-06-16 17:19:11 -07:00
|
|
|
|
|
|
|
// **********
|
2010-07-18 08:58:10 -07:00
|
|
|
// Title: storage.js
|
2010-05-11 17:22:06 -07:00
|
|
|
|
2010-04-22 14:26:57 -07:00
|
|
|
// ##########
|
2010-07-15 17:23:39 -07:00
|
|
|
// Class: Storage
|
2010-07-29 12:37:25 -07:00
|
|
|
// Singleton for permanent storage of TabView data.
|
2010-08-11 19:01:29 -07:00
|
|
|
let Storage = {
|
2010-08-11 15:28:45 -07:00
|
|
|
GROUP_DATA_IDENTIFIER: "tabview-group",
|
2010-07-29 12:37:25 -07:00
|
|
|
GROUPS_DATA_IDENTIFIER: "tabview-groups",
|
2010-08-11 15:28:45 -07:00
|
|
|
TAB_DATA_IDENTIFIER: "tabview-tab",
|
|
|
|
UI_DATA_IDENTIFIER: "tabview-ui",
|
2010-05-21 15:44:15 -07:00
|
|
|
|
2011-02-26 06:28:00 -08:00
|
|
|
// ----------
|
|
|
|
// Function: toString
|
|
|
|
// Prints [Storage] for debug use
|
|
|
|
toString: function Storage_toString() {
|
|
|
|
return "[Storage]";
|
|
|
|
},
|
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: init
|
2010-07-18 08:58:10 -07:00
|
|
|
// Sets up the object.
|
2010-09-08 10:02:08 -07:00
|
|
|
init: function Storage_init() {
|
2010-07-02 21:33:33 -07:00
|
|
|
this._sessionStore =
|
2010-08-11 15:28:45 -07:00
|
|
|
Cc["@mozilla.org/browser/sessionstore;1"].
|
|
|
|
getService(Ci.nsISessionStore);
|
2010-05-21 15:44:15 -07:00
|
|
|
},
|
|
|
|
|
2010-08-11 21:36:58 -07:00
|
|
|
// ----------
|
|
|
|
// Function: uninit
|
2010-09-08 10:02:08 -07:00
|
|
|
uninit: function Storage_uninit () {
|
2010-08-11 21:36:58 -07:00
|
|
|
this._sessionStore = null;
|
|
|
|
},
|
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: saveTab
|
2010-07-18 08:58:10 -07:00
|
|
|
// Saves the data for a single tab.
|
2010-09-08 10:02:08 -07:00
|
|
|
saveTab: function Storage_saveTab(tab, data) {
|
2010-08-10 11:13:10 -07:00
|
|
|
Utils.assert(tab, "tab");
|
2010-06-14 16:56:27 -07:00
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
this._sessionStore.setTabValue(tab, this.TAB_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(data));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: getTabData
|
2011-09-01 06:17:27 -07:00
|
|
|
// Load tab data from session store and return it.
|
|
|
|
getTabData: function Storage_getTabData(tab) {
|
2010-08-10 11:13:10 -07:00
|
|
|
Utils.assert(tab, "tab");
|
2011-02-19 10:22:49 -08:00
|
|
|
|
|
|
|
let existingData = null;
|
2010-06-14 16:56:27 -07:00
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
try {
|
2011-02-19 10:22:49 -08:00
|
|
|
let tabData = this._sessionStore.getTabValue(tab, this.TAB_DATA_IDENTIFIER);
|
2011-09-01 06:17:27 -07:00
|
|
|
if (tabData != "")
|
2010-05-21 15:44:15 -07:00
|
|
|
existingData = JSON.parse(tabData);
|
|
|
|
} catch (e) {
|
2011-02-19 10:22:49 -08:00
|
|
|
// getTabValue will fail if the property doesn't exist.
|
2010-06-14 16:56:27 -07:00
|
|
|
Utils.log(e);
|
2010-05-21 15:44:15 -07:00
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
return existingData;
|
|
|
|
},
|
|
|
|
|
2012-03-27 03:59:09 -07:00
|
|
|
// ----------
|
|
|
|
// Function: getTabState
|
|
|
|
// Returns the current state of the given tab.
|
|
|
|
getTabState: function Storage_getTabState(tab) {
|
|
|
|
Utils.assert(tab, "tab");
|
|
|
|
let tabState;
|
|
|
|
|
|
|
|
try {
|
|
|
|
tabState = JSON.parse(this._sessionStore.getTabState(tab));
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return tabState;
|
|
|
|
},
|
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
// ----------
|
2010-08-06 15:46:55 -07:00
|
|
|
// Function: saveGroupItem
|
|
|
|
// Saves the data for a single groupItem, associated with a specific window.
|
2010-09-08 10:02:08 -07:00
|
|
|
saveGroupItem: function Storage_saveGroupItem(win, data) {
|
2010-05-21 15:44:15 -07:00
|
|
|
var id = data.id;
|
2010-08-06 15:46:55 -07:00
|
|
|
var existingData = this.readGroupItemData(win);
|
2010-05-21 15:44:15 -07:00
|
|
|
existingData[id] = data;
|
|
|
|
this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(existingData));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 15:46:55 -07:00
|
|
|
// Function: deleteGroupItem
|
|
|
|
// Deletes the data for a single groupItem from the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
deleteGroupItem: function Storage_deleteGroupItem(win, id) {
|
2010-08-06 15:46:55 -07:00
|
|
|
var existingData = this.readGroupItemData(win);
|
2010-05-21 15:44:15 -07:00
|
|
|
delete existingData[id];
|
|
|
|
this._sessionStore.setWindowValue(win, this.GROUP_DATA_IDENTIFIER,
|
|
|
|
JSON.stringify(existingData));
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 15:46:55 -07:00
|
|
|
// Function: readGroupItemData
|
|
|
|
// Returns the data for all groupItems associated with the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
readGroupItemData: function Storage_readGroupItemData(win) {
|
2010-05-21 15:44:15 -07:00
|
|
|
var existingData = {};
|
2010-09-24 17:24:31 -07:00
|
|
|
let data;
|
2010-05-21 15:44:15 -07:00
|
|
|
try {
|
2010-09-24 17:24:31 -07:00
|
|
|
data = this._sessionStore.getWindowValue(win, this.GROUP_DATA_IDENTIFIER);
|
|
|
|
if (data)
|
|
|
|
existingData = JSON.parse(data);
|
2010-05-21 15:44:15 -07:00
|
|
|
} catch (e) {
|
|
|
|
// getWindowValue will fail if the property doesn't exist
|
2010-09-24 17:24:31 -07:00
|
|
|
Utils.log("Error in readGroupItemData: "+e, data);
|
2010-05-21 15:44:15 -07:00
|
|
|
}
|
|
|
|
return existingData;
|
|
|
|
},
|
|
|
|
|
2011-08-16 02:06:14 -07:00
|
|
|
// ----------
|
|
|
|
// Function: readWindowBusyState
|
|
|
|
// Returns the current busyState for the given window.
|
|
|
|
readWindowBusyState: function Storage_readWindowBusyState(win) {
|
|
|
|
let state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let data = this._sessionStore.getWindowState(win);
|
|
|
|
if (data)
|
|
|
|
state = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error while parsing window state");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (state && state.windows[0].busy);
|
|
|
|
},
|
|
|
|
|
2010-05-21 15:44:15 -07:00
|
|
|
// ----------
|
2010-08-06 15:46:55 -07:00
|
|
|
// Function: saveGroupItemsData
|
|
|
|
// Saves the global data for the <GroupItems> singleton for the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
saveGroupItemsData: function Storage_saveGroupItemsData(win, data) {
|
2010-05-24 14:49:03 -07:00
|
|
|
this.saveData(win, this.GROUPS_DATA_IDENTIFIER, data);
|
2010-05-21 15:44:15 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-08-06 15:46:55 -07:00
|
|
|
// Function: readGroupItemsData
|
|
|
|
// Reads the global data for the <GroupItems> singleton for the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
readGroupItemsData: function Storage_readGroupItemsData(win) {
|
2010-05-24 14:49:03 -07:00
|
|
|
return this.readData(win, this.GROUPS_DATA_IDENTIFIER);
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: saveUIData
|
2010-07-23 00:03:40 -07:00
|
|
|
// Saves the global data for the <UIManager> singleton for the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
saveUIData: function Storage_saveUIData(win, data) {
|
2010-05-24 14:49:03 -07:00
|
|
|
this.saveData(win, this.UI_DATA_IDENTIFIER, data);
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
2010-05-24 14:49:03 -07:00
|
|
|
|
2010-04-22 17:19:42 -07:00
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: readUIData
|
2010-07-23 00:03:40 -07:00
|
|
|
// Reads the global data for the <UIManager> singleton for the given window.
|
2010-09-08 10:02:08 -07:00
|
|
|
readUIData: function Storage_readUIData(win) {
|
2010-05-24 14:49:03 -07:00
|
|
|
return this.readData(win, this.UI_DATA_IDENTIFIER);
|
2010-04-22 17:19:42 -07:00
|
|
|
},
|
2010-07-12 21:04:04 -07:00
|
|
|
|
2011-01-17 18:44:50 -08:00
|
|
|
// ----------
|
|
|
|
// Function: saveVisibilityData
|
|
|
|
// Saves visibility for the given window.
|
|
|
|
saveVisibilityData: function Storage_saveVisibilityData(win, data) {
|
|
|
|
this._sessionStore.setWindowValue(
|
|
|
|
win, win.TabView.VISIBILITY_IDENTIFIER, data);
|
|
|
|
},
|
|
|
|
|
2010-05-24 14:49:03 -07:00
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: saveData
|
2010-07-18 08:58:10 -07:00
|
|
|
// Generic routine for saving data to a window.
|
2010-09-08 10:02:08 -07:00
|
|
|
saveData: function Storage_saveData(win, id, data) {
|
2010-05-24 14:49:03 -07:00
|
|
|
try {
|
|
|
|
this._sessionStore.setWindowValue(win, id, JSON.stringify(data));
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error in saveData: "+e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// ----------
|
2010-07-15 17:23:39 -07:00
|
|
|
// Function: readData
|
2010-07-18 08:58:10 -07:00
|
|
|
// Generic routine for reading data from a window.
|
2010-09-08 10:02:08 -07:00
|
|
|
readData: function Storage_readData(win, id) {
|
2010-05-24 14:49:03 -07:00
|
|
|
var existingData = {};
|
|
|
|
try {
|
|
|
|
var data = this._sessionStore.getWindowValue(win, id);
|
2010-07-11 17:54:42 -07:00
|
|
|
if (data)
|
2010-05-24 14:49:03 -07:00
|
|
|
existingData = JSON.parse(data);
|
|
|
|
} catch (e) {
|
|
|
|
Utils.log("Error in readData: "+e);
|
|
|
|
}
|
2010-07-18 08:58:10 -07:00
|
|
|
|
2010-05-24 14:49:03 -07:00
|
|
|
return existingData;
|
2010-04-22 14:26:57 -07:00
|
|
|
}
|
2010-07-22 12:35:11 -07:00
|
|
|
};
|
2011-02-19 10:22:49 -08:00
|
|
|
|