tools/linguist/shared/translatormessage.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 "translatormessage.h"
       
    43 
       
    44 #include <qplatformdefs.h>
       
    45 
       
    46 #ifndef QT_NO_TRANSLATION
       
    47 
       
    48 #include <QDataStream>
       
    49 #include <QDebug>
       
    50 
       
    51 #include <stdlib.h>
       
    52 
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 TranslatorMessage::TranslatorMessage()
       
    57   : m_lineNumber(-1), m_type(Unfinished), m_utf8(false), m_nonUtf8(false), m_plural(false)
       
    58 {
       
    59 }
       
    60 
       
    61 TranslatorMessage::TranslatorMessage(const QString &context,
       
    62     const QString &sourceText, const QString &comment,
       
    63     const QString &userData,
       
    64     const QString &fileName, int lineNumber, const QStringList &translations,
       
    65     Type type, bool plural)
       
    66   : m_context(context), m_sourcetext(sourceText), m_comment(comment),
       
    67     m_userData(userData),
       
    68     m_translations(translations), m_fileName(fileName), m_lineNumber(lineNumber),
       
    69     m_type(type), m_utf8(false), m_nonUtf8(false), m_plural(plural)
       
    70 {
       
    71 }
       
    72 
       
    73 void TranslatorMessage::addReference(const QString &fileName, int lineNumber)
       
    74 {
       
    75     if (m_fileName.isEmpty()) {
       
    76         m_fileName = fileName;
       
    77         m_lineNumber = lineNumber;
       
    78     } else {
       
    79         m_extraRefs.append(Reference(fileName, lineNumber));
       
    80     }
       
    81 }
       
    82 
       
    83 void TranslatorMessage::addReferenceUniq(const QString &fileName, int lineNumber)
       
    84 {
       
    85     if (m_fileName.isEmpty()) {
       
    86         m_fileName = fileName;
       
    87         m_lineNumber = lineNumber;
       
    88     } else {
       
    89         if (fileName == m_fileName && lineNumber == m_lineNumber)
       
    90             return;
       
    91         if (!m_extraRefs.isEmpty()) // Rather common case, so special-case it
       
    92             foreach (const Reference &ref, m_extraRefs)
       
    93                 if (fileName == ref.fileName() && lineNumber == ref.lineNumber())
       
    94                     return;
       
    95         m_extraRefs.append(Reference(fileName, lineNumber));
       
    96     }
       
    97 }
       
    98 
       
    99 void TranslatorMessage::clearReferences()
       
   100 {
       
   101     m_fileName.clear();
       
   102     m_lineNumber = -1;
       
   103     m_extraRefs.clear();
       
   104 }
       
   105 
       
   106 void TranslatorMessage::setReferences(const TranslatorMessage::References &refs0)
       
   107 {
       
   108     if (!refs0.isEmpty()) {
       
   109         References refs = refs0;
       
   110         const Reference &ref = refs.takeFirst();
       
   111         m_fileName = ref.fileName();
       
   112         m_lineNumber = ref.lineNumber();
       
   113         m_extraRefs = refs;
       
   114     } else {
       
   115         clearReferences();
       
   116     }
       
   117 }
       
   118 
       
   119 TranslatorMessage::References TranslatorMessage::allReferences() const
       
   120 {
       
   121     References refs;
       
   122     if (!m_fileName.isEmpty()) {
       
   123         refs.append(Reference(m_fileName, m_lineNumber));
       
   124         refs += m_extraRefs;
       
   125     }
       
   126     return refs;
       
   127 }
       
   128 
       
   129 static bool needs8BitHelper(const QString &ba)
       
   130 {
       
   131     for (int i = ba.size(); --i >= 0; )
       
   132         if (ba.at(i).unicode() >= 0x80)
       
   133             return true;
       
   134     return false;
       
   135 }
       
   136 
       
   137 bool TranslatorMessage::needs8Bit() const
       
   138 {
       
   139     //dump();
       
   140     return needs8BitHelper(m_sourcetext)
       
   141         || needs8BitHelper(m_comment)
       
   142         || needs8BitHelper(m_context);
       
   143 }
       
   144 
       
   145 
       
   146 bool TranslatorMessage::operator==(const TranslatorMessage& m) const
       
   147 {
       
   148     static QString msgIdPlural = QLatin1String("po-msgid_plural");
       
   149 
       
   150     // Special treatment for context comments (empty source).
       
   151     return (m_context == m.m_context)
       
   152         && m_sourcetext == m.m_sourcetext
       
   153         && m_extra[msgIdPlural] == m.m_extra[msgIdPlural]
       
   154         && m_id == m.m_id
       
   155         && (m_sourcetext.isEmpty() || m_comment == m.m_comment);
       
   156 }
       
   157 
       
   158 
       
   159 bool TranslatorMessage::operator<(const TranslatorMessage& m) const
       
   160 {
       
   161     if (m_context != m.m_context)
       
   162         return m_context < m.m_context;
       
   163     if (m_sourcetext != m.m_sourcetext)
       
   164         return m_sourcetext < m.m_sourcetext;
       
   165     if (m_comment != m.m_comment)
       
   166         return m_comment < m.m_comment;
       
   167     return m_id < m.m_id;
       
   168 }
       
   169 
       
   170 int qHash(const TranslatorMessage &msg)
       
   171 {
       
   172     return
       
   173         qHash(msg.context()) ^
       
   174         qHash(msg.sourceText()) ^
       
   175         qHash(msg.extra(QLatin1String("po-msgid_plural"))) ^
       
   176         qHash(msg.comment()) ^
       
   177         qHash(msg.id());
       
   178 }
       
   179 
       
   180 bool TranslatorMessage::hasExtra(const QString &key) const
       
   181 {
       
   182     return m_extra.contains(key);
       
   183 }
       
   184 
       
   185 QString TranslatorMessage::extra(const QString &key) const
       
   186 {
       
   187     return m_extra[key];
       
   188 }
       
   189 
       
   190 void TranslatorMessage::setExtra(const QString &key, const QString &value)
       
   191 {
       
   192     m_extra[key] = value;
       
   193 }
       
   194 
       
   195 void TranslatorMessage::unsetExtra(const QString &key)
       
   196 {
       
   197     m_extra.remove(key);
       
   198 }
       
   199 
       
   200 void TranslatorMessage::dump() const
       
   201 {
       
   202     qDebug()
       
   203         << "\nId                : " << m_id
       
   204         << "\nContext           : " << m_context
       
   205         << "\nSource            : " << m_sourcetext
       
   206         << "\nComment           : " << m_comment
       
   207         << "\nUserData          : " << m_userData
       
   208         << "\nExtraComment      : " << m_extraComment
       
   209         << "\nTranslatorComment : " << m_translatorComment
       
   210         << "\nTranslations      : " << m_translations
       
   211         << "\nFileName          : " << m_fileName
       
   212         << "\nLineNumber        : " << m_lineNumber
       
   213         << "\nType              : " << m_type
       
   214         << "\nPlural            : " << m_plural
       
   215         << "\nExtra             : " << m_extra;
       
   216 }
       
   217 
       
   218 
       
   219 QT_END_NAMESPACE
       
   220 
       
   221 #endif // QT_NO_TRANSLATION