~ubuntu-branches/ubuntu/hardy/videolink/hardy

« back to all changes in this revision

Viewing changes to null_prompt_service.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings
  • Date: 2006-10-15 02:05:46 UTC
  • Revision ID: james.westby@ubuntu.com-20061015020546-0t2uoscwlmwyrp5q
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

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