Mercurial > projects > dwt-linux
view dwt/browser.old/PromptService2.d~ @ 288:4ee8c4237614
old branches... commit by mistake
author | John Reimer<terminal.node@gmail.com> |
---|---|
date | Tue, 05 Aug 2008 18:00:50 -0700 |
parents | |
children |
line wrap: on
line source
/******************************************************************************* * Copyright (c) 2003, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ module dwt.browser.PromptService2; import Utf = tango.text.convert.Utf; import dwt.dwthelper.utils; import dwt.DWT; import dwt.internal.Compatibility; import dwt.internal.mozilla.nsEmbedString; import dwt.internal.mozilla.nsIAuthInformation; import dwt.internal.mozilla.nsIChannel; import dwt.internal.mozilla.nsID; import dwt.internal.mozilla.nsIDOMWindow; import dwt.internal.mozilla.nsIEmbeddingSiteWindow; import dwt.internal.mozilla.nsIMemory; import dwt.internal.mozilla.nsIPromptService; import dwt.internal.mozilla.nsIPromptService2; import dwt.internal.mozilla.nsIServiceManager; import dwt.internal.mozilla.nsISupports; import dwt.internal.mozilla.nsIURI; import dwt.internal.mozilla.nsIWebBrowserChrome; import dwt.internal.mozilla.nsIWindowWatcher; import nsStringAPI = dwt.internal.mozilla.nsStringAPI; import dwt.widgets.MessageBox; import dwt.widgets.Shell; class PromptService2 { private nsrefcnt _refCount = 0; /************************************************************************** **************************************************************************/ this () { } /************************************************************************** **************************************************************************/ nsrefcnt AddRef () { _refCount++; return _refCount; } /************************************************************************** **************************************************************************/ nsresult QueryInterface ( ref nsIID riid, void** ppvObject ) { if (riid is null || ppvObject is null) return NS_ERROR_NO_INTERFACE; if (riid == nsISupports.IID)) { *ppvObject = cast(void*)cast(nsISupports)this; AddRef (); return NS_OK; } if ( riid == nsIPromptService.IID ) { *ppvObject = cast(void*)cast(nsIPromptService)this; AddRef (); return NS_OK; } if ( riid == nsIPromptService2.IID) { *ppvObject = cast(void*)cast(nsIPromptService2)this; AddRef (); return NS_OK; } *ppvObject = null; return NS_ERROR_NO_INTERFACE; } /************************************************************************** **************************************************************************/ nsrefcnt Release () { _refCount--; if (_refCount is 0) return 0; return refCount; } /************************************************************************** **************************************************************************/ Browser getBrowser (nsIDOMWindow aDOMWindow) { if (aDOMWindow is null) return null; nsIServiceManager serviceManager; void* result; nsresult rc = XPCOM.NS_GetServiceManager (&serviceManager); if (rc !is NS_OK) Mozilla.error (rc); if (serviceManager is null) Mozilla.error (NS_NOINTERFACE); nsIWindowWatcher windowWatcher; rc = serviceManager.GetServiceByContractID (aContractID, nsIWindowWatcher.IID, &windowWatcher); if (rc !is NS_OK) Mozilla.error(rc); if (windowWatcher is null) Mozilla.error (NS_NOINTERFACE); serviceManager.Release (); /* the chrome will only be answered for the top-level nsIDOMWindow */ auto window = aDOMWindow; rc = window.GetTop (&result); if (rc !is NS_OK) Mozilla.error (rc); if (result is null) Mozilla.error (NS_NOINTERFACE); aDOMWindow = result; rc = windowWatcher.GetChromeForWindow (aDOMWindow, &result); if (rc !is NS_OK) Mozilla.error (rc); if (result is null) Mozilla.error (NS_NOINTERFACE); windowWatcher.Release (); // This assignment should work if "result" tested valid in prior tests auto webBrowserChrome = cast(nsIWebBrowserChrome) result; rc = webBrowserChrome.QueryInterface (nsIEmbeddingSiteWindow.IID, &result); if (rc !is NS_OK) Mozilla.error (rc); if (result is null) Mozilla.error (NS_NOINTERFACE); webBrowserChrome.Release (); auto embeddingSiteWindow = cast(nsIEmbeddingSiteWindow) result; rc = embeddingSiteWindow.GetSiteWindow (&result); if (rc !is NS_OK) Mozilla.error (rc); if (result is null) Mozilla.error (NS_NOINTERFACE); embeddingSiteWindow.Release (); return Mozilla.findBrowser (result); } /************************************************************************** **************************************************************************/ String getLabel (int buttonFlag, int index, PRUnichar* buttonTitle) { String label = null; int flag = (buttonFlag & (0xff * index)) / index; switch (flag) { case nsIPromptService.BUTTON_TITLE_CANCEL : label = DWT.getMessage ("SWT_Cancel"); break; case nsIPromptService.BUTTON_TITLE_NO : label = DWT.getMessage ("SWT_No"); break; case nsIPromptService.BUTTON_TITLE_OK : label = DWT.getMessage ("SWT_OK"); break; case nsIPromptService.BUTTON_TITLE_SAVE : label = DWT.getMessage ("SWT_Save"); break; case nsIPromptService.BUTTON_TITLE_YES : label = DWT.getMessage ("SWT_Yes"); break; case nsIPromptService.BUTTON_TITLE_IS_STRING : { int span = nsStringAPI.strlen_PRUnichar (buttonTitle); label = Utf.toString( buttonTitle[0..span] ); } } return label; } /************************************************************************** nsIPromptService **************************************************************************/ nsresult Alert (nsIDOMWindow aParent, PRUnichar* aDialogTitle, PRUnichar* aText) { Browser browser = getBrowser (aParent); int span = nsStringAPI.strlen_PRUnichar( aDialogTitle ); String titleLabel = Utf.toString (aDialogTitle[0..span]); span = nsStringAPI.strlen_PRUnichar( aText ); String textLabel = Utf.toString( aText[0..span] ); Shell shell = browser is null ? new Shell () : browser.getShell (); auto messageBox = new MessageBox (shell, DWT.OK | DWT.ICON_WARNING); messageBox.setText (titleLabel); messageBox.setMessage (textLabel); messageBox.open (); return NS_OK; } /************************************************************************** **************************************************************************/ nsresult AlertCheck ( nsIDOMWindow aParent, PRUnichar* aDialogTitle, PRUnichar* aText, PRUnichar* aCheckMsg, PRBool* aCheckState ) { auto browser = getBrowser (aParent); int span = nsStringAPI.strlen_PRUnichar( aDialogTitle ); String titleLabel = Utf.toString( aDialogTitle[0..span] ); span = nsStringAPI.strlen_PRUnichar( aText ); String textLabel = Utf.toString( aText[0..span] ); span = nsStringAPI.strlen_PRUnichar( aCheckMsg ); String checkLabel = Utf.toString( aCheckMsg[0..span] ); auto shell = browser is null ? new Shell () : browser.getShell (); PromptDialog dialog = new PromptDialog( shell ); PRBool check = *aCheckState; dialog.alertCheck( titleLabel, textLabel, checkLabel, check ); return NS_OK; } /************************************************************************** **************************************************************************/ int AsyncPromptAuth(int /*long*/ aParent, int /*long*/ aChannel, int /*long*/ aCallback, int /*long*/ aContext, int level, int /*long*/ authInfo, int /*long*/ checkboxLabel, int /*long*/ checkValue, int /*long*/ _retval) { return NS_ERROR_NOT_IMPLEMENTED; } /************************************************************************** **************************************************************************/ int Confirm (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ _retval) { Browser browser = getBrowser (aParent); int length = XPCOM.strlen_PRUnichar (aDialogTitle); char[] dest = new char[length]; XPCOM.memmove (dest, aDialogTitle, length * 2); String titleLabel = new String (dest); length = XPCOM.strlen_PRUnichar (aText); dest = new char[length]; XPCOM.memmove (dest, aText, length * 2); String textLabel = new String (dest); Shell shell = browser is null ? new Shell () : browser.getShell (); MessageBox messageBox = new MessageBox (shell, DWT.OK | DWT.CANCEL | DWT.ICON_QUESTION); messageBox.setText (titleLabel); messageBox.setMessage (textLabel); int id = messageBox.open (); int[] result = {id is DWT.OK ? 1 : 0}; XPCOM.memmove (_retval, result, 4); return XPCOM.NS_OK; } /************************************************************************** **************************************************************************/ int ConfirmCheck (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) { return XPCOM.NS_ERROR_NOT_IMPLEMENTED; } /************************************************************************** **************************************************************************/ int ConfirmEx (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int aButtonFlags, int /*long*/ aButton0Title, int /*long*/ aButton1Title, int /*long*/ aButton2Title, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) { Browser browser = getBrowser (aParent); int length = XPCOM.strlen_PRUnichar (aDialogTitle); char[] dest = new char[length]; XPCOM.memmove (dest, aDialogTitle, length * 2); String titleLabel = new String (dest); length = XPCOM.strlen_PRUnichar (aText); dest = new char[length]; XPCOM.memmove (dest, aText, length * 2); String textLabel = new String (dest); String checkLabel = null; if (aCheckMsg !is 0) { length = XPCOM.strlen_PRUnichar (aCheckMsg); dest = new char[length]; XPCOM.memmove (dest, aCheckMsg, length * 2); checkLabel = new String (dest); } String button0Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_0, aButton0Title); String button1Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_1, aButton1Title); String button2Label = getLabel (aButtonFlags, nsIPromptService.BUTTON_POS_2, aButton2Title); int defaultIndex = 0; if ((aButtonFlags & nsIPromptService.BUTTON_POS_1_DEFAULT) !is 0) { defaultIndex = 1; } else if ((aButtonFlags & nsIPromptService.BUTTON_POS_2_DEFAULT) !is 0) { defaultIndex = 2; } Shell shell = browser is null ? new Shell () : browser.getShell (); PromptDialog dialog = new PromptDialog (shell); int[] check = new int[1], result = new int[1]; if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4); dialog.confirmEx (titleLabel, textLabel, checkLabel, button0Label, button1Label, button2Label, defaultIndex, check, result); if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4); XPCOM.memmove (_retval, result, 4); return XPCOM.NS_OK; } /************************************************************************** **************************************************************************/ int Prompt (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aValue, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) { Browser browser = getBrowser (aParent); String titleLabel = null, textLabel, checkLabel = null; String[] valueLabel = new String[1]; char[] dest; int length; if (aDialogTitle !is 0) { length = XPCOM.strlen_PRUnichar (aDialogTitle); dest = new char[length]; XPCOM.memmove (dest, aDialogTitle, length * 2); titleLabel = new String (dest); } length = XPCOM.strlen_PRUnichar (aText); dest = new char[length]; XPCOM.memmove (dest, aText, length * 2); textLabel = new String (dest); int /*long*/[] valueAddr = new int /*long*/[1]; XPCOM.memmove (valueAddr, aValue, C.PTR_SIZEOF); if (valueAddr[0] !is 0) { length = XPCOM.strlen_PRUnichar (valueAddr[0]); dest = new char[length]; XPCOM.memmove (dest, valueAddr[0], length * 2); valueLabel[0] = new String (dest); } if (aCheckMsg !is 0) { length = XPCOM.strlen_PRUnichar (aCheckMsg); if (length > 0) { dest = new char[length]; XPCOM.memmove (dest, aCheckMsg, length * 2); checkLabel = new String (dest); } } Shell shell = browser is null ? new Shell () : browser.getShell (); PromptDialog dialog = new PromptDialog (shell); int[] check = new int[1], result = new int[1]; if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4); dialog.prompt (titleLabel, textLabel, checkLabel, valueLabel, check, result); XPCOM.memmove (_retval, result, 4); if (result[0] is 1) { /* * User selected OK. User name and password are returned as PRUnichar values. Any default * value that we override must be freed using the nsIMemory service. */ int cnt, size; int /*long*/ ptr; char[] buffer; int /*long*/[] result2 = new int /*long*/[1]; if (valueLabel[0] !is null) { cnt = valueLabel[0].length (); buffer = new char[cnt + 1]; valueLabel[0].getChars (0, cnt, buffer, 0); size = buffer.length * 2; ptr = C.malloc (size); XPCOM.memmove (ptr, buffer, size); XPCOM.memmove (aValue, new int /*long*/[] {ptr}, C.PTR_SIZEOF); if (valueAddr[0] !is 0) { int rc = XPCOM.NS_GetServiceManager (result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); result2[0] = 0; byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); serviceManager.Release (); nsIMemory memory = new nsIMemory (result2[0]); result2[0] = 0; memory.Free (valueAddr[0]); memory.Release (); } } } if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4); return XPCOM.NS_OK; } /************************************************************************** **************************************************************************/ int PromptAuth(int /*long*/ aParent, int /*long*/ aChannel, int level, int /*long*/ authInfo, int /*long*/ checkboxLabel, int /*long*/ checkboxValue, int /*long*/ _retval) { Browser browser = getBrowser (aParent); String checkLabel = null; int[] checkValue = new int[1]; String[] userLabel = new String[1], passLabel = new String[1]; String title = DWT.getMessage ("SWT_Authentication_Required"); //$NON-NLS-1$ if (checkboxLabel !is 0 && checkboxValue !is 0) { int length = XPCOM.strlen_PRUnichar (checkboxLabel); char[] dest = new char[length]; XPCOM.memmove (dest, checkboxLabel, length * 2); checkLabel = new String (dest); XPCOM.memmove (checkValue, checkboxValue, 4); /* PRBool */ } /* get initial username and password values */ nsIAuthInformation auth = new nsIAuthInformation (authInfo); int /*long*/ ptr = XPCOM.nsEmbedString_new (); int rc = auth.GetUsername (ptr); if (rc !is XPCOM.NS_OK) DWT.error (rc); int length = XPCOM.nsEmbedString_Length (ptr); int /*long*/ buffer = XPCOM.nsEmbedString_get (ptr); char[] chars = new char[length]; XPCOM.memmove (chars, buffer, length * 2); userLabel[0] = new String (chars); XPCOM.nsEmbedString_delete (ptr); ptr = XPCOM.nsEmbedString_new (); rc = auth.GetPassword (ptr); if (rc !is XPCOM.NS_OK) DWT.error (rc); length = XPCOM.nsEmbedString_Length (ptr); buffer = XPCOM.nsEmbedString_get (ptr); chars = new char[length]; XPCOM.memmove (chars, buffer, length * 2); passLabel[0] = new String (chars); XPCOM.nsEmbedString_delete (ptr); /* compute the message text */ ptr = XPCOM.nsEmbedString_new (); rc = auth.GetRealm (ptr); if (rc !is XPCOM.NS_OK) DWT.error (rc); length = XPCOM.nsEmbedString_Length (ptr); buffer = XPCOM.nsEmbedString_get (ptr); chars = new char[length]; XPCOM.memmove (chars, buffer, length * 2); String realm = new String (chars); XPCOM.nsEmbedString_delete (ptr); nsIChannel channel = new nsIChannel (aChannel); int /*long*/[] uri = new int /*long*/[1]; rc = channel.GetURI (uri); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (uri[0] is 0) Mozilla.error (XPCOM.NS_NOINTERFACE); nsIURI nsURI = new nsIURI (uri[0]); int /*long*/ aSpec = XPCOM.nsEmbedCString_new (); rc = nsURI.GetHost (aSpec); if (rc !is XPCOM.NS_OK) DWT.error (rc); length = XPCOM.nsEmbedCString_Length (aSpec); buffer = XPCOM.nsEmbedCString_get (aSpec); byte[] bytes = new byte[length]; XPCOM.memmove (bytes, buffer, length); XPCOM.nsEmbedCString_delete (aSpec); String host = new String (bytes); nsURI.Release (); String message; if (realm.length () > 0 && host.length () > 0) { message = Compatibility.getMessage ("SWT_Enter_Username_and_Password", new String[] {realm, host}); //$NON-NLS-1$ } else { message = ""; //$NON-NLS-1$ } /* open the prompter */ Shell shell = browser is null ? new Shell () : browser.getShell (); PromptDialog dialog = new PromptDialog (shell); int[] result = new int[1]; dialog.promptUsernameAndPassword (title, message, checkLabel, userLabel, passLabel, checkValue, result); XPCOM.memmove (_retval, result, 4); /* PRBool */ if (result[0] is 1) { /* User selected OK */ nsEmbedString string = new nsEmbedString (userLabel[0]); rc = auth.SetUsername(string.getAddress ()); if (rc !is XPCOM.NS_OK) DWT.error (rc); string.dispose (); string = new nsEmbedString (passLabel[0]); rc = auth.SetPassword(string.getAddress ()); if (rc !is XPCOM.NS_OK) DWT.error (rc); string.dispose (); } if (checkboxValue !is 0) XPCOM.memmove (checkboxValue, checkValue, 4); /* PRBool */ return XPCOM.NS_OK; } /************************************************************************** **************************************************************************/ int PromptUsernameAndPassword (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aUsername, int /*long*/ aPassword, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) { Browser browser = getBrowser (aParent); String titleLabel, textLabel, checkLabel = null; String[] userLabel = new String[1], passLabel = new String[1]; char[] dest; int length; if (aDialogTitle !is 0) { length = XPCOM.strlen_PRUnichar (aDialogTitle); dest = new char[length]; XPCOM.memmove (dest, aDialogTitle, length * 2); titleLabel = new String (dest); } else { titleLabel = DWT.getMessage ("SWT_Authentication_Required"); //$NON-NLS-1$ } length = XPCOM.strlen_PRUnichar (aText); dest = new char[length]; XPCOM.memmove (dest, aText, length * 2); textLabel = new String (dest); int /*long*/[] userAddr = new int /*long*/[1]; XPCOM.memmove (userAddr, aUsername, C.PTR_SIZEOF); if (userAddr[0] !is 0) { length = XPCOM.strlen_PRUnichar (userAddr[0]); dest = new char[length]; XPCOM.memmove (dest, userAddr[0], length * 2); userLabel[0] = new String (dest); } int /*long*/[] passAddr = new int /*long*/[1]; XPCOM.memmove (passAddr, aPassword, C.PTR_SIZEOF); if (passAddr[0] !is 0) { length = XPCOM.strlen_PRUnichar (passAddr[0]); dest = new char[length]; XPCOM.memmove (dest, passAddr[0], length * 2); passLabel[0] = new String (dest); } if (aCheckMsg !is 0) { length = XPCOM.strlen_PRUnichar (aCheckMsg); if (length > 0) { dest = new char[length]; XPCOM.memmove (dest, aCheckMsg, length * 2); checkLabel = new String (dest); } } Shell shell = browser is null ? new Shell () : browser.getShell (); PromptDialog dialog = new PromptDialog (shell); int[] check = new int[1], result = new int[1]; if (aCheckState !is 0) XPCOM.memmove (check, aCheckState, 4); /* PRBool */ dialog.promptUsernameAndPassword (titleLabel, textLabel, checkLabel, userLabel, passLabel, check, result); XPCOM.memmove (_retval, result, 4); /* PRBool */ if (result[0] is 1) { /* * User selected OK. User name and password are returned as PRUnichar values. Any default * value that we override must be freed using the nsIMemory service. */ int cnt, size; int /*long*/ ptr; char[] buffer; int /*long*/[] result2 = new int /*long*/[1]; if (userLabel[0] !is null) { cnt = userLabel[0].length (); buffer = new char[cnt + 1]; userLabel[0].getChars (0, cnt, buffer, 0); size = buffer.length * 2; ptr = C.malloc (size); XPCOM.memmove (ptr, buffer, size); XPCOM.memmove (aUsername, new int /*long*/[] {ptr}, C.PTR_SIZEOF); if (userAddr[0] !is 0) { int rc = XPCOM.NS_GetServiceManager (result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); result2[0] = 0; byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); serviceManager.Release (); nsIMemory memory = new nsIMemory (result2[0]); result2[0] = 0; memory.Free (userAddr[0]); memory.Release (); } } if (passLabel[0] !is null) { cnt = passLabel[0].length (); buffer = new char[cnt + 1]; passLabel[0].getChars (0, cnt, buffer, 0); size = buffer.length * 2; ptr = C.malloc (size); XPCOM.memmove (ptr, buffer, size); XPCOM.memmove (aPassword, new int /*long*/[] {ptr}, C.PTR_SIZEOF); if (passAddr[0] !is 0) { int rc = XPCOM.NS_GetServiceManager (result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); nsIServiceManager serviceManager = new nsIServiceManager (result2[0]); result2[0] = 0; byte[] aContractID = MozillaDelegate.wcsToMbcs (null, XPCOM.NS_MEMORY_CONTRACTID, true); rc = serviceManager.GetServiceByContractID (aContractID, nsIMemory.NS_IMEMORY_IID, result2); if (rc !is XPCOM.NS_OK) DWT.error (rc); if (result2[0] is 0) DWT.error (XPCOM.NS_NOINTERFACE); serviceManager.Release (); nsIMemory memory = new nsIMemory (result2[0]); result2[0] = 0; memory.Free (passAddr[0]); memory.Release (); } } } if (aCheckState !is 0) XPCOM.memmove (aCheckState, check, 4); /* PRBool */ return XPCOM.NS_OK; } /************************************************************************** **************************************************************************/ int PromptPassword (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int /*long*/ aPassword, int /*long*/ aCheckMsg, int /*long*/ aCheckState, int /*long*/ _retval) { return NS_ERROR_NOT_IMPLEMENTED; } /************************************************************************** **************************************************************************/ int Select (int /*long*/ aParent, int /*long*/ aDialogTitle, int /*long*/ aText, int aCount, int /*long*/ aSelectList, int /*long*/ aOutSelection, int /*long*/ _retval) { return XPCOM.NS_ERROR_NOT_IMPLEMENTED; } }