tools/linguist/shared/translatormessage.h
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 #ifndef TRANSLATORMESSAGE_H
       
    43 #define TRANSLATORMESSAGE_H
       
    44 
       
    45 #ifndef QT_NO_TRANSLATION
       
    46 
       
    47 #include <QString>
       
    48 #include <QStringList>
       
    49 #include <QHash>
       
    50 
       
    51 
       
    52 QT_BEGIN_NAMESPACE
       
    53 
       
    54 enum TranslatorSaveMode { SaveEverything, SaveStripped };
       
    55 
       
    56 class TranslatorMessage
       
    57 {
       
    58 public:
       
    59     enum Type { Unfinished, Finished, Obsolete };
       
    60     typedef QHash<QString, QString> ExtraData;
       
    61     class Reference
       
    62     {
       
    63         QString m_fileName;
       
    64         int m_lineNumber;
       
    65     public:
       
    66         Reference(const QString &n, int l) : m_fileName(n), m_lineNumber(l) {}
       
    67         bool operator==(const Reference &other) const
       
    68             { return fileName() == other.fileName() && lineNumber() == other.lineNumber(); }
       
    69         QString fileName() const { return m_fileName; }
       
    70         int lineNumber() const { return m_lineNumber; }
       
    71     };
       
    72     typedef QList<Reference> References;
       
    73 
       
    74     TranslatorMessage();
       
    75     TranslatorMessage(const QString &context, const QString &sourceText,
       
    76         const QString &comment, const QString &userData,
       
    77         const QString &fileName, int lineNumber,
       
    78         const QStringList &translations = QStringList(),
       
    79         Type type = Unfinished, bool plural = false);
       
    80 
       
    81     uint hash() const;
       
    82 
       
    83     QString id() const { return m_id; }
       
    84     void setId(const QString &id) { m_id = id; }
       
    85 
       
    86     QString context() const { return m_context; }
       
    87     void setContext(const QString &context) { m_context = context; }
       
    88 
       
    89     QString sourceText() const { return m_sourcetext; }
       
    90     void setSourceText(const QString &sourcetext) { m_sourcetext = sourcetext; }
       
    91     QString oldSourceText() const { return m_oldsourcetext; }
       
    92     void setOldSourceText(const QString &oldsourcetext) { m_oldsourcetext = oldsourcetext; }
       
    93 
       
    94     QString comment() const { return m_comment; }
       
    95     void setComment(const QString &comment) { m_comment = comment; }
       
    96     QString oldComment() const { return m_oldcomment; }
       
    97     void setOldComment(const QString &oldcomment) { m_oldcomment = oldcomment; }
       
    98 
       
    99     QStringList translations() const { return m_translations; }
       
   100     void setTranslations(const QStringList &translations) { m_translations = translations; }
       
   101     QString translation() const { return m_translations.value(0); }
       
   102     void setTranslation(const QString &translation) { m_translations = QStringList(translation); }
       
   103     void appendTranslation(const QString &translation) { m_translations.append(translation); }
       
   104     bool isTranslated() const
       
   105     {
       
   106         foreach (const QString &trans, m_translations)
       
   107             if (!trans.isEmpty())
       
   108                 return true;
       
   109         return false;
       
   110     }
       
   111 
       
   112     bool operator==(const TranslatorMessage& m) const;
       
   113     bool operator<(const TranslatorMessage& m) const;
       
   114 
       
   115     QString fileName() const { return m_fileName; }
       
   116     void setFileName(const QString &fileName) { m_fileName = fileName; }
       
   117     int lineNumber() const { return m_lineNumber; }
       
   118     void setLineNumber(int lineNumber) { m_lineNumber = lineNumber; }
       
   119     void clearReferences();
       
   120     void setReferences(const References &refs);
       
   121     void addReference(const QString &fileName, int lineNumber);
       
   122     void addReference(const Reference &ref) { addReference(ref.fileName(), ref.lineNumber()); }
       
   123     void addReferenceUniq(const QString &fileName, int lineNumber);
       
   124     References extraReferences() const { return m_extraRefs; }
       
   125     References allReferences() const;
       
   126     QString userData() const { return m_userData; }
       
   127     void setUserData(const QString &userData) { m_userData = userData; }
       
   128     QString extraComment() const { return m_extraComment; }
       
   129     void setExtraComment(const QString &extraComment) { m_extraComment = extraComment; }
       
   130     QString translatorComment() const { return m_translatorComment; }
       
   131     void setTranslatorComment(const QString &translatorComment) { m_translatorComment = translatorComment; }
       
   132 
       
   133     bool isNull() const { return m_sourcetext.isNull() && m_lineNumber == -1 && m_translations.isEmpty(); }
       
   134 
       
   135     Type type() const { return m_type; }
       
   136     void setType(Type t) { m_type = t; }
       
   137     bool isUtf8() const { return m_utf8; } // codecForTr override
       
   138     void setUtf8(bool on) { m_utf8 = on; }
       
   139     bool isNonUtf8() const { return m_nonUtf8; } // codecForTr override
       
   140     void setNonUtf8(bool on) { m_nonUtf8 = on; }
       
   141     bool isPlural() const { return m_plural; }
       
   142     void setPlural(bool isplural) { m_plural = isplural; }
       
   143 
       
   144     // note: use '<fileformat>:' as prefix for file format specific members,
       
   145     // e.g. "po-msgid_plural"
       
   146     QString extra(const QString &ba) const;
       
   147     void setExtra(const QString &ba, const QString &var);
       
   148     bool hasExtra(const QString &ba) const;
       
   149     const ExtraData &extras() const { return m_extra; }
       
   150     void setExtras(const ExtraData &extras) { m_extra = extras; }
       
   151     void unsetExtra(const QString &key);
       
   152 
       
   153     bool needs8Bit() const;
       
   154     void dump() const;
       
   155 
       
   156 private:
       
   157     QString     m_id;
       
   158     QString     m_context;
       
   159     QString     m_sourcetext;
       
   160     QString     m_oldsourcetext;
       
   161     QString     m_comment;
       
   162     QString     m_oldcomment;
       
   163     QString     m_userData;
       
   164     ExtraData   m_extra; // PO flags, PO plurals
       
   165     QString     m_extraComment;
       
   166     QString     m_translatorComment;
       
   167     QStringList m_translations;
       
   168     QString     m_fileName;
       
   169     int         m_lineNumber;
       
   170     References  m_extraRefs;
       
   171 
       
   172     Type m_type;
       
   173     bool m_utf8;
       
   174     bool m_nonUtf8;
       
   175     bool m_plural;
       
   176 };
       
   177 
       
   178 Q_DECLARE_TYPEINFO(TranslatorMessage, Q_MOVABLE_TYPE);
       
   179 
       
   180 int qHash(const TranslatorMessage &msg);
       
   181 
       
   182 struct TranslatorMessagePtr {
       
   183     TranslatorMessagePtr(const TranslatorMessage &tm)
       
   184     {
       
   185         ptr = &tm;
       
   186     }
       
   187 
       
   188     inline const TranslatorMessage *operator->() const
       
   189     {
       
   190         return ptr;
       
   191     }
       
   192 
       
   193     const TranslatorMessage *ptr;
       
   194 };
       
   195 
       
   196 Q_DECLARE_TYPEINFO(TranslatorMessagePtr, Q_MOVABLE_TYPE);
       
   197 
       
   198 inline int qHash(TranslatorMessagePtr tmp)
       
   199 {
       
   200     return qHash(*tmp.ptr);
       
   201 }
       
   202 
       
   203 inline bool operator==(TranslatorMessagePtr tmp1, TranslatorMessagePtr tmp2)
       
   204 {
       
   205     return *tmp1.ptr == *tmp2.ptr;
       
   206 }
       
   207 
       
   208 QT_END_NAMESPACE
       
   209 
       
   210 #endif // QT_NO_TRANSLATION
       
   211 
       
   212 #endif // TRANSLATORMESSAGE_H