0,0 → 1,509 |
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
// ===================================================================================================================== |
// FIXME |
// - don't show the dialog in the task bar |
// - if some other dialog is open, lower our zLevel |
// - options and about dialogs |
// - customize standard/modeless dialogs via prefs |
function NoModal_Prompt() |
{ |
this.windowWatcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] |
.getService(Components.interfaces.nsIWindowWatcher); |
this.windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
.getService(Components.interfaces.nsIWindowMediator); |
this.threadManager = Components.classes["@mozilla.org/thread-manager;1"] |
.getService(Components.interfaces.nsIThreadManager); |
|
var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"] |
.getService(Components.interfaces.nsIStringBundleService); |
this.commonBundle = stringBundleService.createBundle("chrome://global/locale/commonDialogs.properties"); |
this.promptBundle = stringBundleService.createBundle("chrome://global/locale/prompts.properties"); |
} |
NoModal_Prompt.prototype = { |
classDescription: "NoModal_Prompt", |
classID: Components.ID("{21fa8349-62d3-406b-b042-4016469d6fe0}"), |
contractID: "@mozilla.org/embedcomp/prompt-service;1", |
QueryInterface: XPCOMUtils.generateQI( [ |
Components.interfaces.nsIPromptService, |
Components.interfaces.nsIPromptService2, |
] ), |
|
// ----------------------------------------------------------------------------------------------------------------- |
isEnabled: function(aParent, dialogName) |
{ |
// replace dialogs for for js-windows only |
if(("" + aParent) != "[object XPCNativeWrapper [object Window]]") { // FIXME a better method to detect js-window? |
return false; |
} |
|
return true; |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
showDialog: function(aParent, params) |
{ |
// parent interfaces |
var parentRequestor = aParent.QueryInterface(Components.interfaces.nsIInterfaceRequestor); |
var parentDocShell = parentRequestor.getInterface(Components.interfaces.nsIWebNavigation) |
.QueryInterface(Components.interfaces.nsIDocShell); |
var parentInt = parentRequestor.getInterface(Components.interfaces.nsIDOMWindowInternal); |
var parentDom = parentRequestor.getInterface(Components.interfaces.nsIDOMWindow); |
var mainWin = this.windowMediator.getMostRecentWindow("navigator:browser"); |
var tabBrowser = mainWin.gBrowser; |
|
var parentTab; |
for(var i = 0, l = tabBrowser.browsers.length; i < l; ++i) { |
var b = tabBrowser.getBrowserAtIndex(i); |
if(b.contentWindow == aParent) { |
parentTab = tabBrowser.tabContainer.childNodes[i]; |
} |
} |
|
// disable all dynamics in the window |
var jsAllowed = parentDocShell.allowJavascript; |
parentDocShell.allowJavascript = false; |
parentDocShell.suspendRefreshURIs(); |
|
// params |
if(!params.dialogTitle) { |
params.dialogTitle = this.commonBundle.GetStringFromName(params.dialogTitleKey); |
} |
|
var args = Components.classes["@mozilla.org/embedcomp/dialogparam;1"] |
.createInstance(Components.interfaces.nsIDialogParamBlock); |
args.SetString(0, params.text); |
args.SetString(1, params.checkboxMsg); |
args.SetString(2, params.iconClass); |
args.SetString(3, params.titleMessage); |
args.SetString(4, params.editfield1Msg); |
args.SetString(5, params.editfield2Msg); |
args.SetString(6, params.editfield1Value); |
args.SetString(7, params.editfield2Value); |
args.SetString(8, params.button0Text); |
args.SetString(9, params.button1Text); |
args.SetString(10, params.button2Text); |
args.SetString(11, params.button3Text); |
args.SetString(12, params.dialogTitle); |
args.SetString(13, params.openingSound); |
args.SetInt (1, params.checkboxState); |
args.SetInt (2, params.numberButtons); |
args.SetInt (3, params.numberEditfields); |
args.SetInt (4, params.editField1Password); |
args.SetInt (5, params.defaultButton); |
args.SetInt (6, params.delayButtonEnable); |
args.SetInt (7, params.soundEventId); |
|
// show the dialog |
var win = this.windowWatcher.openWindow(aParent, "chrome://global/content/commonDialog.xul", |
"_blank", "centerscreen,chrome,titlebar" /*,dependent*/, args); |
win.openInProgress = true; |
|
var winRequestor = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor); |
var winDocShell = winRequestor.getInterface(Components.interfaces.nsIWebNavigation) |
.QueryInterface(Components.interfaces.nsIDocShell); |
var winInt = winRequestor.getInterface(Components.interfaces.nsIDOMWindowInternal); |
|
var winXul; |
var xulEnum = this.windowMediator.getXULWindowEnumerator(null); |
while(xulEnum.hasMoreElements()) { |
var xulWindow = xulEnum.getNext(); |
xulWindow.QueryInterface(Components.interfaces.nsIXULWindow); |
if(xulWindow.docShell == winDocShell) { |
winXul = xulWindow; |
} |
} |
winXul.zLevel = Components.interfaces.nsIXULWindow.raisedZ; |
|
// listeners |
var parentClick = function(aEvent) { |
if(win) { |
win.focus(); |
} |
}; |
var parentUnload = function(aEvent) { |
if(win) { |
win.close(); |
} |
}; |
var winFocus = function(aEvent) { |
if(aEvent.target == win) { |
if(win.openInProgress) { |
win.openInProgress = false; |
} |
else if(win.focusInProgress) { |
win.focusInProgress = false; |
} |
else { |
tabBrowser.selectedTab = parentTab; |
mainWin.focus(); |
win.focusInProgress = true; |
win.focus(); |
} |
} |
}; |
var parentTabSelect = function(aEvent) { |
if(tabBrowser.selectedTab == parentTab) { |
winXul.zLevel = Components.interfaces.nsIXULWindow.raisedZ; |
win.focusInProgress = true; |
win.focus(); |
} |
else { |
winXul.zLevel = Components.interfaces.nsIXULWindow.loweredZ; |
} |
}; |
|
aParent.addEventListener('click', parentClick, true); |
aParent.addEventListener('unload', parentUnload, true); |
win.addEventListener('focus', winFocus, false); |
tabBrowser.tabContainer.addEventListener('TabSelect', parentTabSelect, false); |
|
// wait until the dialog is closed |
while(!winInt.closed && this.threadManager.currentThread.processNextEvent(true)); |
|
if(!parentInt.closed) { |
// restore |
aParent.removeEventListener('click', parentClick, true); |
aParent.removeEventListener('unload', parentUnload, true); |
win.removeEventListener('focus', winFocus, false); |
tabBrowser.tabContainer.removeEventListener('TabSelect', parentTabSelect, false); |
|
parentDocShell.resumeRefreshURIs(); |
parentDocShell.allowJavascript = jsAllowed; |
|
// eval the answer |
params.buttonPressed = args.GetInt(0); |
params.checkboxState = args.GetInt(1); |
params.editfield1Value = args.GetString(6); |
params.editfield2Value = args.GetString(7); |
|
return true; |
} |
else { |
return false; |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
alert: function(aParent, aDialogTitle, aText) |
{ |
if(this.isEnabled(aParent, 'alert')) { |
var params = { |
dialogTitleKey: "Alert", |
dialogTitle: aDialogTitle, |
text: aText, |
numberButtons: 1, |
numberEditfields: 0, |
iconClass: "alert-icon", |
openingSound: "_moz_alertdialog", |
soundEventId: 1, |
url: "chrome://global/content/commonDialog.xul", |
}; |
|
this.showDialog(aParent, params); |
} |
else { |
NoModal_Prompt.origin.alert(aParent, aDialogTitle, aText); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
alertCheck: function(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) |
{ |
return NoModal_Prompt.origin.alertCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState); |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
confirm: function(aParent, aDialogTitle, aText) |
{ |
if(this.isEnabled(aParent, 'confirm')) { |
var params = { |
dialogTitleKey: "Confirm", |
dialogTitle: aDialogTitle, |
text: aText, |
numberButtons: 2, |
numberEditfields: 0, |
iconClass: "question-icon", |
openingSound: "_moz_confirmdialog", |
soundEventId: 2, |
url: "chrome://global/content/commonDialog.xul", |
}; |
|
this.showDialog(aParent, params); |
|
return (params.buttonPressed == 0); |
} |
else { |
return NoModal_Prompt.origin.confirm(aParent, aDialogTitle, aText); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
confirmCheck: function(aParent, aDialogTitle, aText, aCheckMsg, aCheckState) |
{ |
return NoModal_Prompt.origin.confirmCheck(aParent, aDialogTitle, aText, aCheckMsg, aCheckState); |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
confirmEx: function(aParent, aDialogTitle, aText, aButtonFlags, aButton0Title, aButton1Title, aButton2Title, |
aCheckMsg, aCheckState) |
{ |
return NoModal_Prompt.origin.confirmEx(aParent, aDialogTitle, aText, aButtonFlags, aButton0Title, |
aButton1Title, aButton2Title, aCheckMsg, aCheckState); |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
prompt: function(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState) |
{ |
if(this.isEnabled(aParent, 'confirm')) { |
var params = { |
dialogTitleKey: "Prompt", |
dialogTitle: aDialogTitle, |
text: aText, |
numberButtons: 2, |
numberEditfields: 1, |
iconClass: "question-icon", |
openingSound: "_moz_promptdialog", |
soundEventId: 3, |
url: "chrome://global/content/commonDialog.xul", |
editfield1Value: aValue.value, |
}; |
|
if(aCheckMsg && aCheckState) { |
params.checkboxMsg = aCheckMsg; |
params.checkboxState = (aCheckState.value ? 1 : 0); |
} |
|
this.showDialog(aParent, params); |
|
var res = (params.buttonPressed == 0); |
if(res) { |
aValue.value = params.editfield1Value; |
|
if(aCheckState) { |
aCheckState.value = (params.checkboxState == 1); |
} |
} |
|
return res; |
} |
else { |
return NoModal_Prompt.origin.prompt(aParent, aDialogTitle, aText, aValue, aCheckMsg, aCheckState); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
promptUsernameAndPassword: function(aParent, aDialogTitle, aText, aUsername, aPassword, aCheckMsg, aCheckState) |
{ |
if(this.isEnabled(aParent, 'promptUsernameAndPassword')) { |
var params = { |
dialogTitleKey: "PromptUsernameAndPassword2", |
dialogTitle: aDialogTitle, |
text: aText, |
numberButtons: 2, |
numberEditfields: 2, |
iconClass: "authentication-icon question-icon", |
openingSound: "_moz_promptdialog", |
soundEventId: 3, |
url: "chrome://global/content/commonDialog.xul", |
editfield1Value: aUsername.value, |
editfield2Value: aPassword.value, |
}; |
|
if(aCheckMsg && aCheckState) { |
params.checkboxMsg = aCheckMsg; |
params.checkboxState = (aCheckState.value ? 1 : 0); |
} |
|
this.showDialog(aParent, params); |
|
var res = (params.buttonPressed == 0); |
if(res) { |
aUsername.value = params.editfield1Value; |
aPassword.value = params.editfield2Value; |
|
if(aCheckState) { |
aCheckState.value = (params.checkboxState == 1); |
} |
} |
|
return res; |
} |
else { |
return NoModal_Prompt.origin.promptUsernameAndPassword(aParent, aDialogTitle, aText, aUsername, aPassword, |
aCheckMsg, aCheckState); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
promptPassword: function(aParent, aDialogTitle, aText, aPassword, aCheckMsg, aCheckState) |
{ |
if(this.isEnabled(aParent, 'promptPassword')) { |
var params = { |
dialogTitleKey: "PromptPassword2", |
dialogTitle: aDialogTitle, |
text: aText, |
numberButtons: 2, |
numberEditfields: 1, |
editField1Password: 1, |
iconClass: "authentication-icon question-icon", |
openingSound: "_moz_promptdialog", |
soundEventId: 3, |
url: "chrome://global/content/commonDialog.xul", |
editfield1Value: aPassword.value, |
}; |
|
if(aCheckMsg && aCheckState) { |
params.checkboxMsg = aCheckMsg; |
params.checkboxState = (aCheckState.value ? 1 : 0); |
} |
|
this.showDialog(aParent, params); |
|
var res = (params.buttonPressed == 0); |
if(res) { |
aPassword.value = params.editfield1Value; |
|
if(aCheckState) { |
aCheckState.value = (params.checkboxState == 1); |
} |
} |
|
return res; |
} |
else { |
return NoModal_Prompt.origin.promptPassword(aParent, aDialogTitle, aText, aPassword, aCheckMsg, aCheckState); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService |
select: function(aParent, aDialogTitle, aText, aCount, aSelectList, aOutSelection) |
{ |
return NoModal_Prompt.origin.select(aParent, aDialogTitle, aText, aCount, aSelectList, aOutSelection); |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService2 |
promptAuth: function(aParent, aChannel, level, authInfo, checkboxLabel, checkValue) |
{ |
if(this.isEnabled(aParent, 'promptAuth')) { |
return this.promptPasswordAdapter(aParent, aChannel, level, authInfo, checkboxLabel, checkValue); |
} |
else { |
return NoModal_Prompt.origin.promptAuth(aParent, aChannel, level, authInfo, checkboxLabel, checkValue); |
} |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// interface nsIPromptService2 |
asyncPromptAuth: function(aParent, aChannel, aCallback, aContext, level, authInfo, checkboxLabel, checkValue) |
{ |
return NoModal_Prompt.origin.asyncPromptAuth(aParent, aChannel, level, authInfo, checkboxLabel, checkValue); |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
// reimplement nsPrompt.cpp because it has no js-interface |
promptPasswordAdapter: function(aParent, aChannel, level, authInfo, checkboxLabel, checkValue) |
{ |
var msg = this.makeAuthDialogText(aChannel, authInfo); |
|
var user = { value: authInfo.username }; |
var pass = { value: authInfo.password }; |
|
if((authInfo.flags & Components.interfaces.nsIAuthInformation.NEED_DOMAIN) && authInfo.domain) { |
user.value = authInfo.domain + "\\" + user.value; |
} |
|
var res; |
if(authInfo.flags & Components.interfaces.nsIAuthInformation.ONLY_PASSWORD) { |
res = this.promptPassword(aParent, null, msg, pass, checkboxLabel, checkValue); |
} |
else { |
res = this.promptUsernameAndPassword(aParent, null, msg, user, pass, checkboxLabel, checkValue); |
} |
|
authInfo.username = user.value; |
authInfo.password = pass.value; |
|
return res; |
}, |
|
// ----------------------------------------------------------------------------------------------------------------- |
makeAuthDialogText: function(aChannel, authInfo) |
{ |
var host; |
var port; |
if((authInfo.flags & Components.interfaces.nsIAuthInformation.AUTH_PROXY) != 0) { |
aChannel.QueryInterface(Components.interfaces.nsIProxiedChannel); |
host = aChannel.proxyInfo.host; |
port = aChannel.proxyInfo.port; |
} |
else { |
host = aChannel.URI.host; |
port = aChannel.URI.port; |
} |
|
if(port != -1) { |
host += ':' + port; |
} |
|
var proxyAuth = ((authInfo.flags & Components.interfaces.nsIAuthInformation.AUTH_PROXY) != 0); |
|
var realm = authInfo.realm; |
if(realm.length > 150) { |
realm = realm.substring(0, 150); |
|
var ellipsis; |
var prefService = Components.classes["@mozilla.org/preferences-service;1"] |
.getService(Components.interfaces.nsIPrefService); |
var prefBranch = prefService.getBranch("intl."); |
var prefValue = prefBranch.getComplexValue("ellipsis", Components.interfaces.nsIPrefLocalizedString); |
|
ellipsis = prefValue.toString(); |
|
if(!ellipsis) ellipsis = "..."; |
realm += ellipsis; |
} |
|
var msg; |
if(proxyAuth) { |
msg = "EnterLoginForProxy"; |
} |
else { |
msg = "EnterLoginForRealm"; |
host = aChannel.URI.scheme + "://" + host; |
} |
|
var args = [ realm, host ]; |
|
if((authInfo.flags & Components.interfaces.nsIAuthInformation.ONLY_PASSWORD) != 0) { |
msg = "EnterPasswordFor"; |
args[0] = authInfo.username; |
} |
else if(!proxyAuth && !realm) { |
msg = "EnterUserPasswordFor"; |
args = [ host ]; |
} |
|
return this.promptBundle.formatStringFromName(msg, args, args.length); |
}, |
}; |
|
// ===================================================================================================================== |
function NSGetModule(compMgr, fileSpec) |
{ |
NoModal_Prompt.origin = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] |
.getService(Components.interfaces.nsIPromptService); |
NoModal_Prompt.origin.QueryInterface(Components.interfaces.nsIPromptService2); |
|
return XPCOMUtils.generateModule([NoModal_Prompt]); |
} |