|
|
* | * |
* ***** END LICENSE BLOCK ***** */ | * ***** END LICENSE BLOCK ***** */ |
| |
|
#include "config.h" |
#include "GtkPromptService.h" | #include "GtkPromptService.h" |
#include <nsIWindowWatcher.h> | #include <nsIWindowWatcher.h> |
#include <nsIWebBrowserChrome.h> | #include <nsIWebBrowserChrome.h> |
|
|
#include <nsIDOMCharacterData.h> | #include <nsIDOMCharacterData.h> |
#include <nsIDOMText.h> | #include <nsIDOMText.h> |
#include <nsMemory.h> | #include <nsMemory.h> |
|
#ifdef HAVE_GECKO_1_9 |
|
#include <nsIAuthInformation.h> |
|
#include <nsIStringBundle.h> |
|
#include <nsServiceManagerUtils.h> |
|
/* That is ugly but I didn't find another way, except including |
|
* NS_GetAuthHostPort */ |
|
#define kNotFound -1 |
|
NS_COM void AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest ); |
|
#include <nsStringAPI.h> |
|
#include <nsIProxiedChannel.h> |
|
#include <nsIProxyInfo.h> |
|
#include <nsIIDNService.h> |
|
#include <nsNetCID.h> |
|
#include <nsIURI.h> |
|
#include <nsNetUtil.h> |
|
#include <nsPromptUtils.h> |
|
#endif |
| |
#include <glib/gi18n.h> | #include <glib/gi18n.h> |
#include "kz-prompt-dialog.h" | #include "kz-prompt-dialog.h" |
|
|
{ | { |
} | } |
| |
|
#ifdef HAVE_GECKO_1_9 |
|
NS_IMPL_ISUPPORTS2(GtkPromptService, nsIPromptService, nsIPromptService2) |
|
#else |
NS_IMPL_ISUPPORTS1(GtkPromptService, nsIPromptService) | NS_IMPL_ISUPPORTS1(GtkPromptService, nsIPromptService) |
|
#endif |
| |
NS_IMETHODIMP | NS_IMETHODIMP |
GtkPromptService::Alert(nsIDOMWindow* aParent, const PRUnichar* aDialogTitle, | GtkPromptService::Alert(nsIDOMWindow* aParent, const PRUnichar* aDialogTitle, |
|
|
KzPromptDialog *prompt = KZ_PROMPT_DIALOG(kz_prompt_dialog_new_with_parent | KzPromptDialog *prompt = KZ_PROMPT_DIALOG(kz_prompt_dialog_new_with_parent |
(TYPE_PROMPT_USER_PASS, | (TYPE_PROMPT_USER_PASS, |
GTK_WINDOW(GetGtkWindowForDOMWindow(aParent)))); | GTK_WINDOW(GetGtkWindowForDOMWindow(aParent)))); |
|
#ifndef HAVE_GECKO_1_9 |
gchar *host = GetURIForDOMWindow(aParent); | gchar *host = GetURIForDOMWindow(aParent); |
kz_prompt_dialog_set_host (prompt, host); | kz_prompt_dialog_set_host (prompt, host); |
if (host) | if (host) |
g_free(host); | g_free(host); |
|
#endif |
kz_prompt_dialog_set_title(prompt, | kz_prompt_dialog_set_title(prompt, |
aDialogTitle ? cTitle.get() : _("Prompt")); | aDialogTitle ? cTitle.get() : _("Prompt")); |
kz_prompt_dialog_set_message_text(prompt, cText.get()); | kz_prompt_dialog_set_message_text(prompt, cText.get()); |
|
|
KzPromptDialog *prompt = KZ_PROMPT_DIALOG(kz_prompt_dialog_new_with_parent | KzPromptDialog *prompt = KZ_PROMPT_DIALOG(kz_prompt_dialog_new_with_parent |
(TYPE_PROMPT_PASS, | (TYPE_PROMPT_PASS, |
GTK_WINDOW(GetGtkWindowForDOMWindow(aParent)))); | GTK_WINDOW(GetGtkWindowForDOMWindow(aParent)))); |
|
#ifndef HAVE_GECKO_1_9 |
gchar *host = GetURIForDOMWindow(aParent); | gchar *host = GetURIForDOMWindow(aParent); |
kz_prompt_dialog_set_host (prompt, host); | kz_prompt_dialog_set_host (prompt, host); |
if (host) | if (host) |
g_free(host); | g_free(host); |
|
#endif |
kz_prompt_dialog_set_title(prompt, | kz_prompt_dialog_set_title(prompt, |
aDialogTitle ? cTitle.get() : _("Prompt")); | aDialogTitle ? cTitle.get() : _("Prompt")); |
kz_prompt_dialog_set_message_text(prompt, cText.get()); | kz_prompt_dialog_set_message_text(prompt, cText.get()); |
|
|
NS_UTF16ToCString(uri, NS_CSTRING_ENCODING_UTF8, c_uri); | NS_UTF16ToCString(uri, NS_CSTRING_ENCODING_UTF8, c_uri); |
return g_strdup(c_uri.get()); | return g_strdup(c_uri.get()); |
} | } |
|
|
|
#ifdef HAVE_GECKO_1_9 |
|
static nsresult |
|
MakeDialogText(nsIChannel* aChannel, nsIAuthInformation* aAuthInfo, |
|
nsString& message) |
|
{ |
|
nsresult rv; |
|
nsCOMPtr<nsIStringBundleService> bundleSvc = |
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); |
|
NS_ENSURE_SUCCESS(rv, rv); |
|
|
|
nsCOMPtr<nsIStringBundle> bundle; |
|
rv = bundleSvc->CreateBundle("chrome://global/locale/prompts.properties", |
|
getter_AddRefs(bundle)); |
|
NS_ENSURE_SUCCESS(rv, rv); |
|
|
|
// figure out what message to display... |
|
nsCAutoString host; |
|
PRInt32 port; |
|
NS_GetAuthHostPort(aChannel, aAuthInfo, PR_FALSE, host, &port); |
|
|
|
nsAutoString displayHost; |
|
CopyUTF8toUTF16(host, displayHost); |
|
|
|
nsCOMPtr<nsIURI> uri; |
|
aChannel->GetURI(getter_AddRefs(uri)); |
|
|
|
nsCAutoString scheme; |
|
uri->GetScheme(scheme); |
|
|
|
nsAutoString username; |
|
aAuthInfo->GetUsername(username); |
|
|
|
PRUint32 flags; |
|
aAuthInfo->GetFlags(&flags); |
|
PRBool proxyAuth = (flags & nsIAuthInformation::AUTH_PROXY) != 0; |
|
|
|
nsAutoString realm; |
|
aAuthInfo->GetRealm(realm); |
|
|
|
// Append the port if it was specified |
|
if (port != -1) { |
|
displayHost.Append(PRUnichar(':')); |
|
displayHost.AppendInt(port); |
|
} |
|
|
|
NS_NAMED_LITERAL_STRING(proxyText, "EnterLoginForProxy"); |
|
NS_NAMED_LITERAL_STRING(originText, "EnterLoginForRealm"); |
|
NS_NAMED_LITERAL_STRING(noRealmText, "EnterUserPasswordFor"); |
|
NS_NAMED_LITERAL_STRING(passwordText, "EnterPasswordFor"); |
|
|
|
const PRUnichar *text; |
|
if (proxyAuth) { |
|
text = proxyText.get(); |
|
} else { |
|
text = originText.get(); |
|
|
|
// prepend "scheme://" |
|
nsAutoString schemeU; |
|
CopyASCIItoUTF16(scheme, schemeU); |
|
schemeU.AppendLiteral("://"); |
|
displayHost.Insert(schemeU, 0); |
|
} |
|
|
|
const PRUnichar *strings[] = { realm.get(), displayHost.get() }; |
|
PRUint32 count = NS_ARRAY_LENGTH(strings); |
|
|
|
if (flags & nsIAuthInformation::ONLY_PASSWORD) { |
|
text = passwordText.get(); |
|
strings[0] = username.get(); |
|
} else if (!proxyAuth && realm.IsEmpty()) { |
|
text = noRealmText.get(); |
|
count--; |
|
strings[0] = strings[1]; |
|
} |
|
|
|
rv = bundle->FormatStringFromName(text, strings, count, getter_Copies(message)); |
|
return rv; |
|
} |
|
|
|
NS_METHOD |
|
GtkPromptService::PromptAuth(nsIDOMWindow *aParent, |
|
nsIChannel *aChannel, |
|
PRUint32 level, |
|
nsIAuthInformation *authInfo, |
|
const PRUnichar *checkboxLabel, |
|
PRBool *checkValue, |
|
PRBool *retval) |
|
{ |
|
NS_ENSURE_ARG_POINTER (retval); |
|
NS_ENSURE_ARG_POINTER (authInfo); |
|
|
|
nsString message; |
|
MakeDialogText(aChannel, authInfo, message); |
|
|
|
nsAutoString defaultUser, defaultDomain, defaultPass; |
|
authInfo->GetUsername(defaultUser); |
|
authInfo->GetDomain(defaultDomain); |
|
authInfo->GetPassword(defaultPass); |
|
|
|
PRUint32 flags; |
|
authInfo->GetFlags(&flags); |
|
|
|
if ((flags & nsIAuthInformation::NEED_DOMAIN) && !defaultDomain.IsEmpty()) { |
|
defaultDomain.Append(PRUnichar('\\')); |
|
defaultUser.Insert(defaultDomain, 0); |
|
} |
|
|
|
// NOTE: Allocation failure is not fatal here (just default to empty string |
|
// if allocation fails) |
|
PRUnichar *user = ToNewUnicode(defaultUser), |
|
*pass = ToNewUnicode(defaultPass); |
|
nsresult rv; |
|
if (flags & nsIAuthInformation::ONLY_PASSWORD) |
|
rv = PromptPassword(aParent, nsnull, message.get(), |
|
&pass, checkboxLabel, |
|
checkValue, retval); |
|
else |
|
rv = PromptUsernameAndPassword(aParent, nsnull, message.get(), |
|
&user, &pass, checkboxLabel, |
|
checkValue, retval); |
|
|
|
nsString userStr(user); |
|
nsString passStr(pass); |
|
authInfo->SetUsername(userStr); |
|
authInfo->SetPassword(passStr); |
|
|
|
return rv; |
|
} |
|
|
|
NS_METHOD GtkPromptService::AsyncPromptAuth(nsIDOMWindow *aParent, |
|
nsIChannel *aChannel, |
|
nsIAuthPromptCallback *aCallback, |
|
nsISupports *aContext, |
|
PRUint32 level, |
|
nsIAuthInformation *authInfo, |
|
const PRUnichar *checkboxLabel, |
|
PRBool *checkValue, |
|
nsICancelable **retval) |
|
{ |
|
return NS_ERROR_NOT_IMPLEMENTED; |
|
} |
|
|
|
#endif |