1 // Copyright 2006 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
10 #include <nsICharsetConverterManager.h>
11 #include <nsIComponentManager.h>
12 #include <nsIFactory.h>
13 #include <nsIComponentRegistrar.h>
14 #include <nsServiceManagerUtils.h>
15 #include <nsIUnicodeEncoder.h>
17 #include "null_prompt_service.hpp"
18 #include "videolink.hpp"
19 #include "xpcom_support.hpp"
21 using xpcom_support::check;
25 NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
27 class null_prompt_service_factory : public nsIFactory
33 NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
35 NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
36 nsISupports *, const nsIID & iid, void ** result)
38 if (!iid.Equals(prompt_service_iid))
39 return NS_ERROR_NO_INTERFACE;
41 if (null_prompt_service * service =
42 new (std::nothrow) null_prompt_service)
50 return NS_ERROR_OUT_OF_MEMORY;
54 NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool /*lock*/)
56 return NS_ERROR_NOT_IMPLEMENTED;
59 std::string native_error_string(const PRUnichar * text)
63 while (text[text_len])
66 nsCOMPtr<nsICharsetConverterManager> conv_manager;
67 nsCOMPtr<nsIUnicodeEncoder> encoder;
68 static const nsCID charset_converter_manager_cid =
69 NS_ICHARSETCONVERTERMANAGER_CID;
70 if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
71 charset_converter_manager_cid,
72 getter_AddRefs(conv_manager)))
73 && NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
75 getter_AddRefs(encoder))))
77 encoder->SetOutputErrorBehavior(
78 nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
80 char buf[1000]; // Hopefully long enough for an error message
83 PRInt32 out_len = sizeof(buf);
84 encoder->Convert(text, &text_len, out, &out_len);
87 out_len = sizeof(buf) - out_len;
88 encoder->Finish(out, &out_len);
91 result.assign(buf, out);
96 result.resize(text_len);
97 for (PRInt32 i = 0; i != text_len; ++i)
98 result[i] = (text[i] < 0x80) ? text[i] : '?';
105 NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
107 NS_IMETHODIMP null_prompt_service::Alert(
108 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
110 fatal_error(native_error_string(text));
114 NS_IMETHODIMP null_prompt_service::AlertCheck(
115 nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
116 const PRUnichar *, PRBool *)
118 fatal_error(native_error_string(text));
122 NS_IMETHODIMP null_prompt_service::Confirm(
123 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
130 NS_IMETHODIMP null_prompt_service::ConfirmCheck(
131 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
132 const PRUnichar *, PRBool *, PRBool * result)
139 NS_IMETHODIMP null_prompt_service::ConfirmEx(
140 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
141 PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
142 const PRUnichar *, PRBool *, PRInt32 * result)
144 // Accept the default
145 if (flags & BUTTON_POS_1_DEFAULT)
147 else if (flags & BUTTON_POS_2_DEFAULT)
154 NS_IMETHODIMP null_prompt_service::Prompt(
155 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
156 const PRUnichar *, PRBool *, PRBool * result)
163 NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
164 nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
165 PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
172 NS_IMETHODIMP null_prompt_service::PromptPassword(
173 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
174 const PRUnichar *, PRBool *, PRBool * result)
181 NS_IMETHODIMP null_prompt_service::Select(
182 nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
183 const PRUnichar **, PRInt32 *, PRBool * result)
190 void null_prompt_service::install()
192 static const nsCID prompt_service_cid = {
193 0xa2112d6a, 0x0e28, 0x421f,
194 {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
196 nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
197 nsCOMPtr<nsIComponentRegistrar> comp_registrar;
198 check(NS_GetComponentRegistrar(getter_AddRefs(comp_registrar)));
199 check(comp_registrar->RegisterFactory(
202 "@mozilla.org/embedcomp/prompt-service;1",