]> git.decadent.org.uk Git - videolink.git/blob - null_prompt_service.cpp
Renamed package due to name clash.
[videolink.git] / null_prompt_service.cpp
1 // Copyright 2006 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
3
4 #include <new>
5 #include <string>
6
7 #include <langinfo.h>
8
9 #include <nsCOMPtr.h>
10 #include <nsICharsetConverterManager.h>
11 #include <nsIComponentManager.h>
12 #include <nsIFactory.h>
13 #include <nsIServiceManagerUtils.h>
14 #include <nsIUnicodeEncoder.h>
15
16 #include "null_prompt_service.hpp"
17 #include "videolink.hpp"
18 #include "xpcom_support.hpp"
19
20 using xpcom_support::check;
21
22 namespace
23 {
24     NS_DEFINE_IID(prompt_service_iid, NS_IPROMPTSERVICE_IID);
25
26     class null_prompt_service_factory : public nsIFactory
27     {
28         NS_DECL_ISUPPORTS
29         NS_DECL_NSIFACTORY
30     };
31
32     NS_IMPL_ISUPPORTS1(null_prompt_service_factory, nsIFactory)
33
34     NS_IMETHODIMP null_prompt_service_factory::CreateInstance(
35         nsISupports *, const nsIID & iid, void ** result)
36     {
37         if (!iid.Equals(prompt_service_iid))
38             return NS_ERROR_NO_INTERFACE;
39
40         if (null_prompt_service * service =
41                 new (std::nothrow) null_prompt_service)
42         {
43             service->AddRef();
44             *result = service;
45             return NS_OK;
46         }
47         else
48         {
49             return NS_ERROR_OUT_OF_MEMORY;
50         }
51     }
52
53     NS_IMETHODIMP null_prompt_service_factory::LockFactory(PRBool lock)
54     {
55         return NS_ERROR_NOT_IMPLEMENTED;
56     }
57
58     std::string native_error_string(const PRUnichar * text)
59     {
60         std::string result;
61         PRInt32 text_len = 0;
62         while (text[text_len])
63             ++text_len;
64
65         nsCOMPtr<nsICharsetConverterManager> conv_manager;
66         nsCOMPtr<nsIUnicodeEncoder> encoder;
67         static const nsCID charset_converter_manager_cid =
68             NS_ICHARSETCONVERTERMANAGER_CID;
69         if (NS_SUCCEEDED(CallGetService<nsICharsetConverterManager>(
70                              charset_converter_manager_cid,
71                              getter_AddRefs(conv_manager)))
72             && NS_SUCCEEDED(conv_manager->GetUnicodeEncoder(
73                                 nl_langinfo(CODESET),
74                                 getter_AddRefs(encoder))))
75         {
76             encoder->SetOutputErrorBehavior(
77                 nsIUnicodeEncoder::kOnError_Replace, NULL, PRUnichar('?'));
78
79             char buf[1000];  // Hopefully long enough for an error message
80
81             char * out = buf;
82             PRInt32 out_len = sizeof(buf);
83             encoder->Convert(text, &text_len, out, &out_len);
84             out += out_len;
85
86             out_len = sizeof(buf) - out_len;
87             encoder->Finish(out, &out_len);
88             out += out_len;
89
90             result.assign(buf, out);
91         }
92         else
93         {
94             // Convert to ASCII
95             result.resize(text_len);
96             for (PRInt32 i = 0; i != text_len; ++i)
97                 result[i] = (text[i] < 0x80) ? text[i] : '?';
98         }
99
100         return result;
101     }
102 }
103
104 NS_IMPL_ISUPPORTS1(null_prompt_service, nsIPromptService)
105
106 NS_IMETHODIMP null_prompt_service::Alert(
107     nsIDOMWindow *, const PRUnichar *, const PRUnichar * text)
108 {
109     fatal_error(native_error_string(text));
110     return NS_OK;
111 }
112
113 NS_IMETHODIMP null_prompt_service::AlertCheck(
114     nsIDOMWindow *, const PRUnichar *, const PRUnichar * text,
115     const PRUnichar *, PRBool *)
116 {
117     fatal_error(native_error_string(text));
118     return NS_OK;
119 }
120
121 NS_IMETHODIMP null_prompt_service::Confirm(
122     nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRBool * result)
123 {
124     // Cancel
125     *result = false;
126     return NS_OK;
127 }
128
129 NS_IMETHODIMP null_prompt_service::ConfirmCheck(
130     nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
131     const PRUnichar *, PRBool *, PRBool * result)
132 {
133     // Cancel
134     *result = false;
135     return NS_OK;
136 }
137
138 NS_IMETHODIMP null_prompt_service::ConfirmEx(
139     nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
140     PRUint32 flags, const PRUnichar *, const PRUnichar *, const PRUnichar *,
141     const PRUnichar *, PRBool *, PRInt32 * result)
142 {
143     // Accept the default
144     if (flags & BUTTON_POS_1_DEFAULT)
145         *result = 1;
146     else if (flags & BUTTON_POS_2_DEFAULT)
147         *result = 2;
148     else
149         *result = 0;
150     return NS_OK;
151 }
152
153 NS_IMETHODIMP null_prompt_service::Prompt(
154     nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
155     const PRUnichar *, PRBool *, PRBool * result)
156 {
157     // Cancel
158     *result = false;
159     return NS_OK;
160 }
161
162 NS_IMETHODIMP null_prompt_service::PromptUsernameAndPassword(
163     nsIDOMWindow *, const PRUnichar *, const PRUnichar *,
164     PRUnichar **, PRUnichar **, const PRUnichar *, PRBool *, PRBool * result)
165 {
166     // Cancel
167     *result = false;
168     return NS_OK;
169 }
170
171 NS_IMETHODIMP null_prompt_service::PromptPassword(
172     nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUnichar **,
173     const PRUnichar *, PRBool *, PRBool * result)
174 {
175     // Cancel
176     *result = false;
177     return NS_OK;
178 }
179
180 NS_IMETHODIMP null_prompt_service::Select(
181     nsIDOMWindow *, const PRUnichar *, const PRUnichar *, PRUint32,
182     const PRUnichar **, PRInt32 *, PRBool * result)
183 {
184     // Cancel
185     *result = false;
186     return NS_OK;
187 }
188
189 void null_prompt_service::install()
190 {
191     static const nsCID prompt_service_cid = {
192         0xa2112d6a, 0x0e28, 0x421f,
193         {0xb4, 0x6a, 0x25, 0xc0, 0xb3, 0x8, 0xcb, 0xd0}
194     };
195     nsCOMPtr<nsIFactory> prompt_factory(new null_prompt_service_factory);
196     check(nsComponentManager::RegisterFactory(
197               prompt_service_cid,
198               "Prompt Service",
199               "@mozilla.org/embedcomp/prompt-service;1",
200               prompt_factory,
201               PR_TRUE)); // replace existing
202 }