summaryrefslogtreecommitdiffstats
path: root/qtinterface/tqtranslator.cpp
blob: 9fc41461a027b0aae78f3c85d6758326d37e1c2d (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*

Copyright (C) 2010 Timothy Pearson <[email protected]>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.

*/

#include <tqt.h>
#include <tqtranslator.h>

#ifdef USE_QT4

static uint elfHash( const char * name )
{
    const uchar *k;
    uint h = 0;
    uint g;

    if ( name ) {
	k = (const uchar *) name;
	while ( *k ) {
	    h = ( h << 4 ) + *k++;
	    if ( (g = (h & 0xf0000000)) != 0 )
		h ^= g >> 24;
	    h &= ~g;
	}
    }
    if ( !h )
	h = 1;
    return h;
}

/*!
    \class QTranslatorMessage

    \brief The QTranslatorMessage class contains a translator message and its
    properties.

    \ingroup i18n
    \ingroup environment

    This class is of no interest to most applications. It is useful
    for translation tools such as \link linguist-manual.book Qt
    Linguist\endlink. It is provided simply to make the API complete
    and regular.

    For a QTranslator object, a lookup key is a triple (\e context, \e
    {source text}, \e comment) that uniquely identifies a message. An
    extended key is a quadruple (\e hash, \e context, \e {source
    text}, \e comment), where \e hash is computed from the source text
    and the comment. Unless you plan to read and write messages
    yourself, you need not worry about the hash value.

    QTranslatorMessage stores this triple or quadruple and the relevant
    translation if there is any.

    \sa QTranslator
*/

/*!
    Constructs a translator message with the extended key (0, 0, 0, 0)
    and QString::null as translation.
*/

QTranslatorMessage::QTranslatorMessage()
    : h( 0 ), cx( 0 ), st( 0 ), cm( 0 )
{
}


/*!
    Constructs an translator message with the extended key (\e h, \a
    context, \a sourceText, \a comment), where \e h is computed from
    \a sourceText and \a comment, and possibly with a \a translation.
*/

QTranslatorMessage::QTranslatorMessage( const char * context,
					const char * sourceText,
					const char * comment,
					const QString& translation )
    : cx( context ), st( sourceText ), cm( comment ), tn( translation )
{
    // 0 means we don't know, "" means empty
    if ( cx == (const char*)0 )
	cx = "";
    if ( st == (const char*)0 )
	st = "";
    if ( cm == (const char*)0 )
	cm = "";
    h = elfHash( st + cm );
}


/*!
    Constructs a translator message read from the \a stream. The
    resulting message may have any combination of content.

    \sa QTranslator::save()
*/

QTranslatorMessage::QTranslatorMessage( QDataStream & stream )
    : cx( 0 ), st( 0 ), cm( 0 )
{
    QString str16;
    char tag;
    Q_UINT8 obs1;

    for ( ;; ) {
	tag = 0;
	if ( !stream.atEnd() )
	    stream.readRawBytes( &tag, 1 );
	switch( (Tag)tag ) {
	case Tag_End:
	    if ( h == 0 )
		h = elfHash( st + cm );
	    return;
	case Tag_SourceText16: // obsolete
	    stream >> str16;
	    st = str16.latin1();
	    break;
	case Tag_Translation:
	    stream >> tn;
	    break;
	case Tag_Context16: // obsolete
	    stream >> str16;
	    cx = str16.latin1();
	    break;
	case Tag_Hash:
	    stream >> h;
	    break;
	case Tag_SourceText:
	    stream >> st;
	    break;
	case Tag_Context:
	    stream >> cx;
	    if ( cx == "" ) // for compatibility
		cx = 0;
	    break;
	case Tag_Comment:
	    stream >> cm;
	    break;
	case Tag_Obsolete1: // obsolete
	    stream >> obs1;
	    break;
	default:
	    h = 0;
	    st = 0;
	    cx = 0;
	    cm = 0;
	    tn = QString::null;
	    return;
	}
    }
}


/*!
    Constructs a copy of translator message \a m.
*/

QTranslatorMessage::QTranslatorMessage( const QTranslatorMessage & m )
    : cx( m.cx ), st( m.st ), cm( m.cm ), tn( m.tn )
{
    h = m.h;
}


/*!
    Assigns message \a m to this translator message and returns a
    reference to this translator message.
*/

QTranslatorMessage & QTranslatorMessage::operator=(
	const QTranslatorMessage & m )
{
    h = m.h;
    cx = m.cx;
    st = m.st;
    cm = m.cm;
    tn = m.tn;
    return *this;
}


/*!
    \fn uint QTranslatorMessage::hash() const

    Returns the hash value used internally to represent the lookup
    key. This value is zero only if this translator message was
    constructed from a stream containing invalid data.

    The hashing function is unspecified, but it will remain unchanged
    in future versions of Qt.
*/

/*!
    \fn const char *QTranslatorMessage::context() const

    Returns the context for this message (e.g. "MyDialog").

    \warning This may return 0 if the QTranslator object is stripped
    (compressed).
*/

/*!
    \fn const char *QTranslatorMessage::sourceText() const

    Returns the source text of this message (e.g. "&Save").

    \warning This may return 0 if the QTranslator object is stripped
    (compressed).
*/

/*!
    \fn const char *QTranslatorMessage::comment() const

    Returns the comment for this message (e.g. "File|Save").

    \warning This may return 0 if the QTranslator object is stripped
    (compressed).
*/

/*!
    \fn void QTranslatorMessage::setTranslation( const QString & translation )

    Sets the translation of the source text to \a translation.

    \sa translation()
*/

/*!
    \fn QString QTranslatorMessage::translation() const

    Returns the translation of the source text (e.g., "&Sauvegarder").

    \sa setTranslation()
*/

/*!
    \enum QTranslatorMessage::Prefix

    Let (\e h, \e c, \e s, \e m) be the extended key. The possible
    prefixes are

    \value NoPrefix  no prefix
    \value Hash  only (\e h)
    \value HashContext  only (\e h, \e c)
    \value HashContextSourceText  only (\e h, \e c, \e s)
    \value HashContextSourceTextComment  the whole extended key, (\e
	h, \e c, \e s, \e m)

    \sa write() commonPrefix()
*/

/*!
    Writes this translator message to the \a stream. If \a strip is
    FALSE (the default), all the information in the message is
    written. If \a strip is TRUE, only the part of the extended key
    specified by \a prefix is written with the translation (\c
    HashContextSourceTextComment by default).

    \sa commonPrefix()
*/

void QTranslatorMessage::write( QDataStream & stream, bool strip,
				Prefix prefix ) const
{
    char tag;

    tag = (char)Tag_Translation;
    stream.writeRawBytes( &tag, 1 );
    stream << tn;

    bool mustWriteHash = TRUE;
    if ( !strip )
	prefix = HashContextSourceTextComment;

    switch ( prefix ) {
    case HashContextSourceTextComment:
	tag = (char)Tag_Comment;
	stream.writeRawBytes( &tag, 1 );
	stream << cm;
	// fall through
    case HashContextSourceText:
	tag = (char)Tag_SourceText;
	stream.writeRawBytes( &tag, 1 );
	stream << st;
	// fall through
    case HashContext:
	tag = (char)Tag_Context;
	stream.writeRawBytes( &tag, 1 );
	stream << cx;
	// fall through
    default:
	if ( mustWriteHash ) {
	    tag = (char)Tag_Hash;
	    stream.writeRawBytes( &tag, 1 );
	    stream << h;
	}
    }

    tag = (char)Tag_End;
    stream.writeRawBytes( &tag, 1 );
}


/*!
    Returns the widest lookup prefix that is common to this translator
    message and to message \a m.

    For example, if the extended key is for this message is (71,
    "PrintDialog", "Yes", "Print?") and that for \a m is (71,
    "PrintDialog", "No", "Print?"), this function returns \c
    HashContext.

    \sa write()
*/

QTranslatorMessage::Prefix QTranslatorMessage::commonPrefix(
	const QTranslatorMessage& m ) const
{
    if ( h != m.h )
	return NoPrefix;
    if ( cx != m.cx )
	return Hash;
    if ( st != m.st )
	return HashContext;
    if ( cm != m.cm )
	return HashContextSourceText;
    return HashContextSourceTextComment;
}


/*!
 Returns TRUE if the extended key of this object is equal to that of
 \a m; otherwise returns FALSE.
*/

bool QTranslatorMessage::operator==( const QTranslatorMessage& m ) const
{
    return h == m.h && cx == m.cx && st == m.st && cm == m.cm;
}


/*!
    \fn bool QTranslatorMessage::operator!=( const QTranslatorMessage& m ) const

    Returns TRUE if the extended key of this object is different from
    that of \a m; otherwise returns FALSE.
*/


/*!
    Returns TRUE if the extended key of this object is
    lexicographically before than that of \a m; otherwise returns
    FALSE.
*/

bool QTranslatorMessage::operator<( const QTranslatorMessage& m ) const
{
    return h != m.h ? h < m.h
	   : ( cx != m.cx ? cx < m.cx
	     : (st != m.st ? st < m.st : cm < m.cm) );
}


/*!
    \fn bool QTranslatorMessage::operator<=( const QTranslatorMessage& m ) const

    Returns TRUE if the extended key of this object is
    lexicographically before that of \a m or if they are equal;
    otherwise returns FALSE.
*/

/*!
    \fn bool QTranslatorMessage::operator>( const QTranslatorMessage& m ) const

    Returns TRUE if the extended key of this object is
    lexicographically after that of \a m; otherwise returns FALSE.
*/

/*!
    \fn bool QTranslatorMessage::operator>=( const QTranslatorMessage& m ) const

    Returns TRUE if the extended key of this object is
    lexicographically after that of \a m or if they are equal;
    otherwise returns FALSE.
*/

#endif // USE_QT4