summaryrefslogtreecommitdiffstats
path: root/comm/suite/chatzilla/js/lib/message-manager.js
blob: 56020d48f634c0df172639e9827580b614fd4d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//
function MessageManager(entities)
{
    const UC_CTRID = "@mozilla.org/intl/scriptableunicodeconverter";
    const nsIUnicodeConverter = 
        Components.interfaces.nsIScriptableUnicodeConverter;

    this.ucConverter =
        Components.classes[UC_CTRID].getService(nsIUnicodeConverter);
    this.defaultBundle = null;
    this.bundleList = new Array();
    // Provide a fallback so we don't break getMsg and related constants later.
    this.entities = entities || {};
}

// ISO-2022-JP (often used for Japanese on IRC) doesn't contain any support
// for hankaku kana (half-width katakana), so we support the option to convert
// it to zenkaku kana (full-width katakana). This does not affect any other
// encoding at this time.
MessageManager.prototype.enableHankakuToZenkaku = false;

MessageManager.prototype.loadBrands =
function mm_loadbrands()
{
    var entities = this.entities;
    var app = getService("@mozilla.org/xre/app-info;1", "nsIXULAppInfo");
    if (app)
    {
        // Use App info if possible
        entities.brandShortName = app.name;
        entities.brandFullName = app.name + " " + app.version;
        entities.brandVendorName = app.vendor;
        return;
    }

    var brandBundle;
    var path = "chrome://branding/locale/brand.properties";
    try
    {
        brandBundle = this.addBundle(path);
    }
    catch (exception)
    {
        // May be an older mozilla version, try another location.
        path = "chrome://global/locale/brand.properties";
        brandBundle = this.addBundle(path);
    }

    entities.brandShortName = brandBundle.GetStringFromName("brandShortName");
    entities.brandVendorName = brandBundle.GetStringFromName("vendorShortName");
    // Not all versions of Suite / Fx have this defined; Cope:
    try
    {
        entities.brandFullName = brandBundle.GetStringFromName("brandFullName");
    }
    catch(exception)
    {
        entities.brandFullName = entities.brandShortName;
    }

    // Remove all of this junk, or it will be the default bundle for getMsg...
    this.bundleList.pop();
}

MessageManager.prototype.addBundle = 
function mm_addbundle(bundlePath, targetWindow)
{
    var bundle = srGetStrBundle(bundlePath);
    this.bundleList.push(bundle);

    // The bundle will load if the file doesn't exist. This will fail though.
    // We want to be clean and remove the bundle again.
    try
    {
        this.importBundle(bundle, targetWindow, this.bundleList.length - 1);
    }
    catch (exception)
    {
        // Clean up and return the exception.
        this.bundleList.pop();
        throw exception;
    }
    return bundle;
}

MessageManager.prototype.importBundle =
function mm_importbundle(bundle, targetWindow, index)
{
    var me = this;
    function replaceEntities(matched, entity)
    {
        if (entity in me.entities)
            return me.entities[entity];

        return matched;
    };
    const nsIPropertyElement = Components.interfaces.nsIPropertyElement;

    if (!targetWindow)
        targetWindow = window;

    if (typeof index == "undefined")
        index = arrayIndexOf(this.bundleList, bundle);
    
    var pfx;
    if (index == 0)
        pfx = "";
    else
        pfx = index + ":";

    var enumer = bundle.getSimpleEnumeration();

    while (enumer.hasMoreElements())
    {
        var prop = enumer.getNext().QueryInterface(nsIPropertyElement);
        var ary = prop.key.match (/^(msg|msn)/);
        if (ary)
        {
            var constValue;
            var constName = prop.key.toUpperCase().replace (/\./g, "_");
            if (ary[1] == "msn" || prop.value.search(/%(\d+\$)?s/i) != -1)
                constValue = pfx + prop.key;
            else
                constValue = prop.value.replace (/^\"/, "").replace (/\"$/, "");

            constValue = constValue.replace(/\&(\w+)\;/g, replaceEntities);
            targetWindow[constName] = constValue;
        }
    }

    if (this.bundleList.length == 1)
        this.defaultBundle = bundle;
}

MessageManager.prototype.convertHankakuToZenkaku =
function mm_converthankakutozenkaku(msg)
{
    const basicMapping = [
        /* 0xFF60 */ 0xFF60,0x3002,0x300C,0x300D,0x3001,0x30FB,0x30F2,0x30A1,
        /* 0xFF68 */ 0x30A3,0x30A5,0x30A7,0x30A9,0x30E3,0x30E5,0x30E7,0x30C3,
        /* 0xFF70 */ 0x30FC,0x30A2,0x30A4,0x30A6,0x30A8,0x30AA,0x30AB,0x30AD,
        /* 0xFF78 */ 0x30AF,0x30B1,0x30B3,0x30B5,0x30B7,0x30B9,0x30BB,0x30BD,
        /* 0xFF80 */ 0x30BF,0x30C1,0x30C4,0x30C6,0x30C8,0x30CA,0x30CB,0x30CC,
        /* 0xFF88 */ 0x30CD,0x30CE,0x30CF,0x30D2,0x30D5,0x30D8,0x30DB,0x30DE,
        /* 0xFF90 */ 0x30DF,0x30E0,0x30E1,0x30E2,0x30E4,0x30E6,0x30E8,0x30E9,
        /* 0xFF98 */ 0x30EA,0x30EB,0x30EC,0x30ED,0x30EF,0x30F3,0x309B,0x309C
    ];

    const HANKAKU_BASE1 = 0xFF60;
    const HANKAKU_BASE2 = 0xFF80;
    const HANKAKU_MASK  = 0xFFE0;

    const MOD_NIGORI      = 0xFF9E;
    const NIGORI_MIN1     = 0xFF76;
    const NIGORI_MAX1     = 0xFF84;
    const NIGORI_MIN2     = 0xFF8A;
    const NIGORI_MAX2     = 0xFF8E;
    const NIGORI_MODIFIER = 1;

    const MOD_MARU      = 0xFF9F;
    const MARU_MIN      = 0xFF8A;
    const MARU_MAX      = 0xFF8E;
    const MARU_MODIFIER = 2;

    var i, src, srcMod, dest;
    var rv = "";

    for (i = 0; i < msg.length; i++)
    {
        // Get both this character and the next one, which could be a modifier.
        src = msg.charCodeAt(i);
        if (i < msg.length - 1)
            srcMod = msg.charCodeAt(i + 1);

        // Is the source characher hankaku?
        if ((HANKAKU_BASE1 == (src & HANKAKU_MASK)) ||
            (HANKAKU_BASE2 == (src & HANKAKU_MASK)))
        {
            // Do the basic character mapping first.
            dest = basicMapping[src - HANKAKU_BASE1];

            // If the source character is in the nigori or maru ranges and
            // the following character is the associated modifier, we apply
            // the modification and skip over the modifier.
            if (i < msg.length - 1)
            {
                if ((MOD_NIGORI == srcMod) &&
                    (((src >= NIGORI_MIN1) && (src <= NIGORI_MAX1)) ||
                     ((src >= NIGORI_MIN2) && (src <= NIGORI_MAX2))))
                {
                    dest += NIGORI_MODIFIER;
                    i++;
                }
                else if ((MOD_MARU == srcMod) &&
                         (src >= MARU_MIN) && (src <= MARU_MAX))
                {
                    dest += MARU_MODIFIER;
                    i++;
                }
            }

            rv += String.fromCharCode(dest);
        }
        else
        {
            rv += msg[i];
        }
    }

    return rv;
}

MessageManager.prototype.checkCharset =
function mm_checkset(charset)
{
    try
    {
        this.ucConverter.charset = charset;
    }
    catch (ex)
    {
        return false;
    }
    
    return true;
}

MessageManager.prototype.toUnicode =
function mm_tounicode(msg, charset)
{
    if (!charset)
        return msg;
    
    try
    {
        this.ucConverter.charset = charset;
        msg = this.ucConverter.ConvertToUnicode(msg);
    }
    catch (ex)
    {
        //dd ("caught exception " + ex + " converting " + msg + " to charset " +
        //    charset);
    }

    return msg;
}

MessageManager.prototype.fromUnicode =
function mm_fromunicode(msg, charset)
{
    if (!charset)
        return msg;

    if (this.enableHankakuToZenkaku && (charset.toLowerCase() == "iso-2022-jp"))
        msg = this.convertHankakuToZenkaku(msg);

    try
    {
        // This can actually fail in bizare cases. Cope.
        if (charset != this.ucConverter.charset)
            this.ucConverter.charset = charset;

        if ("Finish" in this.ucConverter)
        {
            msg = this.ucConverter.ConvertFromUnicode(msg) +
                this.ucConverter.Finish();
        }
        else
        {
            msg = this.ucConverter.ConvertFromUnicode(msg + " ");
            msg = msg.substr(0, msg.length - 1);
        }
    }
    catch (ex)
    {
        //dd ("caught exception " + ex + " converting " + msg + " to charset " +
        //    charset);
    }
    
    return msg;
}

MessageManager.prototype.getMsg = 
function mm_getmsg (msgName, params, deflt)
{
    try
    {    
        var bundle;
        var ary = msgName.match (/(\d+):(.+)/);
        if (ary)
        {
            return (this.getMsgFrom(this.bundleList[ary[1]], ary[2], params,
                                    deflt));
        }
        
        return this.getMsgFrom(this.bundleList[0], msgName, params, deflt);
    }
    catch (ex)
    {
        ASSERT (0, "Caught exception getting message: " + msgName + "/" +
                params);
        return deflt ? deflt : msgName;
    }
}

MessageManager.prototype.getMsgFrom =
function mm_getfrom (bundle, msgName, params, deflt)
{
    var me = this;
    function replaceEntities(matched, entity)
    {
        if (entity in me.entities)
            return me.entities[entity];

        return matched;
    };

    try 
    {
        var rv;
        
        if (params && isinstance(params, Array))
            rv = bundle.formatStringFromName (msgName, params, params.length);
        else if (params || params == 0)
            rv = bundle.formatStringFromName (msgName, [params], 1);
        else
            rv = bundle.GetStringFromName (msgName);
        
        /* strip leading and trailing quote characters, see comment at the
         * top of venkman.properties.
         */
        rv = rv.replace(/^\"/, "");
        rv = rv.replace(/\"$/, "");
        rv = rv.replace(/\&(\w+)\;/g, replaceEntities);

        return rv;
    }
    catch (ex)
    {
        if (typeof deflt == "undefined")
        {
            ASSERT (0, "caught exception getting value for ``" + msgName +
                    "''\n" + ex + "\n");
            return msgName;
        }
        return deflt;
    }

    return null;
}