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