1 // Copyright 2006 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
9 #include "wchar_t_short.h"
11 #include <nsICharsetConverterManager.h>
12 #include <nsIComponentManager.h>
13 #include <nsIFactory.h>
14 #include <nsIComponentRegistrar.h>
15 #include <nsServiceManagerUtils.h>
16 #include <nsIUnicodeEncoder.h>
17 #include "wchar_t_default.h"
19 #include "null_prompt_service.hpp"
20 #include "videolink.hpp"
21 #include "xpcom_support.hpp"
23 using xpcom_support::check;
27 NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
29 class null_prompt_service_factory : public nsIFactory
35 NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
37 NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
38 nsISupports *, const nsIID & iid, void ** result)
40 if (!iid.Equals(prompt_service_iid))
41 return NS_ERROR_NO_INTERFACE;
43 if (null_prompt_service * service =
44 new (std::nothrow) null_prompt_service)
52 return NS_ERROR_OUT_OF_MEMORY;
56 NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool /*lock*/)
58 return NS_ERROR_NOT_IMPLEMENTED;
61 std::string native_error_string(const PRUnichar * text)
65 while (text[text_len])
68 nsCOMPtr<nsICharsetConverterManager> conv_manager;
69 nsCOMPtr<nsIUnicodeEncoder> encoder;
70 static const nsCID charset_converter_manager_cid =
71 NS_ICHARSETCONVERTERMANAGER_CID;
72 if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
73 charset_converter_manager_cid,
74 getter_AddRefs(conv_manager)))
75 && NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
77 getter_AddRefs(encoder))))
79 encoder->SetOutputErrorBehavior(
80 nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
82 char buf[1000]; // Hopefully long enough for an error message
85 PRInt32 out_len = sizeof(buf);
86 encoder->Convert(text, &text_len, out, &out_len);
89 out_len = sizeof(buf) - out_len;
90 encoder->Finish(out, &out_len);
93 result.assign(buf, out);
98 result.resize(text_len);
99 for (PRInt32 i = 0; i != text_len; ++i)
100 result[i] = (text[i] < 0x80) ? text[i] : '?';
107 NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
109 NS_IMETHODIMP null_prompt_service::Alert(
110 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
112 fatal_error(native_error_string(text));
116 NS_IMETHODIMP null_prompt_service::AlertCheck(
117 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
118 const PRUnichar *, PRBool *)
120 fatal_error(native_error_string(text));
124 NS_IMETHODIMP null_prompt_service::Confirm(
125 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
132 NS_IMETHODIMP null_prompt_service::ConfirmCheck(
133 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
134 const PRUnichar *, PRBool *, PRBool * result)
141 NS_IMETHODIMP null_prompt_service::ConfirmEx(
142 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
143 PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
144 const PRUnichar *, PRBool *, PRInt32 * result)
146 // Accept the default
147 if (flags & BUTTON_POS_1_DEFAULT)
149 else if (flags & BUTTON_POS_2_DEFAULT)
156 NS_IMETHODIMP null_prompt_service::Prompt(
157 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
158 const PRUnichar *, PRBool *, PRBool * result)
165 NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
166 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
167 PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
174 NS_IMETHODIMP null_prompt_service::PromptPassword(
175 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
176 const PRUnichar *, PRBool *, PRBool * result)
183 NS_IMETHODIMP null_prompt_service::Select(
184 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
185 const PRUnichar **, PRInt32 *, PRBool * result)
192 void null_prompt_service::install()
194 static const nsCID prompt_service_cid = {
195 0xa2112d6a, 0x0e28, 0x421f,
196 {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
198 nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
199 nsCOMPtr<nsIComponentRegistrar> comp_registrar;
200 check(NS_GetComponentRegistrar(getter_AddRefs(comp_registrar)));
201 check(comp_registrar->RegisterFactory(
204 "@mozilla.org/embedcomp/prompt-service;1",