1 // Copyright 2006 Ben Hutchings <ben@decadentplace.org.uk>.
2 // See the file "COPYING" for licence details.
9 #include <nsICharsetConverterManager.h>
10 #include <nsIComponentManager.h>
11 #include <nsIFactory.h>
12 #include <nsIServiceManagerUtils.h>
13 #include <nsIUnicodeEncoder.h>
15 #include "null_prompt_service.hpp"
17 #include "xpcom_support.hpp"
19 using xpcom_support::check;
23 NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
25 class null_prompt_service_factory : public nsIFactory
31 NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
33 NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
34 nsISupports *, const nsIID & iid, void ** result)
36 if (!iid.Equals(prompt_service_iid))
37 return NS_ERROR_NO_INTERFACE;
41 *result = new null_prompt_service;
44 catch (std::bad_alloc &)
46 return NS_ERROR_OUT_OF_MEMORY;
50 NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool lock)
52 return NS_ERROR_NOT_IMPLEMENTED;
55 std::string native_error_string(const PRUnichar * text)
59 while (text[text_len])
62 nsCOMPtr<nsICharsetConverterManager> conv_manager;
63 nsCOMPtr<nsIUnicodeEncoder> encoder;
64 static const nsCID charset_converter_manager_cid =
65 NS_ICHARSETCONVERTERMANAGER_CID;
66 if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
67 charset_converter_manager_cid,
68 getter_AddRefs(conv_manager)))
69 && NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
71 getter_AddRefs(encoder))))
73 encoder->SetOutputErrorBehavior(
74 nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
76 char buf[1000]; // Hopefully long enough for an error message
79 PRInt32 out_len = sizeof(buf);
80 encoder->Convert(text, &text_len, out, &out_len);
83 out_len = sizeof(buf) - out_len;
84 encoder->Finish(out, &out_len);
87 result.assign(buf, out);
92 result.resize(text_len);
93 for (PRInt32 i = 0; i != text_len; ++i)
94 result[i] = (text[i] < 0x80) ? text[i] : '?';
101 NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
103 NS_IMETHODIMP null_prompt_service::Alert(
104 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
106 fatal_error(native_error_string(text));
110 NS_IMETHODIMP null_prompt_service::AlertCheck(
111 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
112 const PRUnichar *, PRBool *)
114 fatal_error(native_error_string(text));
118 NS_IMETHODIMP null_prompt_service::Confirm(
119 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
126 NS_IMETHODIMP null_prompt_service::ConfirmCheck(
127 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
128 const PRUnichar *, PRBool *, PRBool * result)
135 NS_IMETHODIMP null_prompt_service::ConfirmEx(
136 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
137 PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
138 const PRUnichar *, PRBool *, PRInt32 * result)
140 // Accept the default
141 if (flags & BUTTON_POS_1_DEFAULT)
143 else if (flags & BUTTON_POS_2_DEFAULT)
150 NS_IMETHODIMP null_prompt_service::Prompt(
151 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
152 const PRUnichar *, PRBool *, PRBool * result)
159 NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
160 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
161 PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
168 NS_IMETHODIMP null_prompt_service::PromptPassword(
169 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
170 const PRUnichar *, PRBool *, PRBool * result)
177 NS_IMETHODIMP null_prompt_service::Select(
178 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
179 const PRUnichar **, PRInt32 *, PRBool * result)
186 void null_prompt_service::install()
188 static const nsCID prompt_service_cid = {
189 0xa2112d6a, 0x0e28, 0x421f,
190 {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
192 nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
193 check(nsComponentManager::RegisterFactory(
196 "@mozilla.org/embedcomp/prompt-service;1",
198 PR_TRUE)); // replace existing