1 if(!dojo._hasResource["openils.XUL"]) {
3 dojo.provide("openils.XUL");
4 dojo.require('dojo.cookie');
5 dojo.declare('openils.XUL', null, {});
7 openils.XUL.isXUL = function() {
8 return Boolean(dojo.cookie('xul')) || Boolean(window.IAMXUL);
11 openils.XUL.buildId = function() {
12 return window.XUL_BUILD_ID || '';
15 openils.XUL.getStash = function() {
16 if(openils.XUL.isXUL()) {
18 if(openils.XUL.enableXPConnect()) {
19 var CacheClass = new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
20 return new CacheClass().wrappedJSObject.OpenILS.prototype.data;
23 console.log("Error loading XUL stash: " + e);
30 openils.XUL.newTab = function(path, tabInfo, options) {
32 throw new Error('xulG is not defined. Cannot open tab');
33 xulG.new_tab(path, tabInfo, options);
37 * @return bool True if a new session was successfully created, false otherwise.
39 openils.XUL.getNewSession = function(callback) {
40 return xulG.get_new_session({callback : callback});
44 * This can be used by privileged Firefox in addition to XUL.
45 * To use use in Firefox directly, set signed.applets.codebase_principal_support to true in about:config
47 openils.XUL.enableXPConnect = function() {
49 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
52 console.error("Unable to enable UniversalXPConnect privileges. " +
53 "Try setting 'signed.applets.codebase_principal_support' to true in about:config");
60 /* This class cuts down on the obscenely long incantations needed to
61 * use XPCOM components. */
62 openils.XUL.SimpleXPCOM = function() {};
63 openils.XUL.SimpleXPCOM.prototype = {
65 "iface": Components.interfaces.nsIFilePicker,
66 "cls": "@mozilla.org/filepicker;1"
69 "iface": Components.interfaces.nsIFileInputStream,
70 "cls": "@mozilla.org/network/file-input-stream;1"
73 "iface": Components.interfaces.nsIScriptableInputStream,
74 "cls": "@mozilla.org/scriptableinputstream;1"
77 "iface": Components.interfaces.nsIFileOutputStream,
78 "cls": "@mozilla.org/network/file-output-stream;1"
80 "create": function(key) {
81 return Components.classes[this[key].cls].
82 createInstance(this[key].iface);
84 "getPrivilegeManager": function() {
85 return netscape.security.PrivilegeManager;
89 openils.XUL.contentFromFileOpenDialog = function(windowTitle, sizeLimit) {
90 var api = new openils.XUL.SimpleXPCOM();
92 /* The following enablePrivilege() call must happen at this exact
93 * level of scope -- not wrapped in another function -- otherwise
95 api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
97 var picker = api.create("FP");
99 window, windowTitle || "Upload File", api.FP.iface.modeOpen
101 if (picker.show() == api.FP.iface.returnOK && picker.file) {
102 var fis = api.create("FIS");
103 var sis = api.create("SIS");
105 fis.init(picker.file, 1 /* MODE_RDONLY */, 0, 0);
108 return sis.read(sizeLimit || -1);
114 openils.XUL.contentToFileSaveDialog = function(content, windowTitle) {
115 var api = new openils.XUL.SimpleXPCOM();
116 api.getPrivilegeManager().enablePrivilege("UniversalXPConnect");
118 var picker = api.create("FP");
120 window, windowTitle || "Save File", api.FP.iface.modeSave
122 var result = picker.show();
124 (result == api.FP.iface.returnOK ||
125 result == api.FP.iface.returnReplace)) {
126 if (!picker.file.exists())
127 picker.file.create(0, 0644); /* XXX hardcoded = bad */
128 var fos = api.create("FOS");
129 fos.init(picker.file, 42 /* WRONLY | CREAT | TRUNCATE */, 0644, 0);
130 return fos.write(content, content.length);