tools/linguist/shared/qm.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Linguist of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "translator.h"
       
    43 
       
    44 #include <QtCore/QCoreApplication>
       
    45 #include <QtCore/QDebug>
       
    46 #include <QtCore/QDir>
       
    47 #include <QtCore/QFile>
       
    48 #include <QtCore/QFileInfo>
       
    49 #include <QtCore/QMap>
       
    50 #include <QtCore/QString>
       
    51 #include <QtCore/QTextCodec>
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 // magic number for the file
       
    56 static const int MagicLength = 16;
       
    57 static const uchar magic[MagicLength] = {
       
    58     0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95,
       
    59     0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd
       
    60 };
       
    61 
       
    62 
       
    63 namespace {
       
    64 
       
    65 enum Tag {
       
    66     Tag_End          = 1,
       
    67     Tag_SourceText16 = 2,
       
    68     Tag_Translation  = 3,
       
    69     Tag_Context16    = 4,
       
    70     Tag_Obsolete1    = 5,
       
    71     Tag_SourceText   = 6,
       
    72     Tag_Context      = 7,
       
    73     Tag_Comment      = 8,
       
    74     Tag_Obsolete2    = 9
       
    75 };
       
    76 
       
    77 enum Prefix {
       
    78     NoPrefix,
       
    79     Hash,
       
    80     HashContext,
       
    81     HashContextSourceText,
       
    82     HashContextSourceTextComment
       
    83 };
       
    84 
       
    85 } // namespace anon
       
    86 
       
    87 static uint elfHash(const QByteArray &ba)
       
    88 {
       
    89     const uchar *k = (const uchar *)ba.data();
       
    90     uint h = 0;
       
    91     uint g;
       
    92 
       
    93     if (k) {
       
    94         while (*k) {
       
    95             h = (h << 4) + *k++;
       
    96             if ((g = (h & 0xf0000000)) != 0)
       
    97                 h ^= g >> 24;
       
    98             h &= ~g;
       
    99         }
       
   100     }
       
   101     if (!h)
       
   102         h = 1;
       
   103     return h;
       
   104 }
       
   105 
       
   106 class ByteTranslatorMessage
       
   107 {
       
   108 public:
       
   109     ByteTranslatorMessage(
       
   110             const QByteArray &context,
       
   111             const QByteArray &sourceText,
       
   112             const QByteArray &comment,
       
   113             const QStringList &translations) :
       
   114         m_context(context),
       
   115         m_sourcetext(sourceText),
       
   116         m_comment(comment),
       
   117         m_translations(translations)
       
   118     {}
       
   119     const QByteArray &context() const { return m_context; }
       
   120     const QByteArray &sourceText() const { return m_sourcetext; }
       
   121     const QByteArray &comment() const { return m_comment; }
       
   122     const QStringList &translations() const { return m_translations; }
       
   123     bool operator<(const ByteTranslatorMessage& m) const;
       
   124 
       
   125 private:
       
   126     QByteArray m_context;
       
   127     QByteArray m_sourcetext;
       
   128     QByteArray m_comment;
       
   129     QStringList m_translations;
       
   130 };
       
   131 
       
   132 Q_DECLARE_TYPEINFO(ByteTranslatorMessage, Q_MOVABLE_TYPE);
       
   133 
       
   134 bool ByteTranslatorMessage::operator<(const ByteTranslatorMessage& m) const
       
   135 {
       
   136     if (m_context != m.m_context)
       
   137         return m_context < m.m_context;
       
   138     if (m_sourcetext != m.m_sourcetext)
       
   139         return m_sourcetext < m.m_sourcetext;
       
   140     return m_comment < m.m_comment;
       
   141 }
       
   142 
       
   143 class Releaser
       
   144 {
       
   145 public:
       
   146     struct Offset {
       
   147         Offset()
       
   148             : h(0), o(0)
       
   149         {}
       
   150         Offset(uint hash, uint offset)
       
   151             : h(hash), o(offset)
       
   152         {}
       
   153 
       
   154         bool operator<(const Offset &other) const {
       
   155             return (h != other.h) ? h < other.h : o < other.o;
       
   156         }
       
   157         bool operator==(const Offset &other) const {
       
   158             return h == other.h && o == other.o;
       
   159         }
       
   160         uint h;
       
   161         uint o;
       
   162     };
       
   163 
       
   164     enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88 };
       
   165 
       
   166     Releaser() : m_codec(0) {}
       
   167 
       
   168     void setCodecName(const QByteArray &codecName)
       
   169     {
       
   170         m_codec = QTextCodec::codecForName(codecName);
       
   171     }
       
   172 
       
   173     bool save(QIODevice *iod);
       
   174 
       
   175     void insert(const TranslatorMessage &msg, bool forceComment);
       
   176     void insertIdBased(const TranslatorMessage &message);
       
   177 
       
   178     void squeeze(TranslatorSaveMode mode);
       
   179 
       
   180     void setNumerusRules(const QByteArray &rules);
       
   181 
       
   182 private:
       
   183     Q_DISABLE_COPY(Releaser)
       
   184 
       
   185     // This should reproduce the byte array fetched from the source file, which
       
   186     // on turn should be the same as passed to the actual tr(...) calls
       
   187     QByteArray originalBytes(const QString &str, bool isUtf8) const;
       
   188 
       
   189     void insertInternal(const TranslatorMessage &message, bool forceComment, bool isUtf8);
       
   190 
       
   191     static Prefix commonPrefix(const ByteTranslatorMessage &m1, const ByteTranslatorMessage &m2);
       
   192 
       
   193     static uint msgHash(const ByteTranslatorMessage &msg);
       
   194 
       
   195     void writeMessage(const ByteTranslatorMessage & msg, QDataStream & stream,
       
   196         TranslatorSaveMode strip, Prefix prefix) const;
       
   197 
       
   198     // for squeezed but non-file data, this is what needs to be deleted
       
   199     QByteArray m_messageArray;
       
   200     QByteArray m_offsetArray;
       
   201     QByteArray m_contextArray;
       
   202     QMap<ByteTranslatorMessage, void *> m_messages;
       
   203     QByteArray m_numerusRules;
       
   204 
       
   205     // Used to reproduce the original bytes
       
   206     QTextCodec *m_codec;
       
   207 };
       
   208 
       
   209 QByteArray Releaser::originalBytes(const QString &str, bool isUtf8) const
       
   210 {
       
   211     if (str.isEmpty()) {
       
   212         // Do not use QByteArray() here as the result of the serialization
       
   213         // will be different.
       
   214         return QByteArray("");
       
   215     }
       
   216     if (isUtf8)
       
   217         return str.toUtf8();
       
   218     return m_codec ? m_codec->fromUnicode(str) : str.toLatin1();
       
   219 }
       
   220 
       
   221 uint Releaser::msgHash(const ByteTranslatorMessage &msg)
       
   222 {
       
   223     return elfHash(msg.sourceText() + msg.comment());
       
   224 }
       
   225 
       
   226 Prefix Releaser::commonPrefix(const ByteTranslatorMessage &m1, const ByteTranslatorMessage &m2)
       
   227 {
       
   228     if (msgHash(m1) != msgHash(m2))
       
   229         return NoPrefix;
       
   230     if (m1.context() != m2.context())
       
   231         return Hash;
       
   232     if (m1.sourceText() != m2.sourceText())
       
   233         return HashContext;
       
   234     if (m1.comment() != m2.comment())
       
   235         return HashContextSourceText;
       
   236     return HashContextSourceTextComment;
       
   237 }
       
   238 
       
   239 void Releaser::writeMessage(const ByteTranslatorMessage &msg, QDataStream &stream,
       
   240     TranslatorSaveMode mode, Prefix prefix) const
       
   241 {
       
   242     for (int i = 0; i < msg.translations().count(); ++i)
       
   243         stream << quint8(Tag_Translation) << msg.translations().at(i);
       
   244 
       
   245     if (mode == SaveEverything)
       
   246         prefix = HashContextSourceTextComment;
       
   247 
       
   248     // lrelease produces "wrong" QM files for QByteArrays that are .isNull().
       
   249     switch (prefix) {
       
   250     default:
       
   251     case HashContextSourceTextComment:
       
   252         stream << quint8(Tag_Comment) << msg.comment();
       
   253         // fall through
       
   254     case HashContextSourceText:
       
   255         stream << quint8(Tag_SourceText) << msg.sourceText();
       
   256         // fall through
       
   257     case HashContext:
       
   258         stream << quint8(Tag_Context) << msg.context();
       
   259         break;
       
   260     }
       
   261 
       
   262     stream << quint8(Tag_End);
       
   263 }
       
   264 
       
   265 
       
   266 bool Releaser::save(QIODevice *iod)
       
   267 {
       
   268     QDataStream s(iod);
       
   269     s.writeRawData((const char *)magic, MagicLength);
       
   270 
       
   271     if (!m_offsetArray.isEmpty()) {
       
   272         quint32 oas = quint32(m_offsetArray.size());
       
   273         s << quint8(Hashes) << oas;
       
   274         s.writeRawData(m_offsetArray.constData(), oas);
       
   275     }
       
   276     if (!m_messageArray.isEmpty()) {
       
   277         quint32 mas = quint32(m_messageArray.size());
       
   278         s << quint8(Messages) << mas;
       
   279         s.writeRawData(m_messageArray.constData(), mas);
       
   280     }
       
   281     if (!m_contextArray.isEmpty()) {
       
   282         quint32 cas = quint32(m_contextArray.size());
       
   283         s << quint8(Contexts) << cas;
       
   284         s.writeRawData(m_contextArray.constData(), cas);
       
   285     }
       
   286     if (!m_numerusRules.isEmpty()) {
       
   287         quint32 nrs = m_numerusRules.size();
       
   288         s << quint8(NumerusRules) << nrs;
       
   289         s.writeRawData(m_numerusRules.constData(), nrs);
       
   290     }
       
   291     return true;
       
   292 }
       
   293 
       
   294 void Releaser::squeeze(TranslatorSaveMode mode)
       
   295 {
       
   296     if (m_messages.isEmpty() && mode == SaveEverything)
       
   297         return;
       
   298 
       
   299     QMap<ByteTranslatorMessage, void *> messages = m_messages;
       
   300 
       
   301     // re-build contents
       
   302     m_messageArray.clear();
       
   303     m_offsetArray.clear();
       
   304     m_contextArray.clear();
       
   305     m_messages.clear();
       
   306 
       
   307     QMap<Offset, void *> offsets;
       
   308 
       
   309     QDataStream ms(&m_messageArray, QIODevice::WriteOnly);
       
   310     QMap<ByteTranslatorMessage, void *>::const_iterator it, next;
       
   311     int cpPrev = 0, cpNext = 0;
       
   312     for (it = messages.constBegin(); it != messages.constEnd(); ++it) {
       
   313         cpPrev = cpNext;
       
   314         next = it;
       
   315         ++next;
       
   316         if (next == messages.constEnd())
       
   317             cpNext = 0;
       
   318         else
       
   319             cpNext = commonPrefix(it.key(), next.key());
       
   320         offsets.insert(Offset(msgHash(it.key()), ms.device()->pos()), (void *)0);
       
   321         writeMessage(it.key(), ms, mode, Prefix(qMax(cpPrev, cpNext + 1)));
       
   322     }
       
   323 
       
   324     QMap<Offset, void *>::Iterator offset;
       
   325     offset = offsets.begin();
       
   326     QDataStream ds(&m_offsetArray, QIODevice::WriteOnly);
       
   327     while (offset != offsets.end()) {
       
   328         Offset k = offset.key();
       
   329         ++offset;
       
   330         ds << quint32(k.h) << quint32(k.o);
       
   331     }
       
   332 
       
   333     if (mode == SaveStripped) {
       
   334         QMap<QByteArray, int> contextSet;
       
   335         for (it = messages.constBegin(); it != messages.constEnd(); ++it)
       
   336             ++contextSet[it.key().context()];
       
   337 
       
   338         quint16 hTableSize;
       
   339         if (contextSet.size() < 200)
       
   340             hTableSize = (contextSet.size() < 60) ? 151 : 503;
       
   341         else if (contextSet.size() < 2500)
       
   342             hTableSize = (contextSet.size() < 750) ? 1511 : 5003;
       
   343         else
       
   344             hTableSize = (contextSet.size() < 10000) ? 15013 : 3 * contextSet.size() / 2;
       
   345 
       
   346         QMultiMap<int, QByteArray> hashMap;
       
   347         QMap<QByteArray, int>::const_iterator c;
       
   348         for (c = contextSet.constBegin(); c != contextSet.constEnd(); ++c)
       
   349             hashMap.insert(elfHash(c.key()) % hTableSize, c.key());
       
   350 
       
   351         /*
       
   352           The contexts found in this translator are stored in a hash
       
   353           table to provide fast lookup. The context array has the
       
   354           following format:
       
   355 
       
   356               quint16 hTableSize;
       
   357               quint16 hTable[hTableSize];
       
   358               quint8  contextPool[...];
       
   359 
       
   360           The context pool stores the contexts as Pascal strings:
       
   361 
       
   362               quint8  len;
       
   363               quint8  data[len];
       
   364 
       
   365           Let's consider the look-up of context "FunnyDialog".  A
       
   366           hash value between 0 and hTableSize - 1 is computed, say h.
       
   367           If hTable[h] is 0, "FunnyDialog" is not covered by this
       
   368           translator. Else, we check in the contextPool at offset
       
   369           2 * hTable[h] to see if "FunnyDialog" is one of the
       
   370           contexts stored there, until we find it or we meet the
       
   371           empty string.
       
   372         */
       
   373         m_contextArray.resize(2 + (hTableSize << 1));
       
   374         QDataStream t(&m_contextArray, QIODevice::WriteOnly);
       
   375 
       
   376         quint16 *hTable = new quint16[hTableSize];
       
   377         memset(hTable, 0, hTableSize * sizeof(quint16));
       
   378 
       
   379         t << hTableSize;
       
   380         t.device()->seek(2 + (hTableSize << 1));
       
   381         t << quint16(0); // the entry at offset 0 cannot be used
       
   382         uint upto = 2;
       
   383 
       
   384         QMap<int, QByteArray>::const_iterator entry = hashMap.constBegin();
       
   385         while (entry != hashMap.constEnd()) {
       
   386             int i = entry.key();
       
   387             hTable[i] = quint16(upto >> 1);
       
   388 
       
   389             do {
       
   390                 const char *con = entry.value().constData();
       
   391                 uint len = uint(entry.value().length());
       
   392                 len = qMin(len, 255u);
       
   393                 t << quint8(len);
       
   394                 t.writeRawData(con, len);
       
   395                 upto += 1 + len;
       
   396                 ++entry;
       
   397             } while (entry != hashMap.constEnd() && entry.key() == i);
       
   398             if (upto & 0x1) {
       
   399                 // offsets have to be even
       
   400                 t << quint8(0); // empty string
       
   401                 ++upto;
       
   402             }
       
   403         }
       
   404         t.device()->seek(2);
       
   405         for (int j = 0; j < hTableSize; j++)
       
   406             t << hTable[j];
       
   407         delete [] hTable;
       
   408 
       
   409         if (upto > 131072) {
       
   410             qWarning("Releaser::squeeze: Too many contexts");
       
   411             m_contextArray.clear();
       
   412         }
       
   413     }
       
   414 }
       
   415 
       
   416 void Releaser::insertInternal(const TranslatorMessage &message, bool forceComment, bool isUtf8)
       
   417 {
       
   418     ByteTranslatorMessage bmsg(originalBytes(message.context(), isUtf8),
       
   419                                originalBytes(message.sourceText(), isUtf8),
       
   420                                originalBytes(message.comment(), isUtf8),
       
   421                                message.translations());
       
   422     if (!forceComment) {
       
   423         ByteTranslatorMessage bmsg2(
       
   424                 bmsg.context(), bmsg.sourceText(), QByteArray(""), bmsg.translations());
       
   425         if (!m_messages.contains(bmsg2)) {
       
   426             m_messages.insert(bmsg2, 0);
       
   427             return;
       
   428         }
       
   429     }
       
   430     m_messages.insert(bmsg, 0);
       
   431 }
       
   432 
       
   433 void Releaser::insert(const TranslatorMessage &message, bool forceComment)
       
   434 {
       
   435     insertInternal(message, forceComment, message.isUtf8());
       
   436     if (message.isUtf8() && message.isNonUtf8())
       
   437         insertInternal(message, forceComment, false);
       
   438 }
       
   439 
       
   440 void Releaser::insertIdBased(const TranslatorMessage &message)
       
   441 {
       
   442     QStringList tlns = message.translations();
       
   443     if (message.type() == TranslatorMessage::Unfinished)
       
   444         for (int i = 0; i < tlns.size(); ++i)
       
   445             if (tlns.at(i).isEmpty())
       
   446                 tlns[i] = message.sourceText();
       
   447     ByteTranslatorMessage bmsg("", originalBytes(message.id(), false), "", tlns);
       
   448     m_messages.insert(bmsg, 0);
       
   449 }
       
   450 
       
   451 void Releaser::setNumerusRules(const QByteArray &rules)
       
   452 {
       
   453     m_numerusRules = rules;
       
   454 }
       
   455 
       
   456 static quint8 read8(const uchar *data)
       
   457 {
       
   458     return *data;
       
   459 }
       
   460 
       
   461 static quint32 read32(const uchar *data)
       
   462 {
       
   463     return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]);
       
   464 }
       
   465 
       
   466 static void fromBytes(const char *str, int len, QTextCodec *codec, QTextCodec *utf8Codec,
       
   467                       QString *out, QString *utf8Out,
       
   468                       bool *isSystem, bool *isUtf8, bool *needs8Bit)
       
   469 {
       
   470     for (int i = 0; i < len; ++i)
       
   471         if (str[i] & 0x80) {
       
   472             if (utf8Codec) {
       
   473                 QTextCodec::ConverterState cvtState;
       
   474                 *utf8Out = utf8Codec->toUnicode(str, len, &cvtState);
       
   475                 *isUtf8 = !cvtState.invalidChars;
       
   476             }
       
   477             QTextCodec::ConverterState cvtState;
       
   478             *out = codec->toUnicode(str, len, &cvtState);
       
   479             *isSystem = !cvtState.invalidChars;
       
   480             *needs8Bit = true;
       
   481             return;
       
   482         }
       
   483     *out = QString::fromLatin1(str, len);
       
   484     *isSystem = true;
       
   485     if (utf8Codec) {
       
   486         *utf8Out = *out;
       
   487         *isUtf8 = true;
       
   488     }
       
   489     *needs8Bit = false;
       
   490 }
       
   491 
       
   492 bool loadQM(Translator &translator, QIODevice &dev, ConversionData &cd)
       
   493 {
       
   494     QByteArray ba = dev.readAll();
       
   495     const uchar *data = (uchar*)ba.data();
       
   496     int len = ba.size();
       
   497     if (len < MagicLength || memcmp(data, magic, MagicLength) != 0) {
       
   498         cd.appendError(QLatin1String("QM-Format error: magic marker missing"));
       
   499         return false;
       
   500     }
       
   501 
       
   502     enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88 };
       
   503 
       
   504     // for squeezed but non-file data, this is what needs to be deleted
       
   505     const uchar *messageArray = 0;
       
   506     const uchar *offsetArray = 0;
       
   507     const uchar *contextArray = 0;
       
   508     const uchar *numerusRulesArray = 0;
       
   509     uint messageLength = 0;
       
   510     uint offsetLength = 0;
       
   511     uint contextLength = 0;
       
   512     uint numerusRulesLength = 0;
       
   513 
       
   514     bool ok = true;
       
   515     const uchar *end = data + len;
       
   516 
       
   517     data += MagicLength;
       
   518 
       
   519     while (data < end - 4) {
       
   520         quint8 tag = read8(data++);
       
   521         quint32 blockLen = read32(data);
       
   522         //qDebug() << "TAG:" << tag <<  "BLOCKLEN:" << blockLen;
       
   523         data += 4;
       
   524         if (!tag || !blockLen)
       
   525             break;
       
   526         if (data + blockLen > end) {
       
   527             ok = false;
       
   528             break;
       
   529         }
       
   530 
       
   531         if (tag == Contexts) {
       
   532             contextArray = data;
       
   533             contextLength = blockLen;
       
   534             //qDebug() << "CONTEXTS: " << contextLength << QByteArray((const char *)contextArray, contextLength).toHex();
       
   535         } else if (tag == Hashes) {
       
   536             offsetArray = data;
       
   537             offsetLength = blockLen;
       
   538             //qDebug() << "HASHES: " << offsetLength << QByteArray((const char *)offsetArray, offsetLength).toHex();
       
   539         } else if (tag == Messages) {
       
   540             messageArray = data;
       
   541             messageLength = blockLen;
       
   542             //qDebug() << "MESSAGES: " << messageLength << QByteArray((const char *)messageArray, messageLength).toHex();
       
   543         } else if (tag == NumerusRules) {
       
   544             numerusRulesArray = data;
       
   545             numerusRulesLength = blockLen;
       
   546             //qDebug() << "NUMERUSRULES: " << numerusRulesLength << QByteArray((const char *)numerusRulesArray, numerusRulesLength).toHex();
       
   547         }
       
   548 
       
   549         data += blockLen;
       
   550     }
       
   551 
       
   552 
       
   553     size_t numItems = offsetLength / (2 * sizeof(quint32));
       
   554     //qDebug() << "NUMITEMS: " << numItems;
       
   555 
       
   556     QTextCodec *codec = QTextCodec::codecForName(
       
   557         cd.m_codecForSource.isEmpty() ? QByteArray("Latin1") : cd.m_codecForSource);
       
   558     QTextCodec *utf8Codec = 0;
       
   559     if (codec->name() != "UTF-8")
       
   560         utf8Codec = QTextCodec::codecForName("UTF-8");
       
   561 
       
   562     QString strProN = QLatin1String("%n");
       
   563     QLocale::Language l;
       
   564     QLocale::Country c;
       
   565     Translator::languageAndCountry(translator.languageCode(), &l, &c);
       
   566     QStringList numerusForms;
       
   567     bool guessPlurals = true;
       
   568     if (getNumerusInfo(l, c, 0, &numerusForms))
       
   569         guessPlurals = (numerusForms.count() == 1);
       
   570 
       
   571     QString context, contextUtf8;
       
   572     bool contextIsSystem, contextIsUtf8, contextNeeds8Bit;
       
   573     QString sourcetext, sourcetextUtf8;
       
   574     bool sourcetextIsSystem, sourcetextIsUtf8, sourcetextNeeds8Bit;
       
   575     QString comment, commentUtf8;
       
   576     bool commentIsSystem, commentIsUtf8, commentNeeds8Bit;
       
   577     QStringList translations;
       
   578 
       
   579     for (const uchar *start = offsetArray; start != offsetArray + (numItems << 3); start += 8) {
       
   580         //quint32 hash = read32(start);
       
   581         quint32 ro = read32(start + 4);
       
   582         //qDebug() << "\nHASH:" << hash;
       
   583         const uchar *m = messageArray + ro;
       
   584 
       
   585         for (;;) {
       
   586             uchar tag = read8(m++);
       
   587             //qDebug() << "Tag:" << tag << " ADDR: " << m;
       
   588             switch(tag) {
       
   589             case Tag_End:
       
   590                 goto end;
       
   591             case Tag_Translation: {
       
   592                 int len = read32(m);
       
   593                 if (len % 1) {
       
   594                     cd.appendError(QLatin1String("QM-Format error"));
       
   595                     return false;
       
   596                 }
       
   597                 m += 4;
       
   598                 QString str = QString::fromUtf16((const ushort *)m, len/2);
       
   599                 if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
       
   600                     for (int i = 0; i < str.length(); ++i)
       
   601                         str[i] = QChar((str.at(i).unicode() >> 8) +
       
   602                             ((str.at(i).unicode() << 8) & 0xff00));
       
   603                 }
       
   604                 translations << str;
       
   605                 m += len;
       
   606                 break;
       
   607             }
       
   608             case Tag_Obsolete1:
       
   609                 m += 4;
       
   610                 //qDebug() << "OBSOLETE";
       
   611                 break;
       
   612             case Tag_SourceText: {
       
   613                 quint32 len = read32(m);
       
   614                 m += 4;
       
   615                 //qDebug() << "SOURCE LEN: " << len;
       
   616                 //qDebug() << "SOURCE: " << QByteArray((const char*)m, len);
       
   617                 fromBytes((const char*)m, len, codec, utf8Codec,
       
   618                           &sourcetext, &sourcetextUtf8,
       
   619                           &sourcetextIsSystem, &sourcetextIsUtf8, &sourcetextNeeds8Bit);
       
   620                 m += len;
       
   621                 break;
       
   622             }
       
   623             case Tag_Context: {
       
   624                 quint32 len = read32(m);
       
   625                 m += 4;
       
   626                 //qDebug() << "CONTEXT LEN: " << len;
       
   627                 //qDebug() << "CONTEXT: " << QByteArray((const char*)m, len);
       
   628                 fromBytes((const char*)m, len, codec, utf8Codec,
       
   629                           &context, &contextUtf8,
       
   630                           &contextIsSystem, &contextIsUtf8, &contextNeeds8Bit);
       
   631                 m += len;
       
   632                 break;
       
   633             }
       
   634             case Tag_Comment: {
       
   635                 quint32 len = read32(m);
       
   636                 m += 4;
       
   637                 //qDebug() << "COMMENT LEN: " << len;
       
   638                 //qDebug() << "COMMENT: " << QByteArray((const char*)m, len);
       
   639                 fromBytes((const char*)m, len, codec, utf8Codec,
       
   640                           &comment, &commentUtf8,
       
   641                           &commentIsSystem, &commentIsUtf8, &commentNeeds8Bit);
       
   642                 m += len;
       
   643                 break;
       
   644             }
       
   645             default:
       
   646                 //qDebug() << "UNKNOWN TAG" << tag;
       
   647                 break;
       
   648             }
       
   649         }
       
   650     end:;
       
   651         TranslatorMessage msg;
       
   652         msg.setType(TranslatorMessage::Finished);
       
   653         if (translations.count() > 1) {
       
   654             // If guessPlurals is not false here, plural form discard messages
       
   655             // will be spewn out later.
       
   656             msg.setPlural(true);
       
   657         } else if (guessPlurals) {
       
   658             // This might cause false positives, so it is a fallback only.
       
   659             if (sourcetext.contains(strProN))
       
   660                 msg.setPlural(true);
       
   661         }
       
   662         msg.setTranslations(translations);
       
   663         translations.clear();
       
   664         if (contextNeeds8Bit || sourcetextNeeds8Bit || commentNeeds8Bit) {
       
   665             if (utf8Codec && contextIsUtf8 && sourcetextIsUtf8 && commentIsUtf8) {
       
   666                 // The message is utf-8, but file is not.
       
   667                 msg.setUtf8(true);
       
   668                 msg.setContext(contextUtf8);
       
   669                 msg.setSourceText(sourcetextUtf8);
       
   670                 msg.setComment(commentUtf8);
       
   671                 translator.append(msg);
       
   672                 continue;
       
   673             }
       
   674             if (!(contextIsSystem && sourcetextIsSystem && commentIsSystem)) {
       
   675                 cd.appendError(QLatin1String(
       
   676                         "Cannot read file with specified input codec"));
       
   677                 return false;
       
   678             }
       
   679             // The message is 8-bit in the file's encoding (utf-8 or not).
       
   680         }
       
   681         msg.setContext(context);
       
   682         msg.setSourceText(sourcetext);
       
   683         msg.setComment(comment);
       
   684         translator.append(msg);
       
   685     }
       
   686     return ok;
       
   687 }
       
   688 
       
   689 
       
   690 
       
   691 static bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd)
       
   692 {
       
   693     Releaser releaser;
       
   694     QLocale::Language l;
       
   695     QLocale::Country c;
       
   696     Translator::languageAndCountry(translator.languageCode(), &l, &c);
       
   697     QByteArray rules;
       
   698     if (getNumerusInfo(l, c, &rules, 0))
       
   699         releaser.setNumerusRules(rules);
       
   700     releaser.setCodecName(translator.codecName());
       
   701 
       
   702     int finished = 0;
       
   703     int unfinished = 0;
       
   704     int untranslated = 0;
       
   705     int missingIds = 0;
       
   706     int droppedData = 0;
       
   707 
       
   708     for (int i = 0; i != translator.messageCount(); ++i) {
       
   709         const TranslatorMessage &msg = translator.message(i);
       
   710         TranslatorMessage::Type typ = msg.type();
       
   711         if (typ != TranslatorMessage::Obsolete) {
       
   712             if (cd.m_idBased && msg.id().isEmpty()) {
       
   713                 ++missingIds;
       
   714                 continue;
       
   715             }
       
   716             if (typ == TranslatorMessage::Unfinished) {
       
   717                 if (!cd.m_idBased && msg.translation().isEmpty()) {
       
   718                     ++untranslated;
       
   719                     continue;
       
   720                 } else {
       
   721                     if (cd.ignoreUnfinished())
       
   722                         continue;
       
   723                     ++unfinished;
       
   724                 }
       
   725             } else {
       
   726                 ++finished;
       
   727             }
       
   728             if (cd.m_idBased) {
       
   729                 if (!msg.context().isEmpty() || !msg.comment().isEmpty())
       
   730                     ++droppedData;
       
   731                 releaser.insertIdBased(msg);
       
   732             } else {
       
   733                 // Drop the comment in (context, sourceText, comment),
       
   734                 // unless the context is empty,
       
   735                 // unless (context, sourceText, "") already exists or
       
   736                 // unless we already dropped the comment of (context,
       
   737                 // sourceText, comment0).
       
   738                 bool forceComment =
       
   739                         msg.comment().isEmpty()
       
   740                         || msg.context().isEmpty()
       
   741                         || translator.contains(msg.context(), msg.sourceText(), QString());
       
   742                 releaser.insert(msg, forceComment);
       
   743             }
       
   744         }
       
   745     }
       
   746 
       
   747     if (missingIds)
       
   748         cd.appendError(QCoreApplication::translate("LRelease",
       
   749             "Dropped %n message(s) which had no ID.", 0,
       
   750             QCoreApplication::CodecForTr, missingIds));
       
   751     if (droppedData)
       
   752         cd.appendError(QCoreApplication::translate("LRelease",
       
   753             "Excess context/disambiguation dropped from %n message(s).", 0,
       
   754             QCoreApplication::CodecForTr, droppedData));
       
   755 
       
   756     releaser.squeeze(cd.m_saveMode);
       
   757     bool saved = releaser.save(&dev);
       
   758     if (saved && cd.isVerbose()) {
       
   759         int generatedCount = finished + unfinished;
       
   760         cd.appendError(QCoreApplication::translate("LRelease",
       
   761             "    Generated %n translation(s) (%1 finished and %2 unfinished)\n", 0,
       
   762             QCoreApplication::CodecForTr, generatedCount).arg(finished).arg(unfinished));
       
   763         if (untranslated)
       
   764             cd.appendError(QCoreApplication::translate("LRelease",
       
   765                 "    Ignored %n untranslated source text(s)\n", 0,
       
   766                 QCoreApplication::CodecForTr, untranslated));
       
   767     }
       
   768     return saved;
       
   769 }
       
   770 
       
   771 int initQM()
       
   772 {
       
   773     Translator::FileFormat format;
       
   774 
       
   775     format.extension = QLatin1String("qm");
       
   776     format.description = QObject::tr("Compiled Qt translations");
       
   777     format.fileType = Translator::FileFormat::TranslationBinary;
       
   778     format.priority = 0;
       
   779     format.loader = &loadQM;
       
   780     format.saver = &saveQM;
       
   781     Translator::registerFileFormat(format);
       
   782 
       
   783     return 1;
       
   784 }
       
   785 
       
   786 Q_CONSTRUCTOR_FUNCTION(initQM)
       
   787 
       
   788 QT_END_NAMESPACE