src/versit/qversitreader_p.h
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 Mobility Components.
       
     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 
       
    43 #ifndef QVERSITREADER_P_H
       
    44 #define QVERSITREADER_P_H
       
    45 
       
    46 //
       
    47 //  W A R N I N G
       
    48 //  -------------
       
    49 //
       
    50 // This file is not part of the Qt API.  It exists purely as an
       
    51 // implementation detail.  This header file may change from version to
       
    52 // version without notice, or even be removed.
       
    53 //
       
    54 // We mean it.
       
    55 //
       
    56 
       
    57 #include "qmobilityglobal.h"
       
    58 #include "qversitreader.h"
       
    59 #include "qversitdocument.h"
       
    60 #include "qversitproperty.h"
       
    61 #include "versitutils_p.h"
       
    62 
       
    63 #include <QObject>
       
    64 #include <QThread>
       
    65 #include <QByteArray>
       
    66 #include <QIODevice>
       
    67 #include <QList>
       
    68 #include <QPointer>
       
    69 #include <QScopedPointer>
       
    70 #include <QByteArray>
       
    71 #include <QMutex>
       
    72 #include <QWaitCondition>
       
    73 #include <QPair>
       
    74 #include <QHash>
       
    75 
       
    76 QT_BEGIN_NAMESPACE
       
    77 class QBuffer;
       
    78 QT_END_NAMESPACE
       
    79 
       
    80 QTM_BEGIN_NAMESPACE
       
    81 
       
    82 // The maximum number of bytes allowed to stay in memory after being read.  The smaller this is,
       
    83 // the more time spent moving bytes around.  The larger it is, the more memory is wasted.
       
    84 static const int MAX_OLD_BYTES_TO_KEEP = 8192;
       
    85 
       
    86 class Q_AUTOTEST_EXPORT VersitCursor
       
    87 {
       
    88 public:
       
    89     VersitCursor() : position(-1), selection(-1) {}
       
    90     explicit VersitCursor(const QByteArray& d) :data(d), position(0), selection(0) {}
       
    91     QByteArray data;
       
    92     int position;
       
    93     int selection;
       
    94 
       
    95     void setData(const QByteArray& d) {data = d; position = selection = 0;}
       
    96     void setPosition(int pos) {position = pos; selection = qMax(pos, selection);}
       
    97     void setSelection(int pos) {selection = qMax(pos, position);}
       
    98     void dropOldData()
       
    99     {
       
   100         if (position > MAX_OLD_BYTES_TO_KEEP && selection >= position) {
       
   101             data.remove(0, position);
       
   102             selection -= position;
       
   103             position = 0;
       
   104         }
       
   105     }
       
   106 };
       
   107 
       
   108 class Q_AUTOTEST_EXPORT LineReader
       
   109 {
       
   110 public:
       
   111     LineReader(QIODevice* device, QTextCodec* codec, int chunkSize = 1000);
       
   112     VersitCursor readLine();
       
   113     int odometer();
       
   114     bool atEnd();
       
   115     QTextCodec* codec();
       
   116 
       
   117 private:
       
   118     bool tryReadLine(VersitCursor& cursor, bool atEnd);
       
   119 
       
   120     QIODevice* mDevice;
       
   121     QTextCodec* mCodec;
       
   122     int mChunkSize; // How many bytes to read in one go.
       
   123     QList<QByteArrayMatcher> mCrlfList;
       
   124     VersitCursor mBuffer;
       
   125     int mOdometer;
       
   126     int mSearchFrom;
       
   127 };
       
   128 
       
   129 class Q_AUTOTEST_EXPORT QVersitReaderPrivate : public QThread
       
   130 {
       
   131     Q_OBJECT
       
   132 
       
   133 public: // Constructors and destructor
       
   134     QVersitReaderPrivate();
       
   135     ~QVersitReaderPrivate();
       
   136     void init(QVersitReader* reader);
       
   137 
       
   138 signals:
       
   139     void stateChanged(QVersitReader::State state);
       
   140     void resultsAvailable();
       
   141 
       
   142 protected: // From QThread
       
   143      void run();
       
   144 
       
   145 public: // New functions
       
   146     void read();
       
   147 
       
   148     // mutexed getters and setters.
       
   149     void setState(QVersitReader::State);
       
   150     QVersitReader::State state() const;
       
   151     void setError(QVersitReader::Error);
       
   152     QVersitReader::Error error() const;
       
   153     void setCanceling(bool cancelling);
       
   154     bool isCanceling();
       
   155 
       
   156     bool parseVersitDocument(LineReader& device,
       
   157                              QVersitDocument& document,
       
   158                              bool foundBegin = false);
       
   159 
       
   160     QVersitProperty parseNextVersitProperty(
       
   161         QVersitDocument::VersitType versitType,
       
   162         LineReader& lineReader);
       
   163 
       
   164     void parseVCard21Property(
       
   165         VersitCursor& text,
       
   166         QVersitProperty& property,
       
   167         LineReader& lineReader);
       
   168 
       
   169     void parseVCard30Property(
       
   170         VersitCursor& text,
       
   171         QVersitProperty& property,
       
   172         LineReader& lineReader);
       
   173 
       
   174     bool setVersionFromProperty(
       
   175         QVersitDocument& document,
       
   176         const QVersitProperty& property) const;
       
   177 
       
   178     bool unencode(
       
   179         QByteArray& value,
       
   180         VersitCursor& cursor,
       
   181         QVersitProperty& property,
       
   182         LineReader& lineReader) const;
       
   183 
       
   184     QString decodeCharset(
       
   185         const QByteArray& value,
       
   186         QVersitProperty& property,
       
   187         QTextCodec* defaultCodec,
       
   188         QTextCodec** codec) const;
       
   189 
       
   190     void decodeQuotedPrintable(QByteArray& text) const;
       
   191 
       
   192 
       
   193     /* These functions operate on a cursor describing a single line */
       
   194     QPair<QStringList,QString> extractPropertyGroupsAndName(VersitCursor& line, QTextCodec* codec)
       
   195             const;
       
   196     QByteArray extractPropertyValue(VersitCursor& line) const;
       
   197     QMultiHash<QString,QString> extractVCard21PropertyParams(VersitCursor& line, QTextCodec* codec)
       
   198             const;
       
   199     QMultiHash<QString,QString> extractVCard30PropertyParams(VersitCursor& line, QTextCodec* codec)
       
   200             const;
       
   201 
       
   202     // "Private" functions
       
   203     QList<QByteArray> extractParams(VersitCursor& line, QTextCodec *codec) const;
       
   204     QList<QByteArray> extractParts(const QByteArray& text, const QByteArray& separator,
       
   205                                    QTextCodec *codec) const;
       
   206     QByteArray extractPart(const QByteArray& text, int startPosition, int length=-1) const;
       
   207     QString paramName(const QByteArray& parameter, QTextCodec* codec) const;
       
   208     QString paramValue(const QByteArray& parameter, QTextCodec* codec) const;
       
   209     static bool containsAt(const QByteArray& text, const QByteArray& ba, int index);
       
   210     bool splitStructuredValue(QVersitProperty& property,
       
   211                               bool hasEscapedBackslashes) const;
       
   212     static QStringList splitValue(const QString& string,
       
   213                                   const QChar& sep,
       
   214                                   QString::SplitBehavior behaviour,
       
   215                                   bool hasEscapedBackslashes);
       
   216     static void removeBackSlashEscaping(QString& text);
       
   217 
       
   218 public: // Data
       
   219     /* key is the document type and property name, value is the type of property it is.
       
   220        If there is no entry, assume it is a PlainType */
       
   221     QHash<QPair<QVersitDocument::VersitType,QString>, QVersitProperty::ValueType> mValueTypeMap;
       
   222     QPointer<QIODevice> mIoDevice;
       
   223     QScopedPointer<QBuffer> mInputBytes; // Holds the data set by setData()
       
   224     QList<QVersitDocument> mVersitDocuments;
       
   225     int mDocumentNestingLevel; // Depth in parsing nested Versit documents
       
   226     QTextCodec* mDefaultCodec;
       
   227     QVersitReader::State mState;
       
   228     QVersitReader::Error mError;
       
   229     bool mIsCanceling;
       
   230     mutable QMutex mMutex;
       
   231 };
       
   232 
       
   233 QTM_END_NAMESPACE
       
   234 
       
   235 #endif // QVERSITREADER_P_H